Skip to content

Commit fa98212

Browse files
committed
[AddressLowering] Look through opaque result type.
A function which returns a value of opaque result type func f() -> some P { S() } has a lowered, substituted signature @convention(thin) () -> @out @_opaqueReturnTypeOf("$s4main1fQryF", 0) opaque featuring an _opaqueReturnTypeOf attr. The SILFunctionArgument for that @out result, however, must be of the type that the opaque result substitutes to in the expansion context of the function.
1 parent faf4021 commit fa98212

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/SILOptimizer/Mandatory/AddressLowering.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,9 @@ static unsigned insertIndirectReturnArgs(AddressLoweringState &pass) {
689689

690690
unsigned argIdx = 0;
691691
for (auto resultTy : pass.loweredFnConv.getIndirectSILResultTypes(typeCtx)) {
692-
auto bodyResultTy = pass.function->mapTypeIntoContext(resultTy);
692+
auto resultTyInContext = pass.function->mapTypeIntoContext(resultTy);
693+
auto bodyResultTy = pass.function->getModule().Types.getLoweredType(
694+
resultTyInContext.getASTType(), *pass.function);
693695
auto var = new (astCtx) ParamDecl(
694696
SourceLoc(), SourceLoc(), astCtx.getIdentifier("$return_value"),
695697
SourceLoc(), astCtx.getIdentifier("$return_value"), declCtx);

test/SILOptimizer/address_lowering.sil

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ protocol P {
1818
func foo()
1919
}
2020

21+
struct S : P {
22+
func foo()
23+
}
24+
2125
enum Optional<T> {
2226
case none
2327
case some(T)
@@ -549,6 +553,23 @@ bb0(%0 : @owned $T, %1 : @owned $C):
549553
return %2 : $(T, C)
550554
}
551555

556+
// CHECK-LABEL: sil [ossa] @f076_opaqueResult : {{.*}} {
557+
// CHECK: {{bb[0-9]+}}([[S_ADDR:%[^,]+]] : $*S):
558+
// CHECK: [[GET:%[^,]+]] = function_ref @getT
559+
// CHECK: [[TEMP:%[^,]+]] = alloc_stack $S
560+
// CHECK: apply [[GET]]<S>([[TEMP]])
561+
// CHECK: [[S:%[^,]+]] = load [trivial] [[TEMP]]
562+
// CHECK: store [[S]] to [trivial] [[S_ADDR]]
563+
// CHECK: dealloc_stack [[TEMP]]
564+
// CHECK-LABEL: } // end sil function 'f076_opaqueResult'
565+
// The following getS definition enables use of the type <<@_opaqueReturnTypeOf("$s5Swift4getSQryF", 0) opaque>>
566+
@_silgen_name("getS") public func getS() -> some P { return S() }
567+
sil [ossa] @f076_opaqueResult : $@convention(thin) <T> () -> @out @_opaqueReturnTypeOf("$s5Swift4getSQryF", 0) __<T> {
568+
%get = function_ref @getT : $@convention(thin) <T> () -> (@out T)
569+
%s = apply %get<S>() : $@convention(thin) <T> () -> (@out T)
570+
return %s : $S
571+
}
572+
552573
// CHECK-LABEL: sil [ossa] @f080_optional : $@convention(thin) <T> (@in T) -> @out Optional<T> {
553574
// CHECK: bb0(%0 : $*Optional<T>, %1 : $*T):
554575
// CHECK: [[DATA:%.*]] = init_enum_data_addr %0 : $*Optional<T>, #Optional.some!enumelt

0 commit comments

Comments
 (0)