[llvm] [X86][CostModel] Cost gathers by the instructions CodeGen emits (PR #220565)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 18 10:56:29 PDT 2026
================
@@ -6633,50 +6668,160 @@ InstructionCost X86TTIImpl::getGSVectorCost(unsigned Opcode,
const Value *Ptrs = GEP->getPointerOperand();
if (Ptrs->getType()->isVectorTy() && !getSplatValue(Ptrs))
return IndexSize;
- for (unsigned I = 1, E = GEP->getNumOperands(); I != E; ++I) {
- if (isa<Constant>(GEP->getOperand(I)))
+ for (gep_type_iterator GTI = gep_type_begin(GEP), GTE = gep_type_end(GEP);
+ GTI != GTE; ++GTI) {
+ const Value *Operand = GTI.getOperand();
+
+ // An index that holds the same value in every lane contributes a fixed
+ // byte offset, which folds into the base pointer instead of reaching the
+ // index operand. That covers every struct member, named by a scalar
+ // constant, as well as a splat. A constant that is not a splat does
+ // reach the index, so it is examined like any other.
+ if (isa<Constant>(Operand) && (!Operand->getType()->isVectorTy() ||
+ getSplatValue(Operand) != nullptr))
continue;
- Type *IndxTy = GEP->getOperand(I)->getType();
- if (auto *IndexVTy = dyn_cast<VectorType>(IndxTy))
- IndxTy = IndexVTy->getElementType();
- if ((IndxTy->getPrimitiveSizeInBits() == 64 &&
- !isa<SExtInst>(GEP->getOperand(I))) ||
- ++NumOfVarIndices > 1)
+
+ if (++NumOfVarIndices > 1)
+ return IndexSize; // 64
+
+ unsigned IndexBits;
+ if (Operand->getType()->isVectorTy()) {
+ // Whether the index reaches the instruction as a dword is a property
+ // of the values it takes, not of how it was written: CodeGen narrows
+ // it when it is representable in a signed dword, which is what the
+ // gather/scatter combine in X86ISelLowering tests with
+ // ComputeNumSignBits. Deciding on the declared width and the
+ // extension opcode instead misses in both directions -- a
+ // zero-extension from a narrow type is representable although it is
+ // not an SExtInst, and an index wider than a pointer is not
+ // representable although it is not exactly 64 bits -- and a constant
+ // vector was not examined at all.
+ IndexBits = ComputeMaxSignificantBits(Operand, DL);
+ } else {
+ // A scalar index reaches here from a caller that has not widened the
+ // address yet, so the component that varies across lanes is not
+ // visible and its range says nothing about the index the instruction
+ // will see. Keep the conservative width for those until the query
+ // carries the vector form.
+ IndexBits = Operand->getType()->getPrimitiveSizeInBits() == 64 &&
+ !isa<SExtInst>(Operand)
+ ? 64
+ : 32;
+ }
+ if (IndexBits > 32)
+ return IndexSize; // 64
+
+ TypeSize EltSize = DL.getTypeAllocSize(GTI.getIndexedType());
+ if (EltSize.isScalable())
+ return IndexSize; // 64
+ uint64_t Stride = EltSize.getFixedValue();
+
+ // A stride the scale field encodes -- 1, 2, 4 or 8 -- is applied by the
+ // addressing mode, so the index reaches the combine as the extension it
+ // was written as, and narrowing follows from its range alone.
+ if (isPowerOf2_64(Stride) && Stride <= 8)
+ continue;
+
+ // Any other stride is multiplied into the index first, and the product
+ // is what the combine sees. It has to fit a signed dword with the
+ // stride included, and it is no longer an extension node, which is the
+ // route the combine normally narrows through.
+ if (IndexBits + Log2_64_Ceil(Stride) > 32)
----------------
MattPD wrote:
This bound is one bit looser than the combine's. `ComputeNumSignBits` on a `mul` adds both operands' significant widths, and a positive constant contributes its magnitude bits plus a sign bit: 12 contributes five where `Log2_64_Ceil(12)` gives four. An index with 28 significant bits under stride 12 passes here at exactly 32 and fails in the combine.
To reproduce, copy `masked-gather-scatter-part-count.ll`. In `gather_v16i32_struct_stride_narrow_index`, take a `<16 x i64> %idx` parameter and replace the `sext` with `and <16 x i64> %idx, splat (i64 134217727)` (28 significant bits). The SKX cost is 1 and `llc` emits two `vpgatherqd`. With `splat (i64 67108863)` (27 bits) both agree on one `vpgatherdd`, and `sext <16 x i28>` splits the same way.
The exact rule also depends on how DAGCombiner expands the multiply: A stride of 3 becomes a shift and an add, which narrows more readily. Could the constant be counted the way `ComputeNumSignBits` counts it, or could the comment say the bound is approximate at the boundary?
https://github.com/llvm/llvm-project/pull/220565
More information about the llvm-commits
mailing list