public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc(refs/vendors/vrull/heads/slp-improvements)] Prorerly handle VIEW_CONVERT_EXPR in for NxM vector permute folding
@ 2024-01-23 20:58 Philipp Tomsich
  0 siblings, 0 replies; 2+ messages in thread
From: Philipp Tomsich @ 2024-01-23 20:58 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:f6475f45816bc749c1f2da9deccca5fbbb842427

commit f6475f45816bc749c1f2da9deccca5fbbb842427
Author: Manolis Tsamis <manolis.tsamis@vrull.eu>
Date:   Fri Dec 15 17:14:05 2023 +0100

    Prorerly handle VIEW_CONVERT_EXPR in for NxM vector permute folding
    
    Ref #354

Diff:
---
 gcc/tree-vect-slp.cc | 76 +++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 61 insertions(+), 15 deletions(-)

diff --git a/gcc/tree-vect-slp.cc b/gcc/tree-vect-slp.cc
index 09686908f81..f629e4de5d3 100644
--- a/gcc/tree-vect-slp.cc
+++ b/gcc/tree-vect-slp.cc
@@ -8261,8 +8261,17 @@ get_perm_def (tree name, gassign **perm, gassign **def)
     return false;
 
   gassign *stmt2 = stmt1;
-  if (gimple_assign_rhs_code (stmt1) == VIEW_CONVERT_EXPR)
-    stmt2 = get_tree_def(TREE_OPERAND (gimple_assign_rhs1 (stmt1), 0), false);
+
+  /* Look through a view convert if it doesn't change the vector's shape.  */
+  if (gimple_assign_rhs_code (stmt1) == VIEW_CONVERT_EXPR) {
+    tree vc = gimple_assign_rhs1(stmt1);
+    if (element_precision (TREE_TYPE (vc))
+	== element_precision (TREE_TYPE (TREE_OPERAND (vc, 0))))
+      stmt2 = get_tree_def (TREE_OPERAND (gimple_assign_rhs1 (stmt1), 0),
+			    false);
+    else
+      return false;
+  }
 
   if (!stmt2)
     return false;
@@ -8538,8 +8547,8 @@ vect_slp_optimize_permutes (function *fun)
 	    || !VECTOR_CST_NELTS (gimple_assign_rhs3 (perms[1])).is_constant ())
 	  continue;
 
-	hash_set<gimple *> all_perms;
-	all_perms.add (stmt);
+	auto_vec<gimple *, 8> all_perms;
+	all_perms.safe_push (stmt);
 
 	tree def0_name = gimple_assign_lhs (defs[0]);
 	tree def1_name = gimple_assign_lhs (defs[1]);
@@ -8566,16 +8575,24 @@ vect_slp_optimize_permutes (function *fun)
 		  break;
 		}
 
-	      all_perms.add (use);
+	      all_perms.safe_push (use);
 	    }
 
 	if (!only_same_perms)
 	  continue;
 
-	for (hash_set <gimple *>::iterator iter = all_perms.begin ();
-	     iter != all_perms.end (); ++iter)
+	unsigned i;
+	gimple *iter;
+
+	FOR_EACH_VEC_ELT (all_perms, i, iter)
 	  {
-	    gassign *perm = dyn_cast <gassign *> (*iter);
+	    gassign *perm = dyn_cast <gassign *> (iter);
+
+	    /* Check if we have already processed this permutation.  */
+	    if (gimple_assign_rhs1 (perm) != def0_name
+		&& gimple_assign_rhs1 (perm) != def1_name)
+	      continue;
+
 	    tree mask = gimple_assign_rhs3 (perm);
 	    unsigned HOST_WIDE_INT nelts
 	      = VECTOR_CST_NELTS (mask).to_constant ();
@@ -8596,25 +8613,54 @@ vect_slp_optimize_permutes (function *fun)
 		if (lane < nelts)
 		  new_lane = TREE_INT_CST_LOW (VECTOR_CST_ELT (mask1, lane));
 		else
-		  new_lane = TREE_INT_CST_LOW (VECTOR_CST_ELT (mask2, lane - nelts));
+		  new_lane = TREE_INT_CST_LOW (VECTOR_CST_ELT (mask2,
+							       lane - nelts));
 
 		sel.quick_push (new_lane);
 	      }
 
 	    vec_perm_indices indices (sel, 2, nelts);
 	    tree vectype = TREE_TYPE (gimple_assign_lhs (perm));
-	    if (!can_vec_perm_const_p (TYPE_MODE (vectype), TYPE_MODE (vectype), indices))
+	    if (!can_vec_perm_const_p (TYPE_MODE (vectype),
+				       TYPE_MODE (vectype), indices))
 	      continue;
 
