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

via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 2 19:49:06 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 =
+                getZenGSOverhead(IsLoad, SrcVTy))
+          return *OpOverhead + NumParts * *Body;
+        if (std::optional<unsigned> PartOverhead =
+                getZenGSOverhead(IsLoad, PartVTy))
+          return NumParts * (*PartOverhead + *Body);
+      }
+    }
+  }
+
+  // Otherwise use the flat break-even overhead plus body. Preserve the
+  // historical per-part overhead for targets outside the Zen cost-table path;
+  // the one-per-logical-operation policy is intentionally local to that path.
+  const int GSOverhead = IsLoad ? getGatherOverhead() : getScatterOverhead();
 
-  // The gather / scatter cost is given by Intel architects. It is a rough
-  // number since we are looking at one instruction in a time.
-  const int GSOverhead = (Opcode == Instruction::Load) ? getGatherOverhead()
-                                                       : getScatterOverhead();
-  return GSOverhead + VF * getMemoryOpCost(Opcode, SrcVTy->getScalarType(),
-                                           Alignment, AddressSpace, CostKind);
+  InstructionCost::CostType OverheadMultiplier =
+      SplitFactor > 1 && !ST->hasPreferGSCostTable() ? SplitFactor : 1;
----------------
MattPD wrote:

The table lookup returns only reciprocal-throughput costs. However, the `hasPreferGSCostTable()` guard suppresses per-part overhead for every cost kind whenever the feature is enabled. On znver4, v16i32 gather latency is 68 with the feature disabled and 66 with it enabled. The size-and-latency cost changes from 20 to 18. Neither cost kind reads the table. On Skylake, enabling the feature also changes the reciprocal-throughput cost for a v8i32 gather from 12 to 10, even though AVX-512 is absent. Could this suppression depend on a successful Zen table lookup instead?

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


More information about the llvm-commits mailing list