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

            Bug ID: 122711
           Summary: cunrolli sometimes duplicates inner loops with
                    simd-UID but proper remapping fails
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: rguenth at gcc dot gnu.org
  Target Milestone: ---

gcc.dg/vect/vect-simd-17.c shows early unrolling doing

12:1: optimized: loop with 1 iterations completely unrolled (header execution
count 14598063)

and with early LIM additionally

12:1: optimized: loop with 2 iterations completely unrolled (header execution
count 14598063)

this is unrolling of outer loops where only the inner loop as ->force_vectorize
set.  While duplicate_loop_body_to_header_edge copies inner loops
appropriately,
in particular ->force_vectorize, it does not duplicate ->simduid nor is there
something that remaps / reallocates them.  copy_body_data is only involved
downstream in copy_bbs, the caller duplicate_loop_body_to_header_edge could
in principle set up remapping appropriately.

The following avoids the trivial two-level case, still allowing eliding of
outer loops.  Other passes like loop versioning or unswitching might be
similarly affected (either appropriately copying or ICEing on copy should
probably done).

diff --git a/gcc/tree-ssa-loop-ivcanon.cc b/gcc/tree-ssa-loop-ivcanon.cc
index ca6295c7de2..e5abb2a7aac 100644
--- a/gcc/tree-ssa-loop-ivcanon.cc
+++ b/gcc/tree-ssa-loop-ivcanon.cc
@@ -1486,7 +1486,9 @@ tree_unroll_loops_completely_1 (bool may_increase_size,
bool unroll_outer,
   if (!loop_father)
     return false;

-  if (loop->unroll > 1)
+  if (loop->inner && loop->inner->force_vectorize)
+    ul = UL_SINGLE_ITER;
+  else if (loop->unroll > 1)
     ul = UL_ALL;
   else if (may_increase_size && optimize_loop_nest_for_speed_p (loop)
       /* Unroll outermost loops only if asked to do so or they do

Reply via email to