https://gcc.gnu.org/g:3d11d69600b7c3ebf0cf8eacc6a4bc8d994710e9

commit r16-5316-g3d11d69600b7c3ebf0cf8eacc6a4bc8d994710e9
Author: Richard Biener <[email protected]>
Date:   Fri Nov 14 13:40:50 2025 +0100

    Always analyze possible partial vector usage
    
    The following makes us always start with 
LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P
    as true and only makes vect_determine_partial_vectors_and_peeling
    honor --param vect-partial-vector-usage or explicit requests from the
    target for epilog vectorization.  This exposes whether we could have
    used partial vectors to the target at costing time as even when the
    main loop is never supposed to get masked the value is useful to
    determine possible epilog loop masking.
    
            * tree-vect-loop.cc (_loop_vec_info::_loop_vec_info):
            Initialize can_use_partial_vectors_p to true.
            (vect_determine_partial_vectors_and_peeling): Add masked_p
            parameter and honor it.
            (vect_analyze_loop_2): Pass through masked_p.
            (vect_analyze_loop_1): Pass down masked_p.
            (vect_analyze_loop): Simplify check on possible masking of
            the epilog when there's no .WHILE_ULT.

Diff:
---
 gcc/tree-vect-loop.cc | 38 ++++++++++++++++++++++----------------
 1 file changed, 22 insertions(+), 16 deletions(-)

diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc
index 9bec57967b02..b11b4c168ab2 100644
--- a/gcc/tree-vect-loop.cc
+++ b/gcc/tree-vect-loop.cc
@@ -740,7 +740,7 @@ _loop_vec_info::_loop_vec_info (class loop *loop_in, 
vec_info_shared *shared)
     scan_map (NULL),
     inner_loop_cost_factor (param_vect_inner_loop_cost_factor),
     vectorizable (false),
-    can_use_partial_vectors_p (param_vect_partial_vector_usage != 0),
+    can_use_partial_vectors_p (true),
     must_use_partial_vectors_p (false),
     using_partial_vectors_p (false),
     using_decrementing_iv_p (false),
@@ -2002,10 +2002,18 @@ vect_get_datarefs_in_loop (loop_p loop, basic_block 
*bbs,
         LOOP_VINFO_USING_PARTIAL_VECTORS_P == false
         LOOP_VINFO_PEELING_FOR_NITER == true
 
+   The MASKED_P argument specifies to what extent
+   param_vect_partial_vector_usage is to be honored.  For MASKED_P == 0
+   no partial vectors are to be used, for MASKED_P == -1 it's
+   param_vect_partial_vector_usage that gets to decide whether we may
+   consider partial vector usage.  For MASKED_P == 1 partial vectors
+   may be used if possible.
+
  */
 
 static opt_result
-vect_determine_partial_vectors_and_peeling (loop_vec_info loop_vinfo)
+vect_determine_partial_vectors_and_peeling (loop_vec_info loop_vinfo,
+                                           int masked_p)
 {
   /* Determine whether there would be any scalar iterations left over.  */
   bool need_peeling_or_partial_vectors_p
@@ -2013,8 +2021,12 @@ vect_determine_partial_vectors_and_peeling 
(loop_vec_info loop_vinfo)
 
   /* Decide whether to vectorize the loop with partial vectors.  */
   LOOP_VINFO_USING_PARTIAL_VECTORS_P (loop_vinfo) = false;
-  if (LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)
-      && LOOP_VINFO_MUST_USE_PARTIAL_VECTORS_P (loop_vinfo))
+  if (masked_p == 0
+      || (masked_p == -1 && param_vect_partial_vector_usage == 0))
+    /* If requested explicitly do not use partial vectors.  */
+    LOOP_VINFO_USING_PARTIAL_VECTORS_P (loop_vinfo) = false;
+  else if (LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)
+          && LOOP_VINFO_MUST_USE_PARTIAL_VECTORS_P (loop_vinfo))
     LOOP_VINFO_USING_PARTIAL_VECTORS_P (loop_vinfo) = true;
   else if (LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)
           && need_peeling_or_partial_vectors_p)
@@ -2072,7 +2084,7 @@ vect_determine_partial_vectors_and_peeling (loop_vec_info 
loop_vinfo)
    SINGLE_LANE_SLP_DONE_FOR_SUGGESTED_UF is to hold whether single-lane
    slp was forced when the suggested unroll factor was worked out.  */
 static opt_result
-vect_analyze_loop_2 (loop_vec_info loop_vinfo, bool &fatal,
+vect_analyze_loop_2 (loop_vec_info loop_vinfo, int masked_p, bool &fatal,
                     unsigned *suggested_unroll_factor,
                     bool& single_lane_slp_done_for_suggested_uf)
 {
@@ -2331,7 +2343,7 @@ start_over:
      assuming that the loop will be used as a main loop.  We will redo
      this analysis later if we instead decide to use the loop as an
      epilogue loop.  */
-  ok = vect_determine_partial_vectors_and_peeling (loop_vinfo);
+  ok = vect_determine_partial_vectors_and_peeling (loop_vinfo, masked_p);
   if (!ok)
     return ok;
 
@@ -2751,13 +2763,11 @@ vect_analyze_loop_1 (class loop *loop, vec_info_shared 
*shared,
 
   machine_mode vector_mode = vector_modes[mode_i];
   loop_vinfo->vector_mode = vector_mode;
-  if (masked_p != -1)
-    loop_vinfo->can_use_partial_vectors_p = masked_p;
   unsigned int suggested_unroll_factor = 1;
   bool single_lane_slp_done_for_suggested_uf = false;
 
   /* Run the main analysis.  */
-  opt_result res = vect_analyze_loop_2 (loop_vinfo, fatal,
+  opt_result res = vect_analyze_loop_2 (loop_vinfo, masked_p, fatal,
                                        &suggested_unroll_factor,
                                        single_lane_slp_done_for_suggested_uf);
   if (dump_enabled_p ())
@@ -2800,8 +2810,8 @@ vect_analyze_loop_1 (class loop *loop, vec_info_shared 
*shared,
            unroll_vinfo->vector_mode = vector_mode;
            unroll_vinfo->suggested_unroll_factor = suggested_unroll_factor;
            opt_result new_res
-               = vect_analyze_loop_2 (unroll_vinfo, fatal, NULL,
-                                      single_lane_slp_done_for_suggested_uf);
+             = vect_analyze_loop_2 (unroll_vinfo, masked_p, fatal, NULL,
+                                    single_lane_slp_done_for_suggested_uf);
            if (new_res)
              {
                delete loop_vinfo;
@@ -3039,13 +3049,9 @@ vect_analyze_loop (class loop *loop, gimple 
*loop_vectorized_call,
 
   bool supports_partial_vectors = (param_vect_partial_vector_usage != 0
                                   || masked_p == 1);
-  machine_mode mask_mode;
   if (supports_partial_vectors
       && !partial_vectors_supported_p ()
-      && !(VECTOR_MODE_P (first_loop_vinfo->vector_mode)
-          && targetm.vectorize.get_mask_mode
-               (first_loop_vinfo->vector_mode).exists (&mask_mode)
-          && SCALAR_INT_MODE_P (mask_mode)))
+      && !LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (first_loop_vinfo))
     supports_partial_vectors = false;
   poly_uint64 first_vinfo_vf = LOOP_VINFO_VECT_FACTOR (first_loop_vinfo);

Reply via email to