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

Michał Górny via flang-commits flang-commits at lists.llvm.org
Mon Dec 23 05:49:45 PST 2024


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

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.

>From 07edba3aa563c70d4a2fcf904a51148811dba22b 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                         | 2 ++
 flang/lib/FrontendTool/CMakeLists.txt                     | 2 ++
 flang/lib/Lower/CMakeLists.txt                            | 6 ++++--
 flang/lib/Optimizer/Analysis/CMakeLists.txt               | 2 ++
 flang/lib/Optimizer/Builder/CMakeLists.txt                | 2 ++
 flang/lib/Optimizer/CodeGen/CMakeLists.txt                | 2 ++
 flang/lib/Optimizer/Dialect/CMakeLists.txt                | 2 ++
 flang/lib/Optimizer/Dialect/CUF/Attributes/CMakeLists.txt | 2 +-
 flang/lib/Optimizer/Dialect/CUF/CMakeLists.txt            | 2 ++
 flang/lib/Optimizer/Dialect/Support/CMakeLists.txt        | 2 +-
 flang/lib/Optimizer/HLFIR/IR/CMakeLists.txt               | 2 ++
 flang/lib/Optimizer/HLFIR/Transforms/CMakeLists.txt       | 2 ++
 flang/lib/Optimizer/OpenMP/CMakeLists.txt                 | 4 +++-
 flang/lib/Optimizer/Passes/CMakeLists.txt                 | 6 ++++--
 flang/lib/Optimizer/Support/CMakeLists.txt                | 2 ++
 flang/lib/Optimizer/Transforms/CMakeLists.txt             | 2 ++
 flang/test/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, 58 insertions(+), 12 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..f4452fc8895caf 100644
