Skip to content

Commit 1388681

Browse files
committed
Expand some documentation with LangRef semantics
1 parent eac2342 commit 1388681

File tree

1 file changed

+127
-7
lines changed

1 file changed

+127
-7
lines changed

Sources/LLVM/IRBuilder.swift

Lines changed: 127 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -838,10 +838,19 @@ public class IRBuilder {
838838
/// Build a branch table that branches on the given value with the given
839839
/// default basic block.
840840
///
841-
/// The switch instruction is used to transfer control flow to one of
842-
/// several different places. It is a generalization of the ‘br‘ instruction,
841+
/// The `switch` instruction is used to transfer control flow to one of
842+
/// several different places. It is a generalization of the `br` instruction,
843843
/// allowing a branch to occur to one of many possible destinations.
844844
///
845+
/// This function returns a value that acts as a representation of the branch
846+
/// table for the `switch` instruction. When the `switch` instruction is
847+
/// executed, this table is searched for the given value. If the value is
848+
/// found, control flow is transferred to the corresponding destination;
849+
/// otherwise, control flow is transferred to the default destination
850+
/// specified by the `else` block.
851+
///
852+
/// To add branches to the `switch` table, see `Switch.addCase(_:_:)`.
853+
///
845854
/// - parameter value: The value to compare.
846855
/// - parameter else: The default destination for control flow should the
847856
/// value not match a case in the branch table.
@@ -888,6 +897,12 @@ public class IRBuilder {
888897

889898
/// Build an unconditional branch to the given basic block.
890899
///
900+
/// The `br` instruction is used to cause control flow to transfer to a
901+
/// different basic block in the current function. There are two forms of this
902+
/// instruction, corresponding to a conditional branch and an unconditional
903+
/// branch. To build a conditional branch, see
904+
/// `buildCondBr(condition:then:`else`:)`.
905+
///
891906
/// - parameter block: The target block to transfer control flow to.
892907
///
893908
/// - returns: A value representing `void`.
@@ -899,6 +914,11 @@ public class IRBuilder {
899914
/// Build a condition branch that branches to the first basic block if the
900915
/// provided condition is `true`, otherwise to the second basic block.
901916
///
917+
/// The `br` instruction is used to cause control flow to transfer to a
918+
/// different basic block in the current function. There are two forms of this
919+
/// instruction, corresponding to a conditional branch and an unconditional
920+
/// branch. To build an unconditional branch, see `buildBr(_:)`.
921+
///
902922
/// - parameter condition: A value of type `i1` that determines which basic
903923
/// block to transfer control flow to.
904924
/// - parameter then: The basic block to transfer control flow to if the
@@ -914,6 +934,14 @@ public class IRBuilder {
914934

915935
/// Build an indirect branch to a label within the current function.
916936
///
937+
/// The `indirectbr` instruction implements an indirect branch to a label
938+
/// within the current function, whose address is specified by the `address`
939+
/// parameter.
940+
///
941+
/// All possible destination blocks must be listed in the `destinations` list,
942+
/// otherwise this instruction has undefined behavior. This implies that jumps
943+
/// to labels defined in other functions have undefined behavior as well.
944+
///
917945
/// - parameter address: The address of the label to branch to.
918946
/// - parameter destinations: The set of possible destinations the address may
919947
/// point to. The same block may appear multiple times in this list, though
@@ -934,6 +962,13 @@ public class IRBuilder {
934962
/// Build a return from the current function back to the calling function
935963
/// with the given value.
936964
///
965+
/// Returning a value with a type that does not correspond to the return
966+
/// type of the current function is a fatal condition.
967+
///
968+
/// There are two forms of the `ret` instruction: one that returns a value and
969+
/// then causes control flow, and one that just causes control flow to occur.
970+
/// To build the `ret` that does not return a value use `buildRetVoid()`.
971+
///
937972
/// - parameter val: The value to return from the current function.
938973
///
939974
/// - returns: A value representing `void`.
@@ -942,15 +977,27 @@ public class IRBuilder {
942977
return LLVMBuildRet(llvm, val.asLLVM())
943978
}
944979

945-
/// Builds a void return from the current function.
980+
/// Build a void return from the current function.
981+
///
982+
/// If the current function does not have a `Void` return value, failure to
983+
/// return a falue is a fatal condition.
984+
///
985+
/// There are two forms of the `ret` instruction: one that returns a value and
986+
/// then causes control flow, and one that just causes control flow to occur.
987+
/// To build the `ret` that returns a value use `buildRet(_:)`.
946988
///
947989
/// - returns: A value representing `void`.
948990
@discardableResult
949991
public func buildRetVoid() -> IRValue {
950992
return LLVMBuildRetVoid(llvm)
951993
}
952994

953-
/// Builds an unreachable instruction in the current function.
995+
/// Build an unreachable instruction in the current function.
996+
///
997+
/// The `unreachable` instruction has no defined semantics. This instruction
998+
/// is used to inform the optimizer that a particular portion of the code is
999+
/// not reachable. This can be used to indicate that the code after a
1000+
/// no-return function cannot be reached, and other facts.
9541001
///
9551002
/// - returns: A value representing `void`.
9561003
@discardableResult
@@ -999,6 +1046,10 @@ public class IRBuilder {
9991046
/// mechanism, control is interrupted and continued at the dynamically nearest
10001047
/// `exception` label.
10011048
///
1049+
/// The `catch` block is a landing pad for the exception. As such, the first
1050+
/// instruction of that block is required to be the `landingpad` instruction,
1051+
/// which contains the information about the behavior of the program after
1052+
/// unwinding happens.
10021053
///
10031054
/// - parameter fn: The function to invoke.
10041055
/// - parameter args: A list of arguments.
@@ -1069,7 +1120,7 @@ public class IRBuilder {
10691120
///
10701121
/// When all cleanups are finished, if an exception is not handled by the
10711122
/// current function, unwinding resumes by calling the resume instruction,
1072-
/// passing in the result of the `landingpad` instruction for the original
1123+
/// passing in the result of the `landingpad` instruction for the original
10731124
/// landing pad.
10741125
///
10751126
/// - parameter: A value representing the result of the original landing pad.
@@ -1100,14 +1151,32 @@ public class IRBuilder {
11001151
/// Build an `alloca` to allocate stack memory to hold a value of the given
11011152
/// type.
11021153
///
1154+
/// The `alloca` instruction allocates `sizeof(<type>)*count` bytes of
1155+
/// memory on the runtime stack, returning a pointer of the appropriate type
1156+
/// to the program. If `count` is specified, it is the number of elements
1157+
/// allocated, otherwise `count` is defaulted to be one. If a constant
1158+
/// alignment is specified, the value result of the allocation is guaranteed
1159+
/// to be aligned to at least that boundary. The alignment may not be
1160+
/// greater than `1 << 29`. If not specified, or if zero, the target can
1161+
/// choose to align the allocation on any convenient boundary compatible with
1162+
/// the type.
1163+
///
11031164
/// - parameter type: The sized type used to determine the amount of stack
11041165
/// memory to allocate.
1166+
/// - parameter count: An optional number of slots to allocate, to simulate a
1167+
/// C array.
11051168
/// - parameter alignment: The alignment of the access.
11061169
/// - parameter name: The name for the newly inserted instruction.
11071170
///
11081171
/// - returns: A value representing `void`.
1109-
public func buildAlloca(type: IRType, alignment: Int = 0, name: String = "") -> IRValue {
1110-
let allocaInst = LLVMBuildAlloca(llvm, type.asLLVM(), name)!
1172+
public func buildAlloca(type: IRType, count: IRValue? = nil,
1173+
alignment: Int = 0, name: String = "") -> IRValue {
1174+
let allocaInst: LLVMValueRef
1175+
if let count = count {
1176+
allocaInst = LLVMBuildArrayAlloca(llvm, type.asLLVM(), count.asLLVM(), name)
1177+
} else {
1178+
allocaInst = LLVMBuildAlloca(llvm, type.asLLVM(), name)!
1179+
}
11111180
LLVMSetAlignment(allocaInst, UInt32(alignment))
11121181
return allocaInst
11131182
}
@@ -1210,6 +1279,12 @@ public class IRBuilder {
12101279
/// Build an ExtractValue instruction to retrieve an indexed value from a
12111280
/// struct or array value.
12121281
///
1282+
/// `extractvalue` function like a GEP, but has different indexing semantics:
1283+
///
1284+
/// - Since the value being indexed is not a pointer, the first index is
1285+
/// omitted and assumed to be zero.
1286+
/// - Not only struct indices but also array indices must be in bounds.
1287+
///
12131288
/// - parameter value: The struct or array you're indexing into.
12141289
/// - parameter index: The index at which to extract.
12151290
///
@@ -1276,6 +1351,14 @@ public class IRBuilder {
12761351
/// Build a bitcast instruction to convert the given value to a value of the
12771352
/// given type by just copying the bit pattern.
12781353
///
1354+
/// The `bitcast` instruction is always a no-op cast because no bits change
1355+
/// with this conversion. The conversion is done as if the value had been
1356+
/// stored to memory and read back as the given type. Pointer (or vector of
1357+
/// pointer) types may only be converted to other pointer (or vector of
1358+
/// pointer) types with the same address space through this instruction. To
1359+
/// convert pointers to other types, see `buildIntToPtr(_:type:name:)` or
1360+
/// `buildPtrToInt(_:type:name:)`.
1361+
///
12791362
/// - parameter val: The value to bitcast.
12801363
/// - parameter type: The destination type.
12811364
/// - parameter name: The name for the newly inserted instruction.
@@ -1302,6 +1385,12 @@ public class IRBuilder {
13021385
/// Build an address space cast instruction that converts a pointer value
13031386
/// to a given type in a different address space.
13041387
///
1388+
/// The `addrspacecast` instruction can be a no-op cast or a complex value
1389+
/// modification, depending on the target and the address space pair. Pointer
1390+
/// conversions within the same address space must be performed with the
1391+
/// `bitcast` instruction. Note that if the address space conversion is legal
1392+
/// then both result and operand refer to the same memory location.
1393+
///
13051394
/// The address spaces of the value and the destination pointer types must
13061395
/// be distinct.
13071396
public func buildAddrSpaceCast(_ val: IRValue, type: IRType, name: String = "") -> IRValue {
@@ -1350,6 +1439,13 @@ public class IRBuilder {
13501439
/// Build an integer-to-pointer instruction to convert the given value to the
13511440
/// given pointer type.
13521441
///
1442+
/// The `inttoptr` instruction converts the given value to the given pointer
1443+
/// type by applying either a zero extension or a truncation depending on the
1444+
/// size of the integer value. If value is larger than the size of a pointer
1445+
/// then a truncation is done. If value is smaller than the size of a pointer
1446+
/// then a zero extension is done. If they are the same size, nothing is done
1447+
/// (no-op cast).
1448+
///
13531449
/// - parameter val: The integer value.
13541450
/// - parameter type: The destination pointer type.
13551451
/// - parameter name: The name for the newly inserted instruction.
@@ -1363,6 +1459,14 @@ public class IRBuilder {
13631459
/// Build a pointer-to-integer instruction to convert the given pointer value
13641460
/// to the given integer type.
13651461
///
1462+
/// The `ptrtoint` instruction converts the given pointer value to the given
1463+
/// integer type by interpreting the pointer value as an integer and either
1464+
/// truncating or zero extending that value to the size of the integer type.
1465+
/// If the pointer value is smaller than the integer type then a zero
1466+
/// extension is done. If the pointer value is larger than the integer type
1467+
/// then a truncation is done. If they are the same size, then nothing is done
1468+
/// (no-op cast) other than a type change.
1469+
///
13661470
/// - parameter val: The pointer value.
13671471
/// - parameter type: The destination integer type.
13681472
/// - parameter name: The name for the newly inserted instruction.
@@ -1457,6 +1561,22 @@ public class IRBuilder {
14571561
/// Build a fence instruction that introduces "happens-before" edges between
14581562
/// operations.
14591563
///
1564+
/// A fence `A` which has (at least) `release` ordering semantics synchronizes
1565+
/// with a fence `B` with (at least) `acquire` ordering semantics if and only
1566+
/// if there exist atomic operations X and Y, both operating on some atomic
1567+
/// object `M`, such that `A` is sequenced before `X`, `X` modifies `M`
1568+
/// (either directly or through some side effect of a sequence headed by `X`),
1569+
/// `Y` is sequenced before `B`, and `Y` observes `M`. This provides a
1570+
/// happens-before dependency between `A` and `B`. Rather than an explicit
1571+
/// fence, one (but not both) of the atomic operations `X` or `Y` might
1572+
/// provide a release or acquire (resp.) ordering constraint and still
1573+
/// synchronize-with the explicit fence and establish the happens-before edge.
1574+
///
1575+
/// A fence which has `sequentiallyConsistent` ordering, in addition to having
1576+
/// both `acquire` and `release` semantics specified above, participates in
1577+
/// the global program order of other `sequentiallyConsistent` operations
1578+
/// and/or fences.
1579+
///
14601580
/// - parameter ordering: Defines the kind of "synchronizes-with" edge this
14611581
/// fence adds.
14621582
/// - parameter singleThreaded: Specifies that the fence only synchronizes

0 commit comments

Comments
 (0)