Skip to content

Commit a1113b1

Browse files
committed
Improve AA docs
Better describe TBAA tree systems and the broad mechanisms behind a few different alias analysis passes and how they differ.
1 parent 431838a commit a1113b1

File tree

2 files changed

+76
-9
lines changed

2 files changed

+76
-9
lines changed

Sources/LLVM/MDBuilder.swift

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,12 @@ public struct TBAAStructField {
234234
}
235235

236236
extension MDBuilder {
237-
/// Build a metadata node for the root of a TBAA hierarchy with the given name.
237+
/// Build a metadata node for the root of a TBAA hierarchy with the given
238+
/// name.
239+
///
240+
/// The root node of a TBAA hierarchy describes a boundary for a source
241+
/// language's type system. For the purposes of optimization, a TBAA analysis
242+
/// pass must consider ancestors of two different root systems `mayalias`.
238243
///
239244
/// - Parameters:
240245
/// - name: The name of the TBAA root node.
@@ -306,13 +311,51 @@ extension MDBuilder {
306311
/// Builds a TBAA Type Descriptor.
307312
///
308313
/// Type descriptors describe the type system of the higher level language
309-
/// being compiled. Scalar type descriptors describe types that do not
310-
/// contain other types. Each scalar type has a parent type, which must also
311-
/// be a scalar type or the TBAA root. Via this parent relation, scalar types
312-
/// within a TBAA root form a tree. Struct type descriptors denote types that
313-
/// contain a sequence of other type descriptors, at known offsets. These
314-
/// contained type descriptors can either be struct type descriptors
315-
/// themselves or scalar type descriptors.
314+
/// being compiled and come in two variants:
315+
///
316+
/// Scalar Type Descriptors
317+
/// =======================
318+
///
319+
/// Scalar type descriptors describe types that do not contain other types,
320+
/// such as fixed-width integral and floating-point types. A scalar type
321+
/// has a single parent, which is required to be another scalar type or
322+
/// the TBAA root node. For example, in C, `int32_t` would be described be
323+
/// a scalar type node with a parent pointer to `unsigned char` which, in
324+
/// turn, points to the root for C.
325+
///
326+
/// ```
327+
/// +----------+ +------+ +-----------+
328+
/// | | | | | |
329+
/// | uint32_t +---> char +---> TBAA Root |
330+
/// | | | | | |
331+
/// +----------+ +------+ +-----------+
332+
/// ```
333+
///
334+
/// Struct Type Descriptors
335+
/// =======================
336+
///
337+
/// Struct type descriptors describe types that contain a sequence of other
338+
/// type descriptors, at known offsets, as fields. These field type
339+
/// descriptors can either be struct type descriptors themselves or scalar
340+
/// type descriptors.
341+
///
342+
/// ```
343+
/// +----------+
344+
/// | |
345+
/// +-------> uint32_t +----+
346+
/// | | | |
347+
/// | +----------+ |
348+
/// +------------+ +---v--+ +-----------+
349+
/// | | | | | |
350+
/// | SomeStruct | | char +---> TBAA Root |
351+
/// | | | | | |
352+
/// +------------+ +---^--+ +-----------+
353+
/// | +-------+ |
354+
/// | | | |
355+
/// +----------> float +----+
356+
/// | |
357+
/// +-------+
358+
/// ```
316359
///
317360
/// - Parameters:
318361
/// - parent: The parent type node of this type node or the TBAA root node

Sources/LLVM/PassManager.swift

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,36 @@ public enum Pass {
134134
case earlyCSE
135135
/// Removes `llvm.expect` intrinsics and creates "block_weights" metadata.
136136
case lowerExpectIntrinsic
137-
/// Adds metadata to LLVM IR types and performs metadata-based TBAA.
137+
/// Adds metadata to LLVM IR types and performs metadata-based
138+
/// Type-Based Alias Analysis (TBAA).
139+
///
140+
/// TBAA requires that two pointers to objects of different types must never
141+
/// alias. Because memory in LLVM is typeless, TBAA is performed using
142+
/// special metadata nodes describing aliasing relationships between types
143+
/// in the source language(s).
144+
///
145+
/// To construct this metadata, see `MDBuilder`.
138146
case typeBasedAliasAnalysis
139147
/// Adds metadata to LLVM IR types and performs metadata-based scoped no-alias
140148
/// analysis.
141149
case scopedNoAliasAA
142150
/// LLVM's primary stateless and local alias analysis.
151+
///
152+
/// Given a pointer value, walk the use-def chain to find out how that
153+
/// pointer is used. The traversal terminates at global variables and
154+
/// aliases, stack allocations, and values of non-pointer types - referred
155+
/// to as "underlying objects". Analysis may reach multiple underlying object
156+
/// values because of branching control flow. If the set of underlying
157+
/// objects for one pointer has a non-empty intersection with another, those
158+
/// two pointers are deemed `mayalias`. Else, an empty intersection deems
159+
/// those pointers `noalias`.
160+
///
161+
/// Basic Alias Analysis should generally be scheduled ahead of other
162+
/// AA passes. This is because it is more conservative than other passes
163+
/// about declaring two pointers `mustalias`, and does so fairly efficiently.
164+
/// For example, loads through two members of a union with distinct types are
165+
/// declared by TBAA to be `noalias`, where BasicAA considers them
166+
/// `mustalias`.
143167
case basicAliasAnalysis
144168
/// Performs alias and mod/ref analysis for internal global values that
145169
/// do not have their address taken.

0 commit comments

Comments
 (0)