On Fri, 29 Dec 2023, Tamar Christina wrote:

> Hi All,
> 
> Only trying to update certain dominators doesn't seem to work very well
> because as the loop gets versioned, peeled, or skip_vector then we end up with
> very complicated control flow.  This means that the final merge blocks for the
> loop exit are not easy to find or update.
> 
> Instead of trying to pick which exits to update, this changes it to update all
> the blocks reachable by the new exits.  This is because they'll contain common
> blocks with e.g. the versioned loop.  It's these blocks that need an update
> most of the time.
> 
> Bootstrapped Regtested on aarch64-none-linux-gnu and no issues.
> 
> Ok for master?

This makes it quadratic in the number of vectorized early exit loops
in a function.  The vectorizer CFG manipulation operates in a local
enough bubble that programmatic updating of dominators should be
possible (after all we manage to produce correct SSA form!), the
proposed change gets us too far off to a point where re-computating
dominance info is likely cheaper (but no, we shouldn't do this either).

Can you instead give manual updating a try again?  I think
versioning should produce up-to-date dominator info, it's only
when you redirect branches during peeling that you'd need
adjustments - but IIRC we're never introducing new merges?

IIRC we can't wipe dominators during transform since we query them
during code generation.  We possibly could code generate all
CFG manipulations of all vectorized loops, recompute all dominators
and then do code generation of all vectorized loops.

But then we're doing a loop transform and the exits will ultimatively
end up in the same place, so the CFG and dominator update is bound to
where the original exits went to.

Richard

> Thanks,
> Tamar
> 
> gcc/ChangeLog:
> 
>       PR middle-end/113144
>       * tree-vect-loop-manip.cc (slpeel_tree_duplicate_loop_to_edge_cfg):
>       Update all dominators reachable from exit.
> 
> gcc/testsuite/ChangeLog:
> 
>       PR middle-end/113144
>       * gcc.dg/vect/vect-early-break_94-pr113144.c: New test.
> 
> --- inline copy of patch -- 
> diff --git a/gcc/testsuite/gcc.dg/vect/vect-early-break_94-pr113144.c 
> b/gcc/testsuite/gcc.dg/vect/vect-early-break_94-pr113144.c
> new file mode 100644
> index 
> 0000000000000000000000000000000000000000..903fe7be6621e81db6f29441e4309fa213d027c5
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/vect/vect-early-break_94-pr113144.c
> @@ -0,0 +1,41 @@
> +/* { dg-do compile } */
> +/* { dg-add-options vect_early_break } */
> +/* { dg-require-effective-target vect_early_break } */
> +/* { dg-require-effective-target vect_int } */
> +
> +/* { dg-final { scan-tree-dump "LOOP VECTORIZED" "vect" } } */
> +
> +long tar_atol256_max, tar_atol256_size, tar_atosl_min;
> +char tar_atol256_s;
> +void __errno_location();
> +
> +
> +inline static long tar_atol256(long min) {
> +  char c;
> +  int sign;
> +  c = tar_atol256_s;
> +  sign = c;
> +  while (tar_atol256_size) {
> +    if (c != sign)
> +      return sign ? min : tar_atol256_max;
> +    c = tar_atol256_size--;
> +  }
> +  if ((c & 128) != (sign & 128))
> +    return sign ? min : tar_atol256_max;
> +  return 0;
> +}
> +
> +inline static long tar_atol(long min) {
> +  return tar_atol256(min);
> +}
> +
> +long tar_atosl() {
> +  long n = tar_atol(-1);
> +  if (tar_atosl_min) {
> +    __errno_location();
> +    return 0;
> +  }
> +  if (n > 0)
> +    return 0;
> +  return n;
> +}
> diff --git a/gcc/tree-vect-loop-manip.cc b/gcc/tree-vect-loop-manip.cc
> index 
> 1066ea17c5674e03412b3dcd8a62ddf4dd54cf31..3810983a80c8b989be9fd9a9993642069fd39b99
>  100644
> --- a/gcc/tree-vect-loop-manip.cc
> +++ b/gcc/tree-vect-loop-manip.cc
> @@ -1716,8 +1716,6 @@ slpeel_tree_duplicate_loop_to_edge_cfg (class loop 
> *loop, edge loop_exit,
>         /* Now link the alternative exits.  */
>         if (multiple_exits_p)
>           {
> -           set_immediate_dominator (CDI_DOMINATORS, new_preheader,
> -                                    main_loop_exit_block);
>             for (auto gsi_from = gsi_start_phis (loop->header),
>                  gsi_to = gsi_start_phis (new_preheader);
>                  !gsi_end_p (gsi_from) && !gsi_end_p (gsi_to);
> @@ -1751,12 +1749,26 @@ slpeel_tree_duplicate_loop_to_edge_cfg (class loop 
> *loop, edge loop_exit,
>  
>        /* Finally after wiring the new epilogue we need to update its main 
> exit
>        to the original function exit we recorded.  Other exits are already
> -      correct.  */
> +      correct.  Because of versioning, skip vectors and others we must update
> +      the dominators of every node reachable by the new exits.  */
>        if (multiple_exits_p)
>       {
>         update_loop = new_loop;
> -       for (edge e : get_loop_exit_edges (loop))
> -         doms.safe_push (e->dest);
> +       hash_set <basic_block> visited;
> +       auto_vec <edge> workset;
> +       edge ev;
> +       edge_iterator ei;
> +       workset.safe_splice (get_loop_exit_edges (loop));
> +       while (!workset.is_empty ())
> +         {
> +           auto bb = workset.pop ()->dest;
> +           if (visited.add (bb))
> +             continue;
> +           doms.safe_push (bb);
> +           FOR_EACH_EDGE (ev, ei, bb->succs)
> +             workset.safe_push (ev);
> +         }
> +       visited.empty ();
>         doms.safe_push (exit_dest);
>  
>         /* Likely a fall-through edge, so update if needed.  */
> 
> 
> 
> 
> 

-- 
Richard Biener <rguent...@suse.de>
SUSE Software Solutions Germany GmbH,
Frankenstrasse 146, 90461 Nuernberg, Germany;
GF: Ivo Totev, Andrew McDonald, Werner Knoblich; (HRB 36809, AG Nuernberg)

Reply via email to