Skip to content

Speculative Devirtualizer: Don't speculate _withUnsafeGuaranteedRef c… #8002

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
11 changes: 11 additions & 0 deletions lib/SILOptimizer/Transforms/SpeculativeDevirtualizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,17 @@ static bool tryToSpeculateTarget(FullApplySite AI,
if (CMI->isVolatile())
return false;

// Don't devirtualize withUnsafeGuaranteed 'self' as this would prevent
// retain/release removal.
// unmanged._withUnsafeGuaranteedRef { $0.method() }
if (auto *TupleExtract = dyn_cast<TupleExtractInst>(CMI->getOperand()))
if (auto *UnsafeGuaranteedSelf =
dyn_cast<BuiltinInst>(TupleExtract->getOperand()))
if (UnsafeGuaranteedSelf->getBuiltinKind() ==
BuiltinValueKind::UnsafeGuaranteed &&
TupleExtract->getFieldNo() == 0)
return false;

// Strip any upcasts off of our 'self' value, potentially leaving us
// with a value whose type is closer (in the class hierarchy) to the
// actual dynamic type.
Expand Down
20 changes: 20 additions & 0 deletions test/SILOptimizer/devirt_speculative.sil
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

sil_stage canonical

import Builtin

@objc class MyNSObject {}
private class Base : MyNSObject {
override init()
Expand Down Expand Up @@ -104,3 +106,21 @@ bb0(%0: $Base2):
// CHECK: checked_cast_br [exact] %0 : $Base2 to $Sub2, bb{{.*}}, bb[[GENCALL:[0-9]+]]
// CHECK: bb[[GENCALL]]{{.*}}:
// CHECK: apply [[METH]]

// Don't devirtualize 'unsafeGuaranteed' self calls.
// CHECK-LABEL: sil @test_unsafeGuaranteed
// CHECK-NOT: check_cast_br
// CHECK: return
sil @test_unsafeGuaranteed : $@convention(thin) (@guaranteed Base2) -> () {
bb0(%0: $Base2):
strong_retain %0 : $Base2
%4 = builtin "unsafeGuaranteed"<Base2>(%0 : $Base2) : $(Base2, Builtin.Int8)
%5 = tuple_extract %4 : $(Base2, Builtin.Int8), 0
%6 = tuple_extract %4 : $(Base2, Builtin.Int8), 1
%1 = class_method %0 : $Base2, #Base2.foo!1 : (Base2) -> () -> (), $@convention(method) (@guaranteed Base2) -> ()
%2 = apply %1(%5) : $@convention(method) (@guaranteed Base2) -> ()
strong_release %5 : $Base2
%16 = builtin "unsafeGuaranteedEnd"(%6 : $Builtin.Int8) : $()
%3 = tuple()
return %3 : $()
}