Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generating build files with cmake #21

Merged
merged 15 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build and Test
name: Build and Test w/Bazel
permissions: read-all
on:
push:
Expand Down
47 changes: 47 additions & 0 deletions .github/workflows/build_and_test_cmake.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Build and Test w/CMake
permissions: read-all
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: seanmiddleditch/gha-setup-ninja@master

- name: Install prerequisites
run: |
sudo apt update
sudo apt install -y uuid-dev

- name: Cache LLVM artifact
id: cache-llvm
uses: actions/cache@v3
with:
path: |
./externals/llvm-project
key: ${{ runner.os }}-norm-${{ hashFiles('externals/llvm-project/llvm/CMakeLists.txt') }}

- name: Build LLVM
if: steps.cache-llvm.outputs.cache-hit != 'true'
run: |
git submodule update --init --recursive
cd externals/llvm-project
mkdir build && cd build
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"
cmake --build . --target check-mlir

- name: Build and test mlir-tutorial
run: |
mkdir build && cd build
cmake -DLLVM_DIR=$PWD/../externals/llvm-project/build/lib/cmake/llvm -DMLIR_DIR=$PWD/../externals/llvm-project/build/lib/cmake/mlir ..
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: might be better to use $github_workspace/externals/... instead of the relative version

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed, will change that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in #24

cmake --build . --target MLIRAffineFullUnrollPasses
cmake --build . --target MLIRMulToAddPasses
cmake --build . --target mlir-headers
cmake --build . --target tutorial-opt
cmake --build . --target check-mlir-tutorial
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,17 @@ bazel-bin
bazel-mlir-tutorial
bazel-out
bazel-testlogs

