Skip to content
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
17 changes: 17 additions & 0 deletions lib/SILOptimizer/SILCombiner/SILCombine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,14 @@ bool SILCombiner::doOneIteration(SILFunction &F, unsigned Iteration) {
return MadeChange;
}

static bool hasAddressOperands(SILInstruction *inst) {
for (Operand *op : inst->getRealOperands()) {
if (op->get()->getType().isAddress())
return true;
}
return false;
}

void SILCombiner::processInstruction(SILInstruction *I,
SILCombineCanonicalize &scCanonicalize,
bool &MadeChange) {
Expand Down Expand Up @@ -467,6 +475,15 @@ void SILCombiner::processInstruction(SILInstruction *I,
if (auto *svi = dyn_cast<SingleValueInstruction>(I)) {
if (auto fwdOp = ForwardingOperation(svi)) {
if (fwdOp.getSingleForwardingOperand() &&

// Don't risk sinking instructions with address operands out of the
// addressed memory's lifetime. E.g:
// ```
// %3 = mark_dependence %2 on %1 : $*T // must not be moved after the destroy_addr
// destroy_addr %1
// ```
!hasAddressOperands(svi) &&

SILValue(svi)->getOwnershipKind() == OwnershipKind::Owned) {
// Try to sink the value. If we sank the value and deleted it,
// return. If we didn't optimize or sank but we are still able to
Expand Down
20 changes: 20 additions & 0 deletions test/SILOptimizer/sil_combine_ossa.sil
Original file line number Diff line number Diff line change
Expand Up @@ -5577,4 +5577,24 @@ bb0(%0 : @guaranteed $String):
return %1
}

// CHECK-LABEL: sil [ossa] @dont_sink_mark_dependence_out_of_memory_lifetime :
// CHECK: mark_dependence
// CHECK: bb1:
// CHECK: } // end sil function 'dont_sink_mark_dependence_out_of_memory_lifetime'
sil [ossa] @dont_sink_mark_dependence_out_of_memory_lifetime : $@convention(thin) (@owned Klass, @owned Klass) -> () {
bb0(%0 : @owned $Klass, %1 : @owned $Klass):
%2 = alloc_stack $Klass
store %1 to [init] %2
%4 = mark_dependence %0 on %2
destroy_addr %2
br bb1

bb1:
destroy_value %4
dealloc_stack %2
%9 = tuple ()
return %9
}