No description
  • MLIR 44.8%
  • C++ 41.6%
  • Starlark 6.9%
  • Python 6.4%
  • Dockerfile 0.2%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Ze Zhang da237d0e1d
LLVM / Torch-Mlir Upgrade (#8)
- Remove StableHLO dependency
- Apply patches to build torch-mlir without stablehlo and stablehlo lit
tests
- Update APIs realted to` isa<>`, `cast<>`, and `dyn_cast<>`
- Add a new EliminateUnusedTorchOpsPass to remove unused torch ops in
torch-to-tcp (see example below):


```
module {
  func.func @func_main(%arg0: !torch.vtensor<[?,3],f32>, %arg1: !torch.vtensor<[?,3],f32>) -> !torch.vtensor<[?,3],f32> {
    %int2 = torch.constant.int 2
    %int0 = torch.constant.int 0
    %0 = torch.symbolic_int "s35" {min_val = 0, max_val = 9223372036854775807} : !torch.int
    torch.bind_symbolic_shape %arg0, [%0], affine_map<()[s0] -> (s0, 3)> : !torch.vtensor<[?,3],f32>
    torch.bind_symbolic_shape %arg1, [%0], affine_map<()[s0] -> (s0, 3)> : !torch.vtensor<[?,3],f32>
    %1 = torch.aten.size.int %arg0, %int0 : !torch.vtensor<[?,3],f32>, !torch.int -> !torch.int
    %2 = torch.aten.size.int %arg1, %int0 : !torch.vtensor<[?,3],f32>, !torch.int -> !torch.int
    %3 = torch.aten.sub.Tensor %arg0, %arg1, %int2 : !torch.vtensor<[?,3],f32>, !torch.vtensor<[?,3],f32>, !torch.int -> !torch.vtensor<[?,3],f32>
    torch.bind_symbolic_shape %3, [%0], affine_map<()[s0] -> (s0, 3)> : !torch.vtensor<[?,3],f32>
    %4 = torch.aten.eq.int %1, %2 : !torch.int, !torch.int -> !torch.bool
    %5 = torch.aten.Int.bool %4 : !torch.bool -> !torch.int
    %6 = torch.aten.Bool.int %5 : !torch.int -> !torch.bool
    torch.runtime.assert %6, "Runtime assertion failed for expression Eq(s35, s58) on node 'eq_2'"
    return %3 : !torch.vtensor<[?,3],f32>
  }
}
```

where the `torch.runtime.assert` and related checking ops can be
removed.
2025-06-10 09:14:20 -07:00
.github LLVM / Torch-Mlir Upgrade (#8) 2025-06-10 09:14:20 -07:00
.vscode Support clangd for code completion, navigation and insights (#30) 2024-01-19 14:39:52 -08:00
docker [NFC] Dockerfile + Bazel Fixes (#5) 2025-02-21 19:00:25 -08:00
docs Add op for Gather (#68) 2024-04-27 23:46:17 -07:00
include/mlir-tcp LLVM / Torch-Mlir Upgrade (#8) 2025-06-10 09:14:20 -07:00
lib LLVM / Torch-Mlir Upgrade (#8) 2025-06-10 09:14:20 -07:00
test LLVM / Torch-Mlir Upgrade (#8) 2025-06-10 09:14:20 -07:00
third_party LLVM / Torch-Mlir Upgrade (#8) 2025-06-10 09:14:20 -07:00
tools LLVM / Torch-Mlir Upgrade (#8) 2025-06-10 09:14:20 -07:00
.bazelignore LLVM / Torch-Mlir Upgrade (#8) 2025-06-10 09:14:20 -07:00
.bazelrc [NFC] Project maintenance (#65) 2024-04-09 01:22:23 -07:00
.clang-format [NFC] Create include/mlir-tcp and move headers over (#12) 2023-10-23 20:32:04 -07:00
.clang-format-ignore [LLVM Integrate] Bump llvm, torch-mlir (#105) 2025-01-24 18:35:51 -08:00
.gitignore LLVM / Torch-Mlir Upgrade (#8) 2025-06-10 09:14:20 -07:00
BUILD LLVM / Torch-Mlir Upgrade (#8) 2025-06-10 09:14:20 -07:00
deps.bzl LLVM / Torch-Mlir Upgrade (#8) 2025-06-10 09:14:20 -07:00
LICENSE Update LICENSE to Apache v2.0 with LLVM exceptions (#4) 2023-10-10 19:43:59 -07:00
local_repos.bzl LLVM / Torch-Mlir Upgrade (#8) 2025-06-10 09:14:20 -07:00
README.md LLVM / Torch-Mlir Upgrade (#8) 2025-06-10 09:14:20 -07:00
requirements.txt [NFC] Bump torch-mlir and torch pip deps (#99) 2024-10-02 21:28:16 -07:00
requirements_lock.txt LLVM / Torch-Mlir Upgrade (#8) 2025-06-10 09:14:20 -07:00
WORKSPACE [NFC] Miscellaneous Maintenance (#45) 2024-02-23 14:10:34 -08:00

Tensor Compute Primitives

Mid-level intermediate representation for machine learning programs.

Bazel Build and Test (mlir-tcp)

🚧 This project is under active development (WIP).

Project Communication

Developer Guide

To build TCP using Bazel, follow these steps:

  1. (Optional) For a quick start, launch an interactive docker container with clang (and lld) pre-installed:
./docker/run_docker.sh
  1. You can now build tcp-opt by running:
bazel build //:tcp-opt
  1. To run TCP lit and aot compile tests:
bazel test //...

We welcome contributions to mlir-tcp. When authoring new TCP ops with dialect conversions from/to Torch and Linalg, please include lit tests for dialect and conversions, as well as aot_compile generated e2e integration tests. Lastly, please finalize your PR with clang-format, black and bazel buildifier to ensure the C++/python sources and BUILD files are formatted consistently:

# clang-format
find . -type f -name "*.cpp" -o -name "*.h" | xargs clang-format -i

# black
black .

# buildifer
bazel run //tools/buildifier:buildifier

To enable clangd (for code completion, navigation and insights), generate the compilation database using bazel-compile-commands-extractor:

bazel build //...

bazel run //tools/clangd:refresh_compile_commands

When run successfully, a compile_commands.json is generated at the workspace root (and refreshed upon re-runs). If you're using VSCode, just hit CMD+SHIFT+P and select clangd: Restart language server to start clangd. Note that this only works for non-docker builds at the moment.

When bumping upstream dependencies (LLVM, Torch-MLIR), you may validate the set of "green commits" by running the corresponding third-party tests:

bazel test @llvm-project//mlir/...
bazel test @torch-mlir//...

The following CI workflows are automatically triggered anytime upstream dependencies (deps.bzl) are updated:

  • Bazel Build and Test (llvm-project)
  • Bazel Build and Test (torch-mlir)

To use newer torch-mlir and/or torch python packages in our hermetic python sandbox, just regenerate requirements_lock.txt as follows:

truncate -s 0 requirements_lock.txt
bazel run //tools/pip:requirements.update

Debugging Guide

Below are some standard techniques for debugging your compilation process, assuming you've reduced it to a form that can be reproduced with tcp-opt. For MLIR-specific debugging tips, refer here.

printf debugging

Printing to stdout/stderr works as usual:

op.emitWarning() << "HERE: " << myVariable;      // preferred for op/loc diagnostics

llvm::errs() << "HERE: " << myVariable << "\n";  // alternative

You can also hook into the LLVM_DEBUG macro:

#include "llvm/Support/Debug.h"

#define DEBUG_TYPE "foo"
LLVM_DEBUG(llvm::dbgs() << "This only shows up when -debug or -debug-only=foo is provided.\n");
#undef DEBUG_TYPE

#define DEBUG_TYPE "bar"
LLVM_DEBUG(llvm::dbgs() << "This only shows up when -debug or -debug-only=bar is provided.\n");
#undef DEBUG_TYPE

Then run with the -debug-only=foo,bar flag to cuts out messages that aren't associated with the passed DEBUG_TYPEs.

bazel run //:tcp-opt -- --some-pass `pwd`/test.mlir -debug-only=foo,bar

gdb debugging

To debug tcp-opt with gdb:

bazel build --config=gdb //:tcp-opt

gdb --args bazel-bin/tcp-opt -h

For help with gdb commands please refer to gdb cheat sheet.

aot_compile debugging

Refer this README for a step-by-step guide to debugging an end-to-end compilation pipeline using the AOT Compile framework.

Enable llvm-symbolizer

If you get a stack dump without any symbol names:

Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  tcp-opt   0x000055ac1c9c0c1d
1  tcp-opt   0x000055ac1c9c110b
2  tcp-opt   0x000055ac1c9be846
3  tcp-opt   0x000055ac1c9c1855
4  libc.so.6 0x00007f7011c6a520
...

Do this and re-run:

bazel build @llvm-project//llvm:llvm-symbolizer
export LLVM_SYMBOLIZER_PATH=`pwd`/bazel-bin/external/llvm-project/llvm/llvm-symbolizer