Skip to content

Commit f2caa21

Browse files
Merge pull request #227 from matthewseaman/llvm-11-tests
Fix Unit tests under LLVM 11
2 parents 628666d + a4e58aa commit f2caa21

File tree

10 files changed

+66
-33
lines changed

10 files changed

+66
-33
lines changed

Sources/LLVM/DIBuilder.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ extension DIBuilder {
394394
named name: String,
395395
scope: DIScope, file: FileMetadata, line: Int,
396396
type: DIType, alwaysPreserve: Bool = false,
397-
flags: DIFlags = [], alignment: Alignment = .zero
397+
flags: DIFlags = [], alignment: Alignment
398398
) -> LocalVariableMetadata {
399399
let radix = UInt32(self.module.dataLayout.intPointerType().width)
400400
guard let variable = LLVMDIBuilderCreateAutoVariable(
@@ -502,7 +502,7 @@ extension DIBuilder {
502502
/// - file: File where this member is defined.
503503
/// - line: Line number.
504504
/// - size: Member size.
505-
/// - alignmemnt: Member alignment.
505+
/// - alignment: Member alignment.
506506
/// - elements: Enumeration elements.
507507
/// - numElements: Number of enumeration elements.
508508
/// - underlyingType: Underlying type of a C++11/ObjC fixed enum.
@@ -675,7 +675,7 @@ extension DIBuilder {
675675
/// - addressSpace: The address space the pointer type reside in.
676676
/// - name: The name of the pointer type.
677677
public func buildPointerType(
678-
pointee: DIType, size: Size, alignment: Alignment = .zero,
678+
pointee: DIType, size: Size, alignment: Alignment,
679679
addressSpace: AddressSpace = .zero, name: String = ""
680680
) -> DIType {
681681
let radix = UInt32(self.module.dataLayout.intPointerType().width)
@@ -1258,15 +1258,14 @@ extension DIBuilder {
12581258
/// - expression: The location of the global relative to the attached
12591259
/// GlobalVariable.
12601260
/// - declaration: Reference to the corresponding declaration.
1261-
/// - alignment: Variable alignment(or 0 if no alignment attr was
1262-
/// specified)
1261+
/// - alignment: Variable alignment
12631262
public func buildGlobalExpression(
12641263
named name: String, linkageName: String, type: DIType,
12651264
scope: DIScope, file: FileMetadata, line: Int,
12661265
isLocal: Bool = true,
12671266
expression: ExpressionMetadata? = nil,
12681267
declaration: IRMetadata? = nil,
1269-
alignment: Alignment = .zero
1268+
alignment: Alignment
12701269
) -> ExpressionMetadata {
12711270
let radix = UInt32(self.module.dataLayout.intPointerType().width)
12721271
guard let ty = LLVMDIBuilderCreateGlobalVariableExpression(

Sources/LLVM/IRBuilder.swift

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,9 +1034,8 @@ extension IRBuilder {
10341034
/// allocated, otherwise `count` is defaulted to be one. If a constant
10351035
/// alignment is specified, the value result of the allocation is guaranteed
10361036
/// to be aligned to at least that boundary. The alignment may not be
1037-
/// greater than `1 << 29`. If not specified, or if zero, the target can
1038-
/// choose to align the allocation on any convenient boundary compatible with
1039-
/// the type.
1037+
/// greater than `1 << 29`. If not specified, or if zero, the target will
1038+
/// choose a default value that is convenient and compatible with the type.
10401039
///
10411040
/// The returned value is allocated in the address space specified in the data layout string for the target. If
10421041
/// no such value is specified, the value is allocated in the default address space.
@@ -1057,13 +1056,18 @@ extension IRBuilder {
10571056
} else {
10581057
allocaInst = LLVMBuildAlloca(llvm, type.asLLVM(), name)!
10591058
}
1060-
LLVMSetAlignment(allocaInst, alignment.rawValue)
1059+
if !alignment.isZero {
1060+
LLVMSetAlignment(allocaInst, alignment.rawValue)
1061+
}
10611062
return allocaInst
10621063
}
10631064

10641065
/// Build a store instruction that stores the first value into the location
10651066
/// given in the second value.
10661067
///
1068+
/// If alignment is not specified, or if zero, the target will choose a default
1069+
/// value that is convenient and compatible with the type.
1070+
///
10671071
/// - parameter val: The source value.
10681072
/// - parameter ptr: The destination pointer to store into.
10691073
/// - parameter ordering: The ordering effect of the fence for this store,
@@ -1077,13 +1081,18 @@ extension IRBuilder {
10771081
let storeInst = LLVMBuildStore(llvm, val.asLLVM(), ptr.asLLVM())!
10781082
LLVMSetOrdering(storeInst, ordering.llvm)
10791083
LLVMSetVolatile(storeInst, volatile.llvm)
1080-
LLVMSetAlignment(storeInst, alignment.rawValue)
1084+
if !alignment.isZero {
1085+
LLVMSetAlignment(storeInst, alignment.rawValue)
1086+
}
10811087
return storeInst
10821088
}
10831089

10841090
/// Build a load instruction that loads a value from the location in the
10851091
/// given value.
10861092
///
1093+
/// If alignment is not specified, or if zero, the target will choose a default
1094+
/// value that is convenient and compatible with the type.
1095+
///
10871096
/// - parameter ptr: The pointer value to load from.
10881097
/// - parameter type: The type of value loaded from the given pointer.
10891098
/// - parameter ordering: The ordering effect of the fence for this load,
@@ -1098,7 +1107,9 @@ extension IRBuilder {
10981107
let loadInst = LLVMBuildLoad2(llvm, type.asLLVM(), ptr.asLLVM(), name)!
10991108
LLVMSetOrdering(loadInst, ordering.llvm)
11001109
LLVMSetVolatile(loadInst, volatile.llvm)
1101-
LLVMSetAlignment(loadInst, alignment.rawValue)
1110+
if !alignment.isZero {
1111+
LLVMSetAlignment(loadInst, alignment.rawValue)
1112+
}
11021113
return loadInst
11031114
}
11041115

@@ -1927,6 +1938,9 @@ extension IRBuilder {
19271938
/// Build a load instruction that loads a value from the location in the
19281939
/// given value.
19291940
///
1941+
/// If alignment is not specified, or if zero, the target will choose a default
1942+
/// value that is convenient and compatible with the type.
1943+
///
19301944
/// - parameter ptr: The pointer value to load from.
19311945
/// - parameter ordering: The ordering effect of the fence for this load,
19321946
/// if any. Defaults to a nonatomic load.
@@ -1941,7 +1955,9 @@ extension IRBuilder {
19411955
let loadInst = LLVMBuildLoad(llvm, ptr.asLLVM(), name)!
19421956
LLVMSetOrdering(loadInst, ordering.llvm)
19431957
LLVMSetVolatile(loadInst, volatile.llvm)
1944-
LLVMSetAlignment(loadInst, alignment.rawValue)
1958+
if !alignment.isZero {
1959+
LLVMSetAlignment(loadInst, alignment.rawValue)
1960+
}
19451961
return loadInst
19461962
}
19471963

Sources/LLVM/Module.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ import cllvm
1111
/// together with the LLVM linker, which merges function (and global variable)
1212
/// definitions, resolves forward declarations, and merges symbol table entries.
1313
///
14+
/// Creating a Module
15+
/// ==================
16+
///
17+
/// A module can be created using `init(name:context:)`.
18+
/// Note that the default target triple is bare metal and there is no default data layout.
19+
/// If you require these to be specified (e.g. to increase the correctness of default alignment values),
20+
/// be sure to set them yourself.
21+
///
22+
/// if let machine = try? TargetMachine() {
23+
/// module.targetTriple = machine.triple
24+
/// module.dataLayout = machine.dataLayout
25+
/// }
26+
///
1427
/// Verifying a Module
1528
/// ==================
1629
///

Sources/LLVM/Units.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,17 @@ public struct Alignment: Comparable, Hashable {
2727
///
2828
/// An n-byte alignment contains log-base-two-many least-significant zeros.
2929
public func log2() -> UInt32 {
30+
guard !isZero else { return 0 }
3031
return 31 - UInt32(self.rawValue.leadingZeroBitCount)
3132
}
3233

3334
/// Returns the log-base-two value of this alignment as a 64-bit integer.
3435
///
3536
/// An n-byte alignment contains log-base-two-many least-significant zeros.
3637
public func log2() -> UInt64 {
37-
return 63 - UInt64(self.rawValue.leadingZeroBitCount)
38+
guard !isZero else { return 0 }
39+
// rawValue is only 32 bits
40+
return 31 - UInt64(self.rawValue.leadingZeroBitCount)
3841
}
3942

4043
/// Returns the alignment of a pointer which points to the given number of

Tests/LLVMTests/BFC.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,13 @@ private func compileProgramBody(
261261
encoding: .signed, flags: [],
262262
size: builder.module.dataLayout.abiSize(of: cellType))
263263
let diPtrTy = dibuilder.buildPointerType(pointee: diDataTy,
264-
size: builder.module.dataLayout.pointerSize())
264+
size: builder.module.dataLayout.pointerSize(),
265+
alignment: .one)
265266
let diVariable = dibuilder.buildLocalVariable(named: "this",
266267
scope: entryScope,
267268
file: file, line: startPoint.0,
268-
type: diPtrTy, flags: .artificial)
269+
type: diPtrTy, flags: .artificial,
270+
alignment: .one)
269271

270272
var sourceLine = startPoint.0 + 1
271273
var sourceColumn = startPoint.1 + 1

Tests/LLVMTests/DIBuilderSpec.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class DIBuilderSpec : XCTestCase {
4747
// DIEXPRESSION: !{{[0-9]+}} = !DIGlobalVariableExpression(var: !{{[0-9]+}}, expr: !{{[0-9]+}})
4848
let expr = debugBuilder.buildGlobalExpression(
4949
named: "global", linkageName: "global", type: globalTy,
50-
scope: cu, file: file, line: 42)
50+
scope: cu, file: file, line: 42, alignment: global.alignment)
5151
// DIEXPRESSION: !{{[0-9]+}} = distinct !DIGlobalVariable(name: "unattached", linkageName: "unattached", scope: !0, file: !{{[0-9]+}}, line: 42, type: !{{[0-9]+}}, isLocal: true, isDefinition: true)
5252
_ = debugBuilder.buildGlobalExpression(
5353
named: "unattached", linkageName: "unattached",

Tests/LLVMTests/IRAttributesSpec.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class IRAttributesSpec : XCTestCase {
1414
type: FunctionType([IntType.int32, IntType.int32],
1515
IntType.int32))
1616

17-
// FNATTR: define i32 @fn(i32, i32) #0 {
17+
// FNATTR: define i32 @fn(i32 %0, i32 %1) #0 {
1818
fn.addAttribute(.nounwind, to: .function)
1919

2020
// FNATTR-NEXT: entry:
@@ -36,7 +36,7 @@ class IRAttributesSpec : XCTestCase {
3636
type: FunctionType([IntType.int32, IntType.int32],
3737
IntType.int32))
3838

39-
// RVATTR: define signext i32 @fn(i32, i32) {
39+
// RVATTR: define signext i32 @fn(i32 %0, i32 %1) {
4040
fn.addAttribute(.signext, to: .returnValue)
4141

4242
// RVATTR-NEXT: entry:
@@ -58,7 +58,7 @@ class IRAttributesSpec : XCTestCase {
5858
type: FunctionType([IntType.int32, i8ptr],
5959
IntType.int32))
6060

61-
// ARGATTR: define i32 @fn(i32 zeroext, i8* align 8) {
61+
// ARGATTR: define i32 @fn(i32 zeroext %0, i8* align 8 %1) {
6262
fn.addAttribute(.zeroext, to: .argument(0))
6363
fn.addAttribute(.align, value: 8, to: .argument(1))
6464

Tests/LLVMTests/IRIntrinsicSpec.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class IRIntrinsicSpec : XCTestCase {
1313
let module = Module(name: "IRIntrinsicTest")
1414
let builder = IRBuilder(module: module)
1515

16-
// IRINTRINSIC: define i32 @readOneArg(i32, ...) {
16+
// IRINTRINSIC: define i32 @readOneArg(i32 %0, ...) {
1717
let main = builder.addFunction("readOneArg",
1818
type: FunctionType([IntType.int32],
1919
IntType.int32,
@@ -53,13 +53,13 @@ class IRIntrinsicSpec : XCTestCase {
5353
module.dump()
5454

5555
// IRINTRINSIC: ; Function Attrs: nounwind
56-
// IRINTRINSIC-NEXT: declare void @llvm.va_start(i8*) #0
56+
// IRINTRINSIC-NEXT: declare void @llvm.va_start(i8* %0) #0
5757

5858
// IRINTRINSIC: ; Function Attrs: nounwind
59-
// IRINTRINSIC-NEXT: declare void @llvm.va_copy(i8*, i8*) #0
59+
// IRINTRINSIC-NEXT: declare void @llvm.va_copy(i8* %0, i8* %1) #0
6060

6161
// IRINTRINSIC: ; Function Attrs: nounwind
62-
// IRINTRINSIC-NEXT: declare void @llvm.va_end(i8*) #0
62+
// IRINTRINSIC-NEXT: declare void @llvm.va_end(i8* %0) #0
6363
})
6464

6565
XCTAssert(fileCheckOutput(of: .stderr, withPrefixes: ["VIRTUALOVERLOAD-IRINTRINSIC"]) {
@@ -93,7 +93,7 @@ class IRIntrinsicSpec : XCTestCase {
9393
module.dump()
9494

9595
// VIRTUALOVERLOAD-IRINTRINSIC: ; Function Attrs: nounwind readnone
96-
// VIRTUALOVERLOAD-IRINTRINSIC-NEXT: declare i32* @llvm.ssa.copy.p0i32(i32* returned) #0
96+
// VIRTUALOVERLOAD-IRINTRINSIC-NEXT: declare i32* @llvm.ssa.copy.p0i32(i32* returned %0) #0
9797
})
9898

9999
XCTAssert(fileCheckOutput(of: .stderr, withPrefixes: ["INTRINSIC-FAMILY-RESOLVE"]) {

Tests/LLVMTests/IRMetadataSpec.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class IRMetadataSpec : XCTestCase {
6363
XCTAssertNil(builder.defaultFloatingPointMathTag)
6464
builder.defaultFloatingPointMathTag = MDB.buildFloatingPointMathTag(0.1)
6565

66-
// IRFPMATHMETADATA: define float @test(float, float) {
66+
// IRFPMATHMETADATA: define float @test(float %0, float %1) {
6767
let main = builder.addFunction("test",
6868
type: FunctionType([
6969
FloatType.float, FloatType.float
@@ -91,7 +91,7 @@ class IRMetadataSpec : XCTestCase {
9191
let builder = IRBuilder(module: module)
9292
let MDB = MDBuilder()
9393

94-
// IRBWMETADATA: define float @test(i1, float, float) {
94+
// IRBWMETADATA: define float @test(i1 %0, float %1, float %2) {
9595
let main = builder.addFunction("test",
9696
type: FunctionType([
9797
IntType.int1,
@@ -180,7 +180,7 @@ class IRMetadataSpec : XCTestCase {
180180
IntType.int32.constant(0), // .s
181181
])
182182
// B->a.s = 42
183-
// IRSIMPLETBAA-NEXT: store i16 42, i16* %1, !tbaa [[AccessTag:![0-9]+]]
183+
// IRSIMPLETBAA-NEXT: store i16 42, i16* %1, align 2, !tbaa [[AccessTag:![0-9]+]]
184184
let si = builder.buildStore(IntType.int16.constant(42), to: field)
185185
// IRSIMPLETBAA-NEXT: ret void
186186
builder.buildRetVoid()
@@ -226,7 +226,7 @@ class IRMetadataSpec : XCTestCase {
226226
let builder = IRBuilder(module: module)
227227
let MDB = MDBuilder()
228228

229-
// IRMEMTRANSFERTBAA: define void @main(i8*) {
229+
// IRMEMTRANSFERTBAA: define void @main(i8* %0) {
230230
let F = module.addFunction("main", type: FunctionType([PointerType.toVoid], VoidType()))
231231
// IRMEMTRANSFERTBAA-NEXT: entry:
232232
let bb = F.appendBasicBlock(named: "entry")

Tests/LLVMTests/IRPassManagerSpec.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class IRPassManagerSpec : XCTestCase {
109109
// CHECK-EXECUTE-STDOPT: ; ModuleID = 'Test'
110110
// CHECK-EXECUTE-STDOPT: source_filename = "Test"
111111

112-
// CHECK-EXECUTE-STDOPT: define i32 @fun(i32, i32) {
112+
// CHECK-EXECUTE-STDOPT: define i32 @fun(i32 %0, i32 %1) {
113113
// CHECK-EXECUTE-STDOPT: entry:
114114
// CHECK-EXECUTE-STDOPT: %2 = alloca i32, align 4
115115
// CHECK-EXECUTE-STDOPT: %3 = alloca i32, align 4
@@ -148,7 +148,7 @@ class IRPassManagerSpec : XCTestCase {
148148
// CHECK-EXECUTE-STDOPT: source_filename = "Test"
149149

150150
// CHECK-EXECUTE-STDOPT: ; Function Attrs: norecurse nounwind readnone
151-
// CHECK-EXECUTE-STDOPT: define i32 @fun(i32, i32) local_unnamed_addr #0 {
151+
// CHECK-EXECUTE-STDOPT: define i32 @fun(i32 %0, i32 %1) local_unnamed_addr #0 {
152152
// CHECK-EXECUTE-STDOPT: entry:
153153
// CHECK-EXECUTE-STDOPT: %2 = icmp eq i32 %0, 1
154154
// CHECK-EXECUTE-STDOPT: %3 = add nsw i32 %1, 4
@@ -174,7 +174,7 @@ class IRPassManagerSpec : XCTestCase {
174174
// CHECK-EXECUTE-MASK: ; ModuleID = 'Test'
175175
// CHECK-EXECUTE-MASK: source_filename = "Test"
176176

177-
// CHECK-EXECUTE-MASK: define i32 @fun(i32, i32) {
177+
// CHECK-EXECUTE-MASK: define i32 @fun(i32 %0, i32 %1) {
178178
// CHECK-EXECUTE-MASK: entry:
179179
// CHECK-EXECUTE-MASK: %2 = alloca i32, align 4
180180
// CHECK-EXECUTE-MASK: %3 = alloca i32, align 4
@@ -212,7 +212,7 @@ class IRPassManagerSpec : XCTestCase {
212212
// CHECK-EXECUTE-MASK: ; ModuleID = 'Test'
213213
// CHECK-EXECUTE-MASK: source_filename = "Test"
214214

215-
// CHECK-EXECUTE-MASK: define i32 @fun(i32, i32) {
215+
// CHECK-EXECUTE-MASK: define i32 @fun(i32 %0, i32 %1) {
216216
// CHECK-EXECUTE-MASK: entry:
217217
// CHECK-EXECUTE-MASK: %2 = alloca i32, align 4
218218
// CHECK-EXECUTE-MASK: %3 = alloca i32, align 4

0 commit comments

Comments
 (0)