Skip to content

Commit

Permalink
[MLIR] Add support for libMLIR.so
Browse files Browse the repository at this point in the history
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

(updated by Stephen Neuendorffer)

Differential Revision: https://reviews.llvm.org/D73130
  • Loading branch information
vchuravy authored and stephenneuendorffer committed Feb 28, 2020
1 parent 8a2b86b commit 1246e86
Show file tree
Hide file tree
Showing 19 changed files with 100 additions and 26 deletions.
4 changes: 3 additions & 1 deletion mlir/CMakeLists.txt
Expand Up @@ -34,9 +34,11 @@ include_directories( ${MLIR_INCLUDE_DIR})

add_subdirectory(include/mlir)
add_subdirectory(lib)
add_subdirectory(tools)
add_subdirectory(unittests)
add_subdirectory(test)
# 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( LLVM_INCLUDE_EXAMPLES )
add_subdirectory(examples)
Expand Down
31 changes: 29 additions & 2 deletions mlir/cmake/modules/AddMLIR.cmake
Expand Up @@ -49,14 +49,41 @@ function(add_mlir_dialect dialect dialect_doc_filename)
add_dependencies(mlir-doc ${dialect_doc_filename}DocGen)
endfunction()

# Declare a library which can be compiled in libMLIR.so
macro(add_mlir_library name)
cmake_parse_arguments(ARG
"SHARED;INSTALL_WITH_TOOLCHAIN"
""
"ADDITIONAL_HEADERS"
${ARGN})
set(srcs)
if(ARG_SHARED)
set(LIBTYPE SHARED)
else()
# llvm_add_library ignores BUILD_SHARED_LIBS if STATIC is explicitly set,
# so we need to handle it here.
if(BUILD_SHARED_LIBS)
set(LIBTYPE SHARED)
else()
set(LIBTYPE STATIC)
endif()
if(NOT XCODE)
# The Xcode generator doesn't handle object libraries correctly.
list(APPEND LIBTYPE OBJECT)
endif()
set_property(GLOBAL APPEND PROPERTY MLIR_ALL_LIBS ${name})
endif()
add_llvm_library(${name} ${LIBTYPE} ${ARG_UNPARSED_ARGUMENTS} ${srcs})
endmacro(add_mlir_library)

# Declare the library associated with a dialect.
function(add_mlir_dialect_library name)
set_property(GLOBAL APPEND PROPERTY MLIR_DIALECT_LIBS ${name})
add_llvm_library(${ARGV})
add_mlir_library(${ARGV})
endfunction(add_mlir_dialect_library)

# Declare the library associated with a conversion.
function(add_mlir_conversion_library name)
set_property(GLOBAL APPEND PROPERTY MLIR_CONVERSION_LIBS ${name})
add_llvm_library(${ARGV})
add_mlir_library(${ARGV})
endfunction(add_mlir_conversion_library)
4 changes: 2 additions & 2 deletions mlir/lib/Analysis/CMakeLists.txt
Expand Up @@ -12,7 +12,7 @@ set(LLVM_OPTIONAL_SOURCES
Verifier.cpp
)

