Skip to content

Commit b1c0bd3

Browse files
authored
Minor cleanup in ARCSequenceOpts (#33578)
* Remove NewInsts from ARCSequenceOpts * Remove more instances of InsertPts * Address comments from #33504 * Make bottom up loop traversal simpler. Use better apis * Update LoopRegion printer with more info
1 parent 8fbd449 commit b1c0bd3

File tree

10 files changed

+47
-107
lines changed

10 files changed

+47
-107
lines changed

include/swift/SILOptimizer/Analysis/LoopRegionAnalysis.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -726,8 +726,9 @@ class LoopRegion {
726726
return getSubregionData().RPONumOfHeaderBlock;
727727
}
728728

729-
void dump() const;
730-
void print(llvm::raw_ostream &os, bool insertSpaces = false) const;
729+
void dump(bool isVerbose = false) const;
730+
void print(llvm::raw_ostream &os, bool isShort = false,
731+
bool isVerbose = false) const;
731732
void dumpName() const;
732733
void printName(llvm::raw_ostream &os) const;
733734

lib/SILOptimizer/ARC/ARCRegionState.cpp

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,12 @@ bool ARCRegionState::processBlockBottomUp(
235235
return NestingDetected;
236236
}
237237

238-
// Find the relevant insertion points for the loop region R in its
239-
// successors. Returns true if we succeeded. Returns false if any of the
240-
// non-local successors of the region are not leaking blocks. We currently do
241-
// not handle early exits, but do handle trapping blocks.
242-
static bool getInsertionPtsForLoopRegionExits(
238+
// Returns true if any of the non-local successors of the region are leaking
239+
// blocks. We currently do not handle early exits, but do handle trapping
240+
// blocks. Returns false if otherwise
241+
static bool hasEarlyExits(
243242
const LoopRegion *R, LoopRegionFunctionInfo *LRFI,
244-
llvm::DenseMap<const LoopRegion *, ARCRegionState *> &RegionStateInfo,
245-
llvm::SmallVectorImpl<SILInstruction *> &InsertPts) {
243+
llvm::DenseMap<const LoopRegion *, ARCRegionState *> &RegionStateInfo) {
246244
assert(R->isLoop() && "Expected a loop region that is representing a loop");
247245

248246
// Go through all of our non local successors. If any of them cannot be
@@ -251,23 +249,10 @@ static bool getInsertionPtsForLoopRegionExits(
251249
if (any_of(R->getNonLocalSuccs(), [&](unsigned SuccID) -> bool {
252250
return !RegionStateInfo[LRFI->getRegion(SuccID)]->allowsLeaks();
253251
})) {
254-
return false;
255-
}
256-
257-
// We assume that all of our loops have been canonicalized so that /all/ loop
258-
// exit blocks only have exiting blocks as predecessors. This means that all
259-
// successor regions of any region /cannot/ be a region representing a loop.
260-
for (unsigned SuccID : R->getLocalSuccs()) {
261-
auto *SuccRegion = LRFI->getRegion(SuccID);
262-
assert(SuccRegion->isBlock() && "Loop canonicalization failed?!");
263-
InsertPts.push_back(&*SuccRegion->getBlock()->begin());
252+
return true;
264253
}
265254

266-
// Sort and unique the insert points so we can put them into
267-
// ImmutablePointerSets.
268-
sortUnique(InsertPts);
269-
270-
return true;
255+
return false;
271256
}
272257

273258
bool ARCRegionState::processLoopBottomUp(
@@ -276,11 +261,9 @@ bool ARCRegionState::processLoopBottomUp(
276261
ImmutablePointerSetFactory<SILInstruction> &SetFactory) {
277262
ARCRegionState *State = RegionStateInfo[R];
278263

279-
llvm::SmallVector<SILInstruction *, 2> InsertPts;
280-
// Try to lookup insertion points for this region. If when checking for
281-
// insertion points, we find that we have non-leaking early exits, clear state
264+
// If we find that we have non-leaking early exits, clear state
282265
// and bail. We do not handle these for now.
283-
if (!getInsertionPtsForLoopRegionExits(R, LRFI, RegionStateInfo, InsertPts)) {
266+
if (hasEarlyExits(R, LRFI, RegionStateInfo)) {
284267
clearBottomUpState();
285268
return false;
286269
}

lib/SILOptimizer/ARC/ARCSequenceOptUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include "swift/SIL/SILInstruction.h"
2222

2323
namespace swift {
24-
bool isARCSignificantTerminator(TermInst *TI);
24+
bool isARCSignificantTerminator(TermInst *TI);
2525
} // end namespace swift
2626

2727
#endif

lib/SILOptimizer/ARC/ARCSequenceOpts.cpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ llvm::cl::opt<bool> EnableLoopARC("enable-loop-arc", llvm::cl::init(false));
4848
// This routine takes in the ARCMatchingSet \p MatchSet and adds the increments
4949
// and decrements to the delete list.
5050
void ARCPairingContext::optimizeMatchingSet(
51-
ARCMatchingSet &MatchSet, llvm::SmallVectorImpl<SILInstruction *> &NewInsts,
51+
ARCMatchingSet &MatchSet,
5252
llvm::SmallVectorImpl<SILInstruction *> &DeadInsts) {
5353
LLVM_DEBUG(llvm::dbgs() << "**** Optimizing Matching Set ****\n");
5454
// Add the old increments to the delete list.
@@ -69,7 +69,6 @@ void ARCPairingContext::optimizeMatchingSet(
6969
}
7070

7171
bool ARCPairingContext::performMatching(
72-
llvm::SmallVectorImpl<SILInstruction *> &NewInsts,
7372
llvm::SmallVectorImpl<SILInstruction *> &DeadInsts) {
7473
bool MatchedPair = false;
7574

@@ -97,7 +96,7 @@ bool ARCPairingContext::performMatching(
9796
for (auto *I : Set.Decrements)
9897
DecToIncStateMap.erase(I);
9998

100-
optimizeMatchingSet(Set, NewInsts, DeadInsts);
99+
optimizeMatchingSet(Set, DeadInsts);
101100
}
102101
}
103102

@@ -131,7 +130,6 @@ void LoopARCPairingContext::runOnFunction(SILFunction *F) {
131130
bool LoopARCPairingContext::processRegion(const LoopRegion *Region,
132131
bool FreezePostDomReleases,
133132
bool RecomputePostDomReleases) {
134-
llvm::SmallVector<SILInstruction *, 8> NewInsts;
135133
llvm::SmallVector<SILInstruction *, 8> DeadInsts;
136134

137135
// We have already summarized all subloops of this loop. Now summarize our
@@ -145,16 +143,7 @@ bool LoopARCPairingContext::processRegion(const LoopRegion *Region,
145143
do {
146144
NestingDetected = Evaluator.runOnLoop(Region, FreezePostDomReleases,
147145
RecomputePostDomReleases);
148-
MatchedPair = Context.performMatching(NewInsts, DeadInsts);
149-
150-
if (!NewInsts.empty()) {
151-
LLVM_DEBUG(llvm::dbgs() << "Adding new interesting insts!\n");
152-
do {
153-
auto *I = NewInsts.pop_back_val();
154-
LLVM_DEBUG(llvm::dbgs() << " " << *I);
155-
Evaluator.addInterestingInst(I);
156-
} while (!NewInsts.empty());
157-
}
146+
MatchedPair = Context.performMatching(DeadInsts);
158147

159148
if (!DeadInsts.empty()) {
160149
LLVM_DEBUG(llvm::dbgs() << "Removing dead interesting insts!\n");

lib/SILOptimizer/ARC/ARCSequenceOpts.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,9 @@ struct ARCPairingContext {
3939

4040
ARCPairingContext(SILFunction &F, RCIdentityFunctionInfo *RCIA)
4141
: F(F), DecToIncStateMap(), IncToDecStateMap(), RCIA(RCIA) {}
42-
bool performMatching(llvm::SmallVectorImpl<SILInstruction *> &NewInsts,
43-
llvm::SmallVectorImpl<SILInstruction *> &DeadInsts);
42+
bool performMatching(llvm::SmallVectorImpl<SILInstruction *> &DeadInsts);
4443

4544
void optimizeMatchingSet(ARCMatchingSet &MatchSet,
46-
llvm::SmallVectorImpl<SILInstruction *> &NewInsts,
4745
llvm::SmallVectorImpl<SILInstruction *> &DeadInsts);
4846
};
4947

@@ -68,10 +66,8 @@ struct BlockARCPairingContext {
6866
bool NestingDetected = Evaluator.run(FreezePostDomReleases);
6967
Evaluator.clear();
7068

71-
llvm::SmallVector<SILInstruction *, 8> NewInsts;
7269
llvm::SmallVector<SILInstruction *, 8> DeadInsts;
73-
bool MatchedPair = Context.performMatching(NewInsts, DeadInsts);
74-
NewInsts.clear();
70+
bool MatchedPair = Context.performMatching(DeadInsts);
7571
while (!DeadInsts.empty())
7672
DeadInsts.pop_back_val()->eraseFromParent();
7773
return NestingDetected && MatchedPair;

lib/SILOptimizer/ARC/GlobalARCSequenceDataflow.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ bool ARCSequenceDataflowEvaluator::processBBTopDown(ARCBBState &BBState) {
118118
void ARCSequenceDataflowEvaluator::mergePredecessors(
119119
ARCBBStateInfoHandle &DataHandle) {
120120
bool HasAtLeastOnePred = false;
121-
llvm::SmallVector<SILBasicBlock *, 4> BBThatNeedInsertPts;
122121

123122
SILBasicBlock *BB = DataHandle.getBB();
124123
ARCBBState &BBState = DataHandle.getState();
@@ -223,10 +222,8 @@ bool ARCSequenceDataflowEvaluator::processBBBottomUp(
223222
SetFactory);
224223

225224
auto II = BB.rbegin();
226-
if (isa<TermInst>(*II)) {
227-
if (!isARCSignificantTerminator(&cast<TermInst>(*II))) {
228-
II++;
229-
}
225+
if (!isARCSignificantTerminator(&cast<TermInst>(*II))) {
226+
II++;
230227
}
231228

232229
// For each instruction I in BB visited in reverse...

lib/SILOptimizer/ARC/GlobalLoopARCSequenceDataflow.cpp

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -200,40 +200,14 @@ bool LoopARCSequenceDataflowEvaluator::processLoopBottomUp(
200200
bool NestingDetected = false;
201201

202202
// For each BB in our post order...
203-
auto Start = R->subregion_begin(), End = R->subregion_end();
204-
if (Start == End)
205-
return false;
206-
207-
--End;
208-
while (Start != End) {
209-
unsigned SubregionIndex = *End;
203+
for (unsigned SubregionIndex : R->getReverseSubregions()) {
210204
auto *Subregion = LRFI->getRegion(SubregionIndex);
211205
auto &SubregionData = getARCState(Subregion);
212206

213207
// This will always succeed since we have an entry for each BB in our post
214208
// order.
215-
LLVM_DEBUG(llvm::dbgs() << "Processing Subregion#: " << SubregionIndex
216-
<< "\n");
217-
218-
LLVM_DEBUG(llvm::dbgs() << "Merging Successors!\n");
219-
mergeSuccessors(Subregion, SubregionData);
220-
221-
// Then perform the region optimization.
222-
NestingDetected |= SubregionData.processBottomUp(
223-
AA, RCFI, EAFI, LRFI, FreezeOwnedArgEpilogueReleases, IncToDecStateMap,
224-
RegionStateInfo, SetFactory);
225-
--End;
226-
}
227-
228-
{
229-
unsigned SubregionIndex = *End;
230-
auto *Subregion = LRFI->getRegion(SubregionIndex);
231-
auto &SubregionData = getARCState(Subregion);
232-
233-
// This will always succeed since we have an entry for each BB in our post
234-
// order.
235-
LLVM_DEBUG(llvm::dbgs() << "Processing Subregion#: " << SubregionIndex
236-
<< "\n");
209+
LLVM_DEBUG(llvm::dbgs()
210+
<< "Processing Subregion#: " << SubregionIndex << "\n");
237211

238212
LLVM_DEBUG(llvm::dbgs() << "Merging Successors!\n");
239213
mergeSuccessors(Subregion, SubregionData);

lib/SILOptimizer/ARC/RefCountState.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,6 @@ handleRefCountInstMatch(SILInstruction *RefCountInst) {
285285
return false;
286286
case LatticeState::Decremented:
287287
case LatticeState::MightBeUsed:
288-
// Unset InsertPt so we remove retain release pairs instead of
289-
// performing code motion.
290-
LLVM_FALLTHROUGH;
291288
case LatticeState::MightBeDecremented:
292289
return true;
293290
}

lib/SILOptimizer/ARC/RefCountState.h

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -185,17 +185,13 @@ class BottomUpRefCountState : public RefCountState {
185185
bool initWithMutatorInst(ImmutablePointerSet<SILInstruction> *I,
186186
RCIdentityFunctionInfo *RCFI);
187187

188-
/// Update this reference count's state given the instruction \p I. \p
189-
/// InsertPt is the point furthest up the CFG where we can move the currently
190-
/// tracked reference count.
188+
/// Update this reference count's state given the instruction \p I.
191189
void
192190
updateForSameLoopInst(SILInstruction *I,
193191
ImmutablePointerSetFactory<SILInstruction> &SetFactory,
194192
AliasAnalysis *AA);
195193

196-
/// Update this reference count's state given the instruction \p I. \p
197-
/// InsertPts are the points furthest up the CFG where we can move the
198-
/// currently tracked reference count.
194+
/// Update this reference count's state given the instruction \p I.
199195
//
200196
/// The main difference in between this routine and update for same loop inst
201197
/// is that if we see any decrements on a value, we treat it as being
@@ -244,9 +240,7 @@ class BottomUpRefCountState : public RefCountState {
244240
bool valueCanBeUsedGivenLatticeState() const;
245241

246242
/// Given the current lattice state, if we have seen a use, advance the
247-
/// lattice state. Return true if we do so and false otherwise. \p InsertPt is
248-
/// the location where if \p PotentialUser is a user of this ref count, we
249-
/// would insert a release.
243+
/// lattice state. Return true if we do so and false otherwise.
250244
bool handleUser(SILValue RCIdentity,
251245
ImmutablePointerSetFactory<SILInstruction> &SetFactory,
252246
AliasAnalysis *AA);
@@ -264,9 +258,7 @@ class BottomUpRefCountState : public RefCountState {
264258
bool valueCanBeGuaranteedUsedGivenLatticeState() const;
265259

266260
/// Given the current lattice state, if we have seen a use, advance the
267-
/// lattice state. Return true if we do so and false otherwise. \p InsertPt is
268-
/// the location where if \p PotentialUser is a user of this ref count, we
269-
/// would insert a release.
261+
/// lattice state. Return true if we do so and false otherwise.
270262
bool
271263
handleGuaranteedUser(SILValue RCIdentity,
272264
ImmutablePointerSetFactory<SILInstruction> &SetFactory,
@@ -338,17 +330,13 @@ class TopDownRefCountState : public RefCountState {
338330
/// Uninitialize the current state.
339331
void clear();
340332

341-
/// Update this reference count's state given the instruction \p I. \p
342-
/// InsertPt is the point furthest up the CFG where we can move the currently
343-
/// tracked reference count.
333+
/// Update this reference count's state given the instruction \p I.
344334
void
345335
updateForSameLoopInst(SILInstruction *I,
346336
ImmutablePointerSetFactory<SILInstruction> &SetFactory,
347337
AliasAnalysis *AA);
348338

349-
/// Update this reference count's state given the instruction \p I. \p
350-
/// InsertPts are the points furthest up the CFG where we can move the
351-
/// currently tracked reference count.
339+
/// Update this reference count's state given the instruction \p I.
352340
///
353341
/// The main difference in between this routine and update for same loop inst
354342
/// is that if we see any decrements on a value, we treat it as being

lib/SILOptimizer/Analysis/LoopRegionAnalysis.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ LoopRegion::FunctionTy *LoopRegion::getFunction() const {
4545
return Ptr.get<FunctionTy *>();
4646
}
4747

48-
void LoopRegion::dump() const {
49-
print(llvm::outs());
48+
void LoopRegion::dump(bool isVerbose) const {
49+
print(llvm::outs(), false, isVerbose);
5050
llvm::outs() << "\n";
5151
}
5252

@@ -69,7 +69,8 @@ void LoopRegion::printName(llvm::raw_ostream &os) const {
6969
return;
7070
}
7171

72-
void LoopRegion::print(llvm::raw_ostream &os, bool isShort) const {
72+
void LoopRegion::print(llvm::raw_ostream &os, bool isShort,
73+
bool isVerbose) const {
7374
os << "(region id:" << ID;
7475
if (isShort) {
7576
os << ")";
@@ -88,6 +89,20 @@ void LoopRegion::print(llvm::raw_ostream &os, bool isShort) const {
8889

8990
os << " ucfh:" << (IsUnknownControlFlowEdgeHead? "true " : "false")
9091
<< " ucft:" << (IsUnknownControlFlowEdgeTail? "true " : "false");
92+
93+
if (!isVerbose) {
94+
return;
95+
}
96+
os << "\n";
97+
if (isBlock()) {
98+
getBlock()->dump();
99+
} else if (isLoop()) {
100+
getLoop()->dump();
101+
} else if (isFunction()) {
102+
getFunction()->dump();
103+
} else {
104+
llvm_unreachable("Unknown region type");
105+
}
91106
}
92107

93108
llvm::raw_ostream &llvm::operator<<(llvm::raw_ostream &os, LoopRegion &LR) {

0 commit comments

Comments
 (0)