https://gcc.gnu.org/g:e2f1aec551be259b6ac562dae4f04eff77a06f23

commit r17-5994-ge2f1aec551be259b6ac562dae4f04eff77a06f23
Author: Richard Biener <[email protected]>
Date:   Wed Nov 4 10:43:08 2026 +0100

    tree-optimization/128433 - disable partial vectors for loads not covering 
whole vectors
    
    For permuted SLP loads the group accessed in one vector iteration
    might not fill a whole number of vectors, for example for a load
    of s[i] with load permutation { 0, 0 } and V16SF vectors at VF 8
    which loads 16 elements but only uses 8.  
check_load_store_for_partial_vectors
    rounded the number of vectors up and recorded a loop mask for
    one V16SF vector.  But the rgroup controls are computed for
    NVECTORS * NUNITS / VF scalars per iteration, so the load ended up
    using the mask for two scalars per iteration, enabling twice the
    number of elements the scalar loop accesses and faulting when that
    crosses into an unmapped page.  The same applies to loop lengths.
    
    Since neither masks nor lengths can represent such an access,
    do not use partial vectors for it.
    
            PR tree-optimization/128433
            * tree-vect-stmts.cc (check_load_store_for_partial_vectors):
            Disable partial vectors when the group accessed per vector
            iteration does not cover a whole number of vectors.
    
            * gcc.target/i386/vect-partial-perm-load-1.c: New testcase.
            * gcc.dg/vect/pr128433.c: Likewise.

Diff:
---
 gcc/testsuite/gcc.dg/vect/pr128433.c               | 40 +++++++++++++++++
 .../gcc.target/i386/vect-partial-perm-load-1.c     | 52 ++++++++++++++++++++++
 gcc/tree-vect-stmts.cc                             | 30 +++++++------
 3 files changed, 109 insertions(+), 13 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/vect/pr128433.c 
b/gcc/testsuite/gcc.dg/vect/pr128433.c
new file mode 100644
index 000000000000..42726a890b6d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/pr128433.c
@@ -0,0 +1,40 @@
+/* { dg-require-effective-target mmap } */
+
+#include "tree-vect.h"
+
+#include <stdlib.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+__attribute__ ((noipa))
+static void
+copy_float (const float *s, float *d, long n)
+{
+  for (long i = 0; i < n; ++i)
+    {
+      d[2 * i] = s[i];
+      d[2 * i + 1] = s[i];
+    }
+}
+
+int
+main ()
+{
+  check_vect ();
+
+  long page = sysconf (_SC_PAGESIZE);
+  char *memory = mmap (0, 2 * page, PROT_READ | PROT_WRITE,
+                       MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+  if (memory == MAP_FAILED
+      || mprotect (memory + page, page, PROT_NONE))
+    return 0;
+
+  volatile long count = 1;
+  float *s = (float *) (memory + page - count * sizeof (*s));
+  float d[2] = { 0, 0 };
+  s[0] = 1;
+  copy_float (s, d, count);
+  if (d[0] != 1 || d[1] != 1)
+    abort ();
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.target/i386/vect-partial-perm-load-1.c 
b/gcc/testsuite/gcc.target/i386/vect-partial-perm-load-1.c
new file mode 100644
index 000000000000..df35fa475e39
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/vect-partial-perm-load-1.c
@@ -0,0 +1,52 @@
+/* PR128433 */
+/* { dg-do run { target { avx512f && mmap } } } */
+/* { dg-options "-O3 -mavx512f -mprefer-vector-width=512 --param 
vect-partial-vector-usage=2" } */
+
+#include "avx512f-check.h"
+
+#include <stdlib.h>
+#include <sys/mman.h>
+#include <unistd.h>
+#ifndef MAP_ANONYMOUS
+#define MAP_ANONYMOUS MAP_ANON
+#endif
+#ifndef MAP_ANON
+#define MAP_ANON 0
+#endif
+#ifndef MAP_FAILED
+#define MAP_FAILED ((void *)-1)
+#endif
+
+/* The permuted SLP load of s[i] { 0, 0 } accesses only half of a
+   vector per vector iteration, we may not use the store loop mask
+   (two scalars per iteration) for it.  */
+
+__attribute__ ((noipa))
+static void
+copy_float (const float *s, float *d, long n)
+{
+  for (long i = 0; i < n; ++i)
+    {
+      d[2 * i] = s[i];
+      d[2 * i + 1] = s[i];
+    }
+}
+
+static void
+avx512f_test (void)
+{
+  long page = sysconf (_SC_PAGESIZE);
+  char *memory = mmap (0, 2 * page, PROT_READ | PROT_WRITE,
+                      MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+  if (memory == MAP_FAILED
+      || mprotect (memory + page, page, PROT_NONE))
+    return;
+
+  volatile long count = 1;
+  float *s = (float *) (memory + page - count * sizeof (*s));
+  float d[2] = { 0, 0 };
+  s[0] = 1;
+  copy_float (s, d, count);
+  if (d[0] != 1 || d[1] != 1)
+    abort ();
+}
diff --git a/gcc/tree-vect-stmts.cc b/gcc/tree-vect-stmts.cc
index 7a4596c337cd..24c2969c2107 100644
--- a/gcc/tree-vect-stmts.cc
+++ b/gcc/tree-vect-stmts.cc
@@ -1540,26 +1540,31 @@ check_load_store_for_partial_vectors (loop_vec_info 
loop_vinfo, tree vectype,
       return;
     }
 
-  /* We might load more scalars than we need for permuting SLP loads.
-     We checked in get_load_store_type that the extra elements
-     don't leak into a new vector.  */
-  auto group_memory_nvectors = [](poly_uint64 size, poly_uint64 nunits)
-  {
-    unsigned int nvectors;
-    if (can_div_away_from_zero_p (size, nunits, &nvectors))
-      return nvectors;
-    gcc_unreachable ();
-  };
-
   poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
   poly_uint64 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
+
+  /* For permuted SLP loads the group accessed in one vector iteration
+     might not fill a whole number of vectors, for example when the
+     load permutation duplicates elements.  The loop masks and lengths
+     are computed based on NVECTORS * NUNITS / VF scalars per iteration
+     which would then enable more elements than the scalar loop accesses.  */
+  if (!constant_multiple_p (group_size * vf, nunits, &nvectors))
+    {
+      if (dump_enabled_p ())
+       dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
+                        "can't operate on partial vectors because the"
+                        " access does not cover a whole number of"
+                        " vectors.\n");
+      LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
+      return;
+    }
+
   machine_mode mask_mode;
   machine_mode vmode;
   bool using_partial_vectors_p = false;
   if (get_len_load_store_mode
       (vecmode, is_load, nullptr, elsvals).exists (&vmode))
     {
-      nvectors = group_memory_nvectors (group_size * vf, nunits);
       unsigned factor = (vecmode == vmode) ? 1 : GET_MODE_UNIT_SIZE (vecmode);
       vect_record_loop_len (loop_vinfo, lens, nvectors, vectype, factor);
       using_partial_vectors_p = true;
@@ -1568,7 +1573,6 @@ check_load_store_for_partial_vectors (loop_vec_info 
loop_vinfo, tree vectype,
           && can_vec_mask_load_store_p (vecmode, mask_mode, is_load, NULL,
                                         elsvals))
     {
-      nvectors = group_memory_nvectors (group_size * vf, nunits);
       vect_record_loop_mask (loop_vinfo, masks, nvectors, vectype, 
scalar_mask);
       using_partial_vectors_p = true;
     }

Reply via email to