+	    gimple_stmt_iterator gsi_r = gsi_for_stmt (perm);
 	    tree new_mask = vect_gen_perm_mask_checked (vectype, indices);
+	    gimple* perm_replacement;
+	    bool has_view_convert = defs[0] != perms[0];
+
+	    if (has_view_convert)
+	      {
+		tree new_perm_lhs
+		  = make_ssa_name (TREE_TYPE (gimple_assign_lhs (perms[0])));
+
+		gimple* new_perm
+		  = gimple_build_assign (new_perm_lhs, VEC_PERM_EXPR,
+					 gimple_assign_rhs1 (perms[0]),
+					 gimple_assign_rhs2 (perms[0]),
+					 new_mask);
+
+		gsi_insert_before (&gsi_r, new_perm, GSI_SAME_STMT);
+
+		perm_replacement
+		  = gimple_build_assign (gimple_assign_lhs (perm),
+					 VIEW_CONVERT_EXPR,
+					 build1 (VIEW_CONVERT_EXPR,
+						 vectype, new_perm_lhs));
+	      }
+	    else
+	      perm_replacement
+		= gimple_build_assign (gimple_assign_lhs (perm), VEC_PERM_EXPR,
+				       gimple_assign_rhs1 (perms[0]),
+				       gimple_assign_rhs2 (perms[0]),
+				       new_mask);
+
+	    /* temporary solution for not re-processing these statements.  */
 	    gimple_assign_set_rhs1 (perm, gimple_assign_rhs1 (perms[0]));
 	    gimple_assign_set_rhs2 (perm, gimple_assign_rhs2 (perms[0]));
-	    gimple_assign_set_rhs3 (perm, new_mask);
-	    update_stmt (perm);
 
-	     if (dump_file) {
-		fprintf(dump_file, "  New ");  print_gimple_stmt(dump_file, perm, 0);
-	      }
+	    gsi_replace (&gsi_r, perm_replacement, false);
 	  }
       }
 }

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [gcc(refs/vendors/vrull/heads/slp-improvements)] Prorerly handle VIEW_CONVERT_EXPR in for NxM vector permute folding
@ 2024-01-17 19:15 Philipp Tomsich
  0 siblings, 0 replies; 2+ messages in thread
From: Philipp Tomsich @ 2024-01-17 19:15 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:eacb8fdaaeffa77fb090ce24a5bd3d0b0dc66cb5

commit eacb8fdaaeffa77fb090ce24a5bd3d0b0dc66cb5
Author: Manolis Tsamis <manolis.tsamis@vrull.eu>
Date:   Fri Dec 15 17:14:05 2023 +0100

    Prorerly handle VIEW_CONVERT_EXPR in for NxM vector permute folding
    
    Ref #354

Diff:
---
 gcc/tree-vect-slp.cc | 76 +++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 61 insertions(+), 15 deletions(-)

diff --git a/gcc/tree-vect-slp.cc b/gcc/tree-vect-slp.cc
index 67ca0ffc0da..c630ce5bbe0 100644
--- a/gcc/tree-vect-slp.cc
+++ b/gcc/tree-vect-slp.cc
@@ -8132,8 +8132,17 @@ get_perm_def (tree name, gassign **perm, gassign **def)
     return false;
 
   gassign *stmt2 = stmt1;
-  if (gimple_assign_rhs_code (stmt1) == VIEW_CONVERT_EXPR)
-    stmt2 = get_tree_def(TREE_OPERAND (gimple_assign_rhs1 (stmt1), 0), false);
+
+  /* Look through a view convert if it doesn't change the vector's shape.  */
+  if (gimple_assign_rhs_code (stmt1) == VIEW_CONVERT_EXPR) {
+    tree vc = gimple_assign_rhs1(stmt1);
+    if (element_precision (TREE_TYPE (vc))
+	== element_precision (TREE_TYPE (TREE_OPERAND (vc, 0))))
+      stmt2 = get_tree_def (TREE_OPERAND (gimple_assign_rhs1 (stmt1), 0),
+			    false);
+    else
+      return false;
+  }
 
   if (!stmt2)
     return false;
@@ -8409,8 +8418,8 @@ vect_slp_optimize_permutes (function *fun)
 	    || !VECTOR_CST_NELTS (gimple_assign_rhs3 (perms[1])).is_constant ())
 	  continue;
 
