|
| 1 | +import LLVM |
| 2 | +import XCTest |
| 3 | +import FileCheck |
| 4 | +import Foundation |
| 5 | + |
| 6 | +class IRAttributesSpec : XCTestCase { |
| 7 | + func testIRAttributes() { |
| 8 | + XCTAssert(fileCheckOutput(of: .stderr, withPrefixes: ["FNATTR"]) { |
| 9 | + // FNATTR: ; ModuleID = '[[ModuleName:IRBuilderTest]]' |
| 10 | + // FNATTR-NEXT: source_filename = "[[ModuleName]]" |
| 11 | + let module = Module(name: "IRBuilderTest") |
| 12 | + let builder = IRBuilder(module: module) |
| 13 | + let fn = builder.addFunction("fn", |
| 14 | + type: FunctionType(argTypes: [IntType.int32, IntType.int32], |
| 15 | + returnType: IntType.int32)) |
| 16 | + |
| 17 | + // FNATTR: define i32 @fn(i32, i32) #0 { |
| 18 | + fn.addAttribute(.nounwind, to: .function) |
| 19 | + |
| 20 | + // FNATTR-NEXT: entry: |
| 21 | + let entry = fn.appendBasicBlock(named: "entry") |
| 22 | + builder.positionAtEnd(of: entry) |
| 23 | + // FNATTR-NEXT: ret i32 0 |
| 24 | + builder.buildRet(IntType.int32.constant(0)) |
| 25 | + // FNATTR-NEXT: } |
| 26 | + // FNATTR: attributes #0 = { nounwind } |
| 27 | + module.dump() |
| 28 | + }) |
| 29 | + |
| 30 | + XCTAssert(fileCheckOutput(of: .stderr, withPrefixes: ["RVATTR"]) { |
| 31 | + // RVATTR: ; ModuleID = '[[ModuleName:IRBuilderTest]]' |
| 32 | + // RVATTR-NEXT: source_filename = "[[ModuleName]]" |
| 33 | + let module = Module(name: "IRBuilderTest") |
| 34 | + let builder = IRBuilder(module: module) |
| 35 | + let fn = builder.addFunction("fn", |
| 36 | + type: FunctionType(argTypes: [IntType.int32, IntType.int32], |
| 37 | + returnType: IntType.int32)) |
| 38 | + |
| 39 | + // RVATTR: define signext i32 @fn(i32, i32) { |
| 40 | + fn.addAttribute(.signext, to: .returnValue) |
| 41 | + |
| 42 | + // RVATTR-NEXT: entry: |
| 43 | + let entry = fn.appendBasicBlock(named: "entry") |
| 44 | + builder.positionAtEnd(of: entry) |
| 45 | + // RVATTR-NEXT: ret i32 0 |
| 46 | + builder.buildRet(IntType.int32.constant(0)) |
| 47 | + // RVATTR-NEXT: } |
| 48 | + module.dump() |
| 49 | + }) |
| 50 | + |
| 51 | + XCTAssert(fileCheckOutput(of: .stderr, withPrefixes: ["ARGATTR"]) { |
| 52 | + // ARGATTR: ; ModuleID = '[[ModuleName:IRBuilderTest]]' |
| 53 | + // ARGATTR-NEXT: source_filename = "[[ModuleName]]" |
| 54 | + let module = Module(name: "IRBuilderTest") |
| 55 | + let builder = IRBuilder(module: module) |
| 56 | + let fn = builder.addFunction("fn", |
| 57 | + type: FunctionType(argTypes: [IntType.int32, IntType.int32], |
| 58 | + returnType: IntType.int32)) |
| 59 | + |
| 60 | + // ARGATTR: define i32 @fn(i32 zeroext, i32 signext) { |
| 61 | + fn.addAttribute(.zeroext, to: .argument(0)) |
| 62 | + fn.addAttribute(.signext, to: .argument(1)) |
| 63 | + |
| 64 | + // ARGATTR-NEXT: entry: |
| 65 | + let entry = fn.appendBasicBlock(named: "entry") |
| 66 | + builder.positionAtEnd(of: entry) |
| 67 | + // ARGATTR-NEXT: ret i32 0 |
| 68 | + builder.buildRet(IntType.int32.constant(0)) |
| 69 | + // ARGATTR-NEXT: } |
| 70 | + module.dump() |
| 71 | + }) |
| 72 | + } |
| 73 | + |
| 74 | + #if !os(macOS) |
| 75 | + static var allTests = testCase([ |
| 76 | + ("testIRAttributes", testIRAttributes), |
| 77 | + ]) |
| 78 | + #endif |
| 79 | +} |
| 80 | + |
0 commit comments