This repository contains supporting material for the tutorial which was presented in the 29th Brazilian Symposium on Programming Languages (SBLP 2025).
This tutorial is a brief introduction to MLIR. You can expect to learn basic concepts, such as dialects, operations, and passes. For more in-depth information about MLIR, we recommend checking out the resources in the official MLIR page.
This is a very simple pass which replaces multiplications by constant integer powers of two with an arithmetic shift left. In compiler construction, this kind of optimization which replaces an operation by a cheaper equivalent is called strength reduction.
The purpose of this pass is to demonstrate how to use MLIR's pattern rewriting infrastructure, which is a very useful method for applying transformations based on pattern matching.
This is a more involved pass, which applies loop tiling to all linalg operations present in the IR. The pass will compute the tile size as the greatest common divisor of the dimensions of all operands for a given linalg op.
This pass illustrates many useful concepts for MLIR transformations, such as IR traversal and rewriting.
We have included two examples of pass pipelines. Pipelines are simply sequences of passes which are applied in order, using the output of a pass as the input of the subsequent one.
The optimize-loops pipeline applies a few optimizations and transformations that are useful for linalg ops, such as bufferization, loop tiling and loop unrolling. The mlir-to-llvm pipeline applies a sequence of conversions passes to lower the MLIR example with linalg operations to the llvm dialect, which can be later translated to LLVM IR.
This tool is an extension of the mlir-opt binary which adds all passes and pipelines implemented for the tutorial. *-opt tools are usually used in MLIR-based projects as the entry point for running transformations, optimizations, and lowerings.
We have included an example of a test infrastructure which follows the guidelines for LLVM-based projects. These tests use llvm-lit and FileCheck to verify that sblp-opt outputs the IR as expected when applying different transformations. This is not crucial for learning MLIR and thus is only a complement to the tutorial. However, testing is essential to the work of a compiler enginner so it's good to have a context of how to test MLIR-based projects.
We haven't tested running this code on Windows, therefore we recommend using Linux or macOS. These are the software requirements for running the code in this tutorial:
| Dependency | Version | Installation Link |
|---|---|---|
| LLVM | >= 21 | llvm.org |
| Python | >= 3.10 | python.org |
| CMake | >= 3.20 | cmake.org |
| Ninja | >= 1.10 | ninja-build GitHub |
When building LLVM, be sure to enable the MLIR project in the LLVM_ENABLE_PROJECTS CMake variable. Here are some commands that can be used:
git clone -b llvmorg-21.1.0 https://github.com/llvm/llvm-project.git
mkdir llvm-project/build
cd llvm-project/build
cmake -G Ninja ../llvm \
-DLLVM_ENABLE_PROJECTS="mlir;clang" \
-DLLVM_BUILD_EXAMPLES=ON \
-DLLVM_TARGETS_TO_BUILD="Native" \
-DCMAKE_BUILD_TYPE=Debug \
-DLLVM_ENABLE_ASSERTIONS=ON
ninjaFor more information on how to build LLVM, you can check Prof. Fernando Pereira's video on the topic.
This repository includes an utility script to easily build the MLIR tutorial project. To use it, you must set the LLVM_BUILD_DIR variable to the directory where you have built LLVM. Here's an example:
export LLVM_BUILD_DIR=path/to/llvm-project/build
./scripts/build.shThis will output the sblp-opt binary in the ./build/bin directory. Once you have built the project, you can run tests with the following command:
ninja -C build check-sblpWe have written a paper to be used as a guide for this tutorial.