On Wed, 18 Nov 2026, Avinash Jayakar wrote:

> From: Avinash Jayakar <[email protected]>
> 
> Hi,
> 
> This patch partially fixes the issue mentioned in PR117379. It handles the
> case where we miss the reduction vectorization of 2 different contiguous 
> memory
> blocks, but does not handle the mixed operations.
> 
> Bootstrapped and regtested on (powerpc64le, x86, aarch64)-linux-gnu. Please 
> let
> me know if this approach is ok.

It looks way more complicated than I suggested (I hope so..).  I had
expected to see a special case for N (or just two ...) isomorphic
groups of the same size, handling them as a single SLP instance
that merges the group root vectors by means of additional SLP
nodes with the reduction operation (the PLUS), leaving a single
root as expected by the BB reduction epilog code generation.
That would also extend to the case of c += a[0];... c -= b[0]; ...
where the vector merging could then be a MINUS.  So no need
for slp_inst_kind_bb_reduc_part instances.

Richard.

> Thanks,
> Avinash Jayakar
> 
> BB SLP reduction discovery picks the largest set of isomorphic lanes and
> leaves the remaining matching groups as scalar remains, even when those
> groups have the same shape as the primary group.  For code such as
> 
>   c += a[0]; ... c += a[7];
>   c += b[0]; ... c += b[7];
> 
> the 16-lane reduction group fails to build because the loads from 'a'
> and 'b' form different interleaving chains, so only the 'a' lanes end up
> vectorized while all 'b' lanes stay scalar.
> 
> This patch vectorizes such secondary matching groups as
> slp_inst_kind_bb_reduc_part instances linked to a primary instance:
> 
> - vect_build_slp_instance is split into a worker returning the new
>   instance and a wrapper that collects the leftover matching groups,
>   builds part instances for them and links them to the primary.
> 
> - Part instances are costed together with the primary and the primary's
>   root stmt handler is deferred until all trees in the subgraph are
>   scheduled, so that it can combine the part vector defs.  Parts that
>   end up not being vectorized are reduced from their scalar lanes.
> 
> - The epilogue of a bb_reduc primary combines the scalar results of all
>   parts, plus scalar lanes, into the reduction.
> 
> - Removal paths unlink instances and fold parts that cannot be
>   vectorized back into the primary's scalar remains.
> 
> 2026-11-18  Avinash Jayakar  <[email protected]>
> 
> gcc/ChangeLog:
>       PR tree-optimization/117379
>       * tree-vectorizer.h (enum slp_instance_kind): Add
>       slp_inst_kind_bb_reduc_part.
>       (struct _slp_instance): Add reduc_parts and reduc_primary.
>       * tree-vect-slp.cc (vect_free_slp_instance): Release reduc_parts.
>       (vect_unlink_slp_instance): New.
>       (vect_build_slp_instance_1): New worker, formerly
>       vect_build_slp_instance.
>       (vect_build_slp_instance): New wrapper creating part instances.
>       (vect_slp_analyze_operations): Validate part epilogues and unlink
>       instances when removing them.
>       (vect_slp_analyze_bb_1): Unlink instances when removing them.
>       (vect_slp_region): Cost and schedule parts together with their
>       primary.
>       (vectorize_slp_instance_root_stmt): Combine the results of all
>       parts, falling back to scalar lanes for parts that were not
>       vectorized.
>       (vect_schedule_slp): Add DEFER_BB_REDUC_PARTS and emit the deferred
>       root stmt handlers after all trees are scheduled; unlink instances
>       when removing them.
> 
> gcc/testsuite/ChangeLog:
>       PR tree-optimization/117379
>       * gcc.dg/vect/bb-reduc-parts-mobility.c: New test.
> ---
>  .../gcc.dg/vect/bb-reduc-parts-mobility.c     |  51 ++
>  gcc/tree-vect-slp.cc                          | 457 ++++++++++++++----
>  gcc/tree-vectorizer.h                         |  12 +-
>  3 files changed, 433 insertions(+), 87 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.dg/vect/bb-reduc-parts-mobility.c
> 
> diff --git a/gcc/testsuite/gcc.dg/vect/bb-reduc-parts-mobility.c 
> b/gcc/testsuite/gcc.dg/vect/bb-reduc-parts-mobility.c
> new file mode 100644
> index 00000000000..257d066625b
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/vect/bb-reduc-parts-mobility.c
> @@ -0,0 +1,51 @@
> +/* Test that a SLP BB reduction with two equal-shape groups vectorizes
> +   both groups, using a primary plus a part instance.  */
> +/* { dg-do run } */
> +/* { dg-require-effective-target vect_int } */
> +/* { dg-options "-O2 -fdump-tree-slp-details" } */
> +
> +#include "tree-vect.h"
> +
> +typedef unsigned short T;
> +
> +T __attribute__ ((noinline))
> +mobility (T *a, T *b)
> +{
> +  T c = 0;
> +  c += a[0];
> +  c += a[1];
> +  c += a[2];
> +  c += a[3];
> +  c += a[4];
> +  c += a[5];
> +  c += a[6];
> +  c += a[7];
> +
> +  c += b[0];
> +  c += b[1];
> +  c += b[2];
> +  c += b[3];
> +  c += b[4];
> +  c += b[5];
> +  c += b[6];
> +  c += b[7];
> +  return c;
> +}
> +
> +int
> +main (void)
> +{
> +  check_vect ();
> +
> +  T a[8] = {1, 2, 3, 4, 5, 6, 7, 8};
> +  T b[8] = {10, 20, 30, 40, 50, 60, 70, 80};
> +  T result = mobility (a, b);
> +  T expected = 0;
> +  for (int i = 0; i < 8; i++)
> +    expected += a[i] + b[i];
> +  if (result != expected)
> +    __builtin_abort ();
> +  return 0;
> +}
> +
> +/* { dg-final { scan-tree-dump-times "BB reduction part instance" 1 "slp2" { 
> target vect_int } } } */
> diff --git a/gcc/tree-vect-slp.cc b/gcc/tree-vect-slp.cc
> index 87a235f28b1..52029a7ea3a 100644
> --- a/gcc/tree-vect-slp.cc
> +++ b/gcc/tree-vect-slp.cc
> @@ -215,9 +215,49 @@ vect_free_slp_instance (slp_instance instance)
>    SLP_INSTANCE_REMAIN_DEFS (instance).release ();
>    instance->subgraph_entries.release ();
>    instance->cost_vec.release ();
> +  /* The parts are owned by vinfo->slp_instances, only release the vector.  
> */
> +  instance->reduc_parts.release ();
>    free (instance);
>  }
>  
> +/* Unlink INSTANCE from the BB reduction primary/part relationship before
> +   it is freed.  When FOLD_LANES is true and INSTANCE is a part, fold its
> +   scalar lanes back into the primary's remain_defs so the epilogue still
> +   reduces them.  When INSTANCE is a primary its parts are orphaned and
> +   will be removed when they are encountered.  */
> +
> +static void
> +vect_unlink_slp_instance (slp_instance instance, bool fold_lanes)
> +{
> +  if (SLP_INSTANCE_KIND (instance) == slp_inst_kind_bb_reduc_part)
> +    {
> +      slp_instance primary = instance->reduc_primary;
> +      if (!primary)
> +     return;
> +      if (fold_lanes)
> +     {
> +       slp_tree part_root = SLP_INSTANCE_TREE (instance);
> +       for (stmt_vec_info stmt_info : SLP_TREE_SCALAR_STMTS (part_root))
> +         if (stmt_info)
> +           primary->remain_defs.safe_push
> +             (gimple_get_lhs (vect_orig_stmt (stmt_info)->stmt));
> +     }
> +      /* Unlink from the primary.  */
> +      for (unsigned i = 0; i < primary->reduc_parts.length (); ++i)
> +     if (primary->reduc_parts[i] == instance)
> +       {
> +         primary->reduc_parts.ordered_remove (i);
> +         break;
> +       }
> +      instance->reduc_primary = NULL;
> +    }
> +  else if (SLP_INSTANCE_KIND (instance) == slp_inst_kind_bb_reduc)
> +    {
> +      for (slp_instance part : instance->reduc_parts)
> +     part->reduc_primary = NULL;
> +    }
> +}
> +
>  
>  /* Create a SLP node with NOPS children with CODE, either VEC_PERM_EXPR
>     for a permute node or else ERROR_MARK.  */
> @@ -4294,25 +4334,29 @@ vect_build_slp_store_interleaving (vec<slp_tree> 
> &rhs_nodes,
>  }
>  
>  /* Analyze an SLP instance starting from SCALAR_STMTS which are a group
> -   of KIND.  Return true if successful.  SCALAR_STMTS is owned by this
> -   function, REMAIN and ROOT_STMT_INFOS ownership is transferred back to
> -   the caller upon failure.  */
> +   of KIND.  Return the new instance on success, NULL on failure.
> +   SCALAR_STMTS is owned by this function, REMAIN and ROOT_STMT_INFOS
> +   ownership is transferred back to the caller upon failure.
> +   On success the instance is not yet added to vinfo->slp_instances.
> +   When OTHER_GROUPS is non-NULL and the bb_reduc split detects additional
> +   matching groups of size >= 2 beyond the primary, they are collected
> +   there (each entry is a fresh stmt vector owned by the caller).  */
>  
> -static bool
> -vect_build_slp_instance (vec_info *vinfo,
> -                      slp_instance_kind kind,
> -                      vec<stmt_vec_info> &scalar_stmts,
> -                      vec<stmt_vec_info> &root_stmt_infos,
> -                      vec<tree> &remain,
> -                      unsigned max_tree_size, unsigned *limit,
> -                      scalar_stmts_to_slp_tree_map_t *bst_map,
> -                      bool force_single_lane)
> +static slp_instance
> +vect_build_slp_instance_1 (vec_info *vinfo, slp_instance_kind kind,
> +                        vec<stmt_vec_info> &scalar_stmts,
> +                        vec<stmt_vec_info> &root_stmt_infos,
> +                        vec<tree> &remain, unsigned max_tree_size,
> +                        unsigned *limit,
> +                        scalar_stmts_to_slp_tree_map_t *bst_map,
> +                        bool force_single_lane,
> +                        vec<vec<stmt_vec_info>> *other_groups = NULL)
>  {
>    /* If there's no budget left bail out early.  */
>    if (*limit == 0)
>      {
>        scalar_stmts.release ();
> -      return false;
> +      return NULL;
>      }
>  
>    if (kind == slp_inst_kind_ctor)
> @@ -4372,14 +4416,14 @@ vect_build_slp_instance (vec_info *vinfo,
>        new_instance->reduc_phis = NULL;
>        new_instance->cost_vec = vNULL;
>        new_instance->subgraph_entries = vNULL;
> +      new_instance->reduc_parts = vNULL;
> +      new_instance->reduc_primary = NULL;
>  
>        if (dump_enabled_p ())
>       dump_printf_loc (MSG_NOTE, vect_location,
>                        "SLP size %u vs. limit %u.\n",
>                        tree_size, max_tree_size);
>  
> -      vinfo->slp_instances.safe_push (new_instance);
> -
>        /* ???  We've replaced the old SLP_INSTANCE_GROUP_SIZE with
>        the number of SLP lanes of the root in a few places.
>        Verify that assumption holds.  */
> @@ -4399,7 +4443,7 @@ vect_build_slp_instance (vec_info *vinfo,
>                               SLP_INSTANCE_TREE (new_instance));
>       }
>  
> -      return true;
> +      return new_instance;
>      }
>    /* Failed to SLP.  */
>  
> @@ -4414,8 +4458,11 @@ vect_build_slp_instance (vec_info *vinfo,
>       otherwise we get the non-power-of-two tail of the lanes failed.
>       For BB reductions we mainly want to catch the first case so we pick
>       a more useful subset of lanes to reduce.  Pick the largest matching
> -     subset of that covers half of the group or more.  */
> -  if (kind == slp_inst_kind_bb_reduc && matches[0] != -1)
> +     subset of that covers half of the group or more.  Additional
> +     matching groups become parts vectorized separately.  */
> +  if ((kind == slp_inst_kind_bb_reduc
> +       || kind == slp_inst_kind_bb_reduc_part)
> +      && matches[0] != -1)
>      {
>        unsigned *n_matching = XALLOCAVEC (unsigned, group_size);
>        memset (n_matching, 0, sizeof (unsigned) * group_size);
> @@ -4426,31 +4473,66 @@ vect_build_slp_instance (vec_info *vinfo,
>        for (unsigned i = 1; i < group_size; ++i)
>       if (n_matching[i] > n_matching[largest_i])
>         largest_i = i;
> -      /* Pick the largest matching part and put the rest to remain.  */
> -      if (n_matching[largest_i] >= 2
> -       && n_matching[largest_i] >= group_size / 2)
> +      /* The primary group must have >= 2 lanes and cover >= half the group. 
>  */
> +      if (n_matching[largest_i] >= 2)
>       {
>         /* As we know the matches[] stmts match up, recursing for
>            non-power-of-two sizes will just force-fail the tail
> -          for us at hopefully optimal vector size and succesfully
> +          for us at hopefully optimal vector size and successfully
>            finish discovery.  */
>         vec<stmt_vec_info> scalar_stmts2;
>         scalar_stmts2.create (n_matching[largest_i]);
> -       for (unsigned i = 0; i < group_size; ++i)
> -         if (matches[i] == (int)largest_i)
> -           scalar_stmts2.quick_push (scalar_stmts[i]);
> -         else
> -           remain.safe_push
> -             (gimple_get_lhs (vect_orig_stmt (scalar_stmts[i])->stmt));
> +       unsigned n_scalar_remains = 0;
> +       unsigned n_other_groups = 0;
> +       if (other_groups)
> +         {
> +           /* Collect the remaining groups by their leader lane.  */
> +           auto_vec<vec<stmt_vec_info>> by_leader;
> +           by_leader.safe_grow_cleared (group_size);
> +           for (unsigned i = 0; i < group_size; ++i)
> +             {
> +               if (matches[i] == (int) largest_i)
> +                 scalar_stmts2.quick_push (scalar_stmts[i]);
> +               else if (matches[i] != -2 && matches[i] != -1
> +                        && n_matching[matches[i].v] >= 2)
> +                 by_leader[matches[i].v].safe_push (scalar_stmts[i]);
> +               else
> +                 {
> +                   remain.safe_push
> +                     (gimple_get_lhs (vect_orig_stmt 
> (scalar_stmts[i])->stmt));
> +                   n_scalar_remains++;
> +                 }
> +             }
> +           for (unsigned i = 0; i < group_size; ++i)
> +             if (by_leader[i].is_empty ())
> +               by_leader[i].release ();
> +             else
> +               {
> +                 other_groups->safe_push (by_leader[i]);
> +                 n_other_groups++;
> +               }
> +         }
> +       else
> +         for (unsigned i = 0; i < group_size; ++i)
> +           if (matches[i] == (int) largest_i)
> +             scalar_stmts2.quick_push (scalar_stmts[i]);
> +           else
> +             {
> +               remain.safe_push
> +                 (gimple_get_lhs (vect_orig_stmt (scalar_stmts[i])->stmt));
> +               n_scalar_remains++;
> +             }
>         if (dump_enabled_p ())
> -         dump_printf_loc (MSG_NOTE, vect_location, "Splitting %d "
> -                          "non-matching lanes to scalar remains\n",
> -                          scalar_stmts.length () - scalar_stmts2.length ());
> +         dump_printf_loc (MSG_NOTE, vect_location,
> +                          "Splitting %u non-matching lanes to %u "
> +                          "part groups and %u scalar remains\n",
> +                          group_size - n_matching[largest_i],
> +                          n_other_groups, n_scalar_remains);
>         scalar_stmts.release ();
> -       return vect_build_slp_instance (vinfo, kind, scalar_stmts2,
> -                                       root_stmt_infos, remain,
> -                                       max_tree_size, limit, bst_map,
> -                                       force_single_lane);
> +       return vect_build_slp_instance_1 (vinfo, kind, scalar_stmts2,
> +                                         root_stmt_infos, remain,
> +                                         max_tree_size, limit, bst_map,
> +                                         force_single_lane);
>       }
>      }
>  
> @@ -4460,7 +4542,94 @@ vect_build_slp_instance (vec_info *vinfo,
>    /* Failed to SLP.  */
>    if (dump_enabled_p ())
>      dump_printf_loc (MSG_NOTE, vect_location, "SLP discovery failed\n");
> -  return false;
> +  return NULL;
> +}
> +
> +/* Analyze an SLP instance starting from SCALAR_STMTS which are a group
> +   of KIND.  Return true if successful.  SCALAR_STMTS is owned by this
> +   function, REMAIN and ROOT_STMT_INFOS ownership is transferred back to
> +   the caller upon failure.
> +   For slp_inst_kind_bb_reduc the matching groups of lanes that do not
> +   belong to the primary group are vectorized as
> +   slp_inst_kind_bb_reduc_part instances linked to the primary.  */
> +
> +static bool
> +vect_build_slp_instance (vec_info *vinfo,
> +                      slp_instance_kind kind,
> +                      vec<stmt_vec_info> &scalar_stmts,
> +                      vec<stmt_vec_info> &root_stmt_infos,
> +                      vec<tree> &remain,
> +                      unsigned max_tree_size, unsigned *limit,
> +                      scalar_stmts_to_slp_tree_map_t *bst_map,
> +                      bool force_single_lane)
> +{
> +  auto_vec<vec<stmt_vec_info>> other_groups;
> +  vec<vec<stmt_vec_info>> *other_groups_p
> +    = kind == slp_inst_kind_bb_reduc ? &other_groups : NULL;
> +
> +  slp_instance primary = vect_build_slp_instance_1 (vinfo, kind, 
> scalar_stmts,
> +                                                  root_stmt_infos, remain,
> +                                                  max_tree_size, limit,
> +                                                  bst_map, force_single_lane,
> +                                                  other_groups_p);
> +  if (!primary)
> +    {
> +      release_vec_vec (other_groups);
> +      return false;
> +    }
> +
> +  vinfo->slp_instances.safe_push (primary);
> +
> +  for (vec<stmt_vec_info> &part_stmts : other_groups)
> +    {
> +      /* Record the defs of the group before _1 possibly releases it.  */
> +      auto_vec<tree> part_lhs_defs (part_stmts.length ());
> +      for (stmt_vec_info stmt_info : part_stmts)
> +     part_lhs_defs.quick_push
> +       (gimple_get_lhs (vect_orig_stmt (stmt_info)->stmt));
> +
> +      vec<stmt_vec_info> part_roots = vNULL;
> +      /* Lanes that _1 splits off from the part again stay scalar.  */
> +      vec<tree> part_remain = vNULL;
> +      slp_instance part
> +     = vect_build_slp_instance_1 (vinfo, slp_inst_kind_bb_reduc_part,
> +                                  part_stmts, part_roots, part_remain,
> +                                  max_tree_size, limit, bst_map, false);
> +      if (part)
> +     {
> +       part->reduc_primary = primary;
> +       primary->reduc_parts.safe_push (part);
> +       vinfo->slp_instances.safe_push (part);
> +       /* The part does not use its own remain_defs, hand those lanes
> +          to the primary's epilogue and detach the vector.  */
> +       for (tree lhs : part_remain)
> +         primary->remain_defs.safe_push (lhs);
> +       part->remain_defs = vNULL;
> +       if (dump_enabled_p ())
> +         dump_printf_loc (MSG_NOTE, vect_location,
> +                          "BB reduction part instance %p covering %u "
> +                          "lanes linked to primary %p\n",
> +                          (void *) part, part_lhs_defs.length (),
> +                          (void *) primary);
> +     }
> +      else
> +     {
> +       /* The part cannot be vectorized, fold all its lanes into the
> +          primary's remain_defs.  part_remain may already contain a
> +          subset of them, release it to avoid double-counting.  */
> +       part_remain.release ();
> +       for (tree lhs : part_lhs_defs)
> +         primary->remain_defs.safe_push (lhs);
> +       if (dump_enabled_p ())
> +         dump_printf_loc (MSG_NOTE, vect_location,
> +                          "BB reduction part not vectorized, folding %u "
> +                          "lanes into primary remains\n",
> +                          part_lhs_defs.length ());
> +     }
> +      part_remain.release ();
> +    }
> +
> +  return true;
>  }
>  
>  /* Analyze an SLP instance starting from a the start of a reduction chain.
> @@ -4733,6 +4902,8 @@ vect_analyze_slp_reduc_chain (loop_vec_info vinfo,
>        new_instance->reduc_phis = NULL;
>        new_instance->cost_vec = vNULL;
>        new_instance->subgraph_entries = vNULL;
> +      new_instance->reduc_parts = vNULL;
> +      new_instance->reduc_primary = NULL;
>  
>        vinfo->slp_instances.safe_push (new_instance);
>  
> @@ -4797,6 +4968,8 @@ vect_analyze_slp_reduc_chain (loop_vec_info vinfo,
>        new_instance->reduc_phis = NULL;
>        new_instance->cost_vec = vNULL;
>        new_instance->subgraph_entries = vNULL;
> +      new_instance->reduc_parts = vNULL;
> +      new_instance->reduc_primary = NULL;
>  
>        vect_reduc_info reduc_info = info_for_reduction (vinfo, node);
>        reduc_info->is_reduc_chain = true;
> @@ -4935,6 +5108,8 @@ vect_analyze_slp_reduction (loop_vec_info vinfo,
>        new_instance->reduc_phis = NULL;
>        new_instance->cost_vec = vNULL;
>        new_instance->subgraph_entries = vNULL;
> +      new_instance->reduc_parts = vNULL;
> +      new_instance->reduc_primary = NULL;
>  
>        if (dump_enabled_p ())
>       dump_printf_loc (MSG_NOTE, vect_location,
> @@ -5001,6 +5176,8 @@ vect_analyze_slp_reduction_group (loop_vec_info 
> loop_vinfo,
>        new_instance->reduc_phis = NULL;
>        new_instance->cost_vec = vNULL;
>        new_instance->subgraph_entries = vNULL;
> +      new_instance->reduc_parts = vNULL;
> +      new_instance->reduc_primary = NULL;
>  
>        if (dump_enabled_p ())
>       dump_printf_loc (MSG_NOTE, vect_location,
> @@ -5235,6 +5412,8 @@ vect_analyze_slp_instance (vec_info *vinfo,
>        new_instance->reduc_phis = NULL;
>        new_instance->cost_vec = vNULL;
>        new_instance->subgraph_entries = vNULL;
> +      new_instance->reduc_parts = vNULL;
> +      new_instance->reduc_primary = NULL;
>  
>        if (dump_enabled_p ())
>       dump_printf_loc (MSG_NOTE, vect_location,
> @@ -5475,6 +5654,8 @@ vect_analyze_slp_instance (vec_info *vinfo,
>         new_instance->reduc_phis = NULL;
>         new_instance->cost_vec = vNULL;
>         new_instance->subgraph_entries = vNULL;
> +       new_instance->reduc_parts = vNULL;
> +       new_instance->reduc_primary = NULL;
>  
>         if (dump_enabled_p ())
>           dump_printf_loc (MSG_NOTE, vect_location,
> @@ -9546,6 +9727,23 @@ vect_slp_analyze_operations (vec_info *vinfo)
>        cost_vec.create (2);
>        if (is_a <bb_vec_info> (vinfo))
>       vect_location = instance->location ();
> +      /* A bb_reduc part has no root stmts on its own, validate its epilogue
> +      using the primary's root stmt.  */
> +      bool part_epilogue_ok = true;
> +      if (SLP_INSTANCE_KIND (instance) == slp_inst_kind_bb_reduc_part)
> +     {
> +       slp_instance primary = instance->reduc_primary;
> +       if (!primary)
> +         /* The primary was removed, this orphaned part has to go, too.  */
> +         part_epilogue_ok = false;
> +       else
> +         {
> +           SLP_INSTANCE_ROOT_STMTS (instance) = primary->root_stmts;
> +           part_epilogue_ok
> +             = vectorizable_bb_reduc_epilogue (instance, &cost_vec);
> +           SLP_INSTANCE_ROOT_STMTS (instance) = vNULL;
> +         }
> +     }
>        if (!vect_slp_analyze_node_operations (vinfo,
>                                            SLP_INSTANCE_TREE (instance),
>                                            instance, visited, visited_vec,
> @@ -9563,6 +9761,9 @@ vect_slp_analyze_operations (vec_info *vinfo)
>         /* Check we can vectorize the reduction.  */
>         || (SLP_INSTANCE_KIND (instance) == slp_inst_kind_bb_reduc
>             && !vectorizable_bb_reduc_epilogue (instance, &cost_vec))
> +       /* Check we can vectorize the reduction parts epilogue.  */
> +       || (SLP_INSTANCE_KIND (instance) == slp_inst_kind_bb_reduc_part
> +           && !part_epilogue_ok)
>         /* Check we can vectorize the gcond.  */
>         || (SLP_INSTANCE_KIND (instance) == slp_inst_kind_gcond
>             && !vectorizable_early_exit (as_a <loop_vec_info> (vinfo),
> @@ -9604,6 +9805,10 @@ vect_slp_analyze_operations (vec_info *vinfo)
>               }
>             visited.remove (node);
>           }
> +       /* Unlink from the primary/parts relation.  A removed part folds
> +          its lanes into the primary's remains, a removed primary orphans
> +          its parts and those are removed when encountered.  */
> +       vect_unlink_slp_instance (instance, true);
>         vect_free_slp_instance (instance);
>            vinfo->slp_instances.ordered_remove (i);
>       }
> @@ -9643,6 +9848,7 @@ vect_slp_analyze_operations (vec_info *vinfo)
>             dump_printf_loc (MSG_NOTE, vect_location,
>                              "removing SLP instance operations starting "
>                              "from: %G", root->stmt);
> +         vect_unlink_slp_instance (instance, false);
>           vect_free_slp_instance (instance);
>           vinfo->slp_instances.ordered_remove (i);
>         }
> @@ -10505,6 +10711,7 @@ vect_slp_analyze_bb_1 (bb_vec_info bb_vinfo, int 
> n_stmts, bool &fatal,
>           dump_printf_loc (MSG_NOTE, vect_location,
>                            "removing SLP instance operations starting from: 
> %G",
>                            stmt_info->stmt);
> +       vect_unlink_slp_instance (instance, true);
>         vect_free_slp_instance (instance);
>         BB_VINFO_SLP_INSTANCES (bb_vinfo).ordered_remove (i);
>         continue;
> @@ -10599,12 +10806,22 @@ vect_slp_region (vec<basic_block> bbs, 
> vec<data_reference_p> datarefs,
>             if (instance->subgraph_entries.is_empty ())
>               continue;
>  
> +           /* Parts of a BB reduction are costed and scheduled together
> +              with their primary.  */
> +           if (SLP_INSTANCE_KIND (instance) == slp_inst_kind_bb_reduc_part)
> +             continue;
> +
> +           auto_vec<slp_instance> subgraphs;
> +           subgraphs.safe_splice (instance->subgraph_entries);
> +           for (slp_instance part : instance->reduc_parts)
> +             subgraphs.safe_splice (part->subgraph_entries);
> +
>             dump_user_location_t saved_vect_location = vect_location;
>             vect_location = instance->location ();
>             if (!unlimited_cost_model (NULL)
>                 && !param_vect_allow_possibly_not_worthwhile_vectorizations
>                 && !vect_bb_vectorization_profitable_p
> -                     (bb_vinfo, instance->subgraph_entries, orig_loop))
> +                     (bb_vinfo, subgraphs.to_vec_legacy (), orig_loop))
>               {
>                 if (dump_enabled_p ())
>                   dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
> @@ -10666,15 +10883,22 @@ vect_slp_region (vec<basic_block> bbs, 
> vec<data_reference_p> datarefs,
>                                "using SLP\n");
>             vectorized = true;
>  
> +           /* Parts are scheduled before their primary so that the
> +              primary root stmt handler can refer to their vector defs.  */
> +           auto_vec<slp_instance> subgraphs;
> +           for (slp_instance part : instance->reduc_parts)
> +             subgraphs.safe_splice (part->subgraph_entries);
> +           subgraphs.safe_splice (instance->subgraph_entries);
> +
>             /* Dump before scheduling as store vectorization will remove
>                the original stores and mess with the instance tree
>                so querying its location will eventually ICE.  */
>             if (flag_checking)
> -             for (slp_instance sub : instance->subgraph_entries)
> +             for (slp_instance sub : subgraphs)
>                 gcc_assert (SLP_TREE_VECTYPE (SLP_INSTANCE_TREE (sub)));
>             unsigned HOST_WIDE_INT bytes;
>             if (dump_enabled_p ())
> -             for (slp_instance sub : instance->subgraph_entries)
> +             for (slp_instance sub : subgraphs)
>                 {
>                   tree vtype = SLP_TREE_VECTYPE (SLP_INSTANCE_TREE (sub));
>                   if (GET_MODE_SIZE (TYPE_MODE (vtype)).is_constant (&bytes))
> @@ -10692,7 +10916,7 @@ vect_slp_region (vec<basic_block> bbs, 
> vec<data_reference_p> datarefs,
>             dump_user_location_t saved_vect_location = vect_location;
>             vect_location = instance->location ();
>  
> -           vect_schedule_slp (bb_vinfo, instance->subgraph_entries, false);
> +           vect_schedule_slp (bb_vinfo, subgraphs, false, true);
>  
>             vect_location = saved_vect_location;
>           }
> @@ -12645,54 +12869,98 @@ vectorize_slp_instance_root_stmt (vec_info *vinfo, 
> slp_tree node, slp_instance i
>        if (reduc_code == MINUS_EXPR)
>       reduc_code = PLUS_EXPR;
>        gimple_seq epilogue = NULL;
> -      /* We may end up with more than one vector result, reduce them
> -      to one vector.  */
> -      tree vec_def = vec_defs[0];
> -      tree vectype = TREE_TYPE (vec_def);
> -      tree compute_vectype = vectype;
> -      bool pun_for_overflow_p = (ANY_INTEGRAL_TYPE_P (vectype)
> -                              && TYPE_OVERFLOW_UNDEFINED (vectype)
> -                              && operation_can_overflow (reduc_code));
> -      if (pun_for_overflow_p)
> -     {
> -       compute_vectype = unsigned_type_for (vectype);
> -       vec_def = gimple_build (&epilogue, VIEW_CONVERT_EXPR,
> -                               compute_vectype, vec_def);
> -     }
> -      for (unsigned i = 1; i < vec_defs.length (); ++i)
> -     {
> -       tree def = vec_defs[i];
> +
> +      /* Reduce the vector defs DEFS to a single scalar.  */
> +      auto reduce_vecs_to_scalar = [&] (vec<tree> &defs) -> tree
> +     {
> +       /* We may end up with more than one vector result, reduce them
> +          to one vector.  */
> +       tree vec_def = defs[0];
> +       tree vectype = TREE_TYPE (vec_def);
> +       tree compute_vectype = vectype;
> +       bool pun_for_overflow_p
> +         = (ANY_INTEGRAL_TYPE_P (vectype)
> +            && TYPE_OVERFLOW_UNDEFINED (vectype)
> +            && operation_can_overflow (reduc_code));
>         if (pun_for_overflow_p)
> -         def = gimple_build (&epilogue, VIEW_CONVERT_EXPR,
> -                             compute_vectype, def);
> -       vec_def = gimple_build (&epilogue, reduc_code, compute_vectype,
> -                               vec_def, def);
> -     }
> +         {
> +           compute_vectype = unsigned_type_for (vectype);
> +           vec_def = gimple_build (&epilogue, VIEW_CONVERT_EXPR,
> +                                   compute_vectype, vec_def);
> +         }
> +       for (unsigned i = 1; i < defs.length (); ++i)
> +         {
> +           tree def = defs[i];
> +           if (pun_for_overflow_p)
> +             def = gimple_build (&epilogue, VIEW_CONVERT_EXPR,
> +                                 compute_vectype, def);
> +           vec_def = gimple_build (&epilogue, reduc_code, compute_vectype,
> +                                   vec_def, def);
> +         }
> +       /* ???  Support other schemes than direct internal fn or two
> +          element vectors.  */
> +       tree scalar_def;
> +       internal_fn reduc_fn;
> +       if (!reduction_fn_for_scalar_code (reduc_code, &reduc_fn)
> +           || reduc_fn == IFN_LAST
> +           || !direct_internal_fn_supported_p (reduc_fn, compute_vectype,
> +                                               OPTIMIZE_FOR_BOTH))
> +         {
> +           gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (compute_vectype),
> +                                 2u));
> +           tree tem0 = gimple_build (&epilogue, BIT_FIELD_REF,
> +                                     TREE_TYPE (compute_vectype), vec_def,
> +                                     TYPE_SIZE (TREE_TYPE (compute_vectype)),
> +                                     bitsize_zero_node);
> +           tree tem1 = gimple_build (&epilogue, BIT_FIELD_REF,
> +                                     TREE_TYPE (compute_vectype), vec_def,
> +                                     TYPE_SIZE (TREE_TYPE (compute_vectype)),
> +                                     TYPE_SIZE (TREE_TYPE 
> (compute_vectype)));
> +           scalar_def = gimple_build (&epilogue, reduc_code,
> +                                      TREE_TYPE (compute_vectype),
> +                                      tem0, tem1);
> +         }
> +       else
> +         scalar_def = gimple_build (&epilogue, as_combined_fn (reduc_fn),
> +                                    TREE_TYPE (compute_vectype), vec_def);
> +       return gimple_convert (&epilogue, TREE_TYPE (vectype), scalar_def);
> +     };
> +
> +      tree vectype = TREE_TYPE (vec_defs[0]);
> +      tree scalar_def = reduce_vecs_to_scalar (vec_defs);
>        vec_defs.release ();
> -      /* ???  Support other schemes than direct internal fn or two
> -      element vectors.  */
> -      tree scalar_def;
> -      internal_fn reduc_fn;
> -      if (!reduction_fn_for_scalar_code (reduc_code, &reduc_fn)
> -       || reduc_fn == IFN_LAST
> -       || !direct_internal_fn_supported_p (reduc_fn, compute_vectype,
> -                                           OPTIMIZE_FOR_BOTH))
> -     {
> -       gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (compute_vectype), 2u));
> -       tree tem0 = gimple_build (&epilogue, BIT_FIELD_REF,
> -                                 TREE_TYPE (compute_vectype), vec_def,
> -                                 TYPE_SIZE (TREE_TYPE (compute_vectype)),
> -                                 bitsize_zero_node);
> -       tree tem1 = gimple_build (&epilogue, BIT_FIELD_REF,
> -                                 TREE_TYPE (compute_vectype), vec_def,
> -                                 TYPE_SIZE (TREE_TYPE (compute_vectype)),
> -                                 TYPE_SIZE (TREE_TYPE (compute_vectype)));
> +
> +      /* Combine the scalar results of the part instances.  */
> +      for (slp_instance part : instance->reduc_parts)
> +     {
> +       vec<tree> part_defs = vNULL;
> +       vect_get_slp_defs (SLP_INSTANCE_TREE (part), &part_defs);
> +       if (part_defs.is_empty ())
> +         {
> +           /* The part was not vectorized, reduce its scalar lanes.  */
> +           for (stmt_vec_info stmt_info
> +                : SLP_TREE_SCALAR_STMTS (SLP_INSTANCE_TREE (part)))
> +             if (stmt_info)
> +               {
> +                 tree def
> +                   = gimple_get_lhs (vect_orig_stmt (stmt_info)->stmt);
> +                 def = gimple_convert (&epilogue, TREE_TYPE (scalar_def),
> +                                       def);
> +                 scalar_def = gimple_build (&epilogue, reduc_code,
> +                                            TREE_TYPE (scalar_def),
> +                                            scalar_def, def);
> +               }
> +           continue;
> +         }
> +       tree part_scalar = reduce_vecs_to_scalar (part_defs);
> +       part_defs.release ();
> +       part_scalar = gimple_convert (&epilogue, TREE_TYPE (scalar_def),
> +                                     part_scalar);
>         scalar_def = gimple_build (&epilogue, reduc_code,
> -                                  TREE_TYPE (compute_vectype), tem0, tem1);
> +                                  TREE_TYPE (scalar_def),
> +                                  scalar_def, part_scalar);
>       }
> -      else
> -     scalar_def = gimple_build (&epilogue, as_combined_fn (reduc_fn),
> -                                TREE_TYPE (compute_vectype), vec_def);
> +
>        if (!SLP_INSTANCE_REMAIN_DEFS (instance).is_empty ())
>       {
>         tree rem_def = NULL_TREE;
> @@ -12946,7 +13214,7 @@ vect_schedule_scc (vec_info *vinfo, slp_tree node, 
> slp_instance instance,
>  
>  bool
>  vect_schedule_slp (vec_info *vinfo, vec<slp_instance> &slp_instances,
> -                bool place_only)
> +                bool place_only, bool defer_bb_reduc_parts)
>  {
>    slp_instance instance;
>    unsigned int i;
> @@ -12990,7 +13258,9 @@ vect_schedule_slp (vec_info *vinfo, vec<slp_instance> 
> &slp_instances,
>                     || (node->si != root_stmt
>                         && vect_stmt_dominates_stmt_p (node->si, root_stmt)));
>           }
> -       else
> +       else if (!defer_bb_reduc_parts
> +                || SLP_INSTANCE_KIND (instance) != slp_inst_kind_bb_reduc
> +                || instance->reduc_parts.is_empty ())
>           vectorize_slp_instance_root_stmt (vinfo, node, instance);
>       }
>  
> @@ -13005,6 +13275,7 @@ vect_schedule_slp (vec_info *vinfo, vec<slp_instance> 
> &slp_instances,
>           dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
>                            "not vectorized: cannot schedule SLP graph "
>                            "entry %p\n", (void *)instance);
> +       vect_unlink_slp_instance (instance, true);
>         vect_free_slp_instance (instance);
>         slp_instances.ordered_remove (i);
>         continue;
> @@ -13015,6 +13286,20 @@ vect_schedule_slp (vec_info *vinfo, 
> vec<slp_instance> &slp_instances,
>    if (place_only)
>      return !slp_instances.is_empty ();
>  
> +  /* The epilogue of a BB reduction with parts has to be emitted after all
> +     trees are scheduled, the parts can end up in any subgraph entry and
> +     their vector defs are required by the primary's root stmt handler.  */
> +  if (defer_bb_reduc_parts)
> +    FOR_EACH_VEC_ELT (slp_instances, i, instance)
> +      if (!SLP_INSTANCE_ROOT_STMTS (instance).is_empty ()
> +       && SLP_INSTANCE_KIND (instance) == slp_inst_kind_bb_reduc
> +       && !instance->reduc_parts.is_empty ())
> +     {
> +       vect_location = instance->location ();
> +       vectorize_slp_instance_root_stmt (vinfo, SLP_INSTANCE_TREE (instance),
> +                                         instance);
> +     }
> +
>    FOR_EACH_VEC_ELT (slp_instances, i, instance)
>      {
>        slp_tree root = SLP_INSTANCE_TREE (instance);
> diff --git a/gcc/tree-vectorizer.h b/gcc/tree-vectorizer.h
> index 7184f193b07..ded5604295c 100644
> --- a/gcc/tree-vectorizer.h
> +++ b/gcc/tree-vectorizer.h
> @@ -435,6 +435,7 @@ enum slp_instance_kind {
>      slp_inst_kind_reduc_group,
>      slp_inst_kind_reduc_chain,
>      slp_inst_kind_bb_reduc,
> +    slp_inst_kind_bb_reduc_part,
>      slp_inst_kind_ctor,
>      slp_inst_kind_gcond
>  };
> @@ -467,6 +468,14 @@ public:
>       entries into the same subgraph, including itself.  */
>    vec<_slp_instance *> subgraph_entries;
>  
> +  /* For slp_inst_kind_bb_reduc the part instances vectorizing the
> +     remaining lanes of the same reduction, vNULL otherwise.  */
> +  vec<_slp_instance *> reduc_parts;
> +
> +  /* For slp_inst_kind_bb_reduc_part the primary instance of the
> +     reduction this part belongs to, NULL otherwise.  */
> +  _slp_instance *reduc_primary;
> +
>    /* The type of operation the SLP instance is performing.  */
>    slp_instance_kind kind;
>  
> @@ -2775,7 +2784,8 @@ extern bool vect_transform_slp_perm_load (vec_info *, 
> slp_tree, const vec<tree>
>  extern bool vectorizable_slp_permutation (vec_info *, gimple_stmt_iterator *,
>                                         slp_tree, stmt_vector_for_cost *);
>  extern bool vect_slp_analyze_operations (vec_info *);
> -extern bool vect_schedule_slp (vec_info *, vec<slp_instance> &, bool);
> +extern bool vect_schedule_slp (vec_info *, vec<slp_instance> &, bool,
> +                            bool = false);
>  extern opt_result vect_analyze_slp (vec_info *, unsigned, bool);
>  extern bool vect_make_slp_decision (loop_vec_info);
>  extern void vect_optimize_slp (vec_info *);
> 

-- 
Richard Biener <[email protected]>
SUSE Software Solutions Germany GmbH,
Frankenstrasse 146, 90461 Nuernberg, Germany;
GF: Stefan Gaiser, Jochen Jaser, Abhinav Puri; (HRB 36809, AG Nuernberg)

Reply via email to