public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Philipp Tomsich <ptomsich@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc(refs/vendors/vrull/heads/slp-improvements)] Prorerly handle VIEW_CONVERT_EXPR in for NxM vector permute folding
Date: Wed, 17 Jan 2024 19:15:32 +0000 (GMT)	[thread overview]
Message-ID: <20240117191532.78D193858C60@sourceware.org> (raw)

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);
 	  }
       }
 }

             reply	other threads:[~2024-01-17 19:15 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-17 19:15 Philipp Tomsich [this message]
2024-01-23 20:58 Philipp Tomsich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240117191532.78D193858C60@sourceware.org \
    --to=ptomsich@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).