Skip to content

Commit

Permalink
Add global_addr, thin_to_thick_function and convert_function (tensorf…
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Paszke authored and Eugene Burmako committed Sep 11, 2019
1 parent 99dbe8b commit feac97b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
12 changes: 12 additions & 0 deletions Sources/SIL/SIL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ public enum Instruction {
// convert_escape_to_noescape [not_guaranteed] %29 : $@callee_guaranteed () -> Bool to $@noescape @callee_guaranteed () -> Bool
case convertEscapeToNoescape(_ notGuaranteed: Bool, _ escaped: Bool, _ operand: Operand, _ type: Type)

// https://github.com/apple/swift/blob/master/docs/SIL.rst#convert-function
// convert_function %0 : $@convention(thin) () -> Bool to $@convention(thin) @noescape () -> Bool
case convertFunction(_ operand: Operand, _ withoutActuallyEscaping: Bool, _ type: Type)

// https://github.com/apple/swift/blob/master/docs/SIL.rst#copy-addr
// copy_addr %1 to [initialization] %33 : $*Self
case copyAddr(_ take: Bool, _ value: String, _ initialization: Bool, _ operand: Operand)
Expand Down Expand Up @@ -180,6 +184,10 @@ public enum Instruction {
// function_ref @$s4main11threadCountSiyF : $@convention(thin) () -> Int
case functionRef(_ name: String, _ type: Type)

// https://github.com/apple/swift/blob/master/docs/SIL.rst#global-addr
// global_addr @$s5small4____Sivp : $*Int
case globalAddr(_ name: String, _ type: Type)

// https://github.com/apple/swift/blob/master/docs/SIL.rst#index-addr
// index_addr %5 : $*Int, %11 : $Builtin.Word
case indexAddr(_ addr: Operand, _ index: Operand)
Expand Down Expand Up @@ -235,6 +243,10 @@ public enum Instruction {
// switch_enum %122 : $Optional<Int>, case #Optional.some!enumelt.1: bb11, case #Optional.none!enumelt: bb18
case switchEnum(_ operand: Operand, _ cases: [Case])

// https://github.com/apple/swift/blob/master/docs/SIL.rst#thin-to-thick-function
// %2 = thin_to_thick_function %1 : $@convention(thin) @noescape () -> Bool to $@noescape @callee_guaranteed () -> Bool
case thinToThickFunction(_ operand: Operand, _ type: Type)

// https://github.com/apple/swift/blob/master/docs/SIL.rst#tuple
// tuple (%a : $A, %b : $B, ...)
// tuple $(a:A, b:B, ...) (%a, %b, ...)
Expand Down
3 changes: 3 additions & 0 deletions Sources/SIL/SILAnalysis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ extension Instruction {
return unwrap(trueOperands) + unwrap(falseOperands) + [cond]
case let .condFail(operand, _): return [operand.value]
case let .convertEscapeToNoescape(_, _, operand, _): return [operand.value]
case let .convertFunction(operand, _, _): return [operand.value]
case let .copyAddr(_, value, _, operand): return [value, operand.value]
case let .copyValue(operand): return [operand.value]
case let .deallocStack(operand): return [operand.value]
Expand All @@ -28,6 +29,7 @@ extension Instruction {
case let .enum(_, _, maybeOperand): return maybeOperand.map{ [$0.value] } ?? []
case .floatLiteral(_, _): return []
case .functionRef(_, _): return []
case .globalAddr(_, _): return []
case let .indexAddr(addr, index): return [addr.value, index.value]
case .integerLiteral(_, _): return []
case let .load(_, operand): return [operand.value]
Expand All @@ -41,6 +43,7 @@ extension Instruction {
case let .structElementAddr(operand, _): return [operand.value]
case let .structExtract(operand, _): return [operand.value]
case let .switchEnum(operand, _): return [operand.value]
case let .thinToThickFunction(operand, _): return [operand.value]
case let .tuple(elements):
switch elements {
case let .unlabeled(operands): return unwrap(operands)
Expand Down
22 changes: 19 additions & 3 deletions Sources/SIL/SILParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class SILParser: Parser {
try take("sil")
let linkage = try parseLinkage()
let attributes = try parseNilOrMany("[") { try parseFunctionAttribute() } ?? []
let name = try parseFunctionName()
let name = try parseGlobalName()
try take(":")
let type = try parseType()
let blocks = try parseNilOrMany("{", "", "}") { try parseBlock() } ?? []
Expand Down Expand Up @@ -137,6 +137,12 @@ class SILParser: Parser {
try take("to")
let type = try parseType()
return .convertEscapeToNoescape(notGuaranteed, escaped, operand, type)
case "convert_function":
let operand = try parseOperand()
try take("to")
let withoutActuallyEscaping = skip("[without_actually_escaping]")
let type = try parseType()
return .convertFunction(operand, withoutActuallyEscaping, type)
case "copy_addr":
let take = skip("[take]")
let value = try parseValue()
Expand Down Expand Up @@ -187,10 +193,15 @@ class SILParser: Parser {
let value = take(while: { $0.isHexDigit })
return .floatLiteral(type, value)
case "function_ref":
let name = try parseFunctionName()
let name = try parseGlobalName()
try take(":")
let type = try parseType()
return .functionRef(name, type)
case "global_addr":
let name = try parseGlobalName()
try take(":")
let type = try parseType()
return .globalAddr(name, type)
case "index_addr":
let addr = try parseOperand()
try take(",")
Expand Down Expand Up @@ -266,6 +277,11 @@ class SILParser: Parser {
let operand = try parseOperand()
let cases = try parseUntilNil { try parseCase() }
return .switchEnum(operand, cases)
case "thin_to_thick_function":
let operand = try parseOperand()
try take("to")
let type = try parseType()
return .thinToThickFunction(operand, type)
case "tuple":
let elements = try parseTupleElements()
return .tuple(elements)
Expand Down Expand Up @@ -435,7 +451,7 @@ class SILParser: Parser {
}

// https://github.com/apple/swift/blob/master/docs/SIL.rst#functions
func parseFunctionName() throws -> String {
func parseGlobalName() throws -> String {
let start = position
if skip("@") {
// TODO(#14): Make name parsing more thorough.
Expand Down
17 changes: 17 additions & 0 deletions Sources/SIL/SILPrinter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ class SILPrinter: Printer {
print(operand)
print(" to ")
print(type)
case let .convertFunction(operand, withoutActuallyEscaping, type):
print("convert_function ")
print(operand)
print(" to ")
print(when: withoutActuallyEscaping, "[without_actually_escaping] ")
print(type)
case let .copyAddr(take, value, initialization, operand):
print("copy_addr ")
print(when: take, "[take] ")
Expand Down Expand Up @@ -154,6 +160,12 @@ class SILPrinter: Printer {
print(name)
print(" : ")
print(type)
case let .globalAddr(name, type):
print("global_addr ")
print("@")
print(name)
print(" : ")
print(type)
case let .indexAddr(addr, index):
print("index_addr ")
print(addr)
Expand Down Expand Up @@ -224,6 +236,11 @@ class SILPrinter: Printer {
print("switch_enum ")
print(operand)
print(whenEmpty: false, "", cases, "", "") { print($0) }
case let .thinToThickFunction(operand, type):
print("thin_to_thick_function ")
print(operand)
print(" to ")
print(type)
case let .tuple(elements):
print("tuple ")
print(elements)
Expand Down
3 changes: 3 additions & 0 deletions Tests/SILTests/InstructionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ let instructionDefs = [
"cond_br %11, bb3, bb2",
"cond_br %12, label(%0 : $A), label(%1 : $B)",
"%94 = convert_escape_to_noescape [not_guaranteed] %93 : $@callee_guaranteed () -> Bool to $@noescape @callee_guaranteed () -> Bool",
"%1 = convert_function %0 : $@convention(thin) () -> Bool to [without_actually_escaping] $@convention(thin) @noescape () -> Bool",
"copy_addr %1 to [initialization] %33 : $*Self",
"dealloc_stack %162 : $*IndexingIterator<Range<Int>>",
"debug_value %1 : $Array<Float>, let, name \"input\", argno 2",
Expand All @@ -47,6 +48,7 @@ let instructionDefs = [
"function_ref @$ss6stride4from2to2bys8StrideToVyxGx_x0E0QztSxRzlF : $@convention(thin) <τ_0_0 where τ_0_0 : Strideable> (@in_guaranteed τ_0_0, @in_guaranteed τ_0_0, @in_guaranteed τ_0_0.Stride) -> @out StrideTo<τ_0_0>",
"function_ref @$s4main1CV3fooyyqd___qd_0_tSayqd__GRszSxRd_0_r0_lF : $@convention(method) <τ_0_0><τ_1_0, τ_1_1 where τ_0_0 == Array<τ_1_0>, τ_1_1 : Strideable> (@in_guaranteed τ_1_0, @in_guaranteed τ_1_1, C<Array<τ_1_0>>) -> ()",
"function_ref @$ss8StrideToV12makeIterators0abD0VyxGyF : $@convention(method) <τ_0_0 where τ_0_0 : Strideable> (@in StrideTo<τ_0_0>) -> @out StrideToIterator<τ_0_0>",
"%0 = global_addr @$s5small4____Sivp : $*Int",
"(%5, %6) = destructure_tuple %2 : $(Array<Int>, Builtin.RawPointer)",
"%42 = index_addr %35 : $*Int, %41 : $Builtin.Word",
"%7 = pointer_to_address %6 : $Builtin.RawPointer to [strict] $*Int",
Expand All @@ -71,6 +73,7 @@ let instructionDefs = [
"tuple (%a : $A, %b : $B)",
// TODO(#23): Parse tuple types with argument labels
// "tuple $(a:A, b:B) (%a, %b)",
"%2 = thin_to_thick_function %1 : $@convention(thin) @noescape () -> Bool to $@noescape @callee_guaranteed () -> Bool",
"unreachable",
"witness_method $Self, #Comparable.\"<=\"!1 : <Self where Self : Comparable> (Self.Type) -> (Self, Self) -> Bool : $@convention(witness_method: Comparable) <τ_0_0 where τ_0_0 : Comparable> (@in_guaranteed τ_0_0, @in_guaranteed τ_0_0, @thick τ_0_0.Type) -> Bool"
]
Expand Down

0 comments on commit feac97b

Please sign in to comment.