Skip to content

[AddressLowering] Omit dealloc_stack in dead ends. #62453

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
15 changes: 12 additions & 3 deletions lib/SILOptimizer/Mandatory/AddressLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILValue.h"
#include "swift/SIL/SILVisitor.h"
#include "swift/SILOptimizer/Analysis/DeadEndBlocksAnalysis.h"
#include "swift/SILOptimizer/Analysis/PostOrderAnalysis.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
#include "swift/SILOptimizer/Utils/BasicBlockOptUtils.h"
Expand Down Expand Up @@ -413,6 +414,9 @@ struct AddressLoweringState {
// Dominators remain valid throughout this pass.
DominanceInfo *domInfo;

// Dead-end blocks remain valid through this pass.
DeadEndBlocks *deBlocks;

InstructionDeleter deleter;

// All opaque values mapped to their associated storage.
Expand Down Expand Up @@ -441,9 +445,10 @@ struct AddressLoweringState {
// Handle moves from a phi's operand storage to the phi storage.
std::unique_ptr<PhiRewriter> phiRewriter;

AddressLoweringState(SILFunction *function, DominanceInfo *domInfo)
AddressLoweringState(SILFunction *function, DominanceInfo *domInfo,
DeadEndBlocks *deBlocks)
: function(function), loweredFnConv(getLoweredFnConv(function)),
domInfo(domInfo) {
domInfo(domInfo), deBlocks(deBlocks) {
for (auto &block : *function) {
if (block.getTerminator()->isFunctionExiting())
exitingInsts.push_back(block.getTerminator());
Expand Down Expand Up @@ -1332,6 +1337,8 @@ void OpaqueStorageAllocation::finalizeOpaqueStorage() {
// a use projection.
computeDominatedBoundaryBlocks(alloc->getParent(), pass.domInfo, boundary);
for (SILBasicBlock *deallocBlock : boundary) {
if (pass.deBlocks->isDeadEnd(deallocBlock))
continue;
auto deallocBuilder = pass.getBuilder(deallocBlock->back().getIterator());
deallocBuilder.createDeallocStack(pass.genLoc(), alloc);
}
Expand Down Expand Up @@ -3859,8 +3866,10 @@ void AddressLowering::runOnFunction(SILFunction *function) {
removeUnreachableBlocks(*function);

auto *dominance = PM->getAnalysis<DominanceAnalysis>();
auto *deadEnds = PM->getAnalysis<DeadEndBlocksAnalysis>();

AddressLoweringState pass(function, dominance->get(function));
AddressLoweringState pass(function, dominance->get(function),
deadEnds->get(function));

// ## Step #1: Map opaque values
//
Expand Down
13 changes: 13 additions & 0 deletions test/SILOptimizer/address_lowering.sil
Original file line number Diff line number Diff line change
Expand Up @@ -1740,6 +1740,19 @@ bb2:
unwind
}

// CHECK-LABEL: sil [ossa] @testUnreachable1NoDestroy : {{.*}} {
// CHECK: [[STORAGE:%[^,]+]] = alloc_stack $T
// CHECK: apply undef<T>([[STORAGE]])
// CHECK: apply undef<T>([[STORAGE]])
// CHECK: unreachable
// CHECK-LABEL: } // end sil function 'testUnreachable1NoDestroy'
sil [ossa] @testUnreachable1NoDestroy : $@convention(thin) <T> () -> () {
bb0:
%instance = apply undef<T>() : $@convention(thin) <τ> () -> @out τ
apply undef<T>(%instance) : $@convention(thin) <τ> (@in_guaranteed τ) -> ()
unreachable
}

sil [ossa] @use_T : $@convention(thin) <T> (@in T) -> ()

// CHECK-LABEL: sil [ossa] @test_checked_cast_br1 : $@convention(method) <T> (@in Any) -> () {
Expand Down