Skip to content

Commit d04fa9c

Browse files
authored
Generating build files with cmake (#21)
* ignoring cmake related build files * Adding LLVM dependency as external dependecy. Using git submodules. * Initial workspace setup with CMake * Including tests into the CMake build. Adding the LLVM module so that all `lit` related functions are available. A new target is made available for running tests: `check-mlir-tutorial`. * Including tools directory into the CMake build So that `tutorial-opt` can be built. This introduces as well a new target `tutorial-opt`. * Adding affine full unroll related passes to cmake build This introduces a new mlir lib: `AffineFullUnroll` and a new build target to build it: `MLIRAffineFullUnrollPasses`. * Adding tablgen cmake module to have access to all tablgen related cmake statements. * add mlir include dirs * Adding mul to add related passes to cmake build This introduces a new mlir lib: `MulToAdd` and a new build target to build it: `MLIRMulToAddPasses`. * Renaming `PolyOps.td` to `Poly.td` So that on the cmake side of things we can use the `add_mlir_dialect` macro without too many changes to the include directives. Ref: https://github.com/llvm/llvm-project/blob/main/mlir/cmake/modules/AddMLIR.cmake#L166-L177 * ignoring vs code metafiles plus compilation database files. * Adding poly dialect lib to cmake build * Adding CI for cmake build. * Adding canonicalizers to cmake build * Inlining cmake commands for Poly dialect generation. Instead of using `add_mlir_dialect` helper function for dialect generation, we inline it here so that we can have control over the naming of generated `*.inc` files. This avoids renaming of `PolyOps.td` so that cmake builds work well on the existing source code.
1 parent ff952d1 commit d04fa9c

17 files changed

+253
-2
lines changed

.github/workflows/build_and_test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Build and Test
1+
name: Build and Test w/Bazel
22
permissions: read-all
33
on:
44
push:
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Build and Test w/CMake
2+
permissions: read-all
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v3
15+
- uses: seanmiddleditch/gha-setup-ninja@master
16+
17+
- name: Install prerequisites
18+
run: |
19+
sudo apt update
20+
sudo apt install -y uuid-dev
21+
22+
- name: Cache LLVM artifact
23+
id: cache-llvm
24+
uses: actions/cache@v3
25+
with:
26+
path: |
27+
./externals/llvm-project
28+
key: ${{ runner.os }}-norm-${{ hashFiles('externals/llvm-project/llvm/CMakeLists.txt') }}
29+
30+
- name: Build LLVM
31+
if: steps.cache-llvm.outputs.cache-hit != 'true'
32+
run: |
33+
git submodule update --init --recursive
34+
cd externals/llvm-project
35+
mkdir build && cd build
36+
cmake -G Ninja ../llvm -DLLVM_ENABLE_PROJECTS=mlir -DLLVM_BUILD_EXAMPLES=ON -DLLVM_ENABLE_ASSERTIONS=ON -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_RTTI=ON -DLLVM_TARGETS_TO_BUILD="host"
37+
cmake --build . --target check-mlir
38+
39+
- name: Build and test mlir-tutorial
40+
run: |
41+
mkdir build && cd build
42+
cmake -DLLVM_DIR=$PWD/../externals/llvm-project/build/lib/cmake/llvm -DMLIR_DIR=$PWD/../externals/llvm-project/build/lib/cmake/mlir ..
43+
cmake --build . --target MLIRAffineFullUnrollPasses
44+
cmake --build . --target MLIRMulToAddPasses
45+
cmake --build . --target mlir-headers
46+
cmake --build . --target tutorial-opt
47+
cmake --build . --target check-mlir-tutorial

.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,17 @@ bazel-bin
33
bazel-mlir-tutorial
44
bazel-out
55
bazel-testlogs
6+
7+
# cmake related files
8+
# ignore the user specified CMake presets in subproject directories.
9+
/*/CMakeUserPresets.json
10+
11+
# Nested build directory
12+
/build*
13+
14+
# Visual Studio built-in CMake configuration
15+
.vscode*
16+
/CMakeSettings.json
17+
# Compilation databases
18+
compile_commands.json
19+
tablegen_compile_commands.yml

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "externals/llvm-project"]
2+
path = externals/llvm-project
3+
url = https://github.com/llvm/llvm-project.git

CMakeLists.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
cmake_minimum_required(VERSION 3.20.0)
2+
3+
project(mlir-tutorial LANGUAGES CXX C)
4+
5+
set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")
6+
7+
find_package(MLIR REQUIRED CONFIG)
8+
9+
message(STATUS "Using MLIRConfig.cmake in: ${MLIR_DIR}")
10+
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
11+
12+
set(MLIR_BINARY_DIR ${CMAKE_BINARY_DIR})
13+
14+
include(AddLLVM)
15+
include(TableGen)
16+
17+
list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}")
18+
include(AddMLIR)
19+
include_directories(${LLVM_INCLUDE_DIRS})
20+
include_directories(${MLIR_INCLUDE_DIRS})
21+
include_directories(${PROJECT_SOURCE_DIR})
22+
include_directories(${PROJECT_SOURCE_DIR}/externals/llvm-project)
23+
include_directories(${PROJECT_BINARY_DIR})
24+
25+
add_subdirectory(tests)
26+
add_subdirectory(tools)
27+
add_subdirectory(lib)

externals/llvm-project

Submodule llvm-project added at cd5fcea

lib/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
add_subdirectory(Dialect)
2+
add_subdirectory(Transform)

lib/Dialect/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(Poly)

lib/Dialect/Poly/CMakeLists.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Inlining `add_mlir_dialect(Poly poly)` commands so that
2+
# we can custom name `*.inc` generated files.
3+
set(LLVM_TARGET_DEFINITIONS PolyOps.td)
4+
mlir_tablegen(PolyOps.h.inc -gen-op-decls)
5+
mlir_tablegen(PolyOps.cpp.inc -gen-op-defs)
6+
mlir_tablegen(PolyTypes.h.inc -gen-typedef-decls -typedefs-dialect=poly)
7+
mlir_tablegen(PolyTypes.cpp.inc -gen-typedef-defs -typedefs-dialect=poly)
8+
mlir_tablegen(PolyDialect.h.inc -gen-dialect-decls -dialect=poly)
9+
mlir_tablegen(PolyDialect.cpp.inc -gen-dialect-defs -dialect=poly)
10+
add_public_tablegen_target(MLIRPolyOpsIncGen)
11+
add_dependencies(mlir-headers MLIRPolyOpsIncGen)
12+
13+
add_mlir_doc(PolyDialect PolyDialect Poly/ -gen-dialect-doc)
14+
15+
set(LLVM_TARGET_DEFINITIONS PolyPatterns.td)
16+
mlir_tablegen(PolyCanonicalize.cpp.inc -gen-rewriters)
17+
add_public_tablegen_target(MLIRPolyCanonicalizationIncGen)
18+
19+
add_mlir_dialect_library(MLIRPoly
20+
PolyDialect.cpp
21+
PolyOps.cpp
22+
23+
ADDITIONAL_HEADER_DIRS
24+
${PROJECT_SOURCE_DIR}/lib/Dialect/Poly
25+
26+
LINK_LIBS PUBLIC
27+
)

lib/Dialect/Poly/PolyDialect.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef LIB_DIALECT_POLY_POLYDIALECT_TD_
22
#define LIB_DIALECT_POLY_POLYDIALECT_TD_
33

4-
include "mlir/IR/DialectBase.td"
4+
include "mlir/IR/OpBase.td"
55

66
def Poly_Dialect : Dialect {
77
let name = "poly";

lib/Transform/Affine/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
add_mlir_library(AffineFullUnroll
2+
AffineFullUnroll.cpp
3+
AffineFullUnrollPatternRewrite.cpp
4+
5+
${PROJECT_SOURCE_DIR}/lib/Transform/Affine/
6+
ADDITIONAL_HEADER_DIRS
7+
LINK_LIBS PUBLIC
8+
)
9+
10+
set(LLVM_TARGET_DEFINITIONS Passes.td)
11+
mlir_tablegen(Passes.h.inc -gen-pass-decls -name Affine)
12+
add_public_tablegen_target(MLIRAffineFullUnrollPasses)
13+
add_mlir_doc(Passes AffinePasses ./ -gen-pass-doc)

lib/Transform/Arith/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
add_mlir_library(MulToAdd
2+
MulToAdd.cpp
3+
4+
${PROJECT_SOURCE_DIR}/lib/Transform/Arith/
5+
ADDITIONAL_HEADER_DIRS
6+
LINK_LIBS PUBLIC
7+
)
8+
9+
set(LLVM_TARGET_DEFINITIONS Passes.td)
10+
mlir_tablegen(Passes.h.inc -gen-pass-decls -name Arith)
11+
add_public_tablegen_target(MLIRMulToAddPasses)
12+
add_mlir_doc(Passes ArithPasses ./ -gen-pass-doc)

lib/Transform/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
add_subdirectory(Affine)
2+
add_subdirectory(Arith)

tests/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
configure_lit_site_cfg(
2+
${CMAKE_CURRENT_SOURCE_DIR}/lit.cmake.site.cfg.py.in
3+
${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg.py
4+
MAIN_CONFIG
5+
${CMAKE_CURRENT_SOURCE_DIR}/lit.cmake.cfg.py
6+
)
7+
8+
set (MLIR_TUTORIAL_TEST_DEPENDS
9+
FileCheck count not
10+
mlir-opt
11+
mlir-cpu-runner
12+
# tutorial-opt
13+
)
14+
15+
add_lit_testsuite(check-mlir-tutorial "Running the MLIR tutorial regression tests"
16+
${CMAKE_CURRENT_BINARY_DIR}
17+
DEPENDS ${MLIR_TUTORIAL_TEST_DEPENDS}
18+
)

tests/lit.cmake.cfg.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# -*- Python -*-
2+
3+
import os
4+
5+
import lit.formats
6+
import lit.util
7+
8+
from lit.llvm import llvm_config
9+
10+
# Configuration file for the 'lit' test runner.
11+
12+
# name: The name of this test suite.
13+
config.name = "MLIR_TUTORIAL"
14+
15+
config.test_format = lit.formats.ShTest(not llvm_config.use_lit_shell)
16+
17+
# suffixes: A list of file extensions to treat as test files.
18+
config.suffixes = [".mlir"]
19+
20+
# test_source_root: The root path where tests are located.
21+
config.test_source_root = os.path.dirname(__file__)
22+
23+
# test_exec_root: The root path where tests should be run.
24+
config.test_exec_root = os.path.join(config.project_binary_dir, "tests")
25+
26+
config.substitutions.append(("%PATH%", config.environment["PATH"]))
27+
config.substitutions.append(("%shlibext", config.llvm_shlib_ext))
28+
29+
llvm_config.with_system_environment(["HOME", "INCLUDE", "LIB", "TMP", "TEMP"])
30+
31+
llvm_config.use_default_substitutions()
32+
33+
# excludes: A list of directories to exclude from the testsuite. The 'Inputs'
34+
# subdirectories contain auxiliary inputs for various tests in their parent
35+
# directories.
36+
config.excludes = ["Inputs", "Examples", "CMakeLists.txt", "README.txt", "LICENSE.txt"]
37+
38+
# test_exec_root: The root path where tests should be run.
39+
config.test_exec_root = os.path.join(config.project_binary_dir, "test")
40+
config.project_tools_dir = os.path.join(config.project_binary_dir, "tools")
41+
42+
# Tweak the PATH to include the tools dir.
43+
llvm_config.with_environment("PATH", config.llvm_tools_dir, append_path=True)
44+
45+
tool_dirs = [config.project_tools_dir, config.llvm_tools_dir]
46+
tools = [
47+
"mlir-opt",
48+
"mlir-cpu-runner",
49+
"tutorial-opt"
50+
]
51+
52+
llvm_config.add_tool_substitutions(tools, tool_dirs)

tests/lit.cmake.site.cfg.py.in

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@LIT_SITE_CFG_IN_HEADER@
2+
3+
config.llvm_tools_dir = lit_config.substitute("@LLVM_TOOLS_DIR@")
4+
config.mlir_obj_dir = "@MLIR_BINARY_DIR@"
5+
config.llvm_shlib_ext = "@SHLIBEXT@"
6+
config.project_binary_dir = "@PROJECT_BINARY_DIR@"
7+
config.project_source_dir = "@PROJECT_SOURCE_DIR@"
8+
9+
import lit.llvm
10+
lit.llvm.initialize(lit_config, config)
11+
12+
# Let the main config do the real work.
13+
lit_config.load_config(config, "@PROJECT_SOURCE_DIR@/tests/lit.cmake.cfg.py")

tools/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
2+
get_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS)
3+
4+
set (LIBS
5+
${dialect_libs}
6+
${conversion_libs}
7+
MLIRPoly
8+
AffineFullUnroll
9+
MulToAdd
10+
MLIROptLib
11+
MLIRPass
12+
)
13+
14+
add_llvm_executable(tutorial-opt tutorial-opt.cpp)
15+
16+
llvm_update_compile_flags(tutorial-opt)
17+
target_link_libraries(tutorial-opt PRIVATE ${LIBS})
18+
19+
mlir_check_all_link_libraries(tutorial-opt)

0 commit comments

Comments
 (0)