https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128695

            Bug ID: 128695
           Summary: Missed early mode pruning in vect_analyze_loop when
                    chaining multiple epilogues (vectorizer)
           Product: gcc
           Version: 15.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jin_kang66 at 163 dot com
  Target Milestone: ---

Location: gcc/tree-vect-loop.cc (function vect_analyze_loop)

Problem Description:
--------------------
In vect_analyze_loop, when analyzing vector modes for epilogue loops, there is
an early-filtering check intended to skip candidate vector modes that cannot
possibly yield a valid epilogue vectorization factor (VF):

  bool supports_partial_vectors = partial_vectors_supported_p ()
                                  && param_vect_partial_vector_usage != 0;
  poly_uint64 first_vinfo_vf = LOOP_VINFO_VECT_FACTOR (first_loop_vinfo);

  loop_vec_info orig_loop_vinfo = first_loop_vinfo;
  do
    {
      while (1)
        {
          /* If the target does not support partial vectors we can shorten the
             number of modes to analyze for the epilogue as we know we can't
pick
             a mode that would lead to a VF at least as big as the 
             FIRST_VINFO_VF. */
          if (!supports_partial_vectors
              && maybe_ge (cached_vf_per_mode[mode_i], first_vinfo_vf))
            {
              mode_i++;
              if (mode_i == vector_modes.length ())
                break;
              continue;
            }

When epilogue chaining was added via the outer do { ... } while (1) loop,
orig_loop_vinfo is updated to advance to the next epilogue (orig_loop_vinfo =
orig_loop_vinfo->epilogue_vinfo).

However, the check still compares against first_vinfo_vf (the primary vector
loop's VF), rather than the immediate parent loop's VF (LOOP_VINFO_VECT_FACTOR
(orig_loop_vinfo)).

Impact:

For subsequent chained epilogues (e.g. searching for epilogue 2 after
epilogue 1):

  - Suppose Main Loop has VF = 16 (AVX-512) -> first_vinfo_vf = 16.
  - Epilogue 1 is chosen with VF = 8 (AVX2).
  - When searching for Epilogue 2, a valid mode must have VF < 8 (e.g. SSE, VF
    = 4).
  - If another mode in vector_modes has a cached VF of 8: maybe_ge (8, 16)
    evaluates to false, so the mode is NOT pruned early.
  - GCC invokes vect_analyze_loop_1, running unnecessary data reference,
    alignment, dependence, and SLP analyses, only to have vect_analyze_loop_2
    reject it downstream via:
    if (!LOOP_VINFO_USING_PARTIAL_VECTORS_P (loop_vinfo)
        && maybe_ge (LOOP_VINFO_VECT_FACTOR (loop_vinfo),
           LOOP_VINFO_VECT_FACTOR (orig_loop_vinfo)))
      return opt_result::failure_at (...);

While this does not cause a correctness issue (as vect_analyze_loop_2 acts as a
safety net), it misses an early pruning opportunity and leads to redundant
compilation overhead.

Suggested Fix:

Inside the do { ... } while (1) loop, the check should compare against the
current parent loop's VF:

diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc
--- a/gcc/tree-vect-loop.cc
+++ b/gcc/tree-vect-loop.cc
@@ -...,... +...,... @@ vect_analyze_loop (class loop *loop, gimple
*loop_vectorized_call,
   loop_vec_info orig_loop_vinfo = first_loop_vinfo;
   do
     {
+      poly_uint64 parent_vf = LOOP_VINFO_VECT_FACTOR (orig_loop_vinfo);
       while (1)
        {
          /* If the target does not support partial vectors we can shorten the
             number of modes to analyze for the epilogue as we know we can't
pick
             a mode that would lead to a VF at least as big as the 
-            FIRST_VINFO_VF.  */
+            PARENT_VF.  */
          if (!supports_partial_vectors
-             && maybe_ge (cached_vf_per_mode[mode_i], first_vinfo_vf))
+             && maybe_ge (cached_vf_per_mode[mode_i], parent_vf))
            {
              mode_i++;
              if (mode_i == vector_modes.length ())

Reply via email to