Skip to content

Commit

Permalink
Merge pull request #77583 from eeckstein/fix-dce
Browse files Browse the repository at this point in the history
DeadCodeElimination: don't remove end_lifetime instructions with address operands
  • Loading branch information
eeckstein authored Nov 13, 2024
2 parents 2ceb02c + 64698ca commit 6d4db0e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/SILOptimizer/Transforms/DeadCodeElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,11 @@ void DCE::markLive() {
break;
}
case SILInstructionKind::EndLifetimeInst: {
if (I.getOperand(0)->getType().isAddress()) {
// DCE cannot reason about values in memory.
markInstructionLive(&I);
break;
}
// The instruction is live only if it's operand value is also live
addReverseDependency(I.getOperand(0), &I);
break;
Expand Down
22 changes: 21 additions & 1 deletion test/SILOptimizer/dead_code_elimination_ossa.sil
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ struct CAndBit {
}

struct MO: ~Copyable {
deinit
var x: Int
deinit
}

struct Outer : ~Copyable {
var x: MO
}

sil @dummy : $@convention(thin) () -> ()
Expand Down Expand Up @@ -487,3 +492,18 @@ sil [ossa] @dont_delete_move_value_lexical : $@convention(thin) () -> () {
%retval = tuple ()
return %retval : $()
}

// CHECK-LABEL: sil [ossa] @dont_remove_addr_end_lifetime :
// CHECK: end_lifetime
// CHECK-LABEL: } // end sil function 'dont_remove_addr_end_lifetime'
sil [ossa] @dont_remove_addr_end_lifetime : $@convention(thin) (@owned Outer) -> () {
bb0(%0 : @owned $Outer):
%1 = alloc_stack $Outer
store %0 to [init] %1 : $*Outer
%3 = struct_element_addr %1 : $*Outer, #Outer.x
end_lifetime %3 : $*MO
dealloc_stack %1 : $*Outer
%6 = tuple ()
return %6 : $()
}

0 comments on commit 6d4db0e

Please sign in to comment.