]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
vect: Force alignment peeling to vectorize more early break loops [PR118211]
authorAlex Coplan <alex.coplan@arm.com>
Mon, 11 Mar 2024 13:09:10 +0000 (13:09 +0000)
committerTamar Christina <tamar.christina@arm.com>
Fri, 10 Jan 2025 21:21:06 +0000 (21:21 +0000)
This allows us to vectorize more loops with early exits by forcing
peeling for alignment to make sure that we're guaranteed to be able to
safely read an entire vector iteration without crossing a page boundary.

To make this work for VLA architectures we have to allow compile-time
non-constant target alignments.  We also have to override the result of
the target's preferred_vector_alignment hook if it isn't a power-of-two
multiple of the TYPE_SIZE of the chosen vector type.

gcc/ChangeLog:

PR tree-optimization/118211
PR tree-optimization/116126
* tree-vect-data-refs.cc (vect_analyze_early_break_dependences):
Set need_peeling_for_alignment flag on read DRs instead of
failing vectorization.  Punt on gathers.
(dr_misalignment): Handle non-constant target alignments.
(vect_compute_data_ref_alignment): If need_peeling_for_alignment
flag is set on the DR, then override the target alignment chosen
by the preferred_vector_alignment hook to choose a safe
alignment.
(vect_supportable_dr_alignment): Override
support_vector_misalignment hook if need_peeling_for_alignment
is set on the DR: in this case we must return
dr_unaligned_unsupported in order to force peeling.
* tree-vect-loop-manip.cc (vect_do_peeling): Allow prolog
peeling by a compile-time non-constant amount.
* tree-vectorizer.h (dr_vec_info): Add new flag
need_peeling_for_alignment.

gcc/testsuite/ChangeLog:

PR tree-optimization/118211
PR tree-optimization/116126
* gcc.dg/tree-ssa/cunroll-13.c: Don't vectorize.
* gcc.dg/tree-ssa/cunroll-14.c: Likewise.
* gcc.dg/unroll-6.c: Likewise.
* gcc.dg/tree-ssa/gen-vect-28.c: Likewise.
* gcc.dg/vect/vect-104.c: Expect to vectorize.
* gcc.dg/vect/vect-early-break_108-pr113588.c: Likewise.
* gcc.dg/vect/vect-early-break_109-pr113588.c: Likewise.
* gcc.dg/vect/vect-early-break_110-pr113467.c: Likewise.
* gcc.dg/vect/vect-early-break_3.c: Likewise.
* gcc.dg/vect/vect-early-break_65.c: Likewise.
* gcc.dg/vect/vect-early-break_8.c: Likewise.
* gfortran.dg/vect/vect-5.f90: Likewise.
* gfortran.dg/vect/vect-8.f90: Likewise.
* gcc.dg/vect/vect-switch-search-line-fast.c:

Co-Authored-By: Tamar Christina <tamar.christina@arm.com>
17 files changed:
gcc/testsuite/gcc.dg/tree-ssa/cunroll-13.c
gcc/testsuite/gcc.dg/tree-ssa/cunroll-14.c
gcc/testsuite/gcc.dg/tree-ssa/gen-vect-28.c
gcc/testsuite/gcc.dg/unroll-6.c
gcc/testsuite/gcc.dg/vect/vect-104.c
gcc/testsuite/gcc.dg/vect/vect-early-break_108-pr113588.c
gcc/testsuite/gcc.dg/vect/vect-early-break_109-pr113588.c
gcc/testsuite/gcc.dg/vect/vect-early-break_110-pr113467.c
gcc/testsuite/gcc.dg/vect/vect-early-break_3.c
gcc/testsuite/gcc.dg/vect/vect-early-break_65.c
gcc/testsuite/gcc.dg/vect/vect-early-break_8.c
gcc/testsuite/gcc.dg/vect/vect-switch-search-line-fast.c
gcc/testsuite/gfortran.dg/vect/vect-5.f90
gcc/testsuite/gfortran.dg/vect/vect-8.f90
gcc/tree-vect-data-refs.cc
gcc/tree-vect-loop-manip.cc
gcc/tree-vectorizer.h

