Giter VIP home page Giter VIP logo

mlir-tutorial's Introduction

MLIR For Beginners

This is the code repository for a series of articles on the MLIR framework for building compilers.

Articles

  1. Build System (Getting Started)
  2. Running and Testing a Lowering
  3. Writing Our First Pass
  4. Using Tablegen for Passes
  5. Defining a New Dialect
  6. Using Traits
  7. Folders and Constant Propagation
  8. Verifiers
  9. Canonicalizers and Declarative Rewrite Patterns
  10. Dialect Conversion
  11. Lowering through LLVM
  12. A Global Optimization and Dataflow Analysis

Bazel build

Bazel is one of two supported build systems for this tutorial. The other is CMake. If you're unfamiliar with Bazel, you can read the tutorials at https://bazel.build/start. Familiarity with Bazel is not required to build or test, but it is required to follow the articles in the tutorial series and explained in the first article, Build System (Getting Started). The CMake build is maintained, but was added at article 10 (Dialect Conversion) and will not be explained in the articles.

Prerequisites

Install Bazelisk via instructions at https://github.com/bazelbuild/bazelisk#installation. 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.

Build and test

Run

bazel build ...:all
bazel test ...:all

CMake build

CMake is one of two supported build systems for this tutorial. The other is Bazel. If you're unfamiliar with CMake, you can read the tutorials at https://cmake.org/getting-started/. The CMake build is maintained, but was added at article 10 (Dialect Conversion) and will not be explained in the articles.

Prerequisites

Checking out the code

Checkout the tutorial including the LLVM dependency (submodules):

git clone --recurse-submodules https://github.com/j2kun/mlir-tutorial.git
cd mlir-tutorial

Building dependencies

Note: The following steps are suitable for macOs and use ninja as building system, they should not be hard to adapt for your environment.

Build LLVM/MLIR

#!/bin/sh

BUILD_SYSTEM=Ninja
BUILD_TAG=ninja
THIRDPARTY_LLVM_DIR=$PWD/externals/llvm-project
BUILD_DIR=$THIRDPARTY_LLVM_DIR/build
INSTALL_DIR=$THIRDPARTY_LLVM_DIR/install

mkdir -p $BUILD_DIR
mkdir -p $INSTALL_DIR

pushd $BUILD_DIR

cmake ../llvm -G $BUILD_SYSTEM \
      -DCMAKE_CXX_COMPILER="$(xcrun --find clang++)" \
      -DCMAKE_C_COMPILER="$(xcrun --find clang)" \
      -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR \
      -DLLVM_LOCAL_RPATH=$INSTALL_DIR/lib \
      -DLLVM_PARALLEL_COMPILE_JOBS=7 \
      -DLLVM_PARALLEL_LINK_JOBS=1 \
      -DLLVM_BUILD_EXAMPLES=OFF \
      -DLLVM_INSTALL_UTILS=ON \
      -DCMAKE_OSX_ARCHITECTURES="$(uname -m)" \
      -DCMAKE_BUILD_TYPE=Release \
      -DLLVM_ENABLE_ASSERTIONS=ON \
      -DLLVM_CCACHE_BUILD=ON \
      -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
      -DLLVM_ENABLE_PROJECTS='mlir' \
      -DDEFAULT_SYSROOT="$(xcrun --show-sdk-path)" \
      -DCMAKE_OSX_SYSROOT="$(xcrun --show-sdk-path)"

cmake --build . --target check-mlir

popd

Build and test

#!/bin/sh

BUILD_SYSTEM="Ninja"
BUILD_DIR=./build-`echo ${BUILD_SYSTEM}| tr '[:upper:]' '[:lower:]'`

rm -rf $BUILD_DIR
mkdir $BUILD_DIR
pushd $BUILD_DIR

LLVM_BUILD_DIR=externals/llvm-project/build
cmake -G $BUILD_SYSTEM .. \
    -DLLVM_DIR="$LLVM_BUILD_DIR/lib/cmake/llvm" \
    -DMLIR_DIR="$LLVM_BUILD_DIR/lib/cmake/mlir" \
    -DBUILD_DEPS="ON" \
    -DBUILD_SHARED_LIBS="OFF" \
    -DCMAKE_BUILD_TYPE=Debug

popd

cmake --build $BUILD_DIR --target MLIRAffineFullUnrollPasses
cmake --build $BUILD_DIR --target MLIRMulToAddPasses
cmake --build $BUILD_DIR --target MLIRNoisyPasses
cmake --build $BUILD_DIR --target mlir-headers
cmake --build $BUILD_DIR --target mlir-doc
cmake --build $BUILD_DIR --target tutorial-opt
cmake --build $BUILD_DIR --target check-mlir-tutorial

mlir-tutorial's People

Contributors

j2kun avatar jagt avatar jangmin-deepx avatar vguerra avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mlir-tutorial's Issues

