Skip to content

Commit a2e8974

Browse files
BrettRToomeyCodaFi
authored andcommitted
Vector support (#104)
* Added vector support * Moved vector unwrapping to an internal helper * Moved lower vector
1 parent a77e2e4 commit a2e8974

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
/.build
33
/Packages
44
Package.resolved
5+
Package.pins
56
/*.xcodeproj
67
/build

Sources/LLVM/IRBuilder.swift

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,11 @@ public class IRBuilder {
497497
public func buildAdd(_ lhs: IRValue, _ rhs: IRValue,
498498
overflowBehavior: OverflowBehavior = .default,
499499
name: String = "") -> IRValue {
500+
let lhsType = lowerVector(lhs.type)
501+
500502
let lhsVal = lhs.asLLVM()
501503
let rhsVal = rhs.asLLVM()
502-
if lhs.type is IntType {
504+
if lhsType is IntType {
503505
switch overflowBehavior {
504506
case .noSignedWrap:
505507
return LLVMBuildNSWAdd(llvm, lhsVal, rhsVal, name)
@@ -508,7 +510,7 @@ public class IRBuilder {
508510
case .default:
509511
return LLVMBuildAdd(llvm, lhsVal, rhsVal, name)
510512
}
511-
} else if lhs.type is FloatType {
513+
} else if lhsType is FloatType {
512514
return LLVMBuildFAdd(llvm, lhsVal, rhsVal, name)
513515
}
514516
fatalError("Can only add value of int, float, or vector types")
@@ -530,9 +532,11 @@ public class IRBuilder {
530532
public func buildSub(_ lhs: IRValue, _ rhs: IRValue,
531533
overflowBehavior: OverflowBehavior = .default,
532534
name: String = "") -> IRValue {
535+
let lhsType = lowerVector(lhs.type)
536+
533537
let lhsVal = lhs.asLLVM()
534538
let rhsVal = rhs.asLLVM()
535-
if lhs.type is IntType {
539+
if lhsType is IntType {
536540
switch overflowBehavior {
537541
case .noSignedWrap:
538542
return LLVMBuildNSWSub(llvm, lhsVal, rhsVal, name)
@@ -541,7 +545,7 @@ public class IRBuilder {
541545
case .default:
542546
return LLVMBuildSub(llvm, lhsVal, rhsVal, name)
543547
}
544-
} else if lhs.type is FloatType {
548+
} else if lhsType is FloatType {
545549
return LLVMBuildFSub(llvm, lhsVal, rhsVal, name)
546550
}
547551
fatalError("Can only subtract value of int or float types")
@@ -563,9 +567,11 @@ public class IRBuilder {
563567
public func buildMul(_ lhs: IRValue, _ rhs: IRValue,
564568
overflowBehavior: OverflowBehavior = .default,
565569
name: String = "") -> IRValue {
570+
let lhsType = lowerVector(lhs.type)
571+
566572
let lhsVal = lhs.asLLVM()
567573
let rhsVal = rhs.asLLVM()
568-
if lhs.type is IntType {
574+
if lhsType is IntType {
569575
switch overflowBehavior {
570576
case .noSignedWrap:
571577
return LLVMBuildNSWMul(llvm, lhsVal, rhsVal, name)
@@ -574,7 +580,7 @@ public class IRBuilder {
574580
case .default:
575581
return LLVMBuildMul(llvm, lhsVal, rhsVal, name)
576582
}
577-
} else if lhs.type is FloatType {
583+
} else if lhsType is FloatType {
578584
return LLVMBuildFMul(llvm, lhsVal, rhsVal, name)
579585
}
580586
fatalError("Can only multiply value of int or float types")
@@ -598,15 +604,17 @@ public class IRBuilder {
598604
public func buildRem(_ lhs: IRValue, _ rhs: IRValue,
599605
signed: Bool = true,
600606
name: String = "") -> IRValue {
607+
let lhsType = lowerVector(lhs.type)
608+
601609
let lhsVal = lhs.asLLVM()
602610
let rhsVal = rhs.asLLVM()
603-
if lhs.type is IntType {
611+
if lhsType is IntType {
604612
if signed {
605613
return LLVMBuildSRem(llvm, lhsVal, rhsVal, name)
606614
} else {
607615
return LLVMBuildURem(llvm, lhsVal, rhsVal, name)
608616
}
609-
} else if lhs.type is FloatType {
617+
} else if lhsType is FloatType {
610618
return LLVMBuildFRem(llvm, lhsVal, rhsVal, name)
611619
}
612620
fatalError("Can only take remainder of int or float types")
@@ -633,9 +641,11 @@ public class IRBuilder {
633641
public func buildDiv(_ lhs: IRValue, _ rhs: IRValue,
634642
signed: Bool = true, exact: Bool = false,
635643
name: String = "") -> IRValue {
644+
let lhsType = lowerVector(lhs.type)
645+
636646
let lhsVal = lhs.asLLVM()
637647
let rhsVal = rhs.asLLVM()
638-
if lhs.type is IntType {
648+
if lhsType is IntType {
639649
if signed {
640650
if exact {
641651
return LLVMBuildExactSDiv(llvm, lhsVal, rhsVal, name)
@@ -645,7 +655,7 @@ public class IRBuilder {
645655
} else {
646656
return LLVMBuildUDiv(llvm, lhsVal, rhsVal, name)
647657
}
648-
} else if lhs.type is FloatType {
658+
} else if lhsType is FloatType {
649659
return LLVMBuildFDiv(llvm, lhsVal, rhsVal, name)
650660
}
651661
fatalError("Can only divide values of int or float types")
@@ -694,9 +704,11 @@ public class IRBuilder {
694704
public func buildFCmp(_ lhs: IRValue, _ rhs: IRValue,
695705
_ predicate: RealPredicate,
696706
name: String = "") -> IRValue {
707+
let lhsType = lowerVector(lhs.type)
708+
697709
let lhsVal = lhs.asLLVM()
698710
let rhsVal = rhs.asLLVM()
699-
guard lhs.type is FloatType else {
711+
guard lhsType is FloatType else {
700712
fatalError("Can only build FCMP instruction with float types")
701713
}
702714
return LLVMBuildFCmp(llvm, predicate.llvm, lhsVal, rhsVal, name)
@@ -1777,3 +1789,11 @@ public class IRBuilder {
17771789
LLVMDisposeBuilder(llvm)
17781790
}
17791791
}
1792+
1793+
private func lowerVector(_ type: IRType) -> IRType {
1794+
guard let vectorType = type as? VectorType else {
1795+
return type
1796+
}
1797+
1798+
return vectorType.elementType
1799+
}

0 commit comments

Comments
 (0)