Skip to content

Fix Unit tests under LLVM 11 #227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions Sources/LLVM/DIBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ extension DIBuilder {
named name: String,
scope: DIScope, file: FileMetadata, line: Int,
type: DIType, alwaysPreserve: Bool = false,
flags: DIFlags = [], alignment: Alignment = .zero
flags: DIFlags = [], alignment: Alignment
) -> LocalVariableMetadata {
let radix = UInt32(self.module.dataLayout.intPointerType().width)
guard let variable = LLVMDIBuilderCreateAutoVariable(
Expand Down Expand Up @@ -502,7 +502,7 @@ extension DIBuilder {
/// - file: File where this member is defined.
/// - line: Line number.
/// - size: Member size.
/// - alignmemnt: Member alignment.
/// - alignment: Member alignment.
/// - elements: Enumeration elements.
/// - numElements: Number of enumeration elements.
/// - underlyingType: Underlying type of a C++11/ObjC fixed enum.
Expand Down Expand Up @@ -675,7 +675,7 @@ extension DIBuilder {
/// - addressSpace: The address space the pointer type reside in.
/// - name: The name of the pointer type.
public func buildPointerType(
pointee: DIType, size: Size, alignment: Alignment = .zero,
pointee: DIType, size: Size, alignment: Alignment,
addressSpace: AddressSpace = .zero, name: String = ""
) -> DIType {
let radix = UInt32(self.module.dataLayout.intPointerType().width)
Expand Down Expand Up @@ -1258,15 +1258,14 @@ extension DIBuilder {
/// - expression: The location of the global relative to the attached
/// GlobalVariable.
/// - declaration: Reference to the corresponding declaration.
/// - alignment: Variable alignment(or 0 if no alignment attr was
/// specified)
/// - alignment: Variable alignment
public func buildGlobalExpression(
named name: String, linkageName: String, type: DIType,
scope: DIScope, file: FileMetadata, line: Int,
isLocal: Bool = true,
expression: ExpressionMetadata? = nil,
declaration: IRMetadata? = nil,
alignment: Alignment = .zero
alignment: Alignment
) -> ExpressionMetadata {
let radix = UInt32(self.module.dataLayout.intPointerType().width)
guard let ty = LLVMDIBuilderCreateGlobalVariableExpression(
Expand Down
30 changes: 23 additions & 7 deletions Sources/LLVM/IRBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1034,9 +1034,8 @@ extension IRBuilder {
/// allocated, otherwise `count` is defaulted to be one. If a constant
/// alignment is specified, the value result of the allocation is guaranteed
/// to be aligned to at least that boundary. The alignment may not be
/// greater than `1 << 29`. If not specified, or if zero, the target can
/// choose to align the allocation on any convenient boundary compatible with
/// the type.
/// greater than `1 << 29`. If not specified, or if zero, the target will
/// choose a default value that is convenient and compatible with the type.
///
/// The returned value is allocated in the address space specified in the data layout string for the target. If
/// no such value is specified, the value is allocated in the default address space.
Expand All @@ -1057,13 +1056,18 @@ extension IRBuilder {
} else {
allocaInst = LLVMBuildAlloca(llvm, type.asLLVM(), name)!
}
LLVMSetAlignment(allocaInst, alignment.rawValue)
if !alignment.isZero {
LLVMSetAlignment(allocaInst, alignment.rawValue)
}
return allocaInst
}

/// Build a store instruction that stores the first value into the location
/// given in the second value.
///
/// If alignment is not specified, or if zero, the target will choose a default
/// value that is convenient and compatible with the type.
///
/// - parameter val: The source value.
/// - parameter ptr: The destination pointer to store into.
/// - parameter ordering: The ordering effect of the fence for this store,
Expand All @@ -1077,13 +1081,18 @@ extension IRBuilder {
let storeInst = LLVMBuildStore(llvm, val.asLLVM(), ptr.asLLVM())!
LLVMSetOrdering(storeInst, ordering.llvm)
LLVMSetVolatile(storeInst, volatile.llvm)
LLVMSetAlignment(storeInst, alignment.rawValue)
if !alignment.isZero {
LLVMSetAlignment(storeInst, alignment.rawValue)
}
return storeInst
}

/// Build a load instruction that loads a value from the location in the
/// given value.
///
/// If alignment is not specified, or if zero, the target will choose a default
/// value that is convenient and compatible with the type.
///
/// - parameter ptr: The pointer value to load from.
/// - parameter type: The type of value loaded from the given pointer.
/// - parameter ordering: The ordering effect of the fence for this load,
Expand All @@ -1098,7 +1107,9 @@ extension IRBuilder {
let loadInst = LLVMBuildLoad2(llvm, type.asLLVM(), ptr.asLLVM(), name)!
LLVMSetOrdering(loadInst, ordering.llvm)
LLVMSetVolatile(loadInst, volatile.llvm)
LLVMSetAlignment(loadInst, alignment.rawValue)
if !alignment.isZero {
LLVMSetAlignment(loadInst, alignment.rawValue)
}
return loadInst
}

Expand Down Expand Up @@ -1927,6 +1938,9 @@ extension IRBuilder {
/// Build a load instruction that loads a value from the location in the
/// given value.
///
/// If alignment is not specified, or if zero, the target will choose a default
/// value that is convenient and compatible with the type.
///
/// - parameter ptr: The pointer value to load from.
/// - parameter ordering: The ordering effect of the fence for this load,
/// if any. Defaults to a nonatomic load.
Expand All @@ -1941,7 +1955,9 @@ extension IRBuilder {
let loadInst = LLVMBuildLoad(llvm, ptr.asLLVM(), name)!
LLVMSetOrdering(loadInst, ordering.llvm)
LLVMSetVolatile(loadInst, volatile.llvm)
LLVMSetAlignment(loadInst, alignment.rawValue)
if !alignment.isZero {
LLVMSetAlignment(loadInst, alignment.rawValue)
}
return loadInst
}

Expand Down
13 changes: 13 additions & 0 deletions Sources/LLVM/Module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ import cllvm
/// together with the LLVM linker, which merges function (and global variable)
/// definitions, resolves forward declarations, and merges symbol table entries.
///
/// Creating a Module
/// ==================
///
/// A module can be created using `init(name:context:)`.
/// Note that the default target triple is bare metal and there is no default data layout.
/// If you require these to be specified (e.g. to increase the correctness of default alignment values),
/// be sure to set them yourself.
///
/// if let machine = try? TargetMachine() {
/// module.targetTriple = machine.triple
/// module.dataLayout = machine.dataLayout
/// }
///
/// Verifying a Module
/// ==================
///
Expand Down
5 changes: 4 additions & 1 deletion Sources/LLVM/Units.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ public struct Alignment: Comparable, Hashable {
///
/// An n-byte alignment contains log-base-two-many least-significant zeros.
public func log2() -> UInt32 {
guard !isZero else { return 0 }
return 31 - UInt32(self.rawValue.leadingZeroBitCount)
}

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

/// Returns the alignment of a pointer which points to the given number of
Expand Down
6 changes: 4 additions & 2 deletions Tests/LLVMTests/BFC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,13 @@ private func compileProgramBody(
encoding: .signed, flags: [],
size: builder.module.dataLayout.abiSize(of: cellType))
let diPtrTy = dibuilder.buildPointerType(pointee: diDataTy,
size: builder.module.dataLayout.pointerSize())
size: builder.module.dataLayout.pointerSize(),
alignment: .one)
let diVariable = dibuilder.buildLocalVariable(named: "this",
scope: entryScope,
file: file, line: startPoint.0,
type: diPtrTy, flags: .artificial)
type: diPtrTy, flags: .artificial,
alignment: .one)

var sourceLine = startPoint.0 + 1
var sourceColumn = startPoint.1 + 1
Expand Down
2 changes: 1 addition & 1 deletion Tests/LLVMTests/DIBuilderSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class DIBuilderSpec : XCTestCase {
// DIEXPRESSION: !{{[0-9]+}} = !DIGlobalVariableExpression(var: !{{[0-9]+}}, expr: !{{[0-9]+}})
let expr = debugBuilder.buildGlobalExpression(
named: "global", linkageName: "global", type: globalTy,
scope: cu, file: file, line: 42)
scope: cu, file: file, line: 42, alignment: global.alignment)
// DIEXPRESSION: !{{[0-9]+}} = distinct !DIGlobalVariable(name: "unattached", linkageName: "unattached", scope: !0, file: !{{[0-9]+}}, line: 42, type: !{{[0-9]+}}, isLocal: true, isDefinition: true)
_ = debugBuilder.buildGlobalExpression(
named: "unattached", linkageName: "unattached",
Expand Down
6 changes: 3 additions & 3 deletions Tests/LLVMTests/IRAttributesSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class IRAttributesSpec : XCTestCase {
type: FunctionType([IntType.int32, IntType.int32],
IntType.int32))

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

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

// RVATTR: define signext i32 @fn(i32, i32) {
// RVATTR: define signext i32 @fn(i32 %0, i32 %1) {
fn.addAttribute(.signext, to: .returnValue)

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

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

Expand Down
10 changes: 5 additions & 5 deletions Tests/LLVMTests/IRIntrinsicSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class IRIntrinsicSpec : XCTestCase {
let module = Module(name: "IRIntrinsicTest")
let builder = IRBuilder(module: module)

// IRINTRINSIC: define i32 @readOneArg(i32, ...) {
// IRINTRINSIC: define i32 @readOneArg(i32 %0, ...) {
let main = builder.addFunction("readOneArg",
type: FunctionType([IntType.int32],
IntType.int32,
Expand Down Expand Up @@ -53,13 +53,13 @@ class IRIntrinsicSpec : XCTestCase {
module.dump()

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

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

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

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

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

XCTAssert(fileCheckOutput(of: .stderr, withPrefixes: ["INTRINSIC-FAMILY-RESOLVE"]) {
Expand Down
8 changes: 4 additions & 4 deletions Tests/LLVMTests/IRMetadataSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class IRMetadataSpec : XCTestCase {
XCTAssertNil(builder.defaultFloatingPointMathTag)
builder.defaultFloatingPointMathTag = MDB.buildFloatingPointMathTag(0.1)

// IRFPMATHMETADATA: define float @test(float, float) {
// IRFPMATHMETADATA: define float @test(float %0, float %1) {
let main = builder.addFunction("test",
type: FunctionType([
FloatType.float, FloatType.float
Expand Down Expand Up @@ -91,7 +91,7 @@ class IRMetadataSpec : XCTestCase {
let builder = IRBuilder(module: module)
let MDB = MDBuilder()

// IRBWMETADATA: define float @test(i1, float, float) {
// IRBWMETADATA: define float @test(i1 %0, float %1, float %2) {
let main = builder.addFunction("test",
type: FunctionType([
IntType.int1,
Expand Down Expand Up @@ -180,7 +180,7 @@ class IRMetadataSpec : XCTestCase {
IntType.int32.constant(0), // .s
])
// B->a.s = 42
// IRSIMPLETBAA-NEXT: store i16 42, i16* %1, !tbaa [[AccessTag:![0-9]+]]
// IRSIMPLETBAA-NEXT: store i16 42, i16* %1, align 2, !tbaa [[AccessTag:![0-9]+]]
let si = builder.buildStore(IntType.int16.constant(42), to: field)
// IRSIMPLETBAA-NEXT: ret void
builder.buildRetVoid()
Expand Down Expand Up @@ -226,7 +226,7 @@ class IRMetadataSpec : XCTestCase {
let builder = IRBuilder(module: module)
let MDB = MDBuilder()

// IRMEMTRANSFERTBAA: define void @main(i8*) {
// IRMEMTRANSFERTBAA: define void @main(i8* %0) {
let F = module.addFunction("main", type: FunctionType([PointerType.toVoid], VoidType()))
// IRMEMTRANSFERTBAA-NEXT: entry:
let bb = F.appendBasicBlock(named: "entry")
Expand Down
8 changes: 4 additions & 4 deletions Tests/LLVMTests/IRPassManagerSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class IRPassManagerSpec : XCTestCase {
// CHECK-EXECUTE-STDOPT: ; ModuleID = 'Test'
// CHECK-EXECUTE-STDOPT: source_filename = "Test"

// CHECK-EXECUTE-STDOPT: define i32 @fun(i32, i32) {
// CHECK-EXECUTE-STDOPT: define i32 @fun(i32 %0, i32 %1) {
// CHECK-EXECUTE-STDOPT: entry:
// CHECK-EXECUTE-STDOPT: %2 = alloca i32, align 4
// CHECK-EXECUTE-STDOPT: %3 = alloca i32, align 4
Expand Down Expand Up @@ -148,7 +148,7 @@ class IRPassManagerSpec : XCTestCase {
// CHECK-EXECUTE-STDOPT: source_filename = "Test"

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

// CHECK-EXECUTE-MASK: define i32 @fun(i32, i32) {
// CHECK-EXECUTE-MASK: define i32 @fun(i32 %0, i32 %1) {
// CHECK-EXECUTE-MASK: entry:
// CHECK-EXECUTE-MASK: %2 = alloca i32, align 4
// CHECK-EXECUTE-MASK: %3 = alloca i32, align 4
Expand Down Expand Up @@ -212,7 +212,7 @@ class IRPassManagerSpec : XCTestCase {
// CHECK-EXECUTE-MASK: ; ModuleID = 'Test'
// CHECK-EXECUTE-MASK: source_filename = "Test"

// CHECK-EXECUTE-MASK: define i32 @fun(i32, i32) {
// CHECK-EXECUTE-MASK: define i32 @fun(i32 %0, i32 %1) {
// CHECK-EXECUTE-MASK: entry:
// CHECK-EXECUTE-MASK: %2 = alloca i32, align 4
// CHECK-EXECUTE-MASK: %3 = alloca i32, align 4
Expand Down