On 12/2/24 11:34 PM, Jin Ma wrote:
Since XTheadvector does not support vsetivli, vl needs to be put into
registers during the expand phase.

        PR 116593

gcc/ChangeLog:

        * config/riscv/riscv-vector-builtins.cc 
(function_expander::add_input_operand):
        Put const to GPR for vl.
        * config/riscv/thead-vector.md (@th_pred_vl_mov<mode>): New.

gcc/testsuite/ChangeLog:

        * g++.target/riscv/xtheadvector/pr116593.C: New test.
        * g++.target/riscv/xtheadvector/xtheadvector.exp: New test.

Reported-by: nihui <shuizhuyuan...@gmail.com>
---
  gcc/config/riscv/riscv-vector-builtins.cc     | 18 +++++++-
  gcc/config/riscv/thead-vector.md              | 13 ++++++
  .../g++.target/riscv/xtheadvector/pr116593.C  | 45 +++++++++++++++++++
  .../riscv/xtheadvector/xtheadvector.exp       | 37 +++++++++++++++
  4 files changed, 112 insertions(+), 1 deletion(-)
  create mode 100644 gcc/testsuite/g++.target/riscv/xtheadvector/pr116593.C
  create mode 100644 
gcc/testsuite/g++.target/riscv/xtheadvector/xtheadvector.exp

diff --git a/gcc/config/riscv/riscv-vector-builtins.cc 
b/gcc/config/riscv/riscv-vector-builtins.cc
index b9b9d33adab6..cced0461a7bb 100644
--- a/gcc/config/riscv/riscv-vector-builtins.cc
+++ b/gcc/config/riscv/riscv-vector-builtins.cc
@@ -4089,7 +4089,23 @@ function_expander::add_input_operand (unsigned argno)
  {
    tree arg = CALL_EXPR_ARG (exp, argno);
    rtx x = expand_normal (arg);
-  add_input_operand (TYPE_MODE (TREE_TYPE (arg)), x);
+
+  /* Since the parameter vl of XTheadVector does not support
+     immediate numbers, we need to put it in the register
+     in advance.  */
+  if (TARGET_XTHEADVECTOR
+      && CONST_INT_P (x)
+      && base->apply_vl_p ()
+      && argno == (unsigned) (call_expr_nargs (exp) - 1)
+      && x != CONST0_RTX (GET_MODE (x)))
+    {
+      rtx tmp = gen_reg_rtx (word_mode);
+      /* Use UNSPEC to avoid being optimized before vsetvl pass.  */
+      emit_insn (gen_th_pred_vl_mov (word_mode, tmp, x));
+      add_input_operand (TYPE_MODE (TREE_TYPE (arg)), tmp);
+    }
+  else
+    add_input_operand (TYPE_MODE (TREE_TYPE (arg)), x);
  }
So I would just do:


tmp = force_reg (word_mode, x);
add_input_operand (TYPE_MODE (TREE_TYPE (arg)), tmp);

In the thead specific code. That generates the initial code correctly. At that point we just need to make sure nothing like combine, cprop, etc propagates the constant into the vsetvl. The way to prevent that is with the operand predicates on the vsetvl insns.


jeff

Reply via email to