Skip to content

Commit 7c64f6b

Browse files
vchuravystephenneuendorffer
authored andcommitted
[MLIR] Add support for libMLIR.so
Putting this up mainly for discussion on how this should be done. I am interested in MLIR from the Julia side and we currently have a strong preference to dynamically linking against the LLVM shared library, and would like to have a MLIR shared library. This patch adds a new cmake function add_mlir_library() which accumulates a list of targets to be compiled into libMLIR.so. Note that not all libraries make sense to be compiled into libMLIR.so. In particular, we want to avoid libraries which primarily exist to support certain tools (such as mlir-opt and mlir-cpu-runner). Note that the resulting libMLIR.so depends on LLVM, but does not contain any LLVM components. As a result, it is necessary to link with libLLVM.so to avoid linkage errors. So, libMLIR.so requires LLVM_BUILD_LLVM_DYLIB=on FYI, Currently it appears that LLVM_LINK_LLVM_DYLIB is broken because mlir-tblgen is linked against libLLVM.so and and independent LLVM components. Previous version of this patch broke depencies on TableGen targets. This appears to be because it compiled all libraries to OBJECT libraries (probably because cmake is generating different target names). Avoiding object libraries results in correct dependencies. (updated by Stephen Neuendorffer) Differential Revision: https://reviews.llvm.org/D73130
1 parent 4594d0e commit 7c64f6b

File tree

19 files changed

+90
-29
lines changed

19 files changed

+90
-29
lines changed

mlir/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ include_directories( ${MLIR_INCLUDE_DIR})
3434

3535
add_subdirectory(include/mlir)
3636
add_subdirectory(lib)
37-
add_subdirectory(tools)
3837
add_subdirectory(unittests)
3938
add_subdirectory(test)
39+
# Tools needs to come late to ensure that MLIR_ALL_LIBS is populated.
40+
# Generally things after this point may depend on MLIR_ALL_LIBS or libMLIR.so.
41+
add_subdirectory(tools)
4042

4143
if( LLVM_INCLUDE_EXAMPLES )
4244
add_subdirectory(examples)

mlir/cmake/modules/AddMLIR.cmake

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,20 @@ function(add_mlir_dialect dialect dialect_doc_filename)
4949
add_dependencies(mlir-doc ${dialect_doc_filename}DocGen)
5050
endfunction()
5151

52+
# Declare a library which can be compiled in libMLIR.so
53+
macro(add_mlir_library name)
54+
set_property(GLOBAL APPEND PROPERTY MLIR_ALL_LIBS ${name})
55+
add_llvm_library(${ARGV})
56+
endmacro(add_mlir_library)
57+
5258
# Declare the library associated with a dialect.
5359
function(add_mlir_dialect_library name)
5460
set_property(GLOBAL APPEND PROPERTY MLIR_DIALECT_LIBS ${name})
55-
add_llvm_library(${ARGV})
61+
add_mlir_library(${ARGV})
5662
endfunction(add_mlir_dialect_library)
5763

5864
# Declare the library associated with a conversion.
5965
function(add_mlir_conversion_library name)
6066
set_property(GLOBAL APPEND PROPERTY MLIR_CONVERSION_LIBS ${name})
61-
add_llvm_library(${ARGV})
67+
add_mlir_library(${ARGV})
6268
endfunction(add_mlir_conversion_library)

mlir/lib/Analysis/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ set(LLVM_OPTIONAL_SOURCES
1313
Verifier.cpp
1414
)
1515

