Skip to content

Commit 793bb6b

Browse files
committed
Revert "[VPlan] Move predication to VPlanTransform (NFC). (#128420)"
This reverts commit b263c08. Looks like this triggers a crash in one of the Fortran tests. Reverting while I investigate https://lab.llvm.org/buildbot/#/builders/41/builds/6825
1 parent 04d10f1 commit 793bb6b

File tree

7 files changed

+333
-376
lines changed

7 files changed

+333
-376
lines changed

llvm/lib/Transforms/Vectorize/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ add_llvm_component_library(LLVMVectorize
2525
VPlan.cpp
2626
VPlanAnalysis.cpp
2727
VPlanConstruction.cpp
28-
VPlanPredicator.cpp
2928
VPlanRecipes.cpp
3029
VPlanSLP.cpp
3130
VPlanTransforms.cpp

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 265 additions & 36 deletions
Large diffs are not rendered by default.

llvm/lib/Transforms/Vectorize/VPRecipeBuilder.h

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,15 @@ class VPRecipeBuilder {
6868

6969
VPBuilder &Builder;
7070

71-
/// The mask of each VPBB, generated earlier and used for predicating recipes
72-
/// in VPBB.
73-
/// TODO: remove by applying predication when generating the masks.
74-
DenseMap<VPBasicBlock *, VPValue *> &BlockMaskCache;
71+
/// When we if-convert we need to create edge masks. We have to cache values
72+
/// so that we don't end up with exponential recursion/IR. Note that
73+
/// if-conversion currently takes place during VPlan-construction, so these
74+
/// caches are only used at that stage.
75+
using EdgeMaskCacheTy =
76+
DenseMap<std::pair<BasicBlock *, BasicBlock *>, VPValue *>;
77+
using BlockMaskCacheTy = DenseMap<BasicBlock *, VPValue *>;
78+
EdgeMaskCacheTy EdgeMaskCache;
79+
BlockMaskCacheTy BlockMaskCache;
7580

7681
// VPlan construction support: Hold a mapping from ingredients to
7782
// their recipe.
@@ -85,6 +90,10 @@ class VPRecipeBuilder {
8590
/// A mapping of partial reduction exit instructions to their scaling factor.
8691
DenseMap<const Instruction *, unsigned> ScaledReductionMap;
8792

93+
/// A mapping from VP blocks to IR blocks, used temporarily while migrating
94+
/// away from IR references.
95+
const DenseMap<const VPBlockBase *, BasicBlock *> &VPB2IRBB;
96+
8897
/// Loop versioning instance for getting noalias metadata guaranteed by
8998
/// runtime checks.
9099
LoopVersioning *LVer;
@@ -113,6 +122,11 @@ class VPRecipeBuilder {
113122
tryToOptimizeInductionTruncate(TruncInst *I, ArrayRef<VPValue *> Operands,
114123
VFRange &Range);
115124

125+
/// Handle non-loop phi nodes, returning a new VPBlendRecipe. Currently
126+
/// all such phi nodes are turned into a sequence of select instructions as
127+
/// the vectorizer currently performs full if-conversion.
128+
VPBlendRecipe *tryToBlend(VPWidenPHIRecipe *PhiR);
129+
116130
/// Handle call instructions. If \p CI can be widened for \p Range.Start,
117131
/// return a new VPWidenCallRecipe or VPWidenIntrinsicRecipe. Range.End may be
118132
/// decreased to ensure same decision from \p Range.Start to \p Range.End.
@@ -150,11 +164,10 @@ class VPRecipeBuilder {
150164
LoopVectorizationLegality *Legal,
151165
LoopVectorizationCostModel &CM,
152166
PredicatedScalarEvolution &PSE, VPBuilder &Builder,
153-
DenseMap<VPBasicBlock *, VPValue *> &BlockMaskCache,
167+
const DenseMap<const VPBlockBase *, BasicBlock *> &VPB2IRBB,
154168
LoopVersioning *LVer)
155169
: Plan(Plan), OrigLoop(OrigLoop), TLI(TLI), TTI(TTI), Legal(Legal),
156-
CM(CM), PSE(PSE), Builder(Builder), BlockMaskCache(BlockMaskCache),
157-
LVer(LVer) {}
170+
CM(CM), PSE(PSE), Builder(Builder), VPB2IRBB(VPB2IRBB), LVer(LVer) {}
158171

159172
std::optional<unsigned> getScalingForReduction(const Instruction *ExitInst) {
160173
auto It = ScaledReductionMap.find(ExitInst);
@@ -183,11 +196,38 @@ class VPRecipeBuilder {
183196
Ingredient2Recipe[I] = R;
184197
}
185198

186-
/// Returns the *entry* mask for block \p VPBB or null if the mask is
187-
/// all-true.
188-
VPValue *getBlockInMask(VPBasicBlock *VPBB) const {
189-
return BlockMaskCache.lookup(VPBB);
199+
/// Create the mask for the vector loop header block.
200+
void createHeaderMask();
201+
202+
/// A helper function that computes the predicate of the block BB, assuming
203+
/// that the header block of the loop is set to True or the loop mask when
204+
/// tail folding.
205+
void createBlockInMask(const VPBasicBlock *VPBB) {
206+
return createBlockInMask(VPB2IRBB.lookup(VPBB));
190207
}
208+
void createBlockInMask(BasicBlock *BB);
209+
210+
/// Returns the *entry* mask for the block \p VPBB.
211+
VPValue *getBlockInMask(const VPBasicBlock *VPBB) const {
212+
return getBlockInMask(VPB2IRBB.lookup(VPBB));
213+
}
214+
215+
/// Returns the *entry* mask for the block \p BB.
216+
VPValue *getBlockInMask(BasicBlock *BB) const;
217+
218+
/// Create an edge mask for every destination of cases and/or default.
219+
void createSwitchEdgeMasks(SwitchInst *SI);
220+
221+
/// A helper function that computes the predicate of the edge between SRC
222+
/// and DST.
223+
VPValue *createEdgeMask(BasicBlock *Src, BasicBlock *Dst);
224+
225+
/// A helper that returns the previously computed predicate of the edge
226+
/// between SRC and DST.
227+
VPValue *getEdgeMask(const VPBasicBlock *Src, const VPBasicBlock *Dst) const {
228+
return getEdgeMask(VPB2IRBB.lookup(Src), VPB2IRBB.lookup(Dst));
229+
}
230+
VPValue *getEdgeMask(BasicBlock *Src, BasicBlock *Dst) const;
191231

192232
/// Return the recipe created for given ingredient.
193233
VPRecipeBase *getRecipe(Instruction *I) {
@@ -212,15 +252,6 @@ class VPRecipeBuilder {
212252
}
213253
return Plan.getOrAddLiveIn(V);
214254
}
215-
216-
void updateBlockMaskCache(DenseMap<VPValue *, VPValue *> &Old2New) {
217-
for (auto &[_, V] : BlockMaskCache) {
218-
if (auto *New = Old2New.lookup(V)) {
219-
V->replaceAllUsesWith(New);
220-
V = New;
221-
}
222-
}
223-
}
224255
};
225256
} // end namespace llvm
226257

llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ class PlainCFGBuilder {
6565
PlainCFGBuilder(Loop *Lp, LoopInfo *LI)
6666
: TheLoop(Lp), LI(LI), Plan(std::make_unique<VPlan>(Lp)) {}
6767

68-
/// Build plain CFG for TheLoop and connect it to Plan's entry.
69-
std::unique_ptr<VPlan> buildPlainCFG();
68+
/// Build plain CFG for TheLoop and connects it to Plan's entry.
69+
std::unique_ptr<VPlan>
70+
buildPlainCFG(DenseMap<const VPBlockBase *, BasicBlock *> &VPB2IRBB);
7071
};
7172
} // anonymous namespace
7273

@@ -241,7 +242,8 @@ void PlainCFGBuilder::createVPInstructionsForVPBB(VPBasicBlock *VPBB,
241242
}
242243

243244
// Main interface to build the plain CFG.
244-
std::unique_ptr<VPlan> PlainCFGBuilder::buildPlainCFG() {
245+
std::unique_ptr<VPlan> PlainCFGBuilder::buildPlainCFG(
246+
DenseMap<const VPBlockBase *, BasicBlock *> &VPB2IRBB) {
245247
VPIRBasicBlock *Entry = cast<VPIRBasicBlock>(Plan->getEntry());
246248
BB2VPBB[Entry->getIRBasicBlock()] = Entry;
247249
for (VPIRBasicBlock *ExitVPBB : Plan->getExitBlocks())
@@ -332,14 +334,18 @@ std::unique_ptr<VPlan> PlainCFGBuilder::buildPlainCFG() {
332334
}
333335
}
334336

337+
for (const auto &[IRBB, VPB] : BB2VPBB)
338+
VPB2IRBB[VPB] = IRBB;
339+
335340
LLVM_DEBUG(Plan->setName("Plain CFG\n"); dbgs() << *Plan);
336341
return std::move(Plan);
337342
}
338343

339-
std::unique_ptr<VPlan> VPlanTransforms::buildPlainCFG(Loop *TheLoop,
340-
LoopInfo &LI) {
344+
std::unique_ptr<VPlan> VPlanTransforms::buildPlainCFG(
345+
Loop *TheLoop, LoopInfo &LI,
346+
DenseMap<const VPBlockBase *, BasicBlock *> &VPB2IRBB) {
341347
PlainCFGBuilder Builder(TheLoop, &LI);
342-
return Builder.buildPlainCFG();
348+
return Builder.buildPlainCFG(VPB2IRBB);
343349
}
344350

345351
/// Checks if \p HeaderVPB is a loop header block in the plain CFG; that is, it

0 commit comments

Comments
 (0)