To build MLIR, follow the official guide: Getting Started.

Set up some environment variables to make our life easier when working with MLIR:

export LLVM_PROJ_HOME="/path/to/llvm-project"
export MLIR_HOME="$LLVM_PROJ_HOME/mlir"

Write a script to help build MLIR from source:

# @file $LLVM_PROJ_HOME/scripts/build-mlir.sh

cd $LLVM_PROJ_HOME

CC=clang CXX=clang++ \
cmake -S . -B ./build -G Ninja ./llvm \
    -DCMAKE_BUILD_TYPE=Release \
    -DLLVM_ENABLE_PROJECTS=mlir \
    -DLLVM_BUILD_EXAMPLES=ON \
    -DLLVM_TARGETS_TO_BUILD="Native;NVPTX;AMDGPU" \
    -DCMAKE_BUILD_TYPE=Release \
    -DLLVM_ENABLE_ASSERTIONS=ON \
    -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
    -DLLVM_ENABLE_LLD=ON

cmake --build . --target check-mlir -j $(nproc)

Now we can run the script to build the MLIR easily:

bash $LLVM_PROJ_HOME/scripts/build-mlir.sh

The generated binary files are in $LLVM_PROJ_HOME/build/bin.

It would be more convenient to add this directory to PATH when working with MLIR:

export PATH="$LLVM_PROJ_HOME/build/bin:$PATH"