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

via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 5 23:11:12 PDT 2025


Author: Andrew Rogers
Date: 2025-08-05T23:11:09-07:00
New Revision: 8c9feb7e13b2c01fd1cd2ecc5f699ba5cc32e369

URL: https://github.com/llvm/llvm-project/commit/8c9feb7e13b2c01fd1cd2ecc5f699ba5cc32e369
DIFF: https://github.com/llvm/llvm-project/commit/8c9feb7e13b2c01fd1cd2ecc5f699ba5cc32e369.diff

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

## Purpose
Add missing annotations so that LLVM can build as a shared library on
Linux with `-fvisibility=hidden` using GCC.

## Overview
Add a couple of annotations that make GCC happy:
* Add `LLVM_TEMPLATE_ABI` to the `extern` declaration of
`LoopBase<MachineBasicBlock, MachineLoop>`. The instantiation of this of
this template is already properly annotated with `LLVM_EXPORT_TEMPLATE`
and this declaration was missed. This omission did not cause problems
with MSVC or Clang but results in undefined reference to the destructor
with GCC.
* Add `LLVM_ATTRIBUTE_VISIBILITY_DEFAULT` to the template declaration
for `InnerAnalysisManagerProxy::Key`. `LLVM_ABI` cannot be used here
because MSVC disallows storage-class specifiers on class members outside
of the class declaration (C2720). Since
`LLVM_ATTRIBUTE_VISIBILITY_DEFAULT` only applies to non-Windows targets,
it can be used in its place. Omitting this annotation does not cause
problems with Clang but results in undefined reference to
`InnerAnalysisManagerProxy::Key` fields for for explicitly instantiated
declarations when compiling with GCC.

## Background
This effort is tracked in #109483. Additional context is provided in
[this
discourse](https://discourse.llvm.org/t/psa-annotating-llvm-public-interface/85307),
and documentation for `LLVM_ABI` and related annotations is found in the
LLVM repo
[here](https://github.com/llvm/llvm-project/blob/main/llvm/docs/InterfaceExportAnnotations.rst).

## Validation
On Fedora 42, build with GCC `LLVM_BUILD_LLVM_DYLIB=ON`,
`LLVM_BUILD_LLVM_DYLIB_VIS=ON`, and `LLVM_LINK_LLVM_DYLIB=ON`.
```
cmake -B build -S llvm -G Ninja -DLLVM_ENABLE_PROJECTS="llvm;clang;clang-tools-extra;lldb;lld" -DLLVM_OPTIMIZED_TABLEGEN=ON -DLLVM_BUILD_TESTS=ON -DLLVM_BUILD_EXAMPLES=ON -DLLDB_ENABLE_PYTHON=NO -DLLVM_BUILD_LLVM_DYLIB=ON -DLLVM_BUILD_LLVM_DYLIB_VIS=ON -DLLVM_LINK_LLVM_DYLIB=ON -DCLANG_LINK_CLANG_DYLIB=OFF -DCMAKE_BUILD_TYPE=Release
ninja -C build
```

Added: 
    

Modified: 
    llvm/include/llvm/CodeGen/MachineLoopInfo.h
    llvm/include/llvm/IR/PassManager.h

Removed: 
    


################################################################################
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..acb17a8090c51 100644
--- a/llvm/include/llvm/IR/PassManager.h
+++ b/llvm/include/llvm/IR/PassManager.h
@@ -657,8 +657,14 @@ 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>
-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