Skip to content

Commit 09e7068

Browse files
committed
[cmake] Enable accepting external stablehlo project
This MR enables `torch_mlir` project to accept path to external stablehlo and include those directories. This in turn enables `torch_mlir` to be part of bigger compiler project when `stablehlo` is already a dependency.
1 parent e68560d commit 09e7068

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

CMakeLists.txt

+19-6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ option(TORCH_MLIR_ENABLE_STABLEHLO "Add stablehlo dialect" ON)
4343
if(TORCH_MLIR_ENABLE_STABLEHLO)
4444
add_definitions(-DTORCH_MLIR_ENABLE_STABLEHLO)
4545
endif()
46+
# It is possible that both stablehlo and torch_mlir projects are used in some compiler project.
47+
# In this case, we might not want to use stablehlo that is downloaded by torch_mlir (in external/stablehlo folder)
48+
# but instead stablehlo that is part of the top level compiler project.
49+
# TORCH_MLIR_EXTERNAL_STABLEHLO_DIR represents stablehlo directory (<some_path>/stablehlo)
50+
# in top level compiler project that will be included in torch_mlir. It is assumed that top level
51+
# compiler project makes stablehlo targets available (for example with `add_subdirectory`) and
52+
# thus they are not added.
53+
set(TORCH_MLIR_EXTERNAL_STABLEHLO_DIR "" CACHE STRING "Path to stablehlo dir from super project")
4654

4755
option(TORCH_MLIR_OUT_OF_TREE_BUILD "Specifies an out of tree build" OFF)
4856

@@ -233,12 +241,17 @@ endif()
233241
# project that we don't actually depend on. Further some of those parts
234242
# do not even compile on all platforms.
235243
if (TORCH_MLIR_ENABLE_STABLEHLO)
236-
set(STABLEHLO_BUILD_EMBEDDED ON)
237-
set(STABLEHLO_ENABLE_BINDINGS_PYTHON ON)
238-
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/externals/stablehlo
239-
${CMAKE_CURRENT_BINARY_DIR}/stablehlo
240-
EXCLUDE_FROM_ALL)
241-
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/externals/stablehlo)
244+
if (NOT "${TORCH_MLIR_EXTERNAL_STABLEHLO_DIR}" STREQUAL "")
245+
# Only include directories. It is assumed that stablehlo targets are made available by external project.
246+
include_directories(${TORCH_MLIR_EXTERNAL_STABLEHLO_DIR})
247+
else()
248+
set(STABLEHLO_BUILD_EMBEDDED ON)
249+
set(STABLEHLO_ENABLE_BINDINGS_PYTHON ON)
250+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/externals/stablehlo
251+
${CMAKE_CURRENT_BINARY_DIR}/stablehlo
252+
EXCLUDE_FROM_ALL)
253+
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/externals/stablehlo)
254+
endif()
242255
endif()
243256

244257
#-------------------------------------------------------------------------------

0 commit comments

Comments
 (0)