Skip to content

[Mem2Reg] Replaced loop with getSingleSuccessor. #40020

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
33 changes: 23 additions & 10 deletions lib/SILOptimizer/Transforms/SILMem2Reg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,11 @@ StoreInst *StackAllocationPromoter::promoteAllocationInBlock(
runningVals = beginLexicalLifetimeAfterStore(asi, si);
// Create a use of the newly created copy in order to keep phi pruning
// from deleting our lifetime beginning instructions.
//
// TODO: Remove this hack, it is only necessary because erasePhiArgument
// calls deleteEdgeValue which calls
// deleteTriviallyDeadOperandsOfDeadArgument and deletes the copy
// and borrow that we added and want not to have deleted.
SILBuilderWithScope::insertAfter(
runningVals->value.copy->getDefiningInstruction(),
[&](auto builder) {
Expand Down Expand Up @@ -1665,22 +1670,30 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *asi) {
GraphNodeWorklist<SILBasicBlock *, 2> worklist;
worklist.initialize(parentBlock);
while (auto *block = worklist.pop()) {
assert(domInfo->dominates(parentBlock, block));
auto *terminator = block->getTerminator();
if (isa<UnreachableInst>(terminator)) {
endLexicalLifetimeBeforeInst(asi, /*beforeInstruction=*/terminator, ctx,
runningVals->value);
continue;
}
bool endedLifetime = false;
for (auto *successor : block->getSuccessorBlocks()) {
if (!domInfo->dominates(parentBlock, successor)) {
endLexicalLifetimeBeforeInst(asi, /*beforeInstruction=*/terminator,
ctx, runningVals->value);
endedLifetime = true;
break;
}
}
if (endedLifetime) {
SILBasicBlock *successor = nullptr;
// If any successor is not dominated by the parentBlock, then we must end
// the lifetime before that successor.
//
// Suppose that a successor is not dominated by parentBlock. Recall that
// block _is_ dominated by parentBlock. Thus that successor must have
// more than one predecessor: block, and at least one other. (Otherwise
// it would be dominated by parentBlock contrary to our assumption.)
// Recall that SIL does not allow critical edges. Therefore block has
// only a single successor.
//
// Use the above fact to only look for lack of domination of a successor
// if that successor is the single successor of block.
if ((successor = block->getSingleSuccessorBlock()) &&
(!domInfo->dominates(parentBlock, successor))) {
endLexicalLifetimeBeforeInst(asi, /*beforeInstruction=*/terminator, ctx,
runningVals->value);
continue;
}
for (auto *successor : block->getSuccessorBlocks()) {
Expand Down
11 changes: 7 additions & 4 deletions test/SILOptimizer/mem2reg_lifetime.sil
Original file line number Diff line number Diff line change
Expand Up @@ -1548,6 +1548,7 @@ entry(%instance : @owned $C):
}


// End lifetime before unreachable inst.
// CHECK-LABEL: sil [ossa] @unreachable_5 : $@convention(thin) (@owned C) -> () {
// CHECK: {{bb[0-9]+}}([[INSTANCE:%[^,]+]] : @owned $C):
// CHECK: [[LIFETIME:%[^,]+]] = begin_borrow [lexical] [[INSTANCE]]
Expand All @@ -1567,6 +1568,7 @@ exit:
unreachable
}

// Bail out if alloc_stack is in unreachable block.
// CHECK-LABEL: sil [ossa] @unreachable_6 : $@convention(thin) (@owned C) -> () {
// CHECK: alloc_stack [lexical]
// CHECK-LABEL: } // end sil function 'unreachable_6'
Expand All @@ -1585,6 +1587,7 @@ exit:
}


// Bail out if alloc_stack is in unreachable block.
// CHECK-LABEL: sil [ossa] @unreachable_7 : $@convention(thin) (@owned C) -> () {
// CHECK: alloc_stack [lexical]
// CHECK-LABEL: } // end sil function 'unreachable_7'
Expand All @@ -1606,7 +1609,7 @@ exit:
return %res : $()
}


// Bail out if alloc_stack is in unreachable block.
// CHECK-LABEL: sil [ossa] @unreachable_8 : $@convention(thin) (@owned C) -> () {
// CHECK: alloc_stack [lexical]
// CHECK-LABEL: } // end sil function 'unreachable_8'
Expand All @@ -1632,7 +1635,7 @@ exit:
return %res : $()
}


// End lifetime after loop which is followed by unreachable.
// CHECK-LABEL: sil [ossa] @unreachable_loop_1 : $@convention(thin) () -> () {
// CHECK: {{bb[0-9]+}}:
// CHECK: [[GET_C:%[^,]+]] = function_ref @getC : $@convention(thin) () -> @owned C
Expand Down Expand Up @@ -1672,7 +1675,7 @@ die:
unreachable
}


// Bail out if alloc_stack is in unreachable block.
// CHECK-LABEL: sil [ossa] @unreachable_loop_2 : $@convention(thin) () -> () {
// CHECK: alloc_stack [lexical]
// CHECK-LABEL: } // end sil function 'unreachable_loop_2'
Expand All @@ -1697,7 +1700,7 @@ die:
unreachable
}


// Bail out if alloc_stack is in unreachable block.
// CHECK-LABEL: sil [ossa] @unreachable_loop_3 : $@convention(thin) () -> () {
// CHECK: alloc_stack [lexical]
// CHECK-LABEL: } // end sil function 'unreachable_loop_3'
Expand Down