Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
common --enable_bzlmod
common --action_env=BAZEL_CXXOPTS=-std=c++17
common --cxxopt='-std=c++17'
common --deleted_packages=externals
Expand Down
2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.4.0
8.3.1
6 changes: 3 additions & 3 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/build_and_test_cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ 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
uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # pin@v4.2.3
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: |
Expand All @@ -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}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
85 changes: 85 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
###############################################################################
# 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",
)

# 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")
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")
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"
)

# 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",
targets = _LLVM_TARGETS,
)

# Configure Python dependencies
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
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.13",
requirements_lock = "//:requirements.txt",
)
use_repo(pip, "mlir_tutorial_pip_deps")
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Loading