Skip to content

Commit 74fba79

Browse files
committed
add introduction with more details and context
1 parent 01ce75b commit 74fba79

File tree

1 file changed

+72
-17
lines changed

1 file changed

+72
-17
lines changed

llvm/docs/InterfaceExportAnnotations.rst

Lines changed: 72 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,80 @@
11
LLVM Interface Export Annotations
22
=================================
33

4-
With the following build settings, LLVM will be built as a shared library with
5-
hidden default symbol visibility:
4+
Symbols that are part of LLVM's public interface must be explicitly annotated
5+
to support shared library builds with hidden default symbol visibility. This
6+
document provides background and guidelines for annotating the codebase.
7+
8+
LLVM Shared Library
9+
-------------------
10+
LLVM builds as a static library by default, but it can also be built as a shared
11+
library with the following configuration:
612

713
::
814

9-
LLVM_BUILD_LLVM_DYLIB_VIS=On
1015
LLVM_BUILD_LLVM_DYLIB=On
1116
LLVM_LINK_LLVM_DYLIB=On
1217

13-
When building LLVM in this configuration, any symbol intended to be visible to
14-
clients must be explicitly exported.
18+
For ELF and Mach-O builds, this configuration works as-is because all symbols
19+
are exported by default. To build a Windows DLL, however, the situation is more
20+
complex:
21+
22+
- Symbols are not exported from a DLL by default. Symbols must be annotated with
23+
``__declspec(dllexport)`` when building the library to be externally visible.
24+
25+
- Symbols imported from a Windows DLL should generally be annotated with
26+
``__declspec(dllimport)`` when compiling clients.
27+
28+
- A single Windows DLL can export a maximum of 65,535 symbols.
29+
30+
Annotation Macros
31+
-----------------
32+
The distinct DLL import and export annotations required for Windows DLLs
33+
typically lead developers to define a preprocessor macro for annotating exported
34+
symbols in header public files. The custom macro resolves to the _export_
35+
annotation when building the library and the _import_ annotation when building
36+
the client.
1537

16-
Exporting a symbol typically requires annotating it with the ``LLVM_ABI`` macro
17-
defined in `compiler.h
38+
For this purpose, we have defined the `LLVM_ABI` macro in
39+
`llvm/Support/Compiler.h
1840
<https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/Support/Compiler.h#L152>`__.
19-
There are similar annotations that must be used in a few special cases. This
20-
document describes the common patterns for annotating LLVM symbols for export.
41+
42+
.. code:: cpp
43+
44+
#if defined(LLVM_EXPORTS)
45+
#define LLVM_ABI __declspec(dllexport)
46+
#else
47+
#define LLVM_ABI __declspec(dllimport)
48+
#endif
49+
50+
Because building LLVM for Windows is less common than ELF and Mach-O platforms,
51+
Windows DLL symbol visibility requirements are approximated on these platforms
52+
by setting default symbol visibility to hidden
53+
(``-fvisibility-default=hidden``) when building with the following
54+
configuration:
55+
56+
::
57+
58+
LLVM_BUILD_LLVM_DYLIB_VIS=On
59+
60+
For an ELF or Mach-O platform with this setting, the ``LLVM_ABI`` macro is
61+
defined to override the default hidden symbol visibility:
62+
63+
.. code:: cpp
64+
65+
#define LLVM_ABI __attribute__((visibility("default")))
66+
67+
In addition to ``LLVM_ABI``, there are a few other macros for use in less
68+
common cases described below. All ``LLVM_`` export macros are only for use with
69+
symbols defined in the LLVM library. They must not be used to annotate symbols
70+
defined in other LLVM projects such as lld, lldb, and clang. Their use should
71+
generally restricted to source code under ``llvm-project/llvm``.
72+
73+
Annotating Symbols
74+
------------------
2175

2276
Functions
23-
---------
77+
~~~~~~~~~
2478

2579
Exported function declarations in header files must be annotated with
2680
``LLVM_ABI``.
@@ -32,7 +86,7 @@ Exported function declarations in header files must be annotated with
3286
LLVM_ABI void exported_function(int a, int b);
3387
3488
Global Variables
35-
----------------
89+
~~~~~~~~~~~~~~~~
3690

3791
Exported global variables must be annotated with ``LLVM_ABI`` at their
3892
``extern`` declarations.
@@ -44,7 +98,7 @@ Exported global variables must be annotated with ``LLVM_ABI`` at their
4498
LLVM_ABI extern int exported_global_variable;
4599
46100
Classes, Structs, and Unions
47-
----------------------------
101+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
48102

49103
Classes, structs, and unions can be annotated with ``LLVM_ABI`` at their
50104
definition, but this option is generally discouraged. Annotating the entire
@@ -118,7 +172,7 @@ class.
118172
};
119173
120174
Friend Functions
121-
----------------
175+
~~~~~~~~~~~~~~~~
122176

123177
Friend functions declared in a class, struct or union must be annotated with
124178
``LLVM_ABI`` if the corresponding function declaration is also annotated. This
@@ -138,12 +192,13 @@ requirement applies even when the class itself is annotated with ``LLVM_ABI``.
138192
};
139193
140194
.. note::
195+
141196
Annotating the friend declaration avoids an “inconsistent dll linkage”
142197
compiler error when building for Windows. This annotation is harmless but not
143198
required when building ELF or Mach-O shared libraries.
144199

145200
VTable and Type Info
146-
--------------------
201+
~~~~~~~~~~~~~~~~~~~~
147202

148203
Classes and structs with exported virtual methods, or child classes that export
149204
overridden virtual methods, must also export their vtable for ELF and Mach-O
@@ -196,15 +251,15 @@ constructor and copy assignment operator will resolve the issue.
196251
};
197252
198253
Templates
199-
---------
254+
~~~~~~~~~
200255

201256
Most template classes are entirely header-defined and do not need to be exported
202257
because they will be instantiated and compiled into the client as needed. Such
203258
template classes require no export annotations. However, there are some less
204259
common cases where annotations are required for templates.
205260

206261
Specialized Template Functions
207-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
262+
++++++++++++++++++++++++++++++
208263

209264
As with any other exported function, an exported specialization of a template
210265
function not defined in a header file must have its declaration annotated with
@@ -241,7 +296,7 @@ its declaration annotated with ``LLVM_ABI``.
241296
template <> LLVM_ABI int TemplateStruct<int>::method(int a, int b);
242297
243298
Explicitly Instantiated Template Classes
244-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
299+
++++++++++++++++++++++++++++++++++++++++
245300

246301
Explicitly instantiated template classes must be annotated with
247302
template-specific annotations at both declaration and definition.

0 commit comments

Comments
 (0)