Skip to content

Commit e17d9c1

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 31e07d7 commit e17d9c1

File tree

19 files changed

+79
-26
lines changed

19 files changed

+79
-26
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
@@ -12,7 +12,7 @@ set(LLVM_OPTIONAL_SOURCES
1212
Verifier.cpp
1313
)
1414

15-
add_llvm_library(MLIRAnalysis
15+
add_mlir_library(MLIRAnalysis
1616
CallGraph.cpp
1717
InferTypeOpInterface.cpp
1818
Liveness.cpp
@@ -35,7 +35,7 @@ add_llvm_library(MLIRAnalysis
3535
LLVMSupport
3636
)
3737

38-
add_llvm_library(MLIRLoopAnalysis
38+
add_mlir_library(MLIRLoopAnalysis
3939
AffineAnalysis.cpp
4040
AffineStructures.cpp
4141
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
@@ -15,7 +15,7 @@ add_llvm_library(MLIREDSC
1515
LLVMSupport
1616
)
1717

18-
add_llvm_library(MLIREDSCInterface
18+
add_mlir_library(MLIREDSCInterface
1919
CoreAPIs.cpp
2020

2121
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
@@ -19,7 +19,7 @@ add_llvm_library(MLIRQuantizerSupport
1919
)
2020

2121
# Configurations.
22-
add_llvm_library(MLIRQuantizerFxpMathConfig
22+
add_mlir_library(MLIRQuantizerFxpMathConfig
2323
Configurations/FxpMathConfig.cpp
2424

2525
ADDITIONAL_HEADER_DIRS
@@ -38,7 +38,7 @@ add_llvm_library(MLIRQuantizerFxpMathConfig
3838
)
3939

4040
# Transforms.
41-
add_llvm_library(MLIRQuantizerTransforms
41+
add_mlir_library(MLIRQuantizerTransforms
4242
Transforms/AddDefaultStatsTestPass.cpp
4343
Transforms/InferQuantizedTypesPass.cpp
4444
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 @@ add_llvm_library(MLIRSupport
2020
${LLVM_PTHREAD_LIB}
2121
)
2222

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

2626
ADDITIONAL_HEADER_DIRS
@@ -34,7 +34,7 @@ add_llvm_library(MLIROptLib
3434
LLVMSupport
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: 5 additions & 4 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

@@ -19,7 +19,7 @@ add_llvm_library(MLIRTargetLLVMIRModuleTranslation
1919
MLIRTranslation
2020
)
2121

22-
add_llvm_library(MLIRTargetLLVMIR
22+
add_mlir_library(MLIRTargetLLVMIR
2323
LLVMIR/ConvertFromLLVMIR.cpp
2424
LLVMIR/ConvertToLLVMIR.cpp
2525

@@ -36,7 +36,8 @@ add_llvm_library(MLIRTargetLLVMIR
3636
LLVMIRReader
3737
LLVMSupport
3838
)
39-
add_llvm_library(MLIRTargetNVVMIR
39+
40+
add_mlir_library(MLIRTargetNVVMIR
4041
LLVMIR/ConvertToNVVMIR.cpp
4142

4243
ADDITIONAL_HEADER_DIRS
@@ -56,7 +57,7 @@ add_llvm_library(MLIRTargetNVVMIR
5657
LLVMSupport
5758
)
5859

59-
add_llvm_library(MLIRTargetROCDLIR
60+
add_mlir_library(MLIRTargetROCDLIR
6061
LLVMIR/ConvertToROCDLIR.cpp
6162

6263
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)