Skip to content

Commit 0580563

Browse files
authored
[llvm] annotate interfaces in llvm-c for DLL export (#141701)
## Purpose This patch is one in a series of code-mods that annotate LLVM’s public interface for export. This patch annotates the `llvm-c` interface with a new `LLVM_C_ABI` annotation, which behaves like the `LLVM_ABI`. This annotation currently has no meaningful impact on the LLVM build; however, it is a prerequisite to support an LLVM Windows DLL (shared library) build. ## Overview 1. Add a new `llvm-c/Visibility.h` header file that defines a new `LLVM_C_ABI` macro. The macro resolves to the proper DLL export/import annotation on Windows and a "default" visibility annotation elsewhere. 2. Add a new `LLVM_ENABLE_LLVM_C_EXPORT_ANNOTATIONS` `#cmakedefine` that is used to gate the definition of `LLVM_C_ABI`. 3. Remove the existing `LLVM_C_ABI` definition from `llvm/Support/Compiler.h`. Update the small number of `LLVM_C_ABI` references to get it from the new `llvm-c/Visibility.h` header. 4. Code-mod annotate the public `llvm-c` interface using the [Interface Definition Scanner (IDS)](https://github.com/compnerd/ids) tool. 5. Format the changes with `clang-format`. ## 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 Local builds and tests to validate cross-platform compatibility. This included llvm, clang, and lldb on the following configurations: - Windows with MSVC - Windows with Clang - Linux with GCC - Linux with Clang - Darwin with Clang
1 parent f00a7a4 commit 0580563

34 files changed

+1984
-1698
lines changed

llvm/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,10 @@ if (LLVM_BUILD_LLVM_DYLIB OR LLVM_BUILD_SHARED_LIBS OR LLVM_ENABLE_PLUGINS)
10711071
set(LLVM_ENABLE_LLVM_EXPORT_ANNOTATIONS ON)
10721072
endif()
10731073

1074+
# Because LLVM-C is built into the LLVM library, for now export its symbols
1075+
# whenever LLVM symbols are exported.
1076+
set(LLVM_ENABLE_LLVM_C_EXPORT_ANNOTATIONS ${LLVM_ENABLE_LLVM_EXPORT_ANNOTATIONS})
1077+
10741078
set(LLVM_ENABLE_NEW_PASS_MANAGER TRUE CACHE BOOL
10751079
"Enable the new pass manager by default.")
10761080
if(NOT LLVM_ENABLE_NEW_PASS_MANAGER)

llvm/include/llvm-c/Analysis.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "llvm-c/ExternC.h"
2323
#include "llvm-c/Types.h"
24+
#include "llvm-c/Visibility.h"
2425

2526
LLVM_C_EXTERN_C_BEGIN
2627

@@ -41,17 +42,19 @@ typedef enum {
4142
/* Verifies that a module is valid, taking the specified action if not.
4243
Optionally returns a human-readable description of any invalid constructs.
4344
OutMessage must be disposed with LLVMDisposeMessage. */
44-
LLVMBool LLVMVerifyModule(LLVMModuleRef M, LLVMVerifierFailureAction Action,
45-
char **OutMessage);
45+
LLVM_C_ABI LLVMBool LLVMVerifyModule(LLVMModuleRef M,
46+
LLVMVerifierFailureAction Action,
47+
char **OutMessage);
4648

4749
/* Verifies that a single function is valid, taking the specified action. Useful
4850
for debugging. */
49-
LLVMBool LLVMVerifyFunction(LLVMValueRef Fn, LLVMVerifierFailureAction Action);
51+
LLVM_C_ABI LLVMBool LLVMVerifyFunction(LLVMValueRef Fn,
52+
LLVMVerifierFailureAction Action);
5053

5154
/* Open up a ghostview window that displays the CFG of the current function.
5255
Useful for debugging. */
53-
void LLVMViewFunctionCFG(LLVMValueRef Fn);
54-
void LLVMViewFunctionCFGOnly(LLVMValueRef Fn);
56+
LLVM_C_ABI void LLVMViewFunctionCFG(LLVMValueRef Fn);
57+
LLVM_C_ABI void LLVMViewFunctionCFGOnly(LLVMValueRef Fn);
5558

5659
/**
5760
* @}

llvm/include/llvm-c/BitReader.h

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "llvm-c/ExternC.h"
2323
#include "llvm-c/Types.h"
24+
#include "llvm-c/Visibility.h"
2425

2526
LLVM_C_EXTERN_C_BEGIN
2627

@@ -36,30 +37,33 @@ LLVM_C_EXTERN_C_BEGIN
3637
Optionally returns a human-readable error message via OutMessage.
3738
3839
This is deprecated. Use LLVMParseBitcode2. */
39-
LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule,
40-
char **OutMessage);
40+
LLVM_C_ABI LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf,
41+
LLVMModuleRef *OutModule,
42+
char **OutMessage);
4143

