[flang-commits] [flang] [flang] Support linking to MLIR dylib (PR #120966)

Michał Górny via flang-commits flang-commits at lists.llvm.org
Wed Dec 25 00:23:07 PST 2024


https://github.com/mgorny updated https://github.com/llvm/llvm-project/pull/120966

>From d6f27160f6420d465c529b5210f968fc04204427 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= <mgorny at gentoo.org>
Date: Mon, 23 Dec 2024 14:41:17 +0100
Subject: [PATCH] [flang] Support linking to MLIR dylib

Introduce a new `MLIR_LIBS` argument to `add_flang_library`, that uses
`mlir_target_link_libraries` to link the MLIR dylib alterantively
to the component libraries.  Use it, along with a few inline
`mlir_target_link_libraries` in tools, to support linking Flang to MLIR
dylib rather than the static libraries.

With these changes, the vast majority of Flang can be linked
dynamically.  The only parts still using static libraries are these
requiring MLIR test libraries, that are not included in the dylib.
---
 flang/cmake/modules/AddFlang.cmake               |  3 ++-
 flang/lib/Common/CMakeLists.txt                  |  2 +-
 flang/lib/Frontend/CMakeLists.txt                | 16 +++++++++-------
 flang/lib/FrontendTool/CMakeLists.txt            |  4 +++-
 flang/lib/Lower/CMakeLists.txt                   | 12 +++++++-----
 flang/lib/Optimizer/Analysis/CMakeLists.txt      |  2 ++
 flang/lib/Optimizer/Builder/CMakeLists.txt       |  2 ++
 flang/lib/Optimizer/CodeGen/CMakeLists.txt       | 14 ++++++++------
 flang/lib/Optimizer/Dialect/CMakeLists.txt       | 12 +++++++-----
 .../Dialect/CUF/Attributes/CMakeLists.txt        |  6 +++---
 flang/lib/Optimizer/Dialect/CUF/CMakeLists.txt   |  8 +++++---
 .../lib/Optimizer/Dialect/Support/CMakeLists.txt |  2 +-
 flang/lib/Optimizer/HLFIR/IR/CMakeLists.txt      |  6 ++++--
 .../Optimizer/HLFIR/Transforms/CMakeLists.txt    |  6 ++++--
 flang/lib/Optimizer/OpenMP/CMakeLists.txt        |  4 +++-
 flang/lib/Optimizer/Passes/CMakeLists.txt        | 12 +++++++-----
 flang/lib/Optimizer/Support/CMakeLists.txt       |  8 +++++---
 flang/lib/Optimizer/Transforms/CMakeLists.txt    |  2 ++
 .../lib/Analysis/AliasAnalysis/CMakeLists.txt    |  6 ++++--
 flang/tools/bbc/CMakeLists.txt                   |  3 +++
 flang/tools/fir-lsp-server/CMakeLists.txt        |  4 +++-
 flang/tools/fir-opt/CMakeLists.txt               |  3 +++
 flang/tools/tco/CMakeLists.txt                   |  3 +++
 flang/unittests/Frontend/CMakeLists.txt          |  4 ++++
 24 files changed, 95 insertions(+), 49 deletions(-)

diff --git a/flang/cmake/modules/AddFlang.cmake b/flang/cmake/modules/AddFlang.cmake
index 9ed1a3050b7e86..b2fcc5e13cf74f 100644
--- a/flang/cmake/modules/AddFlang.cmake
+++ b/flang/cmake/modules/AddFlang.cmake
@@ -18,7 +18,7 @@ endmacro()
 
 function(add_flang_library name)
   set(options SHARED STATIC INSTALL_WITH_TOOLCHAIN)
-  set(multiValueArgs ADDITIONAL_HEADERS CLANG_LIBS)
+  set(multiValueArgs ADDITIONAL_HEADERS CLANG_LIBS MLIR_LIBS)
   cmake_parse_arguments(ARG
     "${options}"
     ""
@@ -66,6 +66,7 @@ function(add_flang_library name)
   llvm_add_library(${name} ${LIBTYPE} ${ARG_UNPARSED_ARGUMENTS} ${srcs})
 
   clang_target_link_libraries(${name} PRIVATE ${ARG_CLANG_LIBS})
+  mlir_target_link_libraries(${name} PRIVATE ${ARG_MLIR_LIBS})
 
   if (TARGET ${name})
 
diff --git a/flang/lib/Common/CMakeLists.txt b/flang/lib/Common/CMakeLists.txt
index de6bea396f3cbe..4b5df0a49f4031 100644
--- a/flang/lib/Common/CMakeLists.txt
+++ b/flang/lib/Common/CMakeLists.txt
@@ -47,6 +47,6 @@ add_flang_library(FortranCommon
   LINK_COMPONENTS
   Support
 
-  LINK_LIBS
+  MLIR_LIBS
   MLIRIR
 )
diff --git a/flang/lib/Frontend/CMakeLists.txt b/flang/lib/Frontend/CMakeLists.txt
index e954800c3b88b0..dd5aeca6e89e75 100644
--- a/flang/lib/Frontend/CMakeLists.txt
+++ b/flang/lib/Frontend/CMakeLists.txt
@@ -39,13 +39,6 @@ add_flang_library(flangFrontend
   HLFIRTransforms
   flangPasses
   FlangOpenMPTransforms
-  MLIRTransforms
-  MLIRBuiltinToLLVMIRTranslation
-  MLIRLLVMToLLVMIRTranslation
-  MLIRSCFToControlFlow
-  MLIRTargetLLVMIRImport
-  ${dialect_libs}
-  ${extension_libs}
 
   LINK_COMPONENTS
   Passes
@@ -61,6 +54,15 @@ add_flang_library(flangFrontend
   FrontendOpenACC
   FrontendOpenMP
 
+  MLIR_LIBS
+  MLIRTransforms
+  MLIRBuiltinToLLVMIRTranslation
+  MLIRLLVMToLLVMIRTranslation
+  MLIRSCFToControlFlow
+  MLIRTargetLLVMIRImport
+  ${dialect_libs}
+  ${extension_libs}
+
   CLANG_LIBS
   clangBasic
   clangDriver
diff --git a/flang/lib/FrontendTool/CMakeLists.txt b/flang/lib/FrontendTool/CMakeLists.txt
index 2acaffbe50380a..faf56e9d955a13 100644
--- a/flang/lib/FrontendTool/CMakeLists.txt
+++ b/flang/lib/FrontendTool/CMakeLists.txt
@@ -8,12 +8,14 @@ add_flang_library(flangFrontendTool
 
   LINK_LIBS
   flangFrontend
-  MLIRPass
 
   LINK_COMPONENTS
   Option
   Support
 
+  MLIR_LIBS
+  MLIRPass
+
   CLANG_LIBS
   clangBasic
   clangDriver
diff --git a/flang/lib/Lower/CMakeLists.txt b/flang/lib/Lower/CMakeLists.txt
index ba6622d8504a4f..123444c98f8b58 100644
--- a/flang/lib/Lower/CMakeLists.txt
+++ b/flang/lib/Lower/CMakeLists.txt
@@ -54,17 +54,19 @@ add_flang_library(FortranLower
   FIRSupport
   FIRTransforms
   HLFIRDialect
-  ${dialect_libs}
-  ${extension_libs}
   FortranCommon
   FortranParser
   FortranEvaluate
   FortranSemantics
+
+  LINK_COMPONENTS
+  Support
+
+  MLIR_LIBS
+  ${dialect_libs}
+  ${extension_libs}
   MLIRAffineToStandard
   MLIRFuncDialect
   MLIRLLVMDialect
   MLIRSCFToControlFlow
-
-  LINK_COMPONENTS
-  Support
 )
diff --git a/flang/lib/Optimizer/Analysis/CMakeLists.txt b/flang/lib/Optimizer/Analysis/CMakeLists.txt
index 1358219fd98d52..6fe9c70f83765f 100644
--- a/flang/lib/Optimizer/Analysis/CMakeLists.txt
+++ b/flang/lib/Optimizer/Analysis/CMakeLists.txt
@@ -13,6 +13,8 @@ add_flang_library(FIRAnalysis
   FIRBuilder
   FIRDialect
   HLFIRDialect
+
+  MLIR_LIBS
   MLIRFuncDialect
   MLIRLLVMDialect
   MLIRMathTransforms
diff --git a/flang/lib/Optimizer/Builder/CMakeLists.txt b/flang/lib/Optimizer/Builder/CMakeLists.txt
index 05164d41a4cb55..edfc483055e540 100644
--- a/flang/lib/Optimizer/Builder/CMakeLists.txt
+++ b/flang/lib/Optimizer/Builder/CMakeLists.txt
@@ -49,6 +49,8 @@ add_flang_library(FIRBuilder
   FIRDialectSupport
   FIRSupport
   HLFIRDialect
+
+  MLIR_LIBS
   ${dialect_libs}
   ${extension_libs}
 )
diff --git a/flang/lib/Optimizer/CodeGen/CMakeLists.txt b/flang/lib/Optimizer/CodeGen/CMakeLists.txt
index f47d11875f04db..81c8a68b953672 100644
--- a/flang/lib/Optimizer/CodeGen/CMakeLists.txt
+++ b/flang/lib/Optimizer/CodeGen/CMakeLists.txt
@@ -21,6 +21,14 @@ add_flang_library(FIRCodeGen
   FIRDialect
   FIRDialectSupport
   FIRSupport
+
+  LINK_COMPONENTS
+  AsmParser
+  AsmPrinter
+  Remarks
+  TargetParser
+
+  MLIR_LIBS
   MLIRComplexToLLVM
   MLIRComplexToStandard
   MLIRGPUDialect
@@ -34,10 +42,4 @@ add_flang_library(FIRCodeGen
   MLIRLLVMToLLVMIRTranslation
   MLIRTargetLLVMIRExport
   MLIRVectorToLLVM
-
-  LINK_COMPONENTS
-  AsmParser
-  AsmPrinter
-  Remarks
-  TargetParser
 )
diff --git a/flang/lib/Optimizer/Dialect/CMakeLists.txt b/flang/lib/Optimizer/Dialect/CMakeLists.txt
index a8235f841b879d..bd5abf5d3f9639 100644
--- a/flang/lib/Optimizer/Dialect/CMakeLists.txt
+++ b/flang/lib/Optimizer/Dialect/CMakeLists.txt
@@ -20,14 +20,16 @@ add_flang_library(FIRDialect
   LINK_LIBS
   CUFAttrs
   FIRDialectSupport
-  MLIRArithDialect
-  MLIRBuiltinToLLVMIRTranslation
-  MLIROpenMPToLLVM
-  MLIRLLVMToLLVMIRTranslation
-  MLIRTargetLLVMIRExport
 
   LINK_COMPONENTS
   AsmParser
   AsmPrinter
   Remarks
+
+  MLIR_LIBS
+  MLIRArithDialect
+  MLIRBuiltinToLLVMIRTranslation
+  MLIROpenMPToLLVM
+  MLIRLLVMToLLVMIRTranslation
+  MLIRTargetLLVMIRExport
 )
diff --git a/flang/lib/Optimizer/Dialect/CUF/Attributes/CMakeLists.txt b/flang/lib/Optimizer/Dialect/CUF/Attributes/CMakeLists.txt
index ec5484c1d61080..a0f58504eff05d 100644
--- a/flang/lib/Optimizer/Dialect/CUF/Attributes/CMakeLists.txt
+++ b/flang/lib/Optimizer/Dialect/CUF/Attributes/CMakeLists.txt
@@ -7,11 +7,11 @@ add_flang_library(CUFAttrs
   CUFAttrsIncGen
   CUFOpsIncGen
 
-  LINK_LIBS
-  MLIRTargetLLVMIRExport
-
   LINK_COMPONENTS
   AsmParser
   AsmPrinter
   Remarks
+
+  MLIR_LIBS
+  MLIRTargetLLVMIRExport
 )
diff --git a/flang/lib/Optimizer/Dialect/CUF/CMakeLists.txt b/flang/lib/Optimizer/Dialect/CUF/CMakeLists.txt
index 5d4bd0785971f7..e483b4a1641130 100644
--- a/flang/lib/Optimizer/Dialect/CUF/CMakeLists.txt
+++ b/flang/lib/Optimizer/Dialect/CUF/CMakeLists.txt
@@ -14,12 +14,14 @@ add_flang_library(CUFDialect
   CUFAttrs
   FIRDialect
   FIRDialectSupport
-  MLIRIR
-  MLIRGPUDialect
-  MLIRTargetLLVMIRExport
 
   LINK_COMPONENTS
   AsmParser
   AsmPrinter
   Remarks
+
+  MLIR_LIBS
+  MLIRIR
+  MLIRGPUDialect
+  MLIRTargetLLVMIRExport
 )
diff --git a/flang/lib/Optimizer/Dialect/Support/CMakeLists.txt b/flang/lib/Optimizer/Dialect/Support/CMakeLists.txt
index c37b0549f7fc18..bfdd5279b6f293 100644
--- a/flang/lib/Optimizer/Dialect/Support/CMakeLists.txt
+++ b/flang/lib/Optimizer/Dialect/Support/CMakeLists.txt
@@ -8,6 +8,6 @@ add_flang_library(FIRDialectSupport
   MLIRIR
   intrinsics_gen
 
-  LINK_LIBS
+  MLIR_LIBS
   ${dialect_libs}
 )
diff --git a/flang/lib/Optimizer/HLFIR/IR/CMakeLists.txt b/flang/lib/Optimizer/HLFIR/IR/CMakeLists.txt
index 267d6469ee7abb..8a646bedf94b84 100644
--- a/flang/lib/Optimizer/HLFIR/IR/CMakeLists.txt
+++ b/flang/lib/Optimizer/HLFIR/IR/CMakeLists.txt
@@ -13,11 +13,13 @@ add_flang_library(HLFIRDialect
   LINK_LIBS
   CUFAttrs
   FIRDialect
-  MLIRIR
-  ${dialect_libs}
 
   LINK_COMPONENTS
   AsmParser
   AsmPrinter
   Remarks
+
+  MLIR_LIBS
+  MLIRIR
+  ${dialect_libs}
 )
diff --git a/flang/lib/Optimizer/HLFIR/Transforms/CMakeLists.txt b/flang/lib/Optimizer/HLFIR/Transforms/CMakeLists.txt
index d18df2ef49f10e..7d5ffc809d3978 100644
--- a/flang/lib/Optimizer/HLFIR/Transforms/CMakeLists.txt
+++ b/flang/lib/Optimizer/HLFIR/Transforms/CMakeLists.txt
@@ -26,11 +26,13 @@ add_flang_library(HLFIRTransforms
   FIRTransforms
   FlangOpenMPTransforms
   HLFIRDialect
-  MLIRIR
-  ${dialect_libs}
 
   LINK_COMPONENTS
   AsmParser
   AsmPrinter
   Remarks
+
+  MLIR_LIBS
+  MLIRIR
+  ${dialect_libs}
 )
diff --git a/flang/lib/Optimizer/OpenMP/CMakeLists.txt b/flang/lib/Optimizer/OpenMP/CMakeLists.txt
index 4f23b2b970fa44..c8509ccc7fea5a 100644
--- a/flang/lib/Optimizer/OpenMP/CMakeLists.txt
+++ b/flang/lib/Optimizer/OpenMP/CMakeLists.txt
@@ -22,9 +22,11 @@ add_flang_library(FlangOpenMPTransforms
   FIRDialectSupport
   FIRSupport
   FortranCommon
+  HLFIRDialect
+
+  MLIR_LIBS
   MLIRFuncDialect
   MLIROpenMPDialect
-  HLFIRDialect
   MLIRIR
   MLIRPass
   MLIRTransformUtils
diff --git a/flang/lib/Optimizer/Passes/CMakeLists.txt b/flang/lib/Optimizer/Passes/CMakeLists.txt
index 40abbdfbdd6511..eb25beba309bf6 100644
--- a/flang/lib/Optimizer/Passes/CMakeLists.txt
+++ b/flang/lib/Optimizer/Passes/CMakeLists.txt
@@ -12,16 +12,18 @@ add_flang_library(flangPasses
   FIRCodeGen
   FIRTransforms
   FlangOpenMPTransforms
-  ${dialect_libs}
-  ${extension_libs}
   FortranCommon
   HLFIRTransforms
+
+  LINK_COMPONENTS
+  Passes
+
+  MLIR_LIBS
+  ${dialect_libs}
+  ${extension_libs}
   MLIRPass
   MLIRReconcileUnrealizedCasts
   MLIRSCFToControlFlow
   MLIRSupport
   MLIRTransforms
-
-  LINK_COMPONENTS
-  Passes
 )
diff --git a/flang/lib/Optimizer/Support/CMakeLists.txt b/flang/lib/Optimizer/Support/CMakeLists.txt
index 8794c247124173..f8e4fc5bcefea4 100644
--- a/flang/lib/Optimizer/Support/CMakeLists.txt
+++ b/flang/lib/Optimizer/Support/CMakeLists.txt
@@ -16,6 +16,11 @@ add_flang_library(FIRSupport
 
   LINK_LIBS
   FIRDialect
+
+  LINK_COMPONENTS
+  TargetParser
+
+  MLIR_LIBS
   ${dialect_libs}
   ${extension_libs}
   MLIRBuiltinToLLVMIRTranslation
@@ -24,7 +29,4 @@ add_flang_library(FIRSupport
   MLIRLLVMToLLVMIRTranslation
   MLIRTargetLLVMIRExport
   MLIRTargetLLVMIRImport
-
-  LINK_COMPONENTS
-  TargetParser
 )
diff --git a/flang/lib/Optimizer/Transforms/CMakeLists.txt b/flang/lib/Optimizer/Transforms/CMakeLists.txt
index 9eafa4ec234bdd..c6ba19fda4120a 100644
--- a/flang/lib/Optimizer/Transforms/CMakeLists.txt
+++ b/flang/lib/Optimizer/Transforms/CMakeLists.txt
@@ -49,6 +49,8 @@ add_flang_library(FIRTransforms
   FIRSupport
   FortranCommon
   HLFIRDialect
+
+  MLIR_LIBS
   MLIRAffineUtils
   MLIRFuncDialect
   MLIRGPUDialect
diff --git a/flang/test/lib/Analysis/AliasAnalysis/CMakeLists.txt b/flang/test/lib/Analysis/AliasAnalysis/CMakeLists.txt
index c4b3838c9a23e0..cba47a41145171 100644
--- a/flang/test/lib/Analysis/AliasAnalysis/CMakeLists.txt
+++ b/flang/test/lib/Analysis/AliasAnalysis/CMakeLists.txt
@@ -16,14 +16,16 @@ add_flang_library(FIRTestAnalysis
   FIRSupport
   FIRTransforms
   FIRAnalysis
+  MLIRTestAnalysis
+
+  MLIR_LIBS
   ${dialect_libs}
   MLIRFuncDialect
   MLIRLLVMDialect
   MLIRAnalysis
-  MLIRTestAnalysis
   )
 
 target_include_directories(FIRTestAnalysis
   PRIVATE
   ${MLIR_MAIN_SRC_DIR}/..
-  )
\ No newline at end of file
+  )
diff --git a/flang/tools/bbc/CMakeLists.txt b/flang/tools/bbc/CMakeLists.txt
index 9abdcf17b6ebfd..aa4b7e055eeeeb 100644
--- a/flang/tools/bbc/CMakeLists.txt
+++ b/flang/tools/bbc/CMakeLists.txt
@@ -28,6 +28,9 @@ target_link_libraries(bbc PRIVATE
   flangFrontend
   flangPasses
   FlangOpenMPTransforms
+)
+
+mlir_target_link_libraries(bbc PRIVATE
   ${dialect_libs}
   ${extension_libs}
   MLIRAffineToStandard
diff --git a/flang/tools/fir-lsp-server/CMakeLists.txt b/flang/tools/fir-lsp-server/CMakeLists.txt
index ff0ced6693b97f..5a082e13a2826b 100644
--- a/flang/tools/fir-lsp-server/CMakeLists.txt
+++ b/flang/tools/fir-lsp-server/CMakeLists.txt
@@ -11,7 +11,9 @@ get_property(extension_libs GLOBAL PROPERTY MLIR_EXTENSION_LIBS)
 target_link_libraries(fir-lsp-server PRIVATE
   CUFDialect
   FIRDialect
-  HLFIRDialect
+  HLFIRDialect)
+
+mlir_target_link_libraries(fir-lsp-server PRIVATE
   MLIRLspServerLib
   ${dialect_libs}
   ${extension_libs})
diff --git a/flang/tools/fir-opt/CMakeLists.txt b/flang/tools/fir-opt/CMakeLists.txt
index 4c6dbf7d9c8c37..9a7ce946495826 100644
--- a/flang/tools/fir-opt/CMakeLists.txt
+++ b/flang/tools/fir-opt/CMakeLists.txt
@@ -22,6 +22,9 @@ target_link_libraries(fir-opt PRIVATE
   FlangOpenMPTransforms
   FIRAnalysis
   ${test_libs}
+)
+
+mlir_target_link_libraries(fir-opt PRIVATE
   ${dialect_libs}
   ${extension_libs}
 
diff --git a/flang/tools/tco/CMakeLists.txt b/flang/tools/tco/CMakeLists.txt
index aac80437ee11d9..ae36dbe5ca3b89 100644
--- a/flang/tools/tco/CMakeLists.txt
+++ b/flang/tools/tco/CMakeLists.txt
@@ -20,6 +20,9 @@ target_link_libraries(tco PRIVATE
   flangPasses
   FlangOpenMPTransforms
   FortranCommon
+)
+
+mlir_target_link_libraries(tco PRIVATE
   ${dialect_libs}
   ${extension_libs}
   MLIRIR
diff --git a/flang/unittests/Frontend/CMakeLists.txt b/flang/unittests/Frontend/CMakeLists.txt
index 22c568af3d121a..9177997f41f53a 100644
--- a/flang/unittests/Frontend/CMakeLists.txt
+++ b/flang/unittests/Frontend/CMakeLists.txt
@@ -20,5 +20,9 @@ target_link_libraries(FlangFrontendTests
   FortranSemantics
   FortranCommon
   FortranEvaluate
+)
+
+mlir_target_link_libraries(FlangFrontendTests
+  PRIVATE
   MLIRIR
 )



More information about the flang-commits mailing list