From 1e32df3db78a860d2c3416f7f63185a9f48cb137 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Tue, 14 Nov 2023 20:24:33 -0800 Subject: [PATCH] update CMake build --- .github/workflows/build_and_test_cmake.yml | 11 ++++- .gitignore | 3 -- CMakeLists.txt | 13 ++++- README.md | 47 ++++++++++--------- externals/llvm-project | 2 +- lib/Analysis/CMakeLists.txt | 1 + .../ReduceNoiseAnalysis/CMakeLists.txt | 8 ++++ lib/CMakeLists.txt | 3 +- lib/Dialect/CMakeLists.txt | 3 +- lib/Dialect/Noisy/CMakeLists.txt | 6 --- lib/Transform/Affine/CMakeLists.txt | 4 ++ lib/Transform/Arith/CMakeLists.txt | 6 ++- lib/Transform/CMakeLists.txt | 3 +- lib/Transform/Noisy/CMakeLists.txt | 18 +++++++ tools/CMakeLists.txt | 7 ++- 15 files changed, 94 insertions(+), 41 deletions(-) create mode 100644 lib/Analysis/CMakeLists.txt create mode 100644 lib/Analysis/ReduceNoiseAnalysis/CMakeLists.txt create mode 100644 lib/Transform/Noisy/CMakeLists.txt diff --git a/.github/workflows/build_and_test_cmake.yml b/.github/workflows/build_and_test_cmake.yml index a257c48..78d51db 100644 --- a/.github/workflows/build_and_test_cmake.yml +++ b/.github/workflows/build_and_test_cmake.yml @@ -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} @@ -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 diff --git a/.gitignore b/.gitignore index 36802d6..258a6a8 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/CMakeLists.txt b/CMakeLists.txt index 56aab9f..7d3ca4c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) @@ -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) \ No newline at end of file +add_subdirectory(lib) diff --git a/README.md b/README.md index a894731..99a0e5d 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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 diff --git a/externals/llvm-project b/externals/llvm-project index 896749a..ebb8ffd 160000 --- a/externals/llvm-project +++ b/externals/llvm-project @@ -1 +1 @@ -Subproject commit 896749aa0d420ae573255a64a349bc2a76cfed37 +Subproject commit ebb8ffde94476333cb2e95aacfd2e023d7112efb diff --git a/lib/Analysis/CMakeLists.txt b/lib/Analysis/CMakeLists.txt new file mode 100644 index 0000000..a52d48b --- /dev/null +++ b/lib/Analysis/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(ReduceNoiseAnalysis) diff --git a/lib/Analysis/ReduceNoiseAnalysis/CMakeLists.txt b/lib/Analysis/ReduceNoiseAnalysis/CMakeLists.txt new file mode 100644 index 0000000..1889df5 --- /dev/null +++ b/lib/Analysis/ReduceNoiseAnalysis/CMakeLists.txt @@ -0,0 +1,8 @@ +add_mlir_library(ReduceNoiseAnalysis + ReduceNoiseAnalysis.cpp + + ${PROJECT_SOURCE_DIR}/lib/Analysis/ReduceNoiseAnalysis/ + ADDITIONAL_HEADER_DIRS + LINK_LIBS PUBLIC + ortools::ortools +) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 714031d..c1572fe 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -1,3 +1,4 @@ -add_subdirectory(Dialect) +add_subdirectory(Analysis) add_subdirectory(Conversion) +add_subdirectory(Dialect) add_subdirectory(Transform) diff --git a/lib/Dialect/CMakeLists.txt b/lib/Dialect/CMakeLists.txt index aff4d19..1f27a0f 100644 --- a/lib/Dialect/CMakeLists.txt +++ b/lib/Dialect/CMakeLists.txt @@ -1 +1,2 @@ -add_subdirectory(Poly) \ No newline at end of file +add_subdirectory(Noisy) +add_subdirectory(Poly) diff --git a/lib/Dialect/Noisy/CMakeLists.txt b/lib/Dialect/Noisy/CMakeLists.txt index 5e38757..adb5864 100644 --- a/lib/Dialect/Noisy/CMakeLists.txt +++ b/lib/Dialect/Noisy/CMakeLists.txt @@ -12,16 +12,10 @@ add_dependencies(mlir-headers MLIRNoisyOpsIncGen) add_mlir_doc(NoisyDialect NoisyDialect Noisy/ -gen-dialect-doc) -set(LLVM_TARGET_DEFINITIONS NoisyPatterns.td) -mlir_tablegen(NoisyCanonicalize.cpp.inc -gen-rewriters) -add_public_tablegen_target(MLIRNoisyCanonicalizationIncGen) - add_mlir_dialect_library(MLIRNoisy NoisyDialect.cpp NoisyOps.cpp ADDITIONAL_HEADER_DIRS ${PROJECT_SOURCE_DIR}/lib/Dialect/Noisy - - LINK_LIBS PUBLIC ) diff --git a/lib/Transform/Affine/CMakeLists.txt b/lib/Transform/Affine/CMakeLists.txt index eaa2b68..31a6c14 100644 --- a/lib/Transform/Affine/CMakeLists.txt +++ b/lib/Transform/Affine/CMakeLists.txt @@ -4,6 +4,10 @@ add_mlir_library(AffineFullUnroll ${PROJECT_SOURCE_DIR}/lib/Transform/Affine/ ADDITIONAL_HEADER_DIRS + + DEPENDS + MLIRAffineFullUnrollPasses + LINK_LIBS PUBLIC ) diff --git a/lib/Transform/Arith/CMakeLists.txt b/lib/Transform/Arith/CMakeLists.txt index f73be7c..5ae59e2 100644 --- a/lib/Transform/Arith/CMakeLists.txt +++ b/lib/Transform/Arith/CMakeLists.txt @@ -3,10 +3,14 @@ add_mlir_library(MulToAdd ${PROJECT_SOURCE_DIR}/lib/Transform/Arith/ ADDITIONAL_HEADER_DIRS + + DEPENDS + MLIRMulToAddPasses + LINK_LIBS PUBLIC ) set(LLVM_TARGET_DEFINITIONS Passes.td) mlir_tablegen(Passes.h.inc -gen-pass-decls -name Arith) add_public_tablegen_target(MLIRMulToAddPasses) -add_mlir_doc(Passes ArithPasses ./ -gen-pass-doc) \ No newline at end of file +add_mlir_doc(Passes ArithPasses ./ -gen-pass-doc) diff --git a/lib/Transform/CMakeLists.txt b/lib/Transform/CMakeLists.txt index 9caa085..5534bc3 100644 --- a/lib/Transform/CMakeLists.txt +++ b/lib/Transform/CMakeLists.txt @@ -1,2 +1,3 @@ add_subdirectory(Affine) -add_subdirectory(Arith) \ No newline at end of file +add_subdirectory(Arith) +add_subdirectory(Noisy) diff --git a/lib/Transform/Noisy/CMakeLists.txt b/lib/Transform/Noisy/CMakeLists.txt new file mode 100644 index 0000000..94568d8 --- /dev/null +++ b/lib/Transform/Noisy/CMakeLists.txt @@ -0,0 +1,18 @@ +add_mlir_library(NoisyPasses + ReduceNoiseOptimizer.cpp + + ${PROJECT_SOURCE_DIR}/lib/Transform/Noisy/ + ADDITIONAL_HEADER_DIRS + + DEPENDS + MLIRNoisy + MLIRNoisyPasses + + LINK_LIBS PUBLIC + ReduceNoiseAnalysis +) + +set(LLVM_TARGET_DEFINITIONS Passes.td) +mlir_tablegen(Passes.h.inc -gen-pass-decls -name Noisy) +add_public_tablegen_target(MLIRNoisyPasses) +add_mlir_doc(Passes NoisyPasses ./ -gen-pass-doc) diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 59f33b1..8cd5559 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -4,12 +4,15 @@ get_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS) set (LIBS ${dialect_libs} ${conversion_libs} - MLIRPoly AffineFullUnroll - MulToAdd + MLIRNoisy MLIROptLib MLIRPass + MLIRPoly + MulToAdd + NoisyPasses PolyToStandard + ortools::ortools ) add_llvm_executable(tutorial-opt tutorial-opt.cpp)