Skip to content

Added vector support #103

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 1 commit into from
Sep 18, 2017
Merged
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
52 changes: 41 additions & 11 deletions Sources/LLVM/IRBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -497,9 +497,14 @@ public class IRBuilder {
public func buildAdd(_ lhs: IRValue, _ rhs: IRValue,
overflowBehavior: OverflowBehavior = .default,
name: String = "") -> IRValue {
var lhsType = lhs.type
if let vlhs = lhsType as? VectorType {
lhsType = vlhs.elementType
}

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 +513,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 +535,14 @@ public class IRBuilder {
public func buildSub(_ lhs: IRValue, _ rhs: IRValue,
overflowBehavior: OverflowBehavior = .default,
name: String = "") -> IRValue {
var lhsType = lhs.type
if let vlhs = lhsType as? VectorType {
lhsType = vlhs.elementType
}

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 +551,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 +573,14 @@ public class IRBuilder {
public func buildMul(_ lhs: IRValue, _ rhs: IRValue,
overflowBehavior: OverflowBehavior = .default,
name: String = "") -> IRValue {
var lhsType = lhs.type
if let vlhs = lhsType as? VectorType {
lhsType = vlhs.elementType
}

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 +589,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 +613,20 @@ public class IRBuilder {
public func buildRem(_ lhs: IRValue, _ rhs: IRValue,
signed: Bool = true,
name: String = "") -> IRValue {
var lhsType = lhs.type
if let vlhs = lhsType as? VectorType {
lhsType = vlhs.elementType
}

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 +653,14 @@ public class IRBuilder {
public func buildDiv(_ lhs: IRValue, _ rhs: IRValue,
signed: Bool = true, exact: Bool = false,
name: String = "") -> IRValue {
var lhsType = lhs.type
if let vlhs = lhsType as? VectorType {
lhsType = vlhs.elementType
}

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 +670,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 +719,14 @@ public class IRBuilder {
public func buildFCmp(_ lhs: IRValue, _ rhs: IRValue,
_ predicate: RealPredicate,
name: String = "") -> IRValue {
var lhsType = lhs.type
if let vlhs = lhsType as? VectorType {
lhsType = vlhs.elementType
}

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