swiftlang/swift

[SR-13903] Make `ApplyInst` a `MultipleValueInstruction`

Geschlossen

#56.301 geöffnet am 27.11.2020

 (11 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)Swift (10.719 Forks)batch import
SILOptimizercompilergood first issueimprovement

Repository-Metriken

Stars
 (69.989 Sterne)
PR-Merge-Metriken
 (Durchschn. Merge 8T 17h) (510 gemergte PRs in 30 T)

Beschreibung

Previous ID SR-13903
Radar rdar://problem/71913175
Original Reporter @dan-zheng
Type Improvement
Votes 0
Component/s Compiler
Labels Improvement, SILOptimizer, StarterBug
Assignee sachinvas16 (JIRA)
Priority Medium

md5: 41f0f57e0ecea221993e51084b3f1975

Issue Description:

Forum question with context: https://forums.swift.org/t/make-applyinst-a-multiplevalueinstruction/42294


SIL supports instructions that return multiple values (i.e. multiple result {{SILValue}}s).

However, apply instructions don't return multiple values. Instead, they produce a single value, which may have a tuple type. This leads to much avoidable destructuring and restructuring of tuples, which complicates SIL transformations like autodiff:

// example.swift
@_silgen_name("foo")
func foo() -> (Int, Int) {
  (0, 0)
}

@_silgen_name("bar")
func bar() -> (Int, Int) {
  return foo()
}
$ swiftc -emit-silgen example.swift
sil hidden [ossa] @bar : $@convention(thin) () -> (Int, Int) {
bb0:
  %0 = function_ref @foo : $@convention(thin) () -> (Int, Int)
  %1 = apply %0() : $@convention(thin) () -> (Int, Int)
  (%2, %3) = destructure_tuple %1 : $(Int, Int) // this is not good
  %4 = tuple (%2 : $Int, %3 : $Int)
  return %4 : $(Int, Int)
}

Instead, we can change ApplyInst to inherit MultipleResultInstruction.

Steps:

1. Change definition: make ApplyInst inherit MultipleValueInstruction.

Fix obvious compilation errors in the compiler codebase. Fix as many tests as possible.

2. Update and fix ApplyInst users: SIL generation + parsing + verification + printing, analyses and transformations, IRGen.

Contributor Guide