Skip to content

Commit c1be24e

Browse files
Merge pull request #74959 from nate-chandler/opt/20240703/1
[CopyPropagation] Replace LexicalDestroyHoisting with OwnedLifetimeCanonicalization.
2 parents cc8af17 + e0da318 commit c1be24e

21 files changed

+643
-565
lines changed

include/swift/SIL/PrunedLiveness.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,8 @@ struct PrunedLivenessBoundary {
590590
///
591591
/// bool isDef(SILInstruction *inst) const
592592
///
593+
/// bool isDef(SILArgument *arg) const
594+
///
593595
/// bool isDefBlock(SILBasicBlock *block) const
594596
///
595597
template <typename LivenessWithDefs>
@@ -611,6 +613,15 @@ class PrunedLiveRange : public PrunedLiveness {
611613
ValueSet &visited,
612614
SILValue value);
613615

616+
bool isInstructionLive(SILInstruction *instruction, bool liveOut) const;
617+
bool isAvailableOut(SILBasicBlock *block, DeadEndBlocks &deadEndBlocks) const;
618+
bool isInstructionAvailable(SILInstruction *user,
619+
DeadEndBlocks &deadEndBlocks) const;
620+
/// Whether \p user is within the boundary extended from live regions into
621+
/// dead-end regions up to the availability boundary.
622+
bool isWithinExtendedBoundary(SILInstruction *user,
623+
DeadEndBlocks *deadEndBlocks) const;
624+
614625
public:
615626
/// Add \p inst to liveness which uses the def as indicated by \p usage.
616627
void updateForUse(SILInstruction *inst, LifetimeEnding usage);
@@ -735,6 +746,8 @@ class SSAPrunedLiveness : public PrunedLiveRange<SSAPrunedLiveness> {
735746

736747
bool isDef(SILInstruction *inst) const { return inst == defInst; }
737748

749+
bool isDef(SILArgument *arg) const { return def == arg; }
750+
738751
bool isDefBlock(SILBasicBlock *block) const {
739752
return def->getParentBlock() == block;
740753
}

include/swift/SILOptimizer/Utils/CanonicalizeBorrowScope.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,6 @@ bool shrinkBorrowScope(
168168
MoveValueInst *foldDestroysOfCopiedLexicalBorrow(BeginBorrowInst *bbi,
169169
DominanceInfo &dominanceTree,
170170
InstructionDeleter &deleter);
171-
172-
bool hoistDestroysOfOwnedLexicalValue(SILValue const value,
173-
SILFunction &function,
174-
InstructionDeleter &deleter,
175-
BasicCalleeAnalysis *calleeAnalysis);
176-
177171
} // namespace swift
178172

179173
#endif // SWIFT_SILOPTIMIZER_UTILS_CANONICALIZEBORROWSCOPES_H

include/swift/SILOptimizer/Utils/CanonicalizeOSSALifetime.h

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
#include "swift/Basic/SmallPtrSetVector.h"
101101
#include "swift/SIL/PrunedLiveness.h"
102102
#include "swift/SIL/SILInstruction.h"
103+
#include "swift/SILOptimizer/Analysis/DeadEndBlocksAnalysis.h"
103104
#include "swift/SILOptimizer/Analysis/DominanceAnalysis.h"
104105
#include "swift/SILOptimizer/Analysis/NonLocalAccessBlockAnalysis.h"
105106
#include "swift/SILOptimizer/Utils/InstructionDeleter.h"
@@ -247,6 +248,8 @@ class CanonicalizeOSSALifetime final {
247248
// extendLivenessThroughOverlappingAccess is invoked.
248249
NonLocalAccessBlocks *accessBlocks = nullptr;
249250

251+
DeadEndBlocksAnalysis *deadEndBlocksAnalysis;
252+
250253
DominanceInfo *domTree = nullptr;
251254

252255
BasicCalleeAnalysis *calleeAnalysis;
@@ -326,27 +329,22 @@ class CanonicalizeOSSALifetime final {
326329
}
327330
};
328331

329-
CanonicalizeOSSALifetime(PruneDebugInsts_t pruneDebugMode,
330-
MaximizeLifetime_t maximizeLifetime,
331-
SILFunction *function,
332-
NonLocalAccessBlockAnalysis *accessBlockAnalysis,
333-
DominanceInfo *domTree,
334-
BasicCalleeAnalysis *calleeAnalysis,
335-
InstructionDeleter &deleter)
332+
CanonicalizeOSSALifetime(
333+
PruneDebugInsts_t pruneDebugMode, MaximizeLifetime_t maximizeLifetime,
334+
SILFunction *function, NonLocalAccessBlockAnalysis *accessBlockAnalysis,
335+
DeadEndBlocksAnalysis *deadEndBlocksAnalysis, DominanceInfo *domTree,
336+
BasicCalleeAnalysis *calleeAnalysis, InstructionDeleter &deleter)
336337
: pruneDebugMode(pruneDebugMode), maximizeLifetime(maximizeLifetime),
337-
accessBlockAnalysis(accessBlockAnalysis), domTree(domTree),
338+
accessBlockAnalysis(accessBlockAnalysis),
339+
deadEndBlocksAnalysis(deadEndBlocksAnalysis), domTree(domTree),
338340
calleeAnalysis(calleeAnalysis), deleter(deleter) {}
339341

340342
SILValue getCurrentDef() const { return currentDef; }
341343

342344
void initializeLiveness(SILValue def,
343345
ArrayRef<SILInstruction *> lexicalLifetimeEnds) {
344346
assert(consumingBlocks.empty() && debugValues.empty());
345-
// Clear the cached analysis pointer just in case the client invalidates the
346-
// analysis, freeing its memory.
347-
accessBlocks = nullptr;
348-
consumes.clear();
349-
destroys.clear();
347+
clear();
350348

351349
currentDef = def;
352350
currentLexicalLifetimeEnds = lexicalLifetimeEnds;
@@ -358,9 +356,18 @@ class CanonicalizeOSSALifetime final {
358356
}
359357

360358
void clear() {
359+
// Clear the access blocks analysis pointer in case the client invalidates
360+
// the analysis. If the client did, the analysis will be recomputed in
361+
// extendLivenessThroughOverlappingAccess; if it didn't, the analysis
362+
// pointer will just be set back to its old value when the analysis' cache
363+
// is consulted in extendLivenessThroughOverlappingAccess.
364+
accessBlocks = nullptr;
365+
361366
consumingBlocks.clear();
362367
debugValues.clear();
363368
discoveredBlocks.clear();
369+
consumes.clear();
370+
destroys.clear();
364371
}
365372

366373
/// Top-Level API: rewrites copies and destroys within \p def's extended
@@ -472,7 +479,7 @@ class CanonicalizeOSSALifetime final {
472479
void findExtendedBoundary(PrunedLivenessBoundary const &originalBoundary,
473480
PrunedLivenessBoundary &boundary);
474481

475-
void findDestroysOutsideBoundary(SmallVectorImpl<SILInstruction *> &destroys);
482+
void extendLivenessToDeadEnds();
476483
void extendLivenessToDeinitBarriers();
477484

478485
void extendUnconsumedLiveness(PrunedLivenessBoundary const &boundary);
@@ -481,9 +488,10 @@ class CanonicalizeOSSALifetime final {
481488
llvm::function_ref<void(SILInstruction *, PrunedLiveness::LifetimeEnding)>
482489
visitor);
483490

484-
void insertDestroysOnBoundary(PrunedLivenessBoundary const &boundary);
491+
void insertDestroysOnBoundary(PrunedLivenessBoundary const &boundary,
492+
SmallVectorImpl<DestroyValueInst *> &destroys);
485493

486-
void rewriteCopies();
494+
void rewriteCopies(SmallVectorImpl<DestroyValueInst *> const &destroys);
487495
};
488496

489497
} // end namespace swift

0 commit comments

Comments
 (0)