> -----Original Message-----
> From: Richard Biener <[email protected]>
> Sent: 10 November 2026 18:38
> To: [email protected]
> Cc: Prathamesh Kulkarni <[email protected]>
> Subject: [PATCH] Make SLP reduction group discovery fully recursive
> 
> External email: Use caution opening links or attachments
> 
> 
> The following simplifies SLP reduction group analysis by making it
> recursive which also handles sub-groups discovered by analyzing
> previously found isomorphic or unknown sub-groups.  With this the
> testcase extracted from astcenc passes SLP discovery on
> aarch64 with SVE with eight four-lane SLP reduction groups.
> 
> Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed.
> 
> Prathamesh - this gets your testcase up to the point where the missed
> SLP induction is the reason for not vectorizing.
Hi Richard,
Thanks for the patch!

I can see it now forms 4-lane SLP groups in every VLA mode.
With VNx16QI, it now fails at vectorizable_induction,
And for VNx4QI, it still fails earlier at the load permutation issue (due to 
VNx4QI being partial SVE vector).

With the induction patch, in:
https://gcc.gnu.org/pipermail/gcc-patches/2026-November/738358.html

It builds multilane VNx16QI, but similarly falls back to single-lane for VNx4QI.
And VNx4QI wins comfortably:

anon.cpp:121:29: note:  Comparing two main loops (VNx4QI at VF 4 vs VNx16QI at 
VF 4)
anon.cpp:121:29: note:  Issue info for VNx4QI loop:
anon.cpp:121:29: note:    load operations = 7
anon.cpp:121:29: note:    store operations = 0
anon.cpp:121:29: note:    general operations = 57
anon.cpp:121:29: note:    predicate operations = 5
anon.cpp:121:29: note:    reduction latency = 2
anon.cpp:121:29: note:    estimated min cycles per iteration without 
predication = 9.500000
anon.cpp:121:29: note:    estimated min cycles per iteration for predication = 
2.500000
anon.cpp:121:29: note:    estimated min cycles per iteration = 9.500000
anon.cpp:121:29: note:  Issue info for VNx16QI loop:
anon.cpp:121:29: note:    load operations = 27
anon.cpp:121:29: note:    store operations = 0
anon.cpp:121:29: note:    general operations = 100
anon.cpp:121:29: note:    predicate operations = 5
anon.cpp:121:29: note:    reduction latency = 2
anon.cpp:121:29: note:    estimated min cycles per iteration without 
predication = 16.666667
anon.cpp:121:29: note:    estimated min cycles per iteration for predication = 
2.500000
anon.cpp:121:29: note:    estimated min cycles per iteration = 16.666667
anon.cpp:121:29: note:  Weighted cycles per iteration of VNx4QI loop ~= 
38.000000
anon.cpp:121:29: note:  Weighted cycles per iteration of VNx16QI loop ~= 
66.666667
anon.cpp:121:29: note:  Preferring loop with lower cycles per iteration
anon.cpp:121:29: note:  ***** Preferring vector mode VNx4QI to vector mode 
VNx16QI

With code-gen being single-lane VNx4QI, which also gives the speedup.

I will look into why vect_optimize_slp pass doesn't remove redundant load 
permutations for VNx4QI as you suggested in:
https://gcc.gnu.org/pipermail/gcc-patches/2026-November/737734.html

which should result in multilane SLP code-gen for VNx4QI.

