Skip to content

Commit bbf680d

Browse files
committed
forward direct return to direct results
1 parent edcab95 commit bbf680d

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

lib/SILGen/SILGenFunction.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -657,10 +657,11 @@ void SILGenFunction::emitGeneratorFunction(SILDeclRef function, VarDecl *var) {
657657
auto resultType = decl->mapTypeIntoContext(interfaceType)->getCanonicalType();
658658
auto origResultType = AbstractionPattern(resultType);
659659

660+
SmallVector<SILValue, 4> directResults;
661+
660662
if (F.getConventions().hasIndirectSILResults()) {
661663
Scope scope(Cleanups, CleanupLocation(var));
662664

663-
SmallVector<SILValue, 4> directResults;
664665
SmallVector<CleanupHandle, 4> cleanups;
665666
auto init = prepareIndirectResultInit(resultType, directResults, cleanups);
666667

@@ -671,18 +672,19 @@ void SILGenFunction::emitGeneratorFunction(SILDeclRef function, VarDecl *var) {
671672
for (auto cleanup : cleanups) {
672673
Cleanups.forwardCleanup(cleanup);
673674
}
674-
675-
Cleanups.emitBranchAndCleanups(ReturnDest, loc, directResults);
676675
} else {
676+
Scope scope(Cleanups, CleanupLocation(var));
677+
677678
// If we have no indirect results, just return the result.
678679
auto result = emitApplyOfStoredPropertyInitializer(loc, entry, subs,
679680
resultType,
680681
origResultType,
681682
SGFContext())
682-
.getAsSingleValue(*this, loc);
683-
B.createReturn(loc, result);
683+
.ensurePlusOne(*this, loc);
684+
std::move(result).forwardAll(*this, directResults);
684685
}
685686

687+
Cleanups.emitBranchAndCleanups(ReturnDest, loc, directResults);
686688
emitEpilog(loc);
687689
}
688690

test/SILGen/stored_property_default_arg.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,32 @@ func checkReferenceTypes() {
179179
// CHECK-NEXT: [[U2:%.*]] = apply [[U2_REF]]({{.*}}, {{.*}}, [[W1]], {{.*}}) : $@convention(method) (@owned Optional<T>, @owned T, @thin U.Type) -> @out U
180180
let y = U(v: T())
181181
}
182+
183+
// CHECK: default argument 0 of AA.init(ab:)
184+
// CHECK-NEXT: sil hidden [ossa] @$s27stored_property_default_arg2AAV2abAcA1ZCSg2ac_AG2adt_tcfcfA_ : $@convention(thin) () -> (@owned Optional<Z>, @owned Optional<Z>) {
185+
// CHECK-NEXT: bb0:
186+
// CHECK-NEXT: function_ref variable initialization expression of AA.ab
187+
// CHECK-NEXT: [[AA_AB_REF:%.*]] = function_ref @$s27stored_property_default_arg2AAV2abAA1ZCSg2ac_AG2adtvpfi : $@convention(thin) () -> (@owned Optional<Z>, @owned Optional<Z>)
188+
// CHECK-NEXT: [[AA_AB:%.*]] = apply [[AA_AB_REF]]() : $@convention(thin) () -> (@owned Optional<Z>, @owned Optional<Z>)
189+
// CHECK-NEXT: ([[ELT0:%.*]], [[ELT1:%.*]]) = destructure_tuple [[AA_AB]] : $(Optional<Z>, Optional<Z>)
190+
// CHECK-NEXT: [[RETURN:%.*]] = tuple ([[ELT0]] : $Optional<Z>, [[ELT1]] : $Optional<Z>)
191+
// CHECK-NEXT: return [[RETURN]] : $(Optional<Z>, Optional<Z>)
192+
// CHECK-NEXT: }
193+
194+
class Z {}
195+
196+
struct AA {
197+
var ab: (ac: Z?, ad: Z?) = (ac: Z(), ad: Z())
198+
}
199+
200+
// CHECK-LABEL: sil hidden [ossa] @$s27stored_property_default_arg19checkReferenceTupleyyF : $@convention(thin) () -> () {
201+
func checkReferenceTuple() {
202+
// CHECK: function_ref default argument 0 of AA.init(ab:)
203+
// CHECK-NEXT: [[AB1_REF:%.*]] = function_ref @$s27stored_property_default_arg2AAV2abAcA1ZCSg2ac_AG2adt_tcfcfA_ : $@convention(thin) () -> (@owned Optional<Z>, @owned Optional<Z>)
204+
// CHECK-NEXT: [[AB1:%.*]] = apply [[AB1_REF]]() : $@convention(thin) () -> (@owned Optional<Z>, @owned Optional<Z>)
205+
// CHECK-NEXT: ([[ELT0:%.*]], [[ELT1:%.*]]) = destructure_tuple [[AB1]] : $(Optional<Z>, Optional<Z>)
206+
// CHECK-NEXT: function_ref AA.init(ab:)
207+
// CHECK-NEXT: [[AA1_REF:%.*]] = function_ref @$s27stored_property_default_arg2AAV2abAcA1ZCSg2ac_AG2adt_tcfC : $@convention(method) (@owned Optional<Z>, @owned Optional<Z>, @thin AA.Type) -> @owned AA
208+
// CHECK-NEXT: [[AA1:%.*]] = apply [[AA1_REF]]([[ELT0]], [[ELT1]], {{.*}}) : $@convention(method) (@owned Optional<Z>, @owned Optional<Z>, @thin AA.Type) -> @owned AA
209+
let ae = AA.init(ab:)()
210+
}

0 commit comments

Comments
 (0)