4244
/* Builds a module from the bitcode in the specified memory buffer, returning a
4345
reference to the module via the OutModule parameter. Returns 0 on success. */
44-
LLVMBool LLVMParseBitcode2(LLVMMemoryBufferRef MemBuf,
45-
LLVMModuleRef *OutModule);
46+
LLVM_C_ABI LLVMBool LLVMParseBitcode2(LLVMMemoryBufferRef MemBuf,
47+
LLVMModuleRef *OutModule);
4648

4749
/* This is deprecated. Use LLVMParseBitcodeInContext2. */
48-
LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef,
49-
LLVMMemoryBufferRef MemBuf,
50-
LLVMModuleRef *OutModule, char **OutMessage);
50+
LLVM_C_ABI LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef,
51+
LLVMMemoryBufferRef MemBuf,
52+
LLVMModuleRef *OutModule,
53+
char **OutMessage);
5154

52-
LLVMBool LLVMParseBitcodeInContext2(LLVMContextRef ContextRef,
53-
LLVMMemoryBufferRef MemBuf,
54-
LLVMModuleRef *OutModule);
55+
LLVM_C_ABI LLVMBool LLVMParseBitcodeInContext2(LLVMContextRef ContextRef,
56+
LLVMMemoryBufferRef MemBuf,
57+
LLVMModuleRef *OutModule);
5558

5659
/** Reads a module from the specified path, returning via the OutMP parameter
5760
a module provider which performs lazy deserialization. Returns 0 on success.
5861
Optionally returns a human-readable error message via OutMessage.
5962
This is deprecated. Use LLVMGetBitcodeModuleInContext2. */
60-
LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef,
61-
LLVMMemoryBufferRef MemBuf,
62-
LLVMModuleRef *OutM, char **OutMessage);
63+
LLVM_C_ABI LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef,
64+
LLVMMemoryBufferRef MemBuf,
65+
LLVMModuleRef *OutM,
66+
char **OutMessage);
6367

6468
/** Reads a module from the given memory buffer, returning via the OutMP
6569
* parameter a module provider which performs lazy deserialization.
@@ -68,15 +72,17 @@ LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef,
6872
*
6973
* Takes ownership of \p MemBuf if (and only if) the module was read
7074
* successfully. */
71-
LLVMBool LLVMGetBitcodeModuleInContext2(LLVMContextRef ContextRef,
72-
LLVMMemoryBufferRef MemBuf,
73-
LLVMModuleRef *OutM);
75+
LLVM_C_ABI LLVMBool LLVMGetBitcodeModuleInContext2(LLVMContextRef ContextRef,
76+
LLVMMemoryBufferRef MemBuf,
77+
LLVMModuleRef *OutM);
7478

7579
/* This is deprecated. Use LLVMGetBitcodeModule2. */
76-
LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
77-
char **OutMessage);
80+
LLVM_C_ABI LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf,
81+
LLVMModuleRef *OutM,
82+
char **OutMessage);
7883

79-
LLVMBool LLVMGetBitcodeModule2(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM);
84+
LLVM_C_ABI LLVMBool LLVMGetBitcodeModule2(LLVMMemoryBufferRef MemBuf,
85+
LLVMModuleRef *OutM);
8086

