From e2f5e5b765c8cf0d9e74739687588dc903fe3cfd Mon Sep 17 00:00:00 2001 From: Sihao Liu Date: Tue, 15 Jul 2025 12:23:17 -0700 Subject: [PATCH 01/17] Bump Bazel version to 8.3.1 and migrate to bzlmod --- .bazelrc | 55 ++++++ .bazelversion | 2 +- .gitignore | 1 + CLAUDE.md | 88 ++++++++++ MODULE.bazel | 70 ++++++++ README.md | 21 +++ WORKSPACE | 233 +------------------------ extensions.bzl | 44 +++++ lib/Analysis/ReduceNoiseAnalysis/BUILD | 4 +- 9 files changed, 285 insertions(+), 233 deletions(-) create mode 100644 CLAUDE.md create mode 100644 MODULE.bazel create mode 100644 extensions.bzl diff --git a/.bazelrc b/.bazelrc index 2c2270d..692e959 100644 --- a/.bazelrc +++ b/.bazelrc @@ -1,4 +1,11 @@ +# Enable Bzlmod for every Bazel command +common --enable_bzlmod + +# Original configurations common --action_env=BAZEL_CXXOPTS=-std=c++17 +build --action_env=CCACHE_DISABLE=1 +build --action_env=CC=/usr/bin/gcc +build --action_env=CXX=/usr/bin/g++ common --cxxopt='-std=c++17' common --deleted_packages=externals build:macos --apple_platform_type=macos @@ -8,3 +15,51 @@ build:macos_arm64 --cpu=darwin_arm64 common --copt=-fdiagnostics-color=always common --test_output=errors common -c dbg + +# Additional optimizations for C++ compilation +# Note: boost.thread requires exceptions, boost.serialization requires RTTI +build --cxxopt=-fexceptions +# Disabled RTTI as some boost components need it +# build --cxxopt=-fno-rtti + +# GNU source compatibility +build --cxxopt=-D_GNU_SOURCE +build --cxxopt=-D__STDC_CONSTANT_MACROS +build --cxxopt=-D__STDC_FORMAT_MACROS +build --cxxopt=-D__STDC_LIMIT_MACROS + +# Debug build configuration (similar to CMAKE_BUILD_TYPE=DEBUG) +build:debug --cxxopt=-g +build:debug --cxxopt=-O0 +build:debug --strip=never +build:debug --copt=-DDEBUG + +# Release build configuration (similar to CMAKE_BUILD_TYPE=RELEASE) +build:release --cxxopt=-O3 +build:release --cxxopt=-DNDEBUG +build:release --strip=always + +# Build with assertions enabled (similar to LLVM_ENABLE_ASSERTIONS=ON) +build:assertions --cxxopt=-UNDEBUG +build:assertions --cxxopt=-DMLIR_ENABLE_ASSERTIONS + +# Verbose output (similar to ninja -v) +build:verbose --subcommands=pretty_print +build:verbose --verbose_failures + +# Parallel build (similar to ninja -j) +build --jobs=auto + +# Test configuration +test --test_output=errors +test --test_summary=short + +# Cache configuration, disable ccache since bazel cache system is much better +build --disk_cache=~/.cache/bazel +build --repository_cache=~/.cache/bazel/repo + +# Compatibility settings +build --incompatible_enable_cc_toolchain_resolution + +# Sandbox Location configuration +build --sandbox_base=/dev/shm diff --git a/.bazelversion b/.bazelversion index 19b860c..56b6be4 100644 --- a/.bazelversion +++ b/.bazelversion @@ -1 +1 @@ -6.4.0 +8.3.1 diff --git a/.gitignore b/.gitignore index 258a6a8..372ca16 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ bazel-bin bazel-mlir-tutorial bazel-out bazel-testlogs +MODULE.bazel.lock # cmake related files # ignore the user specified CMake presets in subproject directories. diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 0000000..23cbdbd --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1,88 @@ +# CLAUDE.md + +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. + +## Project Overview + +This is an MLIR (Multi-Level Intermediate Representation) tutorial codebase demonstrating compiler construction concepts. The project implements custom MLIR dialects and passes, with comprehensive examples showing how to build compiler transformations. + +## Build Systems + +The project supports two build systems: + +### Bazel (Primary) +- **Version**: Bazel 8.3.1 with Bzlmod enabled +- **Dependency Management**: Uses MODULE.bazel with Bazel Central Registry (BCR) +- **Build all**: `bazel build ...:all` +- **Test all**: `bazel test ...:all` +- **Build specific target**: `bazel build //tools:tutorial-opt` +- **Test specific target**: `bazel test //tests:poly_syntax` + +#### Bzlmod Migration +The project has been migrated from WORKSPACE-based dependency management to Bzlmod: +- **MODULE.bazel**: Main module definition with dependencies from BCR +- **extensions.bzl**: Custom module extension for LLVM-related dependencies +- **WORKSPACE**: Simplified to workspace name only +- Most dependencies (rules_python, protobuf, or-tools, etc.) now use BCR +- LLVM dependencies still use git repositories for latest upstream integration + +### CMake (Secondary) +First build LLVM/MLIR dependencies, then: +- **Configure**: `cmake -G Ninja -DLLVM_DIR="externals/llvm-project/build/lib/cmake/llvm" -DMLIR_DIR="externals/llvm-project/build/lib/cmake/mlir" -DBUILD_DEPS=ON -DCMAKE_BUILD_TYPE=Debug .` +- **Build main tool**: `cmake --build build-ninja --target tutorial-opt` +- **Run tests**: `cmake --build build-ninja --target check-mlir-tutorial` + +## Key Architecture + +### Core Components +- **tutorial-opt**: Main compiler tool in `tools/tutorial-opt.cpp` - processes MLIR files through various passes +- **Custom Dialects**: + - `Poly`: Polynomial arithmetic dialect (`lib/Dialect/Poly/`) + - `Noisy`: Demonstration dialect with noise operations (`lib/Dialect/Noisy/`) +- **Transforms**: Located in `lib/Transform/` with subdirectories for different pass categories +- **Conversions**: Dialect lowering passes in `lib/Conversion/` +- **Analysis**: Data flow analysis passes in `lib/Analysis/` + +### Test Infrastructure +- Uses LLVM's `lit` testing framework +- Test files in `tests/` directory with `.mlir` extension +- Run individual tests: `bazel test //tests:test_name` + +### Pass Pipeline +The `poly-to-llvm` pipeline demonstrates full lowering: +1. Poly dialect → Standard dialect +2. Standard → Linalg +3. Bufferization +4. Linalg → Loops +5. Loops → Control Flow +6. Control Flow → LLVM IR + +## Development Commands + +### Testing Individual Components +- **Syntax test**: `tutorial-opt tests/poly_syntax.mlir` +- **Canonicalization**: `tutorial-opt --canonicalize tests/poly_canonicalize.mlir` +- **Full lowering**: `tutorial-opt --poly-to-llvm tests/poly_to_llvm.mlir` + +### Adding New Passes +1. Define in appropriate `lib/Transform/*/Passes.td` +2. Implement in corresponding `.cpp` file +3. Register in `tools/tutorial-opt.cpp` +4. Add test in `tests/` + +### Tablegen Files +- Dialect definitions: `*.td` files in `lib/Dialect/*/` +- Pass definitions: `Passes.td` files in `lib/Transform/*/` +- Pattern definitions: `*.td` files for rewrite patterns + +## File Structure +- `lib/`: Core implementation (dialects, passes, conversions, analysis) +- `tools/`: Command-line tools (mainly tutorial-opt) +- `tests/`: Test files using lit framework +- `bazel/`: Bazel build configuration (legacy, LLVM import utilities) +- `externals/`: LLVM/MLIR submodule (for CMake builds) +- `MODULE.bazel`: Bzlmod module definition with BCR dependencies +- `extensions.bzl`: Custom module extension for LLVM dependencies +- `WORKSPACE`: Simplified workspace marker (legacy dependencies removed) +- `.bazelversion`: Pins Bazel version to 8.3.1 +- `.bazelrc`: Build configuration with Bzlmod enabled \ No newline at end of file diff --git a/MODULE.bazel b/MODULE.bazel new file mode 100644 index 0000000..72250c2 --- /dev/null +++ b/MODULE.bazel @@ -0,0 +1,70 @@ +############################################################################### +# Bazel now uses Bzlmod by default to manage external dependencies. +# Please consider migrating your external dependencies from WORKSPACE to MODULE.bazel. +# +# For more details, please check https://github.com/bazelbuild/bazel/issues/18958 +############################################################################### + +module( + name = "mlir_tutorial", + version = "1.0.0", + repo_name = "mlir_tutorial", +) + +# Core Bazel dependencies that are available in BCR +bazel_dep(name = "bazel_skylib", version = "1.7.1") +bazel_dep(name = "rules_python", version = "1.2.0") +bazel_dep(name = "platforms", version = "0.0.11") +bazel_dep(name = "rules_cc", version = "0.1.1") +bazel_dep(name = "rules_java", version = "8.12.0") +bazel_dep(name = "protobuf", version = "30.1") +bazel_dep(name = "rules_proto", version = "7.1.0") +bazel_dep(name = "rules_pkg", version = "1.1.0") + +# External dependencies available in BCR +bazel_dep(name = "re2", version = "2024-07-02.bcr.1") +bazel_dep(name = "abseil-cpp", version = "20250512.1") +bazel_dep(name = "or-tools", version = "9.12") +bazel_dep(name = "eigen", version = "4.0.0-20241125.bcr.2") +bazel_dep(name = "highs", version = "1.11.0") +bazel_dep(name = "pcre2", version = "10.46-DEV") +bazel_dep(name = "glpk", version = "5.0.bcr.4") +bazel_dep(name = "bliss", version = "0.73") +bazel_dep(name = "scip", version = "9.2.0.bcr.3") +bazel_dep(name = "zlib-ng", version = "2.0.7") + +# Hedron's Compile Commands Extractor for Bazel +# https://github.com/hedronvision/bazel-compile-commands-extractor +bazel_dep(name = "hedron_compile_commands", dev_dependency = True) +git_override( + module_name = "hedron_compile_commands", + remote = "https://github.com/hedronvision/bazel-compile-commands-extractor.git", + commit = "0e990032f3c5a866e72615cf67e5ce22186dcb97", + # Replace the commit hash (above) with the latest (https://github.com/hedronvision/bazel-compile-commands-extractor/commits/main). + # Even better, set up Renovate and let it do the work for you (see "Suggestion: Updates" in the README). +) + +# Use module extensions for LLVM and other dependencies that aren't in BCR +mlir_tutorial_deps = use_extension("//:extensions.bzl", "mlir_tutorial_deps") +use_repo(mlir_tutorial_deps, + "llvm-raw", + "llvm_zstd", + "llvm_zlib" +) + +# Configure LLVM project using use_repo_rule +llvm_configure = use_repo_rule("@llvm-raw//utils/bazel:configure.bzl", "llvm_configure") +llvm_configure(name = "llvm-project") + +# Configure Python dependencies +python = use_extension("@rules_python//python/extensions:python.bzl", "python") +python.toolchain(python_version = "3.10") +use_repo(python, "python_3_10") + +pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip") +pip.parse( + hub_name = "mlir_tutorial_pip_deps", + python_version = "3.10", + requirements_lock = "//:requirements.txt", +) +use_repo(pip, "mlir_tutorial_pip_deps") \ No newline at end of file diff --git a/README.md b/README.md index 5e5452d..2ac2f23 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,11 @@ the tutorial series and explained in the first article, The CMake build is maintained, but was added at article 10 (Dialect Conversion) and will not be explained in the articles. +**Note**: This project has been upgraded to Bazel 8.3.1 and migrated to use +Bzlmod for dependency management, replacing the traditional WORKSPACE file +approach. Dependencies are now managed through `MODULE.bazel` using the +Bazel Central Registry (BCR) where possible. + ### Prerequisites Install Bazelisk via instructions at @@ -39,6 +44,9 @@ This should create the `bazel` command on your system. You should also have a modern C++ compiler on your system, either `gcc` or `clang`, which Bazel will detect. +**Bazel Version**: This project requires Bazel 8.3.1 or newer. The specific +version is pinned in `.bazelversion`. + ### Build and test Run @@ -48,6 +56,19 @@ bazel build ...:all bazel test ...:all ``` +### Dependency Management + +The project uses Bzlmod (MODULE.bazel) for dependency management: + +- **Core dependencies**: Managed through Bazel Central Registry (BCR) + - rules_python, rules_java, protobuf, abseil-cpp, or-tools, etc. +- **LLVM dependencies**: Managed through custom module extension + - LLVM/MLIR source code via git repository +- **Development tools**: hedron_compile_commands via git_override + +This approach provides better dependency resolution, versioning, and +compatibility compared to the legacy WORKSPACE approach. + ## CMake build CMake is one of two supported build systems for this tutorial. The other is diff --git a/WORKSPACE b/WORKSPACE index ed24794..920dc13 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -1,231 +1,4 @@ -workspace(name = "mlir_tutorial") - -load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository", "new_git_repository") -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") -load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") - -# Bazel skylib is a collection of bazel tools that are a required dependency of -# the LLVM/MLIR bazel build overlay. -http_archive( - name = "bazel_skylib", - sha256 = "74d544d96f4a5bb630d465ca8bbcfe231e3594e5aae57e1edbf17a6eb3ca2506", - urls = [ - "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.3.0/bazel-skylib-1.3.0.tar.gz", - "https://github.com/bazelbuild/bazel-skylib/releases/download/1.3.0/bazel-skylib-1.3.0.tar.gz", - ], -) - -load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace") - -bazel_skylib_workspace() - -load("@bazel_skylib//lib:versions.bzl", "versions") - -versions.check(minimum_bazel_version = "6.3.2") - -# A two-step process for buliding LLVM/MLIR with bazel. First the raw source -# code is downloaded and imported into this workspace as a git repository, -# called `llvm-raw`. Then the build files defined in the LLVM monorepo are -# overlaid using llvm_configure in the setup script below. This defines the -# @llvm-project bazel project which is can be built and depended on. -load("//bazel:import_llvm.bzl", "import_llvm") - -import_llvm("llvm-raw") - -load("//bazel:setup_llvm.bzl", "setup_llvm") - -setup_llvm("llvm-project") - -# LLVM doesn't have proper support for excluding the optional llvm_zstd and -# llvm_zlib dependencies but it is supposed to make LLVM faster, so why not -# include it. See https://reviews.llvm.org/D143344#4232172 -maybe( - http_archive, - name = "llvm_zstd", - build_file = "@llvm-raw//utils/bazel/third_party_build:zstd.BUILD", - sha256 = "7c42d56fac126929a6a85dbc73ff1db2411d04f104fae9bdea51305663a83fd0", - strip_prefix = "zstd-1.5.2", - urls = [ - "https://github.com/facebook/zstd/releases/download/v1.5.2/zstd-1.5.2.tar.gz", - ], -) - -maybe( - http_archive, - name = "llvm_zlib", - build_file = "@llvm-raw//utils/bazel/third_party_build:zlib-ng.BUILD", - sha256 = "e36bb346c00472a1f9ff2a0a4643e590a254be6379da7cddd9daeb9a7f296731", - strip_prefix = "zlib-ng-2.0.7", - urls = [ - "https://github.com/zlib-ng/zlib-ng/archive/refs/tags/2.0.7.zip", - ], -) - -# compile_commands extracts the relevant compile data from bazel into -# `compile_commands.json` so that clangd, clang-tidy, etc., can use it. -# Whenever a build file changes, you must re-run -# -# bazel run @hedron_compile_commands//:refresh_all -# -# to ingest new data into these tools. -# -# See the project repo for more details and configuration options -# https://github.com/hedronvision/bazel-compile-commands-extractor -http_archive( - name = "hedron_compile_commands", - sha256 = "3cd0e49f0f4a6d406c1d74b53b7616f5e24f5fd319eafc1bf8eee6e14124d115", - strip_prefix = "bazel-compile-commands-extractor-3dddf205a1f5cde20faf2444c1757abe0564ff4c", - url = "https://github.com/hedronvision/bazel-compile-commands-extractor/archive/3dddf205a1f5cde20faf2444c1757abe0564ff4c.tar.gz", -) - -load("@hedron_compile_commands//:workspace_setup.bzl", "hedron_compile_commands_setup") - -hedron_compile_commands_setup() - -# Depend on a hermetic python version -new_git_repository( - name = "rules_python", - commit = "9ffb1ecd9b4e46d2a0bca838ac80d7128a352f9f", # v0.23.1 - remote = "https://github.com/bazelbuild/rules_python.git", -) - -load("@rules_python//python:repositories.bzl", "python_register_toolchains") - -python_register_toolchains( - name = "python3_10", - # Available versions are listed at - # https://github.com/bazelbuild/rules_python/blob/main/python/versions.bzl - python_version = "3.10", -) - -load("@python3_10//:defs.bzl", "interpreter") -load("@rules_python//python:pip.bzl", "pip_parse") +# This file marks the root of the Bazel workspace. +# See MODULE.bazel for external dependencies setup using Bzlmod. -pip_parse( - name = "mlir_tutorial_pip_deps", - python_interpreter_target = interpreter, - requirements_lock = "//:requirements.txt", -) - -load("@mlir_tutorial_pip_deps//:requirements.bzl", "install_deps") - -install_deps() - -##### Deps for or-tools ##### - -## Bazel rules. -git_repository( - name = "platforms", - commit = "380c85cc2c7b126c6e354f517dc16d89fe760c9f", - remote = "https://github.com/bazelbuild/platforms.git", -) - -git_repository( - name = "rules_proto", - commit = "3f1ab99b718e3e7dd86ebdc49c580aa6a126b1cd", - remote = "https://github.com/bazelbuild/rules_proto.git", -) - -## ZLIB -new_git_repository( - name = "zlib", - build_file = "@com_google_protobuf//:third_party/zlib.BUILD", - commit = "04f42ceca40f73e2978b50e93806c2a18c1281fc", - remote = "https://github.com/madler/zlib.git", -) - -## Re2 -git_repository( - name = "com_google_re2", - remote = "https://github.com/google/re2.git", - tag = "2023-07-01", -) - -## Abseil-cpp -git_repository( - name = "com_google_absl", - commit = "c2435f8342c2d0ed8101cb43adfd605fdc52dca2", - patch_args = ["-p1"], - patches = ["@com_google_ortools//patches:abseil-cpp-20230125.3.patch"], - remote = "https://github.com/abseil/abseil-cpp.git", -) - -## Protobuf -git_repository( - name = "com_google_protobuf", - # there's a patch for the CMake build in protobuf, ignoring - # patches = ["@com_google_ortools//patches:protobuf-v23.3.patch"], - commit = "4dd15db6eb3955745f379d28fb4a2fcfb6753de3", - patch_args = ["-p1"], - remote = "https://github.com/protocolbuffers/protobuf.git", -) - -# Load common dependencies. -load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps") - -protobuf_deps() - -## Solvers -http_archive( - name = "glpk", - build_file = "@com_google_ortools//bazel:glpk.BUILD", - sha256 = "4a1013eebb50f728fc601bdd833b0b2870333c3b3e5a816eeba921d95bec6f15", - url = "http://ftp.gnu.org/gnu/glpk/glpk-5.0.tar.gz", -) - -http_archive( - name = "bliss", - build_file = "@com_google_ortools//bazel:bliss.BUILD", - patches = ["@com_google_ortools//bazel:bliss-0.73.patch"], - sha256 = "f57bf32804140cad58b1240b804e0dbd68f7e6bf67eba8e0c0fa3a62fd7f0f84", - url = "https://github.com/google/or-tools/releases/download/v9.0/bliss-0.73.zip", - #url = "http://www.tcs.hut.fi/Software/bliss/bliss-0.73.zip", -) - -new_git_repository( - name = "scip", - build_file = "@com_google_ortools//bazel:scip.BUILD", - commit = "62fab8a2e3708f3452fad473a6f48715c367316b", - patch_args = ["-p1"], - patches = ["@com_google_ortools//bazel:scip.patch"], - remote = "https://github.com/scipopt/scip.git", -) - -# Eigen has no Bazel build. -new_git_repository( - name = "eigen", - build_file_content = - """ -cc_library( - name = 'eigen3', - srcs = [], - includes = ['.'], - hdrs = glob(['Eigen/**']), - visibility = ['//visibility:public'], -) -""", - commit = "3147391d946bb4b6c68edd901f2add6ac1f31f8c", - remote = "https://gitlab.com/libeigen/eigen.git", -) - -git_repository( - name = "highs", - branch = "bazel", - remote = "https://github.com/ERGO-Code/HiGHS.git", -) - -## Swig support -# pcre source code repository -new_git_repository( - name = "pcre2", - build_file = "@com_google_ortools//bazel:pcre2.BUILD", - remote = "https://github.com/PCRE2Project/pcre2.git", - tag = "pcre2-10.42", -) - -git_repository( - name = "com_google_ortools", - commit = "1d696f9108a0ebfd99feb73b9211e2f5a6b0812b", - remote = "https://github.com/google/or-tools.git", - shallow_since = "1647023481 +0100", -) +workspace(name = "mlir_tutorial") diff --git a/extensions.bzl b/extensions.bzl new file mode 100644 index 0000000..e047ea4 --- /dev/null +++ b/extensions.bzl @@ -0,0 +1,44 @@ +"""Module extensions for MLIR Tutorial dependencies.""" + +load("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository") +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") +load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") + +def _mlir_tutorial_deps_impl(module_ctx): + """Implementation of the mlir_tutorial_deps module extension.""" + + # Download LLVM/MLIR using a git repository + new_git_repository( + name = "llvm-raw", + build_file_content = "# empty", + commit = "16d4453f2f5d9554ce39507fda0f33ce9066007b", # from import_llvm.bzl + init_submodules = False, + remote = "https://github.com/llvm/llvm-project.git", + ) + + # Optional LLVM dependencies for performance + maybe( + http_archive, + name = "llvm_zstd", + build_file = "@llvm-raw//utils/bazel/third_party_build:zstd.BUILD", + sha256 = "7c42d56fac126929a6a85dbc73ff1db2411d04f104fae9bdea51305663a83fd0", + strip_prefix = "zstd-1.5.2", + urls = [ + "https://github.com/facebook/zstd/releases/download/v1.5.2/zstd-1.5.2.tar.gz", + ], + ) + + maybe( + http_archive, + name = "llvm_zlib", + build_file = "@llvm-raw//utils/bazel/third_party_build:zlib-ng.BUILD", + sha256 = "e36bb346c00472a1f9ff2a0a4643e590a254be6379da7cddd9daeb9a7f296731", + strip_prefix = "zlib-ng-2.0.7", + urls = [ + "https://github.com/zlib-ng/zlib-ng/archive/refs/tags/2.0.7.zip", + ], + ) + +mlir_tutorial_deps = module_extension( + implementation = _mlir_tutorial_deps_impl, +) \ No newline at end of file diff --git a/lib/Analysis/ReduceNoiseAnalysis/BUILD b/lib/Analysis/ReduceNoiseAnalysis/BUILD index 90981d0..6df5387 100644 --- a/lib/Analysis/ReduceNoiseAnalysis/BUILD +++ b/lib/Analysis/ReduceNoiseAnalysis/BUILD @@ -8,8 +8,8 @@ cc_library( hdrs = ["ReduceNoiseAnalysis.h"], deps = [ "//lib/Dialect/Noisy", - "@com_google_ortools//ortools/base", - "@com_google_ortools//ortools/linear_solver", + "@or-tools//ortools/base", + "@or-tools//ortools/linear_solver", "@llvm-project//llvm:Support", "@llvm-project//mlir:IR", ], From 90a3902eabc30b3881764a7cbd00f6c605966840 Mon Sep 17 00:00:00 2001 From: Sihao Liu Date: Tue, 15 Jul 2025 13:10:09 -0700 Subject: [PATCH 02/17] Bazel build and test all works --- MODULE.bazel | 10 ++++------ bazel/lit.bzl | 2 ++ requirements.txt | 2 +- tests/BUILD | 2 +- tests/lit.cfg.py | 16 +++++++++------- 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/MODULE.bazel b/MODULE.bazel index 72250c2..0646fda 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -11,7 +11,7 @@ module( repo_name = "mlir_tutorial", ) -# Core Bazel dependencies that are available in BCR +# Dependencies available in BCR bazel_dep(name = "bazel_skylib", version = "1.7.1") bazel_dep(name = "rules_python", version = "1.2.0") bazel_dep(name = "platforms", version = "0.0.11") @@ -20,8 +20,6 @@ bazel_dep(name = "rules_java", version = "8.12.0") bazel_dep(name = "protobuf", version = "30.1") bazel_dep(name = "rules_proto", version = "7.1.0") bazel_dep(name = "rules_pkg", version = "1.1.0") - -# External dependencies available in BCR bazel_dep(name = "re2", version = "2024-07-02.bcr.1") bazel_dep(name = "abseil-cpp", version = "20250512.1") bazel_dep(name = "or-tools", version = "9.12") @@ -58,13 +56,13 @@ llvm_configure(name = "llvm-project") # Configure Python dependencies python = use_extension("@rules_python//python/extensions:python.bzl", "python") -python.toolchain(python_version = "3.10") -use_repo(python, "python_3_10") +python.toolchain(python_version = "3.13") +use_repo(python, "python_3_13") pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip") pip.parse( hub_name = "mlir_tutorial_pip_deps", - python_version = "3.10", + python_version = "3.13", requirements_lock = "//:requirements.txt", ) use_repo(pip, "mlir_tutorial_pip_deps") \ No newline at end of file diff --git a/bazel/lit.bzl b/bazel/lit.bzl index 8adb209..b5fe9c3 100644 --- a/bazel/lit.bzl +++ b/bazel/lit.bzl @@ -51,8 +51,10 @@ def lit_test(name = None, src = None, size = "small", tags = None): # -v ensures lit outputs useful info during test failures args = ["-v", paths.join(native.package_name(), src)], data = ["@mlir_tutorial//tests:test_utilities", filegroup_name], + deps = ["@mlir_tutorial_pip_deps//lit"], srcs = ["@llvm-project//llvm:lit"], main = "lit.py", + python_version = "PY3", tags = tags, ) diff --git a/requirements.txt b/requirements.txt index ea9fd76..b382aeb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -lit==16.0.6 +lit==18.1.8 diff --git a/tests/BUILD b/tests/BUILD index b7dfc45..2df8ffd 100644 --- a/tests/BUILD +++ b/tests/BUILD @@ -16,7 +16,7 @@ filegroup( "@llvm-project//mlir:mlir-runner", "@llvm-project//mlir:mlir-opt", "@llvm-project//mlir:mlir-translate", - "@mlir_tutorial_pip_deps_lit//:pkg", + "@mlir_tutorial_pip_deps//lit", ], ) diff --git a/tests/lit.cfg.py b/tests/lit.cfg.py index 15ca568..b2897be 100644 --- a/tests/lit.cfg.py +++ b/tests/lit.cfg.py @@ -12,7 +12,7 @@ # lit executes relative to the directory # -# bazel-bin/tests/.runfiles/mlir_tutorial/ +# bazel-bin/tests/.runfiles/_main/ # # which contains all the binary targets included in via the `data` attribute in # the lit.bzl macro, which in turn gets them from the filegroup //tests:test_utilities. @@ -25,14 +25,16 @@ # print(subprocess.run(["ls", "-l", os.environ["RUNFILES_DIR"]]).stdout) # print(subprocess.run([ "env", ]).stdout) # -# Bazel defines RUNFILES_DIR which includes mlir_tutorial/ and third party +# Bazel defines RUNFILES_DIR which includes _main/ and third party # dependencies as their own directory. Generally, it seems that $PWD == -# $RUNFILES_DIR/mlir_tutorial/ +# $RUNFILES_DIR/_main/ runfiles_dir = Path(os.environ["RUNFILES_DIR"]) + +# Fix tool paths to use _main instead of mlir_tutorial tool_relpaths = [ - "llvm-project/mlir", - "llvm-project/llvm", - "mlir_tutorial/tools", + "+_repo_rules+llvm-project/mlir", + "+_repo_rules+llvm-project/llvm", + "_main/tools", ] config.environment["PATH"] = ( @@ -42,6 +44,6 @@ ) substitutions = { - "%project_source_dir": str(runfiles_dir.joinpath(Path('mlir_tutorial'))), + "%project_source_dir": str(runfiles_dir.joinpath(Path('_main'))), } config.substitutions.extend(substitutions.items()) From d749983002cc9eab7704d4985e1c16307b55cca1 Mon Sep 17 00:00:00 2001 From: Sihao Liu Date: Tue, 15 Jul 2025 13:44:31 -0700 Subject: [PATCH 03/17] disable ccache for common build --- .bazelrc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.bazelrc b/.bazelrc index 692e959..775c76c 100644 --- a/.bazelrc +++ b/.bazelrc @@ -3,7 +3,7 @@ common --enable_bzlmod # Original configurations common --action_env=BAZEL_CXXOPTS=-std=c++17 -build --action_env=CCACHE_DISABLE=1 +common --action_env=CCACHE_DISABLE=1 build --action_env=CC=/usr/bin/gcc build --action_env=CXX=/usr/bin/g++ common --cxxopt='-std=c++17' From 9b2cbd92d2ace6f6502a3de92ba8d480e6b05b64 Mon Sep 17 00:00:00 2001 From: Sihao Liu Date: Thu, 17 Jul 2025 10:40:01 -0700 Subject: [PATCH 04/17] keep only one line that needed by bzlmod --- .bazelrc | 53 +---------------------------------------------------- 1 file changed, 1 insertion(+), 52 deletions(-) diff --git a/.bazelrc b/.bazelrc index 775c76c..0cfb641 100644 --- a/.bazelrc +++ b/.bazelrc @@ -3,9 +3,6 @@ common --enable_bzlmod # Original configurations common --action_env=BAZEL_CXXOPTS=-std=c++17 -common --action_env=CCACHE_DISABLE=1 -build --action_env=CC=/usr/bin/gcc -build --action_env=CXX=/usr/bin/g++ common --cxxopt='-std=c++17' common --deleted_packages=externals build:macos --apple_platform_type=macos @@ -14,52 +11,4 @@ build:macos --macos_sdk_version=10.13 build:macos_arm64 --cpu=darwin_arm64 common --copt=-fdiagnostics-color=always common --test_output=errors -common -c dbg - -# Additional optimizations for C++ compilation -# Note: boost.thread requires exceptions, boost.serialization requires RTTI -build --cxxopt=-fexceptions -# Disabled RTTI as some boost components need it -# build --cxxopt=-fno-rtti - -# GNU source compatibility -build --cxxopt=-D_GNU_SOURCE -build --cxxopt=-D__STDC_CONSTANT_MACROS -build --cxxopt=-D__STDC_FORMAT_MACROS -build --cxxopt=-D__STDC_LIMIT_MACROS - -# Debug build configuration (similar to CMAKE_BUILD_TYPE=DEBUG) -build:debug --cxxopt=-g -build:debug --cxxopt=-O0 -build:debug --strip=never -build:debug --copt=-DDEBUG - -# Release build configuration (similar to CMAKE_BUILD_TYPE=RELEASE) -build:release --cxxopt=-O3 -build:release --cxxopt=-DNDEBUG -build:release --strip=always - -# Build with assertions enabled (similar to LLVM_ENABLE_ASSERTIONS=ON) -build:assertions --cxxopt=-UNDEBUG -build:assertions --cxxopt=-DMLIR_ENABLE_ASSERTIONS - -# Verbose output (similar to ninja -v) -build:verbose --subcommands=pretty_print -build:verbose --verbose_failures - -# Parallel build (similar to ninja -j) -build --jobs=auto - -# Test configuration -test --test_output=errors -test --test_summary=short - -# Cache configuration, disable ccache since bazel cache system is much better -build --disk_cache=~/.cache/bazel -build --repository_cache=~/.cache/bazel/repo - -# Compatibility settings -build --incompatible_enable_cc_toolchain_resolution - -# Sandbox Location configuration -build --sandbox_base=/dev/shm +common -c dbg \ No newline at end of file From 0c0981b0bb52bd76653532f89e57e72187877f3d Mon Sep 17 00:00:00 2001 From: Sihao Liu Date: Thu, 17 Jul 2025 10:40:52 -0700 Subject: [PATCH 05/17] more edits --- .bazelrc | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.bazelrc b/.bazelrc index 0cfb641..b301198 100644 --- a/.bazelrc +++ b/.bazelrc @@ -1,7 +1,4 @@ -# Enable Bzlmod for every Bazel command common --enable_bzlmod - -# Original configurations common --action_env=BAZEL_CXXOPTS=-std=c++17 common --cxxopt='-std=c++17' common --deleted_packages=externals @@ -11,4 +8,4 @@ build:macos --macos_sdk_version=10.13 build:macos_arm64 --cpu=darwin_arm64 common --copt=-fdiagnostics-color=always common --test_output=errors -common -c dbg \ No newline at end of file +common -c dbg From a73c623c455d51a26b6b75bf2c812d06c5312149 Mon Sep 17 00:00:00 2001 From: Sihao Liu Date: Thu, 17 Jul 2025 10:41:31 -0700 Subject: [PATCH 06/17] remove personal Claude dev --- CLAUDE.md | 88 ------------------------------------------------------- 1 file changed, 88 deletions(-) delete mode 100644 CLAUDE.md diff --git a/CLAUDE.md b/CLAUDE.md deleted file mode 100644 index 23cbdbd..0000000 --- a/CLAUDE.md +++ /dev/null @@ -1,88 +0,0 @@ -# CLAUDE.md - -This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. - -## Project Overview - -This is an MLIR (Multi-Level Intermediate Representation) tutorial codebase demonstrating compiler construction concepts. The project implements custom MLIR dialects and passes, with comprehensive examples showing how to build compiler transformations. - -## Build Systems - -The project supports two build systems: - -### Bazel (Primary) -- **Version**: Bazel 8.3.1 with Bzlmod enabled -- **Dependency Management**: Uses MODULE.bazel with Bazel Central Registry (BCR) -- **Build all**: `bazel build ...:all` -- **Test all**: `bazel test ...:all` -- **Build specific target**: `bazel build //tools:tutorial-opt` -- **Test specific target**: `bazel test //tests:poly_syntax` - -#### Bzlmod Migration -The project has been migrated from WORKSPACE-based dependency management to Bzlmod: -- **MODULE.bazel**: Main module definition with dependencies from BCR -- **extensions.bzl**: Custom module extension for LLVM-related dependencies -- **WORKSPACE**: Simplified to workspace name only -- Most dependencies (rules_python, protobuf, or-tools, etc.) now use BCR -- LLVM dependencies still use git repositories for latest upstream integration - -### CMake (Secondary) -First build LLVM/MLIR dependencies, then: -- **Configure**: `cmake -G Ninja -DLLVM_DIR="externals/llvm-project/build/lib/cmake/llvm" -DMLIR_DIR="externals/llvm-project/build/lib/cmake/mlir" -DBUILD_DEPS=ON -DCMAKE_BUILD_TYPE=Debug .` -- **Build main tool**: `cmake --build build-ninja --target tutorial-opt` -- **Run tests**: `cmake --build build-ninja --target check-mlir-tutorial` - -## Key Architecture - -### Core Components -- **tutorial-opt**: Main compiler tool in `tools/tutorial-opt.cpp` - processes MLIR files through various passes -- **Custom Dialects**: - - `Poly`: Polynomial arithmetic dialect (`lib/Dialect/Poly/`) - - `Noisy`: Demonstration dialect with noise operations (`lib/Dialect/Noisy/`) -- **Transforms**: Located in `lib/Transform/` with subdirectories for different pass categories -- **Conversions**: Dialect lowering passes in `lib/Conversion/` -- **Analysis**: Data flow analysis passes in `lib/Analysis/` - -### Test Infrastructure -- Uses LLVM's `lit` testing framework -- Test files in `tests/` directory with `.mlir` extension -- Run individual tests: `bazel test //tests:test_name` - -### Pass Pipeline -The `poly-to-llvm` pipeline demonstrates full lowering: -1. Poly dialect → Standard dialect -2. Standard → Linalg -3. Bufferization -4. Linalg → Loops -5. Loops → Control Flow -6. Control Flow → LLVM IR - -## Development Commands - -### Testing Individual Components -- **Syntax test**: `tutorial-opt tests/poly_syntax.mlir` -- **Canonicalization**: `tutorial-opt --canonicalize tests/poly_canonicalize.mlir` -- **Full lowering**: `tutorial-opt --poly-to-llvm tests/poly_to_llvm.mlir` - -### Adding New Passes -1. Define in appropriate `lib/Transform/*/Passes.td` -2. Implement in corresponding `.cpp` file -3. Register in `tools/tutorial-opt.cpp` -4. Add test in `tests/` - -### Tablegen Files -- Dialect definitions: `*.td` files in `lib/Dialect/*/` -- Pass definitions: `Passes.td` files in `lib/Transform/*/` -- Pattern definitions: `*.td` files for rewrite patterns - -## File Structure -- `lib/`: Core implementation (dialects, passes, conversions, analysis) -- `tools/`: Command-line tools (mainly tutorial-opt) -- `tests/`: Test files using lit framework -- `bazel/`: Bazel build configuration (legacy, LLVM import utilities) -- `externals/`: LLVM/MLIR submodule (for CMake builds) -- `MODULE.bazel`: Bzlmod module definition with BCR dependencies -- `extensions.bzl`: Custom module extension for LLVM dependencies -- `WORKSPACE`: Simplified workspace marker (legacy dependencies removed) -- `.bazelversion`: Pins Bazel version to 8.3.1 -- `.bazelrc`: Build configuration with Bzlmod enabled \ No newline at end of file From 9ba025b274d52686ad189c3075938a26d3acde57 Mon Sep 17 00:00:00 2001 From: Sihao Liu Date: Thu, 17 Jul 2025 10:44:52 -0700 Subject: [PATCH 07/17] override LLVM target to speed up build --- MODULE.bazel | 2 -- bazel/setup_llvm.bzl | 24 ------------------------ 2 files changed, 26 deletions(-) delete mode 100644 bazel/setup_llvm.bzl diff --git a/MODULE.bazel b/MODULE.bazel index 0646fda..958f5b6 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -50,8 +50,6 @@ use_repo(mlir_tutorial_deps, "llvm_zlib" ) -# Configure LLVM project using use_repo_rule -llvm_configure = use_repo_rule("@llvm-raw//utils/bazel:configure.bzl", "llvm_configure") llvm_configure(name = "llvm-project") # Configure Python dependencies diff --git a/bazel/setup_llvm.bzl b/bazel/setup_llvm.bzl deleted file mode 100644 index c833fc4..0000000 --- a/bazel/setup_llvm.bzl +++ /dev/null @@ -1,24 +0,0 @@ -"""Configure LLVM Bazel overlays from a 'raw' imported llvm repository""" - -load("@llvm-raw//utils/bazel:configure.bzl", "llvm_configure") - -# The subset of LLVM backend targets that should be compiled -_LLVM_TARGETS = [ - "X86", - # The bazel dependency graph for mlir-opt fails to load (at the analysis - # step) without the NVPTX target in this list, because mlir/test:TestGPU - # depends on the //llvm:NVPTXCodeGen target, which is not defined unless this - # is included. @j2kun asked the LLVM maintiners for tips on how to fix this, - # see https://github.com/llvm/llvm-project/issues/63135 - "NVPTX", - # Needed for Apple M1 targets, see - # https://github.com/j2kun/mlir-tutorial/issues/11 - "AArch64", -] - -def setup_llvm(name): - """Build @llvm-project from @llvm-raw using the upstream bazel overlays.""" - llvm_configure( - name = name, - targets = _LLVM_TARGETS, - ) From 449dc71667a3d2d2fc7b5638afb31b7e5aa99ab7 Mon Sep 17 00:00:00 2001 From: Sihao Liu Date: Thu, 17 Jul 2025 10:45:32 -0700 Subject: [PATCH 08/17] removed since upgraded to bzlmod --- WORKSPACE | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 WORKSPACE diff --git a/WORKSPACE b/WORKSPACE deleted file mode 100644 index 920dc13..0000000 --- a/WORKSPACE +++ /dev/null @@ -1,4 +0,0 @@ -# This file marks the root of the Bazel workspace. -# See MODULE.bazel for external dependencies setup using Bzlmod. - -workspace(name = "mlir_tutorial") From 49a5db9d1f3c27aaf8528d80e35ced55094e63f4 Mon Sep 17 00:00:00 2001 From: Sihao Liu Date: Thu, 17 Jul 2025 10:53:08 -0700 Subject: [PATCH 09/17] bump llvm to latest version --- MODULE.bazel | 2 ++ bazel/setup_llvm.bzl | 7 +++++++ extensions.bzl | 2 +- lib/Dialect/Poly/PolyOps.td | 2 +- tools/tutorial-opt.cpp | 2 +- 5 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 bazel/setup_llvm.bzl diff --git a/MODULE.bazel b/MODULE.bazel index 958f5b6..0646fda 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -50,6 +50,8 @@ use_repo(mlir_tutorial_deps, "llvm_zlib" ) +# Configure LLVM project using use_repo_rule +llvm_configure = use_repo_rule("@llvm-raw//utils/bazel:configure.bzl", "llvm_configure") llvm_configure(name = "llvm-project") # Configure Python dependencies diff --git a/bazel/setup_llvm.bzl b/bazel/setup_llvm.bzl new file mode 100644 index 0000000..7e15ff0 --- /dev/null +++ b/bazel/setup_llvm.bzl @@ -0,0 +1,7 @@ +"""Configure LLVM Bazel overlays from a 'raw' imported llvm repository""" + +load("@llvm-raw//utils/bazel:configure.bzl", "llvm_configure") + +def setup_llvm(name): + """Build @llvm-project from @llvm-raw using the upstream bazel overlays.""" + llvm_configure(name = name) diff --git a/extensions.bzl b/extensions.bzl index e047ea4..61e4047 100644 --- a/extensions.bzl +++ b/extensions.bzl @@ -11,7 +11,7 @@ def _mlir_tutorial_deps_impl(module_ctx): new_git_repository( name = "llvm-raw", build_file_content = "# empty", - commit = "16d4453f2f5d9554ce39507fda0f33ce9066007b", # from import_llvm.bzl + commit = "e8a891b0f9d2a742ac3904116aaec2c7c9231b24", # from import_llvm.bzl init_submodules = False, remote = "https://github.com/llvm/llvm-project.git", ) diff --git a/lib/Dialect/Poly/PolyOps.td b/lib/Dialect/Poly/PolyOps.td index 76cb2f9..230b2f4 100644 --- a/lib/Dialect/Poly/PolyOps.td +++ b/lib/Dialect/Poly/PolyOps.td @@ -10,7 +10,7 @@ include "mlir/Interfaces/SideEffectInterfaces.td" // Type constraint for poly binop arguments: polys, vectors of polys, or // tensors of polys. -def PolyOrContainer : TypeOrContainer; +def PolyOrContainer : TypeOrValueSemanticsContainer; // Inject verification that all integer-like arguments are 32-bits def Has32BitArguments : NativeOpTrait<"Has32BitArguments"> { diff --git a/tools/tutorial-opt.cpp b/tools/tutorial-opt.cpp index 87f213d..e9f4ef4 100644 --- a/tools/tutorial-opt.cpp +++ b/tools/tutorial-opt.cpp @@ -42,7 +42,7 @@ void polyToLLVMPipelineBuilder(mlir::OpPassManager &manager) { // Needed to lower memref.subview manager.addPass(mlir::memref::createExpandStridedMetadataPass()); - manager.addPass(mlir::createConvertSCFToCFPass()); + manager.addPass(mlir::createSCFToControlFlowPass()); manager.addPass(mlir::createConvertControlFlowToLLVMPass()); manager.addPass(mlir::createArithToLLVMConversionPass()); manager.addPass(mlir::createConvertFuncToLLVMPass()); From fbbe6a50dba5f80c5dfb091d3f8bfed985d89361 Mon Sep 17 00:00:00 2001 From: Sihao Liu Date: Thu, 17 Jul 2025 10:57:54 -0700 Subject: [PATCH 10/17] bringup TARGET selections to speed up build --- MODULE.bazel | 19 ++++++++++++++++++- bazel/setup_llvm.bzl | 7 ------- 2 files changed, 18 insertions(+), 8 deletions(-) delete mode 100644 bazel/setup_llvm.bzl diff --git a/MODULE.bazel b/MODULE.bazel index 0646fda..4c6bca2 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -50,9 +50,26 @@ use_repo(mlir_tutorial_deps, "llvm_zlib" ) +# The subset of LLVM backend targets that should be compiled +_LLVM_TARGETS = [ + "X86", + # The bazel dependency graph for mlir-opt fails to load (at the analysis + # step) without the NVPTX target in this list, because mlir/test:TestGPU + # depends on the //llvm:NVPTXCodeGen target, which is not defined unless this + # is included. @j2kun asked the LLVM maintiners for tips on how to fix this, + # see https://github.com/llvm/llvm-project/issues/63135 + "NVPTX", + # Needed for Apple M1 targets, see + # https://github.com/j2kun/mlir-tutorial/issues/11 + "AArch64", +] + # Configure LLVM project using use_repo_rule llvm_configure = use_repo_rule("@llvm-raw//utils/bazel:configure.bzl", "llvm_configure") -llvm_configure(name = "llvm-project") +llvm_configure( + name = "llvm-project", + targets = _LLVM_TARGETS, +) # Configure Python dependencies python = use_extension("@rules_python//python/extensions:python.bzl", "python") diff --git a/bazel/setup_llvm.bzl b/bazel/setup_llvm.bzl deleted file mode 100644 index 7e15ff0..0000000 --- a/bazel/setup_llvm.bzl +++ /dev/null @@ -1,7 +0,0 @@ -"""Configure LLVM Bazel overlays from a 'raw' imported llvm repository""" - -load("@llvm-raw//utils/bazel:configure.bzl", "llvm_configure") - -def setup_llvm(name): - """Build @llvm-project from @llvm-raw using the upstream bazel overlays.""" - llvm_configure(name = name) From e5f299765ed4c716ac82fbdc3ca428fcdc874920 Mon Sep 17 00:00:00 2001 From: Sihao Liu Date: Thu, 17 Jul 2025 11:02:38 -0700 Subject: [PATCH 11/17] import_llvm.bzl now in extensions.bzl --- bazel/import_llvm.bzl | 22 ---------------------- 1 file changed, 22 deletions(-) delete mode 100644 bazel/import_llvm.bzl diff --git a/bazel/import_llvm.bzl b/bazel/import_llvm.bzl deleted file mode 100644 index df11299..0000000 --- a/bazel/import_llvm.bzl +++ /dev/null @@ -1,22 +0,0 @@ -"""Provides the repository macro to import LLVM.""" - -load( - "@bazel_tools//tools/build_defs/repo:git.bzl", - "new_git_repository", -) - -def import_llvm(name): - """Imports LLVM.""" - - # 2025-02-01 - LLVM_COMMIT = "16d4453f2f5d9554ce39507fda0f33ce9066007b" - - new_git_repository( - name = name, - # this BUILD file is intentionally empty, because the LLVM project - # internally contains a set of bazel BUILD files overlaying the project. - build_file_content = "# empty", - commit = LLVM_COMMIT, - init_submodules = False, - remote = "https://github.com/llvm/llvm-project.git", - ) From c979b0b10ae6073f6fa06000f5e909ef635bd8a6 Mon Sep 17 00:00:00 2001 From: Sihao Liu Date: Thu, 17 Jul 2025 11:03:09 -0700 Subject: [PATCH 12/17] minor comments removed from extensions.bzl since import_llvm.bzl does not exist --- extensions.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions.bzl b/extensions.bzl index 61e4047..3565572 100644 --- a/extensions.bzl +++ b/extensions.bzl @@ -11,7 +11,7 @@ def _mlir_tutorial_deps_impl(module_ctx): new_git_repository( name = "llvm-raw", build_file_content = "# empty", - commit = "e8a891b0f9d2a742ac3904116aaec2c7c9231b24", # from import_llvm.bzl + commit = "e8a891b0f9d2a742ac3904116aaec2c7c9231b24", init_submodules = False, remote = "https://github.com/llvm/llvm-project.git", ) From 2b4ad447e220b90c58a51ce7cda916eb9b0e84e2 Mon Sep 17 00:00:00 2001 From: Sihao Liu Date: Thu, 17 Jul 2025 11:06:21 -0700 Subject: [PATCH 13/17] remove empty bazel/BUILD --- bazel/BUILD | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 bazel/BUILD diff --git a/bazel/BUILD b/bazel/BUILD deleted file mode 100644 index e69de29..0000000 From 5f5dec60e9327a5cdf0ecb1630d4373dbbdb8f4e Mon Sep 17 00:00:00 2001 From: Sihao Liu Date: Thu, 17 Jul 2025 11:09:25 -0700 Subject: [PATCH 14/17] weird quirk that we need empty BUILD file to run --- bazel/BUILD | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 bazel/BUILD diff --git a/bazel/BUILD b/bazel/BUILD new file mode 100644 index 0000000..e69de29 From 56697c4ab18b09d761c9fdfa454d5b19ef2437d4 Mon Sep 17 00:00:00 2001 From: Sihao Liu Date: Thu, 17 Jul 2025 11:19:09 -0700 Subject: [PATCH 15/17] Bufferization to BufferizePass --- tools/tutorial-opt.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/tutorial-opt.cpp b/tools/tutorial-opt.cpp index e9f4ef4..e1f2de8 100644 --- a/tools/tutorial-opt.cpp +++ b/tools/tutorial-opt.cpp @@ -29,7 +29,7 @@ void polyToLLVMPipelineBuilder(mlir::OpPassManager &manager) { // One-shot bufferize, from // https://mlir.llvm.org/docs/Bufferization/#ownership-based-buffer-deallocation - mlir::bufferization::OneShotBufferizationOptions bufferizationOptions; + mlir::bufferization::OneShotBufferizePassOptions bufferizationOptions; bufferizationOptions.bufferizeFunctionBoundaries = true; manager.addPass( mlir::bufferization::createOneShotBufferizePass(bufferizationOptions)); From 7b836d42dff575dec0e819ce5b968f96986c91f4 Mon Sep 17 00:00:00 2001 From: Sihao Liu Date: Thu, 17 Jul 2025 13:36:54 -0700 Subject: [PATCH 16/17] update LLVM submodule version used by cmake flow and update CI to use latest file to get llvm commit hash --- .github/workflows/build_and_test.yml | 6 +++--- .github/workflows/build_and_test_cmake.yml | 6 +++--- externals/llvm-project | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index 60f482c..abe4bfa 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -19,10 +19,10 @@ jobs: with: path: | ~/.cache/bazel - # add import_llvm.bzl so that a new build occurs after an LLVM commit hash update - key: ${{ runner.os }}-bazel-${{ hashFiles('bazel/import_llvm.bzl') }}-${{ hashFiles('.bazelversion', '.bazelrc', 'WORKSPACE') }} + # add extensions.bzl so that a new build occurs after an LLVM commit hash update + key: ${{ runner.os }}-bazel-${{ hashFiles('extensions.bzl') }}-${{ hashFiles('.bazelversion', '.bazelrc', 'MODULE.bazel') }} restore-keys: | - ${{ runner.os }}-bazel-${{ hashFiles('bazel/import_llvm.bzl') }} + ${{ runner.os }}-bazel-${{ hashFiles('extensions.bzl') }} - name: "Run `bazel build`" run: | diff --git a/.github/workflows/build_and_test_cmake.yml b/.github/workflows/build_and_test_cmake.yml index f224c76..1b9f49c 100644 --- a/.github/workflows/build_and_test_cmake.yml +++ b/.github/workflows/build_and_test_cmake.yml @@ -27,7 +27,7 @@ jobs: with: path: | ./externals/llvm-project - key: ${{ runner.os }}-cmake-${{ hashFiles('bazel/import_llvm.bzl') }}-${{ hashFiles('**/CMakeLists.txt') }} + key: ${{ runner.os }}-cmake-${{ hashFiles('extensions.bzl') }}-${{ hashFiles('**/CMakeLists.txt') }} - name: Cache mlir-tutorial build id: cache-mlir-tutorial @@ -35,7 +35,7 @@ jobs: with: path: | ./build - key: ${{ runner.os }}-cmake-${{ hashFiles('bazel/import_llvm.bzl') }}-${{ hashFiles('**/CMakeLists.txt') }} + key: ${{ runner.os }}-cmake-${{ hashFiles('extensions.bzl') }}-${{ hashFiles('**/CMakeLists.txt') }} - name: Git config run: | @@ -44,7 +44,7 @@ jobs: - name: Build LLVM if: steps.cache-llvm.outputs.cache-hit != 'true' run: | - LLVM_COMMIT=$(grep LLVM_COMMIT ${GITHUB_WORKSPACE}/bazel/import_llvm.bzl | head -n 1 | cut -d'"' -f 2 ) + LLVM_COMMIT=$(grep 'commit = ' ${GITHUB_WORKSPACE}/extensions.bzl | head -n 1 | cut -d'"' -f 2) git submodule update --init --recursive cd externals/llvm-project git checkout ${LLVM_COMMIT} diff --git a/externals/llvm-project b/externals/llvm-project index 16d4453..e8a891b 160000 --- a/externals/llvm-project +++ b/externals/llvm-project @@ -1 +1 @@ -Subproject commit 16d4453f2f5d9554ce39507fda0f33ce9066007b +Subproject commit e8a891b0f9d2a742ac3904116aaec2c7c9231b24 From 67540d71e96e5296db926ed5810ed8a32b77400a Mon Sep 17 00:00:00 2001 From: Sihao Liu Date: Thu, 17 Jul 2025 15:30:37 -0700 Subject: [PATCH 17/17] choose a CI passed LLVM version d9190f8141661bd6120dea61d28ae8940fd775d0 --- extensions.bzl | 4 ++-- externals/llvm-project | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/extensions.bzl b/extensions.bzl index 3565572..445b7b7 100644 --- a/extensions.bzl +++ b/extensions.bzl @@ -11,7 +11,7 @@ def _mlir_tutorial_deps_impl(module_ctx): new_git_repository( name = "llvm-raw", build_file_content = "# empty", - commit = "e8a891b0f9d2a742ac3904116aaec2c7c9231b24", + commit = "d9190f8141661bd6120dea61d28ae8940fd775d0", init_submodules = False, remote = "https://github.com/llvm/llvm-project.git", ) @@ -41,4 +41,4 @@ def _mlir_tutorial_deps_impl(module_ctx): mlir_tutorial_deps = module_extension( implementation = _mlir_tutorial_deps_impl, -) \ No newline at end of file +) diff --git a/externals/llvm-project b/externals/llvm-project index e8a891b..d9190f8 160000 --- a/externals/llvm-project +++ b/externals/llvm-project @@ -1 +1 @@ -Subproject commit e8a891b0f9d2a742ac3904116aaec2c7c9231b24 +Subproject commit d9190f8141661bd6120dea61d28ae8940fd775d0