Skip to content

Vector support #104

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 4 commits into from
Sep 27, 2017
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
/.build
/Packages
Package.resolved
Package.pins
/*.xcodeproj
/build
42 changes: 31 additions & 11 deletions Sources/LLVM/IRBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -497,9 +497,11 @@ public class IRBuilder {
public func buildAdd(_ lhs: IRValue, _ rhs: IRValue,
overflowBehavior: OverflowBehavior = .default,
name: String = "") -> IRValue {
let lhsType = lowerVector(lhs.type)

let lhsVal = lhs.asLLVM()
let rhsVal = rhs.asLLVM()
if lhs.type is IntType {
if lhsType is IntType {
switch overflowBehavior {
case .noSignedWrap:
return LLVMBuildNSWAdd(llvm, lhsVal, rhsVal, name)
Expand All @@ -508,7 +510,7 @@ public class IRBuilder {
case .default:
return LLVMBuildAdd(llvm, lhsVal, rhsVal, name)
}
} else if lhs.type is FloatType {
} else if lhsType is FloatType {
return LLVMBuildFAdd(llvm, lhsVal, rhsVal, name)
}
fatalError("Can only add value of int, float, or vector types")
Expand All @@ -530,9 +532,11 @@ public class IRBuilder {
public func buildSub(_ lhs: IRValue, _ rhs: IRValue,
overflowBehavior: OverflowBehavior = .default,
name: String = "") -> IRValue {
let lhsType = lowerVector(lhs.type)

let lhsVal = lhs.asLLVM()
let rhsVal = rhs.asLLVM()
if lhs.type is IntType {
if lhsType is IntType {
switch overflowBehavior {
case .noSignedWrap:
return LLVMBuildNSWSub(llvm, lhsVal, rhsVal, name)
Expand All @@ -541,7 +545,7 @@ public class IRBuilder {
case .default:
return LLVMBuildSub(llvm, lhsVal, rhsVal, name)
}
} else if lhs.type is FloatType {
} else if lhsType is FloatType {
return LLVMBuildFSub(llvm, lhsVal, rhsVal, name)
}
fatalError("Can only subtract value of int or float types")
Expand All @@ -563,9 +567,11 @@ public class IRBuilder {
public func buildMul(_ lhs: IRValue, _ rhs: IRValue,
overflowBehavior: OverflowBehavior = .default,
name: String = "") -> IRValue {
let lhsType = lowerVector(lhs.type)

let lhsVal = lhs.asLLVM()
let rhsVal = rhs.asLLVM()
if lhs.type is IntType {
if lhsType is IntType {
switch overflowBehavior {
case .noSignedWrap:
return LLVMBuildNSWMul(llvm, lhsVal, rhsVal, name)
Expand All @@ -574,7 +580,7 @@ public class IRBuilder {
case .default:
return LLVMBuildMul(llvm, lhsVal, rhsVal, name)
}
} else if lhs.type is FloatType {
} else if lhsType is FloatType {
return LLVMBuildFMul(llvm, lhsVal, rhsVal, name)
}
fatalError("Can only multiply value of int or float types")
Expand All @@ -598,15 +604,17 @@ public class IRBuilder {
public func buildRem(_ lhs: IRValue, _ rhs: IRValue,
signed: Bool = true,
name: String = "") -> IRValue {
let lhsType = lowerVector(lhs.type)

let lhsVal = lhs.asLLVM()
let rhsVal = rhs.asLLVM()
if lhs.type is IntType {
if lhsType is IntType {
if signed {
return LLVMBuildSRem(llvm, lhsVal, rhsVal, name)
} else {
return LLVMBuildURem(llvm, lhsVal, rhsVal, name)
}
} else if lhs.type is FloatType {
} else if lhsType is FloatType {
return LLVMBuildFRem(llvm, lhsVal, rhsVal, name)
}
fatalError("Can only take remainder of int or float types")
Expand All @@ -633,9 +641,11 @@ public class IRBuilder {
public func buildDiv(_ lhs: IRValue, _ rhs: IRValue,
signed: Bool = true, exact: Bool = false,
name: String = "") -> IRValue {
let lhsType = lowerVector(lhs.type)

let lhsVal = lhs.asLLVM()
let rhsVal = rhs.asLLVM()
if lhs.type is IntType {
if lhsType is IntType {
if signed {
if exact {
return LLVMBuildExactSDiv(llvm, lhsVal, rhsVal, name)
Expand All @@ -645,7 +655,7 @@ public class IRBuilder {
} else {
return LLVMBuildUDiv(llvm, lhsVal, rhsVal, name)
}
} else if lhs.type is FloatType {
} else if lhsType is FloatType {
return LLVMBuildFDiv(llvm, lhsVal, rhsVal, name)
}
fatalError("Can only divide values of int or float types")
Expand Down Expand Up @@ -694,9 +704,11 @@ public class IRBuilder {
public func buildFCmp(_ lhs: IRValue, _ rhs: IRValue,
_ predicate: RealPredicate,
name: String = "") -> IRValue {
let lhsType = lowerVector(lhs.type)

let lhsVal = lhs.asLLVM()
let rhsVal = rhs.asLLVM()
guard lhs.type is FloatType else {
guard lhsType is FloatType else {
fatalError("Can only build FCMP instruction with float types")
}
return LLVMBuildFCmp(llvm, predicate.llvm, lhsVal, rhsVal, name)
Expand Down Expand Up @@ -1770,3 +1782,11 @@ public class IRBuilder {
LLVMDisposeBuilder(llvm)
}
}

private func lowerVector(_ type: IRType) -> IRType {
guard let vectorType = type as? VectorType else {
return type
}

return vectorType.elementType
}