Skip to content

Commit 5b8cd58

Browse files
Merge pull request #62443 from nate-chandler/opaque-values/1/20221207
[AddressLowering] Handle builtins for resumes.
2 parents becdbaa + b6e6c20 commit 5b8cd58

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

lib/SILOptimizer/Mandatory/AddressLowering.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2937,6 +2937,16 @@ class UseRewriter : SILInstructionVisitor<UseRewriter> {
29372937

29382938
void visitBuiltinInst(BuiltinInst *bi) {
29392939
switch (bi->getBuiltinKind().value_or(BuiltinValueKind::None)) {
2940+
case BuiltinValueKind::ResumeNonThrowingContinuationReturning: {
2941+
SILValue opAddr = addrMat.materializeAddress(use->get());
2942+
bi->setOperand(1, opAddr);
2943+
break;
2944+
}
2945+
case BuiltinValueKind::ResumeThrowingContinuationReturning: {
2946+
SILValue opAddr = addrMat.materializeAddress(use->get());
2947+
bi->setOperand(1, opAddr);
2948+
break;
2949+
}
29402950
case BuiltinValueKind::Copy: {
29412951
SILValue opAddr = addrMat.materializeAddress(use->get());
29422952
bi->setOperand(0, opAddr);

test/SILOptimizer/opaque_values_Onone_stdlib.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
// Like opaque_values_Onone.swift but for code that needs to be compiled with
44
// -parse-stdlib.
55

6+
protocol Error {}
7+
enum Never : Error{}
8+
69
precedencegroup AssignmentPrecedence { assignment: true }
710
precedencegroup CastingPrecedence {}
811

@@ -97,3 +100,40 @@ func getAnotherType<T, U>(_ object: inout T, to ty: U.Type) -> U {
97100
func isOfTypeOfAnyObjectType(fromAny any: Any) -> Bool {
98101
type(of: any) is Builtin.AnyObject.Type
99102
}
103+
104+
@available(SwiftStdlib 5.1, *)
105+
struct UnsafeContinuation<T, E: Error> {
106+
@usableFromInline internal var context: Builtin.RawUnsafeContinuation
107+
108+
// CHECK-LABEL: sil {{.*}}@unsafeContinuationResumeNoThrow : {{.*}} {
109+
// CHECK: {{bb[0-9]+}}([[VALUE:%[^,]+]] : $*T, [[CONTINUATION:%[^,]+]] : $UnsafeContinuation<T, Never>):
110+
// CHECK: [[STACK:%[^,]+]] = alloc_stack $T
111+
// CHECK: [[CONTEXT:%[^,]+]] = struct_extract [[CONTINUATION]]
112+
// CHECK: copy_addr [[VALUE]] to [init] [[STACK]]
113+
// CHECK: builtin "resumeNonThrowingContinuationReturning"<T>([[CONTEXT]] : $Builtin.RawUnsafeContinuation, [[STACK]] : $*T)
114+
// CHECK: destroy_addr [[VALUE]]
115+
// CHECK-LABEL: } // end sil function 'unsafeContinuationResumeNoThrow'
116+
@_silgen_name("unsafeContinuationResumeNoThrow")
117+
@_alwaysEmitIntoClient
118+
public func resume(returning value: __owned T) where E == Never {
119+
#if compiler(>=5.5) && $BuiltinContinuation
120+
Builtin.resumeNonThrowingContinuationReturning(context, value)
121+
#endif
122+
}
123+
124+
// CHECK-LABEL: sil {{.*}}@unsafeContinuationResumeThrow : {{.*}} {
125+
// CHECK: {{bb[0-9]+}}([[VALUE:%[^,]+]] : $*T, [[CONTINUATION:%[^,]+]] : $UnsafeContinuation<T, E>):
126+
// CHECK: [[STACK:%[^,]+]] = alloc_stack $T
127+
// CHECK: [[CONTEXT:%[^,]+]] = struct_extract [[CONTINUATION]]
128+
// CHECK: copy_addr [[VALUE]] to [init] [[STACK]]
129+
// CHECK: builtin "resumeThrowingContinuationReturning"<T>([[CONTEXT]] : $Builtin.RawUnsafeContinuation, [[STACK]] : $*T)
130+
// CHECK: destroy_addr [[VALUE]]
131+
// CHECK-LABEL: } // end sil function 'unsafeContinuationResumeThrow'
132+
@_silgen_name("unsafeContinuationResumeThrow")
133+
@_alwaysEmitIntoClient
134+
public func resume(returning value: __owned T) {
135+
#if compiler(>=5.5) && $BuiltinContinuation
136+
Builtin.resumeThrowingContinuationReturning(context, value)
137+
#endif
138+
}
139+
}

0 commit comments

Comments
 (0)