Skip to content

Commit 25a4d1e

Browse files
Merge pull request #20126 from aschwaighofer/devirt_non_error_try_apply_fix
Don't try to devirtualize methods if the target has no error result b…
2 parents b278f92 + 694c4b2 commit 25a4d1e

File tree

3 files changed

+111
-3
lines changed

3 files changed

+111
-3
lines changed

lib/SILOptimizer/Utils/Devirtualize.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,15 @@ bool swift::canDevirtualizeClassMethod(FullApplySite AI,
670670
return false;
671671
}
672672

673+
// devirtualizeClassMethod below does not support this case. It currently
674+
// assumes it can try_apply call the target.
675+
if (!F->getLoweredFunctionType()->hasErrorResult() &&
676+
isa<TryApplyInst>(AI.getInstruction())) {
677+
LLVM_DEBUG(llvm::dbgs() << " FAIL: Trying to devirtualize a "
678+
"try_apply but vtable entry has no error result.\n");
679+
return false;
680+
}
681+
673682
return true;
674683
}
675684

@@ -975,6 +984,15 @@ static bool canDevirtualizeWitnessMethod(ApplySite AI) {
975984
return false;
976985
}
977986

987+
// devirtualizeWitnessMethod below does not support this case. It currently
988+
// assumes it can try_apply call the target.
989+
if (!F->getLoweredFunctionType()->hasErrorResult() &&
990+
isa<TryApplyInst>(AI.getInstruction())) {
991+
LLVM_DEBUG(llvm::dbgs() << " FAIL: Trying to devirtualize a "
992+
"try_apply but wtable entry has no error result.\n");
993+
return false;
994+
}
995+
978996
return true;
979997
}
980998

test/SILOptimizer/devirt_class_witness_method.sil

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,46 @@ sil_stage canonical
33
// RUN: %FileCheck -check-prefix=YAML -input-file=%t.opt.yaml %s
44

55
import Builtin
6-
6+
enum Error {}
77
protocol P {
88
func f()
9+
func g() throws
910
}
1011

1112
extension P {
1213
func f()
14+
func g() throws
1315
}
1416

1517
class C<T, U> : P {}
1618

1719
sil hidden_external [transparent] [thunk] @witness_thunk : $@convention(witness_method: P) <τ_0_0><τ_1_0, τ_1_1 where τ_0_0 : C<τ_1_0, τ_1_1>> (@in_guaranteed τ_0_0) -> ()
20+
sil hidden_external [transparent] [thunk] @witness_thunk2 : $@convention(witness_method: P) <τ_0_0><τ_1_0, τ_1_1 where τ_0_0 : C<τ_1_0, τ_1_1>> (@in_guaranteed τ_0_0) -> ()
1821

1922
// CHECK-LABEL: sil hidden @caller : $@convention(thin) <T, U> (@owned C<T, U>) -> ()
2023
// CHECK: [[FN:%.*]] = function_ref @witness_thunk
2124
// CHECK: apply [[FN]]<C<T, U>, T, U>(
2225
// CHECK: return
2326

27+
// CHECK-LABEL: sil hidden @caller2
28+
// CHECK: witness_method
29+
// CHECK: try_apply
30+
// CHECK: return
31+
2432
// YAML: --- !Passed
2533
// YAML-NEXT: Pass: sil-devirtualizer
2634
// YAML-NEXT: Name: sil.WitnessMethodDevirtualized
2735
// YAML-NEXT: DebugLoc:
2836
// YAML-NEXT: File: {{.*}}/devirt_class_witness_method.sil
29-
// YAML-NEXT: Line: 47
37+
// YAML-NEXT: Line: 55
3038
// YAML-NEXT: Column: 8
3139
// YAML-NEXT: Function: caller
3240
// YAML-NEXT: Args:
3341
// YAML-NEXT: - String: 'Devirtualized call to '
3442
// YAML-NEXT: - Method: '"witness_thunk"'
3543
// YAML-NEXT: DebugLoc:
3644
// YAML-NEXT: File: {{.*}}/devirt_class_witness_method.sil
37-
// YAML-NEXT: Line: 17
45+
// YAML-NEXT: Line: 19
3846
// YAML-NEXT: Column: 44
3947
// YAML-NEXT: ...
4048

