Skip to content

Commit 8d637e3

Browse files
committed
[llvm] add docs for interface annotations (LLVM_ABI, etc)
1 parent 8fddef8 commit 8d637e3

File tree

1 file changed

+277
-0
lines changed

1 file changed

+277
-0
lines changed
Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
LLVM Interface Export Annotations
2+
=================================
3+
4+
With the following build settings, LLVM will be built as a shared library with
5+
hidden default symbol visibility:
6+
7+
::
8+
9+
LLVM_BUILD_LLVM_DYLIB_VIS=On
10+
LLVM_BUILD_LLVM_DYLIB=On
11+
LLVM_LINK_LLVM_DYLIB=On
12+
13+
When building LLVM in this configuration, any symbol intended to be visible to
14+
clients must be explicitly exported.
15+
16+
Exporting a symbol typically requires annotating it with the ``LLVM_ABI`` macro
17+
defined in `compiler.h
18+
<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.
21+
22+
Functions
23+
---------
24+
25+
Exported function declarations in header files must be annotated with
26+
``LLVM_ABI``.
27+
28+
.. code:: cpp
29+
30+
#include "llvm/Support/Compiler.h"
31+
32+
LLVM_ABI void exported_function(int a, int b);
33+
34+
Global Variables
35+
----------------
36+
37+
Exported global variables must be annotated with ``LLVM_ABI`` at their
38+
``extern`` declarations.
39+
40+
.. code:: cpp
41+
42+
#include "llvm/Support/Compiler.h"
43+
44+
LLVM_ABI extern int exported_global_variable;
45+
46+
Classes, Structs, and Unions
47+
----------------------------
48+
49+
Classes, structs, and unions can be annotated with ``LLVM_ABI`` at their
50+
definition, but this option is generally discouraged. Annotating the entire
51+
definition exports unnecessary symbols, such as private functions, vtables, and
52+
type information. Instead, ``LLVM_ABI`` should be applied only to public and
53+
protected method declarations without a body in the header, including
54+
constructors and destructors.
55+
56+
.. code:: cpp
57+
58+
#include "llvm/Support/Compiler.h"
59+
60+
class ExampleClass {
61+
public:
62+
// Public methods defined externally must be annotatated.
63+
LLVM_ABI int sourceDefinedPublicMethod(int a, int b);
64+
65+
// Methods defined in the class definition do not need annotation.
66+
int headerDefinedPublicMethod(int a, int b) {
67+
return a + b;
68+
}
69+
70+
// Constructors and destructors must be annotated if defined externally.
71+
ExampleClass() {}
72+
LLVM_ABI ~ExampleClass();
73+
74+
// Public static methods defined externally must be annotatated.
75+
LLVM_ABI static int sourceDefinedPublicStaticMethod(int a, int b);
76+
};
77+
78+
Additionally, public and protected static fields that are not initialized at
79+
declaration must be annotated with ``LLVM_ABI``.
80+
81+
.. code:: cpp
82+
83+
#include "llvm/Support/Compiler.h"
84+
85+
class ExampleClass {
86+
public:
87+
// Public static fields defined externally must be annotated.
88+
LLVM_ABI static int mutableStaticField;
89+
LLVM_ABI static const int constStaticField;
90+
91+
// Static members initialized at declaration do not need to be annotated.
92+
static const int initializedConstStaticField = 0;
93+
static constexpr int initializedConstexprStaticField = 0;
94+
};
95+
96+
Private methods may also require ``LLVM_ABI`` annotation in certain cases. This
97+
situation occurs when a method defined in a header calls the private method. The
98+
private method call may be from within the class, a parent class, or a friend
99+
class.
100+
101+
.. code:: cpp
102+
103+
#include "llvm/Support/Compiler.h"
104+
105+
class ExampleClass {
106+
private:
107+
// Private methods must be annotated if referenced by a public method defined a
108+
// header file.
109+
LLVM_ABI int privateMethod(int a, int b);
110+
111+
public:
112+
// Inlineable method defined in the class definition calls a private method
113+
// defined externally. If the private method is not annotated for export, this
114+
// method will fail to link.
115+
int publicMethod(int a, int b) {
116+
return privateMethod(a, b);
117+
}
118+
};
119+
120+
Friend Functions
121+
----------------
122+
123+
Friend functions declared in a class, struct or union must be annotated with
124+
``LLVM_ABI`` if the corresponding function declaration is also annotated. This
125+
requirement applies even when the class itself is annotated with ``LLVM_ABI``.
126+
127+
.. code:: cpp
128+
129+
#include "llvm/Support/Compiler.h"
130+
131+
// An exported function that has friend access to ExampleClass internals.
132+
LLVM_ABI int friend_function(ExampleClass &obj);
133+
134+
class ExampleClass {
135+
// Friend declaration of a function must be annotated the same as the actual
136+
// function declaration.
137+
LLVM_ABI friend int friend_function(ExampleClass &obj);
138+
};
139+
140+
.. note::
141+
Annotating the friend declaration avoids an “inconsistent dll linkage”
142+
compiler error when building for Windows. This annotation is harmless but not
143+
required when building ELF or Mach-O shared libraries.
144+
145+
VTable and Type Info
146+
--------------------
147+
148+
Classes and structs with exported virtual methods, or child classes that export
149+
overridden virtual methods, must also export their vtable for ELF and Mach-O
150+
builds. This can be achieved by annotating the class rather than individual
151+
class members.
152+
153+
.. code:: cpp
154+
155+
#include "llvm/Support/Compiler.h"
156+
157+
class ParentClass {
158+
public:
159+
virtual int virtualMethod(int a, int b);
160+
virtual int anotherVirtualMethod(int a, int b);
161+
virtual ~ParentClass();
162+
};
163+
164+
// Annotating the class exports vtable and type information as well as all
165+
// class members.
166+
class LLVM_ABI ChildClass : public ParentClass {
167+
public:
168+
// Inline method override does not require the class be annotated.
169+
int virtualMethod(int a, int b) override {
170+
return ParentClass::virtualMethod(a, b);
171+
}
172+
173+
// Overriding a virtual method from the parent requires the class be
174+
// annotated. The parent class may require annotation as well.
175+
int pureVirtualMethod(int a, int b) override;
176+
~ChildClass();
177+
};
178+
179+
If annotating a type with ``LLVM_ABI`` causes compilation issues such as those
180+
described
181+
`here <https://devblogs.microsoft.com/oldnewthing/20190927-00/?p=102932>`__,
182+
the class may require modification. Often, explicitly deleting the copy
183+
constructor and copy assignment operator will resolve the issue.
184+
185+
.. code:: cpp
186+
187+
#include "llvm/Support/Compiler.h"
188+
189+
#include <vector>
190+
191+
class LLVM_ABI ExportedClass {
192+
public:
193+
// Explicitly delete the copy constructor and assignment operator.
194+
ExportedClass(ExportedClass const&) = delete;
195+
ExportedClass& operator=(ExportedClass const&) = delete;
196+
};
197+
198+
Templates
199+
---------
200+
201+
Most template classes are entirely header-defined and do not need to be exported
202+
because they will be instantiated and compiled into the client as needed. Such
203+
template classes require no export annotations. However, there are some less
204+
common cases where annotations are required for templates.
205+
206+
Specialized Template Functions
207+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
208+
209+
As with any other exported function, an exported specialization of a template
210+
function not defined in a header file must have its declaration annotated with
211+
``LLVM_ABI``.
212+
213+
.. code:: cpp
214+
215+
#include "llvm/Support/Compiler.h"
216+
217+
template <typename T> T templateMethod(T a, T b) {
218+
return a + b;
219+
}
220+
221+
// The explicitly specialized definition of templateMethod for int is located in
222+
// a source file. This declaration must be annotated with LLVM_ABI to export it.
223+
template <> LLVM_ABI int templateMethod(int a, int b);
224+
225+
Similarly, an exported specialization of a method in a template class must have
226+
its declaration annotated with ``LLVM_ABI``.
227+
228+
.. code:: cpp
229+
230+
#include "llvm/Support/Compiler.h"
231+
232+
template <typename T> class TemplateClass {
233+
public:
234+
int method(int a, int b) {
235+
return a + b;
236+
}
237+
};
238+
239+
// The explicitly specialized definition of method for int is defined in a
240+
// source file. The declaration must be annotated with LLVM_ABI to export it.
241+
template <> LLVM_ABI int TemplateStruct<int>::method(int a, int b);
242+
243+
Explicitly Instantiated Template Classes
244+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
245+
246+
Explicitly instantiated template classes must be annotated with
247+
template-specific annotations at both declaration and definition.
248+
249+
An extern template instantiation in a header file must be annotated with
250+
``LLVM_TEMPLATE_ABI``. This will typically be located in a header file.
251+
252+
.. code:: cpp
253+
254+
#include "llvm/Support/Compiler.h"
255+
256+
template <typename T> class TemplateClass {
257+
public:
258+
TemplateClass(T val) : val_(val) {}
259+
260+
T get() const { return val_; }
261+
262+
private:
263+
const T val_;
264+
};
265+
266+
// Explicitly instantiate and export TempalateClass for int type.
267+
extern template class LLVM_TEMPLATE_ABI TemplateClass<int>;
268+
269+
The corresponding definition of the template instantiation must be annotated
270+
with ``LLVM_EXPORT_TEMPLATE``. This will typically be located in a source file.
271+
272+
.. code:: cpp
273+
274+
#include "TemplateClass.h"
275+
276+
// Explicitly instantiate and export TempalateClass for int type.
277+
template class LLVM_EXPORT_TEMPLATE TemplateClass<int>;

0 commit comments

Comments
 (0)