Skip to content

Commit 8215898

Browse files
author
Farid Zakaria
committed
Introduce a single CMakeLists.txt
Build MLIR & StableHLO via a single cmake configure & build command. This will make the build_mlir.sh script unecessary. This should simplify setting cmake values to be consistent across MLIR and StableHLO builds such as PYTHON_BINDINGS.
1 parent dc4bc72 commit 8215898

File tree

2 files changed

+79
-4
lines changed

2 files changed

+79
-4
lines changed

CMakeLists.txt

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,67 @@ if(STABLEHLO_EXTERNAL_PROJECT_BUILD)
8989
list(APPEND CMAKE_MODULE_PATH "${MLIR_MAIN_SRC_DIR}/cmake/modules")
9090
elseif(NOT STABLEHLO_BUILD_EMBEDDED)
9191
message(STATUS "Building StableHLO with an installed MLIR")
92-
find_package(MLIR REQUIRED CONFIG)
93-
message(STATUS "Using MLIRConfig.cmake in: ${MLIR_DIR}")
94-
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
92+
93+
# These defaults are moderately important to us, but the user *can*
94+
# override them (enabling some of these brings in deps that will conflict,
95+
# so ymmv).
96+
# https://github.com/openxla/iree/blob/f3b6bcd79b24ef4a9b355eb3f3496ffafcbd0881/build_tools/cmake/iree_llvm.cmake#L127
97+
set(LLVM_INCLUDE_EXAMPLES OFF CACHE BOOL "")
98+
set(LLVM_INCLUDE_TESTS OFF CACHE BOOL "")
99+
set(LLVM_INCLUDE_BENCHMARKS OFF CACHE BOOL "")
100+
set(LLVM_APPEND_VC_REV OFF CACHE BOOL "")
101+
set(LLVM_ENABLE_IDE ON CACHE BOOL "")
102+
set(LLVM_ENABLE_BINDINGS OFF CACHE BOOL "")
103+
# LLVM defaults to building all targets. We always enable targets that we need
104+
# as we need them, so default to none. The user can override this as needed,
105+
# which is fine.
106+
set(LLVM_TARGETS_TO_BUILD "" CACHE STRING "")
107+
108+
# We enable LLVM projects as needed. The user can override this.
109+
set(LLVM_ENABLE_PROJECTS "" CACHE STRING "")
110+
set(LLVM_EXTERNAL_PROJECTS "" CACHE STRING "")
111+
112+
# Unconditionally enable mlir.
113+
list(APPEND LLVM_ENABLE_PROJECTS mlir)
114+
95115
set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/bin)
96116
set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/lib)
97-
list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}")
117+
118+
list(APPEND CMAKE_MESSAGE_INDENT " ")
119+
set(_BUNDLED_LLVM_CMAKE_SOURCE_SUBDIR "llvm-project/llvm")
120+
add_subdirectory("${_BUNDLED_LLVM_CMAKE_SOURCE_SUBDIR}" "llvm-project" EXCLUDE_FROM_ALL)
121+
get_directory_property(LLVM_VERSION_MAJOR DIRECTORY "${_BUNDLED_LLVM_CMAKE_SOURCE_SUBDIR}" LLVM_VERSION_MAJOR)
122+
if (NOT LLVM_VERSION_MAJOR)
123+
message(SEND_ERROR "Failed to read LLVM_VERSION_MAJOR property on LLVM directory. Should have been set since https://github.com/llvm/llvm-project/pull/83346.")
124+
endif()
125+
list(POP_BACK CMAKE_MESSAGE_INDENT)
126+
127+
# Set some CMake variables that mirror things exported in the find_package
128+
# world. Source of truth for these is in an installed LLVMConfig.cmake,
129+
# MLIRConfig.cmake, LLDConfig.cmake (etc) and in the various standalone
130+
# build segments of each project's top-level CMakeLists.
131+
set(LLVM_CMAKE_DIR "${CMAKE_BINARY_DIR}/llvm-project/lib/cmake/llvm")
98132
list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")
133+
# TODO: Fix MLIR upstream so it doesn't spew into the containing project
134+
# binary dir. See mlir/cmake/modules/CMakeLists.txt
135+
# (and other LLVM sub-projects).
136+
set(MLIR_CMAKE_DIR "${CMAKE_BINARY_DIR}/lib/cmake/mlir")
137+
if(NOT EXISTS "${MLIR_CMAKE_DIR}/AddMLIR.cmake")
138+
message(SEND_ERROR "Could not find AddMLIR.cmake in ${MLIR_CMAKE_DIR}: LLVM sub-projects may have changed their layout. See the mlir_cmake_builddir variable in mlir/cmake/modules/CMakeLists.txt")
139+
endif()
140+
list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}")
141+
142+
message(STATUS "Using MLIRConfig.cmake in: ${MLIR_DIR}")
143+
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
144+
145+
set(LLVM_INCLUDE_DIRS
146+
${CMAKE_SOURCE_DIR}/${_BUNDLED_LLVM_CMAKE_SOURCE_SUBDIR}/include
147+
${CMAKE_BINARY_DIR}/llvm-project/include
148+
)
149+
set(MLIR_INCLUDE_DIRS
150+
${CMAKE_SOURCE_DIR}/llvm-project/mlir/include
151+
${CMAKE_BINARY_DIR}/llvm-project/tools/mlir/include
152+
)
99153
else()
100154
message(STATUS "Building StableHLO embedded in another project")
101155
endif()

CMakePresets.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,27 @@
1919
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
2020
"MLIR_DIR": "${sourceDir}/llvm-build/lib/cmake/mlir"
2121
}
22+
},
23+
{
24+
"name": "debug-python",
25+
"displayName": "Debug w/ python bindings",
26+
"inherits": "debug",
27+
"cacheVariables": {
28+
"STABLEHLO_ENABLE_BINDINGS_PYTHON" : "ON",
29+
"STABLEHLO_ENABLE_SANITIZER": "OFF"
30+
}
31+
}
32+
],
33+
"buildPresets": [
34+
{
35+
"name": "debug",
36+
"displayName": "Build Debug",
37+
"configurePreset": "debug"
38+
},
39+
{
40+
"name": "debug-python",
41+
"displayName": "Build Debug w/ python bindings",
42+
"configurePreset": "debug-python"
2243
}
2344
]
2445
}

0 commit comments

Comments
 (0)