[llvm] [X86][CostModel] Add per-shape gather/scatter cost tables for AMD znver4+ (PR #199488)

via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 6 23:18:22 PDT 2026


================
@@ -6647,36 +6831,72 @@ InstructionCost X86TTIImpl::getGSVectorCost(unsigned Opcode,
     return (unsigned)32;
   };
 
-  // Trying to reduce IndexSize to 32 bits for vector 16.
-  // By default the IndexSize is equal to pointer size.
-  unsigned IndexSize = (ST->hasAVX512() && VF >= 16)
-                           ? getIndexSizeInBits(Ptr, DL)
-                           : DL.getPointerSizeInBits();
+  // By default the index is pointer-sized. The generic model has historically
+  // tried to narrow only VF 16+, where this avoids a legalization split. The
+  // Zen schedule-driven path also needs the real index width at smaller VFs to
+  // select the matching DQ/QQ (or DD/QD) instruction class.
+  unsigned IndexSize =
+      ST->hasAVX512() && (VF >= 16 || ST->hasPreferGSCostTable())
+          ? getIndexSizeInBits(Ptr, DL)
+          : DL.getPointerSizeInBits();
 
   auto *IndexVTy = FixedVectorType::get(
       IntegerType::get(SrcVTy->getContext(), IndexSize), VF);
   std::pair<InstructionCost, MVT> IdxsLT = getTypeLegalizationCost(IndexVTy);
   std::pair<InstructionCost, MVT> SrcLT = getTypeLegalizationCost(SrcVTy);
   InstructionCost::CostType SplitFactor =
       std::max(IdxsLT.first, SrcLT.first).getValue();
-  if (SplitFactor > 1) {
-    // Handle splitting of vector of pointers
-    auto *SplitSrcTy =
-        FixedVectorType::get(SrcVTy->getScalarType(), VF / SplitFactor);
-    return SplitFactor * getGSVectorCost(Opcode, CostKind, SplitSrcTy, Ptr,
-                                         Alignment, AddressSpace);
-  }
+  unsigned NumParts =
+      std::max(getNumberOfParts(IndexVTy), getNumberOfParts(SrcVTy));
+  const bool IsLoad = Opcode == Instruction::Load;
 
-  // If we didn't split, this will be a single gather/scatter instruction.
+  // The legalization multiplier is rounded to a power of two, so the Zen path
+  // counts the parts CodeGen really emits for an irregular vector such as v24.
+  // Other targets keep the multiplier they have always been given here.
   if (CostKind == TTI::TCK_CodeSize)
-    return 1;
+    return ST->hasPreferGSCostTable() ? NumParts : SplitFactor;
+
+  // AMD znver4+ (TuningPreferGSCostTable): decompose the operation into the
+  // legal power-of-two shape that CodeGen emits, then read that shape's body
+  // from the schedule model using the actual index width. For a calibrated
+  // native data shape, its profitability overhead belongs to the whole logical
+  // operation and is paid once even if wider indices split it. Irregular and
+  // wider-than-native shapes have no logical-operation row, so tile them with
+  // the modeled legal shape and pay that shape's complete cost per part.
+  if (CostKind == TTI::TCK_RecipThroughput && ST->hasPreferGSCostTable() &&
+      ST->hasAVX512() && NumParts) {
+    Type *EltTy = SrcVTy->getScalarType();
+    // Take the width from the legalized type, not the IR one: a pointer
+    // element gathers as its integer equivalent (the same VPGATHERQQ), but
+    // reports a primitive size of 0.
+    EVT SrcEVT = TLI->getValueType(DL, SrcVTy);
+    unsigned EltBits = SrcEVT.isVector() ? SrcEVT.getScalarSizeInBits() : 0;
+    if (EltBits == 32 || EltBits == 64) {
+      unsigned PartVF = PowerOf2Ceil(divideCeil(VF, NumParts));
+      auto *PartVTy = FixedVectorType::get(EltTy, PartVF);
+      std::optional<unsigned> Body =
+          getModeledGSInstrCost(IsLoad, PartVTy, IndexSize, CostKind);
+      if (Body) {
+        if (std::optional<unsigned> OpOverhead =
----------------
MattPD wrote:

Confirmed: The qword-index v12i32 and v16i32 cases now share the same emitted decomposition and costs: 37 for gather and 29 for scatter.

https://github.com/llvm/llvm-project/pull/199488


More information about the llvm-commits mailing list