Skip to content

Commit 6c0b9ce

Browse files
authored
Analysis Passes (#28)
* 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 <j2kun@users.noreply.github.com>
1 parent 1c276e9 commit 6c0b9ce

38 files changed

Lines changed: 1243 additions & 43 deletions

.github/workflows/build_and_test_cmake.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ jobs:
2929
./externals/llvm-project
3030
key: ${{ runner.os }}-cmake-${{ hashFiles('bazel/import_llvm.bzl') }}-${{ hashFiles('**/CMakeLists.txt') }}
3131

32+
- name: Cache mlir-tutorial build
33+
id: cache-mlir-tutorial
34+
uses: actions/cache@v3
35+
with:
36+
path: |
37+
./build
38+
key: ${{ runner.os }}-cmake-${{ hashFiles('bazel/import_llvm.bzl') }}-${{ hashFiles('**/CMakeLists.txt') }}
39+
3240
- name: Git config
3341
run: |
3442
git config --global --add safe.directory ${GITHUB_WORKSPACE}
@@ -47,9 +55,10 @@ jobs:
4755
- name: Build and test mlir-tutorial
4856
run: |
4957
mkdir build && cd build
50-
cmake -DLLVM_DIR=${GITHUB_WORKSPACE}/externals/llvm-project/build/lib/cmake/llvm -DMLIR_DIR=${GITHUB_WORKSPACE}/externals/llvm-project/build/lib/cmake/mlir ..
58+
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" ..
5159
cmake --build . --target MLIRAffineFullUnrollPasses
5260
cmake --build . --target MLIRMulToAddPasses
61+
cmake --build . --target MLIRNoisyPasses
5362
cmake --build . --target mlir-headers
5463
cmake --build . --target tutorial-opt
5564
cmake --build . --target check-mlir-tutorial

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ bazel-mlir-tutorial
44
bazel-out
55
bazel-testlogs
66

7-
# git submodule
8-
externals
9-
107
# cmake related files
118
# ignore the user specified CMake presets in subproject directories.
129
/*/CMakeUserPresets.json

CMakeLists.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.20.0)
33
project(mlir-tutorial LANGUAGES CXX C)
44

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

78
find_package(MLIR REQUIRED CONFIG)
89

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

26+
message(STATUS "Fetching or-tools...")
27+
include(FetchContent)
28+
FetchContent_Declare(
29+
or-tools
30+
GIT_REPOSITORY https://github.com/google/or-tools.git
31+
GIT_TAG main
32+
)
33+
FetchContent_MakeAvailable(or-tools)
34+
message(STATUS "Done fetching or-tools")
35+
2536
add_subdirectory(tests)
2637
add_subdirectory(tools)
27-
add_subdirectory(lib)
38+
add_subdirectory(lib)

README.md

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
11
# MLIR For Beginners
22

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

66
## Articles
77

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

20-
2120
## Bazel build
2221

2322
Bazel is one of two supported build systems for this tutorial. The other is
2423
CMake. If you're unfamiliar with Bazel, you can read the tutorials at
2524
[https://bazel.build/start](https://bazel.build/start). Familiarity with Bazel
2625
is not required to build or test, but it is required to follow the articles in
27-
the tutorial series and explained in the first article, [Build System (Getting
28-
Started)](https://jeremykun.com/2023/08/10/mlir-getting-started/). The CMake
29-
build is maintained, but was added at article 10 (Dialect Conversion) and will
30-
not be explained in the articles.
26+
the tutorial series and explained in the first article,
27+
[Build System (Getting Started)](https://jeremykun.com/2023/08/10/mlir-getting-started/).
28+
The CMake build is maintained, but was added at article 10 (Dialect Conversion)
29+
and will not be explained in the articles.
3130

3231
### Prerequisites
3332

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

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

5857
### Prerequisites
5958

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

6564
### Checking out the code
6665

@@ -131,12 +130,14 @@ LLVM_BUILD_DIR=externals/llvm-project/build
131130
cmake -G $BUILD_SYSTEM .. \
132131
-DLLVM_DIR="$LLVM_BUILD_DIR/lib/cmake/llvm" \
133132
-DMLIR_DIR="$LLVM_BUILD_DIR/lib/cmake/mlir" \
133+
-DBUILD_DEPS="ON" \
134134
-DCMAKE_BUILD_TYPE=Debug
135135

136136
popd
137137

138138
cmake --build $BUILD_DIR --target MLIRAffineFullUnrollPasses
139139
cmake --build $BUILD_DIR --target MLIRMulToAddPasses
140+
cmake --build $BUILD_DIR --target MLIRNoisyPasses
140141
cmake --build $BUILD_DIR --target mlir-headers
141142
cmake --build $BUILD_DIR --target mlir-doc
142143
cmake --build $BUILD_DIR --target tutorial-opt

WORKSPACE

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
workspace(name = "mlir_tutorial")
22

3-
load("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository")
3+
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository", "new_git_repository")
44
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
55
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
66

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

2020
bazel_skylib_workspace()
21+
2122
load("@bazel_skylib//lib:versions.bzl", "versions")
23+
2224
versions.check(minimum_bazel_version = "6.3.2")
2325

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

110112
install_deps()
113+
114+
##### Deps for or-tools #####
115+
116+
## Bazel rules.
117+
git_repository(
118+
name = "platforms",
119+
commit = "380c85cc2c7b126c6e354f517dc16d89fe760c9f",
120+
remote = "https://github.com/bazelbuild/platforms.git",
121+
)
122+
123+
git_repository(
124+
name = "rules_proto",
125+
commit = "3f1ab99b718e3e7dd86ebdc49c580aa6a126b1cd",
126+
remote = "https://github.com/bazelbuild/rules_proto.git",
127+
)
128+
129+
## ZLIB
130+
new_git_repository(
131+
name = "zlib",
132+
build_file = "@com_google_protobuf//:third_party/zlib.BUILD",
133+
commit = "04f42ceca40f73e2978b50e93806c2a18c1281fc",
134+
remote = "https://github.com/madler/zlib.git",
135+
)
136+
137+
## Re2
138+
git_repository(
139+
name = "com_google_re2",
140+
remote = "https://github.com/google/re2.git",
141+
tag = "2023-07-01",
142+
)
143+
144+
## Abseil-cpp
145+
git_repository(
146+
name = "com_google_absl",
147+
commit = "c2435f8342c2d0ed8101cb43adfd605fdc52dca2",
148+
patch_args = ["-p1"],
149+
patches = ["@com_google_ortools//patches:abseil-cpp-20230125.3.patch"],
150+
remote = "https://github.com/abseil/abseil-cpp.git",
151+
)
152+
153+
## Protobuf
154+
git_repository(
155+
name = "com_google_protobuf",
156+
# there's a patch for the CMake build in protobuf, ignoring
157+
# patches = ["@com_google_ortools//patches:protobuf-v23.3.patch"],
158+
commit = "4dd15db6eb3955745f379d28fb4a2fcfb6753de3",
159+
patch_args = ["-p1"],
160+
remote = "https://github.com/protocolbuffers/protobuf.git",
161+
)
162+
163+
# Load common dependencies.
164+
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
165+
166+
protobuf_deps()
167+
168+
## Solvers
169+
http_archive(
170+
name = "glpk",
171+
build_file = "@com_google_ortools//bazel:glpk.BUILD",
172+
sha256 = "4a1013eebb50f728fc601bdd833b0b2870333c3b3e5a816eeba921d95bec6f15",
173+
url = "http://ftp.gnu.org/gnu/glpk/glpk-5.0.tar.gz",
174+
)
175+
176+
http_archive(
177+
name = "bliss",
178+
build_file = "@com_google_ortools//bazel:bliss.BUILD",
179+
patches = ["@com_google_ortools//bazel:bliss-0.73.patch"],
180+
sha256 = "f57bf32804140cad58b1240b804e0dbd68f7e6bf67eba8e0c0fa3a62fd7f0f84",
181+
url = "https://github.com/google/or-tools/releases/download/v9.0/bliss-0.73.zip",
182+
#url = "http://www.tcs.hut.fi/Software/bliss/bliss-0.73.zip",
183+
)
184+
185+
new_git_repository(
186+
name = "scip",
187+
build_file = "@com_google_ortools//bazel:scip.BUILD",
188+
commit = "62fab8a2e3708f3452fad473a6f48715c367316b",
189+
patch_args = ["-p1"],
190+
patches = ["@com_google_ortools//bazel:scip.patch"],
191+
remote = "https://github.com/scipopt/scip.git",
192+
)
193+
194+
# Eigen has no Bazel build.
195+
new_git_repository(
196+
name = "eigen",
197+
build_file_content =
198+
"""
199+
cc_library(
200+
name = 'eigen3',
201+
srcs = [],
202+
includes = ['.'],
203+
hdrs = glob(['Eigen/**']),
204+
visibility = ['//visibility:public'],
205+
)
206+
""",
207+
commit = "3147391d946bb4b6c68edd901f2add6ac1f31f8c",
208+
remote = "https://gitlab.com/libeigen/eigen.git",
209+
)
210+
211+
git_repository(
212+
name = "highs",
213+
branch = "bazel",
214+
remote = "https://github.com/ERGO-Code/HiGHS.git",
215+
)
216+
217+
## Swig support
218+
# pcre source code repository
219+
new_git_repository(
220+
name = "pcre2",
221+
build_file = "@com_google_ortools//bazel:pcre2.BUILD",
222+
remote = "https://github.com/PCRE2Project/pcre2.git",
223+
tag = "pcre2-10.42",
224+
)
225+
226+
git_repository(
227+
name = "com_google_ortools",
228+
commit = "1d696f9108a0ebfd99feb73b9211e2f5a6b0812b",
229+
remote = "https://github.com/google/or-tools.git",
230+
shallow_since = "1647023481 +0100",
231+
)

bazel/import_llvm.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ load(
88
def import_llvm(name):
99
"""Imports LLVM."""
1010

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

1414
new_git_repository(
1515
name = name,

externals/llvm-project

Submodule llvm-project updated 5948 files

lib/Analysis/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(ReduceNoiseAnalysis)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package(
2+
default_visibility = ["//visibility:public"],
3+
)
4+
5+
cc_library(
6+
name = "ReduceNoiseAnalysis",
7+
srcs = ["ReduceNoiseAnalysis.cpp"],
8+
hdrs = ["ReduceNoiseAnalysis.h"],
9+
deps = [
10+
"//lib/Dialect/Noisy",
11+
"@com_google_ortools//ortools/base",
12+
"@com_google_ortools//ortools/linear_solver",
13+
"@llvm-project//llvm:Support",
14+
"@llvm-project//mlir:IR",
15+
],
16+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
add_mlir_library(ReduceNoiseAnalysis
2+
ReduceNoiseAnalysis.cpp
3+
4+
${PROJECT_SOURCE_DIR}/lib/Analysis/ReduceNoiseAnalysis/
5+
ADDITIONAL_HEADER_DIRS
6+
LINK_LIBS PUBLIC
7+
ortools::ortools
8+
)

0 commit comments

Comments
 (0)