Skip to content

Commit 0604dc1

Browse files
committed
Revert "[VPlan] Set branch weight metadata on middle term in VPlan (NFC) (#143035)"
This caused assertion failures: llvm/lib/Transforms/Vectorize/VPlan.h:4021: llvm::VPBasicBlock* llvm::VPlan::getMiddleBlock(): Assertion `LoopRegion && "cannot call the function after vector loop region has been removed"' failed. See comment on the PR. > Manage branch weights for the BranchOnCond in the middle block in VPlan. > This requires updating VPInstruction to inherit from VPIRMetadata, which > in general makes sense as there are a number of opcodes that could take > metadata. > > There are other branches (part of the skeleton) that also need branch > weights adding. > > PR: #143035 This reverts commit db8d34d.
1 parent d698ede commit 0604dc1

File tree

3 files changed

+45
-62
lines changed

3 files changed

+45
-62
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7272,33 +7272,6 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
72727272
BypassBlock, MainResumePhi->getIncomingValueForBlock(BypassBlock));
72737273
}
72747274

7275-
/// Add branch weight metadata, if the \p Plan's middle block is terminated by a
7276-
/// BranchOnCond recipe.
7277-
static void addBranchWeightToMiddleTerminator(VPlan &Plan, ElementCount VF,
7278-
Loop *OrigLoop) {
7279-
// 4. Adjust branch weight of the branch in the middle block.
7280-
Instruction *LatchTerm = OrigLoop->getLoopLatch()->getTerminator();
7281-
if (!hasBranchWeightMD(*LatchTerm))
7282-
return;
7283-
7284-
VPBasicBlock *MiddleVPBB = Plan.getMiddleBlock();
7285-
auto *MiddleTerm =
7286-
dyn_cast_or_null<VPInstruction>(MiddleVPBB->getTerminator());
7287-
// Only add branch metadata if there is a (conditional) terminator.
7288-
if (!MiddleTerm)
7289-
return;
7290-
7291-
assert(MiddleTerm->getOpcode() == VPInstruction::BranchOnCond &&
7292-
"must have a BranchOnCond");
7293-
// Assume that `Count % VectorTripCount` is equally distributed.
7294-
unsigned TripCount = Plan.getUF() * VF.getKnownMinValue();
7295-
assert(TripCount > 0 && "trip count should not be zero");
7296-
MDBuilder MDB(LatchTerm->getContext());
7297-
MDNode *BranchWeights =
7298-
MDB.createBranchWeights({1, TripCount - 1}, /*IsExpected=*/false);
7299-
MiddleTerm->addMetadata(LLVMContext::MD_prof, BranchWeights);
7300-
}
7301-
73027275
DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
73037276
ElementCount BestVF, unsigned BestUF, VPlan &BestVPlan,
73047277
InnerLoopVectorizer &ILV, DominatorTree *DT, bool VectorizingEpilogue) {
@@ -7321,8 +7294,11 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
73217294

73227295
VPlanTransforms::convertToConcreteRecipes(BestVPlan,
73237296
*Legal->getWidestInductionType());
7324-
7325-
addBranchWeightToMiddleTerminator(BestVPlan, BestVF, OrigLoop);
7297+
// Retrieve and store the middle block before dissolving regions. Regions are
7298+
// dissolved after optimizing for VF and UF, which completely removes unneeded
7299+
// loop regions first.
7300+
VPBasicBlock *MiddleVPBB =
7301+
BestVPlan.getVectorLoopRegion() ? BestVPlan.getMiddleBlock() : nullptr;
73267302
VPlanTransforms::dissolveLoopRegions(BestVPlan);
73277303
// Perform the actual loop transformation.
73287304
VPTransformState State(&TTI, BestVF, LI, DT, ILV.AC, ILV.Builder, &BestVPlan,
@@ -7465,6 +7441,20 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
74657441

74667442
ILV.printDebugTracesAtEnd();
74677443

7444+
// 4. Adjust branch weight of the branch in the middle block.
7445+
if (HeaderVPBB) {
7446+
auto *MiddleTerm =
7447+
cast<BranchInst>(State.CFG.VPBB2IRBB[MiddleVPBB]->getTerminator());
7448+
if (MiddleTerm->isConditional() &&
7449+
hasBranchWeightMD(*OrigLoop->getLoopLatch()->getTerminator())) {
7450+
// Assume that `Count % VectorTripCount` is equally distributed.
7451+
unsigned TripCount = BestVPlan.getUF() * State.VF.getKnownMinValue();
7452+
assert(TripCount > 0 && "trip count should not be zero");
7453+
const uint32_t Weights[] = {1, TripCount - 1};
7454+
setBranchWeights(*MiddleTerm, Weights, /*IsExpected=*/false);
7455+
}
7456+
}
7457+
74687458
return ExpandedSCEVs;
74697459
}
74707460

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -882,39 +882,11 @@ template <unsigned PartOpIdx> class VPUnrollPartAccessor {
882882
unsigned getUnrollPart(VPUser &U) const;
883883
};
884884

885-
/// Helper to manage IR metadata for recipes. It filters out metadata that
886-
/// cannot be propagated.
887-
class VPIRMetadata {
888-
SmallVector<std::pair<unsigned, MDNode *>> Metadata;
889-
890-
public:
891-
VPIRMetadata() {}
892-
893-
/// Adds metatadata that can be preserved from the original instruction
894-
/// \p I.
895-
VPIRMetadata(Instruction &I) { getMetadataToPropagate(&I, Metadata); }
896-
897-
/// Adds metatadata that can be preserved from the original instruction
898-
/// \p I and noalias metadata guaranteed by runtime checks using \p LVer.
899-
VPIRMetadata(Instruction &I, LoopVersioning *LVer);
900-
901-
/// Copy constructor for cloning.
902-
VPIRMetadata(const VPIRMetadata &Other) : Metadata(Other.Metadata) {}
903-
904-
/// Add all metadata to \p I.
905-
void applyMetadata(Instruction &I) const;
906-
907-
void addMetadata(unsigned Kind, MDNode *Node) {
908-
Metadata.emplace_back(Kind, Node);
909-
}
910-
};
911-
912885
/// This is a concrete Recipe that models a single VPlan-level instruction.
913886
/// While as any Recipe it may generate a sequence of IR instructions when
914887
/// executed, these instructions would always form a single-def expression as
915888
/// the VPInstruction is also a single def-use vertex.
916889
class VPInstruction : public VPRecipeWithIRFlags,
917-
public VPIRMetadata,
918890
public VPUnrollPartAccessor<1> {
919891
friend class VPlanSlp;
920892

@@ -1004,7 +976,7 @@ class VPInstruction : public VPRecipeWithIRFlags,
1004976
VPInstruction(unsigned Opcode, ArrayRef<VPValue *> Operands, DebugLoc DL = {},
1005977
const Twine &Name = "")
1006978
: VPRecipeWithIRFlags(VPDef::VPInstructionSC, Operands, DL),
1007-
VPIRMetadata(), Opcode(Opcode), Name(Name.str()) {}
979+
Opcode(Opcode), Name(Name.str()) {}
1008980

1009981
VPInstruction(unsigned Opcode, ArrayRef<VPValue *> Operands,
1010982
const VPIRFlags &Flags, DebugLoc DL = {},
@@ -1296,6 +1268,29 @@ struct VPIRPhi : public VPIRInstruction, public VPPhiAccessors {
12961268
const VPRecipeBase *getAsRecipe() const override { return this; }
12971269
};
12981270

1271+
/// Helper to manage IR metadata for recipes. It filters out metadata that
1272+
/// cannot be propagated.
1273+
class VPIRMetadata {
1274+
SmallVector<std::pair<unsigned, MDNode *>> Metadata;
1275+
1276+
public:
1277+
VPIRMetadata() {}
1278+
1279+
/// Adds metatadata that can be preserved from the original instruction
1280+
/// \p I.
1281+
VPIRMetadata(Instruction &I) { getMetadataToPropagate(&I, Metadata); }
1282+
1283+
/// Adds metatadata that can be preserved from the original instruction
1284+
/// \p I and noalias metadata guaranteed by runtime checks using \p LVer.
1285+
VPIRMetadata(Instruction &I, LoopVersioning *LVer);
1286+
1287+
/// Copy constructor for cloning.
1288+
VPIRMetadata(const VPIRMetadata &Other) : Metadata(Other.Metadata) {}
1289+
1290+
/// Add all metadata to \p I.
1291+
void applyMetadata(Instruction &I) const;
1292+
};
1293+
12991294
/// VPWidenRecipe is a recipe for producing a widened instruction using the
13001295
/// opcode and operands of the recipe. This recipe covers most of the
13011296
/// traditional vectorization cases where each recipe transforms into a

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ VPInstruction::VPInstruction(unsigned Opcode, ArrayRef<VPValue *> Operands,
410410
const VPIRFlags &Flags, DebugLoc DL,
411411
const Twine &Name)
412412
: VPRecipeWithIRFlags(VPDef::VPInstructionSC, Operands, Flags, DL),
413-
VPIRMetadata(), Opcode(Opcode), Name(Name.str()) {
413+
Opcode(Opcode), Name(Name.str()) {
414414
assert(flagsValidForOpcode(getOpcode()) &&
415415
"Set flags not supported for the provided opcode");
416416
}
@@ -591,9 +591,7 @@ Value *VPInstruction::generate(VPTransformState &State) {
591591
}
592592
case VPInstruction::BranchOnCond: {
593593
Value *Cond = State.get(getOperand(0), VPLane(0));
594-
auto *Br = createCondBranch(Cond, getParent(), State);
595-
applyMetadata(*Br);
596-
return Br;
594+
return createCondBranch(Cond, getParent(), State);
597595
}
598596
case VPInstruction::BranchOnCount: {
599597
// First create the compare.

0 commit comments

Comments
 (0)