Hi,
After years of complaining about it, I finally went ahead and drafted
something to improve the permute costing situation. RVV can benefit
from better costing because we implement very simple as well as
somewhat complex gather sequences. Additionally we're faced with
potentially quadratic latency increase at higher LMUL
(grouped vector registers).
The patch passes permute indices to targets so they can classify the
permute in a straightforward way. The main complication is that
vec_perm_indices currently live on the stack but costing happens after
they go out of scope. On top, they're not directly copyable. The
approach this patch takes is to let vec_info own pointers in
vec<vec_perm_indices *> to all necessary indices by rebuilding them on
the heap (and freeing them when vec_info is destroyed) in a new function
vec_info::record_perm_indices.
Those heap pointers are passed to targets via a new perm field in
stmt_info_for_cost. Interested targets can access it by add_slp_cost.
Wanting to touch as little as possible, there are still a few cases I
left out, most prominently load permutations. It doesn't look too
difficult to include them as well but for a first shot, I didn't.
Regtested and bootstrapped on x86, power10, and aarch64.
Regtested on riscv64. I also regtested with riscv bits that basically
calls expand_vec_perm_const but there are still a few layering issue so
I'm not posting that patch just yet.
Regards
Robin
gcc/ChangeLog:
* tree-vect-slp.cc (vectorizable_slp_permutation_1): Cost
permutes directly.
(vect_optimize_slp_pass::change_layout_cost): Pass null as
cost_vec.
(vect_optimize_slp_pass::internal_node_cost): Likewise.
(vect_optimize_slp_pass::get_result_with_layout): Likewise.
(vect_optimize_slp_pass::materialize): Likewise.
(vectorizable_slp_permutation): Do not cost permutes here.
* tree-vect-stmts.cc (record_stmt_cost): Declare new overloads.
(perm_mask_for_reverse): Just build indices rather than their
tree.
(get_negative_load_store_type): Pass indices.
(vectorizable_bswap): Pass indices to costing.
(scan_store_can_perm_p): Add comment.
(vectorizable_store): Pass indices to costing.
(vectorizable_load): Likewise.
* tree-vectorizer.cc (vec_info::~vec_info): Call
free_perm_indices.
(vec_info::record_perm_indices): New function.
(vec_info::free_perm_indices): New function.
* tree-vectorizer.h (struct stmt_info_for_cost): Add perm field.
(perm_mask_for_reverse): Change return value and add indices
param.
(record_stmt_cost): Declare.
---
gcc/tree-vect-slp.cc | 35 +++++++++++++-------
gcc/tree-vect-stmts.cc | 74 ++++++++++++++++++++++++++++++++----------
gcc/tree-vectorizer.cc | 26 +++++++++++++++
gcc/tree-vectorizer.h | 28 +++++++++++++++-
4 files changed, 133 insertions(+), 30 deletions(-)
diff --git a/gcc/tree-vect-slp.cc b/gcc/tree-vect-slp.cc
index 0a28e958117..86ae779c568 100644
--- a/gcc/tree-vect-slp.cc
+++ b/gcc/tree-vect-slp.cc
@@ -65,7 +65,8 @@ static bool vect_transform_slp_perm_load_1 (vec_info *,
slp_tree,
unsigned * = nullptr,
bool = false);
static int vectorizable_slp_permutation_1 (vec_info *, gimple_stmt_iterator *,
- slp_tree, lane_permutation_t &,
+ slp_tree, stmt_vector_for_cost *,
+ lane_permutation_t &,
vec<slp_tree> &, bool);
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> &);
@@ -7134,7 +7135,8 @@ vect_optimize_slp_pass::change_layout_cost (slp_tree node,
perm.quick_push ({ 0, i });
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,
+ auto count = vectorizable_slp_permutation_1 (m_vinfo, nullptr, node,
+ nullptr, perm,
children, false);
if (count >= 0)
return MAX (count, 1);
@@ -7229,7 +7231,7 @@ vect_optimize_slp_pass::internal_node_cost (slp_tree
node, int in_layout_i,
change_vec_perm_layout (node, tmp_perm, in_layout_i, out_layout_i);
int count = vectorizable_slp_permutation_1 (m_vinfo, nullptr,
- node, tmp_perm,
+ node, nullptr, tmp_perm,
SLP_TREE_CHILDREN (node),
false);
if (count < 0)
@@ -7988,7 +7990,7 @@ vect_optimize_slp_pass::get_result_with_layout (slp_tree
node,
if (to_layout_i != 0)
vect_slp_permute (m_perms[to_layout_i], tmp_perm, true);
if (vectorizable_slp_permutation_1 (m_vinfo, nullptr, node,
- tmp_perm,
+ nullptr, tmp_perm,
SLP_TREE_CHILDREN (node),
false) >= 0)
num_inputs = SLP_TREE_CHILDREN (node).length ();
@@ -8085,7 +8087,7 @@ vect_optimize_slp_pass::materialize ()
tmp_perm.safe_splice (perm);
change_vec_perm_layout (node, tmp_perm, -1, layout_i);
if (vectorizable_slp_permutation_1 (m_vinfo, nullptr, node,
- tmp_perm,
+ nullptr, tmp_perm,
SLP_TREE_CHILDREN (node),
false) >= 0)
{
@@ -11767,7 +11769,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,
+ slp_tree node, stmt_vector_for_cost *cost_vec,
+ lane_permutation_t &perm,
vec<slp_tree> &children, bool dump_p)
{
tree vectype = SLP_TREE_VECTYPE (node);
@@ -12103,7 +12106,18 @@ vectorizable_slp_permutation_1 (vec_info *vinfo,
gimple_stmt_iterator *gsi,
}
if (!identity_p)
- nperms += CEIL (total_nelts, process_nelts) - (ei > last_ei);
+ {
+ int perms = CEIL (total_nelts, process_nelts) - (ei > last_ei);
+
+ nperms += perms;
+ if (!gsi && cost_vec)
+ {
+ const vec_perm_indices *cost_perm
+ = vinfo->record_perm_indices (indices);
+ record_stmt_cost (cost_vec, perms, vec_perm, node, vectype,
+ 0, cost_perm, vect_body);
+ }
+ }
if (gsi)
{
if (second_vec.first == -1U)
@@ -12149,17 +12163,14 @@ bool
vectorizable_slp_permutation (vec_info *vinfo, gimple_stmt_iterator *gsi,
slp_tree node, stmt_vector_for_cost *cost_vec)
{
- tree vectype = SLP_TREE_VECTYPE (node);
lane_permutation_t &perm = SLP_TREE_LANE_PERMUTATION (node);
- int nperms = vectorizable_slp_permutation_1 (vinfo, gsi, node, perm,
+ int nperms = vectorizable_slp_permutation_1 (vinfo, gsi, node,
+ cost_vec, perm,
SLP_TREE_CHILDREN (node),
dump_enabled_p ());
if (nperms < 0)
return false;
- if (!gsi && nperms != 0)
- record_stmt_cost (cost_vec, nperms, vec_perm, node, vectype, 0, vect_body);
-
return true;
}
diff --git a/gcc/tree-vect-stmts.cc b/gcc/tree-vect-stmts.cc
index 7a4596c337c..41648871648 100644
--- a/gcc/tree-vect-stmts.cc
+++ b/gcc/tree-vect-stmts.cc
@@ -93,6 +93,7 @@ record_stmt_cost (stmt_vector_for_cost *body_cost_vec, int
count,
enum vect_cost_for_stmt kind,
stmt_vec_info stmt_info, slp_tree node,
tree vectype, int misalign,
+ const vec_perm_indices *perm,
enum vect_cost_model_location where)
{
if ((kind == vector_load || kind == unaligned_load)
@@ -103,13 +104,24 @@ record_stmt_cost (stmt_vector_for_cost *body_cost_vec,
int count,
kind = vector_scatter_store;
stmt_info_for_cost si
- = { count, kind, where, stmt_info, node, vectype, misalign };
+ = { count, kind, where, stmt_info, node, vectype, misalign, perm };
body_cost_vec->safe_push (si);
return (unsigned)
(builtin_vectorization_cost (kind, vectype, misalign) * count);
}
+unsigned
+record_stmt_cost (stmt_vector_for_cost *body_cost_vec, int count,
+ enum vect_cost_for_stmt kind,
+ stmt_vec_info stmt_info, slp_tree node,
+ tree vectype, int misalign,
+ enum vect_cost_model_location where)
+{
+ return record_stmt_cost (body_cost_vec, count, kind, stmt_info, node,
+ vectype, misalign, nullptr, where);
+}
+
unsigned
record_stmt_cost (stmt_vector_for_cost *body_cost_vec, int count,
enum vect_cost_for_stmt kind, stmt_vec_info stmt_info,
@@ -131,6 +143,17 @@ record_stmt_cost (stmt_vector_for_cost *body_cost_vec, int
count,
vectype, misalign, where);
}
+unsigned
+record_stmt_cost (stmt_vector_for_cost *body_cost_vec, int count,
+ enum vect_cost_for_stmt kind, slp_tree node,
+ tree vectype, int misalign, const vec_perm_indices *perm,
+ enum vect_cost_model_location where)
+{
+ return record_stmt_cost (body_cost_vec, count, kind,
+ SLP_TREE_REPRESENTATIVE (node), node,
+ vectype, misalign, perm, where);
+}
+
unsigned
record_stmt_cost (stmt_vector_for_cost *body_cost_vec, int count,
enum vect_cost_for_stmt kind,
@@ -1867,10 +1890,11 @@ compare_step_with_zero (vec_info *vinfo, stmt_vec_info
stmt_info)
}
/* If the target supports a permute mask that reverses the elements in
- a vector of type VECTYPE, return that mask, otherwise return null. */
+ a vector of type VECTYPE, return true and set INDICES to the permute
+ indices. Otherwise return false. */
-tree
-perm_mask_for_reverse (tree vectype)
+bool
+perm_mask_for_reverse (tree vectype, vec_perm_indices &indices)
{
poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
@@ -1879,11 +1903,9 @@ perm_mask_for_reverse (tree vectype)
for (int i = 0; i < 3; ++i)
sel.quick_push (nunits - 1 - i);
- vec_perm_indices indices (sel, 1, nunits);
- if (!can_vec_perm_const_p (TYPE_MODE (vectype), TYPE_MODE (vectype),
- indices))
- return NULL_TREE;
- return vect_gen_perm_mask_checked (vectype, indices);
+ indices.new_vector (sel, 1, nunits);
+ return can_vec_perm_const_p (TYPE_MODE (vectype), TYPE_MODE (vectype),
+ indices);
}
/* A subroutine of get_load_store_type, with a subset of the same
@@ -1935,7 +1957,8 @@ get_negative_load_store_type (vec_info *vinfo,
return VMAT_CONTIGUOUS_DOWN;
}
- if (!perm_mask_for_reverse (vectype))
+ vec_perm_indices indices;
+ if (!perm_mask_for_reverse (vectype, indices))
{
if (dump_enabled_p ())
dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
@@ -3339,9 +3362,11 @@ vectorizable_bswap (vec_info *vinfo,
DUMP_VECT_SCOPE ("vectorizable_bswap");
record_stmt_cost (cost_vec,
1, vector_stmt, slp_node, 0, vect_prologue);
+
+ const vec_perm_indices *cost_perm = vinfo->record_perm_indices (indices);
record_stmt_cost (cost_vec,
vect_get_num_copies (vinfo, slp_node),
- vec_perm, slp_node, 0, vect_body);
+ vec_perm, slp_node, 0, cost_perm, vect_body);
return true;
}
@@ -7465,6 +7490,7 @@ scan_store_can_perm_p (tree vectype, tree init,
for (k = 0; j < nunits; ++j, ++k)
sel[j] = nunits + k;
}
+ /* ??? We should be costing this. */
vec_perm_indices indices (sel, i == units_log2 ? 1 : 2, nunits);
if (!can_vec_perm_const_p (vec_mode, vec_mode, indices))
{
@@ -9437,13 +9463,20 @@ vectorizable_store (vec_info *vinfo,
if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
{
+ vec_perm_indices indices;
+ perm_mask_for_reverse (vectype, indices);
if (costing_p)
- inside_cost += record_stmt_cost (cost_vec, 1, vec_perm,
- slp_node, 0, vect_body);
+ {
+ const vec_perm_indices *cost_indices
+ = vinfo->record_perm_indices (indices);
+ inside_cost += record_stmt_cost (cost_vec, 1, vec_perm,
+ slp_node, 0, cost_indices,
+ vect_body);
+ }
else
{
- tree perm_mask = perm_mask_for_reverse (vectype);
tree new_temp = make_ssa_name (vectype);
+ tree perm_mask = vect_gen_perm_mask_checked (vectype, indices);
/* Generate the permute statement. */
gimple *perm_stmt
@@ -12000,12 +12033,19 @@ vectorizable_load (vec_info *vinfo,
if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
{
+ vec_perm_indices indices;
+ perm_mask_for_reverse (vectype, indices);
if (costing_p)
- inside_cost = record_stmt_cost (cost_vec, 1, vec_perm,
- slp_node, 0, vect_body);
+ {
+ const vec_perm_indices *cost_indices
+ = vinfo->record_perm_indices (indices);
+ inside_cost += record_stmt_cost (cost_vec, 1, vec_perm,
+ slp_node, 0, cost_indices,
+ vect_body);
+ }
else
{
- tree perm_mask = perm_mask_for_reverse (vectype);
+ tree perm_mask = vect_gen_perm_mask_checked (vectype, indices);
new_temp = permute_vec_elements (vinfo, new_temp, new_temp,
perm_mask, stmt_info, gsi);
new_stmt = SSA_NAME_DEF_STMT (new_temp);
diff --git a/gcc/tree-vectorizer.cc b/gcc/tree-vectorizer.cc
index e089adddca0..a515b221800 100644
--- a/gcc/tree-vectorizer.cc
+++ b/gcc/tree-vectorizer.cc
@@ -73,6 +73,7 @@ along with GCC; see the file COPYING3. If not see
#include "tree-cfg.h"
#include "cfgloop.h"
#include "tree-vectorizer.h"
+#include "vec-perm-indices.h"
#include "tree-ssa-propagate.h"
#include "dbgcnt.h"
#include "tree-scalar-evolution.h"
@@ -481,6 +482,8 @@ vec_info::~vec_info ()
vect_free_slp_instance (instance);
free_stmt_vec_infos ();
+
+ free_perm_indices ();
}
vec_info_shared::vec_info_shared ()
@@ -766,6 +769,19 @@ vec_info::set_vinfo_for_stmt (gimple *stmt, stmt_vec_info
info, bool check_ro)
}
}
+/* Copy INDICES to the heap and return the pointer to it. */
+
+const vec_perm_indices *
+vec_info::record_perm_indices (const vec_perm_indices &indices)
+{
+ vec_perm_indices *perm = new vec_perm_indices;
+ perm->new_vector (indices.encoding (), indices.ninputs (),
+ indices.nelts_per_input ());
+
+ perm_indices.safe_push (perm);
+ return perm;
+}
+
/* Free the contents of stmt_vec_infos. */
void
@@ -777,6 +793,16 @@ vec_info::free_stmt_vec_infos (void)
stmt_vec_infos.release ();
}
+/* Same for perm_indices. */
+
+void
+vec_info::free_perm_indices (void)
+{
+ for (vec_perm_indices *perm : perm_indices)
+ delete perm;
+ perm_indices.release ();
+}
+
/* Free STMT_INFO. */
void
diff --git a/gcc/tree-vectorizer.h b/gcc/tree-vectorizer.h
index 7184f193b07..77ad0919a60 100644
--- a/gcc/tree-vectorizer.h
+++ b/gcc/tree-vectorizer.h
@@ -126,6 +126,7 @@ struct stmt_info_for_cost {
slp_tree node;
tree vectype;
int misalign;
+ const vec_perm_indices *perm;
};
typedef vec<stmt_info_for_cost> stmt_vector_for_cost;
@@ -658,6 +659,7 @@ public:
void replace_stmt (gimple_stmt_iterator *, stmt_vec_info, gimple *);
void insert_on_entry (stmt_vec_info, gimple *);
void insert_seq_on_entry (stmt_vec_info, gimple_seq);
+ const vec_perm_indices *record_perm_indices (const vec_perm_indices &);
/* The type of vectorization. */
vec_kind kind;
@@ -709,10 +711,14 @@ public:
and are reset when undoing patterns. */
gimple_seq inv_pattern_def_seq;
+ /* Tracks perm-indices pointers. */
+ auto_vec<vec_perm_indices *> perm_indices;
+
private:
stmt_vec_info new_stmt_vec_info (gimple *stmt);
void set_vinfo_for_stmt (gimple *, stmt_vec_info, bool = true);
void free_stmt_vec_infos ();
+ void free_perm_indices ();
void free_stmt_vec_info (stmt_vec_info);
};
@@ -2534,7 +2540,7 @@ extern bool vect_is_simple_use (vec_info *, slp_tree,
unsigned, slp_tree *,
enum vect_def_type *, tree *);
extern bool vect_maybe_update_slp_op_vectype (slp_tree, tree);
-extern tree perm_mask_for_reverse (tree);
+extern bool perm_mask_for_reverse (tree, vec_perm_indices &);
extern bool supportable_widening_operation (code_helper, tree, tree, bool,
code_helper*, code_helper*,
int*, vec<tree> *);
@@ -2553,6 +2559,10 @@ extern unsigned record_stmt_cost (stmt_vector_for_cost
*, int,
extern unsigned record_stmt_cost (stmt_vector_for_cost *, int,
enum vect_cost_for_stmt, slp_tree,
tree, int, enum vect_cost_model_location);
+extern unsigned record_stmt_cost (stmt_vector_for_cost *, int,
+ enum vect_cost_for_stmt, slp_tree,
+ tree, int, const vec_perm_indices *,
+ enum vect_cost_model_location);
extern unsigned record_stmt_cost (stmt_vector_for_cost *, int,
enum vect_cost_for_stmt,
enum vect_cost_model_location);
@@ -2560,6 +2570,11 @@ extern unsigned record_stmt_cost (stmt_vector_for_cost
*, int,
enum vect_cost_for_stmt, stmt_vec_info,
slp_tree, tree, int,
enum vect_cost_model_location);
+extern unsigned record_stmt_cost (stmt_vector_for_cost *, int,
+ enum vect_cost_for_stmt, stmt_vec_info,
+ slp_tree, tree, int,
+ const vec_perm_indices *,
+ enum vect_cost_model_location);
/* Overload of record_stmt_cost with VECTYPE derived from STMT_INFO. */
@@ -2583,6 +2598,17 @@ record_stmt_cost (stmt_vector_for_cost *body_cost_vec,
int count,
SLP_TREE_VECTYPE (node), misalign, where);
}
+/* Same with a permute. */
+inline unsigned
+record_stmt_cost (stmt_vector_for_cost *body_cost_vec, int count,
+ enum vect_cost_for_stmt kind, slp_tree node,
+ int misalign, const vec_perm_indices *perm,
+ enum vect_cost_model_location where)
+{
+ return record_stmt_cost (body_cost_vec, count, kind, node,
+ SLP_TREE_VECTYPE (node), misalign, perm, where);
+}
+
extern void vect_finish_replace_stmt (vec_info *, stmt_vec_info, gimple *);
extern void vect_finish_stmt_generation (vec_info *, stmt_vec_info, gimple *,
gimple_stmt_iterator *);
--
2.56.0