On Wed, Nov 11, 2026 at 11:11 AM Robin Dapp <[email protected]> wrote:
>
> > So, this is indeed sth on my rather longish TODO list, also for
> > load permutations.  But implementation-wise I'd have gone a different
> > way, namely using _slp_tree::data to record the analysis-time data
> > and during transform use that data.  That would also allow to
> > side-step the vec_per_indices construction a bit because I'd consider
> > it OK to simply build and store the tree objects.  Targets can then
> > inspect this.
> > I consider stmt_info_for_cost a bit outdated - I know it's somewhat
> > convenient for a SLP node that generates multiple stmts and my approach
> > might fall a bit short on that.
>
> If storing trees is acceptable that makes it easier :)
> Your approach would roughly look like the attached (I'm trying to have
> this in 17 still).  Would that have a way forward?  There are still some
> issues with it, for example that the bswap perm is not included.  Also,
> I guess we could get rid of ls.n_perms now, but I didn't yet.  While
> crawling through vectorizable_slp_permutation_1, I thought I should
> really "rename"/alias !gsi to analyze_only but resisted...
>
> Local LLM review flagged that the node data stays around when rolling
> back the visited nodes/set in vect_slp_analyze_node_operations and we
> could get stale perms if we re-analyzed.  I haven't convinced myself yet
> but it could be real.
>
> And, as you say, per-stmt costing is going to be trickier with multiple
> perms per stmt.  Even though this has been regtested, I'd obviously
> still polish it before posting the real patch.
>
> Regards
>  Robin
>
>
> From 64c94a4fadf8b58f421d9ae915151b5bb6139c98 Mon Sep 17 00:00:00 2001
> From: Robin Dapp <[email protected]>
> Date: Tue, 10 Nov 2026 14:24:31 +0100
> Subject: [PATCH] perm costing
>
> ---
>  gcc/tree-vect-slp.cc   | 84 ++++++++++++++++++++++++++++++------------
>  gcc/tree-vect-stmts.cc | 15 +++++---
>  gcc/tree-vectorizer.h  |  8 +++-
>  3 files changed, 78 insertions(+), 29 deletions(-)
>
> diff --git a/gcc/tree-vect-slp.cc b/gcc/tree-vect-slp.cc
> index e52b278c0de..4b7c0691fcb 100644
> --- a/gcc/tree-vect-slp.cc
> +++ b/gcc/tree-vect-slp.cc
> @@ -61,12 +61,13 @@ static bool vect_transform_slp_perm_load_1 (vec_info *, 
> slp_tree,
>                                             const vec<tree> &,
>                                             gimple_stmt_iterator *,
>                                             poly_uint64, bool, bool,
> -                                           unsigned *,
> +                                           vect_data *, unsigned *,
>                                             unsigned * = nullptr,
>                                             bool = false);
>  static int vectorizable_slp_permutation_1 (vec_info *, gimple_stmt_iterator 
> *,
>                                            slp_tree, lane_permutation_t &,
> -                                          vec<slp_tree> &, bool);
> +                                          vec<slp_tree> &, bool,
> +                                          vect_data *);
>  static void vect_print_slp_tree (dump_flags_t, dump_location_t, slp_tree);
>  static bool vect_slp_can_convert_to_external (const vec<stmt_vec_info> &);
>
> @@ -7240,7 +7241,7 @@ vect_optimize_slp_pass::change_layout_cost (slp_tree 
> node,
>    if (to_layout_i > 0)
>      vect_slp_permute (m_perms[to_layout_i], perm, true);
>    auto count = vectorizable_slp_permutation_1 (m_vinfo, nullptr, node, perm,
> -                                              children, false);
> +                                              children, false, nullptr);
>    if (count >= 0)
>      return MAX (count, 1);
>
> @@ -7336,7 +7337,7 @@ vect_optimize_slp_pass::internal_node_cost (slp_tree 
> node, int in_layout_i,
>        int count = vectorizable_slp_permutation_1 (m_vinfo, nullptr,
>                                                   node, tmp_perm,
>                                                   SLP_TREE_CHILDREN (node),
> -                                                 false);
> +                                                 false, nullptr);
>        if (count < 0)
>         {
>           if (in_layout_i == 0 && out_layout_i == 0)
> @@ -7376,7 +7377,8 @@ vect_optimize_slp_pass::internal_node_cost (slp_tree 
> node, int in_layout_i,
>         vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
>        unsigned int n_perms;
>        if (!vect_transform_slp_perm_load_1 (m_vinfo, node, tmp_perm, vNULL,
> -                                          nullptr, vf, true, false, 
> &n_perms))
> +                                          nullptr, vf, true, false, nullptr,
> +                                          &n_perms))
>         {
>           auto rep = SLP_TREE_REPRESENTATIVE (node);
>           if (out_layout_i == 0)
> @@ -8095,7 +8097,7 @@ vect_optimize_slp_pass::get_result_with_layout 
> (slp_tree node,
>           if (vectorizable_slp_permutation_1 (m_vinfo, nullptr, node,
>                                               tmp_perm,
>                                               SLP_TREE_CHILDREN (node),
> -                                             false) >= 0)
> +                                             false, nullptr) >= 0)
>             num_inputs = SLP_TREE_CHILDREN (node).length ();
>           else
>             tmp_perm.truncate (0);
> @@ -8192,7 +8194,7 @@ vect_optimize_slp_pass::materialize ()
>           if (vectorizable_slp_permutation_1 (m_vinfo, nullptr, node,
>                                               tmp_perm,
>                                               SLP_TREE_CHILDREN (node),
> -                                             false) >= 0)
> +                                             false, nullptr) >= 0)
>             {
>               if (dump_enabled_p ()
>                   && !std::equal (tmp_perm.begin (), tmp_perm.end (),
> @@ -11463,6 +11465,7 @@ vect_transform_slp_perm_load_1 (vec_info *vinfo, 
> slp_tree node,
>                                 const vec<tree> &dr_chain,
>                                 gimple_stmt_iterator *gsi, poly_uint64 vf,
>                                 bool analyze_only, bool dump_p,
> +                               vect_data *perm_data,
>                                 unsigned *n_perms, unsigned int *n_loads,
>                                 bool dce_chain)
>  {
> @@ -11632,8 +11635,16 @@ vect_transform_slp_perm_load_1 (vec_info *vinfo, 
> slp_tree node,
>                 }
>
>               tree mask_vec = NULL_TREE;
> -             if (!analyze_only)
> -               mask_vec = vect_gen_perm_mask_checked (vectype, indices);
> +             if (perm_data)
> +               {
> +                 if (analyze_only)
> +                   {
> +                     mask_vec = vect_gen_perm_mask_checked (vectype, 
> indices);
> +                     perm_data->perms.safe_push ({mask_vec, 
> nvectors_per_build});
> +                   }
> +                 else
> +                   mask_vec = perm_data->perms[*n_perms].first;
> +               }
>
>               if (second_vec_index == -1)
>                 second_vec_index = first_vec_index;
> @@ -11749,14 +11760,21 @@ bool
>  vect_transform_slp_perm_load (vec_info *vinfo,
>                               slp_tree node, const vec<tree> &dr_chain,
>                               gimple_stmt_iterator *gsi, poly_uint64 vf,
> -                             bool analyze_only, unsigned *n_perms,
> -                             unsigned int *n_loads, bool dce_chain)
> +                             bool analyze_only, vect_data *perm_data,
> +                             unsigned *n_perms, unsigned int *n_loads,
> +                             bool dce_chain)
>  {
> -  return vect_transform_slp_perm_load_1 (vinfo, node,
> -                                        SLP_TREE_LOAD_PERMUTATION (node),
> -                                        dr_chain, gsi, vf, analyze_only,
> -                                        dump_enabled_p (), n_perms, n_loads,
> -                                        dce_chain);
> +  if (analyze_only && perm_data)
> +    perm_data->perms.truncate (0);
> +  bool ok = vect_transform_slp_perm_load_1 (vinfo, node,
> +                                           SLP_TREE_LOAD_PERMUTATION (node),
> +                                           dr_chain, gsi, vf, analyze_only,
> +                                           dump_enabled_p (), perm_data,
> +                                           n_perms, n_loads, dce_chain);
> +  if (!ok && perm_data)
> +    perm_data->perms.truncate (0);
> +
> +  return ok;
>  }
>
>  /* Produce the next vector result for SLP permutation NODE by adding a vector
> @@ -11873,7 +11891,8 @@ vect_add_slp_permutation (vec_info *vinfo, 
> gimple_stmt_iterator *gsi,
>  static int
>  vectorizable_slp_permutation_1 (vec_info *vinfo, gimple_stmt_iterator *gsi,
>                                 slp_tree node, lane_permutation_t &perm,
> -                               vec<slp_tree> &children, bool dump_p)
> +                               vec<slp_tree> &children, bool dump_p,
> +                               vect_data *perm_data)
>  {
>    tree vectype = SLP_TREE_VECTYPE (node);
>
> @@ -12122,6 +12141,7 @@ vectorizable_slp_permutation_1 (vec_info *vinfo, 
> gimple_stmt_iterator *gsi,
>    mask.quick_grow (count);
>    vec_perm_indices indices;
>    unsigned nperms = 0;
> +  unsigned int perm_count = 0;
>    /* When REPEATING_P is true, we only have UNPACK_FACTOR unique permute
>       vectors to check during analysis, but we need to generate NOUTPUTS
>       vectors during transformation.  */
> @@ -12140,6 +12160,8 @@ vectorizable_slp_permutation_1 (vec_info *vinfo, 
> gimple_stmt_iterator *gsi,
>        unsigned vi = i / olanes * (pack_p ? 2 : 1);
>        unsigned ei = i % olanes;
>        mask_element = vperm[ei].second;
> +      if (ei == 0)
> +       perm_count = 0;
>        if (pack_p)
>         {
>           /* In this case, we have N outputs and the single child provides 2N
> @@ -12207,8 +12229,23 @@ vectorizable_slp_permutation_1 (vec_info *vinfo, 
> gimple_stmt_iterator *gsi,
>               return -1;
>             }
>
> +         tree mask_vec = NULL_TREE;
> +         unsigned perm_num
> +           = CEIL (total_nelts, process_nelts) - (ei > last_ei);
> +         if (!identity_p && perm_data)
> +           {
> +             if (!gsi)
> +               {
> +                 mask_vec = vect_gen_perm_mask_checked (vectype, indices);
> +                 perm_data->perms.safe_push ({mask_vec, perm_num});
> +               }
> +             else
> +               mask_vec = perm_data->perms[perm_count].first;
> +             perm_count++;
> +           }
> +
>           if (!identity_p)
> -           nperms += CEIL (total_nelts, process_nelts) - (ei > last_ei);
> +           nperms += perm_num;
>           if (gsi)
>             {
>               if (second_vec.first == -1U)
> @@ -12218,10 +12255,6 @@ vectorizable_slp_permutation_1 (vec_info *vinfo, 
> gimple_stmt_iterator *gsi,
>                 first_node = children[first_vec.first],
>                 second_node = children[second_vec.first];
>
> -             tree mask_vec = NULL_TREE;
> -             if (!identity_p)
> -               mask_vec = vect_gen_perm_mask_checked (vectype, indices);
> -
>               tree first_def
>                 = vect_get_slp_vect_def (first_node, first_vec.second + vi);
>               tree second_def
> @@ -12256,15 +12289,20 @@ vectorizable_slp_permutation (vec_info *vinfo, 
> gimple_stmt_iterator *gsi,
>  {
>    tree vectype = SLP_TREE_VECTYPE (node);
>    lane_permutation_t &perm = SLP_TREE_LANE_PERMUTATION (node);
> +  vect_data _data;
> +  vect_data &data = node->get_data (_data);
>    int nperms = vectorizable_slp_permutation_1 (vinfo, gsi, node, perm,
>                                                SLP_TREE_CHILDREN (node),
> -                                              dump_enabled_p ());
> +                                              dump_enabled_p (), &data);
>    if (nperms < 0)
>      return false;
>
>    if (!gsi && nperms != 0)
>      record_stmt_cost (cost_vec, nperms, vec_perm, node, vectype, 0, 
> vect_body);
>
> +  if (!gsi)
> +    node->data = new vect_data (std::move (data));
> +
>    return true;
>  }
>
> diff --git a/gcc/tree-vect-stmts.cc b/gcc/tree-vect-stmts.cc
> index fb9aa652289..6b1bad9f1f5 100644
> --- a/gcc/tree-vect-stmts.cc
> +++ b/gcc/tree-vect-stmts.cc
> @@ -2107,7 +2107,7 @@ get_load_store_type (vec_info  *vinfo, stmt_vec_info 
> stmt_info,
>
>    if (SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
>      perm_ok = vect_transform_slp_perm_load (vinfo, slp_node, vNULL, NULL,
> -                                           vf, true, n_perms, n_loads);
> +                                           vf, true, ls, n_perms, n_loads);
>
>    if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
>      {
> @@ -2751,6 +2751,11 @@ get_load_store_type (vec_info  *vinfo, stmt_vec_info 
> stmt_info,
>        *slp_perm = true;
>      }
>
> +  /* If we didn't end up using the permute, clear them in the costing 
> structure
> +     as well.  */
> +  if (!*slp_perm)
> +    ls->perms.truncate (0);
> +
>    return true;
>  }
>
> @@ -10566,7 +10571,7 @@ vectorizable_load (vec_info *vinfo,
>             {
>               unsigned n_perms2;
>               vect_transform_slp_perm_load (vinfo, slp_node, dr_chain, gsi, 
> vf,
> -                                           false, &n_perms2);
> +                                           false, &ls, &n_perms2);
>               gcc_assert (ls.n_perms == n_perms2);
>             }
>         }
> @@ -11410,7 +11415,7 @@ vectorizable_load (vec_info *vinfo,
>             {
>               unsigned n_perms2;
>               vect_transform_slp_perm_load (vinfo, slp_node, dr_chain, gsi, 
> vf,
> -                                           false, &n_perms2);
> +                                           false, &ls, &n_perms2);
>               gcc_assert (ls.n_perms == n_perms2);
>             }
>         }
> @@ -12062,8 +12067,8 @@ vectorizable_load (vec_info *vinfo,
>         {
>           unsigned n_perms2, n_loads2;
>           bool ok = vect_transform_slp_perm_load (vinfo, slp_node, dr_chain,
> -                                                 gsi, vf, false, &n_perms2,
> -                                                 &n_loads2, true);
> +                                                 gsi, vf, false, &ls,
> +                                                 &n_perms2, &n_loads2, true);
>           gcc_assert (ok && ls.n_perms == n_perms2 && ls.n_loads == n_loads2);
>         }
>      }
> diff --git a/gcc/tree-vectorizer.h b/gcc/tree-vectorizer.h
> index 7184f193b07..86dd077d8c6 100644
> --- a/gcc/tree-vectorizer.h
> +++ b/gcc/tree-vectorizer.h
> @@ -257,6 +257,12 @@ typedef auto_vec<unsigned, 16> auto_load_permutation_t;
>
>  struct vect_data {
>    virtual ~vect_data () = default;
> +  vect_data () = default;
> +  vect_data (vect_data &&other) = default;
> +
> +  /* Keep the permutes here so load/store nodes as well as permute nodes can
> +     use it.  */
> +  auto_vec<std::pair<tree, unsigned>> perms;
>  };
>

please derive from vect_data like vect_load_store_data does, at least for
vect permute nodes.  For load permutations put the permute data into
vect_load_store_data and if there's common routines involved (IIRC not)
then consider passing the address of the perms vector itself instead
of the abstract data class.

Thanks,
Richard.

>  /* Analysis data from vectorizable_simd_clone_call for
> @@ -2770,7 +2776,7 @@ extern void vect_slp_fini (void);
>  extern void vect_free_slp_instance (slp_instance);
>  extern bool vect_transform_slp_perm_load (vec_info *, slp_tree, const 
> vec<tree> &,
>                                           gimple_stmt_iterator *, poly_uint64,
> -                                         bool, unsigned *,
> +                                         bool, vect_data *, unsigned *,
>                                           unsigned * = nullptr, bool = false);
>  extern bool vectorizable_slp_permutation (vec_info *, gimple_stmt_iterator *,
>                                           slp_tree, stmt_vector_for_cost *);
> --
> 2.56.0
>
>

Reply via email to