public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Juzhe-Zhong <juzhe.zhong@rivai.ai>
To: gcc-patches@gcc.gnu.org
Cc: richard.sandiford@arm.com, rguenther@suse.de,
	Juzhe-Zhong <juzhe.zhong@rivai.ai>
Subject: [PATCH] VECT: Clear LOOP_VINFO_USING_SELECT_VL_P when loop is not partial vectorized
Date: Thu, 16 Nov 2023 12:09:07 +0800	[thread overview]
Message-ID: <20231116040907.1647406-1-juzhe.zhong@rivai.ai> (raw)

This patch fixes ICE:
https://godbolt.org/z/z8T6o6qov

<source>: In function 'b':
<source>:2:6: error: missing definition
    2 | void b() {
      |      ^
for SSA_NAME: loop_len_8 in statement:
_1 = -loop_len_8;
during GIMPLE pass: vect
<source>:2:6: internal compiler error: verify_ssa failed
0x7f1b56331082 __libc_start_main
	???:0
Please submit a full bug report, with preprocessed source (by using -freport-bug).
Please include the complete backtrace with any bug report.
See <https://gcc.gnu.org/bugs/> for instructions.
Compiler returned: 1

The root cause is we generate such IR in vectorization:

  _1 = -loop_len_8;
  vect_cst__11 = {_1, _1};
  _18 = vect_vec_iv_.6_14 + vect_cst__11;

loop_len_8 is uninitialized value.

The IR _18 = vect_vec_iv_.6_14 + vect_cst__11; is generated because of we are adding induction variable with
the result of SELECT_VL instead of VF.

The code is:

  else if (LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo))
    {
      /* When we're using loop_len produced by SELEC_VL, the non-final
	 iterations are not always processing VF elements.  So vectorize
	 induction variable instead of

	   _21 = vect_vec_iv_.6_22 + { VF, ... };

	 We should generate:

	   _35 = .SELECT_VL (ivtmp_33, VF);
	   vect_cst__22 = [vec_duplicate_expr] _35;
	   _21 = vect_vec_iv_.6_22 + vect_cst__22;  */
      gcc_assert (!slp_node);
      gimple_seq seq = NULL;
      vec_loop_lens *lens = &LOOP_VINFO_LENS (loop_vinfo);
      tree len = vect_get_loop_len (loop_vinfo, NULL, lens, 1, vectype, 0, 0);
      expr = force_gimple_operand (fold_convert (TREE_TYPE (step_expr),
						 unshare_expr (len)),
				   &seq, true, NULL_TREE);
      new_name = gimple_build (&seq, MULT_EXPR, TREE_TYPE (step_expr), expr,
			       step_expr);
      gsi_insert_seq_before (&si, seq, GSI_SAME_STMT);
      step_iv_si = &si;
    }

LOOP_VINFO_USING_SELECT_VL_P is set before loop vectorization analysis so we don't know whether it is partial
vectorization or not but the induction variable depends on SELECT_VL_P is true.

So update SELECT_VL_P as false when it is not partial vectorization.

	PR middle-end/112554

gcc/ChangeLog:

	* tree-vect-loop.cc (vect_determine_partial_vectors_and_peeling): Clear SELECT_VL_P for non-partial vectorization.

gcc/testsuite/ChangeLog:

	* gcc.target/riscv/rvv/autovec/pr112554.c: New test.

---
 .../gcc.target/riscv/rvv/autovec/pr112554.c         | 12 ++++++++++++
 gcc/tree-vect-loop.cc                               | 13 +++++++++++++
 2 files changed, 25 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/riscv/rvv/autovec/pr112554.c

diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr112554.c b/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr112554.c
new file mode 100644
index 00000000000..4afa7c2b15c
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/rvv/autovec/pr112554.c
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gcv -mabi=lp64d -O3 --param=riscv-autovec-preference=fixed-vlmax" } */
+
+int a;
+void b() {
+  unsigned long c = 18446744073709551612UL;
+d:
+  --c;
+  a ^= c;
+  if (c)
+    goto d;
+}
diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc
index fb8d999ee6b..3f59139cb01 100644
--- a/gcc/tree-vect-loop.cc
+++ b/gcc/tree-vect-loop.cc
@@ -2657,6 +2657,19 @@ vect_determine_partial_vectors_and_peeling (loop_vec_info loop_vinfo)
     = (!LOOP_VINFO_USING_PARTIAL_VECTORS_P (loop_vinfo)
        && need_peeling_or_partial_vectors_p);
 
+  /* We set LOOP_VINFO_USING_SELECT_VL_P as true before loop vectorization
+     analysis that we don't know whether the loop is vectorized by partial
+     vectors (More details see tree-vect-loop-manip.cc).
+
+     However, SELECT_VL vectorizaton style should only applied on partial
+     vectorization since SELECT_VL is the GIMPLE IR that calculates the
+     number of elements to be process for each iteration.
+
+     After loop vectorization analysis, Clear LOOP_VINFO_USING_SELECT_VL_P
+     if it is not partial vectorized loop.  */
+  if (!LOOP_VINFO_USING_PARTIAL_VECTORS_P (loop_vinfo))
+    LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo) = false;
+
   return opt_result::success ();
 }
 
-- 
2.36.3


             reply	other threads:[~2023-11-16  4:09 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-16  4:09 Juzhe-Zhong [this message]
2023-11-16  6:52 ` Richard Biener

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=20231116040907.1647406-1-juzhe.zhong@rivai.ai \
    --to=juzhe.zhong@rivai.ai \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=rguenther@suse.de \
    --cc=richard.sandiford@arm.com \
    /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).