Skip to content

Commit e2f5e5b

Browse files
committed
Bump Bazel version to 8.3.1 and migrate to bzlmod
1 parent 5f38e79 commit e2f5e5b

File tree

9 files changed

+285
-233
lines changed

9 files changed

+285
-233
lines changed

.bazelrc

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1+
# Enable Bzlmod for every Bazel command
2+
common --enable_bzlmod
3+
4+
# Original configurations
15
common --action_env=BAZEL_CXXOPTS=-std=c++17
6+
build --action_env=CCACHE_DISABLE=1
7+
build --action_env=CC=/usr/bin/gcc
8+
build --action_env=CXX=/usr/bin/g++
29
common --cxxopt='-std=c++17'
310
common --deleted_packages=externals
411
build:macos --apple_platform_type=macos
@@ -8,3 +15,51 @@ build:macos_arm64 --cpu=darwin_arm64
815
common --copt=-fdiagnostics-color=always
916
common --test_output=errors
1017
common -c dbg
18+
19+
# Additional optimizations for C++ compilation
20+
# Note: boost.thread requires exceptions, boost.serialization requires RTTI
21+
build --cxxopt=-fexceptions
22+
# Disabled RTTI as some boost components need it
23+
# build --cxxopt=-fno-rtti
24+
25+
# GNU source compatibility
26+
build --cxxopt=-D_GNU_SOURCE
27+
build --cxxopt=-D__STDC_CONSTANT_MACROS
28+
build --cxxopt=-D__STDC_FORMAT_MACROS
29+
build --cxxopt=-D__STDC_LIMIT_MACROS
30+
31+
# Debug build configuration (similar to CMAKE_BUILD_TYPE=DEBUG)
32+
build:debug --cxxopt=-g
33+
build:debug --cxxopt=-O0
34+
build:debug --strip=never
35+
build:debug --copt=-DDEBUG
36+
37+
# Release build configuration (similar to CMAKE_BUILD_TYPE=RELEASE)
38+
build:release --cxxopt=-O3
39+
build:release --cxxopt=-DNDEBUG
40+
build:release --strip=always
41+
42+
# Build with assertions enabled (similar to LLVM_ENABLE_ASSERTIONS=ON)
43+
build:assertions --cxxopt=-UNDEBUG
44+
build:assertions --cxxopt=-DMLIR_ENABLE_ASSERTIONS
45+
46+
# Verbose output (similar to ninja -v)
47+
build:verbose --subcommands=pretty_print
48+
build:verbose --verbose_failures
49+
50+
# Parallel build (similar to ninja -j)
51+
build --jobs=auto
52+
53+
# Test configuration
54+
test --test_output=errors
55+
test --test_summary=short
56+
57+
# Cache configuration, disable ccache since bazel cache system is much better
58+
build --disk_cache=~/.cache/bazel
59+
build --repository_cache=~/.cache/bazel/repo
60+
61+
# Compatibility settings
62+
build --incompatible_enable_cc_toolchain_resolution
63+
64+
# Sandbox Location configuration
65+
build --sandbox_base=/dev/shm

.bazelversion

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6.4.0
1+
8.3.1

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ bazel-bin
33
bazel-mlir-tutorial
44
bazel-out
55
bazel-testlogs
6+
MODULE.bazel.lock
67

78
# cmake related files
89
# ignore the user specified CMake presets in subproject directories.

