Hi,

This is a v2 which hopefully addresses the feedback for v1 of the 1/5
patch, originally posted here:
https://gcc.gnu.org/pipermail/gcc-patches/2024-October/666648.html

As mentioned on IRC, it will need follow-up work to fix up latent
profile issues, but that can be done during stage 3.  We will also need
a simple (hopefully obvious, even) follow-up patch to fix expectations
for various tests (since we now vectorize loops which we previously
couldn't).

OK for trunk?

Thanks,
Alex

-- >8 --

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 already a
power-of-two multiple of the amount read per vector iteration.

gcc/ChangeLog:

        * 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 and strided_p accesses.
        (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.  Add new result parameter.  Use it ...
        (vect_analyze_data_refs_alignment): ... here, and fail
        analysis if vect_compute_data_ref_alignment sets it to a
        failure.
        (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.
diff --git a/gcc/tree-vect-data-refs.cc b/gcc/tree-vect-data-refs.cc
index 6276139b71f..ff18f53bd2a 100644
--- a/gcc/tree-vect-data-refs.cc
+++ b/gcc/tree-vect-data-refs.cc
@@ -739,15 +739,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))
@@ -1230,11 +1238,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;
 }
@@ -1302,6 +1314,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
 
@@ -1310,7 +1325,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;
@@ -1337,6 +1352,56 @@ 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 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
+      if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
+       vf *= DR_GROUP_SIZE (stmt_info);
+
+      auto num_vectors = vect_get_num_vectors (vf, 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;
+
+      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
@@ -2852,8 +2917,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;
        }
     }
 
@@ -7208,7 +7277,8 @@ vect_supportable_dr_alignment (vec_info *vinfo, 
dr_vec_info *dr_info,
   if (misalignment == DR_MISALIGNMENT_UNKNOWN)
     is_packed = not_size_aligned (DR_REF (dr));
   if (targetm.vectorize.support_vector_misalignment (mode, type, misalignment,
-                                                    is_packed))
+                                                    is_packed)
+      && !dr_info->need_peeling_for_alignment)
     return dr_unaligned_supported;
 
   /* Unsupported.  */
diff --git a/gcc/tree-vect-loop-manip.cc b/gcc/tree-vect-loop-manip.cc
index c8dc7153298..be2c2a1bc75 100644
--- a/gcc/tree-vect-loop-manip.cc
+++ b/gcc/tree-vect-loop-manip.cc
@@ -3129,12 +3129,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);
diff --git a/gcc/tree-vectorizer.h b/gcc/tree-vectorizer.h
index 273e8c644e7..3c3bfd4916f 100644
--- a/gcc/tree-vectorizer.h
+++ b/gcc/tree-vectorizer.h
@@ -1277,6 +1277,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

Reply via email to