Skip to content

DeadObjectElimination: handle OSSA instructions when analyzing class destructors #78361

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 1 commit into from
Jan 2, 2025
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
1 change: 1 addition & 0 deletions lib/SIL/Utils/InstructionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ SILValue swift::stripCasts(SILValue v) {
if (auto *svi = dyn_cast<SingleValueInstruction>(v)) {
if (isIdentityPreservingRefCast(svi) ||
isa<UncheckedTrivialBitCastInst>(v) || isa<MarkDependenceInst>(v) ||
isa<UncheckedOwnershipConversionInst>(v) ||
isa<BeginAccessInst>(v)) {
v = cast<SingleValueInstruction>(v)->getOperand(0);
continue;
Expand Down
4 changes: 4 additions & 0 deletions lib/SILOptimizer/Transforms/DeadObjectElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ static DestructorEffects doesDestructorHaveSideEffects(AllocRefInstBase *ARI) {
continue;
}

if (isa<BeginBorrowInst>(I) || isa<EndBorrowInst>(I) || isa<EndLifetimeInst>(I)) {
continue;
}

// dealloc_ref on self can be ignored, but dealloc_ref on anything else
// cannot be eliminated.
if (auto *DeallocRef = dyn_cast<DeallocRefInst>(&I)) {
Expand Down
38 changes: 38 additions & 0 deletions test/SILOptimizer/dead_alloc_elim_ossa.sil
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class ArrayStorage {
init()
}

class Kl2 {
@_hasStorage var i: Int
}


protocol P { }
struct X : P { }

Expand Down Expand Up @@ -65,6 +70,19 @@ bb0(%0 : @owned $NontrivialDestructor):
return %7 : $()
}

sil [ossa] @$s4main3Kl2CfD : $@convention(method) (@owned Kl2) -> () {
bb0(%0 : @owned $Kl2):
%1 = begin_borrow %0
%2 = unchecked_ref_cast %1 to $Builtin.NativeObject
%3 = unchecked_ownership_conversion %2, @guaranteed to @owned
end_borrow %1
end_lifetime %0
%6 = unchecked_ref_cast %3 to $Kl2
dealloc_ref %6
%8 = tuple ()
return %8
}

sil @ptr_user : $@convention(thin) (Builtin.NativeObject) -> ()
sil @int_user : $@convention(thin) (Builtin.Int32) -> ()

Expand Down Expand Up @@ -541,3 +559,23 @@ bb0(%0 : @owned $_ContiguousArrayStorage<Element>):
return %15 : $()
}

// CHECK-LABEL: sil [ossa] @dead_alloc_with_complex_ossa_destructor
// CHECK-NOT: alloc_ref
// CHECK: } // end sil function 'dead_alloc_with_complex_ossa_destructor'
sil [ossa] @dead_alloc_with_complex_ossa_destructor : $@convention(thin) () -> Int {
bb0:
%0 = alloc_ref $Kl2
%1 = move_value [lexical] %0
%2 = end_init_let_ref %1
%3 = begin_borrow %2
%4 = integer_literal $Builtin.Int64, 27
%5 = struct $Int (%4)
%6 = ref_element_addr %3, #Kl2.i
store %5 to [trivial] %6
end_borrow %3
%9 = move_value [lexical] [var_decl] %2
debug_value %9, let, name "k"
destroy_value %9
return %5
}