Skip to content

Commit b905806

Browse files
committed
Add << overload for VPDef and getVFScaleFactor utility function
1 parent 8427362 commit b905806

File tree

5 files changed

+31
-16
lines changed

5 files changed

+31
-16
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4874,6 +4874,20 @@ void LoopVectorizationCostModel::collectElementTypesForWidening() {
48744874
}
48754875
}
48764876

4877+
/// Get the VF scaling factor applied to the recipe's output, if the recipe has
4878+
/// one.
4879+
static unsigned getVFScaleFactor(VPRecipeBase *R) {
4880+
if (isa<VPPartialReductionRecipe, VPReductionPHIRecipe>(R)) {
4881+
auto *ReductionR = dyn_cast<VPReductionPHIRecipe>(R);
4882+
auto *PartialReductionR =
4883+
ReductionR ? nullptr : dyn_cast<VPPartialReductionRecipe>(R);
4884+
unsigned ScaleFactor = ReductionR ? ReductionR->getVFScaleFactor()
4885+
: PartialReductionR->getVFScaleFactor();
4886+
return ScaleFactor;
4887+
}
4888+
return 1;
4889+
}
4890+
48774891
/// Estimate the register usage for \p Plan and vectorization factors in \p VFs
48784892
/// by calculating the highest number of values that are live at a single
48794893
/// location as a rough estimate. Returns the register usage for each VF in \p
@@ -5027,22 +5041,13 @@ calculateRegisterUsage(VPlan &Plan, ArrayRef<ElementCount> VFs,
50275041
// even in the scalar case.
50285042
RegUsage[ClassID] += 1;
50295043
} else {
5030-
// The output from scaled phis and scaled reductions actually have
5044+
// The output from scaled phis and scaled reductions actually has
50315045
// fewer lanes than the VF.
5032-
ElementCount VF = VFs[J];
5033-
if (isa<VPPartialReductionRecipe, VPReductionPHIRecipe>(R)) {
5034-
auto *ReductionR = dyn_cast<VPReductionPHIRecipe>(R);
5035-
auto *PartialReductionR =
5036-
ReductionR ? nullptr : dyn_cast<VPPartialReductionRecipe>(R);
5037-
unsigned ScaleFactor = ReductionR
5038-
? ReductionR->getVFScaleFactor()
5039-
: PartialReductionR->getVFScaleFactor();
5040-
VF = VF.divideCoefficientBy(ScaleFactor);
5041-
}
5046+
unsigned ScaleFactor = getVFScaleFactor(R);
5047+
ElementCount VF = VFs[J].divideCoefficientBy(ScaleFactor);
50425048
LLVM_DEBUG(if (VF != VFs[J]) {
50435049
dbgs() << "LV(REG): Scaled down VF from " << VFs[J] << " to " << VF
5044-
<< " for ";
5045-
R->dump();
5050+
<< " for " << *R << "\n";
50465051
});
50475052

50485053
for (VPValue *DefV : R->definedValues()) {

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ raw_ostream &llvm::operator<<(raw_ostream &OS, const VPValue &V) {
7171
V.print(OS, SlotTracker);
7272
return OS;
7373
}
74+
raw_ostream &llvm::operator<<(raw_ostream &OS, const VPDef &D) {
75+
/// If this def has a single value, we can cast it to an instruction and use
76+
/// its plan for the slot tracker.
77+
if (const VPValue *Val = D.getVPSingleValue())
78+
return OS << *Val;
79+
VPSlotTracker SlotTracker(nullptr);
80+
D.print(OS, "", SlotTracker);
81+
return OS;
82+
}
7483
#endif
7584

7685
Value *VPLane::getAsRuntimeExpr(IRBuilderBase &Builder,

llvm/lib/Transforms/Vectorize/VPlanSLP.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,8 +516,8 @@ VPInstruction *VPlanSlp::buildGraph(ArrayRef<VPValue *> Values) {
516516
auto *Inst = cast<VPInstruction>(Values[0])->getUnderlyingInstr();
517517
auto *VPI = new VPInstruction(Opcode, CombinedOperands, Inst->getDebugLoc());
518518

519-
LLVM_DEBUG(dbgs() << "Create VPInstruction " << *VPI << " "
520-
<< *cast<VPInstruction>(Values[0]) << "\n");
519+
LLVM_DEBUG(dbgs() << "Create VPInstruction " << cast<VPValue>(*VPI) << " "
520+
<< Values[0] << "\n");
521521
addCombined(Values, VPI);
522522
return VPI;
523523
}

llvm/lib/Transforms/Vectorize/VPlanValue.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ typedef DenseMap<Value *, VPValue *> Value2VPValueTy;
195195
typedef DenseMap<VPValue *, Value *> VPValue2ValueTy;
196196

197197
raw_ostream &operator<<(raw_ostream &OS, const VPValue &V);
198+
raw_ostream &operator<<(raw_ostream &OS, const VPDef &D);
198199

199200
/// This class augments VPValue with operands which provide the inverse def-use
200201
/// edges from VPValue's users to their defs.

llvm/unittests/Transforms/Vectorize/VPlanTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ Successor(s): ir-bb<scalar.header>
812812
{
813813
std::string I4Dump;
814814
raw_string_ostream OS(I4Dump);
815-
OS << *I4;
815+
OS << *cast<VPDef>(I4);
816816
EXPECT_EQ("EMIT vp<%5> = mul vp<%3>, vp<%2>", I4Dump);
817817
}
818818
}

0 commit comments

Comments
 (0)