Apple silicon requires an additional `_LLVM_TARGETS`

Hi again! I hope that my issue raising comes across as intending to help rather than complain; please let me know if I get too noisy/annoying!

In order to get everything to build on my M1 Mac, I needed to add "AArch64" to the _LLVM_TARGETS list in bazel/setup_llvm.bzl. Perhaps this step is obvious to those more in the know, but I wasn't sure of the exact string to put in to get things to build at first ("darwin-arm64"?).

Thanks again for this tutorial!

Question on test `affine_loop_unroll.mlir`

Specifically regarding the test on function @test_doubly_nested_loop: should line 23 be as well preceded by a // CHECK-NOT: affine.for? IIUC, the point of the test is to make sure that nested instances of affine.for are as well transformed or it is not really necessary?

Linking Error with lib/Dialect/Poly/libMLIRPoly.dylib in MLIR Project

Hi~, I tried to build this tutorial as below commit version and also build corresponding correct external mlir project
I am encountering linking errors when building a shared library (lib/Dialect/Poly/libMLIRPoly.dylib). The error occurs during the linking phase and appears to be related to undefined symbols in the MLIR framework.
For example:

Undefined symbols:
mlir::Diagnostic::operator<<(llvm::Twine&&), referenced from: ...
mlir::ValueRange::dereference_iterator(llvm::PointerUnion<mlir::Value const*, mlir::OpOperand*, mlir::detail::OpResultImpl*> const&, long), referenced from: 

Outline

  • Build system
  • Running mlir-opt and testing
  • Writing a first pass (trivial wrapper around fullUnroll, use rewrite pattern framework)
  • Converting passes to tablegen
  • Defining a new dialect (poly)
  • Writing a dialect conversion pass to tensor+affine (and compiling down through LLVM to machine code?)
  • Adding folders/canonicalizers
  • Interfaces and Traits?

Add SparseConstantPropagation to the data flow solver demo

See https://discourse.llvm.org/t/mlir-dead-code-analysis/67568/8

I tried out the data flow analysis framework, and also realized the DeadCodeAnalysis was required for pretty much every analysis I wanted to do. So I’m sympathetic to making this easier to get started with, but I’m also curious what making it a “built-in” feature would entail. Maybe rather than built-in in the sense of “always included in DataFlowSolver” is too strong, but we could provide some helper that includes some sensible defaults (e.g. DeadCodeAnalysis and SparseConstantPropagation), which users could extend.

Otherwise it won't properly handle control flow, which I didn't test enough in my tutorial, but ran into when I was working on this in HEIR.

Ignored `-std=c++17` on Mac OS X

First off, thank you very much for this tutorial! I've been meaning to pick up both bazel and MLIR, so this is extremely helpful.

One thing I stumbled over in getting set up: apparently, bazel automatically detects XCode and picks a different toolchain, which led to it ignoring the -std=c++17 setting. Exporting BAZEL_USE_CPP_ONLY_TOOLCHAIN=1 in my shell ensured that the setting went through. I thought I'd share to save others the trouble I went through.

Thanks again!

Early articles may have problems running lit

If you're following along PR by PR and checking out individual commits to run examples, you may encounter python complaining it can't find the lit module.

In the fourth article in the series I realized bazel was using the system Python, and aac8490 migrates to a bazel-managed python runtime.

A simple fix is to run pip install python in your system Python. Or else cherry pick aac84908f7b09ec1b14489bbc0837e697b191630 to your step in the article series.

Bazel errors on Ubuntu 22.04

Hi! Unfortunately, I'm still on the first page of this great guide. I'm trying to install this build on common systems (Ubuntu 22.04, Debian 12, FreeBSD 13.2). My actions (Ubuntu 22.04):

# apt install bazel-bootstrap bazel-skylib
# git clone https://github.com/j2kun/mlir-tutorial.git
# cd mlir-tutorial
# bazel build @llvm-project//mlir:IR

After this I get the following errors:

root@ubuntu:~/mlir-tutorial# bazel build @llvm-project//mlir:IR
INFO: Repository rules_python instantiated at:
  no stack (--record_rule_instantiation_callstack not enabled)
Repository rule new_git_repository defined at:
  /root/.cache/bazel/_bazel_root/fac3d405565bb727227c10fb24b5c557/external/bazel_tools/tools/build_defs/repo/git.bzl:182:37: in <toplevel>
ERROR: An error occurred during the fetch of repository 'rules_python':
   Traceback (most recent call last):
        File "/root/.cache/bazel/_bazel_root/fac3d405565bb727227c10fb24b5c557/external/bazel_tools/tools/build_defs/repo/git.bzl", line 169, column 13, in _new_git_repository_implementation
                fail("Exactly one of build_file and build_file_content must be provided.")