8187
/**
8288
* @}

llvm/include/llvm-c/BitWriter.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "llvm-c/ExternC.h"
2323
#include "llvm-c/Types.h"
24+
#include "llvm-c/Visibility.h"
2425

2526
LLVM_C_EXTERN_C_BEGIN
2627

@@ -34,18 +35,18 @@ LLVM_C_EXTERN_C_BEGIN
3435
/*===-- Operations on modules ---------------------------------------------===*/
3536

3637
/** Writes a module to the specified path. Returns 0 on success. */
37-
int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path);
38+
LLVM_C_ABI int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path);
3839

3940
/** Writes a module to an open file descriptor. Returns 0 on success. */
40-
int LLVMWriteBitcodeToFD(LLVMModuleRef M, int FD, int ShouldClose,
41-
int Unbuffered);
41+
LLVM_C_ABI int LLVMWriteBitcodeToFD(LLVMModuleRef M, int FD, int ShouldClose,
42+
int Unbuffered);
4243

4344
/** Deprecated for LLVMWriteBitcodeToFD. Writes a module to an open file
4445
descriptor. Returns 0 on success. Closes the Handle. */
45-
int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int Handle);
46+
LLVM_C_ABI int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int Handle);
4647

4748
/** Writes a module to a new memory buffer and returns it. */
48-
LLVMMemoryBufferRef LLVMWriteBitcodeToMemoryBuffer(LLVMModuleRef M);
49+
LLVM_C_ABI LLVMMemoryBufferRef LLVMWriteBitcodeToMemoryBuffer(LLVMModuleRef M);
4950

5051
/**
5152
* @}

llvm/include/llvm-c/Comdat.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "llvm-c/ExternC.h"
1818
#include "llvm-c/Types.h"
19+
#include "llvm-c/Visibility.h"
1920

2021
LLVM_C_EXTERN_C_BEGIN
2122

@@ -43,35 +44,37 @@ typedef enum {
4344
*
4445
* @see llvm::Module::getOrInsertComdat()
4546
*/
46-
LLVMComdatRef LLVMGetOrInsertComdat(LLVMModuleRef M, const char *Name);
47+
LLVM_C_ABI LLVMComdatRef LLVMGetOrInsertComdat(LLVMModuleRef M,
48+
const char *Name);
4749

4850
/**
4951
* Get the Comdat assigned to the given global object.
5052
*
5153
* @see llvm::GlobalObject::getComdat()
5254
*/
53-
LLVMComdatRef LLVMGetComdat(LLVMValueRef V);
55+
LLVM_C_ABI LLVMComdatRef LLVMGetComdat(LLVMValueRef V);
5456

5557
/**
5658
* Assign the Comdat to the given global object.
5759
*
5860
* @see llvm::GlobalObject::setComdat()
5961
*/
60-
void LLVMSetComdat(LLVMValueRef V, LLVMComdatRef C);
62+
LLVM_C_ABI void LLVMSetComdat(LLVMValueRef V, LLVMComdatRef C);
6163

6264
/*
6365
* Get the conflict resolution selection kind for the Comdat.
6466
*
6567
* @see llvm::Comdat::getSelectionKind()
6668
*/
67-
LLVMComdatSelectionKind LLVMGetComdatSelectionKind(LLVMComdatRef C);
69+
LLVM_C_ABI LLVMComdatSelectionKind LLVMGetComdatSelectionKind(LLVMComdatRef C);
6870

6971
/*
7072
* Set the conflict resolution selection kind for the Comdat.
7173
*
7274
* @see llvm::Comdat::setSelectionKind()
7375
*/
74-
void LLVMSetComdatSelectionKind(LLVMComdatRef C, LLVMComdatSelectionKind Kind);
76+
LLVM_C_ABI void LLVMSetComdatSelectionKind(LLVMComdatRef C,
77+
LLVMComdatSelectionKind Kind);
7578

7679
/**
7780
* @}

0 commit comments

Comments
 (0)