Skip to content

Commit aa2cfd3

Browse files
authored
Merge pull request #78361 from eeckstein/dead-obj-elimination
DeadObjectElimination: handle OSSA instructions when analyzing class destructors
2 parents 95f1992 + 41dd3dc commit aa2cfd3

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

lib/SIL/Utils/InstructionUtils.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ SILValue swift::stripCasts(SILValue v) {
172172
if (auto *svi = dyn_cast<SingleValueInstruction>(v)) {
173173
if (isIdentityPreservingRefCast(svi) ||
174174
isa<UncheckedTrivialBitCastInst>(v) || isa<MarkDependenceInst>(v) ||
175+
isa<UncheckedOwnershipConversionInst>(v) ||
175176
isa<BeginAccessInst>(v)) {
176177
v = cast<SingleValueInstruction>(v)->getOperand(0);
177178
continue;

lib/SILOptimizer/Transforms/DeadObjectElimination.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ static DestructorEffects doesDestructorHaveSideEffects(AllocRefInstBase *ARI) {
204204
continue;
205205
}
206206

207+
if (isa<BeginBorrowInst>(I) || isa<EndBorrowInst>(I) || isa<EndLifetimeInst>(I)) {
208+
continue;
209+
}
210+
207211
// dealloc_ref on self can be ignored, but dealloc_ref on anything else
208212
// cannot be eliminated.
209213
if (auto *DeallocRef = dyn_cast<DeallocRefInst>(&I)) {

test/SILOptimizer/dead_alloc_elim_ossa.sil

+38
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ class ArrayStorage {
2828
init()
2929
}
3030

31+
class Kl2 {
32+
@_hasStorage var i: Int
33+
}
34+
35+
3136
protocol P { }
3237
struct X : P { }
3338

@@ -65,6 +70,19 @@ bb0(%0 : @owned $NontrivialDestructor):
6570
return %7 : $()
6671
}
6772

73+
sil [ossa] @$s4main3Kl2CfD : $@convention(method) (@owned Kl2) -> () {
74+
bb0(%0 : @owned $Kl2):
75+
%1 = begin_borrow %0
76+
%2 = unchecked_ref_cast %1 to $Builtin.NativeObject
77+
%3 = unchecked_ownership_conversion %2, @guaranteed to @owned
78+
end_borrow %1
79+
end_lifetime %0
80+
%6 = unchecked_ref_cast %3 to $Kl2
81+
dealloc_ref %6
82+
%8 = tuple ()
83+
return %8
84+
}
85+
6886
sil @ptr_user : $@convention(thin) (Builtin.NativeObject) -> ()
6987
sil @int_user : $@convention(thin) (Builtin.Int32) -> ()
7088

@@ -541,3 +559,23 @@ bb0(%0 : @owned $_ContiguousArrayStorage<Element>):
541559
return %15 : $()
542560
}
543561

562+
// CHECK-LABEL: sil [ossa] @dead_alloc_with_complex_ossa_destructor
563+
// CHECK-NOT: alloc_ref
564+
// CHECK: } // end sil function 'dead_alloc_with_complex_ossa_destructor'
565+
sil [ossa] @dead_alloc_with_complex_ossa_destructor : $@convention(thin) () -> Int {
566+
bb0:
567+
%0 = alloc_ref $Kl2
568+
%1 = move_value [lexical] %0
569+
%2 = end_init_let_ref %1
570+
%3 = begin_borrow %2
571+
%4 = integer_literal $Builtin.Int64, 27
572+
%5 = struct $Int (%4)
573+
%6 = ref_element_addr %3, #Kl2.i
574+
store %5 to [trivial] %6
575+
end_borrow %3
576+
%9 = move_value [lexical] [var_decl] %2
577+
debug_value %9, let, name "k"
578+
destroy_value %9
579+
return %5
580+
}
581+

0 commit comments

Comments
 (0)