Error in fail: Exactly one of build_file and build_file_content must be provided.
ERROR: no such package '@rules_python//python': Exactly one of build_file and build_file_content must be provided.
INFO: Elapsed time: 0.056s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded)

I tried to compile this tutorial on Ubuntu 22.04, Debian 12 and FreeBSD 13.2, but no luck so far. What makes debugging Bazel even more difficult is that the first step (cloning the monorepository) is quite time-consuming. Given your excellent example code, I might even find it easier to write my own CMakeLists.txt files.

Thank you for prudently indicating the commit number from which the compiler infrastructure should be built. Could you provide the version of the operating system and package versions that you use to successfully build this tutorial, as well as the commit version of the mlir-tutorial repository itself?

Shared Library Build Option

Hi, Thank you so much for the tutorial series. It is really great and helped me a lot to understand a few core concepts of MLIR framework.

I was wondering if you can also include a tutorial where you show the way to build the passes as standalone shared library and show the process to call load the dialect and the pass library from command line using mlir-opt. I think this would be a great addition to the already great tutorial series.

Thanks in advance.

Someone who gets error on build with cmake

who uses C++ under 20 (17 or 13) might get error on build with cmake.

As ortools(stable or maybe main) uses C++20 (see here) should install C++20 or use ex-version of ortools.
in my case, ortools with v9.0 worked

message(STATUS "Fetching or-tools...")
include(FetchContent)
FetchContent_Declare(
  or-tools
  GIT_REPOSITORY https://github.com/google/or-tools.git
  GIT_TAG        v9.0     <== change here main -> v9.0
  )
FetchContent_MakeAvailable(or-tools)
message(STATUS "Done fetching or-tools")

I hope this issue helps someone with same issue.

Building with CMake

I just wanted to give a small guide for people looking to build this tutorial using CMake:

Prerequisites

Checking out the code

Checkout the tutorial including the LLVM dependency (submodules)

git clone --recurse-submodules https://github.com/j2kun/mlir-tutorial.git
cd mlir-tutorial

Building dependencies

Note: The following steps are suitable for macOs and use ninja as building system, they should not be hard to adapt for your environment.

Build LLVM/MLIR

#!/bin/sh

BUILD_SYSTEM=Ninja
BUILD_TAG=ninja
THIRDPARTY_LLVM_DIR=$PWD/externals/llvm-project
BUILD_DIR=$THIRDPARTY_LLVM_DIR/build
INSTALL_DIR=$THIRDPARTY_LLVM_DIR/install

mkdir -p $BUILD_DIR
mkdir -p $INSTALL_DIR

pushd $BUILD_DIR

cmake ../llvm -G $BUILD_SYSTEM \
      -DCMAKE_CXX_COMPILER="$(xcrun --find clang++)" \
      -DCMAKE_C_COMPILER="$(xcrun --find clang)" \
      -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR \
      -DLLVM_LOCAL_RPATH=$INSTALL_DIR/lib \
      -DLLVM_PARALLEL_COMPILE_JOBS=7 \
      -DLLVM_PARALLEL_LINK_JOBS=1 \
      -DLLVM_BUILD_EXAMPLES=OFF \
      -DLLVM_INSTALL_UTILS=ON \
      -DCMAKE_OSX_ARCHITECTURES="$(uname -m)" \
      -DCMAKE_BUILD_TYPE=Release \
      -DLLVM_ENABLE_ASSERTIONS=ON \
      -DLLVM_CCACHE_BUILD=ON \
      -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
      -DLLVM_ENABLE_PROJECTS='mlir' \
      -DDEFAULT_SYSROOT="$(xcrun --show-sdk-path)" \
      -DCMAKE_OSX_SYSROOT="$(xcrun --show-sdk-path)"

cmake --build . --target check-mlir

popd

Building the tutorial and running tests

#!/bin/sh

BUILD_SYSTEM="Ninja"
BUILD_DIR=./build-`echo ${BUILD_SYSTEM}| tr '[:upper:]' '[:lower:]'`

rm -rf $BUILD_DIR
mkdir $BUILD_DIR
pushd $BUILD_DIR

LLVM_BUILD_DIR=externals/llvm-project/build
cmake -G $BUILD_SYSTEM .. \
    -DLLVM_DIR="$LLVM_BUILD_DIR/lib/cmake/llvm" \
    -DMLIR_DIR="$LLVM_BUILD_DIR/lib/cmake/mlir" \
    -DCMAKE_BUILD_TYPE=Debug

popd

cmake --build $BUILD_DIR --target MLIRAffineFullUnrollPasses
cmake --build $BUILD_DIR --target MLIRMulToAddPasses
cmake --build $BUILD_DIR --target mlir-headers
cmake --build $BUILD_DIR --target mlir-doc
cmake --build $BUILD_DIR --target tutorial-opt
cmake --build $BUILD_DIR --target check-mlir-tutorial

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.