Skip to content

Commit e175ecf

Browse files
[llvm] revisions to LLVM_ABI export macro definitions (#144598)
## Purpose Simplify the logic used to define `LLVM_ABI` and related macros, eliminate the `LLVM_ABI_FRIEND` macro, and update the `LLVM_ABI` macro to always resolve to `__attribute__((visibility("default")))` when building LLVM as a shared library for ELF or Mach-O targets. ## Background Previously, `LLVM_ABI` was defined to the C++ style attribute `[[gnu::visibility("default")]]` when compiling with gcc, which has more restrictions on its placement. Of note, the C++ style attributes cannot decorate `friend` functions and must not appear after `extern` on variable declarations. 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). ## Overview - Define a new CMake config value, `LLVM_ENABLE_LLVM_EXPORT_ANNOTATIONS`, which is implicitly set whenever `LLVM_BUILD_LLVM_DYLIB`, `LLVM_BUILD_SHARED_LIBS`, or `LLVM_ENABLE_PLUGINS` is set. Add it as a `#cmakedefine` to llvm-config.h so its definition is available to projects building against LLVM as required so clients see `__declspec(dllimport)` on Windows. - Gate the `LLVM_ABI` macro definitions in Compiler.h behind the new `LLVM_ENABLE_LLVM_EXPORT_ANNOTATIONS` definition. This is simpler/cleaner, but should be equivalent to the previous logic. - Maintain `LLVM_BUILD_STATIC` as an override to be used by specific targets that don't want to build against the DLL/shared library, such as tablegen. - For ELF and Mach-O targets, directly define `LLVM_ABI` as `__attribute__((visibility("default")))` instead of `LLVM_ATTRIBUTE_VISIBILITY_DEFAULT`, which resolves to C++ style `[[gnu::visibility("default")]]` when compiling with gcc. - Remove the `LLVM_ABI_FRIEND` macro and replace all usages of it with `LLVM_ABI`. - Update the documentation for exporting friend functions to no longer reference `LLVM_ABI_FRIEND`. ## Validation - Built as static lib with clang and gcc on Linux. - Built as static with clang-cl and MSVC on Windows. - Built as shared lib with clang and gcc on Linux (+ additional local changes not yet merged). - Built as DLL with clang-cl and MSVC on Windows (+ additional local changes not yet merged). --------- Co-authored-by: SquallATF <squallatf@gmail.com>
1 parent 6a8899c commit e175ecf

21 files changed

+70
-80
lines changed

llvm/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,13 @@ if (LLVM_LINK_LLVM_DYLIB AND BUILD_SHARED_LIBS)
942942
message(FATAL_ERROR "Cannot enable BUILD_SHARED_LIBS with LLVM_LINK_LLVM_DYLIB. We recommend disabling BUILD_SHARED_LIBS.")
943943
endif()
944944

945+
set(LLVM_ENABLE_LLVM_EXPORT_ANNOTATIONS OFF)
946+
if (LLVM_BUILD_LLVM_DYLIB OR LLVM_BUILD_SHARED_LIBS OR LLVM_ENABLE_PLUGINS)
947+
# Export annotations for LLVM must be enabled if building as a shared lib or
948+
# enabling plugins.
949+
set(LLVM_ENABLE_LLVM_EXPORT_ANNOTATIONS ON)
950+
endif()
951+
945952
option(LLVM_OPTIMIZED_TABLEGEN "Force TableGen to be built with optimization" OFF)
946953
if(CMAKE_CROSSCOMPILING OR (LLVM_OPTIMIZED_TABLEGEN AND (LLVM_ENABLE_ASSERTIONS OR CMAKE_CONFIGURATION_TYPES)))
947954
set(LLVM_USE_HOST_TOOLS ON)

llvm/docs/InterfaceExportAnnotations.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ method in a C++ class, it may be annotated for export.
222222
Friend Functions
223223
~~~~~~~~~~~~~~~~
224224
Friend functions declared in a class, struct or union must be annotated with
225-
``LLVM_ABI_FRIEND`` if the corresponding function declaration is annotated with
225+
``LLVM_ABI`` if the corresponding function declaration is annotated with
226226
``LLVM_ABI``. This requirement applies even when the class containing the friend
227227
declaration is annotated with ``LLVM_ABI``.
228228

@@ -236,14 +236,13 @@ declaration is annotated with ``LLVM_ABI``.
236236
class ExampleClass {
237237
// Friend declaration of a function must be annotated the same as the actual
238238
// function declaration.
239-
LLVM_ABI_FRIEND friend int friend_function(ExampleClass &obj);
239+
LLVM_ABI friend int friend_function(ExampleClass &obj);
240240
};
241241
242242
.. note::
243243

244244
Annotating the friend declaration avoids an “inconsistent dll linkage”
245-
compiler error when building a DLL for Windows. The ``LLVM_ABI_FRIEND``
246-
annotation is a no-op when building ELF or Mach-O shared libraries.
245+
compiler error when building a DLL for Windows.
247246

248247
Virtual Table and Type Info
249248
~~~~~~~~~~~~~~~~~~~~~~~~~~~

llvm/include/llvm/ADT/APFloat.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ class IEEEFloat final {
572572
/// emphasizes producing different codes for different inputs in order to
573573
/// be used in canonicalization and memoization. As such, equality is
574574
/// bitwiseIsEqual, and 0 != -0.
575-
LLVM_ABI_FRIEND friend hash_code hash_value(const IEEEFloat &Arg);
575+
LLVM_ABI friend hash_code hash_value(const IEEEFloat &Arg);
576576

577577
/// Converts this value into a decimal string.
578578
///
@@ -629,13 +629,12 @@ class IEEEFloat final {
629629
/// 0 -> \c IEK_Zero
630630
/// Inf -> \c IEK_Inf
631631
///
632-
LLVM_ABI_FRIEND friend int ilogb(const IEEEFloat &Arg);
632+
LLVM_ABI friend int ilogb(const IEEEFloat &Arg);
633633

634634
/// Returns: X * 2^Exp for integral exponents.
635-
LLVM_ABI_FRIEND friend IEEEFloat scalbn(IEEEFloat X, int Exp, roundingMode);
635+
LLVM_ABI friend IEEEFloat scalbn(IEEEFloat X, int Exp, roundingMode);
636636

637-
LLVM_ABI_FRIEND friend IEEEFloat frexp(const IEEEFloat &X, int &Exp,
638-
roundingMode);
637+
LLVM_ABI friend IEEEFloat frexp(const IEEEFloat &X, int &Exp, roundingMode);
639638

640639
/// \name Special value setters.
641640
/// @{
@@ -906,11 +905,11 @@ class DoubleAPFloat final {
906905
LLVM_ABI LLVM_READONLY int getExactLog2() const;
907906
LLVM_ABI LLVM_READONLY int getExactLog2Abs() const;
908907

909-
LLVM_ABI_FRIEND friend DoubleAPFloat scalbn(const DoubleAPFloat &X, int Exp,
910-
roundingMode);
911-
LLVM_ABI_FRIEND friend DoubleAPFloat frexp(const DoubleAPFloat &X, int &Exp,
912-
roundingMode);
913-
LLVM_ABI_FRIEND friend hash_code hash_value(const DoubleAPFloat &Arg);
908+
LLVM_ABI friend DoubleAPFloat scalbn(const DoubleAPFloat &X, int Exp,
909+
roundingMode);
910+
LLVM_ABI friend DoubleAPFloat frexp(const DoubleAPFloat &X, int &Exp,
911+
roundingMode);
912+
LLVM_ABI friend hash_code hash_value(const DoubleAPFloat &Arg);
914913
};
915914

916915
LLVM_ABI hash_code hash_value(const DoubleAPFloat &Arg);
@@ -1518,7 +1517,7 @@ class APFloat : public APFloatBase {
15181517
APFLOAT_DISPATCH_ON_SEMANTICS(getExactLog2());
15191518
}
15201519

1521-
LLVM_ABI_FRIEND friend hash_code hash_value(const APFloat &Arg);
1520+
LLVM_ABI friend hash_code hash_value(const APFloat &Arg);
15221521
friend int ilogb(const APFloat &Arg) { return ilogb(Arg.getIEEE()); }
15231522
friend APFloat scalbn(APFloat X, int Exp, roundingMode RM);
15241523
friend APFloat frexp(const APFloat &X, int &Exp, roundingMode RM);

llvm/include/llvm/ADT/SlowDynamicAPInt.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,17 @@ class SlowDynamicAPInt {
6262
LLVM_ABI SlowDynamicAPInt &operator++();
6363
LLVM_ABI SlowDynamicAPInt &operator--();
6464

65-
LLVM_ABI_FRIEND friend SlowDynamicAPInt abs(const SlowDynamicAPInt &X);
66-
LLVM_ABI_FRIEND friend SlowDynamicAPInt ceilDiv(const SlowDynamicAPInt &LHS,
67-
const SlowDynamicAPInt &RHS);
68-
LLVM_ABI_FRIEND friend SlowDynamicAPInt floorDiv(const SlowDynamicAPInt &LHS,
69-
const SlowDynamicAPInt &RHS);
65+
LLVM_ABI friend SlowDynamicAPInt abs(const SlowDynamicAPInt &X);
66+
LLVM_ABI friend SlowDynamicAPInt ceilDiv(const SlowDynamicAPInt &LHS,
67+
const SlowDynamicAPInt &RHS);
68+
LLVM_ABI friend SlowDynamicAPInt floorDiv(const SlowDynamicAPInt &LHS,
69+
const SlowDynamicAPInt &RHS);
7070
/// The operands must be non-negative for gcd.
71-
LLVM_ABI_FRIEND friend SlowDynamicAPInt gcd(const SlowDynamicAPInt &A,
72-
const SlowDynamicAPInt &B);
71+
LLVM_ABI friend SlowDynamicAPInt gcd(const SlowDynamicAPInt &A,
72+
const SlowDynamicAPInt &B);
7373

7474
/// Overload to compute a hash_code for a SlowDynamicAPInt value.
75-
LLVM_ABI_FRIEND friend hash_code
76-
hash_value(const SlowDynamicAPInt &X); // NOLINT
75+
LLVM_ABI friend hash_code hash_value(const SlowDynamicAPInt &X); // NOLINT
7776

7877
// Make DynamicAPInt a friend so it can access Val directly.
7978
friend DynamicAPInt;

llvm/include/llvm/Bitcode/BitcodeReader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ struct ParserCallbacks {
120120
IdentificationBit(IdentificationBit), ModuleBit(ModuleBit) {}
121121

122122
// Calls the ctor.
123-
LLVM_ABI_FRIEND friend Expected<BitcodeFileContents>
123+
LLVM_ABI friend Expected<BitcodeFileContents>
124124
getBitcodeFileContents(MemoryBufferRef Buffer);
125125

126126
Expected<std::unique_ptr<Module>>

llvm/include/llvm/CodeGen/MachineOperand.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ class MachineOperand {
764764
/// isIdenticalTo uses for comparison. It is thus suited for use in hash
765765
/// tables which use that function for equality comparisons only. This must
766766
/// stay exactly in sync with isIdenticalTo above.
767-
LLVM_ABI_FRIEND friend hash_code hash_value(const MachineOperand &MO);
767+
LLVM_ABI friend hash_code hash_value(const MachineOperand &MO);
768768

769769
/// ChangeToImmediate - Replace this operand with a new immediate operand of
770770
/// the specified value. If an operand is known to be an immediate already,

llvm/include/llvm/CodeGen/PseudoSourceValue.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ class LLVM_ABI PseudoSourceValue {
4646
private:
4747
unsigned Kind;
4848
unsigned AddressSpace;
49-
LLVM_ABI_FRIEND friend raw_ostream &
50-
llvm::operator<<(raw_ostream &OS, const PseudoSourceValue *PSV);
49+
LLVM_ABI friend raw_ostream &llvm::operator<<(raw_ostream &OS,
50+
const PseudoSourceValue *PSV);
5151

5252
friend class MachineMemOperand; // For printCustom().
5353
friend class MIRFormatter; // For printCustom().

llvm/include/llvm/Config/llvm-config.h.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@
110110
/* Define if building LLVM with BUILD_SHARED_LIBS */
111111
#cmakedefine LLVM_BUILD_SHARED_LIBS
112112

113+
/* Define if exporting LLVM public interface for shared library */
114+
#cmakedefine LLVM_ENABLE_LLVM_EXPORT_ANNOTATIONS
115+
113116
/* Define if building LLVM with LLVM_FORCE_USE_OLD_TOOLCHAIN_LIBS */
114117
#cmakedefine LLVM_FORCE_USE_OLD_TOOLCHAIN ${LLVM_FORCE_USE_OLD_TOOLCHAIN}
115118

llvm/include/llvm/ExecutionEngine/Orc/LLJIT.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ class ExecutorProcessControl;
4242
class LLVM_ABI LLJIT {
4343
template <typename, typename, typename> friend class LLJITBuilderSetters;
4444

45-
LLVM_ABI_FRIEND friend Expected<JITDylibSP>
46-
setUpGenericLLVMIRPlatform(LLJIT &J);
45+
LLVM_ABI friend Expected<JITDylibSP> setUpGenericLLVMIRPlatform(LLJIT &J);
4746

4847
public:
4948
/// Initializer support for LLJIT.

llvm/include/llvm/ExecutionEngine/Orc/SymbolStringPool.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ class SymbolStringPool {
3636
friend class SymbolStringPoolEntryUnsafe;
3737

3838
// Implemented in DebugUtils.h.
39-
LLVM_ABI_FRIEND friend raw_ostream &operator<<(raw_ostream &OS,
40-
const SymbolStringPool &SSP);
39+
LLVM_ABI friend raw_ostream &operator<<(raw_ostream &OS,
40+
const SymbolStringPool &SSP);
4141

4242
public:
4343
/// Destroy a SymbolStringPool.
@@ -94,8 +94,8 @@ class SymbolStringPtrBase {
9494
return LHS.S < RHS.S;
9595
}
9696

97-
LLVM_ABI_FRIEND friend raw_ostream &
98-
operator<<(raw_ostream &OS, const SymbolStringPtrBase &Sym);
97+
LLVM_ABI friend raw_ostream &operator<<(raw_ostream &OS,
98+
const SymbolStringPtrBase &Sym);
9999

100100
#ifndef NDEBUG
101101
// Returns true if the pool entry's ref count is above zero (or if the entry

0 commit comments

Comments
 (0)