[RFC] Factor out repetitive cmake patterns for llvm-style projects
New projects (particularly out of tree) have a tendency to hijack the existing
llvm configuration options and build targets (add_llvm_library,
add_llvm_tool). This can lead to some confusion.
1) When querying a configuration variable, do we care about how LLVM was
configured, or how these options were configured for the out of tree project?
2) LLVM has lots of defaults, which are easy to miss
(e.g. LLVM_BUILD_TOOLS=ON). These options all need to be duplicated in the
CMakeLists.txt for the project.
In addition, with LLVM Incubators coming online, we need better ways for these
incubators to do things the "LLVM way" without alot of futzing. Ideally, this
would happen in a way that eases importing into the LLVM monorepo when
projects mature.
This patch creates some generic infrastructure in llvm/cmake/modules and
refactors MLIR to use this infrastructure. This should expand to include
add_xxx_library, which is by far the most complicated bit of building a
project correctly, since it has to deal with lots of shared library
configuration bits. (MLIR currently hijacks the LLVM infrastructure for
building libMLIR.so, so this needs to get refactored anyway.)
Differential Revision: https://reviews.llvm.org/D85140
GitOrigin-RevId: e9b87f43bde8b5f0d8a79c5884fdce639b12e0ca
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 50511fd..ffba3be 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -21,6 +21,10 @@
add_dependencies(mlir-headers mlir-generic-headers)
add_custom_target(mlir-doc)
+# Get a bunch of LLVM-style default options.
+include(LLVMProjectOptions)
+add_llvm_project_options(mlir)
+
# Build the CUDA conversions and run according tests if the NVPTX backend
# is available
if ("NVPTX" IN_LIST LLVM_TARGETS_TO_BUILD)
@@ -44,13 +48,6 @@
set(MLIR_ROCM_RUNNER_ENABLED 0 CACHE BOOL "Enable building the mlir ROCm runner")
set(MLIR_VULKAN_RUNNER_ENABLED 0 CACHE BOOL "Enable building the mlir Vulkan runner")
-option(MLIR_INCLUDE_TESTS
- "Generate build targets for the MLIR unit tests."
- ${LLVM_INCLUDE_TESTS})
-
-option(MLIR_INCLUDE_INTEGRATION_TESTS
- "Generate build targets for the MLIR integration tests.")
-
#-------------------------------------------------------------------------------
# Python Bindings Configuration
# Requires:
@@ -83,42 +80,46 @@
"extension = '${PYTHON_MODULE_EXTENSION}")
endif()
+# Get a bunch of default targets
+include(LLVMProjectTargets)
+add_llvm_project_targets(mlir)
+
include_directories( "include")
include_directories( ${MLIR_INCLUDE_DIR})
# Adding tools/mlir-tblgen here as calling add_tablegen sets some variables like
# MLIR_TABLEGEN_EXE in PARENT_SCOPE which gets lost if that folder is included
# from another directory like tools
-add_subdirectory(tools/mlir-tblgen)
+if (MLIR_INCLUDE_TOOLS)
+ add_subdirectory(tools/mlir-tblgen)
+endif()
add_subdirectory(include/mlir)
add_subdirectory(lib)
# C API needs all dialects for registration, but should be built before tests.
add_subdirectory(lib/CAPI)
if (MLIR_INCLUDE_TESTS)
- add_definitions(-DMLIR_INCLUDE_TESTS)
add_subdirectory(unittests)
add_subdirectory(test)
endif()
if (MLIR_INCLUDE_INTEGRATION_TESTS)
- add_definitions(-DMLIR_INCLUDE_INTEGRATION_TESTS)
add_subdirectory(integration_test)
endif()
# Tools needs to come late to ensure that MLIR_ALL_LIBS is populated.
# Generally things after this point may depend on MLIR_ALL_LIBS or libMLIR.so.
-add_subdirectory(tools)
+if (MLIR_INCLUDE_TOOLS)
+ add_subdirectory(tools)
+endif()
-if( LLVM_INCLUDE_EXAMPLES )
+if (MLIR_INCLUDE_EXAMPLES)
add_subdirectory(examples)
endif()
-option(MLIR_INCLUDE_DOCS "Generate build targets for the MLIR docs."
- ${LLVM_INCLUDE_DOCS})
if (MLIR_INCLUDE_DOCS)
add_subdirectory(docs)
endif()
-if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
+if (NOT MLIR_INSTALL_TOOLCHAIN_ONLY)
install(DIRECTORY include/mlir include/mlir-c
DESTINATION include
COMPONENT mlir-headers
diff --git a/cmake/modules/AddMLIR.cmake b/cmake/modules/AddMLIR.cmake
index 8394c05..56742db 100644
--- a/cmake/modules/AddMLIR.cmake
+++ b/cmake/modules/AddMLIR.cmake
@@ -24,7 +24,12 @@
endfunction()
-# Generate Documentation
+# Generate Documentation using the mlir-doc rule
+# doc_filename: the basename of a .td tablegen file
+# command: the tablegen command to run, typically "-gen-op-doc",
+# "-gen-pass-doc", or "-gen-dialect-doc"
+# output_file: the basename of a .md markdown file to be output
+# output_directory: the directory to place the output
function(add_mlir_doc doc_filename command output_file output_directory)
set(LLVM_TARGET_DEFINITIONS ${doc_filename}.td)
tablegen(MLIR ${output_file}.md ${command} "-I${MLIR_MAIN_INCLUDE_DIR}" "-I${MLIR_INCLUDE_DIR}")
@@ -40,7 +45,7 @@
endfunction()
# Declare an mlir library which can be compiled in libMLIR.so
-# In addition to everything that llvm_add_librar accepts, this
+# In addition to everything that llvm_add_library accepts, this
# also has the following option:
# EXCLUDE_FROM_LIBMLIR
# Don't include this library in libMLIR.so. This option should be used
diff --git a/examples/standalone/CMakeLists.txt b/examples/standalone/CMakeLists.txt
index 45dc808..721efae 100644
--- a/examples/standalone/CMakeLists.txt
+++ b/examples/standalone/CMakeLists.txt
@@ -31,8 +31,17 @@
include(TableGen)
include(AddLLVM)
include(AddMLIR)
+
+# Get a bunch of LLVM-style default options.
+include(LLVMProjectOptions)
+add_llvm_project_options(standalone)
+
include(HandleLLVMOptions)
+# Get a bunch of default targets
+include(LLVMProjectTargets)
+add_llvm_project_targets(standalone)
+
include_directories(${LLVM_INCLUDE_DIRS})
include_directories(${MLIR_INCLUDE_DIRS})
include_directories(${PROJECT_SOURCE_DIR}/include)
diff --git a/examples/standalone/standalone-opt/CMakeLists.txt b/examples/standalone/standalone-opt/CMakeLists.txt
index 06bbb47..e4b12e0 100644
--- a/examples/standalone/standalone-opt/CMakeLists.txt
+++ b/examples/standalone/standalone-opt/CMakeLists.txt
@@ -6,7 +6,7 @@
MLIROptLib
MLIRStandalone
)
-add_llvm_executable(standalone-opt standalone-opt.cpp)
+add_standalone_tool(standalone-opt standalone-opt.cpp)
llvm_update_compile_flags(standalone-opt)
target_link_libraries(standalone-opt PRIVATE ${LIBS})
diff --git a/examples/standalone/standalone-translate/CMakeLists.txt b/examples/standalone/standalone-translate/CMakeLists.txt
index 137f794..15aa237 100644
--- a/examples/standalone/standalone-translate/CMakeLists.txt
+++ b/examples/standalone/standalone-translate/CMakeLists.txt
@@ -5,7 +5,7 @@
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
get_property(translation_libs GLOBAL PROPERTY MLIR_TRANSLATION_LIBS)
-add_llvm_executable(standalone-translate
+add_standalone_tool(standalone-translate
standalone-translate.cpp
)
llvm_update_compile_flags(standalone-translate)
diff --git a/examples/toy/CMakeLists.txt b/examples/toy/CMakeLists.txt
index 56002b1..39f6bd0 100644
--- a/examples/toy/CMakeLists.txt
+++ b/examples/toy/CMakeLists.txt
@@ -3,7 +3,7 @@
macro(add_toy_chapter name)
add_dependencies(Toy ${name})
- add_llvm_example(${name} ${ARGN})
+ add_mlir_example(${name} ${ARGN})
endmacro(add_toy_chapter name)
add_subdirectory(Ch1)
diff --git a/test/Examples/standalone/test.toy b/test/Examples/standalone/test.toy
index 7b4a9c2..cd183c9 100644
--- a/test/Examples/standalone/test.toy
+++ b/test/Examples/standalone/test.toy
@@ -1,4 +1,5 @@
# RUN: %cmake %mlir_src_root/examples/standalone -DCMAKE_CXX_COMPILER=%host_cxx -DCMAKE_C_COMPILER=%host_cc -DMLIR_DIR=%llvm_lib_dir/cmake/mlir ; %cmake --build . --target check-standalone | tee %t | FileCheck %s
+# RUN: %cmake --build . --target mlir-doc
# CHECK: Passed: 3
# UNSUPPORTED: windows, android
diff --git a/tools/mlir-cpu-runner/CMakeLists.txt b/tools/mlir-cpu-runner/CMakeLists.txt
index 596012c..7cd8112 100644
--- a/tools/mlir-cpu-runner/CMakeLists.txt
+++ b/tools/mlir-cpu-runner/CMakeLists.txt
@@ -4,7 +4,7 @@
nativecodegen
)
-add_llvm_tool(mlir-cpu-runner
+add_mlir_tool(mlir-cpu-runner
mlir-cpu-runner.cpp
)
llvm_update_compile_flags(mlir-cpu-runner)
diff --git a/tools/mlir-cuda-runner/CMakeLists.txt b/tools/mlir-cuda-runner/CMakeLists.txt
index 5488262..16daca8 100644
--- a/tools/mlir-cuda-runner/CMakeLists.txt
+++ b/tools/mlir-cuda-runner/CMakeLists.txt
@@ -68,7 +68,7 @@
LIST(APPEND targets_to_link "LLVM${t}")
ENDFOREACH(t)
- add_llvm_tool(mlir-cuda-runner
+ add_mlir_tool(mlir-cuda-runner
mlir-cuda-runner.cpp
DEPENDS
diff --git a/tools/mlir-linalg-ods-gen/CMakeLists.txt b/tools/mlir-linalg-ods-gen/CMakeLists.txt
index bc9a0c1..c27857b 100644
--- a/tools/mlir-linalg-ods-gen/CMakeLists.txt
+++ b/tools/mlir-linalg-ods-gen/CMakeLists.txt
@@ -2,7 +2,7 @@
Core
Support
)
-add_llvm_tool(mlir-linalg-ods-gen
+add_mlir_tool(mlir-linalg-ods-gen
mlir-linalg-ods-gen.cpp
)
llvm_update_compile_flags(mlir-linalg-ods-gen)
diff --git a/tools/mlir-opt/CMakeLists.txt b/tools/mlir-opt/CMakeLists.txt
index 483dcfe..65a328f 100644
--- a/tools/mlir-opt/CMakeLists.txt
+++ b/tools/mlir-opt/CMakeLists.txt
@@ -50,7 +50,7 @@
${LIBS}
)
-add_llvm_tool(mlir-opt
+add_mlir_tool(mlir-opt
mlir-opt.cpp
DEPENDS
diff --git a/tools/mlir-reduce/CMakeLists.txt b/tools/mlir-reduce/CMakeLists.txt
index 958c2c9..8e4a42f 100644
--- a/tools/mlir-reduce/CMakeLists.txt
+++ b/tools/mlir-reduce/CMakeLists.txt
@@ -43,7 +43,7 @@
MLIRTransformUtils
)
-add_llvm_tool(mlir-reduce
+add_mlir_tool(mlir-reduce
OptReductionPass.cpp
Passes/OpReducer.cpp
ReductionNode.cpp
diff --git a/tools/mlir-rocm-runner/CMakeLists.txt b/tools/mlir-rocm-runner/CMakeLists.txt
index 2c0791d..3c90bea 100644
--- a/tools/mlir-rocm-runner/CMakeLists.txt
+++ b/tools/mlir-rocm-runner/CMakeLists.txt
@@ -104,7 +104,7 @@
LIST(APPEND targets_to_link "LLVM${t}")
ENDFOREACH(t)
- add_llvm_tool(mlir-rocm-runner
+ add_mlir_tool(mlir-rocm-runner
mlir-rocm-runner.cpp
DEPENDS
diff --git a/tools/mlir-translate/CMakeLists.txt b/tools/mlir-translate/CMakeLists.txt
index 99b98f9..cc7ff64 100644
--- a/tools/mlir-translate/CMakeLists.txt
+++ b/tools/mlir-translate/CMakeLists.txt
@@ -5,7 +5,7 @@
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
get_property(translation_libs GLOBAL PROPERTY MLIR_TRANSLATION_LIBS)
-add_llvm_tool(mlir-translate
+add_mlir_tool(mlir-translate
mlir-translate.cpp
)
llvm_update_compile_flags(mlir-translate)
diff --git a/tools/mlir-vulkan-runner/CMakeLists.txt b/tools/mlir-vulkan-runner/CMakeLists.txt
index c7a0325..c11b4ef 100644
--- a/tools/mlir-vulkan-runner/CMakeLists.txt
+++ b/tools/mlir-vulkan-runner/CMakeLists.txt
@@ -85,7 +85,7 @@
LIST(APPEND targets_to_link "LLVM${t}")
ENDFOREACH(t)
- add_llvm_tool(mlir-vulkan-runner
+ add_mlir_tool(mlir-vulkan-runner
mlir-vulkan-runner.cpp
)
add_dependencies(mlir-vulkan-runner vulkan-runtime-wrappers)