Skip to content

Commit

Permalink
Analysis Passes (#28)
Browse files Browse the repository at this point in the history
* update llvm version to support change to integer range analysis

* Add noisy dialect

* implement InferIntRangeInterface on noisy ops

* add empty noisy-reduce-noise-optimizer transform

* add noise verification analysis check

* add a test where inserting reduce_noise is necessary

* add template for ReduceNoiseAnalysis

* add or-tools dependency

* implement solver model

* implement pass to use solver solution

* improve and add more noise reduction tests

* update CMake build

---------

Co-authored-by: Jeremy Kun <[email protected]>
  • Loading branch information
j2kun and j2kun authored Nov 15, 2023
1 parent 1c276e9 commit 6c0b9ce
Show file tree
Hide file tree
Showing 38 changed files with 1,243 additions and 43 deletions.
11 changes: 10 additions & 1 deletion .github/workflows/build_and_test_cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ jobs:
./externals/llvm-project
key: ${{ runner.os }}-cmake-${{ hashFiles('bazel/import_llvm.bzl') }}-${{ hashFiles('**/CMakeLists.txt') }}

- name: Cache mlir-tutorial build
id: cache-mlir-tutorial
uses: actions/cache@v3
with:
path: |
./build
key: ${{ runner.os }}-cmake-${{ hashFiles('bazel/import_llvm.bzl') }}-${{ hashFiles('**/CMakeLists.txt') }}

- name: Git config
run: |
git config --global --add safe.directory ${GITHUB_WORKSPACE}
Expand All @@ -47,9 +55,10 @@ jobs:
- name: Build and test mlir-tutorial
run: |
mkdir build && cd build
cmake -DLLVM_DIR=${GITHUB_WORKSPACE}/externals/llvm-project/build/lib/cmake/llvm -DMLIR_DIR=${GITHUB_WORKSPACE}/externals/llvm-project/build/lib/cmake/mlir ..
cmake -DLLVM_DIR=${GITHUB_WORKSPACE}/externals/llvm-project/build/lib/cmake/llvm -DMLIR_DIR=${GITHUB_WORKSPACE}/externals/llvm-project/build/lib/cmake/mlir -DBUILD_DEPS="ON" ..
cmake --build . --target MLIRAffineFullUnrollPasses
cmake --build . --target MLIRMulToAddPasses
cmake --build . --target MLIRNoisyPasses
cmake --build . --target mlir-headers
cmake --build . --target tutorial-opt
cmake --build . --target check-mlir-tutorial
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ bazel-mlir-tutorial
bazel-out
bazel-testlogs

# git submodule
externals

# cmake related files
# ignore the user specified CMake presets in subproject directories.
/*/CMakeUserPresets.json
Expand Down
13 changes: 12 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.20.0)
project(mlir-tutorial LANGUAGES CXX C)

set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

find_package(MLIR REQUIRED CONFIG)

Expand All @@ -22,6 +23,16 @@ include_directories(${PROJECT_SOURCE_DIR})
include_directories(${PROJECT_SOURCE_DIR}/externals/llvm-project)
include_directories(${PROJECT_BINARY_DIR})

message(STATUS "Fetching or-tools...")
include(FetchContent)
FetchContent_Declare(
or-tools
GIT_REPOSITORY https://github.com/google/or-tools.git
GIT_TAG main
)
FetchContent_MakeAvailable(or-tools)
message(STATUS "Done fetching or-tools")

add_subdirectory(tests)
add_subdirectory(tools)
add_subdirectory(lib)
add_subdirectory(lib)
47 changes: 24 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
# MLIR For Beginners

This is the code repository for a series of articles
on the [MLIR framework](https://mlir.llvm.org/) for building compilers.
This is the code repository for a series of articles on the
[MLIR framework](https://mlir.llvm.org/) for building compilers.

## Articles

1. [Build System (Getting Started)](https://jeremykun.com/2023/08/10/mlir-getting-started/)
2. [Running and Testing a Lowering](https://jeremykun.com/2023/08/10/mlir-running-and-testing-a-lowering/)
3. [Writing Our First Pass](https://jeremykun.com/2023/08/10/mlir-writing-our-first-pass/)
4. [Using Tablegen for Passes](https://jeremykun.com/2023/08/10/mlir-using-tablegen-for-passes/)
5. [Defining a New Dialect](https://jeremykun.com/2023/08/21/mlir-defining-a-new-dialect/)
6. [Using Traits](https://jeremykun.com/2023/09/07/mlir-using-traits/)
7. [Folders and Constant Propagation](https://jeremykun.com/2023/09/11/mlir-folders/)
8. [Verifiers](https://jeremykun.com/2023/09/13/mlir-verifiers/)
9. [Canonicalizers and Declarative Rewrite Patterns](https://jeremykun.com/2023/09/20/mlir-canonicalizers-and-declarative-rewrite-patterns/)
1. [Build System (Getting Started)](https://jeremykun.com/2023/08/10/mlir-getting-started/)
2. [Running and Testing a Lowering](https://jeremykun.com/2023/08/10/mlir-running-and-testing-a-lowering/)
3. [Writing Our First Pass](https://jeremykun.com/2023/08/10/mlir-writing-our-first-pass/)
4. [Using Tablegen for Passes](https://jeremykun.com/2023/08/10/mlir-using-tablegen-for-passes/)
5. [Defining a New Dialect](https://jeremykun.com/2023/08/21/mlir-defining-a-new-dialect/)
6. [Using Traits](https://jeremykun.com/2023/09/07/mlir-using-traits/)
7. [Folders and Constant Propagation](https://jeremykun.com/2023/09/11/mlir-folders/)
8. [Verifiers](https://jeremykun.com/2023/09/13/mlir-verifiers/)
9. [Canonicalizers and Declarative Rewrite Patterns](https://jeremykun.com/2023/09/20/mlir-canonicalizers-and-declarative-rewrite-patterns/)
10. [Dialect Conversion](https://jeremykun.com/2023/10/23/mlir-dialect-conversion/)
11. [Lowering through LLVM](https://jeremykun.com/2023/11/01/mlir-lowering-through-llvm/)


## 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](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)](https://jeremykun.com/2023/08/10/mlir-getting-started/). The CMake
build is maintained, but was added at article 10 (Dialect Conversion) and will
not be explained in the articles.
the tutorial series and explained in the first article,
[Build System (Getting Started)](https://jeremykun.com/2023/08/10/mlir-getting-started/).
The CMake build is maintained, but was added at article 10 (Dialect Conversion)
and will not be explained in the articles.

### Prerequisites

Expand All @@ -51,16 +50,16 @@ bazel test ...:all

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/](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.
[https://cmake.org/getting-started/](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

* Make sure you have installed everything needed to build LLVM
https://llvm.org/docs/GettingStarted.html#software
* For this recipe Ninja is used so be sure to have it as well installed
https://github.com/ninja-build/ninja/wiki/Pre-built-Ninja-packages
* Make sure you have installed everything needed to build LLVM
https://llvm.org/docs/GettingStarted.html#software
* For this recipe Ninja is used so be sure to have it as well installed
https://github.com/ninja-build/ninja/wiki/Pre-built-Ninja-packages

### Checking out the code

Expand Down Expand Up @@ -131,12 +130,14 @@ 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" \
-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
Expand Down
123 changes: 122 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
workspace(name = "mlir_tutorial")

load("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository", "new_git_repository")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")

Expand All @@ -18,7 +18,9 @@ http_archive(
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

bazel_skylib_workspace()

load("@bazel_skylib//lib:versions.bzl", "versions")

versions.check(minimum_bazel_version = "6.3.2")

# A two-step process for buliding LLVM/MLIR with bazel. First the raw source
Expand Down Expand Up @@ -108,3 +110,122 @@ pip_parse(
load("@mlir_tutorial_pip_deps//:requirements.bzl", "install_deps")

install_deps()

##### Deps for or-tools #####

## Bazel rules.
git_repository(
name = "platforms",
commit = "380c85cc2c7b126c6e354f517dc16d89fe760c9f",
remote = "https://github.com/bazelbuild/platforms.git",
)

git_repository(
name = "rules_proto",
commit = "3f1ab99b718e3e7dd86ebdc49c580aa6a126b1cd",
remote = "https://github.com/bazelbuild/rules_proto.git",
)

## ZLIB
new_git_repository(
name = "zlib",
build_file = "@com_google_protobuf//:third_party/zlib.BUILD",
commit = "04f42ceca40f73e2978b50e93806c2a18c1281fc",
remote = "https://github.com/madler/zlib.git",
)

## Re2
git_repository(
name = "com_google_re2",
remote = "https://github.com/google/re2.git",
tag = "2023-07-01",
)

## Abseil-cpp
git_repository(
name = "com_google_absl",
commit = "c2435f8342c2d0ed8101cb43adfd605fdc52dca2",
patch_args = ["-p1"],
patches = ["@com_google_ortools//patches:abseil-cpp-20230125.3.patch"],
remote = "https://github.com/abseil/abseil-cpp.git",
)

## Protobuf
git_repository(
name = "com_google_protobuf",
# there's a patch for the CMake build in protobuf, ignoring
# patches = ["@com_google_ortools//patches:protobuf-v23.3.patch"],
commit = "4dd15db6eb3955745f379d28fb4a2fcfb6753de3",
patch_args = ["-p1"],
remote = "https://github.com/protocolbuffers/protobuf.git",
)

# Load common dependencies.
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")

protobuf_deps()

## Solvers
http_archive(
name = "glpk",
build_file = "@com_google_ortools//bazel:glpk.BUILD",
sha256 = "4a1013eebb50f728fc601bdd833b0b2870333c3b3e5a816eeba921d95bec6f15",
url = "http://ftp.gnu.org/gnu/glpk/glpk-5.0.tar.gz",
)

http_archive(
name = "bliss",
build_file = "@com_google_ortools//bazel:bliss.BUILD",
patches = ["@com_google_ortools//bazel:bliss-0.73.patch"],
sha256 = "f57bf32804140cad58b1240b804e0dbd68f7e6bf67eba8e0c0fa3a62fd7f0f84",
url = "https://github.com/google/or-tools/releases/download/v9.0/bliss-0.73.zip",
#url = "http://www.tcs.hut.fi/Software/bliss/bliss-0.73.zip",
)

new_git_repository(
name = "scip",
build_file = "@com_google_ortools//bazel:scip.BUILD",
commit = "62fab8a2e3708f3452fad473a6f48715c367316b",
patch_args = ["-p1"],
patches = ["@com_google_ortools//bazel:scip.patch"],
remote = "https://github.com/scipopt/scip.git",
)

# Eigen has no Bazel build.
new_git_repository(
name = "eigen",
build_file_content =
"""
cc_library(
name = 'eigen3',
srcs = [],
includes = ['.'],
hdrs = glob(['Eigen/**']),
visibility = ['//visibility:public'],
)
""",
commit = "3147391d946bb4b6c68edd901f2add6ac1f31f8c",
remote = "https://gitlab.com/libeigen/eigen.git",
)

git_repository(
name = "highs",
branch = "bazel",
remote = "https://github.com/ERGO-Code/HiGHS.git",
)

## Swig support
# pcre source code repository
new_git_repository(
name = "pcre2",
build_file = "@com_google_ortools//bazel:pcre2.BUILD",
remote = "https://github.com/PCRE2Project/pcre2.git",
tag = "pcre2-10.42",
)

git_repository(
name = "com_google_ortools",
commit = "1d696f9108a0ebfd99feb73b9211e2f5a6b0812b",
remote = "https://github.com/google/or-tools.git",
shallow_since = "1647023481 +0100",
)
4 changes: 2 additions & 2 deletions bazel/import_llvm.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ load(
def import_llvm(name):
"""Imports LLVM."""

# 2023-10-30
LLVM_COMMIT = "896749aa0d420ae573255a64a349bc2a76cfed37"
# 2023-11-13
LLVM_COMMIT = "f778eafdd878e8b11ad76f9e0a312ce7791a7481"

new_git_repository(
name = name,
Expand Down
2 changes: 1 addition & 1 deletion externals/llvm-project
Submodule llvm-project updated 5948 files
1 change: 1 addition & 0 deletions lib/Analysis/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(ReduceNoiseAnalysis)
16 changes: 16 additions & 0 deletions lib/Analysis/ReduceNoiseAnalysis/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package(
default_visibility = ["//visibility:public"],
)

cc_library(
name = "ReduceNoiseAnalysis",
srcs = ["ReduceNoiseAnalysis.cpp"],
hdrs = ["ReduceNoiseAnalysis.h"],
deps = [
"//lib/Dialect/Noisy",
"@com_google_ortools//ortools/base",
"@com_google_ortools//ortools/linear_solver",
"@llvm-project//llvm:Support",
"@llvm-project//mlir:IR",
],
)
8 changes: 8 additions & 0 deletions lib/Analysis/ReduceNoiseAnalysis/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
add_mlir_library(ReduceNoiseAnalysis
ReduceNoiseAnalysis.cpp

${PROJECT_SOURCE_DIR}/lib/Analysis/ReduceNoiseAnalysis/
ADDITIONAL_HEADER_DIRS
LINK_LIBS PUBLIC
ortools::ortools
)
Loading

0 comments on commit 6c0b9ce

Please sign in to comment.