Skip to content

Commit

Permalink
[CIR] Support standalone builds against MLIR dylib
Browse files Browse the repository at this point in the history
This commit combines three changes needed for ClangIR to be built
successfully on Gentoo with standalone Clang/MLIR install:

1. It unblocks `CLANG_ENABLE_CIR` + `CLANG_BUILT_STANDALONE` scenario,
   obtaining the necessary bits from system-wide installation of MLIR
   via `find_package(MLIR)`.

2. It switches linking to MLIR libraries to use
   `mlir_target_link_libraries()` introduced
   in 10ef20f, which supports both
   linking to invidual MLIR static libraries and to the shared
   libMLIR library.

3. It makes dependencies on MLIR-specific targets conditional
   to non-standalone builds -- since these targets aren't present
   in clang build tree when it is built standalone.
  • Loading branch information
mgorny committed Dec 22, 2024
1 parent b36cb65 commit fa15e8f
Show file tree
Hide file tree
Showing 14 changed files with 126 additions and 48 deletions.
8 changes: 4 additions & 4 deletions clang/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ endif()

if(CLANG_ENABLE_CIR)
if (CLANG_BUILT_STANDALONE)
message(FATAL_ERROR
"ClangIR is not yet supported in the standalone build.")
endif()
if (NOT "${LLVM_ENABLE_PROJECTS}" MATCHES "MLIR|mlir")
find_package(MLIR REQUIRED)
list(APPEND CMAKE_MODULE_PATH "${MLIR_DIR}")
include(AddMLIR)
elseif (NOT "${LLVM_ENABLE_PROJECTS}" MATCHES "MLIR|mlir")
message(FATAL_ERROR
"Cannot build ClangIR without MLIR in LLVM_ENABLE_PROJECTS")
endif()
Expand Down
12 changes: 10 additions & 2 deletions clang/lib/CIR/CodeGen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ set(

get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)

set(deps)
if (NOT CLANG_BUILT_STANDALONE)
set(deps ${dialect_libs})
endif()