16-
add_llvm_library(MLIRAnalysis
16+
add_mlir_library(MLIRAnalysis
1717
CallGraph.cpp
1818
ControlFlowInterfaces.cpp
1919
InferTypeOpInterface.cpp
@@ -37,7 +37,7 @@ target_link_libraries(MLIRAnalysis
3737
MLIRLoopOps
3838
)
3939

40-
add_llvm_library(MLIRLoopAnalysis
40+
add_mlir_library(MLIRLoopAnalysis
4141
AffineAnalysis.cpp
4242
AffineStructures.cpp
4343
LoopAnalysis.cpp

mlir/lib/Dialect/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ add_subdirectory(SPIRV)
1212
add_subdirectory(StandardOps)
1313
add_subdirectory(VectorOps)
1414

15-
1615
set(LLVM_OPTIONAL_SOURCES
1716
Traits.cpp
1817
)
1918

20-
add_llvm_library(MLIRDialect
19+
add_mlir_library(MLIRDialect
2120
Traits.cpp
2221

2322
ADDITIONAL_HEADER_DIRS

mlir/lib/EDSC/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ set(LLVM_OPTIONAL_SOURCES
33
CoreAPIs.cpp
44
)
55

6-
add_llvm_library(MLIREDSC
6+
add_mlir_library(MLIREDSC
77
Builders.cpp
88

99
ADDITIONAL_HEADER_DIRS
@@ -16,7 +16,7 @@ target_link_libraries(MLIREDSC
1616
MLIRSupport
1717
)
1818

19-
add_llvm_library(MLIREDSCInterface
19+
add_mlir_library(MLIREDSCInterface
2020
CoreAPIs.cpp
2121

2222
ADDITIONAL_HEADER_DIRS

mlir/lib/ExecutionEngine/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ set(LLVM_OPTIONAL_SOURCES
66
)
77

88
llvm_map_components_to_libnames(outlibs "nativecodegen" "IPO")
9-
add_llvm_library(MLIRExecutionEngine
9+
add_mlir_library(MLIRExecutionEngine
1010
ExecutionEngine.cpp
1111
OptUtils.cpp
1212

mlir/lib/IR/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
file(GLOB globbed *.c *.cpp)
2-
add_llvm_library(MLIRIR
2+
add_mlir_library(MLIRIR
33
${globbed}
44

55
ADDITIONAL_HEADER_DIRS

mlir/lib/Parser/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
add_llvm_library(MLIRParser
1+
add_mlir_library(MLIRParser
22
Lexer.cpp
33
Parser.cpp
44
Token.cpp

mlir/lib/Pass/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
file(GLOB globbed *.c *.cpp)
2-
add_llvm_library(MLIRPass
2+
add_mlir_library(MLIRPass
33
${globbed}
44

55
ADDITIONAL_HEADER_DIRS

mlir/lib/Quantizer/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Support.
2-
add_llvm_library(MLIRQuantizerSupport
2+
add_mlir_library(MLIRQuantizerSupport
33
Support/Configuration.cpp
44
Support/ConstraintAnalysisGraph.cpp
55
Support/Metadata.cpp
@@ -21,7 +21,7 @@ target_link_libraries(MLIRQuantizerSupport
2121
)
2222

2323
# Configurations.
24-
add_llvm_library(MLIRQuantizerFxpMathConfig
24+
add_mlir_library(MLIRQuantizerFxpMathConfig
2525
Configurations/FxpMathConfig.cpp
2626

2727
ADDITIONAL_HEADER_DIRS
@@ -42,7 +42,7 @@ target_link_libraries(MLIRQuantizerFxpMathConfig
4242
)
4343

4444
# Transforms.
45-
add_llvm_library(MLIRQuantizerTransforms
45+
add_mlir_library(MLIRQuantizerTransforms
4646
Transforms/AddDefaultStatsTestPass.cpp
4747
Transforms/InferQuantizedTypesPass.cpp
4848
Transforms/RemoveInstrumentationPass.cpp

mlir/lib/Support/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ set(LLVM_OPTIONAL_SOURCES
77
TranslateClParser.cpp
88
)
99

10-
add_llvm_library(MLIRSupport
10+
add_mlir_library(MLIRSupport
1111
FileUtilities.cpp
1212
StorageUniquer.cpp
1313
ToolUtilities.cpp
@@ -20,7 +20,7 @@ target_link_libraries(MLIRSupport
2020
LLVMSupport
2121
${LLVM_PTHREAD_LIB})
2222

23-
add_llvm_library(MLIROptLib
23+
add_mlir_library(MLIROptLib
2424
MlirOptMain.cpp
2525

2626
ADDITIONAL_HEADER_DIRS
@@ -34,7 +34,7 @@ target_link_libraries(MLIROptLib
3434
MLIRSupport
3535
)
3636

37-
add_llvm_library(MLIRTranslateClParser
37+
add_mlir_library(MLIRTranslateClParser
3838
TranslateClParser.cpp
3939

4040
ADDITIONAL_HEADER_DIRS

mlir/lib/Target/CMakeLists.txt

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
add_llvm_library(MLIRTargetLLVMIRModuleTranslation
1+
add_mlir_library(MLIRTargetLLVMIRModuleTranslation
22
LLVMIR/DebugTranslation.cpp
33
LLVMIR/ModuleTranslation.cpp
44

@@ -9,9 +9,15 @@ add_llvm_library(MLIRTargetLLVMIRModuleTranslation
99
)
1010
target_link_libraries(MLIRTargetLLVMIRModuleTranslation
1111
PUBLIC
12-
MLIRLLVMIR LLVMCore LLVMIRReader LLVMSupport LLVMTransformUtils
13-
MLIRTranslation)
14-
add_llvm_library(MLIRTargetLLVMIR
12+
MLIRLLVMIR
13+
LLVMCore
14+
LLVMIRReader
15+
LLVMSupport
16+
LLVMTransformUtils
17+
MLIRTranslation
18+
)
19+
20+
add_mlir_library(MLIRTargetLLVMIR
1521
LLVMIR/ConvertFromLLVMIR.cpp
1622
LLVMIR/ConvertToLLVMIR.cpp
1723

@@ -20,8 +26,10 @@ add_llvm_library(MLIRTargetLLVMIR
2026
)
2127
target_link_libraries(MLIRTargetLLVMIR
2228
PUBLIC
23-
MLIRTargetLLVMIRModuleTranslation)
24-
add_llvm_library(MLIRTargetNVVMIR
29+
MLIRTargetLLVMIRModuleTranslation
30+
)
31+
32+
add_mlir_library(MLIRTargetNVVMIR
2533
LLVMIR/ConvertToNVVMIR.cpp
2634

2735
ADDITIONAL_HEADER_DIRS
@@ -37,7 +45,8 @@ target_link_libraries(MLIRTargetNVVMIR
3745
MLIRNVVMIR
3846
MLIRTargetLLVMIRModuleTranslation
3947
)
40-
add_llvm_library(MLIRTargetROCDLIR
48+
49+
add_mlir_library(MLIRTargetROCDLIR
4150
LLVMIR/ConvertToROCDLIR.cpp
4251

4352
ADDITIONAL_HEADER_DIRS

mlir/lib/Transforms/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
add_subdirectory(Utils)
22

3-
add_llvm_library(MLIRTransforms
3+
add_mlir_library(MLIRTransforms
44
AffineDataCopyGeneration.cpp
55
AffineLoopInvariantCodeMotion.cpp
66
Canonicalizer.cpp

mlir/lib/Transforms/Utils/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
add_llvm_library(MLIRTransformUtils
1+
add_mlir_library(MLIRTransformUtils
22
FoldUtils.cpp
33
GreedyPatternRewriteDriver.cpp
44
InliningUtils.cpp

mlir/lib/Translation/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
add_llvm_library(MLIRTranslation
1+
add_mlir_library(MLIRTranslation
22
Translation.cpp
33

44
ADDITIONAL_HEADER_DIRS

mlir/tools/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ add_subdirectory(mlir-opt)
44
add_subdirectory(mlir-tblgen)
55
add_subdirectory(mlir-translate)
66
add_subdirectory(mlir-vulkan-runner)
7+
add_subdirectory(mlir-shlib)

mlir/tools/mlir-opt/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set(LLVM_OPTIONAL_SOURCES
44

55
set(LIB_LIBS
66
MLIRAnalysis
7+
MLIRIR
78
MLIRLLVMIR
89
MLIROptLib
910
MLIRParser

mlir/tools/mlir-shlib/CMakeLists.txt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Building libmlir-cpp.so fails if LLVM_ENABLE_PIC=Off
2+
if (NOT LLVM_ENABLE_PIC)
3+
return()
4+
endif()
5+
6+
# Building libmlir-cpp.so may not work on MSVC
7+
if (MSVC)
8+
return()
9+
endif()
10+
11+
get_property(mlir_libs GLOBAL PROPERTY MLIR_ALL_LIBS)
12+
list(REMOVE_DUPLICATES mlir_libs)
13+
14+
foreach (lib ${mlir_libs})
15+
if(XCODE)
16+
# Xcode doesn't support object libraries, so we have to trick it into
17+
# linking the static libraries instead.
18+
list(APPEND _DEPS "-force_load" ${lib})
19+
else()
20+
list(APPEND _OBJECTS $<TARGET_OBJECTS:obj.${lib}>)
21+
endif()
22+
list(APPEND _DEPS $<TARGET_PROPERTY:${lib},LINK_LIBRARIES>)
23+
endforeach ()
24+
25+
if(MLIR_LINK_MLIR_DYLIB)
26+
set(INSTALL_WITH_TOOLCHAIN INSTALL_WITH_TOOLCHAIN)
27+
endif()
28+
29+
# libMLIR.so depends on LLVM components. To avoid multiple
30+
# copies of those LLVM components, libMLIR.so depends on libLLVM.so.
31+
# This probably won't work if some LLVM components are not included
32+
# in libLLVM.so.
33+
if(LLVM_BUILD_LLVM_DYLIB)
34+
add_llvm_library(MLIR
35+
SHARED
36+
${INSTALL_WITH_TOOLCHAIN}
37+
38+
mlir-shlib.cpp
39+
)
40+
target_link_libraries(MLIR PRIVATE LLVM ${LLVM_PTHREAD_LIB})
41+
whole_archive_link(MLIR ${mlir_libs})
42+
endif()

mlir/tools/mlir-shlib/mlir-shlib.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// Intentionally empty source file to make CMake happy

0 commit comments

Comments
 (0)