[llvm] [AARCH64][CostModel] - Amortize sdiv costs to allow for loops with predicated sdivs to vectorize (PR #201296)
David Sherwood via llvm-commits
llvm-commits at lists.llvm.org
Wed Jun 3 01:59:27 PDT 2026
================
@@ -3515,9 +3515,36 @@ InstructionCost VPReplicateRecipe::computeCost(ElementCount VF,
}))
break;
+ // Model the extract/insert overhead for predicated div/rem as one
+ // extractelement per vector operand plus one insertelement for the result,
+ // rather than VF of each (as getScalarizationOverhead would charge).
+ //
+ // The rationale is that on targets with separate integer and vector/FP
+ // execution units (e.g. all AArch64 subtargets), the extractelement and
+ // insertelement instructions that wrap each scalar div/rem lane execute on
+ // the vector register file ports, while the div/rem itself executes on the
+ // integer divide unit. These are structurally independent resources, so
+ // the VF extract/insert instructions are fully pipelined and their
+ // throughput cost is hidden behind the divide latency. Charging VF *
+ // (extract + insert) per operand/result therefore over-estimates the real
+ // bottleneck by a factor of VF. One extract per vector operand and one
+ // insert for the result is a better amortised approximation of the actual
+ // overhead seen at the register-file boundary.
+ //
+ // VPIRValue operands are loop-invariant scalars that never reside in a
+ // vector register, so they require no extraction at all.
+ Type *ScalarTy = Ctx.Types.inferScalarType(this);
+ auto *VecTy = cast<VectorType>(toVectorTy(ScalarTy, VF));
+ InstructionCost ExtractCost = Ctx.TTI.getVectorInstrCost(
+ Instruction::ExtractElement, VecTy, Ctx.CostKind);
+ InstructionCost InsertCost = Ctx.TTI.getVectorInstrCost(
+ Instruction::InsertElement, VecTy, Ctx.CostKind);
+ unsigned NumVectorOperands =
+ count_if(operands(), [](const VPValue *Op) {
+ return !isa<VPIRValue>(Op);
+ });
ScalarCost = ScalarCost * VF.getFixedValue() +
- Ctx.getScalarizationOverhead(Ctx.Types.inferScalarType(this),
- to_vector(operands()), VF);
----------------
david-arm wrote:
I haven't reviewed the PR yet, but I suspect the better solution would be to fix `getScalarizationOverhead`. I'm guessing the problem you're hitting is actually common to all recipes that would call `getScalarizationOverhead`. I also recently noticed a problem with replicated stores that double-count the cost of some inserts/extracts. By fixing `getScalarizationOverhead` it would a) increase code reuse, and b) share the benefits with other loops that don't have udiv/urem/etc
Just out of curiosity - does this PR also fix your problem? https://github.com/llvm/llvm-project/pull/192056. I opened this a while ago, but @fhahn had some questions about the X86 test changes in terms of whether they are profitable or not on real hardware. While investigating this I found a whole load of other problems too and haven't gotten around to pushing this further. One example is precisely the double-counting of inserts/extracts for replicated stores that I mentioned above.
https://github.com/llvm/llvm-project/pull/201296
More information about the llvm-commits
mailing list