Skip to content

SILGen: Don't reabstract a member ref we're about to directly apply. #38658

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
21 changes: 21 additions & 0 deletions lib/SILGen/SILGenApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,27 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
}
}

void visitMemberRefExpr(MemberRefExpr *e) {
// If we're loading a closure-type property out of a generic aggregate,
// we might reabstract it under normal circumstances, but since we're
// going to apply it immediately here, there's no reason to. We can
// invoke the function value at whatever abstraction level we get.
assert(isa<VarDecl>(e->getMember().getDecl()));

// Any writebacks for this access are tightly scoped.
FormalEvaluationScope scope(SGF);

LValue lv = SGF.emitLValue(e, SGFAccessKind::OwnedObjectRead);
if (lv.isLastComponentTranslation())
lv.dropLastTranslationComponent();

ManagedValue fn = SGF.emitLoadOfLValue(e, std::move(lv), SGFContext())
.getAsSingleValue(SGF, e);

setCallee(Callee::forIndirect(fn, lv.getOrigFormalType(),
cast<FunctionType>(lv.getSubstFormalType()), e));
}

void visitAbstractClosureExpr(AbstractClosureExpr *e) {
// Emit the closure body.
SGF.SGM.emitClosure(e);
Expand Down
10 changes: 4 additions & 6 deletions test/SILGen/reabstract.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,13 @@ func testInoutOpaque(_ c: C, i: Int) {
// CHECK: function_ref @$s10reabstract1CCSiIegly_ACSiytIeglnr_TR
// CHECK: partial_apply
// CHECK: store
// CHECK: load
// CHECK: function_ref @$s10reabstract1CCSiytIeglnr_ACSiIegly_TR
// CHECK: partial_apply
// CHECK: apply
// CHECK: [[CLOSURE:%.*]] = struct_extract {{.*}}, #Box.t
// CHECK: [[CLOSURE1:%.*]] = copy_value [[CLOSURE]]
// CHECK: [[CLOSURE2:%.*]] = begin_borrow [[CLOSURE1]]
// CHECK: apply [[CLOSURE2]]
// CHECK: } // end sil function '$s10reabstract15testInoutOpaque_1iyAA1CC_SitF'

// CHECK-LABEL: sil shared [transparent] [serializable] [reabstraction_thunk] [ossa] @$s10reabstract1CCSiIegly_ACSiytIeglnr_TR : $@convention(thin) (@inout C, @in_guaranteed Int, @guaranteed @callee_guaranteed (@inout C, Int) -> ()) -> @out () {
// CHECK-LABEL: sil shared [transparent] [serializable] [reabstraction_thunk] [ossa] @$s10reabstract1CCSiytIeglnr_ACSiIegly_TR : $@convention(thin) (@inout C, Int, @guaranteed @callee_guaranteed (@inout C, @in_guaranteed Int) -> @out ()) -> () {

func closureTakingOptional(_ fn: (Int?) -> ()) {}
closureTakingOptional({ (_: Any) -> () in })
Expand All @@ -107,7 +106,6 @@ closureTakingOptional({ (_: Any) -> () in })
func evenLessFun(_ s: __shared C, _ o: __owned C) {}

// CHECK-LABEL: sil shared [transparent] [serializable] [reabstraction_thunk] [ossa] @$s10reabstract1CCACIeggx_A2CytIegnir_TR : $@convention(thin) (@in_guaranteed C, @in C, @guaranteed @callee_guaranteed (@guaranteed C, @owned C) -> ()) -> @out ()
// CHECK-LABEL: sil shared [transparent] [serializable] [reabstraction_thunk] [ossa] @$s10reabstract1CCACytIegnir_A2CIeggx_TR : $@convention(thin) (@guaranteed C, @owned C, @guaranteed @callee_guaranteed (@in_guaranteed C, @in C) -> @out ()) -> ()
func testSharedOwnedOpaque(_ s: C, o: C) {
let box = Box(t: evenLessFun)
box.t(s, o)
Expand Down
3 changes: 2 additions & 1 deletion test/SILGen/same_type_abstraction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ struct S1 {}
struct S2 {}

// CHECK-LABEL: sil hidden [ossa] @$s21same_type_abstraction28callClosureWithConcreteTypes{{[_0-9a-zA-Z]*}}F
// CHECK: function_ref @$s{{.*}}TR :
// CHECK-NOT: function_ref @$s{{.*}}TR :
// CHECK: apply {{.*}} : ${{.*}}(@in_guaranteed
func callClosureWithConcreteTypes<T: Associated, U: Associated>(x: Abstracted<T, U>, arg: S1) -> S2 where T.Assoc == S1, U.Assoc == S2 {
return x.closure(arg)
}
Expand Down