--- a/flang/lib/Frontend/CMakeLists.txt
+++ b/flang/lib/Frontend/CMakeLists.txt
@@ -39,6 +39,8 @@ add_flang_library(flangFrontend
   HLFIRTransforms
   flangPasses
   FlangOpenMPTransforms
+
+  MLIR_LIBS
   MLIRTransforms
   MLIRBuiltinToLLVMIRTranslation
   MLIRLLVMToLLVMIRTranslation
diff --git a/flang/lib/FrontendTool/CMakeLists.txt b/flang/lib/FrontendTool/CMakeLists.txt
index 2acaffbe50380a..d37d2a2677df36 100644
--- a/flang/lib/FrontendTool/CMakeLists.txt
+++ b/flang/lib/FrontendTool/CMakeLists.txt
@@ -8,6 +8,8 @@ add_flang_library(flangFrontendTool
 
   LINK_LIBS
   flangFrontend
+
+  MLIR_LIBS
   MLIRPass
 
   LINK_COMPONENTS
diff --git a/flang/lib/Lower/CMakeLists.txt b/flang/lib/Lower/CMakeLists.txt
index ba6622d8504a4f..ae4f2bd91025d6 100644
--- a/flang/lib/Lower/CMakeLists.txt
+++ b/flang/lib/Lower/CMakeLists.txt
@@ -54,12 +54,14 @@ add_flang_library(FortranLower
   FIRSupport
   FIRTransforms
   HLFIRDialect
-  ${dialect_libs}
-  ${extension_libs}
   FortranCommon
   FortranParser
   FortranEvaluate
   FortranSemantics
+
+  MLIR_LIBS
+  ${dialect_libs}
+  ${extension_libs}
   MLIRAffineToStandard
   MLIRFuncDialect
   MLIRLLVMDialect
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..5e8ff0983cb3c9 100644
--- a/flang/lib/Optimizer/CodeGen/CMakeLists.txt
+++ b/flang/lib/Optimizer/CodeGen/CMakeLists.txt
@@ -21,6 +21,8 @@ add_flang_library(FIRCodeGen
   FIRDialect
   FIRDialectSupport
   FIRSupport
+
+  MLIR_LIBS
   MLIRComplexToLLVM
   MLIRComplexToStandard
   MLIRGPUDialect
diff --git a/flang/lib/Optimizer/Dialect/CMakeLists.txt b/flang/lib/Optimizer/Dialect/CMakeLists.txt
index a8235f841b879d..a7adb09fcba9bb 100644
--- a/flang/lib/Optimizer/Dialect/CMakeLists.txt
+++ b/flang/lib/Optimizer/Dialect/CMakeLists.txt
@@ -20,6 +20,8 @@ add_flang_library(FIRDialect
   LINK_LIBS
   CUFAttrs
   FIRDialectSupport
+
+  MLIR_LIBS
   MLIRArithDialect
   MLIRBuiltinToLLVMIRTranslation
   MLIROpenMPToLLVM
diff --git a/flang/lib/Optimizer/Dialect/CUF/Attributes/CMakeLists.txt b/flang/lib/Optimizer/Dialect/CUF/Attributes/CMakeLists.txt
index ec5484c1d61080..559aa09073494a 100644
--- a/flang/lib/Optimizer/Dialect/CUF/Attributes/CMakeLists.txt
+++ b/flang/lib/Optimizer/Dialect/CUF/Attributes/CMakeLists.txt
@@ -7,7 +7,7 @@ add_flang_library(CUFAttrs
   CUFAttrsIncGen
   CUFOpsIncGen
 
-  LINK_LIBS
+  MLIR_LIBS
   MLIRTargetLLVMIRExport
 
   LINK_COMPONENTS
diff --git a/flang/lib/Optimizer/Dialect/CUF/CMakeLists.txt b/flang/lib/Optimizer/Dialect/CUF/CMakeLists.txt
index 5d4bd0785971f7..c7d4857644b2fa 100644
--- a/flang/lib/Optimizer/Dialect/CUF/CMakeLists.txt
+++ b/flang/lib/Optimizer/Dialect/CUF/CMakeLists.txt
@@ -14,6 +14,8 @@ add_flang_library(CUFDialect
   CUFAttrs
   FIRDialect
   FIRDialectSupport
+
+  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..acda2478ecf069 100644
--- a/flang/lib/Optimizer/HLFIR/IR/CMakeLists.txt
+++ b/flang/lib/Optimizer/HLFIR/IR/CMakeLists.txt
@@ -13,6 +13,8 @@ add_flang_library(HLFIRDialect
   LINK_LIBS
   CUFAttrs
   FIRDialect
+
+  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..d8ea200e8c6a64 100644
--- a/flang/lib/Optimizer/HLFIR/Transforms/CMakeLists.txt
+++ b/flang/lib/Optimizer/HLFIR/Transforms/CMakeLists.txt
@@ -26,6 +26,8 @@ add_flang_library(HLFIRTransforms
   FIRTransforms
   FlangOpenMPTransforms
   HLFIRDialect
+
+  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..9fb67ef16b0ccb 100644
--- a/flang/lib/Optimizer/Passes/CMakeLists.txt
+++ b/flang/lib/Optimizer/Passes/CMakeLists.txt
@@ -12,10 +12,12 @@ add_flang_library(flangPasses
   FIRCodeGen
   FIRTransforms
   FlangOpenMPTransforms
-  ${dialect_libs}
-  ${extension_libs}
   FortranCommon
   HLFIRTransforms
+
+  MLIR_LIBS
+  ${dialect_libs}
+  ${extension_libs}
   MLIRPass
   MLIRReconcileUnrealizedCasts
   MLIRSCFToControlFlow
diff --git a/flang/lib/Optimizer/Support/CMakeLists.txt b/flang/lib/Optimizer/Support/CMakeLists.txt
index 8794c247124173..e1b57583e82af0 100644
--- a/flang/lib/Optimizer/Support/CMakeLists.txt
+++ b/flang/lib/Optimizer/Support/CMakeLists.txt
@@ -16,6 +16,8 @@ add_flang_library(FIRSupport
 
   LINK_LIBS
   FIRDialect
+
+  MLIR_LIBS
   ${dialect_libs}
   ${extension_libs}
   MLIRBuiltinToLLVMIRTranslation
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