CLAUDE.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# CLAUDE.md
2+
3+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4+
5+
## Project Overview
6+
7+
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.
8+
9+
## Build Systems
10+
11+
The project supports two build systems:
12+
13+
### Bazel (Primary)
14+
- **Version**: Bazel 8.3.1 with Bzlmod enabled
15+
- **Dependency Management**: Uses MODULE.bazel with Bazel Central Registry (BCR)
16+
- **Build all**: `bazel build ...:all`
17+
- **Test all**: `bazel test ...:all`
18+
- **Build specific target**: `bazel build //tools:tutorial-opt`
19+
- **Test specific target**: `bazel test //tests:poly_syntax`
20+
21+
#### Bzlmod Migration
22+
The project has been migrated from WORKSPACE-based dependency management to Bzlmod:
23+
- **MODULE.bazel**: Main module definition with dependencies from BCR
24+
- **extensions.bzl**: Custom module extension for LLVM-related dependencies
25+
- **WORKSPACE**: Simplified to workspace name only
26+
- Most dependencies (rules_python, protobuf, or-tools, etc.) now use BCR
27+
- LLVM dependencies still use git repositories for latest upstream integration
28+
29+
### CMake (Secondary)
30+
First build LLVM/MLIR dependencies, then:
31+
- **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 .`
32+
- **Build main tool**: `cmake --build build-ninja --target tutorial-opt`
33+
- **Run tests**: `cmake --build build-ninja --target check-mlir-tutorial`
34+
35+
## Key Architecture
36+
37+
### Core Components
38+
- **tutorial-opt**: Main compiler tool in `tools/tutorial-opt.cpp` - processes MLIR files through various passes
39+
- **Custom Dialects**:
40+
- `Poly`: Polynomial arithmetic dialect (`lib/Dialect/Poly/`)
41+
- `Noisy`: Demonstration dialect with noise operations (`lib/Dialect/Noisy/`)
42+
- **Transforms**: Located in `lib/Transform/` with subdirectories for different pass categories
43+
- **Conversions**: Dialect lowering passes in `lib/Conversion/`
44+
- **Analysis**: Data flow analysis passes in `lib/Analysis/`
45+
46+
### Test Infrastructure
47+
- Uses LLVM's `lit` testing framework
48+
- Test files in `tests/` directory with `.mlir` extension
49+
- Run individual tests: `bazel test //tests:test_name`
50+
51+
### Pass Pipeline
52+
The `poly-to-llvm` pipeline demonstrates full lowering:
53+
1. Poly dialect → Standard dialect
54+
2. Standard → Linalg
55+
3. Bufferization
56+
4. Linalg → Loops
57+
5. Loops → Control Flow
58+
6. Control Flow → LLVM IR
59+
60+
## Development Commands
61+
62+
### Testing Individual Components
63+
- **Syntax test**: `tutorial-opt tests/poly_syntax.mlir`
64+
- **Canonicalization**: `tutorial-opt --canonicalize tests/poly_canonicalize.mlir`
65+
- **Full lowering**: `tutorial-opt --poly-to-llvm tests/poly_to_llvm.mlir`
66+
67+
### Adding New Passes
68+
1. Define in appropriate `lib/Transform/*/Passes.td`
69+
2. Implement in corresponding `.cpp` file
70+
3. Register in `tools/tutorial-opt.cpp`
71+
4. Add test in `tests/`
72+
73+
### Tablegen Files
74+
- Dialect definitions: `*.td` files in `lib/Dialect/*/`
75+
- Pass definitions: `Passes.td` files in `lib/Transform/*/`
76+
- Pattern definitions: `*.td` files for rewrite patterns
77+
78+
## File Structure
79+
- `lib/`: Core implementation (dialects, passes, conversions, analysis)
80+
- `tools/`: Command-line tools (mainly tutorial-opt)
81+
- `tests/`: Test files using lit framework
82+
- `bazel/`: Bazel build configuration (legacy, LLVM import utilities)
83+
- `externals/`: LLVM/MLIR submodule (for CMake builds)
84+
- `MODULE.bazel`: Bzlmod module definition with BCR dependencies
85+
- `extensions.bzl`: Custom module extension for LLVM dependencies
86+
- `WORKSPACE`: Simplified workspace marker (legacy dependencies removed)
87+
- `.bazelversion`: Pins Bazel version to 8.3.1
88+
- `.bazelrc`: Build configuration with Bzlmod enabled

