> 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.

Ok, makes sense and requires fewer changes as well.  The attached v2 has 
that.

This patch adds a permute array to vect_load_store_data and to the new
vect_permute_data.  During analysis we build a permute constant as tree
and add it to the array.  The codegen phase will reuse the constant and
a target can query all permutes belonging to an SLP node which allows
more specific costing.

As suggested by a local LLM review, I also added code to delete node 
data when "reverting" visited nodes in vect_slp_analyze_node_operations.

gcc/ChangeLog:

        * tree-vect-slp.cc (vectorizable_slp_permutation_1): Add
        permute data parameter.
        (vect_optimize_slp_pass::change_layout_cost): Pass null data.
        (vect_optimize_slp_pass::internal_node_cost): Likewise.
        (vect_optimize_slp_pass::get_result_with_layout): Likewise.
        (vect_optimize_slp_pass::materialize): Likewise.
        (vect_slp_analyze_node_operations): Delete node-specific data
        after failed analysis.
        (vect_transform_slp_perm_load_1): Add data parameter.
        (vect_transform_slp_perm_load): Ditto.
        (vectorizable_slp_permutation): Ditto.
        * tree-vect-stmts.cc (get_load_store_type): Pass load_store
        data.
        (vectorizable_load): Ditto.
        * tree-vectorizer.h (struct vect_permute_data): New struct.
        (vect_transform_slp_perm_load): Add data param.
---
 gcc/tree-vect-slp.cc   | 93 +++++++++++++++++++++++++++++++-----------
 gcc/tree-vect-stmts.cc | 15 ++++---
 gcc/tree-vectorizer.h  |  8 +++-
 3 files changed, 86 insertions(+), 30 deletions(-)

diff --git a/gcc/tree-vect-slp.cc b/gcc/tree-vect-slp.cc
index 6fbbc91418e..1030ea32503 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_load_store_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_permute_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> &);
 
@@ -7130,7 +7131,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);
 
@@ -7226,7 +7227,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)
@@ -7266,7 +7267,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)
@@ -7985,7 +7987,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);
@@ -8082,7 +8084,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 (),
@@ -9217,7 +9219,12 @@ vect_slp_analyze_node_operations (vec_info *vinfo, 
slp_tree node,
   if (!res)
     {
       while (visited_vec.length () >= visited_rec_start)
-       visited_set.remove (visited_vec.pop ());
+       {
+         slp_tree nd = visited_vec.pop ();
+         visited_set.remove (nd);
+         delete nd->data;
+         nd->data = nullptr;
+       }
       cost_vec->truncate (cost_vec_rec_start);
     }
 
@@ -11354,6 +11361,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_load_store_data *perm_data,
                                unsigned *n_perms, unsigned int *n_loads,
                                bool dce_chain)
 {
@@ -11414,6 +11422,7 @@ vect_transform_slp_perm_load_1 (vec_info *vinfo, 
slp_tree node,
   int second_vec_index = -1;
   bool noop_p = true;
   *n_perms = 0;
+  unsigned int perm_count = 0;
 
   vec_perm_builder mask;
   unsigned int nelts_to_build;
@@ -11523,8 +11532,17 @@ 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[perm_count].first;
+                 perm_count++;
+               }
 
              if (second_vec_index == -1)
                second_vec_index = first_vec_index;
@@ -11640,14 +11658,18 @@ 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_load_store_data *ls_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 && ls_data)
+    ls_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 (), ls_data,
+                                           n_perms, n_loads, dce_chain);
+  return ok;
 }
 
 /* Produce the next vector result for SLP permutation NODE by adding a vector
@@ -11764,7 +11786,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_permute_data *perm_data)
 {
   tree vectype = SLP_TREE_VECTYPE (node);
 
@@ -12013,6 +12036,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.  */
@@ -12031,6 +12055,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
@@ -12098,8 +12124,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)
@@ -12109,10 +12150,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
@@ -12147,15 +12184,23 @@ 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_permute_data _data;
+  vect_permute_data &data = node->get_data (_data);
+  /* Delete any leftover permutes if we're analyzing just in case.  */
+  if (!gsi)
+    data.perms.truncate (0);
   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)
+    node->data = new vect_permute_data (std::move (data));
+
   return true;
 }
 
diff --git a/gcc/tree-vect-stmts.cc b/gcc/tree-vect-stmts.cc
index cd2300cd30d..e50124c4bf9 100644
--- a/gcc/tree-vect-stmts.cc
+++ b/gcc/tree-vect-stmts.cc
@@ -2118,7 +2118,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))
     {
@@ -2763,6 +2763,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;
 }
 
@@ -10580,7 +10585,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);
            }
        }
@@ -11426,7 +11431,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);
            }
        }
@@ -12078,8 +12083,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 84c23960b71..6391563516f 100644
--- a/gcc/tree-vectorizer.h
+++ b/gcc/tree-vectorizer.h
@@ -310,12 +310,18 @@ struct vect_load_store_data : vect_data {
   auto_vec<int> elsvals;
   /* True if the load requires a load permutation.  */
   bool slp_perm;    // SLP_TREE_LOAD_PERMUTATION
+  auto_vec<std::pair<tree, unsigned>> perms;
   unsigned n_perms; // SLP_TREE_LOAD_PERMUTATION
   unsigned n_loads; // SLP_TREE_LOAD_PERMUTATION
   /* Whether the load permutation is consecutive and simple.  */
   bool subchain_p; // VMAT_STRIDED_SLP and VMAT_GATHER_SCATTER
 };
 
+/* Analysis data from vectorizable_slp_permutation.  */
+struct vect_permute_data : vect_data {
+  auto_vec<std::pair<tree, unsigned>> perms;
+};
+
 struct match_elt_t {
   int v;
   operator bool () const = delete;
@@ -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_load_store_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