Skip to content

Commit

Permalink
[flang] Fix standalone builds against installed MLIR (#126387)
Browse files Browse the repository at this point in the history
1. Add a new `MLIR_DEPS` argument group to `flang_add_library()`, and
move MLIR-specific dependencies to that group. These dependencies are
added as usual in regular builds, and are skipped in standalone builds,
since MLIR targets are not visible there (and were already built and
installed).
2. Fix the value of `MLIR_MAIN_SRC_DIR` to refer to the current source
directory rather than the directory written into MLIR CMake files. The
latter refers to the directory used to build the MLIR package, and is no
longer valid.
3. Fix non-dylib friendly linking of `LLVMTargetParser` in `Optimizer`
unittests.

With these changes, I can successfully run Flang's regression tests.
  • Loading branch information
mgorny authored Feb 15, 2025
1 parent 776fa2d commit c30a7f4
Show file tree
Hide file tree
Showing 19 changed files with 87 additions and 38 deletions.
5 changes: 5 additions & 0 deletions flang/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ if (FLANG_STANDALONE_BUILD)
add_custom_target(doxygen ALL)
endif()

# Override the value from installed CMake files, as they refer
# to the directory used during the original MLIR package build,
# which may be no longer available. Instead, use the current checkout.
set(MLIR_MAIN_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../mlir )

else()
option(FLANG_INCLUDE_TESTS
"Generate build targets for the Flang unit tests."
Expand Down
5 changes: 4 additions & 1 deletion flang/cmake/modules/AddFlang.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ endmacro()

function(add_flang_library name)
set(options SHARED STATIC INSTALL_WITH_TOOLCHAIN)
set(multiValueArgs ADDITIONAL_HEADERS CLANG_LIBS MLIR_LIBS)
set(multiValueArgs ADDITIONAL_HEADERS CLANG_LIBS MLIR_LIBS MLIR_DEPS)
cmake_parse_arguments(ARG
"${options}"
""
Expand Down Expand Up @@ -69,6 +69,9 @@ function(add_flang_library name)
if (ARG_MLIR_LIBS)
mlir_target_link_libraries(${name} PRIVATE ${ARG_MLIR_LIBS})
endif()
if (ARG_MLIR_DEPS AND NOT FLANG_STANDALONE_BUILD)
add_dependencies(${name} ${ARG_MLIR_DEPS})
endif()

if (TARGET ${name})

Expand Down
8 changes: 5 additions & 3 deletions flang/lib/Frontend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ add_flang_library(flangFrontend
FIROptCodeGenPassIncGen
FIROptTransformsPassIncGen
HLFIRDialect
MLIRIR
${dialect_libs}
${extension_libs}

LINK_LIBS
CUFDialect
Expand Down Expand Up @@ -56,6 +53,11 @@ add_flang_library(flangFrontend
FrontendOpenACC
FrontendOpenMP

MLIR_DEPS
MLIRIR
${dialect_libs}
${extension_libs}

MLIR_LIBS
MLIRTransforms
MLIRBuiltinToLLVMIRTranslation
Expand Down
6 changes: 4 additions & 2 deletions flang/lib/Lower/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ add_flang_library(FortranLower
FIRDialect
FIRTransforms
HLFIRDialect
${dialect_libs}
${extension_libs}

LINK_LIBS
CUFAttrs
Expand All @@ -64,6 +62,10 @@ add_flang_library(FortranLower
LINK_COMPONENTS
Support

MLIR_DEPS
${dialect_libs}
${extension_libs}

MLIR_LIBS
${dialect_libs}
${extension_libs}
Expand Down
6 changes: 4 additions & 2 deletions flang/lib/Optimizer/Analysis/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ add_flang_library(FIRAnalysis
FIRDialect
FIRSupport
HLFIRDialect
MLIRIR
MLIROpenMPDialect

LINK_LIBS
FIRBuilder
FIRDialect
FIRSupport
HLFIRDialect

MLIR_DEPS
MLIRIR
MLIROpenMPDialect

MLIR_LIBS
MLIRFuncDialect
MLIRLLVMDialect
Expand Down
6 changes: 4 additions & 2 deletions flang/lib/Optimizer/Builder/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ add_flang_library(FIRBuilder
CUFDialect
FIRDialect
HLFIRDialect
${dialect_libs}
${extension_libs}

LINK_LIBS
CUFAttrs
Expand All @@ -52,6 +50,10 @@ add_flang_library(FIRBuilder
FortranEvaluate
HLFIRDialect

MLIR_DEPS
${dialect_libs}
${extension_libs}

MLIR_LIBS
${dialect_libs}
${extension_libs}
Expand Down
4 changes: 3 additions & 1 deletion flang/lib/Optimizer/Dialect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ add_flang_library(FIRDialect

DEPENDS
CanonicalizationPatternsIncGen
MLIRIR
FIROpsIncGen
CUFAttrsIncGen
intrinsics_gen
Expand All @@ -26,6 +25,9 @@ add_flang_library(FIRDialect
AsmPrinter
Remarks

MLIR_DEPS
MLIRIR

MLIR_LIBS
MLIRArithDialect
MLIRBuiltinToLLVMIRTranslation
Expand Down
4 changes: 3 additions & 1 deletion flang/lib/Optimizer/Dialect/CUF/Attributes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ add_flang_library(CUFAttrs
CUFAttr.cpp

DEPENDS
MLIRIR
CUFAttrsIncGen
CUFOpsIncGen

Expand All @@ -12,6 +11,9 @@ add_flang_library(CUFAttrs
AsmPrinter
Remarks

MLIR_DEPS
MLIRIR

MLIR_LIBS
MLIRTargetLLVMIRExport
)
4 changes: 3 additions & 1 deletion flang/lib/Optimizer/Dialect/CUF/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ add_flang_library(CUFDialect
CUFToLLVMIRTranslation.cpp

DEPENDS
MLIRIR
CUFAttrsIncGen
CUFOpsIncGen

Expand All @@ -20,6 +19,9 @@ add_flang_library(CUFDialect
AsmPrinter
Remarks

MLIR_DEPS
MLIRIR

MLIR_LIBS
MLIRIR
MLIRGPUDialect
Expand Down
4 changes: 3 additions & 1 deletion flang/lib/Optimizer/Dialect/Support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ add_flang_library(FIRDialectSupport
FIRContext.cpp

DEPENDS
MLIRIR
intrinsics_gen

MLIR_DEPS
MLIRIR

MLIR_LIBS
${dialect_libs}
)
4 changes: 3 additions & 1 deletion flang/lib/Optimizer/HLFIR/IR/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ add_flang_library(HLFIRDialect
CUFAttrsIncGen
FIRDialect
HLFIROpsIncGen
${dialect_libs}

LINK_LIBS
CUFAttrs
Expand All @@ -19,6 +18,9 @@ add_flang_library(HLFIRDialect
AsmPrinter
Remarks

MLIR_DEPS
${dialect_libs}

MLIR_LIBS
MLIRIR
${dialect_libs}
Expand Down
4 changes: 3 additions & 1 deletion flang/lib/Optimizer/HLFIR/Transforms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ add_flang_library(HLFIRTransforms
CUFAttrsIncGen
FIRDialect
HLFIROpsIncGen
${dialect_libs}

LINK_LIBS
CUFAttrs
Expand All @@ -33,6 +32,9 @@ add_flang_library(HLFIRTransforms
AsmPrinter
Remarks

MLIR_DEPS
${dialect_libs}

MLIR_LIBS
MLIRIR
${dialect_libs}
Expand Down
4 changes: 3 additions & 1 deletion flang/lib/Optimizer/OpenACC/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ add_flang_library(FIROpenACCSupport
FIRDialectSupport
FIRSupport
HLFIRDialect
MLIROpenACCDialect

LINK_LIBS
FIRBuilder
Expand All @@ -21,6 +20,9 @@ add_flang_library(FIROpenACCSupport
FIRSupport
HLFIRDialect

MLIR_DEPS
MLIROpenACCDialect

MLIR_LIBS
MLIROpenACCDialect
)
4 changes: 3 additions & 1 deletion flang/lib/Optimizer/OpenMP/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ add_flang_library(FlangOpenMPTransforms
FIRDialect
HLFIROpsIncGen
FlangOpenMPPassesIncGen
${dialect_libs}

LINK_LIBS
FIRAnalysis
Expand All @@ -24,6 +23,9 @@ add_flang_library(FlangOpenMPTransforms
FortranSupport
HLFIRDialect

MLIR_DEPS
${dialect_libs}

MLIR_LIBS
MLIRFuncDialect
MLIROpenMPDialect
Expand Down
8 changes: 5 additions & 3 deletions flang/lib/Optimizer/Support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ add_flang_library(FIRSupport
DEPENDS
FIROpsIncGen
HLFIROpsIncGen
MLIRIR
${dialect_libs}
${extension_libs}

LINK_LIBS
FIRDialect

LINK_COMPONENTS
TargetParser

MLIR_DEPS
MLIRIR
${dialect_libs}
${extension_libs}

MLIR_LIBS
${dialect_libs}
${extension_libs}
Expand Down
22 changes: 13 additions & 9 deletions flang/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,30 @@ set(FLANG_TEST_PARAMS

set(FLANG_TEST_DEPENDS
flang
llvm-config
FileCheck
count
not
module_files
fir-opt
tco
bbc
llvm-dis
llvm-objdump
llvm-readobj
split-file
FortranDecimal
)
if (NOT FLANG_STANDALONE_BUILD)
list(APPEND FLANG_TEST_DEPENDS
llvm-config
FileCheck
count
not
llvm-dis
llvm-objdump
llvm-readobj
split-file
)
endif ()

if (FLANG_INCLUDE_RUNTIME)
list(APPEND FLANG_TEST_DEPENDS flang_rt.runtime)
endif ()

if (LLVM_ENABLE_PLUGINS AND NOT WIN32)
if (LLVM_ENABLE_PLUGINS AND NOT WIN32 AND NOT FLANG_STANDALONE_BUILD)
list(APPEND FLANG_TEST_DEPENDS Bye)
endif()

Expand Down
4 changes: 3 additions & 1 deletion flang/test/lib/Analysis/AliasAnalysis/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ add_flang_library(FIRTestAnalysis
FIRSupport
FIRTransforms
FIRAnalysis
${dialect_libs}

LINK_LIBS
FIRDialect
Expand All @@ -18,6 +17,9 @@ add_flang_library(FIRTestAnalysis
FIRAnalysis
MLIRTestAnalysis

MLIR_DEPS
${dialect_libs}

MLIR_LIBS
${dialect_libs}
MLIRFuncDialect
Expand Down
10 changes: 6 additions & 4 deletions flang/test/lib/OpenACC/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ add_flang_library(FIRTestOpenACCInterfaces
FIRDialect
FIROpenACCSupport
FIRSupport
MLIRIR
MLIROpenACCDialect
MLIRPass
MLIRSupport

LINK_LIBS
FIRDialect
FIROpenACCSupport
FIRSupport

MLIR_DEPS
MLIRIR
MLIROpenACCDialect
MLIRPass
MLIRSupport

MLIR_LIBS
MLIRIR
MLIROpenACCDialect
Expand Down
13 changes: 10 additions & 3 deletions flang/unittests/Optimizer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
get_property(extension_libs GLOBAL PROPERTY MLIR_EXTENSION_LIBS)

set(LLVM_LINK_COMPONENTS
TargetParser
)

set(LIBS
CUFDialect
FIRBuilder
Expand All @@ -9,7 +13,6 @@ set(LIBS
FIRDialectSupport
FIRSupport
HLFIRDialect
LLVMTargetParser
)

add_flang_unittest(FlangOptimizerTests
Expand Down Expand Up @@ -39,8 +42,12 @@ DEPENDS
CUFDialect
FIRDialect
FIRSupport
HLFIRDialect
${dialect_libs})
HLFIRDialect)

if(NOT FLANG_STANDALONE_BUILD)
add_dependencies(FlangOptimizerTests
${dialect_libs})
endif()

target_link_libraries(FlangOptimizerTests
PRIVATE
Expand Down

0 comments on commit c30a7f4

Please sign in to comment.