Thanks,
Prathamesh
> 
> Richard.
> 
>         * tree-vect-slp.cc (vect_analyze_slp_reduction_group):
>         Add sub-group discovery here and handle failures by
>         recursing.
>         (vect_analyze_slp_reductions): Simplify with code
>         moved to vect_analyze_slp_reduction_group.
> ---
>  gcc/tree-vect-slp.cc | 227 +++++++++++++++++++++---------------------
> -
>  1 file changed, 111 insertions(+), 116 deletions(-)
> 
> diff --git a/gcc/tree-vect-slp.cc b/gcc/tree-vect-slp.cc index
> 0a28e958117..ae73837a76d 100644
> --- a/gcc/tree-vect-slp.cc
> +++ b/gcc/tree-vect-slp.cc
> @@ -4971,66 +4971,121 @@ vect_analyze_slp_reduction (loop_vec_info
> vinfo,
>    return false;
>  }
> 
> -/* Analyze a single SLP reduction group.  If successful add a SLP
> instance
> -   for it and return true, otherwise return false and have *MATCHES
> -   populated.  */
> +/* Analyze SCALAR_STMTS for SLP reduction groups.  If successful add
> +   SLP instances for them and return true.  Any groups of size one
> +   are handled appropriately.  Otherwise return false.  */
> 
>  static bool
>  vect_analyze_slp_reduction_group (loop_vec_info loop_vinfo,
>                                   vec<stmt_vec_info> scalar_stmts,
>                                   scalar_stmts_to_slp_tree_map_t
> *bst_map,
> -                                 unsigned max_tree_size, unsigned
> *limit,
> -                                 match_elt_t *matches)
> +                                 unsigned max_tree_size, unsigned
> + *limit)
>  {
>    /* Try to form a reduction group.  Size-1 groups are not suitable
>       for SLP reduction and should fall back to single-lane reduction.
> */
>    unsigned int group_size = scalar_stmts.length ();
> -  if (group_size <= 1)
> -    return false;
> -  if (!matches)
> -    matches = XALLOCAVEC (match_elt_t, group_size);
> +  gcc_assert (group_size > 1);
> +  match_elt_t *matches = XALLOCAVEC (match_elt_t, group_size);
>    unsigned tree_size = 0;
>    slp_tree node = vect_build_slp_tree (loop_vinfo, scalar_stmts,
> matches, limit,
>                                        &tree_size, bst_map);
> -  if (!node)
> -    return false;
> +  if (node)
> +    {
> +      /* Create a new SLP instance.  */
> +      slp_instance new_instance = XNEW (class _slp_instance);
> +      SLP_INSTANCE_TREE (new_instance) = node;
> +      SLP_INSTANCE_LOADS (new_instance) = vNULL;
> +      SLP_INSTANCE_ROOT_STMTS (new_instance) = vNULL;
> +      SLP_INSTANCE_REMAIN_DEFS (new_instance) = vNULL;
> +      SLP_INSTANCE_KIND (new_instance) = slp_inst_kind_reduc_group;
> +      new_instance->reduc_phis = NULL;
> +      new_instance->cost_vec = vNULL;
> +      new_instance->subgraph_entries = vNULL;
> 
> -  /* Create a new SLP instance.  */
> -  slp_instance new_instance = XNEW (class _slp_instance);
> -  SLP_INSTANCE_TREE (new_instance) = node;
> -  SLP_INSTANCE_LOADS (new_instance) = vNULL;
> -  SLP_INSTANCE_ROOT_STMTS (new_instance) = vNULL;
> -  SLP_INSTANCE_REMAIN_DEFS (new_instance) = vNULL;
> -  SLP_INSTANCE_KIND (new_instance) = slp_inst_kind_reduc_group;
> -  new_instance->reduc_phis = NULL;
> -  new_instance->cost_vec = vNULL;
> -  new_instance->subgraph_entries = vNULL;
> +      if (dump_enabled_p ())
> +       dump_printf_loc (MSG_NOTE, vect_location,
> +                        "SLP size %u vs. limit %u.\n",
> +                        tree_size, max_tree_size);
> 
> -  if (dump_enabled_p ())
> -    dump_printf_loc (MSG_NOTE, vect_location,
> -                    "SLP size %u vs. limit %u.\n",
> -                    tree_size, max_tree_size);
> +      loop_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.  */
> +      gcc_assert (SLP_TREE_LANES (SLP_INSTANCE_TREE (new_instance))
> +                 == group_size);
> 
> -  loop_vinfo->slp_instances.safe_push (new_instance);
> +      if (dump_enabled_p ())
> +       {
> +         dump_printf_loc (MSG_NOTE, vect_location,
> +                          "SLP discovery of size %d reduction group "
> +                          "succeeded\n", group_size);
> +         dump_printf_loc (MSG_NOTE, vect_location,
> +                          "Final SLP tree for instance %p:\n",
> +                          (void *) new_instance);
> +         vect_print_slp_graph (MSG_NOTE, vect_location,
> +                               SLP_INSTANCE_TREE (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.  */
> -  gcc_assert (SLP_TREE_LANES (SLP_INSTANCE_TREE (new_instance))
> -             == group_size);
> +      return true;
> +    }
> 
> -  if (dump_enabled_p ())
> +  /* Bail on fatal error.  */
> +  if (matches[0] == -1)
> +    return false;
> +
> +  /* When analysis as a single SLP reduction group failed try to form
> +     sub-groups by collecting matching lanes.  Everything not
> +     covered by a sub-group gets single-reduction treatment.  */
> + vec<stmt_vec_info> cands = vNULL;  int first_cand = -1;  do
>      {
> -      dump_printf_loc (MSG_NOTE, vect_location,
> -                      "SLP discovery of size %d reduction group "
> -                      "succeeded\n", group_size);
> -      dump_printf_loc (MSG_NOTE, vect_location,
> -                      "Final SLP tree for instance %p:\n",
> -                      (void *) new_instance);
> -      vect_print_slp_graph (MSG_NOTE, vect_location,
> -                           SLP_INSTANCE_TREE (new_instance));
> +      cands.truncate (0);
> +      cands.reserve (group_size, true);
> +      int new_first_cand = -1;
> +      for (unsigned i = 0; i < group_size; ++i)
> +       if (matches[i] == first_cand)
> +         {
> +           cands.quick_push (scalar_stmts[i]);
> +           scalar_stmts[i] = NULL;
> +         }
> +       else if (new_first_cand == -1
> +                && matches[i] > first_cand)
> +         {
> +           gcc_assert (matches[i] == (int)i);
> +           new_first_cand = i;
> +         }
> +
> +      if (cands.is_empty ())
> +       ;
> +      else if (cands.length () == 1)
> +       {
> +         if (! vect_analyze_slp_reduction (loop_vinfo,
> +                                           vect_stmt_to_vectorize
> (cands[0]),
> +                                           max_tree_size, limit,
> +                                           bst_map, false))
> +           {
> +             cands.release ();
> +             return false;
> +           }
> +         cands.release ();
> +       }
> +      /* Try to form a reduction group.  */
> +      else if (!vect_analyze_slp_reduction_group (loop_vinfo, cands,
> bst_map,
> +                                                 max_tree_size,
> limit))
> +       {
> +         cands.release ();
> +         return false;
> +       }
> +      else
> +       cands = vNULL;
> +      first_cand = new_first_cand;
>      }
> +  while (first_cand >= 0);
> +  cands.release ();
> 
> +  scalar_stmts.release ();
>    return true;
>  }
> 
> @@ -5080,88 +5135,28 @@ vect_analyze_slp_reductions (loop_vec_info
> loop_vinfo,
>         }
>      }
> 
> +  /* Try to form reduction groups.  */
>    if (scalar_stmts.length () > 1)
>      {
> -      /* Try to form a reduction group.  */
> -      unsigned int group_size = scalar_stmts.length ();
> -      match_elt_t *matches = XALLOCAVEC (match_elt_t, group_size);
> -      if (vect_analyze_slp_reduction_group (loop_vinfo, scalar_stmts,
> bst_map,
> -                                           max_tree_size, limit,
> matches))
> -       return true;
> -
> -      /* When analysis as a single SLP reduction group failed try to
> -        form sub-groups by collecting matching lanes.  Everything not
> -        covered by a sub-group gets single-reduction treatment.  */
> -      vec<stmt_vec_info> cands = vNULL;
> -      int first_cand = matches[0] != -1 ? 0 : -1;
> -      while (first_cand >= 0)
> -       {
> -         cands.truncate (0);
> -         cands.reserve (group_size, true);
> -         int new_first_cand = -1;
> -         for (unsigned i = 0; i < group_size; ++i)
> -           if (matches[i] == first_cand)
> -             {
> -               cands.quick_push (scalar_stmts[i]);
> -               scalar_stmts[i] = NULL;
> -             }
> -           else if (new_first_cand == -1
> -                    && matches[i] > first_cand)
> -             {
> -               gcc_assert (matches[i] == (int)i);
> -               new_first_cand = i;
> -             }
> -
> -         if (cands.is_empty ())
> -           ;
> -         /* Try to form a reduction group.  */
> -         else if (vect_analyze_slp_reduction_group (loop_vinfo,
> cands,
> -                                                    bst_map,
> max_tree_size,
> -                                                    limit, NULL))
> -           cands = vNULL;
> -         else
> -           {
> -             /* Do SLP discovery for single-lane reductions.  */
> -             for (auto stmt_info : cands)
> -               if (! vect_analyze_slp_reduction (loop_vinfo,
> -
> vect_stmt_to_vectorize
> -                                                   (stmt_info),
> -                                                 max_tree_size,
> limit,
> -                                                 bst_map,
> force_single_lane))
> -                 {
> -                   scalar_stmts.release ();
> -                   cands.release ();
> -                   return false;
> -                 }
> -           }
> -         first_cand = new_first_cand;
> -       }
> -      cands.release ();
> -
> -      /* Handle unmatched entries as single-lane.  */
> -      for (unsigned i = 0; i < group_size; ++i)
> -       if (scalar_stmts[i] != NULL)
> -         if (! vect_analyze_slp_reduction (loop_vinfo,
> -                                           vect_stmt_to_vectorize
> -                                             (scalar_stmts[i]),
> -                                           max_tree_size, limit,
> -                                           bst_map,
> force_single_lane))
> -           {
> -             scalar_stmts.release ();
> -             return false;
> -           }
> -    }
> -  else
> -    /* Do SLP discovery for single-lane reductions.  */
> -    for (auto stmt_info : scalar_stmts)
> -      if (! vect_analyze_slp_reduction (loop_vinfo,
> -                                       vect_stmt_to_vectorize
> (stmt_info),
> -                                       max_tree_size, limit,
> -                                       bst_map, force_single_lane))
> +      if (!vect_analyze_slp_reduction_group (loop_vinfo,
> scalar_stmts, bst_map,
> +                                            max_tree_size, limit))
>         {
>           scalar_stmts.release ();
>           return false;
>         }
> +      return true;
> +    }
> +
> +  /* Do SLP discovery for a single reduction.  */  if
> + (scalar_stmts.length () == 1
> +      && !vect_analyze_slp_reduction (loop_vinfo,
> +                                     vect_stmt_to_vectorize
> (scalar_stmts[0]),
> +                                     max_tree_size, limit,
> +                                     bst_map, force_single_lane))
> +    {
> +      scalar_stmts.release ();
> +      return false;
> +    }
> 
>    scalar_stmts.release ();
>    return true;
> 
> base-commit: 78148ac7f7e6bc7a7e82e0b761b48168c81c1988
> prerequisite-patch-id: e52d299db98b0892f974152cd9fa5f6859fec5b7
> prerequisite-patch-id: 39da6ec3264bae454fa504f127cd5b80a0662f04
> prerequisite-patch-id: 6d6c263a964425334ad8c2edd4dd89dcbff44bfe
> prerequisite-patch-id: 9a12a1706cbdff1edf8959682cd3cf9d900cd8f1
> prerequisite-patch-id: b24057443c46d6979436cce479836178b19355de
> prerequisite-patch-id: d61bf2c4249ecd3d9da5eeda002d332ed3a8b4fd
> prerequisite-patch-id: 59cbeca60f782f48f945bc532c2235a50de6f0e5
> prerequisite-patch-id: 8eddd3172e2007d0fc591124a56b372dfe83e683
> prerequisite-patch-id: 862842dd3742066d039f66ffe9528bc490308c50
> prerequisite-patch-id: e413d39d9af45f25483eaf229cf883c771444fbb
> prerequisite-patch-id: 2faef186ad1d816bb104d89638bc4cec6d863a46
> prerequisite-patch-id: b32cceb111f4f57798e4b1d58822281e7bbaf25e
> prerequisite-patch-id: 08ff1c06921c5e253d911128be3a8ebb76137d3c
> prerequisite-patch-id: 72aa7ba47bd81909871a9e1a2e9de14f2cf42174
> prerequisite-patch-id: 2ce10434bf0a7ab22e27ff177073e10b4b7d0d72
> prerequisite-patch-id: e286b2e30887f3f43a3458c7c9b3f8c8b513a91b
> prerequisite-patch-id: dc07b9957107f8b453a87ad64fd8477ae26e370c
> prerequisite-patch-id: 146a9a34e1f46284793e3329e70d1d689efb4145
> --
> 2.51.0

Reply via email to