[llvm] [AArch64] Keep v16i8 -> v2i64 partial_reduce fixed-length for VL > 128 (PR #204938)

Matthew Blewitt via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 20 10:50:41 PDT 2026


https://github.com/mble created https://github.com/llvm/llvm-project/pull/204938

A fixed-length llvm.vector.partial.reduce.add with a <2 x i64> accumulator and i8 inputs was lowered, on any +sve target, through a scalable partial reduction whose result is then truncated back to the low 128 bits via convertFromScalableVector. A partial reduction is not lane-wise: the scalable result distributes the accumulated sums across all VL/64 lanes, so for any runtime vector length greater than 128 bits the high lanes are silently dropped. On a 256-bit machine (e.g. Neoverse V1) exactly half the result is lost.

This is the i64 sibling of the v16i8 -> v2i32 case fixed in #177119 (issue #176954); the fixed-length support was introduced in #142032. Keep the v2i64 <- v16i8 reduction fixed-length when NEON is available, using the VL-independent NEON dot path (udot v.4s + uaddw/uaddw2) instead of the scalable round-trip.

The carve-out is scoped to the exact 128-bit types: the fixed-length dot path hardcodes a v4i32 dot node, so a wider <4 x i64> <- <32 x i8> reduction must stay on its existing path. The UADDW{B,T}/SADDW{B,T} widening-add branch is additionally restricted to scalable result types, as those instructions are scalable-only.

Streaming-SVE (NEON unavailable) still takes the scalable path and is left as a separate fix.

>From 77d3d28bfb780459990e268612e40a6c6d8fa88e Mon Sep 17 00:00:00 2001
From: Matt Blewitt <mble at planetscale.com>
Date: Sat, 20 Jun 2026 10:28:18 -0700
Subject: [PATCH] [AArch64] Keep v16i8 -> v2i64 partial_reduce fixed-length for
 VL > 128

A fixed-length llvm.vector.partial.reduce.add with a <2 x i64>
accumulator and i8 inputs was lowered, on any +sve target, through a
scalable partial reduction whose result is then truncated back to the
low 128 bits via convertFromScalableVector. A partial reduction is not
lane-wise: the scalable result distributes the accumulated sums across
all VL/64 lanes, so for any runtime vector length greater than 128 bits
the high lanes are silently dropped. On a 256-bit machine (e.g. Neoverse
V1) exactly half the result is lost.

This is the i64 sibling of the v16i8 -> v2i32 case fixed in #177119
(issue #176954); the fixed-length support was introduced in #142032.
Keep the v2i64 <- v16i8 reduction fixed-length when NEON is available,
using the VL-independent NEON dot path (udot v.4s + uaddw/uaddw2)
instead of the scalable round-trip.

The carve-out is scoped to the exact 128-bit types: the fixed-length
dot path hardcodes a v4i32 dot node, so a wider <4 x i64> <- <32 x i8>
reduction must stay on its existing path. The UADDW{B,T}/SADDW{B,T}
widening-add branch is additionally restricted to scalable result
types, as those instructions are scalable-only.

Streaming-SVE (NEON unavailable) still takes the scalable path and is
left as a separate fix.
---
 .../Target/AArch64/AArch64ISelLowering.cpp    |  16 +-
 .../sve-fixed-length-partial-reduce.ll        | 155 ++++++++----------
 2 files changed, 86 insertions(+), 85 deletions(-)

diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index 67ef911117eff..34a8a417192ce 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -33445,9 +33445,18 @@ AArch64TargetLowering::LowerPARTIAL_REDUCE_MLA(SDValue Op,
     return DAG.getNode(ISD::SUB, DL, ResultVT, BiasedDot, BiasCorrection);
   }
 
+  // Keep a fixed-length i8 -> i64 reduction fixed-length when NEON is
+  // available. The scalable path spreads the sums across all VL-dependent lanes
+  // but only extracts the low fixed-width lanes, dropping the high lanes for VL
+  // > the fixed width (e.g. 256-bit Neoverse V1). i64 sibling of the v16i8 ->
+  // v2i32 case fixed in PR #177119 / issue #176954.
+  bool KeepFixedI8ToI64 = Subtarget->isNeonAvailable() &&
+                          ResultVT == MVT::v2i64 && OpVT == MVT::v16i8;
+
   bool ConvertToScalable =
       ResultVT.isFixedLengthVector() &&
-      useSVEForFixedLengthVectorVT(ResultVT, /*OverrideNEON=*/true);
+      useSVEForFixedLengthVectorVT(ResultVT, /*OverrideNEON=*/true) &&
+      !KeepFixedI8ToI64;
 
   if (ConvertToScalable) {
     ResultVT = getContainerForFixedLengthVector(DAG, ResultVT);
@@ -33470,7 +33479,10 @@ AArch64TargetLowering::LowerPARTIAL_REDUCE_MLA(SDValue Op,
 
   SDValue Res;
   bool IsUnsigned = Op.getOpcode() == ISD::PARTIAL_REDUCE_UMLA;
-  if (Subtarget->hasSVE2() || Subtarget->isStreamingSVEAvailable()) {
+  // UADDW{B,T}/SADDW{B,T} are scalable-only; a fixed-length result (the i8 ->
+  // i64 case kept fixed above) must use the NEON widening-add path below.
+  if (ResultVT.isScalableVector() &&
+      (Subtarget->hasSVE2() || Subtarget->isStreamingSVEAvailable())) {
     unsigned LoOpcode = IsUnsigned ? AArch64ISD::UADDWB : AArch64ISD::SADDWB;
     unsigned HiOpcode = IsUnsigned ? AArch64ISD::UADDWT : AArch64ISD::SADDWT;
     SDValue Lo = DAG.getNode(LoOpcode, DL, ResultVT, Acc, DotNode);
diff --git a/llvm/test/CodeGen/AArch64/sve-fixed-length-partial-reduce.ll b/llvm/test/CodeGen/AArch64/sve-fixed-length-partial-reduce.ll
index ae7aa9b35f62a..e80e2e795c47c 100644
--- a/llvm/test/CodeGen/AArch64/sve-fixed-length-partial-reduce.ll
+++ b/llvm/test/CodeGen/AArch64/sve-fixed-length-partial-reduce.ll
@@ -444,30 +444,16 @@ define <4 x i32> @four_way_i8_i32_vl128_sudot(ptr %accptr, ptr %uptr, ptr %sptr)
 }
 
 define <2 x i64> @four_way_i8_i64_vl128_usdot(ptr %accptr, ptr %uptr, ptr %sptr) {
-; NEON-LABEL: four_way_i8_i64_vl128_usdot:
-; NEON:       // %bb.0:
-; NEON-NEXT:    movi v0.2d, #0000000000000000
-; NEON-NEXT:    ldr q1, [x1]
-; NEON-NEXT:    ldr q2, [x2]
-; NEON-NEXT:    usdot v0.4s, v1.16b, v2.16b
-; NEON-NEXT:    ldr q1, [x0]
-; NEON-NEXT:    saddw v1.2d, v1.2d, v0.2s
-; NEON-NEXT:    saddw2 v0.2d, v1.2d, v0.4s
-; NEON-NEXT:    ret
-;
-; SVE-LABEL: four_way_i8_i64_vl128_usdot:
-; SVE:       // %bb.0:
-; SVE-NEXT:    movi v0.2d, #0000000000000000
-; SVE-NEXT:    ldr q1, [x1]
-; SVE-NEXT:    ldr q2, [x2]
-; SVE-NEXT:    usdot z0.s, z1.b, z2.b
-; SVE-NEXT:    ldr q2, [x0]
-; SVE-NEXT:    sunpklo z1.d, z0.s
-; SVE-NEXT:    sunpkhi z0.d, z0.s
-; SVE-NEXT:    add z1.d, z2.d, z1.d
-; SVE-NEXT:    add z0.d, z1.d, z0.d
-; SVE-NEXT:    // kill: def $q0 killed $q0 killed $z0
-; SVE-NEXT:    ret
+; COMMON-LABEL: four_way_i8_i64_vl128_usdot:
+; COMMON:       // %bb.0:
+; COMMON-NEXT:    movi v0.2d, #0000000000000000
+; COMMON-NEXT:    ldr q1, [x1]
+; COMMON-NEXT:    ldr q2, [x2]
+; COMMON-NEXT:    usdot v0.4s, v1.16b, v2.16b
+; COMMON-NEXT:    ldr q1, [x0]
+; COMMON-NEXT:    saddw v1.2d, v1.2d, v0.2s
+; COMMON-NEXT:    saddw2 v0.2d, v1.2d, v0.4s
+; COMMON-NEXT:    ret
 ;
 ; SME-LABEL: four_way_i8_i64_vl128_usdot:
 ; SME:       // %bb.0:
@@ -814,30 +800,16 @@ define <4 x i64> @four_way_i16_i64_vl256(ptr %accptr, ptr %uptr, ptr %sptr) vsca
 
 define <2 x i64> @eight_way_i8_i64_vl128(ptr %accptr, ptr %uptr, ptr %sptr) {
 ;
-; NEON-LABEL: eight_way_i8_i64_vl128:
-; NEON:       // %bb.0:
-; NEON-NEXT:    movi v0.2d, #0000000000000000
-; NEON-NEXT:    ldr q1, [x1]
-; NEON-NEXT:    ldr q2, [x2]
-; NEON-NEXT:    udot v0.4s, v2.16b, v1.16b
-; NEON-NEXT:    ldr q1, [x0]
-; NEON-NEXT:    uaddw v1.2d, v1.2d, v0.2s
-; NEON-NEXT:    uaddw2 v0.2d, v1.2d, v0.4s
-; NEON-NEXT:    ret
-;
-; SVE-LABEL: eight_way_i8_i64_vl128:
-; SVE:       // %bb.0:
-; SVE-NEXT:    movi v0.2d, #0000000000000000
-; SVE-NEXT:    ldr q1, [x1]
-; SVE-NEXT:    ldr q2, [x2]
-; SVE-NEXT:    udot z0.s, z2.b, z1.b
-; SVE-NEXT:    ldr q2, [x0]
-; SVE-NEXT:    uunpklo z1.d, z0.s
-; SVE-NEXT:    uunpkhi z0.d, z0.s
-; SVE-NEXT:    add z1.d, z2.d, z1.d
-; SVE-NEXT:    add z0.d, z1.d, z0.d
-; SVE-NEXT:    // kill: def $q0 killed $q0 killed $z0
-; SVE-NEXT:    ret
+; COMMON-LABEL: eight_way_i8_i64_vl128:
+; COMMON:       // %bb.0:
+; COMMON-NEXT:    movi v0.2d, #0000000000000000
+; COMMON-NEXT:    ldr q1, [x1]
+; COMMON-NEXT:    ldr q2, [x2]
+; COMMON-NEXT:    udot v0.4s, v2.16b, v1.16b
+; COMMON-NEXT:    ldr q1, [x0]
+; COMMON-NEXT:    uaddw v1.2d, v1.2d, v0.2s
+; COMMON-NEXT:    uaddw2 v0.2d, v1.2d, v0.4s
+; COMMON-NEXT:    ret
 ;
 ; SME-LABEL: eight_way_i8_i64_vl128:
 ; SME:       // %bb.0:
@@ -859,43 +831,60 @@ define <2 x i64> @eight_way_i8_i64_vl128(ptr %accptr, ptr %uptr, ptr %sptr) {
   ret <2 x i64> %partial.reduce
 }
 
-define <4 x i64> @four_way_i8_i64_vl128_double_width(ptr %accptr, ptr %uptr, ptr %sptr) {
+; Regression test for the off-diagonal case: a 128-bit (<2 x i64>) result at
+; VL=256, where the fixed result width is smaller than the SVE vector length.
+; Before the fix the SVE run lowered via a scalable partial reduction whose
+; sums were spread across 4 d-lanes and then truncated to the low 2, dropping
+; half the count at runtime. The fix keeps it on the VL-independent NEON path.
+define <2 x i64> @eight_way_i8_i64_vl256(ptr %accptr, ptr %uptr, ptr %sptr) vscale_range(2,2) {
 ;
-; NEON-LABEL: four_way_i8_i64_vl128_double_width:
-; NEON:       // %bb.0:
-; NEON-NEXT:    movi v1.2d, #0000000000000000
-; NEON-NEXT:    movi v0.2d, #0000000000000000
-; NEON-NEXT:    ldp q3, q2, [x1]
-; NEON-NEXT:    ldp q5, q4, [x2]
-; NEON-NEXT:    udot v0.4s, v5.16b, v3.16b
-; NEON-NEXT:    udot v1.4s, v4.16b, v2.16b
-; NEON-NEXT:    ldp q3, q2, [x0]
-; NEON-NEXT:    uaddw v3.2d, v3.2d, v0.2s
-; NEON-NEXT:    uaddw v2.2d, v2.2d, v1.2s
-; NEON-NEXT:    uaddw2 v0.2d, v3.2d, v0.4s
-; NEON-NEXT:    uaddw2 v1.2d, v2.2d, v1.4s
-; NEON-NEXT:    ret
+; COMMON-LABEL: eight_way_i8_i64_vl256:
+; COMMON:       // %bb.0:
+; COMMON-NEXT:    movi v0.2d, #0000000000000000
+; COMMON-NEXT:    ldr q1, [x1]
+; COMMON-NEXT:    ldr q2, [x2]
+; COMMON-NEXT:    udot v0.4s, v2.16b, v1.16b
+; COMMON-NEXT:    ldr q1, [x0]
+; COMMON-NEXT:    uaddw v1.2d, v1.2d, v0.2s
+; COMMON-NEXT:    uaddw2 v0.2d, v1.2d, v0.4s
+; COMMON-NEXT:    ret
 ;
-; SVE-LABEL: four_way_i8_i64_vl128_double_width:
-; SVE:       // %bb.0:
-; SVE-NEXT:    movi v0.2d, #0000000000000000
-; SVE-NEXT:    movi v1.2d, #0000000000000000
-; SVE-NEXT:    ldp q3, q2, [x1]
-; SVE-NEXT:    ldp q5, q4, [x2]
-; SVE-NEXT:    udot z1.s, z5.b, z3.b
-; SVE-NEXT:    udot z0.s, z4.b, z2.b
-; SVE-NEXT:    ldp q5, q4, [x0]
-; SVE-NEXT:    uunpklo z2.d, z1.s
-; SVE-NEXT:    uunpklo z3.d, z0.s
-; SVE-NEXT:    uunpkhi z1.d, z1.s
-; SVE-NEXT:    uunpkhi z6.d, z0.s
-; SVE-NEXT:    add z0.d, z5.d, z2.d
-; SVE-NEXT:    add z2.d, z4.d, z3.d
-; SVE-NEXT:    add z0.d, z0.d, z1.d
-; SVE-NEXT:    add z1.d, z2.d, z6.d
-; SVE-NEXT:    // kill: def $q0 killed $q0 killed $z0
-; SVE-NEXT:    // kill: def $q1 killed $q1 killed $z1
-; SVE-NEXT:    ret
+; SME-LABEL: eight_way_i8_i64_vl256:
+; SME:       // %bb.0:
+; SME-NEXT:    mov z0.s, #0 // =0x0
+; SME-NEXT:    ldr q1, [x1]
+; SME-NEXT:    ldr q2, [x2]
+; SME-NEXT:    udot z0.s, z2.b, z1.b
+; SME-NEXT:    ldr q1, [x0]
+; SME-NEXT:    uaddwb z1.d, z1.d, z0.s
+; SME-NEXT:    uaddwt z0.d, z1.d, z0.s
+; SME-NEXT:    ret
+  %acc = load <2 x i64>, ptr %accptr
+  %u = load <16 x i8>, ptr %uptr
+  %s = load <16 x i8>, ptr %sptr
+  %u.wide = zext <16 x i8> %u to <16 x i64>
+  %s.wide = zext <16 x i8> %s to <16 x i64>
+  %mult = mul nuw nsw <16 x i64> %s.wide, %u.wide
+  %partial.reduce = tail call <2 x i64> @llvm.vector.partial.reduce.add(<2 x i64> %acc, <16 x i64> %mult)
+  ret <2 x i64> %partial.reduce
+}
+
+define <4 x i64> @four_way_i8_i64_vl128_double_width(ptr %accptr, ptr %uptr, ptr %sptr) {
+;
+; COMMON-LABEL: four_way_i8_i64_vl128_double_width:
+; COMMON:       // %bb.0:
+; COMMON-NEXT:    movi v1.2d, #0000000000000000
+; COMMON-NEXT:    movi v0.2d, #0000000000000000
+; COMMON-NEXT:    ldp q3, q2, [x1]
+; COMMON-NEXT:    ldp q5, q4, [x2]
+; COMMON-NEXT:    udot v0.4s, v5.16b, v3.16b
+; COMMON-NEXT:    udot v1.4s, v4.16b, v2.16b
+; COMMON-NEXT:    ldp q3, q2, [x0]
+; COMMON-NEXT:    uaddw v3.2d, v3.2d, v0.2s
+; COMMON-NEXT:    uaddw v2.2d, v2.2d, v1.2s
+; COMMON-NEXT:    uaddw2 v0.2d, v3.2d, v0.4s
+; COMMON-NEXT:    uaddw2 v1.2d, v2.2d, v1.4s
+; COMMON-NEXT:    ret
 ;
 ; SME-LABEL: four_way_i8_i64_vl128_double_width:
 ; SME:       // %bb.0:



More information about the llvm-commits mailing list