]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[PATCH v2] RISC-V: Reject non-monotonic shuffle masks in slide patterns [PR126411]
authorSouradipto Das <souradiptodas6@gmail.com>
Mon, 3 Aug 2026 03:58:01 +0000 (21:58 -0600)
committerJeff Law <jeffrey.law@oss.qualcomm.com>
Mon, 3 Aug 2026 03:58:01 +0000 (21:58 -0600)
shuffle_slide_patterns did not verify that the endpoints of a combined
slideup+slidedown sequence actually correspond to OP0's and OP1's
expected positions, allowing a non-monotonic shuffle mask to be
accepted as a valid slide pattern. This produced wrong code at -O0
for masks such as { 7, 0, 7, 0 } on a 4-element vector, as reported
in PR target/126411.

This patch checks that d->perm[0] and d->perm[vlen - 1] correspond
to the expected OP0/OP1 boundary positions (vlen - slideup_cnt and
2 * vlen - 1 - slideup_cnt respectively), and rejects the pattern
otherwise. need_slideup_p is also added to the existing second-pivot
rejection check.

PR target/126411
gcc/ChangeLog:

* config/riscv/riscv-v.cc (shuffle_slide_patterns): Check that
the sequence endpoints correspond to OP0's and OP1's expected
positions and also reject a second pivot when need_slideup_p is set.

gcc/testsuite/ChangeLog:

* gcc.target/riscv/rvv/base/bug126411.c: New test.

Suggested-by: Raphael M Zinsly <raphael.zinsly@oss.qualcomm.com>
Signed-off-by: Souradipto Das <souradiptodas6@gmail.com>
gcc/config/riscv/riscv-v.cc
gcc/testsuite/gcc.target/riscv/rvv/base/bug126411.c [new file with mode: 0644]

index d8d20925dd5c480b2b03faf2148a8b640e3d79e5..5644ac270c6c72f78b50ec2ce82a9345ed50a365 100644 (file)
@@ -3829,6 +3829,13 @@ shuffle_slide_patterns (struct expand_vec_perm_d *d)
        }
       else
        return false;
+
+      /* Check if the beginning and end of the sequence corresponds
+        to OP0 and OP1 respectively */
+      if (!(known_eq (d->perm[0], vlen - slideup_cnt)
+         && known_eq (d->perm[vlen - 1], 2 * vlen - 1 - slideup_cnt)))
+       return false;
+
     }
 
   /* Check for a monotonic sequence with one or two pivots.  */
@@ -3841,8 +3848,14 @@ shuffle_slide_patterns (struct expand_vec_perm_d *d)
       if (i > 0 && i != pivot
          && maybe_ne (d->perm[i], d->perm[i - 1] + 1))
        {
-         /* A second pivot would indicate the vector length and is in OP0.  */
-         if (known_ge (d->perm[i], vec_len) || pivot == -1 || len != 0)
+         /* A second pivot would indicate the vector length and is in OP0.
+            Also a second pivot would indicate three or more monotonic
+            sequences in the given permutation which cannot be handled by
+            slideup function. */
+         if (known_ge (d->perm[i], vec_len)
+             || pivot == -1
+             || len != 0
+             || need_slideup_p)
            return false;
          len = i;
        }
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/base/bug126411.c b/gcc/testsuite/gcc.target/riscv/rvv/base/bug126411.c
new file mode 100644 (file)
index 0000000..3d42835
--- /dev/null
@@ -0,0 +1,16 @@
+/* { dg-do run { target { riscv_v } } } */
+/* { dg-require-effective-target riscv_v_ok } */
+/* { dg-additional-options " -O0 " } */
+
+#include <stdint.h>
+
+typedef int8_t v4i8 __attribute__((vector_size(4)));
+v4i8 g2 = { 7, 0, 8, 70 }, g12;
+
+int main()
+{
+    g12 = __builtin_shufflevector(g2, g2, 7, 0, 7, 0);
+    if (g12[2] != 70)
+      __builtin_abort();
+    return 0;
+}