Skip to content

Commit fa5852c

Browse files
authored
Merge pull request #12334 from DougGregor/callee-analysis-witness-thunks
[Callee analysis] Consider witness thunks when known.
2 parents 7aa9839 + aad6b40 commit fa5852c

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

lib/SILOptimizer/Analysis/BasicCalleeAnalysis.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,22 @@ void CalleeCache::computeWitnessMethodCalleesForWitnessTable(
134134

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

137-
// FIXME: For now, conservatively assume that unknown functions
138-
// can be called from any witness_method call site.
139-
TheCallees.setInt(true);
137+
// If we can't resolve the witness, conservatively assume it can call
138+
// anything.
139+
if (!Requirement.getDecl()->isProtocolRequirement() ||
140+
!WT.getConformance()->hasWitness(Requirement.getDecl())) {
141+
TheCallees.setInt(true);
142+
continue;
143+
}
144+
145+
auto Witness = WT.getConformance()->getWitness(Requirement.getDecl(),
146+
nullptr);
147+
auto DeclRef = SILDeclRef(Witness.getDecl());
148+
149+
bool canCallUnknown = !calleesAreStaticallyKnowable(M, DeclRef);
150+
151+
if (canCallUnknown)
152+
TheCallees.setInt(true);
140153
}
141154
}
142155

test/SILOptimizer/basic-callee-printer.sil

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ bb0(%0 : $*private_proto_private_class):
425425
// CHECK: Function call site:
426426
// 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
427427
// CHECK: %3 = apply %2<T>(%0) : $@convention(witness_method) <τ_0_0 where τ_0_0 : private_proto_1> (@in_guaranteed τ_0_0) -> ()
428-
// CHECK: Incomplete callee list? : Yes
428+
// CHECK: Incomplete callee list? : No
429429
// CHECK: Known callees:
430430
// CHECK: private_proto_1_private_class_witness
431431
sil private @call_through_private_proto_1 : $@convention(thin) <T where T : private_proto_1> (@in T) -> () {
@@ -468,7 +468,8 @@ bb0(%0 : $*private_proto_internal_class):
468468
// CHECK: Function call site:
469469
// 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
470470
// CHECK: %3 = apply %2<T>(%0) : $@convention(witness_method) <τ_0_0 where τ_0_0 : private_proto_2> (@in_guaranteed τ_0_0) -> ()
471-
// CHECK: Incomplete callee list? : Yes
471+
// CHECK-WMO: Incomplete callee list? : No
472+
// CHECK-NOWMO: Incomplete callee list? : Yes
472473
// CHECK: Known callees:
473474
// CHECK: private_proto_2_internal_class_witness
474475
sil private @call_through_private_proto_2 : $@convention(thin) <T where T : private_proto_2> (@in T) -> () {
@@ -552,7 +553,7 @@ bb0(%0 : $*private_proto_public_class_private_method):
552553
// CHECK: Function call site:
553554
// 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
554555
// CHECK: %3 = apply %2<T>(%0) : $@convention(witness_method) <τ_0_0 where τ_0_0 : private_proto_4> (@in_guaranteed τ_0_0) -> ()
555-
// CHECK: Incomplete callee list? : Yes
556+
// CHECK: Incomplete callee list? : No
556557
// CHECK: Known callees:
557558
// CHECK: private_proto_4_public_class_private_method_witness
558559
sil private @call_through_private_proto_4 : $@convention(thin) <T where T : private_proto_4> (@in T) -> () {

0 commit comments

Comments
 (0)