add_llvm_library(MLIRAnalysis
add_mlir_library(MLIRAnalysis
CallGraph.cpp
InferTypeOpInterface.cpp
Liveness.cpp
Expand All @@ -35,7 +35,7 @@ add_llvm_library(MLIRAnalysis
LLVMSupport
)

add_llvm_library(MLIRLoopAnalysis
add_mlir_library(MLIRLoopAnalysis
AffineAnalysis.cpp
AffineStructures.cpp
LoopAnalysis.cpp
Expand Down
3 changes: 1 addition & 2 deletions mlir/lib/Dialect/CMakeLists.txt
Expand Up @@ -12,12 +12,11 @@ add_subdirectory(SPIRV)
add_subdirectory(StandardOps)
add_subdirectory(VectorOps)


set(LLVM_OPTIONAL_SOURCES
Traits.cpp
)

add_llvm_library(MLIRDialect
add_mlir_library(MLIRDialect
Traits.cpp

ADDITIONAL_HEADER_DIRS
Expand Down
4 changes: 2 additions & 2 deletions mlir/lib/EDSC/CMakeLists.txt
Expand Up @@ -3,7 +3,7 @@ set(LLVM_OPTIONAL_SOURCES
CoreAPIs.cpp
)

add_llvm_library(MLIREDSC
add_mlir_library(MLIREDSC
Builders.cpp

ADDITIONAL_HEADER_DIRS
Expand All @@ -15,7 +15,7 @@ add_llvm_library(MLIREDSC
LLVMSupport
)

add_llvm_library(MLIREDSCInterface
add_mlir_library(MLIREDSCInterface
CoreAPIs.cpp

ADDITIONAL_HEADER_DIRS
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/ExecutionEngine/CMakeLists.txt
Expand Up @@ -6,7 +6,7 @@ set(LLVM_OPTIONAL_SOURCES
)

llvm_map_components_to_libnames(outlibs "nativecodegen" "IPO")
add_llvm_library(MLIRExecutionEngine
add_mlir_library(MLIRExecutionEngine
ExecutionEngine.cpp
OptUtils.cpp

Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/IR/CMakeLists.txt
@@ -1,5 +1,5 @@
file(GLOB globbed *.c *.cpp)
add_llvm_library(MLIRIR
add_mlir_library(MLIRIR
${globbed}

ADDITIONAL_HEADER_DIRS
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Parser/CMakeLists.txt
@@ -1,4 +1,4 @@
add_llvm_library(MLIRParser
add_mlir_library(MLIRParser
Lexer.cpp
Parser.cpp
Token.cpp
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Pass/CMakeLists.txt
@@ -1,5 +1,5 @@
file(GLOB globbed *.c *.cpp)
add_llvm_library(MLIRPass
add_mlir_library(MLIRPass
${globbed}

ADDITIONAL_HEADER_DIRS
Expand Down
6 changes: 3 additions & 3 deletions mlir/lib/Quantizer/CMakeLists.txt
@@ -1,5 +1,5 @@
# Support.
add_llvm_library(MLIRQuantizerSupport
add_mlir_library(MLIRQuantizerSupport
Support/Configuration.cpp
Support/ConstraintAnalysisGraph.cpp
Support/Metadata.cpp
Expand All @@ -19,7 +19,7 @@ add_llvm_library(MLIRQuantizerSupport
)

# Configurations.
add_llvm_library(MLIRQuantizerFxpMathConfig
add_mlir_library(MLIRQuantizerFxpMathConfig
Configurations/FxpMathConfig.cpp

ADDITIONAL_HEADER_DIRS
Expand All @@ -38,7 +38,7 @@ add_llvm_library(MLIRQuantizerFxpMathConfig
)

# Transforms.
add_llvm_library(MLIRQuantizerTransforms
add_mlir_library(MLIRQuantizerTransforms
Transforms/AddDefaultStatsTestPass.cpp
Transforms/InferQuantizedTypesPass.cpp
Transforms/RemoveInstrumentationPass.cpp
Expand Down
6 changes: 3 additions & 3 deletions mlir/lib/Support/CMakeLists.txt
Expand Up @@ -7,7 +7,7 @@ set(LLVM_OPTIONAL_SOURCES
TranslateClParser.cpp
)

add_llvm_library(MLIRSupport
add_mlir_library(MLIRSupport
FileUtilities.cpp
StorageUniquer.cpp
ToolUtilities.cpp
Expand All @@ -20,7 +20,7 @@ add_llvm_library(MLIRSupport
${LLVM_PTHREAD_LIB}
)

add_llvm_library(MLIROptLib
add_mlir_library(MLIROptLib
MlirOptMain.cpp

ADDITIONAL_HEADER_DIRS
Expand All @@ -34,7 +34,7 @@ add_llvm_library(MLIROptLib
LLVMSupport
)

add_llvm_library(MLIRTranslateClParser
add_mlir_library(MLIRTranslateClParser
TranslateClParser.cpp

ADDITIONAL_HEADER_DIRS
Expand Down
9 changes: 5 additions & 4 deletions mlir/lib/Target/CMakeLists.txt
@@ -1,4 +1,4 @@
add_llvm_library(MLIRTargetLLVMIRModuleTranslation
add_mlir_library(MLIRTargetLLVMIRModuleTranslation
LLVMIR/DebugTranslation.cpp
LLVMIR/ModuleTranslation.cpp

Expand All @@ -19,7 +19,7 @@ add_llvm_library(MLIRTargetLLVMIRModuleTranslation
MLIRTranslation
)

add_llvm_library(MLIRTargetLLVMIR
add_mlir_library(MLIRTargetLLVMIR
LLVMIR/ConvertFromLLVMIR.cpp
LLVMIR/ConvertToLLVMIR.cpp

Expand All @@ -36,7 +36,8 @@ add_llvm_library(MLIRTargetLLVMIR
LLVMIRReader
LLVMSupport
)
add_llvm_library(MLIRTargetNVVMIR

add_mlir_library(MLIRTargetNVVMIR
LLVMIR/ConvertToNVVMIR.cpp

ADDITIONAL_HEADER_DIRS
Expand All @@ -56,7 +57,7 @@ add_llvm_library(MLIRTargetNVVMIR
LLVMSupport
)

add_llvm_library(MLIRTargetROCDLIR
add_mlir_library(MLIRTargetROCDLIR
LLVMIR/ConvertToROCDLIR.cpp

ADDITIONAL_HEADER_DIRS
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Transforms/CMakeLists.txt
@@ -1,6 +1,6 @@
add_subdirectory(Utils)

add_llvm_library(MLIRTransforms
add_mlir_library(MLIRTransforms
AffineDataCopyGeneration.cpp
AffineLoopInvariantCodeMotion.cpp
Canonicalizer.cpp
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Transforms/Utils/CMakeLists.txt
@@ -1,4 +1,4 @@
add_llvm_library(MLIRTransformUtils
add_mlir_library(MLIRTransformUtils
FoldUtils.cpp
GreedyPatternRewriteDriver.cpp
InliningUtils.cpp
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Translation/CMakeLists.txt
@@ -1,4 +1,4 @@
add_llvm_library(MLIRTranslation
add_mlir_library(MLIRTranslation
Translation.cpp

ADDITIONAL_HEADER_DIRS
Expand Down
1 change: 1 addition & 0 deletions mlir/tools/CMakeLists.txt
Expand Up @@ -4,3 +4,4 @@ add_subdirectory(mlir-opt)
add_subdirectory(mlir-tblgen)
add_subdirectory(mlir-translate)
add_subdirectory(mlir-vulkan-runner)
add_subdirectory(mlir-shlib)
1 change: 1 addition & 0 deletions mlir/tools/mlir-opt/CMakeLists.txt
Expand Up @@ -4,6 +4,7 @@ set(LLVM_OPTIONAL_SOURCES

set(LIB_LIBS
MLIRAnalysis
MLIRIR
MLIRLLVMIR
MLIROptLib
MLIRParser
Expand Down
42 changes: 42 additions & 0 deletions mlir/tools/mlir-shlib/CMakeLists.txt
@@ -0,0 +1,42 @@
# Building libmlir-cpp.so fails if LLVM_ENABLE_PIC=Off
if (NOT LLVM_ENABLE_PIC)
return()
endif()

# Building libmlir-cpp.so may not work on MSVC
if (MSVC)
return()
endif()

get_property(mlir_libs GLOBAL PROPERTY MLIR_ALL_LIBS)
list(REMOVE_DUPLICATES mlir_libs)

foreach (lib ${mlir_libs})
if(XCODE)
# Xcode doesn't support object libraries, so we have to trick it into
# linking the static libraries instead.
list(APPEND _DEPS "-force_load" ${lib})
else()
list(APPEND _OBJECTS $<TARGET_OBJECTS:obj.${lib}>)
endif()
list(APPEND _DEPS $<TARGET_PROPERTY:${lib},LINK_LIBRARIES>)
endforeach ()

if(MLIR_LINK_MLIR_DYLIB)
set(INSTALL_WITH_TOOLCHAIN INSTALL_WITH_TOOLCHAIN)
endif()

# libMLIR.so depends on LLVM components. To avoid multiple
# copies of those LLVM components, libMLIR.so depends on libLLVM.so.
# This probably won't work if some LLVM components are not included
# in libLLVM.so.
if(LLVM_BUILD_LLVM_DYLIB)
add_llvm_library(MLIR
SHARED
${INSTALL_WITH_TOOLCHAIN}

mlir-shlib.cpp
)
target_link_libraries(MLIR PRIVATE LLVM ${LLVM_PTHREAD_LIB})
whole_archive_link(MLIR ${mlir_libs})
endif()
1 change: 1 addition & 0 deletions mlir/tools/mlir-shlib/mlir-shlib.cpp
@@ -0,0 +1 @@
// Intentionally empty source file to make CMake happy

0 comments on commit 1246e86

Please sign in to comment.