Skip to content

Commit d48956c

Browse files
authored
Merge pull request #82808 from eeckstein/fix-let-property-lowering-6.2
[6.2] LetPropertyLowering: remove redundant phis after ssa-update
2 parents ee5f4df + 972fde7 commit d48956c

File tree

7 files changed

+59
-1
lines changed

7 files changed

+59
-1
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/LetPropertyLowering.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ private func insertEndInitInstructions(
122122
use.set(to: ssaUpdater.getValue(atEndOf: use.instruction.parentBlock), context)
123123
}
124124
}
125+
// This peephole optimization is required to avoid ownership errors.
126+
replacePhisWithIncomingValues(phis: ssaUpdater.insertedPhis, context)
125127
}
126128

127129
private func constructLetInitRegion(

SwiftCompilerSources/Sources/Optimizer/Utilities/SSAUpdater.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,14 @@ struct SSAUpdater<Context: MutatingContext> {
5050
context.notifyInstructionsChanged()
5151
return context._bridged.SSAUpdater_getValueInMiddleOfBlock(block.bridged).value
5252
}
53+
54+
var insertedPhis: [Phi] {
55+
var phis = [Phi]()
56+
let numPhis = context._bridged.SSAUpdater_getNumInsertedPhis()
57+
phis.reserveCapacity(numPhis)
58+
for idx in 0..<numPhis {
59+
phis.append(Phi(context._bridged.SSAUpdater_getInsertedPhi(idx).value)!)
60+
}
61+
return phis
62+
}
5363
}

include/swift/SILOptimizer/OptimizerBridging.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,8 @@ struct BridgedPassContext {
367367
BRIDGED_INLINE void SSAUpdater_addAvailableValue(BridgedBasicBlock block, BridgedValue value) const;
368368
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedValue SSAUpdater_getValueAtEndOfBlock(BridgedBasicBlock block) const;
369369
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedValue SSAUpdater_getValueInMiddleOfBlock(BridgedBasicBlock block) const;
370+
BRIDGED_INLINE SwiftInt SSAUpdater_getNumInsertedPhis() const;
371+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedValue SSAUpdater_getInsertedPhi(SwiftInt idx) const;
370372

371373
// Options
372374

include/swift/SILOptimizer/OptimizerBridgingImpl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,14 @@ BridgedValue BridgedPassContext::SSAUpdater_getValueInMiddleOfBlock(BridgedBasic
548548
invocation->getSSAUpdater()->getValueInMiddleOfBlock(block.unbridged())};
549549
}
550550

551+
SwiftInt BridgedPassContext::SSAUpdater_getNumInsertedPhis() const {
552+
return (SwiftInt)invocation->getInsertedPhisBySSAUpdater().size();
553+
}
554+
555+
BridgedValue BridgedPassContext::SSAUpdater_getInsertedPhi(SwiftInt idx) const {
556+
return {invocation->getInsertedPhisBySSAUpdater()[idx]};
557+
}
558+
551559
bool BridgedPassContext::enableStackProtection() const {
552560
swift::SILModule *mod = invocation->getPassManager()->getModule();
553561
return mod->getOptions().EnableStackProtection;

include/swift/SILOptimizer/PassManager/PassManager.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class SwiftPassInvocation {
7575
SILModule::SlabList allocatedSlabs;
7676

7777
SILSSAUpdater *ssaUpdater = nullptr;
78+
SmallVector<SILPhiArgument *, 4> insertedPhisBySSAUpdater;
7879

7980
SwiftPassInvocation *nestedSwiftPassInvocation = nullptr;
8081

@@ -178,8 +179,9 @@ class SwiftPassInvocation {
178179

179180
void initializeSSAUpdater(SILFunction *fn, SILType type,
180181
ValueOwnershipKind ownership) {
182+
insertedPhisBySSAUpdater.clear();
181183
if (!ssaUpdater)
182-
ssaUpdater = new SILSSAUpdater;
184+
ssaUpdater = new SILSSAUpdater(&insertedPhisBySSAUpdater);
183185
ssaUpdater->initialize(fn, type, ownership);
184186
}
185187

@@ -188,6 +190,8 @@ class SwiftPassInvocation {
188190
return ssaUpdater;
189191
}
190192

193+
ArrayRef<SILPhiArgument *> getInsertedPhisBySSAUpdater() { return insertedPhisBySSAUpdater; }
194+
191195
SwiftPassInvocation *initializeNestedSwiftPassInvocation(SILFunction *newFunction) {
192196
assert(!nestedSwiftPassInvocation && "Nested Swift pass invocation already initialized");
193197
nestedSwiftPassInvocation = new SwiftPassInvocation(passManager, transform, newFunction);

lib/SILOptimizer/PassManager/PassManager.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,6 +1562,7 @@ irgen::IRGenModule *SwiftPassInvocation::getIRGenModule() {
15621562
}
15631563

15641564
void SwiftPassInvocation::endPass() {
1565+
insertedPhisBySSAUpdater.clear();
15651566
assert(allocatedSlabs.empty() && "StackList is leaking slabs");
15661567
assert(numBlockSetsAllocated == 0 && "Not all BasicBlockSets deallocated");
15671568
assert(numNodeSetsAllocated == 0 && "Not all NodeSets deallocated");

test/SILOptimizer/let-property-lowering.sil

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,34 @@ bb1(%3a : @reborrow $C):
311311
return %2 : $C
312312
}
313313

314+
315+
// CHECK-LABEL: sil [ossa] @test_no_phis :
316+
// CHECK: %3 = end_init_let_ref %2
317+
// CHECK: return %3
318+
// CHECK: } // end sil function 'test_no_phis'
319+
sil [ossa] @test_no_phis : $@convention(thin) (Int, @owned C) -> @owned C {
320+
bb0(%0 : $Int, %1 : @owned $C):
321+
%2 = mark_uninitialized [rootself] %1
322+
%3 = begin_borrow %2
323+
cond_br undef, bb1, bb2
324+
bb1:
325+
br bb7
326+
bb2:
327+
cond_br undef, bb3, bb8
328+
bb3:
329+
cond_br undef, bb4, bb5
330+
bb4:
331+
br bb7
332+
bb5:
333+
br bb6
334+
bb6:
335+
end_borrow %3
336+
return %2
337+
bb7:
338+
br bb6
339+
bb8:
340+
end_borrow %3
341+
destroy_value [dead_end] %2
342+
unreachable
343+
}
344+

0 commit comments

Comments
 (0)