Skip to content

Commit 397a040

Browse files
committed
Don't try to devirtualize methods if the target has no error result but the original class method call was a try_apply
The code handling the devirtualization does not handle this case. rdar://44710251
1 parent d026b10 commit 397a040

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

lib/SILOptimizer/Utils/Devirtualize.cpp

Lines changed: 9 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

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)