Skip to content
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

Remove LLVM dependency from front end #1074

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ let package = Package(
.executable(name: "hc", targets: ["hc"]),
.executable(name: "hylo-demangle", targets: ["hylo-demangle"]),
.library(name: "Hylo", targets: ["Driver"]),
.library(name: "HyloFrontEnd", targets: ["FrontEnd"]),
],

dependencies: [
Expand Down Expand Up @@ -87,7 +88,6 @@ let package = Package(
dependencies: [
"Utils",
.product(name: "Collections", package: "swift-collections"),
.product(name: "LLVM", package: "Swifty-LLVM"),
],
swiftSettings: allTargetsSwiftSettings),

Expand Down
8 changes: 8 additions & 0 deletions Sources/CodeGen/LLVM/FloatingPointPredicate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import LLVM
import Core

extension LLVM.FloatingPointPredicate {
public init(_ p: Core.FloatingPointPredicate) {
self = LLVM.FloatingPointPredicate(rawValue: p.rawValue)!
}
}
8 changes: 8 additions & 0 deletions Sources/CodeGen/LLVM/IntegerPredicate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import LLVM
import Core

extension LLVM.IntegerPredicate {
public init(_ p: Core.IntegerPredicate) {
self = LLVM.IntegerPredicate(rawValue: p.rawValue)!
}
}
12 changes: 12 additions & 0 deletions Sources/CodeGen/LLVM/OverflowBehavior.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import LLVM
import Core

extension LLVM.OverflowBehavior {
public init(_ ob: Core.OverflowBehavior) {
switch ob {
case .ignore: self = OverflowBehavior.ignore
case .nuw: self = OverflowBehavior.nuw
case .nsw: self = OverflowBehavior.nsw
}
}
}
10 changes: 5 additions & 5 deletions Sources/CodeGen/LLVM/Transpilation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -760,17 +760,17 @@ extension LLVM.Module {
case .add(let p, _):
let l = llvm(s.operands[0])
let r = llvm(s.operands[1])
register[.register(i)] = insertAdd(overflow: p, l, r, at: insertionPoint)
register[.register(i)] = insertAdd(overflow: OverflowBehavior(p), l, r, at: insertionPoint)

case .sub(let p, _):
let l = llvm(s.operands[0])
let r = llvm(s.operands[1])
register[.register(i)] = insertSub(overflow: p, l, r, at: insertionPoint)
register[.register(i)] = insertSub(overflow: OverflowBehavior(p), l, r, at: insertionPoint)

case .mul(let p, _):
let l = llvm(s.operands[0])
let r = llvm(s.operands[1])
register[.register(i)] = insertMul(overflow: p, l, r, at: insertionPoint)
register[.register(i)] = insertMul(overflow: OverflowBehavior(p), l, r, at: insertionPoint)

case .shl:
let l = llvm(s.operands[0])
Expand Down Expand Up @@ -852,7 +852,7 @@ extension LLVM.Module {
case .icmp(let p, _):
let l = llvm(s.operands[0])
let r = llvm(s.operands[1])
register[.register(i)] = insertIntegerComparison(p, l, r, at: insertionPoint)
register[.register(i)] = insertIntegerComparison(IntegerPredicate(p), l, r, at: insertionPoint)

case .and(_):
let l = llvm(s.operands[0])
Expand Down Expand Up @@ -916,7 +916,7 @@ extension LLVM.Module {
case .fcmp(_, let p, _):
let l = llvm(s.operands[0])
let r = llvm(s.operands[1])
register[.register(i)] = insertFloatingPointComparison(p, l, r, at: insertionPoint)
register[.register(i)] = insertFloatingPointComparison(FloatingPointPredicate(p), l, r, at: insertionPoint)

case .fptrunc(_, let t):
let target = ir.llvm(builtinType: t, in: &self)
Expand Down
7 changes: 3 additions & 4 deletions Sources/Core/BuiltinFunction.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import LLVM
import Utils

/// A function representing an IR instruction in Hylo source code.
Expand Down Expand Up @@ -335,7 +334,7 @@ private func mathFlags(_ stream: inout ArraySlice<Substring>) -> NativeInstructi
}

/// Returns an overflow behavior parsed from `stream` or `.ignore` if none can be parsed.
private func overflowBehavior(_ stream: inout ArraySlice<Substring>) -> LLVM.OverflowBehavior {
private func overflowBehavior(_ stream: inout ArraySlice<Substring>) -> OverflowBehavior {
switch stream.first {
case "nuw":
stream.removeFirst()
Expand All @@ -362,12 +361,12 @@ private let integerArithmeticTail =

/// Parses the parameters and type of `icmp`.
private let integerComparisonTail =
take(LLVM.IntegerPredicate.self) ++ builtinType
take(IntegerPredicate.self) ++ builtinType

/// Parses the parameters and type of a floating-point arithmetic instruction.
private let floatingPointArithmeticTail =
mathFlags ++ builtinType

/// Parses the parameters and type of `fcmp`.
private let floatingPointComparisonTail =
mathFlags ++ take(LLVM.FloatingPointPredicate.self) ++ builtinType
mathFlags ++ take(FloatingPointPredicate.self) ++ builtinType
66 changes: 66 additions & 0 deletions Sources/Core/FloatingPointPredicate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/// Copied from SwiftLLVM, used to remove LLVM dependency from Core

/// The predicate of an integer comparison.
///
/// - Note: Ordered means that neither operand is a QNAN while unordered means that either operand
/// may be a QNAN.
public enum FloatingPointPredicate: String, Hashable {

/// No comparison; always false.
case alwaysFalse = "false"

/// No comparison; always true.
case alwaysTrue = "true"

/// Values are ordered and equal.
case oeq

/// Values are ordered and not equal.
case one

/// Values are ordered and LHS is greater than RHS.
case ogt

/// Values are ordered and LHS greater than or equal to RHS.
case oge

/// Values are ordered and LHS is less than RHS.
case olt

/// Values are ordered and LHS is less than or equal to RHS.
case ole

/// Values are ordered (no nans).
case ord

/// Values are unordered or equal.
case ueq

/// Values are unordered or not equal.
case une

/// Values are unordered or LHS is greater than RHS.
case ugt

/// Values are unordered or LHS is greater than or equal to RHS.
case uge

/// Values are unordered or LHS is less than RHS.
case ult

/// Values are unordered or LHS is less than or equal to RHS.
case ule

/// Values are unordered (either nans).
case uno
}

extension FloatingPointPredicate: LosslessStringConvertible {

public init?(_ description: String) {
self.init(rawValue: description)
}

public var description: String { self.rawValue }

}
46 changes: 46 additions & 0 deletions Sources/Core/IntegerPredicate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/// Copied from SwiftLLVM, used to remove LLVM dependency from Core

/// The predicate of an integer comparison.
public enum IntegerPredicate: String, Hashable {

/// Values are equal.
case eq

/// Values are not equal.
case ne

/// LHS is greater than RHS, by unsigned comparison.
case ugt

/// LHS is greater than or equal to RHS, by unsigned comparison.
case uge

/// LHS is less than RHS, by unsigned comparison.
case ult

/// LHS is less than or equal to RHS, by unsigned comparison.
case ule

/// LHS is less than RHS, by signed comparison.
case slt

/// LHS is greater than or equal to RHS, by signed comparison.
case sge

/// LHS is greater than RHS, by signed comparison.
case sgt

/// LHS is less than or equal to RHS, by signed comparison.
case sle

}

extension IntegerPredicate: LosslessStringConvertible {

public init?(_ description: String) {
self.init(rawValue: description)
}

public var description: String { self.rawValue }

}
14 changes: 6 additions & 8 deletions Sources/Core/NativeInstruction.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import LLVM

/// The name of an native instruction mapped to a built-in function.
///
/// Native instructions implement basis operations on built-in types, such as `Builtin.i64`, with
Expand Down Expand Up @@ -27,13 +25,13 @@ import LLVM
/// integral and floating-point numbers as well as conversions from and to these types.
public enum NativeInstruction: Hashable {

case add(LLVM.OverflowBehavior, BuiltinType)
case add(OverflowBehavior, BuiltinType)

case sub(LLVM.OverflowBehavior, BuiltinType)
case sub(OverflowBehavior, BuiltinType)

case mul(LLVM.OverflowBehavior, BuiltinType)
case mul(OverflowBehavior, BuiltinType)

case shl(LLVM.OverflowBehavior, BuiltinType)
case shl(OverflowBehavior, BuiltinType)

case udiv(exact: Bool, BuiltinType)

Expand Down Expand Up @@ -71,7 +69,7 @@ public enum NativeInstruction: Hashable {
// Corresponding LLVM instruction: umul.with.overflow
case unsignedMultiplicationWithOverflow(BuiltinType)

case icmp(LLVM.IntegerPredicate, BuiltinType)
case icmp(IntegerPredicate, BuiltinType)

case trunc(BuiltinType, BuiltinType)

Expand All @@ -97,7 +95,7 @@ public enum NativeInstruction: Hashable {

case frem(MathFlags, BuiltinType)

case fcmp(MathFlags, LLVM.FloatingPointPredicate, BuiltinType)
case fcmp(MathFlags, FloatingPointPredicate, BuiltinType)

case fptrunc(BuiltinType, BuiltinType)

Expand Down
15 changes: 15 additions & 0 deletions Sources/Core/OverflowBehavior.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// Copied from SwiftLLVM, used to remove LLVM dependency from Core

/// The behavior that should occur on overflow during mathematical operations.
public enum OverflowBehavior {

/// Overflow is ignored.
case ignore

/// The result is a poison value should unsigned overflow occur.
case nuw

/// The result is a poison value should signed overflow occur.
case nsw

}