@@ -51,10 +59,31 @@ bb0(%0 : $C<T, U>):
5159
return %9 : $()
5260
}
5361

62+
sil hidden @caller2 : $@convention(thin) <T, U> (@owned C<T, U>) -> () {
63+
bb0(%0 : $C<T, U>):
64+
strong_retain %0 : $C<T, U>
65+
%4 = alloc_stack $C<T, U>
66+
store %0 to %4 : $*C<T, U>
67+
%6 = witness_method $C<T, U>, #P.g!1 : <Self where Self : P> (Self) -> () throws -> () : $@convention(witness_method: P) <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> @error Error
68+
try_apply %6<C<T, U>>(%4) : $@convention(witness_method: P) <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> @error Error, normal bb1, error bb2
69+
70+
bb1(%8 : $()):
71+
br bb3
72+
73+
bb2(%11 : $Error):
74+
br bb3
75+
76+
bb3:
77+
dealloc_stack %4 : $*C<T, U>
78+
strong_release %0 : $C<T, U>
79+
%9 = tuple ()
80+
return %9 : $()
81+
}
5482
sil_vtable C {}
5583

5684
sil_witness_table hidden <T, U> C<T, U>: P module clsx {
5785
method #P.f!1: <Self where Self : P> (Self) -> () -> () : @witness_thunk
86+
method #P.g!1: <Self where Self : P> (Self) -> () throws -> () : @witness_thunk2
5887
}
5988

6089
sil_default_witness_table hidden P {

test/SILOptimizer/devirt_speculative.sil

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,64 @@ bb0(%0 : $Base):
154154
unreachable
155155
}
156156

157+
class Throw {
158+
func mayThrow() throws
159+
}
160+
161+
class S1 : Throw {
162+
override func mayThrow()
163+
}
164+
165+
class S2 : Throw {
166+
override func mayThrow() throws
167+
}
168+
169+
sil hidden [thunk] @$S4main4ThrowC8mayThrowyyKF : $@convention(method) (@guaranteed Throw) -> @error Error {
170+
bb0(%0 : $Throw):
171+
%1 = tuple ()
172+
return %1 : $()
173+
}
174+
175+
sil hidden @$S4main2S1C8mayThrowyyF : $@convention(method) (@guaranteed S1) -> () {
176+
bb0(%0 : $S1):
177+
%1 = tuple ()
178+
return %1 : $()
179+
}
180+
181+
sil hidden [thunk] [always_inline] @$S4main2S2C8mayThrowyyKF : $@convention(method) (@guaranteed S2) -> @error Error {
182+
bb0(%0 : $S2):
183+
%1 = tuple ()
184+
return %1 : $()
185+
}
186+
187+
// CHECK-LABEL: sil{{.*}} @test_devirt_of_throw_without_error
188+
// CHECK-NOT: checked_cast_br [exact] %0 : $Throw to $S1
189+
// CHECK: return
190+
191+
sil hidden [noinline] @test_devirt_of_throw_without_error : $@convention(thin) (@owned Throw) -> () {
192+
bb0(%0 : $Throw):
193+
%80 = class_method %0 : $Throw, #Throw.mayThrow!1 : (Throw) -> () throws -> (), $@convention(method) (@guaranteed Throw) -> @error Error // user: %81
194+
try_apply %80(%0) : $@convention(method) (@guaranteed Throw) -> @error Error, normal bb7, error bb6
195+
196+
bb6(%82 : $Error):
197+
br bb1
198+
199+
bb7(%84 : $()):
200+
br bb1
201+
202+
bb1:
203+
%3 = tuple ()
204+
return %3 : $()
205+
}
206+
207+
sil_vtable Throw {
208+
#Throw.mayThrow!1: (Throw) -> () throws -> () : @$S4main4ThrowC8mayThrowyyKF
209+
}
210+
211+
sil_vtable S1 {
212+
#Throw.mayThrow!1: (Throw) -> () throws -> () : @$S4main2S1C8mayThrowyyF [override]
213+
}
214+
215+
sil_vtable S2 {
216+
#Throw.mayThrow!1: (Throw) -> () throws -> () : @$S4main2S2C8mayThrowyyKF [override]
217+
}

0 commit comments

Comments
 (0)