index 98cb56a8564becfc8d5cd0253fa74f23ccf1bc77..154e2963f12de8819c00ba03846f355692c587b1 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do compile } */
-/* { dg-options "-O3 -fgimple -fdump-tree-cunroll-blocks-details" } */
+/* { dg-options "-O3 -fgimple -fdump-tree-cunroll-blocks-details -fno-tree-vectorize" } */
 
 #if __SIZEOF_INT__ < 4
 __extension__ typedef __INT32_TYPE__ i32;
index 5f112da310c8354eb9698b6d964700d73a043735..4b369f7ad2789db10b2a2c5d72031abd272550e6 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do compile } */
-/* { dg-options "-O3 -fdump-tree-cunroll-blocks-details" } */
+/* { dg-options "-O3 -fdump-tree-cunroll-blocks-details -fno-tree-vectorize" } */
 struct a {int a[100];};
 void
 t(struct a *a)
index c5f1b5aff1159adbc7fa652982d5d0b8fe14ad39..5c0ea58a7b00d27e38bc4dbbea7271c793af66fa 100644 (file)
@@ -20,6 +20,7 @@ int main_1 (int off)
     }
 
   /* check results:  */
+#pragma GCC novector
   for (i = 0; i < N; i++)
     {
       if (ia[i+off] != 5)
index 7664bbff109f9d79fef623cab8e60368cb1487c7..7be1b7cfadba88b87d8dc0f2969cb28e9bb09655 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do compile } */
-/* { dg-options "-O3 -fdump-rtl-loop2_unroll-details-blocks -funroll-loops" } */
+/* { dg-options "-O3 -fdump-rtl-loop2_unroll-details-blocks -funroll-loops -fno-tree-vectorize" } */
 /* { dg-require-effective-target int32plus } */
 
 void abort (void);
