Skip to content

Commit 80778de

Browse files
committed
AccessUtils: use the begin_apply result instead of the begin_apply instruction for a yield access base
A begin_apply can yield multiple addresses. We need to store the result of the apply in order to distinguish between two AccessBases with different results from the same begin_apply.
1 parent f623a87 commit 80778de

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/AccessUtils.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
// %l = load %s the access
2929
// ```
3030
//===----------------------------------------------------------------------===//
31+
3132
import SIL
3233

3334
/// AccessBase describes the base address of a memory access (e.g. of a `load` or `store``).
@@ -68,7 +69,7 @@ enum AccessBase : CustomStringConvertible, Hashable {
6869
case argument(FunctionArgument)
6970

7071
/// An indirect result of a `begin_apply`.
71-
case yield(BeginApplyInst)
72+
case yield(MultipleValueInstructionResult)
7273

7374
/// An address which is derived from a `Builtin.RawPointer`.
7475
case pointer(PointerToAddressInst)
@@ -86,8 +87,8 @@ enum AccessBase : CustomStringConvertible, Hashable {
8687
case let arg as FunctionArgument : self = .argument(arg)
8788
case let ga as GlobalAddrInst : self = .global(ga.global)
8889
case let mvr as MultipleValueInstructionResult:
89-
if let ba = mvr.parentInstruction as? BeginApplyInst, baseAddress.type.isAddress {
90-
self = .yield(ba)
90+
if mvr.parentInstruction is BeginApplyInst && baseAddress.type.isAddress {
91+
self = .yield(mvr)
9192
} else {
9293
self = .unidentified
9394
}
@@ -105,7 +106,7 @@ enum AccessBase : CustomStringConvertible, Hashable {
105106
case .class(let rea): return "class - \(rea)"
106107
case .tail(let rta): return "tail - \(rta.instance)"
107108
case .argument(let arg): return "argument - \(arg)"
108-
case .yield(let ba): return "yield - \(ba)"
109+
case .yield(let result): return "yield - \(result)"
109110
case .pointer(let p): return "pointer - \(p)"
110111
}
111112
}
@@ -180,8 +181,8 @@ enum AccessBase : CustomStringConvertible, Hashable {
180181
return gl1 == gl2
181182
case (.argument(let arg1), .argument(let arg2)):
182183
return arg1 == arg2
183-
case (.yield(let ba1), .yield(let ba2)):
184-
return ba1 == ba2
184+
case (.yield(let baResult1), .yield(let baResult2)):
185+
return baResult1 == baResult2
185186
case (.pointer(let p1), .pointer(let p2)):
186187
return p1 == p2
187188
default:

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public class SingleValueInstruction : Instruction, Value {
165165
}
166166
}
167167

168-
public final class MultipleValueInstructionResult : Value {
168+
public final class MultipleValueInstructionResult : Value, Hashable {
169169
public var parentInstruction: MultipleValueInstruction {
170170
bridged.getParent().getAs(MultipleValueInstruction.self)
171171
}
@@ -179,6 +179,14 @@ public final class MultipleValueInstructionResult : Value {
179179
var bridged: BridgedMultiValueResult {
180180
BridgedMultiValueResult(obj: SwiftObject(self))
181181
}
182+
183+
public static func ==(lhs: MultipleValueInstructionResult, rhs: MultipleValueInstructionResult) -> Bool {
184+
lhs === rhs
185+
}
186+
187+
public func hash(into hasher: inout Hasher) {
188+
hasher.combine(ObjectIdentifier(self))
189+
}
182190
}
183191

184192
extension BridgedMultiValueResult {

test/SILOptimizer/accessutils.sil

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,32 @@ bb0(%0 : $MyArray<String>):
338338
return %14 : $()
339339
}
340340

341+
sil @yieldTwoAddresses : $@yield_once @convention(thin) () -> (@yields @in_guaranteed String, @yields @in_guaranteed Int64)
342+
343+
// CHECK-LABEL: Accesses for testBeginApply
344+
// CHECK-NEXT: Value: (**%1**, %2, %3) = begin_apply %0() : $@yield_once @convention(thin) () -> (@yields @in_guaranteed String, @yields @in_guaranteed Int64) // user: %4
345+
// CHECK-NEXT: Scope: base
346+
// CHECK-NEXT: Base: yield - (**%1**, %2, %3) = begin_apply %0() : $@yield_once @convention(thin) () -> (@yields @in_guaranteed String, @yields @in_guaranteed Int64) // user: %4
347+
// CHECK-NEXT: Path: ""
348+
// CHECK-NEXT: no Storage paths
349+
// CHECK-NEXT: Value: (%1, **%2**, %3) = begin_apply %0() : $@yield_once @convention(thin) () -> (@yields @in_guaranteed String, @yields @in_guaranteed Int64) // user: %5
350+
// CHECK-NEXT: Scope: base
351+
// CHECK-NEXT: Base: yield - (%1, **%2**, %3) = begin_apply %0() : $@yield_once @convention(thin) () -> (@yields @in_guaranteed String, @yields @in_guaranteed Int64) // user: %5
352+
// CHECK-NEXT: Path: ""
353+
// CHECK-NEXT: no Storage paths
354+
// CHECK-NEXT: End accesses for testBeginApply
355+
sil @testBeginApply : $@convention(thin) () -> () {
356+
bb0:
357+
%0 = function_ref @yieldTwoAddresses : $@yield_once @convention(thin) () -> (@yields @in_guaranteed String, @yields @in_guaranteed Int64)
358+
(%1, %2, %3) = begin_apply %0() : $@yield_once @convention(thin) () -> (@yields @in_guaranteed String, @yields @in_guaranteed Int64)
359+
%4 = load %1 : $*String
360+
%5 = load %2 : $*Int64
361+
end_apply %3
362+
%7 = tuple ()
363+
return %7 : $()
364+
}
365+
366+
341367
sil_global @global1 : $Int64
342368
sil_global @global2 : $Int64
343369

0 commit comments

Comments
 (0)