MODULE.bazel

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
###############################################################################
2+
# Bazel now uses Bzlmod by default to manage external dependencies.
3+
# Please consider migrating your external dependencies from WORKSPACE to MODULE.bazel.
4+
#
5+
# For more details, please check https://github.com/bazelbuild/bazel/issues/18958
6+
###############################################################################
7+
8+
module(
9+
name = "mlir_tutorial",
10+
version = "1.0.0",
11+
repo_name = "mlir_tutorial",
12+
)
13+
14+
# Core Bazel dependencies that are available in BCR
15+
bazel_dep(name = "bazel_skylib", version = "1.7.1")
16+
bazel_dep(name = "rules_python", version = "1.2.0")
17+
bazel_dep(name = "platforms", version = "0.0.11")
18+
bazel_dep(name = "rules_cc", version = "0.1.1")
19+
bazel_dep(name = "rules_java", version = "8.12.0")
20+
bazel_dep(name = "protobuf", version = "30.1")
21+
bazel_dep(name = "rules_proto", version = "7.1.0")
22+
bazel_dep(name = "rules_pkg", version = "1.1.0")
23+
24+
# External dependencies available in BCR
25+
bazel_dep(name = "re2", version = "2024-07-02.bcr.1")
26+
bazel_dep(name = "abseil-cpp", version = "20250512.1")
27+
bazel_dep(name = "or-tools", version = "9.12")
28+
bazel_dep(name = "eigen", version = "4.0.0-20241125.bcr.2")
29+
bazel_dep(name = "highs", version = "1.11.0")
30+
bazel_dep(name = "pcre2", version = "10.46-DEV")
31+
bazel_dep(name = "glpk", version = "5.0.bcr.4")
32+
bazel_dep(name = "bliss", version = "0.73")
33+
bazel_dep(name = "scip", version = "9.2.0.bcr.3")
34+
bazel_dep(name = "zlib-ng", version = "2.0.7")
35+
36+
# Hedron's Compile Commands Extractor for Bazel
37+
# https://github.com/hedronvision/bazel-compile-commands-extractor
38+
bazel_dep(name = "hedron_compile_commands", dev_dependency = True)
39+
git_override(
40+
module_name = "hedron_compile_commands",
41+
remote = "https://github.com/hedronvision/bazel-compile-commands-extractor.git",
42+
commit = "0e990032f3c5a866e72615cf67e5ce22186dcb97",
43+
# Replace the commit hash (above) with the latest (https://github.com/hedronvision/bazel-compile-commands-extractor/commits/main).
44+
# Even better, set up Renovate and let it do the work for you (see "Suggestion: Updates" in the README).
45+
)
46+
47+
# Use module extensions for LLVM and other dependencies that aren't in BCR
48+
mlir_tutorial_deps = use_extension("//:extensions.bzl", "mlir_tutorial_deps")
49+
use_repo(mlir_tutorial_deps,
50+
"llvm-raw",
51+
"llvm_zstd",
52+
"llvm_zlib"
53+
)
54+
55+
# Configure LLVM project using use_repo_rule
56+
llvm_configure = use_repo_rule("@llvm-raw//utils/bazel:configure.bzl", "llvm_configure")
57+
llvm_configure(name = "llvm-project")
58+
59+
# Configure Python dependencies
60+
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
61+
python.toolchain(python_version = "3.10")
62+
use_repo(python, "python_3_10")
63+
64+
pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
65+
pip.parse(
66+
hub_name = "mlir_tutorial_pip_deps",
67+
python_version = "3.10",
68+
requirements_lock = "//:requirements.txt",
69+
)
70+
use_repo(pip, "mlir_tutorial_pip_deps")

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ the tutorial series and explained in the first article,
3030
The CMake build is maintained, but was added at article 10 (Dialect Conversion)
3131
and will not be explained in the articles.
3232

33+
**Note**: This project has been upgraded to Bazel 8.3.1 and migrated to use
34+
Bzlmod for dependency management, replacing the traditional WORKSPACE file
35+
approach. Dependencies are now managed through `MODULE.bazel` using the
36+
Bazel Central Registry (BCR) where possible.
37+
3338
### Prerequisites
3439

3540
Install Bazelisk via instructions at
@@ -39,6 +44,9 @@ This should create the `bazel` command on your system.
3944
You should also have a modern C++ compiler on your system, either `gcc` or
4045
`clang`, which Bazel will detect.
4146

47+
**Bazel Version**: This project requires Bazel 8.3.1 or newer. The specific
48+
version is pinned in `.bazelversion`.
49+
4250
### Build and test
4351

4452
Run
@@ -48,6 +56,19 @@ bazel build ...:all
4856
bazel test ...:all
4957
```
5058

59+
### Dependency Management
60+
61+
The project uses Bzlmod (MODULE.bazel) for dependency management:
62+
63+
- **Core dependencies**: Managed through Bazel Central Registry (BCR)
64+
- rules_python, rules_java, protobuf, abseil-cpp, or-tools, etc.
65+
- **LLVM dependencies**: Managed through custom module extension
66+
- LLVM/MLIR source code via git repository
67+
- **Development tools**: hedron_compile_commands via git_override
68+
69+
This approach provides better dependency resolution, versioning, and
70+
compatibility compared to the legacy WORKSPACE approach.
71+
5172
## CMake build
5273

5374
CMake is one of two supported build systems for this tutorial. The other is

0 commit comments

Comments
 (0)