https://gcc.gnu.org/g:5ae6df447e12cbdfca0c65fa6341e161046df6b7
commit r17-6686-g5ae6df447e12cbdfca0c65fa6341e161046df6b7 Author: Richard Biener <[email protected]> Date: Tue Nov 24 18:37:08 2026 +0100 tree-optimization/128923 - improve BB SLP discovery for to-be-split graphs The following extends what was done in r17-4410-gbca59752267990, delaying soft-fail of SLP discovery until we have to to avoid succeding for leafs. This allows to discover better split-points for groups like in the case for PR128923. I have opted to avoid operand swapping overhead since we cannot currently distinguish a soft-fail from a real fail. Ideally soft-fail would be only enforced at outermost level but that's a much larger change. PR tree-optimization/128923 * tree-vect-slp.cc (force_split_matches): Clear leading entries in matches[]. (vect_build_slp_tree_2): Delay soft-failing to leaf nodes. Avoid swapping operands on to-be soft-failed nodes and never build those from scalars. * g++.dg/vect/pr128923.cc: New testcase. * gcc.dg/vect/bb-slp-pr128923-2.c: Likewise. * gcc.dg/vect/bb-slp-pr128923-3.c: Likewise. Diff: --- gcc/testsuite/g++.dg/vect/pr128923.cc | 30 +++++++++++ gcc/testsuite/gcc.dg/vect/bb-slp-pr128923-2.c | 18 +++++++ gcc/testsuite/gcc.dg/vect/bb-slp-pr128923-3.c | 44 ++++++++++++++++ gcc/tree-vect-slp.cc | 74 ++++++++++++++++++++------- 4 files changed, 147 insertions(+), 19 deletions(-) diff --git a/gcc/testsuite/g++.dg/vect/pr128923.cc b/gcc/testsuite/g++.dg/vect/pr128923.cc new file mode 100644 index 000000000000..98f1154ec132 --- /dev/null +++ b/gcc/testsuite/g++.dg/vect/pr128923.cc @@ -0,0 +1,30 @@ +// { dg-do compile } +// { dg-require-effective-target c++11 } +// { dg-additional-options "-fdump-tree-slp-details" } + +struct Vec3 { float x, y, z; }; +struct Point { Vec3 coord; int id; }; +struct Box +{ + Vec3 min{ 3.4e38f, 3.4e38f, 3.4e38f }, max{ -3.4e38f, -3.4e38f, -3.4e38f }; + void include( const Vec3 & p ) + { + min.x = p.x < min.x ? p.x : min.x; max.x = p.x > max.x ? p.x : max.x; + min.y = p.y < min.y ? p.y : min.y; max.y = p.y > max.y ? p.y : max.y; + min.z = p.z < min.z ? p.z : min.z; max.z = p.z > max.z ? p.z : max.z; + } +}; + +Box boxOf( const Point * p, int n ) +{ + Box b; + for ( int i = 0; i < n; ++i ) + b.include( p[i].coord ); + return b; +} + +// We want to either not vectorize or arrange to vectorize both a pair +// of stores to min and max, specifically avoid a vector load from p +// that is used in scalar form in the other half of the stores. For this +// SLP discovery has to appropriately split up the 6-lane store group. +// { dg-final { scan-tree-dump-not "extracting lane for live stmt" "slp1" } } diff --git a/gcc/testsuite/gcc.dg/vect/bb-slp-pr128923-2.c b/gcc/testsuite/gcc.dg/vect/bb-slp-pr128923-2.c new file mode 100644 index 000000000000..42a77d85a424 --- /dev/null +++ b/gcc/testsuite/gcc.dg/vect/bb-slp-pr128923-2.c @@ -0,0 +1,18 @@ +/* { dg-do compile } */ +/* { dg-require-effective-target vect_float } */ +/* { dg-require-effective-target vect_double } */ + +void foo (float *__restrict__ out, double *__restrict__ a, + double *__restrict__ b0, double *__restrict__ b1, + double *__restrict__ b2, double *__restrict__ b3, + double *__restrict__ b4, double *__restrict__ b5) +{ + out[0] = (float)(a[0] + *b0); + out[1] = (float)(a[1] + *b1); + out[2] = (float)(a[2] + *b2); + out[3] = (float)(a[3] + *b3); + out[4] = (float)(a[4] + *b4); + out[5] = (float)(a[5] + *b5); +} + +/* { dg-final { scan-tree-dump "Splitting SLP group at stmt 4" "slp2" } } */ diff --git a/gcc/testsuite/gcc.dg/vect/bb-slp-pr128923-3.c b/gcc/testsuite/gcc.dg/vect/bb-slp-pr128923-3.c new file mode 100644 index 000000000000..d8106799d988 --- /dev/null +++ b/gcc/testsuite/gcc.dg/vect/bb-slp-pr128923-3.c @@ -0,0 +1,44 @@ +/* { dg-do compile } */ +/* { dg-require-effective-target vect_float } */ +/* { dg-require-effective-target vect_double } */ + +void foo1 (float *__restrict__ out, + double *__restrict__ b0, double *__restrict__ b1, + double *__restrict__ b2, double *__restrict__ b3, + double *__restrict__ b4, double *__restrict__ b5) +{ + out[0] = (float)*b0; + out[1] = (float)*b1; + out[2] = (float)*b2; + out[3] = (float)*b3; + out[4] = (float)*b4; + out[5] = (float)*b5; +} + +void foo2 (float *__restrict__ out, float *__restrict__ a, + float *__restrict__ b0, float *__restrict__ b1, + float *__restrict__ b2, float *__restrict__ b3, + float *__restrict__ b4, float *__restrict__ b5) +{ + out[0] = *b0 + a[0]; + out[1] = *b1 + a[1]; + out[2] = *b2 + a[2]; + out[3] = *b3 + a[3]; + out[4] = *b4 + a[4]; + out[5] = *b5 + a[5]; +} + +void foo3 (float *__restrict__ out, + float *__restrict__ b0, float *__restrict__ b1, + float *__restrict__ b2, float *__restrict__ b3, + float *__restrict__ b4, float *__restrict__ b5) +{ + out[0] = *b0; + out[1] = *b1; + out[2] = *b2; + out[3] = *b3; + out[4] = *b4; + out[5] = *b5; +} + +/* { dg-final { scan-tree-dump-times "Splitting SLP group at stmt 4" 3 "slp2" } } */ diff --git a/gcc/tree-vect-slp.cc b/gcc/tree-vect-slp.cc index 6fbbc91418eb..c67314a3c053 100644 --- a/gcc/tree-vect-slp.cc +++ b/gcc/tree-vect-slp.cc @@ -2116,6 +2116,8 @@ force_split_matches (match_elt_t *matches, unsigned group_size, /* With constant vector elements simulate a mismatch at the point we need to split. But indicate the tail is isomorphic. */ unsigned tail = group_size & (const_nunits - 1); + for (unsigned i = 0; i < group_size - tail; ++i) + matches[i] = 0; for (unsigned i = group_size - tail; i < group_size; ++i) matches[i] = (int)(group_size - tail); } @@ -2834,20 +2836,19 @@ out: } swap = NULL; - /* Perform delayed soft-failing only here so we can factor in mismatches - determined by vect_get_and_check_slp_defs. */ - if (soft_fail) - { - force_split_matches (matches, group_size, - TYPE_VECTOR_SUBPARTS (vectype).to_constant ()); - return NULL; - } + /* Instead of soft-failing here, continue building up the SLP + graph, failing only at leafs. Note as we don't know whether + we soft- or hard-failed children avoid any operand swapping + when we soft-failed. */ bool has_two_operators_perm = false; auto_vec<unsigned> two_op_perm_indices[2]; vec<stmt_vec_info> two_op_scalar_stmts[2] = {vNULL, vNULL}; - if (two_operators && oprnds_info.length () == 2 && group_size > 2) + if (!soft_fail + && two_operators + && oprnds_info.length () == 2 + && group_size > 2) { unsigned idx = 0; hash_map<gimple *, unsigned> seen; @@ -3044,6 +3045,13 @@ out: goto fail; } } + /* At leafs force soft-failing. */ + if (soft_fail) + { + force_split_matches (matches, group_size, + TYPE_VECTOR_SUBPARTS (vectype).to_constant ()); + goto fail; + } slp_tree invnode = vect_create_new_slp_node (oprnd_info->ops); SLP_TREE_DEF_TYPE (invnode) = oprnd_info->first_dt; oprnd_info->ops = vNULL; @@ -3116,7 +3124,8 @@ out: def_stmts2.release (); } - can_swap = (i == 0 + can_swap = (!soft_fail + && i == 0 && (nops == 2 || nops == 3) && oprnds_info.length () > 1 && oprnds_info[1]->first_dt == vect_internal_def @@ -3235,7 +3244,8 @@ fail: /* If the SLP build failed and we analyze a basic-block simply treat nodes we fail to build as externally defined - (and thus build vectors from the scalar defs). + (and thus build vectors from the scalar defs) when not + soft-failing. The cost model will reject outright expensive cases. ??? This doesn't treat cases where permutation ultimatively fails (or we don't try permutation below). Ideally we'd @@ -3267,14 +3277,28 @@ fail: if (!known_ge (j, TYPE_VECTOR_SUBPARTS (vectype)) && vect_slp_can_convert_to_external (oprnd_info->def_stmts)) { - if (dump_enabled_p ()) - dump_printf_loc (MSG_NOTE, vect_location, - "Building vector operands from scalars\n"); - this_tree_size++; - child = vect_create_new_slp_node (oprnd_info->ops); - children.safe_push (child); - oprnd_info->ops = vNULL; - continue; + /* When we'd soft-fail we do not want to build leaf nodes, + we also do not want to use the matches[] from a fatal + failure to guide splitting if we would build from scalars, so + indicate the soft-fail split point here in that case. */ + if (soft_fail) + { + if (matches[0] == -1) + force_split_matches (matches, group_size, + TYPE_VECTOR_SUBPARTS + (vectype).to_constant ()); + } + else + { + if (dump_enabled_p ()) + dump_printf_loc (MSG_NOTE, vect_location, + "Building vector operands from scalars\n"); + this_tree_size++; + child = vect_create_new_slp_node (oprnd_info->ops); + children.safe_push (child); + oprnd_info->ops = vNULL; + continue; + } } } @@ -3288,6 +3312,18 @@ fail: vect_free_oprnd_info (oprnds_info); + /* Conversions might make children built successfully but we still + have to soft-fail the parent in that case. */ + if (soft_fail) + { + force_split_matches (matches, group_size, + TYPE_VECTOR_SUBPARTS (vectype).to_constant ()); + for (slp_tree child : children) + if (child) + vect_free_slp_tree (child); + return NULL; + } + /* If we have all children of a child built up from uniform scalars or does more than one possibly expensive vector construction then just throw that away, causing it built up from scalars.
