Skip to content

LetPropertyLowering: remove redundant phis after ssa-update #82798

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ private func insertEndInitInstructions(
use.set(to: ssaUpdater.getValue(atEndOf: use.instruction.parentBlock), context)
}
}
// This peephole optimization is required to avoid ownership errors.
replacePhisWithIncomingValues(phis: ssaUpdater.insertedPhis, context)
}

private func constructLetInitRegion(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,14 @@ struct SSAUpdater<Context: MutatingContext> {
context.notifyInstructionsChanged()
return context._bridged.SSAUpdater_getValueInMiddleOfBlock(block.bridged).value
}

var insertedPhis: [Phi] {
var phis = [Phi]()
let numPhis = context._bridged.SSAUpdater_getNumInsertedPhis()
phis.reserveCapacity(numPhis)
for idx in 0..<numPhis {
phis.append(Phi(context._bridged.SSAUpdater_getInsertedPhi(idx).value)!)
}
return phis
}
}
2 changes: 2 additions & 0 deletions include/swift/SILOptimizer/OptimizerBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ struct BridgedPassContext {
BRIDGED_INLINE void SSAUpdater_addAvailableValue(BridgedBasicBlock block, BridgedValue value) const;
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedValue SSAUpdater_getValueAtEndOfBlock(BridgedBasicBlock block) const;
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedValue SSAUpdater_getValueInMiddleOfBlock(BridgedBasicBlock block) const;
BRIDGED_INLINE SwiftInt SSAUpdater_getNumInsertedPhis() const;
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedValue SSAUpdater_getInsertedPhi(SwiftInt idx) const;

// Options

Expand Down
8 changes: 8 additions & 0 deletions include/swift/SILOptimizer/OptimizerBridgingImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,14 @@ BridgedValue BridgedPassContext::SSAUpdater_getValueInMiddleOfBlock(BridgedBasic
invocation->getSSAUpdater()->getValueInMiddleOfBlock(block.unbridged())};
}

SwiftInt BridgedPassContext::SSAUpdater_getNumInsertedPhis() const {
return (SwiftInt)invocation->getInsertedPhisBySSAUpdater().size();
}

BridgedValue BridgedPassContext::SSAUpdater_getInsertedPhi(SwiftInt idx) const {
return {invocation->getInsertedPhisBySSAUpdater()[idx]};
}

bool BridgedPassContext::enableStackProtection() const {
swift::SILModule *mod = invocation->getPassManager()->getModule();
return mod->getOptions().EnableStackProtection;
Expand Down
6 changes: 5 additions & 1 deletion include/swift/SILOptimizer/PassManager/PassManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class SwiftPassInvocation {
SILModule::SlabList allocatedSlabs;

SILSSAUpdater *ssaUpdater = nullptr;
SmallVector<SILPhiArgument *, 4> insertedPhisBySSAUpdater;

SwiftPassInvocation *nestedSwiftPassInvocation = nullptr;

Expand Down Expand Up @@ -178,8 +179,9 @@ class SwiftPassInvocation {

void initializeSSAUpdater(SILFunction *fn, SILType type,
ValueOwnershipKind ownership) {
insertedPhisBySSAUpdater.clear();
if (!ssaUpdater)
ssaUpdater = new SILSSAUpdater;
ssaUpdater = new SILSSAUpdater(&insertedPhisBySSAUpdater);
ssaUpdater->initialize(fn, type, ownership);
}

Expand All @@ -188,6 +190,8 @@ class SwiftPassInvocation {
return ssaUpdater;
}

ArrayRef<SILPhiArgument *> getInsertedPhisBySSAUpdater() { return insertedPhisBySSAUpdater; }

SwiftPassInvocation *initializeNestedSwiftPassInvocation(SILFunction *newFunction) {
assert(!nestedSwiftPassInvocation && "Nested Swift pass invocation already initialized");
nestedSwiftPassInvocation = new SwiftPassInvocation(passManager, transform, newFunction);
Expand Down
1 change: 1 addition & 0 deletions lib/SILOptimizer/PassManager/PassManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1562,6 +1562,7 @@ irgen::IRGenModule *SwiftPassInvocation::getIRGenModule() {
}

void SwiftPassInvocation::endPass() {
insertedPhisBySSAUpdater.clear();
assert(allocatedSlabs.empty() && "StackList is leaking slabs");
assert(numBlockSetsAllocated == 0 && "Not all BasicBlockSets deallocated");
assert(numNodeSetsAllocated == 0 && "Not all NodeSets deallocated");
Expand Down
31 changes: 31 additions & 0 deletions test/SILOptimizer/let-property-lowering.sil
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,34 @@ bb1(%3a : @reborrow $C):
return %2 : $C
}


// CHECK-LABEL: sil [ossa] @test_no_phis :
// CHECK: %3 = end_init_let_ref %2
// CHECK: return %3
// CHECK: } // end sil function 'test_no_phis'
sil [ossa] @test_no_phis : $@convention(thin) (Int, @owned C) -> @owned C {
bb0(%0 : $Int, %1 : @owned $C):
%2 = mark_uninitialized [rootself] %1
%3 = begin_borrow %2
cond_br undef, bb1, bb2
bb1:
br bb7
bb2:
cond_br undef, bb3, bb8
bb3:
cond_br undef, bb4, bb5
bb4:
br bb7
bb5:
br bb6
bb6:
end_borrow %3
return %2
bb7:
br bb6
bb8:
end_borrow %3
destroy_value [dead_end] %2
unreachable
}