Skip to content

Commit d581526

Browse files
committed
Fix alignment accessors
Alignment is not a general property of values, only globals, allocas, stores, and loads.
1 parent 0af501a commit d581526

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

Sources/LLVM/IRBuilder.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,11 +1090,14 @@ public class IRBuilder {
10901090
///
10911091
/// - parameter type: The sized type used to determine the amount of stack
10921092
/// memory to allocate.
1093+
/// - parameter alignment: The alignment of the access.
10931094
/// - parameter name: The name for the newly inserted instruction.
10941095
///
10951096
/// - returns: A value representing `void`.
1096-
public func buildAlloca(type: IRType, name: String = "") -> IRValue {
1097-
return LLVMBuildAlloca(llvm, type.asLLVM(), name)
1097+
public func buildAlloca(type: IRType, alignment: Int = 0, name: String = "") -> IRValue {
1098+
let allocaInst = LLVMBuildAlloca(llvm, type.asLLVM(), name)!
1099+
LLVMSetAlignment(allocaInst, UInt32(alignment))
1100+
return allocaInst
10981101
}
10991102

11001103
/// Builds a store instruction that stores the first value into the location
@@ -1105,13 +1108,15 @@ public class IRBuilder {
11051108
/// - parameter ordering: The ordering effect of the fence for this store,
11061109
/// if any. Defaults to a nonatomic store.
11071110
/// - parameter volatile: Whether this is a store to a volatile memory location.
1111+
/// - parameter alignment: The alignment of the access.
11081112
///
11091113
/// - returns: A value representing `void`.
11101114
@discardableResult
1111-
public func buildStore(_ val: IRValue, to ptr: IRValue, ordering: AtomicOrdering = .notAtomic, volatile: Bool = false) -> IRValue {
1115+
public func buildStore(_ val: IRValue, to ptr: IRValue, ordering: AtomicOrdering = .notAtomic, volatile: Bool = false, alignment: Int = 0) -> IRValue {
11121116
let storeInst = LLVMBuildStore(llvm, val.asLLVM(), ptr.asLLVM())!
11131117
LLVMSetOrdering(storeInst, ordering.llvm)
11141118
LLVMSetVolatile(storeInst, volatile.llvm)
1119+
LLVMSetAlignment(storeInst, UInt32(alignment))
11151120
return storeInst
11161121
}
11171122

@@ -1122,14 +1127,16 @@ public class IRBuilder {
11221127
/// - parameter ordering: The ordering effect of the fence for this load,
11231128
/// if any. Defaults to a nonatomic load.
11241129
/// - parameter volatile: Whether this is a load from a volatile memory location.
1130+
/// - parameter alignment: The alignment of the access.
11251131
/// - parameter name: The name for the newly inserted instruction.
11261132
///
11271133
/// - returns: A value representing the result of a load from the given
11281134
/// pointer value.
1129-
public func buildLoad(_ ptr: IRValue, ordering: AtomicOrdering = .notAtomic, volatile: Bool = false, name: String = "") -> IRValue {
1135+
public func buildLoad(_ ptr: IRValue, ordering: AtomicOrdering = .notAtomic, volatile: Bool = false, alignment: Int = 0, name: String = "") -> IRValue {
11301136
let loadInst = LLVMBuildLoad(llvm, ptr.asLLVM(), name)!
11311137
LLVMSetOrdering(loadInst, ordering.llvm)
11321138
LLVMSetVolatile(loadInst, volatile.llvm)
1139+
LLVMSetAlignment(loadInst, UInt32(alignment))
11331140
return loadInst
11341141
}
11351142

Sources/LLVM/IRGlobal.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import cllvm
77
public protocol IRGlobal: IRValue {}
88

99
extension IRGlobal {
10+
/// Retrieves the alignment of this value.
11+
public var alignment: Int {
12+
get { return Int(LLVMGetAlignment(asLLVM())) }
13+
set { LLVMSetAlignment(asLLVM(), UInt32(newValue)) }
14+
}
15+
1016
/// Retrieves the linkage information for this global.
1117
public var linkage: Linkage {
1218
get { return Linkage(llvm: LLVMGetLinkage(asLLVM())) }

Sources/LLVM/IRValue.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ public extension IRValue {
1515
return convertType(LLVMTypeOf(asLLVM()))
1616
}
1717

18-
/// Retrieves the alignment of this value.
19-
public var alignment: Int {
20-
get { return Int(LLVMGetAlignment(asLLVM())) }
21-
set { LLVMSetAlignment(asLLVM(), UInt32(newValue)) }
22-
}
23-
2418
/// Returns whether this value is a constant.
2519
public var isConstant: Bool {
2620
return LLVMIsConstant(asLLVM()) != 0

0 commit comments

Comments
 (0)