Skip to content

[Callee analysis] Consider witness thunks when known. #12334

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
19 changes: 16 additions & 3 deletions lib/SILOptimizer/Analysis/BasicCalleeAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,22 @@ void CalleeCache::computeWitnessMethodCalleesForWitnessTable(

TheCallees.getPointer()->push_back(WitnessFn);

// FIXME: For now, conservatively assume that unknown functions
// can be called from any witness_method call site.
TheCallees.setInt(true);
// If we can't resolve the witness, conservatively assume it can call
// anything.
if (!Requirement.getDecl()->isProtocolRequirement() ||
!WT.getConformance()->hasWitness(Requirement.getDecl())) {
TheCallees.setInt(true);
continue;
}

auto Witness = WT.getConformance()->getWitness(Requirement.getDecl(),
nullptr);
auto DeclRef = SILDeclRef(Witness.getDecl());

bool canCallUnknown = !calleesAreStaticallyKnowable(M, DeclRef);

if (canCallUnknown)
TheCallees.setInt(true);
}
}

Expand Down
7 changes: 4 additions & 3 deletions test/SILOptimizer/basic-callee-printer.sil
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ bb0(%0 : $*private_proto_private_class):
// CHECK: Function call site:
// CHECK: %2 = witness_method $T, #private_proto_1.theMethod!1 : {{.*}} : $@convention(witness_method) <τ_0_0 where τ_0_0 : private_proto_1> (@in_guaranteed τ_0_0) -> (){{.*}} // user: %3
// CHECK: %3 = apply %2<T>(%0) : $@convention(witness_method) <τ_0_0 where τ_0_0 : private_proto_1> (@in_guaranteed τ_0_0) -> ()
// CHECK: Incomplete callee list? : Yes
// CHECK: Incomplete callee list? : No
// CHECK: Known callees:
// CHECK: private_proto_1_private_class_witness
sil private @call_through_private_proto_1 : $@convention(thin) <T where T : private_proto_1> (@in T) -> () {
Expand Down Expand Up @@ -468,7 +468,8 @@ bb0(%0 : $*private_proto_internal_class):
// CHECK: Function call site:
// CHECK: %2 = witness_method $T, #private_proto_2.theMethod!1 : {{.*}} : $@convention(witness_method) <τ_0_0 where τ_0_0 : private_proto_2> (@in_guaranteed τ_0_0) -> (){{.*}} // user: %3
// CHECK: %3 = apply %2<T>(%0) : $@convention(witness_method) <τ_0_0 where τ_0_0 : private_proto_2> (@in_guaranteed τ_0_0) -> ()
// CHECK: Incomplete callee list? : Yes
// CHECK-WMO: Incomplete callee list? : No
// CHECK-NOWMO: Incomplete callee list? : Yes
// CHECK: Known callees:
// CHECK: private_proto_2_internal_class_witness
sil private @call_through_private_proto_2 : $@convention(thin) <T where T : private_proto_2> (@in T) -> () {
Expand Down Expand Up @@ -552,7 +553,7 @@ bb0(%0 : $*private_proto_public_class_private_method):
// CHECK: Function call site:
// CHECK: %2 = witness_method $T, #private_proto_4.theMethod!1 : {{.*}} : $@convention(witness_method) <τ_0_0 where τ_0_0 : private_proto_4> (@in_guaranteed τ_0_0) -> (){{.*}} // user: %3
// CHECK: %3 = apply %2<T>(%0) : $@convention(witness_method) <τ_0_0 where τ_0_0 : private_proto_4> (@in_guaranteed τ_0_0) -> ()
// CHECK: Incomplete callee list? : Yes
// CHECK: Incomplete callee list? : No
// CHECK: Known callees:
// CHECK: private_proto_4_public_class_private_method_witness
sil private @call_through_private_proto_4 : $@convention(thin) <T where T : private_proto_4> (@in T) -> () {
Expand Down