index 730efd39bd4a38fddd6e600a4bcb185b7d524b70..8890a5da180b71a578c15f94755d50087bdf3b54 100644 (file)
@@ -46,6 +46,7 @@ int main1 (int x) {
 #pragma GCC novector
   for (i = 0; i < N; i++)
    {
+#pragma GCC novector
     for (j = 0; j < N; j++)
      {
        if (p->a[i][j] != c[i][j])
index e488619c9aac41fafbcf479818392a6bb7c6924f..78b22f3b43b48b941cb2282c142e6a996b1a7035 100644 (file)
@@ -3,7 +3,7 @@
 /* { dg-require-effective-target vect_early_break } */
 /* { dg-require-effective-target vect_int } */
 
-/* { dg-final { scan-tree-dump-not "LOOP VECTORIZED" "vect" } } */
+/* { dg-final { scan-tree-dump "LOOP VECTORIZED" "vect" } } */
 
 int foo (const char *s, unsigned long n)
 {
index 488c19d3ede809631d1a7ede0e7f7bcdc7a1ae43..2347fc26a14fc2b2f6251c4c92332f9cac044901 100644 (file)
@@ -3,7 +3,7 @@
 /* { dg-require-effective-target vect_int } */
 /* { dg-require-effective-target mmap } */
 
-/* { dg-final { scan-tree-dump-not "LOOP VECTORIZED" "vect" } } */
+/* { dg-final { scan-tree-dump "LOOP VECTORIZED" "vect" } } */
 
 #include <sys/mman.h>
 #include <unistd.h>
index 12d0ea1e871b51742c040c909ea5741bc820206e..4f5a87c3ab944d4b5ed3153a45a247918e5250c0 100644 (file)
@@ -2,7 +2,7 @@
 /* { dg-require-effective-target vect_early_break } */
 /* { dg-require-effective-target vect_long_long } */
 
-/* { dg-final { scan-tree-dump-not "LOOP VECTORIZED" "vect" } } */
+/* { dg-final { scan-tree-dump "LOOP VECTORIZED" "vect" } } */
 
 #include "tree-vect.h"
 #include <stdint.h>
index 4afbc7266765fc4639b2554c6361e0825411f148..9d6cd0a191f6ed31179faa89e2c0677f30163759 100644 (file)
@@ -5,7 +5,7 @@
 
 /* { dg-additional-options "-Ofast" } */
 
-/* { dg-final { scan-tree-dump-not "LOOP VECTORIZED" "vect" } } */
+/* { dg-final { scan-tree-dump "LOOP VECTORIZED" "vect" } } */
 
 unsigned test4(char x, char *vect, int n)
 {  
index fa87999dcd4ce1002cccaf08373d24716f531bcb..8763a5ff04ec94d41c6e3a09da5817d340fa34a9 100644 (file)
@@ -17,4 +17,4 @@ void f() {
       return;
 }
 
-/* { dg-final { scan-tree-dump-times "vectorized 1 loops" 0 "vect" } } */
+/* { dg-final { scan-tree-dump-times "vectorized 1 loops" 1 "vect" } } */
index 84e19423e2e61144cc575fb992b5f23d480ab89d..541f439a9b49ad4c2e60139398f9df9369084774 100644 (file)
@@ -5,7 +5,7 @@
 
 /* { dg-additional-options "-Ofast" } */
 
-/* { dg-final { scan-tree-dump-not "LOOP VECTORIZED" "vect" } } */
+/* { dg-final { scan-tree-dump "LOOP VECTORIZED" "vect" } } */
 
 #include <complex.h>
 
index 15f3a4ef38a7be68d2049ff3eb41a1b2b9f2af67..02ad7a451ca2218cf827d3b6cca4b36950fba555 100644 (file)
@@ -14,4 +14,5 @@ const unsigned char *search_line_fast2 (const unsigned char *s,
   return s;
 }
 
-/* { dg-final { scan-tree-dump-times "vectorized 1 loops" 1 "vect" { xfail *-*-* } } } */
+/* { dg-final { scan-tree-dump-times "vectorized 1 loops" 1 "vect" { target { ilp32 } } } } */
+/* { dg-final { scan-tree-dump-times "vectorized 1 loops" 0 "vect" { target { ! ilp32 } } } } */
index b11cabaee23d78476f3702fdcb8a74e47e685784..cca4875b859b4111ad4202f0286594617c87b2b2 100644 (file)
@@ -18,6 +18,7 @@
         end do
 
         do I = 1, N
+!GCC$ novector
           do J = I, M
             if (A(J,2) /= B(J)) then
               STOP 1
index 918eddee292f4394ddbe8796f05dc8a6498c6be2..d4ce44feb4b953e599a4df0d9128a86f756ae556 100644 (file)
@@ -706,7 +706,6 @@ CALL track('KERNEL  ')
 RETURN
 END SUBROUTINE kernel
 
-! { dg-final { scan-tree-dump-times "vectorized 2\[56\] loops" 1 "vect" { target aarch64_sve } } }
-! { dg-final { scan-tree-dump-times "vectorized 2\[45\] loops" 1 "vect" { target { aarch64*-*-* && { ! aarch64_sve } } } } }
-! { dg-final { scan-tree-dump-times "vectorized 2\[3456\] loops" 1 "vect" { target { vect_intdouble_cvt && { ! aarch64*-*-* } } } } }
+! { dg-final { scan-tree-dump-times "vectorized 2\[56\] loops" 1 "vect" { target aarch64*-*-* } } }
+! { dg-final { scan-tree-dump-times "vectorized 2\[34567\] loops" 1 "vect" { target { vect_intdouble_cvt && { ! aarch64*-*-* } } } } }
 ! { dg-final { scan-tree-dump-times "vectorized 17 loops" 1 "vect" { target { { ! vect_intdouble_cvt } && { ! aarch64*-*-* } } } } }
index c10508de55543122ac70b63363d077e12aea3e6f..6eda40267bd1382938a77826d11f20dcc959a166 100644 (file)
@@ -34,6 +34,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "optabs-tree.h"
 #include "cgraph.h"
 #include "dumpfile.h"
+#include "pretty-print.h"
 #include "alias.h"
 #include "fold-const.h"
 #include "stor-layout.h"
@@ -750,15 +751,23 @@ vect_analyze_early_break_dependences (loop_vec_info loop_vinfo)
          if (DR_IS_READ (dr_ref)
              && !ref_within_array_bound (stmt, DR_REF (dr_ref)))
            {
+             if (STMT_VINFO_GATHER_SCATTER_P (stmt_vinfo)
+                 || STMT_VINFO_STRIDED_P (stmt_vinfo))
+               {
+                 const char *msg
+                   = "early break not supported: cannot peel "
+                     "for alignment, vectorization would read out of "
+                     "bounds at %G";
+                 return opt_result::failure_at (stmt, msg, stmt);
+               }
+
+             dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_vinfo);
+             dr_info->need_peeling_for_alignment = true;
+
              if (dump_enabled_p ())
-               dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
-                                "early breaks not supported: vectorization "
-                                "would %s beyond size of obj.\n",
-                                DR_IS_READ (dr_ref) ? "read" : "write");
-             return opt_result::failure_at (stmt,
-                                "can't safely apply code motion to "
-                                "dependencies of %G to vectorize "
-                                "the early exit.\n", stmt);
+               dump_printf_loc (MSG_NOTE, vect_location,
+                                "marking DR (read) as needing peeling for "
+                                "alignment at %G", stmt);
            }
 
          if (DR_IS_READ (dr_ref))
@@ -1241,11 +1250,15 @@ dr_misalignment (dr_vec_info *dr_info, tree vectype, poly_int64 offset)
      offset which can for example result from a negative stride access.  */
   poly_int64 misalignment = misalign + diff + offset;
 
-  /* vect_compute_data_ref_alignment will have ensured that target_alignment
-     is constant and otherwise set misalign to DR_MISALIGNMENT_UNKNOWN.  */
-  unsigned HOST_WIDE_INT target_alignment_c
-    = dr_info->target_alignment.to_constant ();
-  if (!known_misalignment (misalignment, target_alignment_c, &misalign))
+  /* Below we reject compile-time non-constant target alignments, but if
+     our misalignment is zero, then we are known to already be aligned
+     w.r.t. any such possible target alignment.  */
+  if (known_eq (misalignment, 0))
+    return 0;
+
+  unsigned HOST_WIDE_INT target_alignment_c;
+  if (!dr_info->target_alignment.is_constant (&target_alignment_c)
+      || !known_misalignment (misalignment, target_alignment_c, &misalign))
     return DR_MISALIGNMENT_UNKNOWN;
   return misalign;
 }
@@ -1313,6 +1326,9 @@ vect_record_base_alignments (vec_info *vinfo)
    Compute the misalignment of the data reference DR_INFO when vectorizing
    with VECTYPE.
 
+   RESULT is non-NULL iff VINFO is a loop_vec_info.  In that case, *RESULT will
+   be set appropriately on failure (but is otherwise left unchanged).
+
    Output:
    1. initialized misalignment info for DR_INFO
 
@@ -1321,7 +1337,7 @@ vect_record_base_alignments (vec_info *vinfo)
 
 static void
 vect_compute_data_ref_alignment (vec_info *vinfo, dr_vec_info *dr_info,
-                                tree vectype)
+                                tree vectype, opt_result *result = nullptr)
 {
   stmt_vec_info stmt_info = dr_info->stmt;
   vec_base_alignments *base_alignments = &vinfo->base_alignments;
@@ -1348,6 +1364,67 @@ vect_compute_data_ref_alignment (vec_info *vinfo, dr_vec_info *dr_info,
   poly_uint64 vector_alignment
     = exact_div (targetm.vectorize.preferred_vector_alignment (vectype),
                 BITS_PER_UNIT);
+
+  /* If this DR needs peeling for alignment for correctness, we must
+     ensure the target alignment is a constant power-of-two multiple of the
+     amount read per vector iteration (overriding the above hook where
+     necessary).  */
+  if (dr_info->need_peeling_for_alignment)
+    {
+      /* Vector size in bytes.  */
+      poly_uint64 safe_align = tree_to_poly_uint64 (TYPE_SIZE_UNIT (vectype));
+
+      /* We can only peel for loops, of course.  */
+      gcc_checking_assert (loop_vinfo);
+
+      /* Calculate the number of vectors read per vector iteration.  If
+        it is a power of two, multiply through to get the required
+        alignment in bytes.  Otherwise, fail analysis since alignment
+        peeling wouldn't work in such a case.  */
+      poly_uint64 num_scalars = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
+      if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
+       num_scalars *= DR_GROUP_SIZE (stmt_info);
+
+      auto num_vectors = vect_get_num_vectors (num_scalars, vectype);
+      if (!pow2p_hwi (num_vectors))
+       {
+         *result = opt_result::failure_at (vect_location,
+                                           "non-power-of-two num vectors %u "
+                                           "for DR needing peeling for "
+                                           "alignment at %G",
+                                           num_vectors, stmt_info->stmt);
+         return;
+       }
+
+      safe_align *= num_vectors;
+      if (maybe_gt (safe_align, 4096U))
+       {
+         pretty_printer pp;
+         pp_wide_integer (&pp, safe_align);
+         *result = opt_result::failure_at (vect_location,
+                                           "alignment required for correctness"
+                                           " (%s) may exceed page size",
+                                           pp_formatted_text (&pp));
+         return;
+       }
+
+      unsigned HOST_WIDE_INT multiple;
+      if (!constant_multiple_p (vector_alignment, safe_align, &multiple)
+         || !pow2p_hwi (multiple))
+       {
+         if (dump_enabled_p ())
+           {
+             dump_printf_loc (MSG_NOTE, vect_location,
+                              "forcing alignment for DR from preferred (");
+             dump_dec (MSG_NOTE, vector_alignment);
+             dump_printf (MSG_NOTE, ") to safe align (");
+             dump_dec (MSG_NOTE, safe_align);
+             dump_printf (MSG_NOTE, ") for stmt: %G", stmt_info->stmt);
+           }
+         vector_alignment = safe_align;
+       }
+    }
+
   SET_DR_TARGET_ALIGNMENT (dr_info, vector_alignment);
 
   /* If the main loop has peeled for alignment we have no way of knowing
@@ -2865,8 +2942,12 @@ vect_analyze_data_refs_alignment (loop_vec_info loop_vinfo)
          if (STMT_VINFO_GROUPED_ACCESS (dr_info->stmt)
              && DR_GROUP_FIRST_ELEMENT (dr_info->stmt) != dr_info->stmt)
            continue;
+         opt_result res = opt_result::success ();
          vect_compute_data_ref_alignment (loop_vinfo, dr_info,
-                                          STMT_VINFO_VECTYPE (dr_info->stmt));
+                                          STMT_VINFO_VECTYPE (dr_info->stmt),
+                                          &res);
+         if (!res)
+           return res;
        }
     }
 
@@ -7130,6 +7211,8 @@ vect_supportable_dr_alignment (vec_info *vinfo, dr_vec_info *dr_info,
 
   if (misalignment == 0)
     return dr_aligned;
+  else if (dr_info->need_peeling_for_alignment)
+    return dr_unaligned_unsupported;
 
   /* For now assume all conditional loads/stores support unaligned
      access without any special code.  */
index 5d1b70aea43c814be19f2aabe98209431397f3be..15cac0fe27dfb239401f75f46dfbfba44ef94e88 100644 (file)
@@ -3128,12 +3128,6 @@ vect_do_peeling (loop_vec_info loop_vinfo, tree niters, tree nitersm1,
   int estimated_vf;
   int prolog_peeling = 0;
   bool vect_epilogues = loop_vinfo->epilogue_vinfo != NULL;
-  /* We currently do not support prolog peeling if the target alignment is not
-     known at compile time.  'vect_gen_prolog_loop_niters' depends on the
-     target alignment being constant.  */
-  dr_vec_info *dr_info = LOOP_VINFO_UNALIGNED_DR (loop_vinfo);
-  if (dr_info && !DR_TARGET_ALIGNMENT (dr_info).is_constant ())
-    return NULL;
 
   if (!vect_use_loop_mask_for_alignment_p (loop_vinfo))
     prolog_peeling = LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo);
index 135eb119ca2eaebbaf5d7fe54ea4bcb98a8552ef..79db02a39a8f7d100d4dce18ff9a7a60c87db20b 100644 (file)
@@ -1278,6 +1278,11 @@ public:
   poly_uint64 target_alignment;
   /* If true the alignment of base_decl needs to be increased.  */
   bool base_misaligned;
+
+  /* Set by early break vectorization when this DR needs peeling for alignment
+     for correctness.  */
+  bool need_peeling_for_alignment;
+
   tree base_decl;
 
   /* Stores current vectorized loop's offset.  To be added to the DR's