Skip to content

Commit d56bab9

Browse files
committed
[VPlan] Get Addr computation cost with scalar type if it is uniform for gather/scatter.
This patch query `getAddressComputationCost()` with scalar type if the address is uniform. This can help the cost for gather/scatter more accurate. In current LV, non consecutive VPWidenMemoryRecipe (gather/scatter) will account the cost of address computation. But there are some cases that the addr is uniform accross lanes, that makes the address can be calculated with scalar type and broadcast. I have a follow optimization that try to converts gather/scatter with uniform memory acces to scalar load/store + broadcast. With this optimization, we can remove this temporary change.
1 parent 354944d commit d56bab9

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6932,6 +6932,12 @@ static bool planContainsAdditionalSimplifications(VPlan &Plan,
69326932
auto Iter = vp_depth_first_deep(Plan.getVectorLoopRegion()->getEntry());
69336933
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(Iter)) {
69346934
for (VPRecipeBase &R : *VPBB) {
6935+
if (auto *MR = dyn_cast<VPWidenMemoryRecipe>(&R)) {
6936+
// The address computation cost can be query as scalar type if the
6937+
// address is uniform.
6938+
if (!MR->isConsecutive() && vputils::isSingleScalar(MR->getAddr()))
6939+
return true;
6940+
}
69356941
if (auto *IR = dyn_cast<VPInterleaveRecipe>(&R)) {
69366942
auto *IG = IR->getInterleaveGroup();
69376943
unsigned NumMembers = IG->getNumMembers();

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3083,9 +3083,18 @@ InstructionCost VPWidenMemoryRecipe::computeCost(ElementCount VF,
30833083
const Value *Ptr = getLoadStorePointerOperand(&Ingredient);
30843084
assert(!Reverse &&
30853085
"Inconsecutive memory access should not have the order.");
3086-
return Ctx.TTI.getAddressComputationCost(Ty) +
3087-
Ctx.TTI.getGatherScatterOpCost(Opcode, Ty, Ptr, IsMasked, Alignment,
3088-
Ctx.CostKind, &Ingredient);
3086+
InstructionCost Cost = 0;
3087+
3088+
// If the address value is uniform across all lane, then the address can be
3089+
// calculated with scalar type and broacast.
3090+
if (vputils::isSingleScalar(getAddr()))
3091+
Cost += Ctx.TTI.getAddressComputationCost(Ty->getScalarType());
3092+
else
3093+
Cost += Ctx.TTI.getAddressComputationCost(Ty);
3094+
3095+
return Cost + Ctx.TTI.getGatherScatterOpCost(Opcode, Ty, Ptr, IsMasked,
3096+
Alignment, Ctx.CostKind,
3097+
&Ingredient);
30893098
}
30903099

30913100
InstructionCost Cost = 0;

0 commit comments

Comments
 (0)