-	hash_set<gimple *> all_perms;
-	all_perms.add (stmt);
+	auto_vec<gimple *, 8> all_perms;
+	all_perms.safe_push (stmt);
 
 	tree def0_name = gimple_assign_lhs (defs[0]);
 	tree def1_name = gimple_assign_lhs (defs[1]);
@@ -8437,16 +8446,24 @@ vect_slp_optimize_permutes (function *fun)
 		  break;
 		}
 
-	      all_perms.add (use);
+	      all_perms.safe_push (use);
 	    }
 
 	if (!only_same_perms)
 	  continue;
 
-	for (hash_set <gimple *>::iterator iter = all_perms.begin ();
-	     iter != all_perms.end (); ++iter)
+	unsigned i;
+	gimple *iter;
+
+	FOR_EACH_VEC_ELT (all_perms, i, iter)
 	  {
-	    gassign *perm = dyn_cast <gassign *> (*iter);
+	    gassign *perm = dyn_cast <gassign *> (iter);
+
+	    /* Check if we have already processed this permutation.  */
+	    if (gimple_assign_rhs1 (perm) != def0_name
+		&& gimple_assign_rhs1 (perm) != def1_name)
+	      continue;
+
 	    tree mask = gimple_assign_rhs3 (perm);
 	    unsigned HOST_WIDE_INT nelts
 	      = VECTOR_CST_NELTS (mask).to_constant ();
@@ -8467,25 +8484,54 @@ vect_slp_optimize_permutes (function *fun)
 		if (lane < nelts)
 		  new_lane = TREE_INT_CST_LOW (VECTOR_CST_ELT (mask1, lane));
 		else
-		  new_lane = TREE_INT_CST_LOW (VECTOR_CST_ELT (mask2, lane - nelts));
+		  new_lane = TREE_INT_CST_LOW (VECTOR_CST_ELT (mask2,
+							       lane - nelts));
 
 		sel.quick_push (new_lane);
 	      }
 
 	    vec_perm_indices indices (sel, 2, nelts);
 	    tree vectype = TREE_TYPE (gimple_assign_lhs (perm));
-	    if (!can_vec_perm_const_p (TYPE_MODE (vectype), TYPE_MODE (vectype), indices))
+	    if (!can_vec_perm_const_p (TYPE_MODE (vectype),
+				       TYPE_MODE (vectype), indices))
 	      continue;
 
+	    gimple_stmt_iterator gsi_r = gsi_for_stmt (perm);
 	    tree new_mask = vect_gen_perm_mask_checked (vectype, indices);
+	    gimple* perm_replacement;
+	    bool has_view_convert = defs[0] != perms[0];
+
+	    if (has_view_convert)
+	      {
+		tree new_perm_lhs
+		  = make_ssa_name (TREE_TYPE (gimple_assign_lhs (perms[0])));
+
+		gimple* new_perm
+		  = gimple_build_assign (new_perm_lhs, VEC_PERM_EXPR,
+					 gimple_assign_rhs1 (perms[0]),
+					 gimple_assign_rhs2 (perms[0]),
+					 new_mask);
+
+		gsi_insert_before (&gsi_r, new_perm, GSI_SAME_STMT);
+
+		perm_replacement
+		  = gimple_build_assign (gimple_assign_lhs (perm),
+					 VIEW_CONVERT_EXPR,
+					 build1 (VIEW_CONVERT_EXPR,
+						 vectype, new_perm_lhs));
+	      }
+	    else
+	      perm_replacement
+		= gimple_build_assign (gimple_assign_lhs (perm), VEC_PERM_EXPR,
+				       gimple_assign_rhs1 (perms[0]),
+				       gimple_assign_rhs2 (perms[0]),
+				       new_mask);
+
+	    /* temporary solution for not re-processing these statements.  */
 	    gimple_assign_set_rhs1 (perm, gimple_assign_rhs1 (perms[0]));
 	    gimple_assign_set_rhs2 (perm, gimple_assign_rhs2 (perms[0]));
-	    gimple_assign_set_rhs3 (perm, new_mask);
-	    update_stmt (perm);
 
-	     if (dump_file) {
-		fprintf(dump_file, "  New ");  print_gimple_stmt(dump_file, perm, 0);
-	      }
+	    gsi_replace (&gsi_r, perm_replacement, false);
 	  }
       }
 }

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-01-23 20:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-23 20:58 [gcc(refs/vendors/vrull/heads/slp-improvements)] Prorerly handle VIEW_CONVERT_EXPR in for NxM vector permute folding Philipp Tomsich
  -- strict thread matches above, loose matches on Subject: below --
2024-01-17 19:15 Philipp Tomsich

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).