[llvm] [SLP] Inefficient cost-modelling and codegen for reductions with slp-… (PR #197875)

Sushant Gokhale via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 4 01:35:50 PDT 2026


================
@@ -30261,25 +30276,30 @@ class HorizontalReduction {
     } else if (auto *VecTy = dyn_cast<FixedVectorType>(DestTy)) {
       unsigned DestTyNumElements = getNumElements(VecTy);
       unsigned VF = getNumElements(Vec->getType()) / DestTyNumElements;
-      Rdx = PoisonValue::get(
-          getWidenedType(Vec->getType()->getScalarType(), DestTyNumElements));
-      for (unsigned I : seq<unsigned>(DestTyNumElements)) {
-        // Do reduction for each lane.
-        // e.g., do reduce add for
-        // VL[0] = <4 x Ty> <a, b, c, d>
-        // VL[1] = <4 x Ty> <e, f, g, h>
-        // Lane[0] = <2 x Ty> <a, e>
-        // Lane[1] = <2 x Ty> <b, f>
-        // Lane[2] = <2 x Ty> <c, g>
-        // Lane[3] = <2 x Ty> <d, h>
-        // result[0] = reduce add Lane[0]
-        // result[1] = reduce add Lane[1]
-        // result[2] = reduce add Lane[2]
-        // result[3] = reduce add Lane[3]
-        SmallVector<int, 16> Mask = createStrideMask(I, DestTyNumElements, VF);
-        Value *Lane = Builder.CreateShuffleVector(Vec, Mask);
-        Rdx = Builder.CreateInsertElement(
-            Rdx, emitReduction(Lane, Builder, &TTI, DestTy), I);
+      Rdx = nullptr;
+      /*
+        e.g. Consider vector reduce add.
+
+        RdxVal[0] = [a, b, c, d]
+        RdxVal[1] = [e, f, g, h]
+        Add0 = zeroinitializer + RdxVal[0]
+        Add1 = Add0 + RdxVal[1]
+
+        After revectorization with VF=2 and Vec = [a, b, c, d, e, f, g, h],
+        the reduction can be expressed as:
+        RdxVal[0] = ExtractVector(Vec, 0, 3) = [a, b, c, d]
+        RdxVal[1] = ExtractVector(Vec, 4, 7) = [e, f, g, h]
+        Add = RdxVal[0] + RdxVal[1]
+      */
+      for (auto I : seq<unsigned>(VF)) {
+        auto Position = I * DestTyNumElements;
+        Value *SubVec =
+            createExtractVector(Builder, Vec, DestTyNumElements, Position);
+        if (!Rdx) {
+          Rdx = SubVec;
+        } else {
+          Rdx = createOp(Builder, RdxKind, Rdx, SubVec, "rdx.op", ReductionOps);
+        }
----------------
sushgokh wrote:

done

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


More information about the llvm-commits mailing list