PR128739 exposes a mismatch between the induction and reduction values passed
to the scalar epilogue of a peeled early-break loop. For example:
unsigned
rd (const unsigned char **pp, const unsigned char *lim)
{
const unsigned char *p = *pp;
unsigned s = *p++;
unsigned len = s;
while (s == 255)
{
if (p >= lim)
return 0;
s = *p++;
len += s;
}
*pp = p;
return len;
}
vect_do_peeling already calls vect_update_ivs_after_vectorizer to initialize
the scalar IVs from the completed-iteration count. However later,
vect_set_loop_condition moves that exit to the latch and overwrites this
constraint. Essentially we have:
header:
acc = PHI <initial, acc_next>;
body:
acc_next = acc + values;
latch:
if (finished) goto exit;
exit:
out = PHI <acc(latch)>;
To fix this populate the redirect map with each header PHI's backedge value
before
copying the exit arguments. The copied PHI then becomes:
out = PHI <acc_next(latch)>;
Sashiko Review:
https://gcc.sashiko.dev/#/patchset/patch-21107-rb-rev-1-tamar%40arm.com
Apply Series: b4 shazam
https://inbox.sourceware.org/gcc-patches/[email protected]/
Thanks,
Tamar
gcc/ChangeLog:
PR tree-optimization/128739
* tree-vect-loop-manip.cc (vect_set_loop_condition): Use backedge
values of header PHIs when copying exit PHI arguments to the latch
exit of peeled early-break loops.
gcc/testsuite/ChangeLog:
PR tree-optimization/128739
* gcc.dg/vect/vect-early-break_151-pr128739.c: New test.
base-commit: 303400bda9e33622ab24ff3c1f82866577fc7351
---
--
diff --git a/gcc/testsuite/gcc.dg/vect/vect-early-break_151-pr128739.c
b/gcc/testsuite/gcc.dg/vect/vect-early-break_151-pr128739.c
new file mode 100644
index
0000000000000000000000000000000000000000..a68dcfb3a80a1abee5b277f361df987562e12728
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/vect-early-break_151-pr128739.c
@@ -0,0 +1,51 @@
+/* PR tree-optimization/128739 */
+/* { dg-do run } */
+/* { dg-add-options vect_early_break } */
+/* { dg-require-effective-target vect_early_break_hw } */
+/* { dg-require-effective-target vect_int } */
+/* { dg-additional-options "-O3" } */
+
+#include "tree-vect.h"
+
+static unsigned char buf[256];
+
+/* Allow constant propagation of LIM to expose the counted loop exit. */
+__attribute__ ((noinline)) static unsigned
+rd (const unsigned char **pp, const unsigned char *lim)
+{
+ const unsigned char *p = *pp;
+ unsigned int s = *p++;
+ unsigned int len = s;
+
+ while (s == 255)
+ {
+ if (p >= lim)
+ return 0;
+
+ s = *p++;
+ len += s;
+ }
+
+ *pp = p;
+ return len;
+}
+
+int
+main (void)
+{
+ check_vect ();
+
+#pragma GCC novector
+ for (int i = 0; i < 100; i++)
+ buf[31 + i] = 255;
+
+ buf[131] = 7;
+
+ const unsigned char *p = buf + 31;
+ if (rd (&p, buf + 132) != 255 * 100 + 7 || p != buf + 132)
+ __builtin_abort ();
+
+ return 0;
+}
+
+/* { dg-final { scan-tree-dump "LOOP VECTORIZED" "vect" { target aarch64*-*-*
} } } */
diff --git a/gcc/tree-vect-loop-manip.cc b/gcc/tree-vect-loop-manip.cc
index
51c8b42deff08be6951534941fd6afcb0076b52f..35884554ab4bc58a30e63044fa81e471a88365bf
100644
--- a/gcc/tree-vect-loop-manip.cc
+++ b/gcc/tree-vect-loop-manip.cc
@@ -1468,7 +1468,25 @@ vect_set_loop_condition (class loop *loop, edge loop_e,
loop_vec_info loop_vinfo
edge latch_exit_edge = make_edge (latch, loop_e->dest, exit_flags);
latch_exit_edge->probability = loop_e->probability;
latch_exit_edge->count () = loop_e->count ();
- copy_phi_arg_into_existing_phi (loop_e, latch_exit_edge);
+
+ /* The header exit uses values from before the iteration. The latch
+ exit follows the body, so use the backedge values of header PHIs. The
+ earlier code in vect_do_peeling corrected the scalar IVs, but the line
+ below still copies the old reduction values to the new exit. Correct
+ these by populating the redirect map with the backedge values before
+ copying the PHIs. */
+ for (gphi_iterator gsi = gsi_start_phis (loop->header);
+ !gsi_end_p (gsi); gsi_next (&gsi))
+ {
+ gphi *phi = gsi.phi ();
+ auto locus = gimple_phi_arg_location_from_edge (phi, latch_e);
+ redirect_edge_var_map_add (latch_exit_edge, gimple_phi_result (phi),
+ PHI_ARG_DEF_FROM_EDGE (phi, latch_e),
+ locus);
+ }
+
+ copy_phi_arg_into_existing_phi (loop_e, latch_exit_edge, true);
+
if (gphi *vphi = get_virtual_phi (latch_exit_edge->dest))
SET_PHI_ARG_DEF_ON_EDGE (vphi, latch_exit_edge,
get_live_virtual_operand_on_edge
@@ -3898,7 +3916,7 @@ vect_do_peeling (loop_vec_info loop_vinfo, tree niters,
tree nitersm1,
}
vect_update_ivs_after_vectorizer (loop_vinfo, vector_iters_vf,
- update_e, uncounted_p);
+ update_e, uncounted_p);
/* Recalculate the dominators after adding the guard edge. */
if (LOOP_VINFO_EARLY_BREAKS (loop_vinfo))