From: Robin Dapp <[email protected]>
This patch implements cond_len_index_first_active and
extract_first_active in order to support the scalar-branch early-break
approach for RVV. At the same time, it removes the old
vcond_mask_len and vector cbranch patterns that are not needed anymore
and adjusts test expectations.
The testsuite is not regression free. I noticed two issues that other
targets likely didn't see yet:
- We set wrong range information for niters. This is a one-line fix I
will send separately.
- It can happen that scalar prolog peeling for alignment peels the
whole loop, leaving nothing for the vectorized loop that runs at
len = 0. Then we attempt to extract a live IV value at index
len - 1 = -1, resulting in garbage and execution failures.
I think we can prevent that by skipping the main loop altogether just
like skip_vect already does in the peeling code. That requires some
plumbing and I don't find the CFG adjustments during peeling terribly
straightforward. Another option would be to remember all prolog live
values and insert appropriate skip-phis for them, letting the main loop
run at len = 0. Not sure yet which is better but I'm leaning towards
skipping the loop.
Another possible optimization I noticed is that we sometimes vectorize
the IV, only to live extract a scalar from it at the main exit. At
least for our simple IVs it should be possible to "statically" compute
the proper value just like vect_update_ivs_after_vectorizer is doing it.
gcc/ChangeLog:
* config/riscv/autovec-opt.md
(*vcond_mask_len_popcount_<VB_VLS:mode><P:mode>):
Remove.
* config/riscv/autovec.md (cond_len_index_first_active_<mode>):
New.
(extract_first_active_<mode>): New.
(vcond_mask_len_<mode>): Remove.
(cbranch<mode>4): Remove.
* config/riscv/riscv-protos.h (expand_cond_len_index_first_active):
Declare.
* config/riscv/riscv-v.cc (expand_cond_len_index_first_active):
New.
gcc/testsuite/ChangeLog:
* gcc.dg/vect/vect-early-break-no-epilog_3.c: Expect no
epilog.
* gcc.dg/vect/vect-early-break-no-epilog_4.c: Ditto.
* gcc.dg/vect/vect-early-break-no-epilog_5.c: Ditto.
* gcc.dg/vect/vect-early-break-no-epilog_6.c: Ditto.
* gcc.target/riscv/rvv/autovec/early-break-1.c: Expect vfirst.
* gcc.target/riscv/rvv/autovec/early-break-2.c: Ditto.
* gcc.target/riscv/rvv/autovec/early-break-3.c: Ditto.
* gcc.target/riscv/rvv/autovec/early-break-4.c: Ditto.
---
gcc/config/riscv/autovec-opt.md | 32 --------
gcc/config/riscv/autovec.md | 73 +++----------------
gcc/config/riscv/riscv-protos.h | 1 +
gcc/config/riscv/riscv-v.cc | 37 ++++++++++
.../vect/vect-early-break-no-epilog_3.c | 4 +-
.../vect/vect-early-break-no-epilog_4.c | 4 +-
.../vect/vect-early-break-no-epilog_5.c | 4 +-
.../vect/vect-early-break-no-epilog_6.c | 4 +-
.../gcc.dg/vect/vect-uncounted-run_1.c | 3 +-
gcc/testsuite/gcc.dg/vect/vect-uncounted_1.c | 3 +-
.../riscv/rvv/autovec/early-break-1.c | 2 +-
.../riscv/rvv/autovec/early-break-2.c | 2 +-
.../riscv/rvv/autovec/early-break-3.c | 2 +-
.../riscv/rvv/autovec/early-break-4.c | 2 +-
14 files changed, 66 insertions(+), 107 deletions(-)
diff --git a/gcc/config/riscv/autovec-opt.md b/gcc/config/riscv/autovec-opt.md
index e6cd23e8399..e969db1b7e8 100644
--- a/gcc/config/riscv/autovec-opt.md
+++ b/gcc/config/riscv/autovec-opt.md
@@ -1536,38 +1536,6 @@ (define_insn_and_split "*n<optab><mode>"
}
[(set_attr "type" "vmalu")])
-;; Optimization pattern for early break auto-vectorization
-;; vcond_mask_len (mask, ones, zeros, len, bias) + vlmax popcount
-;; -> non vlmax popcount (mask, len)
-(define_insn_and_split "*vcond_mask_len_popcount_<VB_VLS:mode><P:mode>"
- [(set (match_operand:P 0 "register_operand")
- (popcount:P
- (unspec:VB_VLS [
- (unspec:VB_VLS [
- (match_operand:VB_VLS 1 "register_operand")
- (match_operand:VB_VLS 2 "const_1_operand")
- (match_operand:VB_VLS 3 "const_0_operand")
- (match_operand 4 "autovec_length_operand")
- (match_operand 5 "const_0_operand")] UNSPEC_SELECT_MASK)
- (match_operand 6 "autovec_length_operand")
- (const_int 1)
- (reg:SI VL_REGNUM)
- (reg:SI VTYPE_REGNUM)] UNSPEC_VPREDICATE)))]
- "TARGET_VECTOR
- && can_create_pseudo_p ()
- && riscv_vector::get_vector_mode (Pmode, GET_MODE_NUNITS
(<VB_VLS:MODE>mode)).exists ()"
- "#"
- "&& 1"
- [(const_int 0)]
- {
- riscv_vector::emit_nonvlmax_insn (
- code_for_pred_popcount (<VB_VLS:MODE>mode, Pmode),
- riscv_vector::CPOP_OP,
- operands, operands[4]);
- DONE;
- }
- [(set_attr "type" "vector")])
-
;; vzext.vf2 + vsll = vwsll.
(define_insn_and_split "*vwsll_zext1_<mode>"
[(set (match_operand:VWEXTI 0 "register_operand" "=vr
")
diff --git a/gcc/config/riscv/autovec.md b/gcc/config/riscv/autovec.md
index 607b9891405..2ae3b20f378 100644
--- a/gcc/config/riscv/autovec.md
+++ b/gcc/config/riscv/autovec.md
@@ -2561,6 +2561,18 @@ (define_expand "len_fold_extract_last_<mode>"
DONE;
})
+(define_expand "cond_len_index_first_active_<mode>"
+ [(match_operand:DI 0 "register_operand")
+ (match_operand:VB_VLS 1 "vector_mask_operand")
+ (match_operand:VB_VLS 2 "register_operand")
+ (match_operand 3 "autovec_length_operand")
+ (match_operand 4 "const_0_operand")]
+ "TARGET_VECTOR"
+ {
+ riscv_vector::expand_cond_len_index_first_active (operands);
+ DONE;
+ })
+
;; -------------------------------------------------------------------------
;; ---- [INT] Average.
;; -------------------------------------------------------------------------
@@ -2986,67 +2998,6 @@ (define_expand "sstrunc<mode><v_oct_trunc>2"
}
)
-;; =========================================================================
-;; == Early break auto-vectorization patterns
-;; =========================================================================
-
-;; vcond_mask_len (mask, 1s, 0s, len, bias)
-;; => mask[i] = mask[i] && i < len ? 1 : 0
-(define_insn_and_split "vcond_mask_len_<mode>"
- [(set (match_operand:VB 0 "register_operand")
- (unspec: VB [
- (match_operand:VB 1 "register_operand")
- (match_operand:VB 2 "const_1_operand")
- (match_operand:VB 3 "const_0_operand")
- (match_operand 4 "autovec_length_operand")
- (match_operand 5 "const_0_operand")] UNSPEC_SELECT_MASK))]
- "TARGET_VECTOR
- && can_create_pseudo_p ()
- && riscv_vector::get_vector_mode (Pmode, GET_MODE_NUNITS
(<MODE>mode)).exists ()"
- "#"
- "&& 1"
- [(const_int 0)]
- {
- machine_mode mode = riscv_vector::get_vector_mode (Pmode,
- GET_MODE_NUNITS (<MODE>mode)).require ();
- rtx reg = gen_reg_rtx (mode);
- riscv_vector::expand_vec_series (reg, const0_rtx, const1_rtx);
- rtx dup_rtx = gen_rtx_VEC_DUPLICATE (mode, operands[4]);
- insn_code icode = code_for_pred_cmp_scalar (mode);
- rtx cmp = gen_rtx_fmt_ee (LTU, <MODE>mode, reg, dup_rtx);
- rtx ops[] = {operands[0], operands[1], operands[1], cmp, reg, operands[4]};
- emit_vlmax_insn (icode, riscv_vector::COMPARE_OP_MU, ops);
- DONE;
- }
- [(set_attr "type" "vector")])
-
-;; cbranch
-(define_expand "cbranch<mode>4"
- [(set (pc)
- (if_then_else
- (match_operator 0 "equality_operator"
- [(match_operand:VB_VLS 1 "register_operand")
- (match_operand:VB_VLS 2 "reg_or_0_operand")])
- (label_ref (match_operand 3 ""))
- (pc)))]
- "TARGET_VECTOR"
- {
- rtx pred;
- if (operands[2] == CONST0_RTX (<MODE>mode))
- pred = operands[1];
- else
- pred = expand_binop (<MODE>mode, xor_optab, operands[1],
- operands[2], NULL_RTX, 0,
- OPTAB_DIRECT);
- rtx reg = gen_reg_rtx (Pmode);
- rtx cpop_ops[] = {reg, pred};
- emit_vlmax_insn (code_for_pred_popcount (<MODE>mode, Pmode),
- riscv_vector::CPOP_OP, cpop_ops);
- operands[1] = reg;
- operands[2] = const0_rtx;
- }
-)
-
;; -------------------------------------------------------------------------
;; - vrol.vv vror.vv
;; -------------------------------------------------------------------------
diff --git a/gcc/config/riscv/riscv-protos.h b/gcc/config/riscv/riscv-protos.h
index 78d6c5ca0d6..ae3d03b0dc4 100644
--- a/gcc/config/riscv/riscv-protos.h
+++ b/gcc/config/riscv/riscv-protos.h
@@ -760,6 +760,7 @@ void expand_cond_len_ternop (unsigned, rtx *);
void prepare_ternary_operands (rtx *);
void expand_lanes_load_store (rtx *, bool);
void expand_fold_extract_last (rtx *);
+void expand_cond_len_index_first_active (rtx *);
void expand_cond_unop (unsigned, rtx *);
void expand_cond_binop (unsigned, rtx *);
void expand_cond_ternop (unsigned, rtx *);
diff --git a/gcc/config/riscv/riscv-v.cc b/gcc/config/riscv/riscv-v.cc
index 6ae806e01aa..80c4a8f8192 100644
--- a/gcc/config/riscv/riscv-v.cc
+++ b/gcc/config/riscv/riscv-v.cc
@@ -5237,6 +5237,43 @@ expand_fold_extract_last (rtx *ops)
emit_label (end_label);
}
+/* Expand to vfirst.m. */
+void
+expand_cond_len_index_first_active (rtx *ops)
+{
+ rtx dst = ops[0];
+ rtx mask = ops[1];
+ rtx pred = ops[2];
+ rtx len = ops[3];
+ machine_mode mode = GET_MODE (pred);
+
+ gcc_assert (ops[4] == const0_rtx);
+
+ rtx idx = gen_reg_rtx (Pmode);
+ insn_code icode = code_for_pred_ffs (mode, Pmode);
+ unsigned flags = CPOP_OP;
+ rtx ffs_ops[3];
+ if (vector_all_trues_mask_operand (mask, mode))
+ {
+ ffs_ops[0] = idx;
+ ffs_ops[1] = pred;
+ }
+ else
+ {
+ flags &= ~USE_ALL_TRUES_MASK_P;
+ ffs_ops[0] = idx;
+ ffs_ops[1] = force_reg (mode, mask);
+ ffs_ops[2] = pred;
+ }
+
+ if (is_vlmax_len_p (mode, len))
+ emit_vlmax_insn (icode, flags, ffs_ops);
+ else
+ emit_nonvlmax_insn (icode, flags, ffs_ops, len);
+
+ convert_move (dst, idx, false);
+}
+
/* Return true if the LMUL of comparison less than or equal to one. */
bool
cmp_lmul_le_one (machine_mode mode)
diff --git a/gcc/testsuite/gcc.dg/vect/vect-early-break-no-epilog_3.c
b/gcc/testsuite/gcc.dg/vect/vect-early-break-no-epilog_3.c
index 633f5339be8..dfab2a571a2 100644
--- a/gcc/testsuite/gcc.dg/vect/vect-early-break-no-epilog_3.c
+++ b/gcc/testsuite/gcc.dg/vect/vect-early-break-no-epilog_3.c
@@ -18,5 +18,5 @@ int foo (void)
}
/* { dg-final { scan-tree-dump "LOOP VECTORIZED" "vect" } } */
-/* { dg-final { scan-tree-dump "early break does not require epilog" "vect" {
target aarch64*-*-* } } } */
-/* { dg-final { scan-tree-dump-not "early break does not require epilog"
"vect" { target { ! aarch64*-*-* } } } } */
+/* { dg-final { scan-tree-dump "early break does not require epilog" "vect" {
target { aarch64*-*-* riscv*-*-* } } } } */
+/* { dg-final { scan-tree-dump-not "early break does not require epilog"
"vect" { target { ! { aarch64*-*-* riscv*-*-* } } } } } */
diff --git a/gcc/testsuite/gcc.dg/vect/vect-early-break-no-epilog_4.c
b/gcc/testsuite/gcc.dg/vect/vect-early-break-no-epilog_4.c
index a2155015b1a..57a7083ff80 100644
--- a/gcc/testsuite/gcc.dg/vect/vect-early-break-no-epilog_4.c
+++ b/gcc/testsuite/gcc.dg/vect/vect-early-break-no-epilog_4.c
@@ -18,5 +18,5 @@ int foo (void)
}
/* { dg-final { scan-tree-dump "LOOP VECTORIZED" "vect" } } */
-/* { dg-final { scan-tree-dump "early break does not require epilog" "vect" {
target aarch64*-*-* } } } */
-/* { dg-final { scan-tree-dump-not "early break does not require epilog"
"vect" { target { ! aarch64*-*-* } } } } */
+/* { dg-final { scan-tree-dump "early break does not require epilog" "vect" {
target { aarch64*-*-* riscv*-*-* } } } } */
+/* { dg-final { scan-tree-dump-not "early break does not require epilog"
"vect" { target { ! { aarch64*-*-* riscv*-*-* } } } } } */
diff --git a/gcc/testsuite/gcc.dg/vect/vect-early-break-no-epilog_5.c
b/gcc/testsuite/gcc.dg/vect/vect-early-break-no-epilog_5.c
index d5585ceca23..2270060a7e0 100644
--- a/gcc/testsuite/gcc.dg/vect/vect-early-break-no-epilog_5.c
+++ b/gcc/testsuite/gcc.dg/vect/vect-early-break-no-epilog_5.c
@@ -18,5 +18,5 @@ short foo (void)
}
/* { dg-final { scan-tree-dump "LOOP VECTORIZED" "vect" } } */
-/* { dg-final { scan-tree-dump "early break does not require epilog" "vect" {
target aarch64*-*-* } } } */
-/* { dg-final { scan-tree-dump-not "early break does not require epilog"
"vect" { target { ! aarch64*-*-* } } } } */
+/* { dg-final { scan-tree-dump "early break does not require epilog" "vect" {
target { aarch64*-*-* riscv*-*-* } } } } */
+/* { dg-final { scan-tree-dump-not "early break does not require epilog"
"vect" { target { ! { aarch64*-*-* riscv*-*-* } } } } } */
diff --git a/gcc/testsuite/gcc.dg/vect/vect-early-break-no-epilog_6.c
b/gcc/testsuite/gcc.dg/vect/vect-early-break-no-epilog_6.c
index c89272d3f44..7dee06171b0 100644
--- a/gcc/testsuite/gcc.dg/vect/vect-early-break-no-epilog_6.c
+++ b/gcc/testsuite/gcc.dg/vect/vect-early-break-no-epilog_6.c
@@ -18,5 +18,5 @@ short foo (void)
}
/* { dg-final { scan-tree-dump "LOOP VECTORIZED" "vect" } } */
-/* { dg-final { scan-tree-dump "early break does not require epilog" "vect" {
target aarch64*-*-* } } } */
-/* { dg-final { scan-tree-dump-not "early break does not require epilog"
"vect" { target { ! aarch64*-*-* } } } } */
+/* { dg-final { scan-tree-dump "early break does not require epilog" "vect" {
target { aarch64*-*-* riscv*-*-* } } } } */
+/* { dg-final { scan-tree-dump-not "early break does not require epilog"
"vect" { target { ! { aarch64*-*-* riscv*-*-* } } } } } */
diff --git a/gcc/testsuite/gcc.dg/vect/vect-uncounted-run_1.c
b/gcc/testsuite/gcc.dg/vect/vect-uncounted-run_1.c
index 71c7bb5329f..8651ec2a031 100644
--- a/gcc/testsuite/gcc.dg/vect/vect-uncounted-run_1.c
+++ b/gcc/testsuite/gcc.dg/vect/vect-uncounted-run_1.c
@@ -52,6 +52,7 @@ main ()
/* { dg-final { scan-tree-dump "vectorized 1 loops in function" "vect" } } */
/* AArch64 extracts the IV value directly from the first active vector lane.
*/
/* { dg-final { scan-tree-dump {_[0-9_]+ = \.EXTRACT_FIRST_ACTIVE
\(vexit_[0-9_]+, vect_i_[0-9_.]+\)} "vect" { target aarch64*-*-* } } } */
+/* { dg-final { scan-tree-dump {_[0-9_]+ = \.VEC_EXTRACT \(vect_i_[0-9_.]+,
vexit_[0-9_]+\)} "vect" { target riscv*-*-* } } } */
/* Other targets still reset the scalar IV to repeat the last vector
iteration in the scalar epilogue loop. */
-/* { dg-final { scan-tree-dump {_[0-9_]+ = BIT_FIELD_REF <vect_i_[0-9_.]+,
[0-9]+, 0>} "vect" { target { ! aarch64*-*-* } } } } */
+/* { dg-final { scan-tree-dump {_[0-9_]+ = BIT_FIELD_REF <vect_i_[0-9_.]+,
[0-9]+, 0>} "vect" { target { ! { aarch64*-*-* riscv*-*-* } } } } } */
diff --git a/gcc/testsuite/gcc.dg/vect/vect-uncounted_1.c
b/gcc/testsuite/gcc.dg/vect/vect-uncounted_1.c
index 243e1d60af9..1e91bd6504c 100644
--- a/gcc/testsuite/gcc.dg/vect/vect-uncounted_1.c
+++ b/gcc/testsuite/gcc.dg/vect/vect-uncounted_1.c
@@ -20,6 +20,7 @@ foo (int *haystack, int needle)
/* { dg-final { scan-tree-dump "vectorized 1 loops in function" "vect" } } */
/* AArch64 extracts the IV value directly from the first active vector lane.
*/
/* { dg-final { scan-tree-dump {_[0-9_]+ = \.EXTRACT_FIRST_ACTIVE
\(vexit_[0-9_]+, vect_i_[0-9_.]+\)} "vect" { target aarch64*-*-* } } } */
+/* { dg-final { scan-tree-dump {_[0-9_]+ = \.VEC_EXTRACT \(vect_i_[0-9_.]+,
vexit_[0-9_]+\)} "vect" { target riscv*-*-* } } } */
/* Other targets still reset the scalar IV to repeat the last vector
iteration in the scalar epilogue loop. */
-/* { dg-final { scan-tree-dump {_[0-9_]+ = BIT_FIELD_REF <vect_i_[0-9_.]+,
[0-9]+, 0>} "vect" { target { ! aarch64*-*-* } } } } */
+/* { dg-final { scan-tree-dump {_[0-9_]+ = BIT_FIELD_REF <vect_i_[0-9_.]+,
[0-9]+, 0>} "vect" { target { ! { aarch64*-*-* riscv*-*-* } } } } } */
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/early-break-1.c
b/gcc/testsuite/gcc.target/riscv/rvv/autovec/early-break-1.c
index f70979e81f1..98e06469917 100644
--- a/gcc/testsuite/gcc.target/riscv/rvv/autovec/early-break-1.c
+++ b/gcc/testsuite/gcc.target/riscv/rvv/autovec/early-break-1.c
@@ -11,7 +11,7 @@ unsigned vect_b[N];
** test:
** ...
** vmsltu\.vv\s+v[0-9]+\s*,v[0-9]+,\s*v[0-9]+
-** vcpop\.m\s+[atx][0-9]+\s*,v[0-9]+
+** vfirst\.m\s+[atx][0-9]+\s*,v[0-9]+
** ...
*/
unsigned test (unsigned x, int n)
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/early-break-2.c
b/gcc/testsuite/gcc.target/riscv/rvv/autovec/early-break-2.c
index d405783d2c4..c3cf0ac1cad 100644
--- a/gcc/testsuite/gcc.target/riscv/rvv/autovec/early-break-2.c
+++ b/gcc/testsuite/gcc.target/riscv/rvv/autovec/early-break-2.c
@@ -11,7 +11,7 @@ unsigned vect_b[N];
** test:
** ...
** vmsltu\.vv\s+v[0-9]+\s*,v[0-9]+,\s*v[0-9]+
-** vcpop\.m\s+[atx][0-9]+\s*,v[0-9]+
+** vfirst\.m\s+[atx][0-9]+\s*,v[0-9]+
** ...
*/
unsigned test (unsigned limit, int n)
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/early-break-3.c
b/gcc/testsuite/gcc.target/riscv/rvv/autovec/early-break-3.c
index 8fff924c28e..d7dbf441064 100644
--- a/gcc/testsuite/gcc.target/riscv/rvv/autovec/early-break-3.c
+++ b/gcc/testsuite/gcc.target/riscv/rvv/autovec/early-break-3.c
@@ -1,7 +1,7 @@
/* { dg-do compile } */
/* { dg-additional-options "-O3 -march=rv64gcv -mabi=lp64d -ftree-vectorize
-std=gnu99" } */
/* { dg-final { scan-assembler-times {vms[lgen][teq]\.v[vxi]} 6 } } */
-/* { dg-final { scan-assembler-times {vcpop\.m} 6 } } */
+/* { dg-final { scan-assembler-times {vfirst\.m} 6 } } */
#define N 640
int a[N] = {0};
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/autovec/early-break-4.c
b/gcc/testsuite/gcc.target/riscv/rvv/autovec/early-break-4.c
index 2549778713c..4ebf52a8b8d 100644
--- a/gcc/testsuite/gcc.target/riscv/rvv/autovec/early-break-4.c
+++ b/gcc/testsuite/gcc.target/riscv/rvv/autovec/early-break-4.c
@@ -1,7 +1,7 @@
/* { dg-do compile } */
/* { dg-additional-options "-O3 -march=rv64gcv -mabi=lp64d -ftree-vectorize
-std=gnu99" } */
/* { dg-final { scan-assembler-times {vms[lgen][teq]\.v[vxi]} 6 } } */
-/* { dg-final { scan-assembler-times {vcpop\.m} 6 } } */
+/* { dg-final { scan-assembler-times {vfirst\.m} 6 } } */
#define N 640
int a[N] = {0};
--
2.56.0