Skip to content

Commit 59fd678

Browse files
authored
Merge pull request #4220 from trentxintong/Refactor
A few major refacting in RRCM and DSE
2 parents 39784ae + 8b7020e commit 59fd678

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

lib/SILOptimizer/Transforms/DeadStoreElimination.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
#include "swift/SIL/SILBuilder.h"
6363
#include "swift/SILOptimizer/Analysis/AliasAnalysis.h"
6464
#include "swift/SILOptimizer/Analysis/PostOrderAnalysis.h"
65-
#include "swift/SILOptimizer/Analysis/ValueTracking.h"
6665
#include "swift/SILOptimizer/PassManager/Passes.h"
6766
#include "swift/SILOptimizer/PassManager/Transforms.h"
6867
#include "swift/SILOptimizer/Utils/CFG.h"
@@ -77,15 +76,15 @@
7776

7877
using namespace swift;
7978

79+
STATISTIC(NumDeadStores, "Number of dead stores removed");
80+
STATISTIC(NumPartialDeadStores, "Number of partial dead stores removed");
81+
8082
/// If a large store is broken down to too many smaller stores, bail out.
8183
/// Currently, we only do partial dead store if we can form a single contiguous
82-
/// non-dead store.
84+
/// live store.
8385
static llvm::cl::opt<unsigned>
8486
MaxPartialStoreCount("max-partial-store-count", llvm::cl::init(1), llvm::cl::Hidden);
8587

