Skip to content

Commit 05304cf

Browse files
lhtinLiaoshihua
authored andcommitted
RISC-V: Add conditional sqrt autovec pattern
This patch adds a combined pattern for combining vfsqrt.v and vcond_mask. gcc/ChangeLog: * config/riscv/autovec-opt.md (*cond_<optab><mode>): Add sqrt + vcond_mask combine pattern. * config/riscv/autovec.md (<optab><mode>2): Change define_expand to define_insn_and_split. gcc/testsuite/ChangeLog: * gcc.target/riscv/rvv/autovec/cond/cond_sqrt-1.c: New test. * gcc.target/riscv/rvv/autovec/cond/cond_sqrt-2.c: New test. * gcc.target/riscv/rvv/autovec/cond/cond_sqrt_run-1.c: New test. * gcc.target/riscv/rvv/autovec/cond/cond_sqrt_run-2.c: New test.
1 parent 68db526 commit 05304cf

File tree

6 files changed

+131
-2
lines changed

6 files changed

+131
-2
lines changed

gcc/config/riscv/autovec-opt.md

+20
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,26 @@
730730
DONE;
731731
})
732732

733+
;; Combine vfsqrt.v and cond_mask
734+
(define_insn_and_split "*cond_<optab><mode>"
735+
[(set (match_operand:VF 0 "register_operand")
736+
(if_then_else:VF
737+
(match_operand:<VM> 1 "register_operand")
738+
(any_float_unop:VF
739+
(match_operand:VF 2 "register_operand"))
740+
(match_operand:VF 3 "register_operand")))]
741+
"TARGET_VECTOR && can_create_pseudo_p ()"
742+
"#"
743+
"&& 1"
744+
[(const_int 0)]
745+
{
746+
insn_code icode = code_for_pred (<CODE>, <MODE>mode);
747+
rtx ops[] = {operands[0], operands[1], operands[2], operands[3],
748+
gen_int_mode (GET_MODE_NUNITS (<MODE>mode), Pmode)};
749+
riscv_vector::expand_cond_len_unop (icode, ops);
750+
DONE;
751+
})
752+
733753
;; Combine vlmax neg and UNSPEC_VCOPYSIGN
734754
(define_insn_and_split "*copysign<mode>_neg"
735755
[(set (match_operand:VF 0 "register_operand")

gcc/config/riscv/autovec.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -994,11 +994,14 @@
994994
;; Includes:
995995
;; - vfsqrt.v
996996
;; -------------------------------------------------------------------------------
997-
(define_expand "<optab><mode>2"
997+
(define_insn_and_split "<optab><mode>2"
998998
[(set (match_operand:VF 0 "register_operand")
999999
(any_float_unop:VF
10001000
(match_operand:VF 1 "register_operand")))]
1001-
"TARGET_VECTOR"
1001+
"TARGET_VECTOR && can_create_pseudo_p ()"
1002+
"#"
1003+
"&& 1"
1004+
[(const_int 0)]
10021005
{
10031006
insn_code icode = code_for_pred (<CODE>, <MODE>mode);
10041007
riscv_vector::emit_vlmax_insn (icode, riscv_vector::UNARY_OP_FRM_DYN, operands);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* { dg-do compile } */
2+
/* { dg-additional-options "-march=rv64gcv_zvfh -mabi=lp64d --param riscv-autovec-preference=scalable -fno-vect-cost-model -ffast-math" } */
3+
4+
#include <stdint.h>
5+
6+
#define DEF_LOOP(TYPE, OP) \
7+
void __attribute__ ((noipa)) \
8+
test_##TYPE##_##OP (TYPE *__restrict r, TYPE *__restrict a, \
9+
TYPE *__restrict pred, int n) \
10+
{ \
11+
for (int i = 0; i < n; ++i) \
12+
r[i] = pred[i] ? OP (a[i]) : a[i]; \
13+
}
14+
15+
#define TEST_ALL(T) \
16+
T (_Float16, __builtin_sqrtf16) \
17+
T (float, __builtin_sqrtf) \
18+
T (double, __builtin_sqrt)
19+
20+
TEST_ALL (DEF_LOOP)
21+
22+
/* { dg-final { scan-assembler-times {\tvfsqrt\.v\tv[0-9]+,v[0-9]+,v0\.t} 3 } } */
23+
24+
/* { dg-final { scan-assembler {\tvsetvli\t[a-z0-9]+,[a-z0-9]+,e[0-9]+,m[f0-9]+,t[au],mu} } } */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* { dg-do compile } */
2+
/* { dg-additional-options "-march=rv64gcv_zvfh -mabi=lp64d --param riscv-autovec-preference=scalable -fno-vect-cost-model -ffast-math" } */
3+
4+
#include <stdint.h>
5+
6+
#define DEF_LOOP(TYPE, OP) \
7+
void __attribute__ ((noipa)) \
8+
test_##TYPE##_##OP (TYPE *__restrict r, TYPE *__restrict a, \
9+
TYPE *__restrict b, TYPE *__restrict pred, int n) \
10+
{ \
11+
for (int i = 0; i < n; ++i) \
12+
r[i] = pred[i] ? OP (a[i]) : b[i]; \
13+
}
14+
15+
#define TEST_ALL(T) \
16+
T (_Float16, __builtin_sqrtf16) \
17+
T (float, __builtin_sqrtf) \
18+
T (double, __builtin_sqrt)
19+
20+
TEST_ALL (DEF_LOOP)
21+
22+
/* { dg-final { scan-assembler-times {\tvfsqrt\.v\tv[0-9]+,v[0-9]+,v0\.t} 3 } } */
23+
24+
/* { dg-final { scan-assembler {\tvsetvli\t[a-z0-9]+,[a-z0-9]+,e[0-9]+,m[f0-9]+,t[au],mu} } } */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* { dg-do run { target { riscv_vector } } } */
2+
/* { dg-additional-options "--param riscv-autovec-preference=scalable -fno-vect-cost-model -ffast-math " } */
3+
4+
#include "cond_sqrt-1.c"
5+
#include <stdio.h>
6+
7+
#define N 99
8+
9+
#define TEST_LOOP(TYPE, OP) \
10+
{ \
11+
TYPE r[N], a[N], pred[N]; \
12+
for (int i = 0; i < N; ++i) \
13+
{ \
14+
a[i] = (i & 1 ? i : 3 * i) * (i % 3 == 0 ? 1 : 2); \
15+
pred[i] = (i % 7 < 4); \
16+
asm volatile("" ::: "memory"); \
17+
} \
18+
test_##TYPE##_##OP (r, a, pred, N); \
19+
for (int i = 0; i < N; ++i) \
20+
if (r[i] != (pred[i] ? OP (a[i]) : a[i])) \
21+
__builtin_abort (); \
22+
}
23+
24+
int
25+
main ()
26+
{
27+
TEST_ALL (TEST_LOOP)
28+
return 0;
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* { dg-do run { target { riscv_vector } } } */
2+
/* { dg-additional-options "--param riscv-autovec-preference=scalable -fno-vect-cost-model -ffast-math" } */
3+
4+
#include "cond_sqrt-2.c"
5+
6+
#define N 99
7+
8+
#define TEST_LOOP(TYPE, OP) \
9+
{ \
10+
TYPE r[N], a[N], b[N], pred[N]; \
11+
for (int i = 0; i < N; ++i) \
12+
{ \
13+
a[i] = (i & 1 ? i : 3 * i) * (i % 3 == 0 ? 1 : 2); \
14+
b[i] = (i % 9) * (i % 7 + 1); \
15+
pred[i] = (i % 7 < 4); \
16+
asm volatile("" ::: "memory"); \
17+
} \
18+
test_##TYPE##_##OP (r, a, b, pred, N); \
19+
for (int i = 0; i < N; ++i) \
20+
if (r[i] != (pred[i] ? OP (a[i]) : b[i])) \
21+
__builtin_abort (); \
22+
}
23+
24+
int
25+
main ()
26+
{
27+
TEST_ALL (TEST_LOOP)
28+
return 0;
29+
}

0 commit comments

Comments
 (0)