# cmake related files
# ignore the user specified CMake presets in subproject directories.
/*/CMakeUserPresets.json

# Nested build directory
/build*

# Visual Studio built-in CMake configuration
.vscode*
/CMakeSettings.json
# Compilation databases
compile_commands.json
tablegen_compile_commands.yml
3 changes: 3 additions & 0 deletions .gitmodules
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be worthwhile to have the git submodule sync with the commit in bazel/import_llvm.bzl, but it doesn't appear to be possible to have git do that in this config file. It only supports a branch. Maybe after merging this I will add a note to the readme for how to do this, with something like

cd externals/llvm-project
git checkout -b upstream $(grep LLVM_COMMIT ../../bazel/import_llvm.bzl | grep -o '".*"' | sed 's/"//g')
cd ../..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be indeed nice to be able to keep in sync the llvm commit for both build systems, I guess a note as you propose would be enough as i dont think this will change often.

As a side note, I remember using a llvm checkout from september when i started to do the Cmake work and there were already some build errors due to some changes in MLIR.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "externals/llvm-project"]
path = externals/llvm-project
url = https://github.com/llvm/llvm-project.git
27 changes: 27 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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")

find_package(MLIR REQUIRED CONFIG)

message(STATUS "Using MLIRConfig.cmake in: ${MLIR_DIR}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")

set(MLIR_BINARY_DIR ${CMAKE_BINARY_DIR})

include(AddLLVM)
include(TableGen)

list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}")
include(AddMLIR)
include_directories(${LLVM_INCLUDE_DIRS})
include_directories(${MLIR_INCLUDE_DIRS})
include_directories(${PROJECT_SOURCE_DIR})
include_directories(${PROJECT_SOURCE_DIR}/externals/llvm-project)
include_directories(${PROJECT_BINARY_DIR})

add_subdirectory(tests)
add_subdirectory(tools)
add_subdirectory(lib)
1 change: 1 addition & 0 deletions externals/llvm-project
Submodule llvm-project added at cd5fce
2 changes: 2 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_subdirectory(Dialect)
add_subdirectory(Transform)
1 change: 1 addition & 0 deletions lib/Dialect/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(Poly)
27 changes: 27 additions & 0 deletions lib/Dialect/Poly/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Inlining `add_mlir_dialect(Poly poly)` commands so that
# we can custom name `*.inc` generated files.
set(LLVM_TARGET_DEFINITIONS PolyOps.td)
mlir_tablegen(PolyOps.h.inc -gen-op-decls)
mlir_tablegen(PolyOps.cpp.inc -gen-op-defs)
mlir_tablegen(PolyTypes.h.inc -gen-typedef-decls -typedefs-dialect=poly)
mlir_tablegen(PolyTypes.cpp.inc -gen-typedef-defs -typedefs-dialect=poly)
mlir_tablegen(PolyDialect.h.inc -gen-dialect-decls -dialect=poly)
mlir_tablegen(PolyDialect.cpp.inc -gen-dialect-defs -dialect=poly)
add_public_tablegen_target(MLIRPolyOpsIncGen)
add_dependencies(mlir-headers MLIRPolyOpsIncGen)

add_mlir_doc(PolyDialect PolyDialect Poly/ -gen-dialect-doc)

set(LLVM_TARGET_DEFINITIONS PolyPatterns.td)
mlir_tablegen(PolyCanonicalize.cpp.inc -gen-rewriters)
add_public_tablegen_target(MLIRPolyCanonicalizationIncGen)

add_mlir_dialect_library(MLIRPoly
PolyDialect.cpp
PolyOps.cpp

ADDITIONAL_HEADER_DIRS
${PROJECT_SOURCE_DIR}/lib/Dialect/Poly

LINK_LIBS PUBLIC
)
2 changes: 1 addition & 1 deletion lib/Dialect/Poly/PolyDialect.td
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef LIB_DIALECT_POLY_POLYDIALECT_TD_
#define LIB_DIALECT_POLY_POLYDIALECT_TD_

include "mlir/IR/DialectBase.td"
include "mlir/IR/OpBase.td"

def Poly_Dialect : Dialect {
let name = "poly";
Expand Down
13 changes: 13 additions & 0 deletions lib/Transform/Affine/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
add_mlir_library(AffineFullUnroll
AffineFullUnroll.cpp
AffineFullUnrollPatternRewrite.cpp

${PROJECT_SOURCE_DIR}/lib/Transform/Affine/
ADDITIONAL_HEADER_DIRS
LINK_LIBS PUBLIC
)

set(LLVM_TARGET_DEFINITIONS Passes.td)
mlir_tablegen(Passes.h.inc -gen-pass-decls -name Affine)
add_public_tablegen_target(MLIRAffineFullUnrollPasses)
add_mlir_doc(Passes AffinePasses ./ -gen-pass-doc)
12 changes: 12 additions & 0 deletions lib/Transform/Arith/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
add_mlir_library(MulToAdd
MulToAdd.cpp

${PROJECT_SOURCE_DIR}/lib/Transform/Arith/
ADDITIONAL_HEADER_DIRS
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)
2 changes: 2 additions & 0 deletions lib/Transform/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_subdirectory(Affine)
add_subdirectory(Arith)
18 changes: 18 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
configure_lit_site_cfg(
${CMAKE_CURRENT_SOURCE_DIR}/lit.cmake.site.cfg.py.in
${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg.py
MAIN_CONFIG
${CMAKE_CURRENT_SOURCE_DIR}/lit.cmake.cfg.py
)

set (MLIR_TUTORIAL_TEST_DEPENDS
FileCheck count not
mlir-opt
mlir-cpu-runner
# tutorial-opt
)

add_lit_testsuite(check-mlir-tutorial "Running the MLIR tutorial regression tests"
${CMAKE_CURRENT_BINARY_DIR}
DEPENDS ${MLIR_TUTORIAL_TEST_DEPENDS}
)
52 changes: 52 additions & 0 deletions tests/lit.cmake.cfg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# -*- Python -*-

import os

import lit.formats
import lit.util

from lit.llvm import llvm_config

# Configuration file for the 'lit' test runner.

# name: The name of this test suite.
config.name = "MLIR_TUTORIAL"

config.test_format = lit.formats.ShTest(not llvm_config.use_lit_shell)

# suffixes: A list of file extensions to treat as test files.
config.suffixes = [".mlir"]

# test_source_root: The root path where tests are located.
config.test_source_root = os.path.dirname(__file__)

# test_exec_root: The root path where tests should be run.
config.test_exec_root = os.path.join(config.project_binary_dir, "tests")

config.substitutions.append(("%PATH%", config.environment["PATH"]))
config.substitutions.append(("%shlibext", config.llvm_shlib_ext))

llvm_config.with_system_environment(["HOME", "INCLUDE", "LIB", "TMP", "TEMP"])

llvm_config.use_default_substitutions()

# excludes: A list of directories to exclude from the testsuite. The 'Inputs'
# subdirectories contain auxiliary inputs for various tests in their parent
# directories.
config.excludes = ["Inputs", "Examples", "CMakeLists.txt", "README.txt", "LICENSE.txt"]

# test_exec_root: The root path where tests should be run.
config.test_exec_root = os.path.join(config.project_binary_dir, "test")
config.project_tools_dir = os.path.join(config.project_binary_dir, "tools")

# Tweak the PATH to include the tools dir.
llvm_config.with_environment("PATH", config.llvm_tools_dir, append_path=True)

tool_dirs = [config.project_tools_dir, config.llvm_tools_dir]
tools = [
"mlir-opt",
"mlir-cpu-runner",
"tutorial-opt"
]

llvm_config.add_tool_substitutions(tools, tool_dirs)
13 changes: 13 additions & 0 deletions tests/lit.cmake.site.cfg.py.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@LIT_SITE_CFG_IN_HEADER@

config.llvm_tools_dir = lit_config.substitute("@LLVM_TOOLS_DIR@")
config.mlir_obj_dir = "@MLIR_BINARY_DIR@"
config.llvm_shlib_ext = "@SHLIBEXT@"
config.project_binary_dir = "@PROJECT_BINARY_DIR@"
config.project_source_dir = "@PROJECT_SOURCE_DIR@"

import lit.llvm
lit.llvm.initialize(lit_config, config)

# Let the main config do the real work.
lit_config.load_config(config, "@PROJECT_SOURCE_DIR@/tests/lit.cmake.cfg.py")
19 changes: 19 additions & 0 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
get_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS)

set (LIBS
${dialect_libs}
${conversion_libs}
MLIRPoly
AffineFullUnroll
MulToAdd
MLIROptLib
MLIRPass
)

add_llvm_executable(tutorial-opt tutorial-opt.cpp)

llvm_update_compile_flags(tutorial-opt)
target_link_libraries(tutorial-opt PRIVATE ${LIBS})

mlir_check_all_link_libraries(tutorial-opt)
Loading