Skip to content

Commit 15f1656

Browse files
authored
Merge pull request #210 from TristanBurnside/StructGEP2
Migrate GEPs to use opaque pointer compatable APIs
2 parents 7c14d64 + 7802214 commit 15f1656

File tree

1 file changed

+84
-1
lines changed

1 file changed

+84
-1
lines changed

Sources/LLVM/IRBuilder.swift

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1090,14 +1090,36 @@ extension IRBuilder {
10901090
///
10911091
/// - returns: A value representing the result of a load from the given
10921092
/// pointer value.
1093+
@available(*, deprecated, message: "Use buildLoad(type:ptr:ordering:volatile:alignment:name) instead")
10931094
public func buildLoad(_ ptr: IRValue, ordering: AtomicOrdering = .notAtomic, volatile: Bool = false, alignment: Alignment = .zero, name: String = "") -> IRInstruction {
10941095
let loadInst = LLVMBuildLoad(llvm, ptr.asLLVM(), name)!
10951096
LLVMSetOrdering(loadInst, ordering.llvm)
10961097
LLVMSetVolatile(loadInst, volatile.llvm)
10971098
LLVMSetAlignment(loadInst, alignment.rawValue)
10981099
return loadInst
10991100
}
1100-
1101+
1102+
/// Build a load instruction that loads a value from the location in the
1103+
/// given value.
1104+
///
1105+
/// - parameter ptr: The pointer value to load from.
1106+
/// - parameter type: The type of value loaded from the given pointer.
1107+
/// - parameter ordering: The ordering effect of the fence for this load,
1108+
/// if any. Defaults to a nonatomic load.
1109+
/// - parameter volatile: Whether this is a load from a volatile memory location.
1110+
/// - parameter alignment: The alignment of the access.
1111+
/// - parameter name: The name for the newly inserted instruction.
1112+
///
1113+
/// - returns: A value representing the result of a load from the given
1114+
/// pointer value.
1115+
public func buildLoad(_ ptr: IRValue, type: IRType, ordering: AtomicOrdering = .notAtomic, volatile: Bool = false, alignment: Alignment = .zero, name: String = "") -> IRInstruction {
1116+
let loadInst = LLVMBuildLoad2(llvm, type.asLLVM(), ptr.asLLVM(), name)!
1117+
LLVMSetOrdering(loadInst, ordering.llvm)
1118+
LLVMSetVolatile(loadInst, volatile.llvm)
1119+
LLVMSetAlignment(loadInst, alignment.rawValue)
1120+
return loadInst
1121+
}
1122+
11011123
/// Build a `GEP` (Get Element Pointer) instruction with a resultant value
11021124
/// that is undefined if the address is outside the actual underlying
11031125
/// allocated object and not the address one-past-the-end.
@@ -1113,12 +1135,36 @@ extension IRBuilder {
11131135
///
11141136
/// - returns: A value representing the address of a subelement of the given
11151137
/// aggregate data structure value.
1138+
@available(*, deprecated, message: "Use buildInBoundsGEP(type:ptr:indices:name) instead")
11161139
public func buildInBoundsGEP(_ ptr: IRValue, indices: [IRValue], name: String = "") -> IRValue {
11171140
var vals = indices.map { $0.asLLVM() as Optional }
11181141
return vals.withUnsafeMutableBufferPointer { buf in
11191142
return LLVMBuildInBoundsGEP(llvm, ptr.asLLVM(), buf.baseAddress, UInt32(buf.count), name)
11201143
}
11211144
}
1145+
1146+
/// Build a `GEP` (Get Element Pointer) instruction with a resultant value
1147+
/// that is undefined if the address is outside the actual underlying
1148+
/// allocated object and not the address one-past-the-end.
1149+
///
1150+
/// The `GEP` instruction is often the source of confusion. LLVM [provides a
1151+
/// document](http://llvm.org/docs/GetElementPtr.html) to answer questions
1152+
/// around its semantics and correct usage.
1153+
///
1154+
/// - parameter ptr: The base address for the index calculation.
1155+
/// - parameter type: The type used to calculate pointer offsets.
1156+
/// - parameter indices: A list of indices that indicate which of the elements
1157+
/// of the aggregate object are indexed.
1158+
/// - parameter name: The name for the newly inserted instruction.
1159+
///
1160+
/// - returns: A value representing the address of a subelement of the given
1161+
/// aggregate data structure value.
1162+
public func buildInBoundsGEP(_ ptr: IRValue, type: IRType, indices: [IRValue], name: String = "") -> IRValue {
1163+
var vals = indices.map { $0.asLLVM() as Optional }
1164+
return vals.withUnsafeMutableBufferPointer { buf in
1165+
return LLVMBuildInBoundsGEP2(llvm, type.asLLVM(), ptr.asLLVM(), buf.baseAddress, UInt32(buf.count), name)
1166+
}
1167+
}
11221168

11231169
/// Build a GEP (Get Element Pointer) instruction.
11241170
///
@@ -1133,13 +1179,35 @@ extension IRBuilder {
11331179
///
11341180
/// - returns: A value representing the address of a subelement of the given
11351181
/// aggregate data structure value.
1182+
@available(*, deprecated, message: "Use buildGEP(type:ptr:indices:name) instead")
11361183
public func buildGEP(_ ptr: IRValue, indices: [IRValue], name: String = "") -> IRValue {
11371184
var vals = indices.map { $0.asLLVM() as Optional }
11381185
return vals.withUnsafeMutableBufferPointer { buf in
11391186
return LLVMBuildGEP(llvm, ptr.asLLVM(), buf.baseAddress, UInt32(buf.count), name)
11401187
}
11411188
}
11421189

1190+
/// Build a GEP (Get Element Pointer) instruction.
1191+
///
1192+
/// The `GEP` instruction is often the source of confusion. LLVM [provides a
1193+
/// document](http://llvm.org/docs/GetElementPtr.html) to answer questions
1194+
/// around its semantics and correct usage.
1195+
///
1196+
/// - parameter ptr: The base address for the index calculation.
1197+
/// - parameter type: The type used to calculate pointer offsets.
1198+
/// - parameter indices: A list of indices that indicate which of the elements
1199+
/// of the aggregate object are indexed.
1200+
/// - parameter name: The name for the newly inserted instruction.
1201+
///
1202+
/// - returns: A value representing the address of a subelement of the given
1203+
/// aggregate data structure value.
1204+
public func buildGEP(_ ptr: IRValue, type: IRType, indices: [IRValue], name: String = "") -> IRValue {
1205+
var vals = indices.map { $0.asLLVM() as Optional }
1206+
return vals.withUnsafeMutableBufferPointer { buf in
1207+
return LLVMBuildGEP2(llvm, type.asLLVM(), ptr.asLLVM(), buf.baseAddress, UInt32(buf.count), name)
1208+
}
1209+
}
1210+
11431211
/// Build a GEP (Get Element Pointer) instruction suitable for indexing into
11441212
/// a struct.
11451213
///
@@ -1149,10 +1217,25 @@ extension IRBuilder {
11491217
///
11501218
/// - returns: A value representing the address of a subelement of the given
11511219
/// struct value.
1220+
@available(*, deprecated, message: "Use buildStructGEP(type:ptr:index:name) instead")
11521221
public func buildStructGEP(_ ptr: IRValue, index: Int, name: String = "") -> IRValue {
11531222
return LLVMBuildStructGEP(llvm, ptr.asLLVM(), UInt32(index), name)
11541223
}
11551224

1225+
/// Build a GEP (Get Element Pointer) instruction suitable for indexing into
1226+
/// a struct of a given type.
1227+
///
1228+
/// - parameter ptr: The base address for the index calculation.
1229+
/// - parameter type: The type of the struct to index into.
1230+
/// - parameter index: The offset from the base for the index calculation.
1231+
/// - parameter name: The name for the newly inserted instruction.
1232+
///
1233+
/// - returns: A value representing the address of a subelement of the given
1234+
/// struct value.
1235+
public func buildStructGEP(_ ptr: IRValue, type: IRType, index: Int, name: String = "") -> IRValue {
1236+
return LLVMBuildStructGEP2(llvm, type.asLLVM(), ptr.asLLVM(), UInt32(index), name)
1237+
}
1238+
11561239
/// Build an ExtractValue instruction to retrieve an indexed value from a
11571240
/// struct or array value.
11581241
///

0 commit comments

Comments
 (0)