[llvm] [AArch64][LV] Cost low-VF interleaved access (PR #205844)

Benjamin Maxwell via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 9 08:11:12 PDT 2026


================
@@ -5389,18 +5389,47 @@ InstructionCost AArch64TTIImpl::getInterleavedMemoryOpCost(
     return InstructionCost::getInvalid();
 
   if (!UseMaskForGaps && Factor <= TLI->getMaxSupportedInterleaveFactor()) {
-    unsigned MinElts = VecVTy->getElementCount().getKnownMinValue();
-    auto *SubVecTy =
-        VectorType::get(VecVTy->getElementType(),
-                        VecVTy->getElementCount().divideCoefficientBy(Factor));
+    ElementCount EC = VecVTy->getElementCount();
+    auto *SubVecTy = VectorType::get(VecVTy->getElementType(),
+                                     EC.divideCoefficientBy(Factor));
 
     // ldN/stN only support legal vector types of size 64 or 128 in bits.
     // Accesses having vector types that are a multiple of 128 bits can be
     // matched to more than one ldN/stN instruction.
     bool UseScalable;
-    if (MinElts % Factor == 0 &&
+    if (EC.isKnownMultipleOf(Factor) &&
         TLI->isLegalInterleavedAccessType(SubVecTy, DL, UseScalable))
       return Factor * TLI->getNumInterleavedAccesses(SubVecTy, DL, UseScalable);
+
+    // Cost the alternative approach for scalable vectors where the interleave
+    // factor is larger than the VF: use a contiguous load/store of the full
+    // wide vector followed by deinterleave/interleave shuffles.
+    if (VecTy->isScalableTy() && EC.isKnownMultipleOf(Factor)) {
+      if (SubVecTy->getElementCount() == ElementCount::getScalable(1))
+        return InstructionCost::getInvalid();
+
+      // Cost of the contiguous memory operation on the wide vector.
+      InstructionCost MemCost;
+      if (UseMaskForCond) {
+        unsigned IID = Opcode == Instruction::Load ? Intrinsic::masked_load
+                                                   : Intrinsic::masked_store;
+        MemCost = getMemIntrinsicInstrCost(
+            MemIntrinsicCostAttributes(IID, VecTy, Alignment, AddressSpace),
+            CostKind);
+      } else {
+        MemCost =
+            getMemoryOpCost(Opcode, VecTy, Alignment, AddressSpace, CostKind);
+      }
+
+      // llvm.vector.deinterleaveN is lowered as a binary tree of deinterleave2
+      // operations. A binary tree producing Factor leaf vectors has
+      // (Factor -1) inner deinterleave2 nodes. Each deinterleave2 on a pair of
+      // SVE registers emits one uzp1 + one uzp2.
+      // Total shuffle cost: (Factor - 1) deinterleave2 operations, each
+      // processing LT.first legal vector parts,with one uzp shuffle per part.
+      auto LT = getTypeLegalizationCost(VecTy);
+      return MemCost + (Factor - 1) * LT.first;
----------------
MacDue wrote:

Looking at the reproducer (https://godbolt.org/z/cWYn5Yjh4:), I think this may be under costing things. 

We have:

```
ldr     z1, [x15, #1, mul vl]
uunpkhi z2.s, z0.h ; Legalize subvectors 
uunpklo z0.s, z0.h ; Legalize subvectors 
uunpkhi z4.s, z1.h ; Legalize subvectors  
uunpklo z1.s, z1.h ; Legalize subvectors (x `Factor` times)
uzp2    z3.s, z0.s, z2.s ; root deinterleave  ; root level
uzp1    z0.s, z0.s, z2.s ; root deinterleave  ; root level
uzp2    z5.s, z1.s, z4.s ; root deinterleave  ; root level
uzp1    z1.s, z1.s, z4.s ; root deinterleave  ; root level
uzp2    z6.s, z3.s, z5.s ; leaf A deinterleave ; leaf level
uzp2    z2.s, z0.s, z1.s ; leaf A deinterleave ; leaf level
uzp1    z3.s, z3.s, z5.s ; leaf B deinterleave ; leaf level
uzp1    z0.s, z0.s, z1.s ; leaf B deinterleave ; leaf level
```
        
There's `(Factor - 1)` internal nodes of the deinterleave tree, but each level of the tree requires `Factor` uzp operations. 
There's also `Factor` instructions to legalize the type of each subvector.

So, I'm thinking maybe the cost should be (which is higher):
 ```
  Factor * getTypeLegalizationCost(SubVecTy) // Legalization cost for each subvector
  + Factor * Log2_64(Factor) // Deinterleave cost (Log2 levels, each with factor operations)
```


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


More information about the llvm-commits mailing list