Skip to content

[VPlan] Move predication to VPlanTransform (NFC). #128420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions llvm/lib/Transforms/Vectorize/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ add_llvm_component_library(LLVMVectorize
VPlan.cpp
VPlanAnalysis.cpp
VPlanConstruction.cpp
VPlanPredicator.cpp
VPlanRecipes.cpp
VPlanSLP.cpp
VPlanTransforms.cpp
Expand Down
300 changes: 35 additions & 265 deletions llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Large diffs are not rendered by default.

71 changes: 20 additions & 51 deletions llvm/lib/Transforms/Vectorize/VPRecipeBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,10 @@ class VPRecipeBuilder {

VPBuilder &Builder;

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
DenseMap<VPBasicBlock *, VPValue *> &BlockMaskCache;
/// The mask of each VPBB, generated earlier and used for predicating recipes in VPBB. TODO: remove by applying predication when generating the masks.
DenseMap<VPBasicBlock *, VPValue *> &BlockMaskCache;

or some other documentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added thanks


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

/// A mapping from VP blocks to IR blocks, used temporarily while migrating
/// away from IR references.
const DenseMap<const VPBlockBase *, BasicBlock *> &VPB2IRBB;

/// Loop versioning instance for getting noalias metadata guaranteed by
/// runtime checks.
LoopVersioning *LVer;
Expand Down Expand Up @@ -122,11 +113,6 @@ class VPRecipeBuilder {
tryToOptimizeInductionTruncate(TruncInst *I, ArrayRef<VPValue *> Operands,
VFRange &Range);

/// Handle non-loop phi nodes, returning a new VPBlendRecipe. Currently
/// all such phi nodes are turned into a sequence of select instructions as
/// the vectorizer currently performs full if-conversion.
VPBlendRecipe *tryToBlend(VPWidenPHIRecipe *PhiR);

/// Handle call instructions. If \p CI can be widened for \p Range.Start,
/// return a new VPWidenCallRecipe or VPWidenIntrinsicRecipe. Range.End may be
/// decreased to ensure same decision from \p Range.Start to \p Range.End.
Expand Down Expand Up @@ -164,10 +150,11 @@ class VPRecipeBuilder {
LoopVectorizationLegality *Legal,
LoopVectorizationCostModel &CM,
PredicatedScalarEvolution &PSE, VPBuilder &Builder,
const DenseMap<const VPBlockBase *, BasicBlock *> &VPB2IRBB,
DenseMap<VPBasicBlock *, VPValue *> &BlockMaskCache,
LoopVersioning *LVer)
: Plan(Plan), OrigLoop(OrigLoop), TLI(TLI), TTI(TTI), Legal(Legal),
CM(CM), PSE(PSE), Builder(Builder), VPB2IRBB(VPB2IRBB), LVer(LVer) {}
CM(CM), PSE(PSE), Builder(Builder), BlockMaskCache(BlockMaskCache),
LVer(LVer) {}

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

/// Create the mask for the vector loop header block.
void createHeaderMask();

/// A helper function that computes the predicate of the block BB, assuming
/// that the header block of the loop is set to True or the loop mask when
/// tail folding.
void createBlockInMask(const VPBasicBlock *VPBB) {
return createBlockInMask(VPB2IRBB.lookup(VPBB));
/// Returns the *entry* mask for block \p VPBB or null if the mask is
/// all-true.
VPValue *getBlockInMask(VPBasicBlock *VPBB) const {
return BlockMaskCache.lookup(VPBB);
}
void createBlockInMask(BasicBlock *BB);

/// Returns the *entry* mask for the block \p VPBB.
VPValue *getBlockInMask(const VPBasicBlock *VPBB) const {
return getBlockInMask(VPB2IRBB.lookup(VPBB));
}

/// Returns the *entry* mask for the block \p BB.
VPValue *getBlockInMask(BasicBlock *BB) const;

/// Create an edge mask for every destination of cases and/or default.
void createSwitchEdgeMasks(SwitchInst *SI);

/// A helper function that computes the predicate of the edge between SRC
/// and DST.
VPValue *createEdgeMask(BasicBlock *Src, BasicBlock *Dst);

/// A helper that returns the previously computed predicate of the edge
/// between SRC and DST.
VPValue *getEdgeMask(const VPBasicBlock *Src, const VPBasicBlock *Dst) const {
return getEdgeMask(VPB2IRBB.lookup(Src), VPB2IRBB.lookup(Dst));
}
VPValue *getEdgeMask(BasicBlock *Src, BasicBlock *Dst) const;

/// Return the recipe created for given ingredient.
VPRecipeBase *getRecipe(Instruction *I) {
Expand All @@ -252,6 +212,15 @@ class VPRecipeBuilder {
}
return Plan.getOrAddLiveIn(V);
}

void updateBlockMaskCache(DenseMap<VPValue *, VPValue *> &Old2New) {
for (auto &[_, V] : BlockMaskCache) {
if (auto *New = Old2New.lookup(V)) {
V->replaceAllUsesWith(New);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: worth removing V from Old2New now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cannot be done for now, as Old2New is used to erase old recipes after updateBlockMaskCache

V = New;
}
}
}
};
} // end namespace llvm

Expand Down
18 changes: 6 additions & 12 deletions llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ class PlainCFGBuilder {
PlainCFGBuilder(Loop *Lp, LoopInfo *LI)
: TheLoop(Lp), LI(LI), Plan(std::make_unique<VPlan>(Lp)) {}

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

Expand Down Expand Up @@ -242,8 +241,7 @@ void PlainCFGBuilder::createVPInstructionsForVPBB(VPBasicBlock *VPBB,
}

// Main interface to build the plain CFG.
std::unique_ptr<VPlan> PlainCFGBuilder::buildPlainCFG(
DenseMap<const VPBlockBase *, BasicBlock *> &VPB2IRBB) {
std::unique_ptr<VPlan> PlainCFGBuilder::buildPlainCFG() {
VPIRBasicBlock *Entry = cast<VPIRBasicBlock>(Plan->getEntry());
BB2VPBB[Entry->getIRBasicBlock()] = Entry;
for (VPIRBasicBlock *ExitVPBB : Plan->getExitBlocks())
Expand Down Expand Up @@ -334,18 +332,14 @@ std::unique_ptr<VPlan> PlainCFGBuilder::buildPlainCFG(
}
}

for (const auto &[IRBB, VPB] : BB2VPBB)
VPB2IRBB[VPB] = IRBB;

LLVM_DEBUG(Plan->setName("Plain CFG\n"); dbgs() << *Plan);
return std::move(Plan);
}

std::unique_ptr<VPlan> VPlanTransforms::buildPlainCFG(
Loop *TheLoop, LoopInfo &LI,
DenseMap<const VPBlockBase *, BasicBlock *> &VPB2IRBB) {
std::unique_ptr<VPlan> VPlanTransforms::buildPlainCFG(Loop *TheLoop,
LoopInfo &LI) {
PlainCFGBuilder Builder(TheLoop, &LI);
return Builder.buildPlainCFG(VPB2IRBB);
return Builder.buildPlainCFG();
}

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