Skip to content

Commit 9d93b50

Browse files
kasuga-fjsvkeerthy
authored andcommitted
[MachinePipeliner] Limit the number of stores in BB (#154940)
The dependency analysis in MachinePipeliner checks dependencies for every pair of store instructions in the target basic block. This means the time complexity of the analysis is `O(N^2)`, where `N` is the number of store instructions. Therefore, compilation time can become significantly long when there are too many store instructions. To mitigate it, this patch introduces logic to count the number of store instructions at the beginning of the pipeliner and bail out if it exceeds the threshold. The default value if the threshold should be large enough. Thus, in most practical cases where the pipeliner is beneficial, this patch should not cause any performance regression. Related issue: #150262
1 parent 7236b95 commit 9d93b50

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

llvm/lib/CodeGen/MachinePipeliner.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ STATISTIC(NumFailZeroMII, "Pipeliner abort due to zero MII");
110110
STATISTIC(NumFailNoSchedule, "Pipeliner abort due to no schedule found");
111111
STATISTIC(NumFailZeroStage, "Pipeliner abort due to zero stage");
112112
STATISTIC(NumFailLargeMaxStage, "Pipeliner abort due to too many stages");
113+
STATISTIC(NumFailTooManyStores, "Pipeliner abort due to too many stores");
113114

114115
/// A command line option to turn software pipelining on or off.
115116
static cl::opt<bool> EnableSWP("enable-pipeliner", cl::Hidden, cl::init(true),
@@ -193,6 +194,13 @@ static cl::opt<bool>
193194
MVECodeGen("pipeliner-mve-cg", cl::Hidden, cl::init(false),
194195
cl::desc("Use the MVE code generator for software pipelining"));
195196

197+
/// A command line argument to limit the number of store instructions in the
198+
/// target basic block.
199+
static cl::opt<unsigned> SwpMaxNumStores(
200+
"pipeliner-max-num-stores",
201+
cl::desc("Maximum number of stores allwed in the target loop."), cl::Hidden,
202+
cl::init(200));
203+
196204
namespace llvm {
197205

198206
// A command line option to enable the CopyToPhi DAG mutation.
@@ -544,6 +552,23 @@ bool MachinePipeliner::canPipelineLoop(MachineLoop &L) {
544552
return false;
545553
}
546554

555+
unsigned NumStores = 0;
556+
for (MachineInstr &MI : *L.getHeader())
557+
if (MI.mayStore())
558+
++NumStores;
559+
if (NumStores > SwpMaxNumStores) {
560+
LLVM_DEBUG(dbgs() << "Too many stores\n");
561+
NumFailTooManyStores++;
562+
ORE->emit([&]() {
563+
return MachineOptimizationRemarkAnalysis(DEBUG_TYPE, "canPipelineLoop",
564+
L.getStartLoc(), L.getHeader())
565+
<< "Too many store instructions in the loop: "
566+
<< ore::NV("NumStores", NumStores) << " > "
567+
<< ore::NV("SwpMaxNumStores", SwpMaxNumStores) << ".";
568+
});
569+
return false;
570+
}
571+
547572
// Remove any subregisters from inputs to phi nodes.
548573
preprocessPhiNodes(*L.getHeader());
549574
return true;

0 commit comments

Comments
 (0)