[llvm] [llvm] get Linux `-fvisibility=hidden` shared library build working with GCC (PR #151365)

Andrew Rogers via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 5 07:41:11 PDT 2025


https://github.com/andrurogerz updated https://github.com/llvm/llvm-project/pull/151365

>From 72b4d5540d42af70adf082ee88878bb36d7a5dab Mon Sep 17 00:00:00 2001
From: Andrew Rogers <andrurogerz at gmail.com>
Date: Tue, 29 Jul 2025 12:11:58 -0700
Subject: [PATCH 1/2] missing LLVM_TEMPLATE_ABI annotations

---
 llvm/include/llvm/CodeGen/MachineLoopInfo.h | 3 ++-
 llvm/include/llvm/IR/PassManager.h          | 2 +-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/MachineLoopInfo.h b/llvm/include/llvm/CodeGen/MachineLoopInfo.h
index 6942264a11c0e..bcec6df39e73c 100644
--- a/llvm/include/llvm/CodeGen/MachineLoopInfo.h
+++ b/llvm/include/llvm/CodeGen/MachineLoopInfo.h
@@ -42,7 +42,8 @@ namespace llvm {
 class MachineDominatorTree;
 // Implementation in LoopInfoImpl.h
 class MachineLoop;
-extern template class LoopBase<MachineBasicBlock, MachineLoop>;
+extern template class LLVM_TEMPLATE_ABI
+    LoopBase<MachineBasicBlock, MachineLoop>;
 
 class MachineLoop : public LoopBase<MachineBasicBlock, MachineLoop> {
 public:
diff --git a/llvm/include/llvm/IR/PassManager.h b/llvm/include/llvm/IR/PassManager.h
index ea8226c6e17ba..cd671439351d5 100644
--- a/llvm/include/llvm/IR/PassManager.h
+++ b/llvm/include/llvm/IR/PassManager.h
@@ -658,7 +658,7 @@ class LLVM_TEMPLATE_ABI InnerAnalysisManagerProxy
 };
 
 template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs>
-AnalysisKey
+LLVM_TEMPLATE_ABI AnalysisKey
     InnerAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>::Key;
 
 /// Provide the \c FunctionAnalysisManager to \c Module proxy.

>From c8c96dd67376b8bdb3c093ba517587b652ac753d Mon Sep 17 00:00:00 2001
From: Andrew Rogers <andrurogerz at gmail.com>
Date: Tue, 29 Jul 2025 12:53:11 -0700
Subject: [PATCH 2/2] fix Windows DLL build

---
 llvm/include/llvm/IR/PassManager.h | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm/IR/PassManager.h b/llvm/include/llvm/IR/PassManager.h
index cd671439351d5..f6cf1795a7a40 100644
--- a/llvm/include/llvm/IR/PassManager.h
+++ b/llvm/include/llvm/IR/PassManager.h
@@ -657,8 +657,15 @@ class LLVM_TEMPLATE_ABI InnerAnalysisManagerProxy
   AnalysisManagerT *InnerAM;
 };
 
+// NOTE: The LLVM_ABI annotation cannot be used here because MSVC disallows
+// storage-class specifiers on class members outside of the class declaration
+// (C2720). LLVM_ATTRIBUTE_VISIBILITY_DEFAULT only applies to non-Windows
+// targets so it is used instead. Without this annotation, compiling LLVM as a
+// shared library with -fvisibility=hidden using GCC fails to export the symbol
+// even though InnerAnalysisManagerProxy is already annotated with LLVM_ABI.
 template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs>
-LLVM_TEMPLATE_ABI AnalysisKey
+LLVM_ATTRIBUTE_VISIBILITY_DEFAULT
+AnalysisKey
     InnerAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>::Key;
 
 /// Provide the \c FunctionAnalysisManager to \c Module proxy.



More information about the llvm-commits mailing list