Skip to content

[AddressLowering] Handle builtins for resumes. #62443

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

Merged
Merged
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
10 changes: 10 additions & 0 deletions lib/SILOptimizer/Mandatory/AddressLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2930,6 +2930,16 @@ class UseRewriter : SILInstructionVisitor<UseRewriter> {

void visitBuiltinInst(BuiltinInst *bi) {
switch (bi->getBuiltinKind().value_or(BuiltinValueKind::None)) {
case BuiltinValueKind::ResumeNonThrowingContinuationReturning: {
SILValue opAddr = addrMat.materializeAddress(use->get());
bi->setOperand(1, opAddr);
break;
}
case BuiltinValueKind::ResumeThrowingContinuationReturning: {
SILValue opAddr = addrMat.materializeAddress(use->get());
bi->setOperand(1, opAddr);
break;
}
case BuiltinValueKind::Copy: {
SILValue opAddr = addrMat.materializeAddress(use->get());
bi->setOperand(0, opAddr);
Expand Down
40 changes: 40 additions & 0 deletions test/SILOptimizer/opaque_values_Onone_stdlib.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// Like opaque_values_Onone.swift but for code that needs to be compiled with
// -parse-stdlib.

protocol Error {}
enum Never : Error{}

precedencegroup AssignmentPrecedence { assignment: true }
precedencegroup CastingPrecedence {}

Expand Down Expand Up @@ -97,3 +100,40 @@ func getAnotherType<T, U>(_ object: inout T, to ty: U.Type) -> U {
func isOfTypeOfAnyObjectType(fromAny any: Any) -> Bool {
type(of: any) is Builtin.AnyObject.Type
}

@available(SwiftStdlib 5.1, *)
struct UnsafeContinuation<T, E: Error> {
@usableFromInline internal var context: Builtin.RawUnsafeContinuation

// CHECK-LABEL: sil {{.*}}@unsafeContinuationResumeNoThrow : {{.*}} {
// CHECK: {{bb[0-9]+}}([[VALUE:%[^,]+]] : $*T, [[CONTINUATION:%[^,]+]] : $UnsafeContinuation<T, Never>):
// CHECK: [[STACK:%[^,]+]] = alloc_stack $T
// CHECK: [[CONTEXT:%[^,]+]] = struct_extract [[CONTINUATION]]
// CHECK: copy_addr [[VALUE]] to [init] [[STACK]]
// CHECK: builtin "resumeNonThrowingContinuationReturning"<T>([[CONTEXT]] : $Builtin.RawUnsafeContinuation, [[STACK]] : $*T)
// CHECK: destroy_addr [[VALUE]]
// CHECK-LABEL: } // end sil function 'unsafeContinuationResumeNoThrow'
@_silgen_name("unsafeContinuationResumeNoThrow")
@_alwaysEmitIntoClient
public func resume(returning value: __owned T) where E == Never {
#if compiler(>=5.5) && $BuiltinContinuation
Builtin.resumeNonThrowingContinuationReturning(context, value)
#endif
}

// CHECK-LABEL: sil {{.*}}@unsafeContinuationResumeThrow : {{.*}} {
// CHECK: {{bb[0-9]+}}([[VALUE:%[^,]+]] : $*T, [[CONTINUATION:%[^,]+]] : $UnsafeContinuation<T, E>):
// CHECK: [[STACK:%[^,]+]] = alloc_stack $T
// CHECK: [[CONTEXT:%[^,]+]] = struct_extract [[CONTINUATION]]
// CHECK: copy_addr [[VALUE]] to [init] [[STACK]]
// CHECK: builtin "resumeThrowingContinuationReturning"<T>([[CONTEXT]] : $Builtin.RawUnsafeContinuation, [[STACK]] : $*T)
// CHECK: destroy_addr [[VALUE]]
// CHECK-LABEL: } // end sil function 'unsafeContinuationResumeThrow'
@_silgen_name("unsafeContinuationResumeThrow")
@_alwaysEmitIntoClient
public func resume(returning value: __owned T) {
#if compiler(>=5.5) && $BuiltinContinuation
Builtin.resumeThrowingContinuationReturning(context, value)
#endif
}
}