5 The Opaque Pointer Type
6 =======================
8 Traditionally, LLVM IR pointer types have contained a pointee type. For example,
9 ``i32*`` is a pointer that points to an ``i32`` somewhere in memory. However,
10 due to a lack of pointee type semantics and various issues with having pointee
11 types, there is a desire to remove pointee types from pointers.
13 The opaque pointer type project aims to replace all pointer types containing
14 pointee types in LLVM with an opaque pointer type. The new pointer type is
15 represented textually as ``ptr``.
17 Some instructions still need to know what type to treat the memory pointed to by
18 the pointer as. For example, a load needs to know how many bytes to load from
19 memory and what type to treat the resulting value as. In these cases,
20 instructions themselves contain a type argument. For example the load
21 instruction from older versions of LLVM
33 Address spaces are still used to distinguish between different kinds of pointers
34 where the distinction is relevant for lowering (e.g. data vs function pointers
35 have different sizes on some architectures). Opaque pointers are not changing
36 anything related to address spaces and lowering. For more information, see
37 `DataLayout <LangRef.html#langref-datalayout>`_. Opaque pointers in non-default
38 address space are spelled ``ptr addrspace(N)``.
40 This was proposed all the way back in
41 `2015 <https://lists.llvm.org/pipermail/llvm-dev/2015-February/081822.html>`_.
43 Issues with explicit pointee types
44 ==================================
46 LLVM IR pointers can be cast back and forth between pointers with different
47 pointee types. The pointee type does not necessarily represent the actual
48 underlying type in memory. In other words, the pointee type carries no real
51 Historically LLVM was some sort of type-safe subset of C. Having pointee types
52 provided an extra layer of checks to make sure that the Clang frontend matched
53 its frontend values/operations with the corresponding LLVM IR. However, as other
54 languages like C++ adopted LLVM, the community realized that pointee types were
55 more of a hindrance for LLVM development and that the extra type checking with
56 some frontends wasn't worth it.
58 LLVM's type system was `originally designed
59 <https://llvm.org/pubs/2003-05-01-GCCSummit2003.html>`_ to support high-level
60 optimization. However, years of LLVM implementation experience have demonstrated
61 that the pointee type system design does not effectively support
62 optimization. Memory optimization algorithms, such as SROA, GVN, and AA,
63 generally need to look through LLVM's struct types and reason about the
64 underlying memory offsets. The community realized that pointee types hinder LLVM
65 development, rather than helping it. Some of the initially proposed high-level
66 optimizations have evolved into `TBAA
67 <https://llvm.org/docs/LangRef.html#tbaa-metadata>`_ due to limitations with
68 representing higher-level language information directly via SSA values.
70 Pointee types provide some value to frontends because the IR verifier uses types
71 to detect straightforward type confusion bugs. However, frontends also have to
72 deal with the complexity of inserting bitcasts everywhere that they might be
73 required. The community consensus is that the costs of pointee types
74 outweight the benefits, and that they should be removed.
76 Many operations do not actually care about the underlying type. These
77 operations, typically intrinsics, usually end up taking an arbitrary pointer
78 type ``i8*`` and sometimes a size. This causes lots of redundant no-op bitcasts
79 in the IR to and from a pointer with a different pointee type.
81 No-op bitcasts take up memory/disk space and also take up compile time to look
82 through. However, perhaps the biggest issue is the code complexity required to
83 deal with bitcasts. When looking up through def-use chains for pointers it's
84 easy to forget to call `Value::stripPointerCasts()` to find the true underlying
85 pointer obfuscated by bitcasts. And when looking down through def-use chains
86 passes need to iterate through bitcasts to handle uses. Removing no-op pointer
87 bitcasts prevents a category of missed optimizations and makes writing LLVM
88 passes a little bit easier.
90 Fewer no-op pointer bitcasts also reduces the chances of incorrect bitcasts in
91 regards to address spaces. People maintaining backends that care a lot about
92 address spaces have complained that frontends like Clang often incorrectly
93 bitcast pointers, losing address space information.
95 An analogous transition that happened earlier in LLVM is integer signedness.
96 Currently there is no distinction between signed and unsigned integer types, but
97 rather each integer operation (e.g. add) contains flags to signal how to treat
98 the integer. Previously LLVM IR distinguished between unsigned and signed
99 integer types and ran into similar issues of no-op casts. The transition from
100 manifesting signedness in types to instructions happened early on in LLVM's
101 timeline to make LLVM easier to work with.
106 During the transition phase, LLVM can be used in two modes: In typed pointer
107 mode all pointer types have a pointee type and opaque pointers cannot be used.
108 In opaque pointers mode (the default), all pointers are opaque. The opaque
109 pointer mode can be disabled using ``-opaque-pointers=0`` in
110 LLVM tools like ``opt``, or ``-Xclang -no-opaque-pointers`` in clang.
111 Additionally, opaque pointer mode is automatically disabled for IR and bitcode
112 files that explicitly mention ``i8*`` style typed pointers.
114 In opaque pointer mode, all typed pointers used in IR, bitcode, or created
115 using ``PointerType::get()`` and similar APIs are automatically converted into
116 opaque pointers. This simplifies migration and allows testing existing IR with
121 define i8* @test(i8* %p) {
122 %p2 = getelementptr i8, i8* %p, i64 1
126 ; Is automatically converted into the following if -opaque-pointers
129 define ptr @test(ptr %p) {
130 %p2 = getelementptr i8, ptr %p, i64 1
134 Migration Instructions
135 ======================
137 In order to support opaque pointers, two types of changes tend to be necessary.
138 The first is the removal of all calls to ``PointerType::getElementType()`` and
139 ``Type::getPointerElementType()``.
141 In the LLVM middle-end and backend, this is usually accomplished by inspecting
142 the type of relevant operations instead. For example, memory access related
143 analyses and optimizations should use the types encoded in the load and store
144 instructions instead of querying the pointer type.
146 Here are some common ways to avoid pointer element type accesses:
148 * For loads, use ``getType()``.
149 * For stores, use ``getValueOperand()->getType()``.
150 * Use ``getLoadStoreType()`` to handle both of the above in one call.
151 * For getelementptr instructions, use ``getSourceElementType()``.
152 * For calls, use ``getFunctionType()``.
153 * For allocas, use ``getAllocatedType()``.
154 * For globals, use ``getValueType()``.
155 * For consistency assertions, use
156 ``PointerType::isOpaqueOrPointeeTypeEquals()``.
157 * To create a pointer type in a different address space, use
158 ``PointerType::getWithSamePointeeType()``.
159 * To check that two pointers have the same element type, use
160 ``PointerType::hasSameElementTypeAs()``.
161 * While it is preferred to write code in a way that accepts both typed and
162 opaque pointers, ``Type::isOpaquePointerTy()`` and
163 ``PointerType::isOpaque()`` can be used to handle opaque pointers specially.
164 ``PointerType::getNonOpaquePointerElementType()`` can be used as a marker in
165 code-paths where opaque pointers have been explicitly excluded.
166 * To get the type of a byval argument, use ``getParamByValType()``. Similar
167 method exists for other ABI-affecting attributes that need to know the
168 element type, such as byref, sret, inalloca and preallocated.
169 * Some intrinsics require an ``elementtype`` attribute, which can be retrieved
170 using ``getParamElementType()``. This attribute is required in cases where
171 the intrinsic does not naturally encode a needed element type. This is also
172 used for inline assembly.
174 Note that some of the methods mentioned above only exist to support both typed
175 and opaque pointers at the same time, and will be dropped once the migration
176 has completed. For example, ``isOpaqueOrPointeeTypeEquals()`` becomes
177 meaningless once all pointers are opaque.
179 While direct usage of pointer element types is immediately apparent in code,
180 there is a more subtle issue that opaque pointers need to contend with: A lot
181 of code assumes that pointer equality also implies that the used load/store
182 type or GEP source element type is the same. Consider the following examples
183 with typed an opaque pointers:
187 define i32 @test(i32* %p) {
189 %bc = bitcast i32* %p to i64*
190 %v = load i64, i64* %bc
194 define i32 @test(ptr %p) {
196 %v = load i64, ptr %p
200 Without opaque pointers, a check that the pointer operand of the load and
201 store are the same also ensures that the accessed type is the same. Using a
202 different type requires a bitcast, which will result in distinct pointer
205 With opaque pointers, the bitcast is not present, and this check is no longer
206 sufficient. In the above example, it could result in store to load forwarding
207 of an incorrect type. Code making such assumptions needs to be adjusted to
208 check the accessed type explicitly:
209 ``LI->getType() == SI->getValueOperand()->getType()``.
214 Frontends need to be adjusted to track pointee types independently of LLVM,
215 insofar as they are necessary for lowering. For example, clang now tracks the
216 pointee type in the ``Address`` structure.
218 Frontends using the C API through an FFI interface should be aware that a
219 number of C API functions are deprecated and will be removed as part of the
220 opaque pointer transition::
222 LLVMBuildLoad -> LLVMBuildLoad2
223 LLVMBuildCall -> LLVMBuildCall2
224 LLVMBuildInvoke -> LLVMBuildInvoke2
225 LLVMBuildGEP -> LLVMBuildGEP2
226 LLVMBuildInBoundsGEP -> LLVMBuildInBoundsGEP2
227 LLVMBuildStructGEP -> LLVMBuildStructGEP2
228 LLVMBuildPtrDiff -> LLVMBuildPtrDiff2
229 LLVMConstGEP -> LLVMConstGEP2
230 LLVMConstInBoundsGEP -> LLVMConstInBoundsGEP2
231 LLVMAddAlias -> LLVMAddAlias2
233 Additionally, it will no longer be possible to call ``LLVMGetElementType()``
236 It is possible to control whether opaque pointers are used (if you want to
237 override the default) using ``LLVMContext::setOpaquePointers``.
239 Temporarily disabling opaque pointers
240 =====================================
242 In LLVM 15, opaque pointers are enabled by default, but it it still possible to
243 use typed pointers using a number of opt-in flags.
245 For users of the clang driver interface, it is possible to temporarily restore
246 the old default using the ``-DCLANG_ENABLE_OPAQUE_POINTERS=OFF`` cmake option,
247 or by passing ``-Xclang -no-opaque-pointers`` to a single clang invocation.
249 For users of the clang cc1 interface, ``-no-opaque-pointers`` can be passed.
250 Note that the ``CLANG_ENABLE_OPAQUE_POINTERS`` cmake option has no effect on
253 Usage for LTO can be disabled by passing ``-Wl,-plugin-opt=no-opaque-pointers``
256 For users of LLVM as a library, opaque pointers can be disabled by calling
257 ``setOpaquePointers(false)`` on the ``LLVMContext``.
259 For users of LLVM tools like opt, opaque pointers can be disabled by passing
260 ``-opaque-pointers=0``.
265 **LLVM 14:** Supports all necessary APIs for migrating to opaque pointers and deprecates/removes incompatible APIs. However, using opaque pointers in the optimization pipeline is **not** fully supported. This release can be used to make out-of-tree code compatible with opaque pointers, but opaque pointers should **not** be enabled in production.
267 **LLVM 15:** Opaque pointers are enabled by default. Typed pointers are still
270 **LLVM 16:** Opaque pointers are enabled by default. Typed pointers are
271 supported on a best-effort basis only and not tested.
273 **LLVM 17:** Only opaque pointers are supported. Typed pointers are not
281 Typed pointers are **not** supported on the ``main`` branch as a matter of
282 policy. Fixes for typed pointer support are not accepted. Typed pointer
283 support code may be removed without notice at any time.
285 However, tests are still in the process of being converted to opaque pointers.
286 As such, care must be taken when actively removing typed pointer support, to
287 avoid breaking remaining tests.
289 The following typed pointer functionality has already been removed:
291 * The ``CLANG_ENABLE_OPAQUE_POINTERS`` cmake flag is no longer supported.
292 * The ``-no-opaque-pointers`` cc1 clang flag is no longer supported.
293 * The ``-plugin-opt=no-opaque-pointers`` LTO flag is no longer supported.
294 * C APIs that do not support opaque pointers (like ``LLVMBuildLoad``) are no
296 * Typed pointer IR and bitcode is implicitly upgraded to use opaque pointers,
297 unless ``-opaque-pointers=0`` is passed.
299 The following typed pointer functionality is still to be removed:
301 * The ``-opaque-pointers=0`` opt flag.
302 * Support for typed pointers in LLVM libraries.