86-
STATISTIC(NumDeadStores, "Number of dead stores removed");
87-
STATISTIC(NumPartialDeadStores, "Number of partial dead stores removed");
88-
8988
/// ComputeMaxStoreSet - If we ignore all reads, what is the max store set that
9089
/// can reach a particular point in a basic block. This helps in generating
9190
/// the genset and killset. i.e. if there is no upward visible store that can
@@ -154,7 +153,6 @@ static bool isDeadStoreInertInstruction(SILInstruction *Inst) {
154153
//===----------------------------------------------------------------------===//
155154

156155
namespace {
157-
158156
/// If this function has too many basic blocks or too many locations, it may
159157
/// take a long time to compute the genset and killset. The number of memory
160158
/// behavior or alias query we need to do in worst case is roughly linear to
@@ -295,7 +293,6 @@ bool BlockState::isTrackingLocation(llvm::SmallBitVector &BV, unsigned i) {
295293
//===----------------------------------------------------------------------===//
296294

297295
namespace {
298-
299296
/// The dead store elimination context, keep information about stores in a basic
300297
/// block granularity.
301298
class DSEContext {
@@ -326,7 +323,7 @@ enum class ProcessKind {
326323
llvm::SpecificBumpPtrAllocator<BlockState> &BPA;
327324

328325
/// The epilogue release matcher we are using.
329-
ConsumedArgToEpilogueReleaseMatcher& ERM;
326+
ConsumedArgToEpilogueReleaseMatcher &ERM;
330327

331328
/// Map every basic block to its location state.
332329
llvm::SmallDenseMap<SILBasicBlock *, BlockState *> BBToLocState;
@@ -1099,7 +1096,7 @@ void DSEContext::runIterativeDSE() {
10991096
// Generate the genset and killset for each basic block. We can process the
11001097
// basic blocks in any order.
11011098
//
1102-
// We also Compute the max store set at the beginning of the basic block.
1099+
// We also compute the max store set at the beginning of the basic block.
11031100
//
11041101
auto *PO = PM->getAnalysis<PostOrderAnalysis>()->get(F);
11051102
for (SILBasicBlock *B : PO->getPostOrder()) {

lib/SILOptimizer/Transforms/RetainReleaseCodeMotion.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ STATISTIC(NumRetainsSunk, "Number of retains sunk");
9393
STATISTIC(NumReleasesHoisted, "Number of releases hoisted");
9494

9595
llvm::cl::opt<bool> DisableRRCodeMotion("disable-rr-cm", llvm::cl::init(false));
96+
97+
/// Disable optimization if we have to break critical edges in the function.
9698
llvm::cl::opt<bool>
9799
DisableIfWithCriticalEdge("disable-with-critical-edge", llvm::cl::init(false));
98100

@@ -127,6 +129,7 @@ struct BlockState {
127129
/// RCRootVault. If this bit is set, that means this is potentially a retain
128130
/// or release that can be sunk or hoisted to this point. This is used to
129131
/// optimize the time for computing genset and killset.
132+
///
130133
/// NOTE: this vector contains an approximation of whether there will be a
131134
/// retain or release to a certain point of a basic block.
132135
llvm::SmallBitVector BBMaxSet;
@@ -275,6 +278,8 @@ class RetainBlockState : public BlockState {
275278
RetainBlockState(bool IsEntry, unsigned size, bool MultiIteration) {
276279
// Iterative forward data flow.
277280
BBSetIn.resize(size, false);
281+
// Initilize to true if we are running optimistic data flow, i.e.
282+
// MultiIteration is true.
278283
BBSetOut.resize(size, MultiIteration);
279284
BBMaxSet.resize(size, !IsEntry && MultiIteration);
280285

@@ -316,7 +321,7 @@ class RetainCodeMotionContext : public CodeMotionContext {
316321
RetainCodeMotionContext(llvm::SpecificBumpPtrAllocator<BlockState> &BPA,
317322
SILFunction *F, PostOrderFunctionInfo *PO,
318323
AliasAnalysis *AA, RCIdentityFunctionInfo *RCFI)
319-
: CodeMotionContext(BPA, F, PO, AA, RCFI) {
324+
: CodeMotionContext(BPA, F, PO, AA, RCFI) {
320325
MultiIteration = requireIteration();
321326
}
322327

@@ -597,6 +602,8 @@ class ReleaseBlockState : public BlockState {
597602
/// constructor.
598603
ReleaseBlockState(bool IsExit, unsigned size, bool MultiIteration) {
599604
// backward data flow.
605+
// Initilize to true if we are running optimistic data flow, i.e.
606+
// MultiIteration is true.
600607
BBSetIn.resize(size, MultiIteration);
601608
BBSetOut.resize(size, false);
602609
BBMaxSet.resize(size, !IsExit && MultiIteration);
@@ -645,10 +652,10 @@ class ReleaseCodeMotionContext : public CodeMotionContext {
645652
AliasAnalysis *AA, RCIdentityFunctionInfo *RCFI,
646653
bool FreezeEpilogueReleases,
647654
ConsumedArgToEpilogueReleaseMatcher &ERM)
648-
: CodeMotionContext(BPA, F, PO, AA, RCFI),
649-
FreezeEpilogueReleases(FreezeEpilogueReleases), ERM(ERM) {
650-
MultiIteration = requireIteration();
651-
}
655+
: CodeMotionContext(BPA, F, PO, AA, RCFI),
656+
FreezeEpilogueReleases(FreezeEpilogueReleases), ERM(ERM) {
657+
MultiIteration = requireIteration();
658+
}
652659

653660
/// virtual destructor.
654661
virtual ~ReleaseCodeMotionContext() {}
@@ -717,16 +724,10 @@ void ReleaseCodeMotionContext::initializeCodeMotionDataFlow() {
717724
}
718725

719726
// Initialize all the data flow bit vector for all basic blocks.
720-
llvm::SmallPtrSet<SILBasicBlock *, 2> Exits;
721-
auto Return = F->findReturnBB();
722-
auto Throw = F->findThrowBB();
723-
if (Return != F->end())
724-
Exits.insert(&*Return);
725-
if (Throw != F->end())
726-
Exits.insert(&*Throw);
727727
for (auto &BB : *F) {
728-
BlockStates[&BB] = new (BPA.Allocate()) ReleaseBlockState(Exits.count(&BB),
729-
RCRootVault.size(), MultiIteration);
728+
BlockStates[&BB] = new (BPA.Allocate())
729+
ReleaseBlockState(BB.getTerminator()->isFunctionExiting(),
730+
RCRootVault.size(), MultiIteration);
730731
}
731732
}
732733

@@ -1050,7 +1051,8 @@ SILTransform *swift::createRetainSinking() {
10501051
return new RRCodeMotion(CodeMotionKind::Retain, false);
10511052
}
10521053

1053-
/// Hoist releases, but not epilogue release.
1054+
/// Hoist releases, but not epilogue release. ASO relies on epilogue releases
1055+
/// to prove knownsafety on enclosed releases.
10541056
SILTransform *swift::createReleaseHoisting() {
10551057
return new RRCodeMotion(CodeMotionKind::Release, true);
10561058
}

0 commit comments

Comments
 (0)