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

Sumukh J Bharadwaj via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 17 03:44:33 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-subharad wrote:

Fixed. The cost path now counts live legalized groups rather than treating every constant mask alike

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


More information about the llvm-commits mailing list