add_clang_library(clangCIR
CIRAsm.cpp
CIRGenAtomic.cpp
Expand Down Expand Up @@ -52,16 +57,19 @@ add_clang_library(clangCIR
MLIRCIRASTAttrInterfacesIncGen
MLIRCIROpInterfacesIncGen
MLIRCIRLoopOpInterfaceIncGen
${dialect_libs}
${deps}

LINK_LIBS
clangAST
clangBasic
clangLex
${dialect_libs}
MLIRCIR
MLIRCIRTransforms
MLIRCIRInterfaces
)

mlir_target_link_libraries(clangCIR PUBLIC
${dialect_libs}
MLIRAffineToStandard
MLIRAnalysis
MLIRDLTIDialect
Expand Down
19 changes: 14 additions & 5 deletions clang/lib/CIR/Dialect/IR/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
set(deps)
if (NOT CLANG_BUILT_STANDALONE)
set(deps
MLIRBuiltinLocationAttributesIncGen
MLIRSymbolInterfacesIncGen
)
endif()

add_clang_library(MLIRCIR
CIRAttrs.cpp
CIROpenCLAttrs.cpp
Expand All @@ -8,22 +16,23 @@ add_clang_library(MLIRCIR
FPEnv.cpp

DEPENDS
MLIRBuiltinLocationAttributesIncGen
${deps}
MLIRCIROpsIncGen
MLIRCIREnumsGen
MLIRSymbolInterfacesIncGen
MLIRCIRASTAttrInterfacesIncGen
MLIRCIROpInterfacesIncGen
MLIRCIRLoopOpInterfaceIncGen

LINK_LIBS PUBLIC
clangAST
)

mlir_target_link_libraries(MLIRCIR PUBLIC
MLIRIR
MLIRCIRInterfaces
MLIRDLTIDialect
MLIRDataLayoutInterfaces
MLIRFuncDialect
MLIRLoopLikeInterface
MLIRLLVMDialect
MLIRSideEffectInterfaces
clangAST
)
MLIRSideEffectInterfaces)
8 changes: 5 additions & 3 deletions clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ add_clang_library(MLIRCIRTransforms
clangBasic
TargetLowering

MLIRCIR
MLIRCIRInterfaces
)

mlir_target_link_libraries(MLIRCIRTransforms PUBLIC
MLIRAnalysis
MLIRIR
MLIRPass
MLIRTransformUtils

MLIRCIR
MLIRCIRInterfaces
)
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ add_clang_library(TargetLowering
LINK_LIBS PUBLIC

clangBasic
MLIRCIR
MLIRCIRInterfaces
)

mlir_target_link_libraries(TargetLowering PUBLIC
MLIRIR
MLIRPass
MLIRDLTIDialect
MLIRCIR
MLIRCIRInterfaces
)
18 changes: 14 additions & 4 deletions clang/lib/CIR/FrontendAction/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ set(LLVM_LINK_COMPONENTS

get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)

set(deps)
if (NOT CLANG_BUILT_STANDALONE)
set(deps
MLIRBuiltinLocationAttributesIncGen
MLIRBuiltinTypeInterfacesIncGen
MLIRFunctionInterfacesIncGen
)
endif()

add_clang_library(clangCIRFrontendAction
CIRGenAction.cpp

Expand All @@ -13,9 +22,7 @@ add_clang_library(clangCIRFrontendAction
MLIRCIRASTAttrInterfacesIncGen
MLIRCIROpInterfacesIncGen
MLIRCIRLoopOpInterfaceIncGen
MLIRBuiltinLocationAttributesIncGen
MLIRBuiltinTypeInterfacesIncGen
MLIRFunctionInterfacesIncGen
${deps}

LINK_LIBS
clangAST
Expand All @@ -26,8 +33,11 @@ add_clang_library(clangCIRFrontendAction
clangCIR
clangCIRLoweringDirectToLLVM
clangCIRLoweringThroughMLIR
${dialect_libs}
MLIRCIR
)

mlir_target_link_libraries(clangCIRFrontendAction PUBLIC
${dialect_libs}
MLIRAnalysis
MLIRIR
MLIRParser
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/CIR/Interfaces/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ add_clang_library(MLIRCIRInterfaces
MLIRCIRFPTypeInterfaceIncGen
MLIRCIRLoopOpInterfaceIncGen
MLIRCIROpInterfacesIncGen
)

LINK_LIBS
mlir_target_link_libraries(clangCIRFrontendAction PUBLIC
${dialect_libs}
MLIRIR
MLIRSupport
Expand Down
18 changes: 14 additions & 4 deletions clang/lib/CIR/Lowering/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ set(LLVM_LINK_COMPONENTS

get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)

set(deps)
if (NOT CLANG_BUILT_STANDALONE)
set(deps
MLIRBuiltinLocationAttributesIncGen
MLIRBuiltinTypeInterfacesIncGen
MLIRFunctionInterfacesIncGen
)
endif()

add_clang_library(clangCIRLoweringHelpers
LoweringHelpers.cpp

Expand All @@ -14,9 +23,7 @@ add_clang_library(clangCIRLoweringHelpers
MLIRCIRASTAttrInterfacesIncGen
MLIRCIROpInterfacesIncGen
MLIRCIRLoopOpInterfaceIncGen
MLIRBuiltinLocationAttributesIncGen
MLIRBuiltinTypeInterfacesIncGen
MLIRFunctionInterfacesIncGen
${deps}

LINK_LIBS
clangAST
Expand All @@ -25,8 +32,11 @@ add_clang_library(clangCIRLoweringHelpers
clangLex
clangFrontend
clangCIR
${dialect_libs}
MLIRCIR
)

mlir_target_link_libraries(clangCIRLoweringHelpers PUBLIC
${dialect_libs}
MLIRAnalysis
MLIRBuiltinToLLVMIRTranslation
MLIRLLVMToLLVMIRTranslation
Expand Down
17 changes: 13 additions & 4 deletions clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ set(LLVM_LINK_COMPONENTS

get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)

set(deps)
if (NOT CLANG_BUILT_STANDALONE)
set(deps
MLIRBuiltinLocationAttributesIncGen
MLIRBuiltinTypeInterfacesIncGen
MLIRFunctionInterfacesIncGen
)
endif()

add_clang_library(clangCIRLoweringDirectToLLVM
LowerToLLVMIR.cpp
LowerToLLVM.cpp
Expand All @@ -15,9 +24,7 @@ add_clang_library(clangCIRLoweringDirectToLLVM
MLIRCIRASTAttrInterfacesIncGen
MLIRCIROpInterfacesIncGen
MLIRCIRLoopOpInterfaceIncGen
MLIRBuiltinLocationAttributesIncGen
MLIRBuiltinTypeInterfacesIncGen
MLIRFunctionInterfacesIncGen
${deps}

LINK_LIBS
clangAST
Expand All @@ -27,8 +34,10 @@ add_clang_library(clangCIRLoweringDirectToLLVM
clangFrontend
clangCIR
clangCIRLoweringHelpers
${dialect_libs}
MLIRCIR
)
mlir_target_link_libraries(clangCIRLoweringDirectToLLVM PUBLIC
${dialect_libs}
MLIRAnalysis
MLIRBuiltinToLLVMIRTranslation
MLIRLLVMToLLVMIRTranslation
Expand Down
18 changes: 14 additions & 4 deletions clang/lib/CIR/Lowering/ThroughMLIR/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ set(LLVM_LINK_COMPONENTS

get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)

set(deps)
if (NOT CLANG_BUILT_STANDALONE)
set(deps
MLIRBuiltinLocationAttributesIncGen
MLIRBuiltinTypeInterfacesIncGen
MLIRFunctionInterfacesIncGen
)
endif()

add_clang_library(clangCIRLoweringThroughMLIR
LowerCIRLoopToSCF.cpp
LowerCIRToMLIR.cpp
Expand All @@ -16,9 +25,7 @@ add_clang_library(clangCIRLoweringThroughMLIR
MLIRCIRASTAttrInterfacesIncGen
MLIRCIROpInterfacesIncGen
MLIRCIRLoopOpInterfaceIncGen
MLIRBuiltinLocationAttributesIncGen
MLIRBuiltinTypeInterfacesIncGen
MLIRFunctionInterfacesIncGen
${deps}

LINK_LIBS
clangAST
Expand All @@ -28,8 +35,11 @@ add_clang_library(clangCIRLoweringThroughMLIR
clangFrontend
clangCIR
clangCIRLoweringHelpers
${dialect_libs}
MLIRCIR
)

mlir_target_link_libraries(clangCIRLoweringThroughMLIR PUBLIC
${dialect_libs}
MLIRAnalysis
MLIRBuiltinToLLVMIRTranslation
MLIRLLVMToLLVMIRTranslation
Expand Down
21 changes: 14 additions & 7 deletions clang/lib/FrontendTool/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ set(deps)
if(CLANG_ENABLE_CIR)
list(APPEND link_libs
clangCIRFrontendAction
MLIRCIRTransforms
MLIRIR
MLIRPass
)
list(APPEND deps
MLIRBuiltinLocationAttributesIncGen
MLIRBuiltinTypeInterfacesIncGen
)
if(NOT CLANG_BUILT_STANDALONE)
list(APPEND deps
MLIRBuiltinLocationAttributesIncGen
MLIRBuiltinTypeInterfacesIncGen
)
endif()

include_directories(${LLVM_MAIN_SRC_DIR}/../mlir/include)
include_directories(${CMAKE_BINARY_DIR}/tools/mlir/include)
Expand Down Expand Up @@ -52,3 +51,11 @@ add_clang_library(clangFrontendTool
LINK_LIBS
${link_libs}
)

if(CLANG_ENABLE_CIR)
mlir_target_link_libraries(clangFrontendTool PUBLIC
MLIRCIRTransforms
MLIRIR
MLIRPass
)
endif()
21 changes: 15 additions & 6 deletions clang/tools/cir-lsp-server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@ get_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS)
include_directories(${LLVM_MAIN_SRC_DIR}/../mlir/include)
include_directories(${CMAKE_BINARY_DIR}/tools/mlir/include)

set(LIBS
set(MLIR_LIBS
${dialect_libs}
${conversion_libs}
${test_libs}
clangCIR
clangCIRLoweringThroughMLIR
clangCIRLoweringDirectToLLVM
MLIRCIR
MLIRAffineAnalysis
MLIRAnalysis
MLIRDialect
Expand All @@ -24,12 +20,25 @@ set(LIBS
MLIRIR
)

set(LIBS
clangCIR
clangCIRLoweringThroughMLIR
clangCIRLoweringDirectToLLVM
MLIRCIR
)

set(DEPS ${LIBS})
if (NOT CLANG_BUILT_STANDALONE)
list(APPEND DEPS ${MLIR_LIBS})
endif()

add_mlir_tool(cir-lsp-server
cir-lsp-server.cpp

DEPENDS
${LIBS}
${DEPS}
)

target_link_libraries(cir-lsp-server PRIVATE ${LIBS})
mlir_target_link_libraries(cir-lsp-server PRIVATE ${MLIR_LIBS})
llvm_update_compile_flags(cir-lsp-server)
2 changes: 1 addition & 1 deletion clang/tools/cir-opt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ clang_target_link_libraries(cir-opt
MLIRCIRTransforms
)

target_link_libraries(cir-opt
mlir_target_link_libraries(cir-opt
PRIVATE
${dialect_libs}
${conversion_libs}
Expand Down
2 changes: 1 addition & 1 deletion clang/tools/cir-translate/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ clang_target_link_libraries(cir-translate
MLIRCIRTransforms
)

target_link_libraries(cir-translate
mlir_target_link_libraries(cir-translate
PRIVATE
${dialect_libs}
${conversion_libs}
Expand Down

0 comments on commit fa15e8f

Please sign in to comment.