tcg/riscv: Add support for vector
cover tcg/riscv: Add support for vector
From: TANG Tiancheng <[email protected]>
This patch set introduces support for the RISC-V vector extension in TCG backend for RISC-V targets.
Key features of this patch series include:
1. Improved register allocation constraints for vector registers.
2. Implementation of vset{i}vli instructions for vector configuration and support for variable-length vector registers and LMUL-based register grouping.
3. Expanded cmp_vec to align TCG's cmp_vec behavior.
We tested the correctness and the acceleration efficiency of vector instructions for this patch set. Conduct testing using the CanMV K230 V1.1 development board in an Ubuntu 23 environment.
* Correctness:
Using risu, we generated 200,000 vector instructions for aarch64 and tested them on the board K230, which supports RISC-V vector 1.0. The K230 board runs Ubuntu 23, operating a statically compiled user-mode qemu-aarch64. We ran risu's master mode with a vector backend qemu and apprentice mode with a non-vector backend qemu.
``` apprentice: $ time qemu-aarch64-release --novec arm64_risu/risu --host=localhost arm64_risu/aarch64sve_20w master: $ script -c "time qemu-aarch64-release arm64_risu/risu --master arm64_risu/aarch64sve_20w" risu_test.log Script started, output log file is 'risu_test.log'. loading test image arm64_risu/aarch64sve_20w... master port 9191 master: waiting for connection on port 9191... starting master image at 0x3fc6df1000 starting image match status... match! scalar_cnt = 50447787 vec_cnt = 786345 All cnts = 51234132 Percent vector: 1.53% RISC-V Vector backend stat: INDEX_op_mov_vec counts: 503 INDEX_op_dup_vec counts: 9989 INDEX_op_ld_vec counts: 27728 INDEX_op_st_vec counts: 272810 INDEX_op_dupm_vec counts: 263 INDEX_op_add_vec counts: 4842 INDEX_op_sub_vec counts: 2993 INDEX_op_mul_vec counts: 677 INDEX_op_neg_vec counts: 968 INDEX_op_ssadd_vec counts: 223 INDEX_op_usadd_vec counts: 229 INDEX_op_sssub_vec counts: 226 INDEX_op_ussub_vec counts: 265 INDEX_op_smin_vec counts: 650 INDEX_op_umin_vec counts: 1134 INDEX_op_smax_vec counts: 908 INDEX_op_umax_vec counts: 625 INDEX_op_and_vec counts: 8053 INDEX_op_or_vec counts: 4520 INDEX_op_xor_vec counts: 2177 INDEX_op_not_vec counts: 2639 INDEX_op_shli_vec counts: 947 INDEX_op_shri_vec counts: 3769 INDEX_op_sari_vec counts: 2130 INDEX_op_rotli_vec counts: 558 INDEX_op_shls_vec counts: 211 INDEX_op_shrs_vec counts: 779 INDEX_op_sars_vec counts: 162 INDEX_op_rotls_vec counts: 0 INDEX_op_shlv_vec counts: 482 INDEX_op_shrv_vec counts: 229 INDEX_op_sarv_vec counts: 253 INDEX_op_rotlv_vec counts: 0 INDEX_op_rotrv_vec counts: 0 INDEX_op_cmp_vec counts: 3211 INDEX_op_rvv_cmpcond_vec counts: 3211 INDEX_op_rvv_merge_vec counts: 3211 INDEX_op_rvv_shli_vec counts: 736 INDEX_op_rvv_shri_vec counts: 2990 INDEX_op_rvv_sari_vec counts: 1968 real 27m55.049s user 8m0.284s sys 1m28.278s Script done. ```
Note:
The test results are as follows: the qemu frontend of aarch64 did not use rotls_vec, rotlv_vec, or rotrv_vec, so their generation count in the test results is 0.
* Vector Acceleration Efficiency:
We used the opencv_perf_core test program, which has been vectorized and optimized with O3 compilation. The tests were conducted on the k230 board running opencv_perf_core via qemu-aarch64.
The result is below:
``` $ time ./qemu-aarch64 ./opencv_test/opencv_perf_core --gtest_filter="*UMatTest*" --perf_force_samples=50 --perf_min_samples=50 Time compensation is 0 TEST: Skip tests with tags: 'mem_6gb', 'verylong' CTEST_FULL_OUTPUT OpenCV version: 4.5.4-dev OpenCV VCS version: 6a00483e71-dirty Build type: Release Compiler: /mnt/rtos_nfs_new/xujf/arm-tools/gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu/bin/aarch64-linux-gnu-g++ (ver 10.2.1) CPU features: NEON FP16 Note: Google Test filter = *UMatTest* [==========] Running 12 tests from 1 test case. [----------] Global test environment set-up. [----------] 12 tests from OCL_UMatTest_CustomPtr ... [----------] Global test environment tear-down [==========] 12 tests from 1 test case ran. (274624 ms total) [ PASSED ] 12 tests. scalar_cnt = 1599444 vec_cnt = 7544 All cnts = 1606988 RISC-V Vector backend stat: INDEX_op_ld_vec counts: 95 INDEX_op_st_vec counts: 3679 INDEX_op_dupm_vec counts: 2 INDEX_op_add_vec counts: 10 INDEX_op_ssadd_vec counts: 0 INDEX_op_usadd_vec counts: 4 INDEX_op_and_vec counts: 24 INDEX_op_or_vec counts: 17 INDEX_op_xor_vec counts: 4 INDEX_op_cmp_vec counts: 28 INDEX_op_rvv_cmpcond_vec counts: 28 INDEX_op_rvv_merge_vec counts: 28 real 5m37.402s user 5m36.656s sys 0m0.588s $ time ./qemu-aarch64 -novec ./opencv_test/opencv_perf_core --gtest_filter="*UMatTest*" --perf_force_samples=50 --perf_min_samples=50 Time compensation is 0 TEST: Skip tests with tags: 'mem_6gb', 'verylong' CTEST_FULL_OUTPUT OpenCV version: 4.5.4-dev OpenCV VCS version: 6a00483e71-dirty Build type: Release Compiler: /mnt/rtos_nfs_new/xujf/arm-tools/gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu/bin/aarch64-linux-gnu-g++ (ver 10.2.1) CPU features: NEON FP16 Note: Google Test filter = *UMatTest* [==========] Running 12 tests from 1 test case. [----------] Global test environment set-up. [----------] 12 tests from OCL_UMatTest_CustomPtr ... [----------] Global test environment tear-down [==========] 12 tests from 1 test case ran. (539970 ms total) [ PASSED ] 12 tests. scalar_cnt = 1598291 vec_cnt = 0 All cnts = 1598291 real 10m4.760s user 10m3.690s sys 0m0.824s ```
Using qemu run times, the acceleration ratio is calculated as (10*60+3)/(5*60+36) = 1.79, indicating a speed improvement of 44%.
And the argument "-novec" is not included in this set, as we only use it for local testing.
Swung0x48 (1):
tcg/riscv: Add basic support for vector
TANG Tiancheng (14):
util: Add RISC-V vector extension probe in cpuinfo tcg/op-gvec: Fix iteration step in 32-bit operation
tcg: Fix register allocation constraints tcg/riscv: Add riscv vset{i}vli support tcg/riscv: Implement vector load/store tcg/riscv: Implement vector mov/dup{m/i} tcg/riscv: Add support for basic vector opcodes tcg/riscv: Implement vector cmp ops tcg/riscv: Implement vector not/neg ops tcg/riscv: Implement vector sat/mul ops tcg/riscv: Implement vector min/max ops tcg/riscv: Implement vector shs/v ops tcg/riscv: Implement vector roti/v/x shi ops tcg/riscv: Enable vector TCG host-native
host/include/riscv/host/cpuinfo.h | 1 + tcg/riscv/tcg-target-con-set.h | 7 + tcg/riscv/tcg-target-con-str.h | 2 + tcg/riscv/tcg-target.c.inc | 839 ++++++++++++++++++++++++++++-- tcg/riscv/tcg-target.h | 82 +-- tcg/riscv/tcg-target.opc.h | 18 + tcg/tcg-op-gvec.c | 2 +- tcg/tcg.c | 20 +- util/cpuinfo-riscv.c | 20 +- 9 files changed, 914 insertions(+), 77 deletions(-) create mode 100644 tcg/riscv/tcg-target.opc.h
Signature · 1 line
2.43.0
PATCH 1/15 util: Add RISC-V vector extension probe in cpuinfo
From: TANG Tiancheng <[email protected]>
Add support for probing RISC-V vector extension availability in the backend. This information will be used when deciding whether to use vector instructions in code generation.
While the compiler doesn't support RISCV_HWPROBE_EXT_ZVE64X, we use RISCV_HWPROBE_IMA_V instead.
--- host/include/riscv/host/cpuinfo.h | 1 + util/cpuinfo-riscv.c | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-)
host/include/riscv/host/cpuinfo.h +1 -0
@@ -10,6 +10,7 @@#define CPUINFO_ZBA (1u << 1)#define CPUINFO_ZBB (1u << 2)#define CPUINFO_ZICOND (1u << 3) +#define CPUINFO_ZVE64X (1u << 4)/* Initialized with a constructor. */ extern unsigned cpuinfo;
util/cpuinfo-riscv.c +18 -2
@@ -33,7 +33,7 @@ static void sigill_handler(int signo, siginfo_t *si, void *data)/* Called both as constructor and (possibly) via other constructors. */ unsigned __attribute__((constructor))cpuinfo_init (void ) { -unsigned left =CPUINFO_ZBA |CPUINFO_ZBB |CPUINFO_ZICOND ; +unsigned left =CPUINFO_ZBA |CPUINFO_ZBB |CPUINFO_ZICOND |CPUINFO_ZVE64X ;unsigned info = cpuinfo;if (info) {
@@ -49,6 +49,9 @@ unsigned __attribute__((constructor)) cpuinfo_init(void)#endif #if defined(__riscv_arch_test) && defined(__riscv_zicond) info |=CPUINFO_ZICOND ; +#endif +#if defined(__riscv_arch_test) && defined(__riscv_zve64x) + info |=CPUINFO_ZVE64X ;#endif left &= ~info;
@@ -64,7 +67,8 @@ unsigned __attribute__((constructor)) cpuinfo_init(void) && pair.key >= 0) { info |= pair.value &RISCV_HWPROBE_EXT_ZBA ?CPUINFO_ZBA :0 ; info |= pair.value &RISCV_HWPROBE_EXT_ZBB ?CPUINFO_ZBB :0 ; - left &= ~(CPUINFO_ZBA |CPUINFO_ZBB ); + info |= pair.value &RISCV_HWPROBE_IMA_V ?CPUINFO_ZVE64X :0 ; + left &= ~(CPUINFO_ZBA |CPUINFO_ZBB |CPUINFO_ZVE64X );#ifdef RISCV_HWPROBE_EXT_ZICOND info |= pair.value &RISCV_HWPROBE_EXT_ZICOND ?CPUINFO_ZICOND :0 ; left &= ~CPUINFO_ZICOND ;
@@ -108,6 +112,18 @@ unsigned __attribute__((constructor)) cpuinfo_init(void) left &= ~CPUINFO_ZICOND ; } +if (left &CPUINFO_ZVE64X ) { +/* Probe for Vector: vsetivli t0,1,e64,m1,ta,ma */ +unsigned vl; + got_sigill =0 ; + + asmvolatile ( +"vsetivli %0, 1, e64, m1, ta, ma\n\t" + :"=r" (vl) : :"vl" + ); + info |= (got_sigill || vl !=1 ) ?0 :CPUINFO_ZVE64X ; + } +sigaction (SIGILL , &sa_old,NULL );assert (left ==0 ); }
Signature · 1 line
2.43.0
PATCH 2/15 tcg/op-gvec: Fix iteration step in 32-bit operation
In reply to LIU ZhiweiFrom: TANG Tiancheng <[email protected]>
The loop in the 32-bit case of the vector compare operation was incorrectly incrementing by 8 bytes per iteration instead of 4 bytes. This caused the function to process only half of the intended elements.
--- tcg/tcg-op-gvec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tcg/tcg-op-gvec.c +1 -1
@@ -3939,7 +3939,7 @@ void tcg_gen_gvec_cmps(TCGCond cond, unsigned vece, uint32_t dofs,uint32_t i;tcg_gen_extrl_i64_i32 (t1, c); -for (i =0 ; i < oprsz; i +=8 ) { +for (i =0 ; i < oprsz; i +=4 ) {tcg_gen_ld_i32 (t0, tcg_env, aofs + i);tcg_gen_negsetcond_i32 (cond, t0, t0, t1);tcg_gen_st_i32 (t0, tcg_env, dofs + i);
Signature · 1 line
2.43.0
PATCH 3/15 tcg: Fix register allocation constraints
In reply to LIU ZhiweiFrom: TANG Tiancheng <[email protected]>
When allocating registers for input and output, ensure they match the available registers to avoid allocating illeagal registers.
We should respect RISC-V vector extension's variable-length registers and LMUL-based register grouping. Coordinate with tcg_target_available_regs initialization tcg_target_init (behind this commit) to ensure proper handling of vector register constraints.
Note: While mov_vec doesn't have constraints, dup_vec and other IRs do. We need to strengthen constraints for all IRs except mov_vec, and this is sufficient.
--- tcg/tcg.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-)
tcg/tcg.c +13 -7
@@ -4722,8 +4722,10 @@ static void tcg_reg_alloc_dup(TCGContext *s, const TCGOp *op)return ; } - dup_out_regs = tcg_op_defs[INDEX_op_dup_vec].args_ct[0 ].regs; - dup_in_regs = tcg_op_defs[INDEX_op_dup_vec].args_ct[1 ].regs; + dup_out_regs = tcg_op_defs[INDEX_op_dup_vec].args_ct[0 ].regs & + tcg_target_available_regs[ots->type]; + dup_in_regs = tcg_op_defs[INDEX_op_dup_vec].args_ct[1 ].regs & + tcg_target_available_regs[its->type];/* Allocate the output register now. */ if (ots->val_type !=TEMP_VAL_REG ) {
@@ -4876,7 +4878,7 @@ static void tcg_reg_alloc_op(TCGContext *s, const TCGOp *op) reg = ts->reg; i_preferred_regs =0 ; - i_required_regs = arg_ct->regs; + i_required_regs = arg_ct->regs & tcg_target_available_regs[ts->type]; allocate_new_reg = false; copyto_new_reg = false;
@@ -5078,6 +5080,7 @@ static void tcg_reg_alloc_op(TCGContext *s, const TCGOp *op)/* satisfy the output constraints */ for (k =0 ; k < nb_oargs; k++) { +TCGRegSet o_required_regs; i = def->args_ct[k].sort_index; arg = op->args[i]; arg_ct = &def->args_ct[i];
@@ -5085,17 +5088,19 @@ static void tcg_reg_alloc_op(TCGContext *s, const TCGOp *op)/* ENV should not be modified. */ tcg_debug_assert (!temp_readonly (ts)); + o_required_regs = arg_ct->regs & + tcg_target_available_regs[ts->type];switch (arg_ct->pair) {case 0 :/* not paired */ if (arg_ct->oalias && !const_args[arg_ct->alias_index]) { reg = new_args[arg_ct->alias_index]; }else if (arg_ct->newreg) { - reg =tcg_reg_alloc (s, arg_ct->regs, + reg =tcg_reg_alloc (s, o_required_regs, i_allocated_regs | o_allocated_regs,output_pref (op, k), ts->indirect_base); }else { - reg =tcg_reg_alloc (s, arg_ct->regs, o_allocated_regs, + reg =tcg_reg_alloc (s, o_required_regs, o_allocated_regs,output_pref (op, k), ts->indirect_base); }break ;
@@ -5104,12 +5109,13 @@ static void tcg_reg_alloc_op(TCGContext *s, const TCGOp *op)if (arg_ct->oalias) { reg = new_args[arg_ct->alias_index]; }else if (arg_ct->newreg) { - reg =tcg_reg_alloc_pair (s, arg_ct->regs, + reg =tcg_reg_alloc_pair (s, o_required_regs, i_allocated_regs | o_allocated_regs,output_pref (op, k), ts->indirect_base); }else { - reg =tcg_reg_alloc_pair (s, arg_ct->regs, o_allocated_regs, + reg =tcg_reg_alloc_pair (s, o_required_regs, + o_allocated_regs,output_pref (op, k), ts->indirect_base); }
Signature · 1 line
2.43.0
Re: [PATCH v1 03/15] tcg: Fix register allocation constraints
Quoted 26 lines · LIU Zhiwei
From: TANG Tiancheng<[email protected]>
When allocating registers for input and output, ensure they match the available registers to avoid allocating illeagal registers.
We should respect RISC-V vector extension's variable-length registers and LMUL-based register grouping. Coordinate with
tcg_target_available_regsinitializationtcg_target_init(behind this commit) to ensure proper handling of vector register constraints.Note: While
mov_vecdoesn't have constraints,dup_vecand other IRs do. We need to strengthen constraints for all IRs exceptmov_vec, and this is sufficient.Signed-off-by TANG Tiancheng<[email protected]>Fixes 29f5e92502 (tcg: Introduce paired register allocation)Reviewed-by Liu Zhiwei<[email protected]>--- tcg/tcg.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-)tcg/tcg.c +0
-0
tcg/tcg.c static void tcg_reg_alloc_dup(TCGContext *s, const TCGOp *op) · line 4729
Quoted 10 lines
tcg/tcg.c +4
-2@@ -4722,8 +4722,10 @@ static void tcg_reg_alloc_dup(TCGContext *s, const TCGOp *op)return ; } - dup_out_regs = tcg_op_defs[INDEX_op_dup_vec].args_ct[0 ].regs; - dup_in_regs = tcg_op_defs[INDEX_op_dup_vec].args_ct[1 ].regs; + dup_out_regs = tcg_op_defs[INDEX_op_dup_vec].args_ct[0 ].regs & + tcg_target_available_regs[ots->type]; + dup_in_regs = tcg_op_defs[INDEX_op_dup_vec].args_ct[1 ].regs & + tcg_target_available_regs[its->type];
Why would you ever have constraints that resolve to unavailable registers?
If you don't want to fix this in the backend, then the next best place is in process_op_defs(), so that we take care of this once at startup, and never have to think about it again.
r~
Re: [PATCH v1 03/15] tcg: Fix register allocation constraints
Quoted 39 lines · Richard Henderson
Quoted 26 lines · LIU Zhiwei
From: TANG Tiancheng<[email protected]>
When allocating registers for input and output, ensure they match the available registers to avoid allocating illeagal registers.
We should respect RISC-V vector extension's variable-length registers and LMUL-based register grouping. Coordinate with
tcg_target_available_regsinitializationtcg_target_init(behind this commit) to ensure proper handling of vector register constraints.Note: While
mov_vecdoesn't have constraints,dup_vecand other IRs do. We need to strengthen constraints for all IRs exceptmov_vec, and this is sufficient.Signed-off-by TANG Tiancheng<[email protected]>Fixes 29f5e92502 (tcg: Introduce paired register allocation)Reviewed-by Liu Zhiwei<[email protected]>---
tcg/tcg.c| 20 +++++++++++++-------1 file changed, 13 insertions(+), 7 deletions(-)tcg/tcg.c +0
-0tcg/tcg.c static void tcg_reg_alloc_dup(TCGContext *s, · line 4729
Quoted 9 lines
tcg/tcg.c +4
-1@@ -4722,8 +4722,10 @@ static void tcg_reg_alloc_dup(TCGContext *s, const TCGOp *op) return; } - dup_out_regs = tcg_op_defs[INDEX_op_dup_vec].args_ct[0].regs; - dup_in_regs = tcg_op_defs[INDEX_op_dup_vec].args_ct[1].regs; + dup_out_regs = tcg_op_defs[INDEX_op_dup_vec].args_ct[0].regs & + tcg_target_available_regs[ots->type]; + dup_in_regs = tcg_op_defs[INDEX_op_dup_vec].args_ct[1].regs & + tcg_target_available_regs[its->type];Why would you ever have constraints that resolve to unavailable registers?
Quoted 1 line
If you don't want to fix this in the backend, then the next best place is in
process_op_defs(), so that we take care of this once at startup, and never have to think about it again.
Hi Richard,
The constraints provided in process_op_defs() are static and tied to the IR operations. For example, if we create constraints for add_vec, the same constraints will apply to all types of add_vec operations (TCG_TYPE_V64, TCG_TYPE_V128, TCG_TYPE_V256). This means the constraints don't change based on the specific type of operation being performed. In contrast, RISC-V's LMUL (Length Multiplier) can change at runtime depending on the type of IR operation. Different LMUL values affect which vector registers are available for use in RISC-V. Let's consider an example where the host's vector register width is 128 bits:
For an add_vec operation on v256 (256-bit vectors), only even-numbered vector registers like 0, 2, 4 can be used. However, for an add_vec operation on v128 (128-bit vectors), all vector registers (0, 1, 2, etc.) are available.
Thus if we want to use all registers of vectors, we have to add a dynamic constraint on register allocation based on IR types.
Thanks,
Zhiwei
Quoted 3 lines
r~
Re: [PATCH v1 03/15] tcg: Fix register allocation constraints
Quoted 1 line · LIU Zhiwei
Thus if we want to use all registers of vectors, we have to add a dynamic constraint on register allocation based on IR types.
My comment vs patch 4 is that you can't do that, at least not without large changes to TCG.
In addition, I said that the register pressure on vector regs is not high enough to justify such changes. There is, so far, little benefit in having more than 4 or 5 vector registers, much less 32. Thus 7 (lmul 4, omitting v0) is sufficient.
r~
Re: [PATCH v1 03/15] tcg: Fix register allocation constraints
Quoted 6 lines · Richard Henderson
Quoted 1 line · LIU Zhiwei
Thus if we want to use all registers of vectors, we have to add a dynamic constraint on register allocation based on IR types.
My comment vs patch 4 is that you can't do that, at least not without large changes to TCG.
In addition, I said that the register pressure on vector regs is not high enough to justify such changes. There is, so far, little benefit in having more than 4 or 5 vector registers, much less 32. Thus 7 (lmul 4, omitting v0) is sufficient.
At least on QEMU, SVE can support 2048 bit vector length with 'sve-default-vector-length=256'. Software optimized with SVE, such as X264 can benefit with long SVE length in less dynamic A64 instructions.
We want to expose all host vector ability. Thus the largest TCG_TYPE_V256 is not enough, as 128-bit RVV can give 8*128=1024 width operation. We have expand TCG_TYPE_V512/1024/2048 types(not in this patch set, but intend to upstream later). With large TCG_TYPE_V1024/2048, we get better performance on RISC-V board with much less translated RISC-V vector instructions. We can give a more detailed experiment result if needed.
However, we will only have 3 vector register when support TCG_TYPE_V1024. And even less for TCG_TYPE_V2048. Current approach will give more vectors TCG_TYPE_V128 even with support TCG_TYPE_V1024, which will relax some guest NEON register pressure.
Thanks,
Zhiwei
Quoted 3 lines
r~
Re: [PATCH v1 03/15] tcg: Fix register allocation constraints
Quoted 7 lines · LIU Zhiwei
Quoted 5 lines · Richard Henderson
Quoted 1 line · LIU Zhiwei
Thus if we want to use all registers of vectors, we have to add a dynamic constraint on register allocation based on IR types.
My comment vs patch 4 is that you can't do that, at least not without large changes to TCG.
Quoted 8 lines
Quoted 1 line
In addition, I said that the register pressure on vector regs is not high enough to justify such changes. There is, so far, little benefit in having more than 4 or 5 vector registers, much less 32. Thus 7 (lmul 4, omitting v0) is sufficient.
At least on QEMU, SVE can support 2048 bit vector length with 'sve-default-vector- length=256'. Software optimized with SVE, such as X264 can benefit with long SVE length in less dynamic A64 instructions.
We want to expose all host vector ability. Thus the largest
TCG_TYPE_V256is not enough, as 128-bit RVV can give 8*128=1024 width operation. We have expand TCG_TYPE_V512/1024/2048types(not in this patch set, but intend to upstream later). With large TCG_TYPE_V1024/2048, we get better performance on RISC-V board with much less translated RISC-V vector instructions. We can give a more detailed experiment result if needed.However, we will only have 3 vector register when support TCG_TYPE_V1024. And even less for TCG_TYPE_V2048. Current approach will give more vectors
TCG_TYPE_V128even with supportTCG_TYPE_V1024, which will relax some guest NEON register pressure.
Then you will have to teach TCG about one operand consuming and clobbering N hard registers, so that you get the spills and fills done correctly.
But you haven't done that in this patch set, so will currently generate incorrect code.
I think you should make longer vector operations a longer term project, and start with something simpler.
r~
Re: [PATCH v1 03/15] tcg: Fix register allocation constraints
Quoted 10 lines · Richard Henderson
Quoted 9 lines · LIU Zhiwei
Quoted 6 lines · Richard Henderson
Quoted 1 line · LIU Zhiwei
Thus if we want to use all registers of vectors, we have to add a dynamic constraint on register allocation based on IR types.
My comment vs patch 4 is that you can't do that, at least not without large changes to TCG.
In addition, I said that the register pressure on vector regs is not high enough to justify such changes. There is, so far, little benefit in having more than 4 or 5 vector registers, much less 32. Thus 7 (lmul 4, omitting v0) is sufficient.
Quoted 8 lines
Quoted 6 lines
At least on QEMU, SVE can support 2048 bit vector length with 'sve-default-vector- length=256'. Software optimized with SVE, such as X264 can benefit with long SVE length in less dynamic A64 instructions.
We want to expose all host vector ability. Thus the largest
TCG_TYPE_V256is not enough, as 128-bit RVV can give 8*128=1024 width operation. We have expand TCG_TYPE_V512/1024/2048types(not in this patch set, but intend to upstream later). With large TCG_TYPE_V1024/2048, we get better performance on RISC-V board with much less translated RISC-V vector instructions. We can give a more detailed experiment result if needed.However, we will only have 3 vector register when support TCG_TYPE_V1024. And even less for TCG_TYPE_V2048. Current approach will give more vectors
TCG_TYPE_V128even with supportTCG_TYPE_V1024, which will relax some guest NEON register pressure.Then you will have to teach TCG about one operand consuming and clobbering N hard registers, so that you get the spills and fills done correctly.
I think we have done this in patch 6.
Quoted 4 lines
But you haven't done that in this patch set, so will currently generate incorrect code.
I think you should make longer vector operations a longer term project,
Does longer vector operations implementation deserves to upstream? We can contribute it sooner as it is ready.
Quoted 1 line
and start with something simpler.
Agree if you insist. 🙂
Zhiwei
Quoted 3 lines
r~
Re: [PATCH v1 03/15] tcg: Fix register allocation constraints
Quoted 14 lines · LIU Zhiwei
Quoted 12 lines · Richard Henderson
Quoted 11 lines · LIU Zhiwei
Quoted 6 lines · Richard Henderson
Quoted 1 line · LIU Zhiwei
Thus if we want to use all registers of vectors, we have to add a dynamic constraint on register allocation based on IR types.
My comment vs patch 4 is that you can't do that, at least not without large changes to TCG.
In addition, I said that the register pressure on vector regs is not high enough to justify such changes. There is, so far, little benefit in having more than 4 or 5 vector registers, much less 32. Thus 7 (lmul 4, omitting v0) is sufficient.
At least on QEMU, SVE can support 2048 bit vector length with 'sve-default-vector- length=256'. Software optimized with SVE, such as X264 can benefit with long SVE length in less dynamic A64 instructions.
Quoted 7 lines
Quoted 6 lines
Quoted 4 lines
We want to expose all host vector ability. Thus the largest
TCG_TYPE_V256is not enough, as 128-bit RVV can give 8*128=1024 width operation. We have expand TCG_TYPE_V512/1024/2048types(not in this patch set, but intend to upstream later). With large TCG_TYPE_V1024/2048, we get better performance on RISC-V board with much less translated RISC-V vector instructions. We can give a more detailed experiment result if needed.However, we will only have 3 vector register when support TCG_TYPE_V1024. And even less for TCG_TYPE_V2048. Current approach will give more vectors
TCG_TYPE_V128even with supportTCG_TYPE_V1024, which will relax some guest NEON register pressure.Then you will have to teach TCG about one operand consuming and clobbering N hard registers, so that you get the spills and fills done correctly.
I think we have done this in patch 6.
No, you have not.
There are no modifications to tcg_reg_alloc, and there are no additional calls to tcg_reg_free, which is where spills are generated. There would also need to be changes on the fill side, temp_load.
Quoted 3 lines
Quoted 1 line
I think you should make longer vector operations a longer term project,
Does longer vector operations implementation deserves to upstream? We can contribute it sooner as it is ready.
Sure.
r~
Re: [PATCH v1 03/15] tcg: Fix register allocation constraints
Quoted 18 lines · Richard Henderson
Quoted 17 lines · LIU Zhiwei
Quoted 15 lines · Richard Henderson
Quoted 14 lines · LIU Zhiwei
Quoted 6 lines · Richard Henderson
Quoted 1 line · LIU Zhiwei
Thus if we want to use all registers of vectors, we have to add a dynamic constraint on register allocation based on IR types.
My comment vs patch 4 is that you can't do that, at least not without large changes to TCG.
In addition, I said that the register pressure on vector regs is not high enough to justify such changes. There is, so far, little benefit in having more than 4 or 5 vector registers, much less 32. Thus 7 (lmul 4, omitting v0) is sufficient.
At least on QEMU, SVE can support 2048 bit vector length with 'sve-default-vector- length=256'. Software optimized with SVE, such as X264 can benefit with long SVE length in less dynamic A64 instructions.
We want to expose all host vector ability. Thus the largest
TCG_TYPE_V256is not enough, as 128-bit RVV can give 8*128=1024 width operation. We have expand TCG_TYPE_V512/1024/2048types(not in this patch set, but intend to upstream later). With large TCG_TYPE_V1024/2048, we get better performance on RISC-V board with much less translated RISC-V vector instructions. We can give a more detailed experiment result if needed.
Quoted 8 lines
Quoted 4 lines
Quoted 3 lines
Quoted 1 line
However, we will only have 3 vector register when support TCG_TYPE_V1024. And even less for TCG_TYPE_V2048. Current approach will give more vectors
TCG_TYPE_V128even with supportTCG_TYPE_V1024, which will relax some guest NEON register pressure.Then you will have to teach TCG about one operand consuming and clobbering N hard registers, so that you get the spills and fills done correctly.
I think we have done this in patch 6.
No, you have not.
There are no modifications to
tcg_reg_alloc, and there are no additional calls totcg_reg_free, which is where spills are generated. There would also need to be changes on the fill side,temp_load.
Thanks. I choose the simple design as you suggest for this patch set. And We will fix this problem when send the longer vector operations implementation.
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
I think you should make longer vector operations a longer term project,
Does longer vector operations implementation deserves to upstream? We can contribute it sooner as it is ready.
Sure.
Good news!
Thanks,
Zhiwei
Quoted 3 lines
r~
PATCH 4/15 tcg/riscv: Add basic support for vector
In reply to LIU ZhiweiFrom: Swung0x48 <[email protected]>
The RISC-V vector instruction set utilizes the LMUL field to group multiple registers, enabling variable-length vector registers. This implementation uses only the first register number of each group while reserving the other register numbers within the group.
The unused register numbers within groups are implemented by adding constraints to tcg_target_available_regs during register allocation in tcg.c in the previous commit.
This patch:
1. Reserves vector register 0 for use as a mask register.
2. When using register groups, reserves the additional registers within each group.
--- tcg/riscv/tcg-target-con-str.h | 1 + tcg/riscv/tcg-target.c.inc | 151 ++++++++++++++++++++++++++------- tcg/riscv/tcg-target.h | 78 ++++++++++------- tcg/riscv/tcg-target.opc.h | 12 +++ 4 files changed, 177 insertions(+), 65 deletions(-) create mode 100644 tcg/riscv/tcg-target.opc.h
tcg/riscv/tcg-target-con-str.h +1 -0
@@ -9,6 +9,7 @@* REGS(letter, register_mask) */ REGS('r', ALL_GENERAL_REGS) +REGS ('v' ,ALL_VECTOR_REGS )/* * Define constraint letters for constants:
tcg/riscv/tcg-target.c.inc +119 -32
200 lines not on this page · raw message
tcg/riscv/tcg-target.h +45 -33
93 lines not on this page · raw message
tcg/riscv/tcg-target.opc.h new file +12 -0
@@ -0,0 +1,12 @@ +/* +* Copyright (c) C-SKY Microsystems Co., Ltd. +* +* This work is licensed under the terms of the GNU GPL, version 2 or +* (at your option) any later version. +* +* See the COPYING file in the top-level directory for details. +* +* Target-specific opcodes for host vector expansion. These will be +* emitted by tcg_expand_vec_op. For those familiar with GCC internals, +* consider these to be UNSPEC with names. +*/
Signature · 1 line
2.43.0
Re: [PATCH v1 04/15] tcg/riscv: Add basic support for vector
Quoted 11 lines · LIU Zhiwei
tcg/riscv/tcg-target.c.inc +11
-0+ if (cpuinfo & CPUINFO_ZVE64X) { + /* We need to get vlenb for vector's extension */ + riscv_get_vlenb(); + tcg_debug_assert(riscv_vlen >= 64 && is_power_of_2(riscv_vlen)); + + if (riscv_vlen >= 256) { + tcg_target_available_regs[TCG_TYPE_V64] = ALL_VECTOR_REGS; + tcg_target_available_regs[TCG_TYPE_V128] = ALL_VECTOR_REGS; + tcg_target_available_regs[TCG_TYPE_V256] = ALL_VECTOR_REGS; + } else if (riscv_vlen == 128) { + tcg_target_available_regs[TCG_TYPE_V64] = ALL_VECTOR_REGS;
tcg/riscv/tcg-target.c.inc static void tcg_out_tb_start(TCGContext *s) · line 2188
Quoted 10 lines
tcg/riscv/tcg-target.c.inc +10
-0+ tcg_target_available_regs[TCG_TYPE_V128] = ALL_VECTOR_REGS; + tcg_target_available_regs[TCG_TYPE_V256] = ALL_DVECTOR_REG_GROUPS; + } else if (riscv_vlen == 64) { + tcg_target_available_regs[TCG_TYPE_V64] = ALL_VECTOR_REGS; + tcg_target_available_regs[TCG_TYPE_V128] = ALL_DVECTOR_REG_GROUPS; + tcg_target_available_regs[TCG_TYPE_V256] = ALL_QVECTOR_REG_GROUPS; + } else { + g_assert_not_reached(); + } + }
I think this is over-complicated, and perhaps the reason for patch 3.
What I believe you're missing with patch 3 is the fact that when you change the lmul, adjacent vector registers get clobbered, and the tcg register allocator does not expect that. This will result in incorrect register allocation.
You need to pick one size at startup, and expose *only* those registers.
This won't affect code generation much, because we never have heavy vector register pressure. Mostly values go out of scope at the end of every guest instruction. So having only 8 or 16 visible host registers instead of 32 isn't a big deal.
r~
PATCH 5/15 tcg/riscv: Add riscv vset{i}vli support
In reply to LIU ZhiweiFrom: TANG Tiancheng <[email protected]>
In RISC-V, vector operations require initial configuration using the vset{i}vl{i} instruction.
This instruction:
1. Sets the vector length (vl) in bytes
2. Configures the vtype register, which includes:
SEW (Single Element Width)
LMUL (vector register group multiplier) Other vector operation parameters
This configuration is crucial for defining subsequent vector operation behavior. To optimize performance, the configuration process is managed dynamically:
1. Reconfiguration using vset{i}vl{i} is necessary when SEW or vector register group width changes.
2. The vset instruction can be omitted when configuration remains unchanged.
This optimization is only effective within a single TB. Each TB requires reconfiguration at its start, as the current state cannot be obtained from hardware.
--- tcg/riscv/tcg-target.c.inc | 121 +++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+)
tcg/riscv/tcg-target.c.inc +121 -0
@@ -167,6 +167,18 @@ static bool tcg_target_const_match(int64_t val, int ct, * RISC-V Base ISA opcodes (IM) */ +#define V_OPIVV (0x0 << 12) +#define V_OPFVV (0x1 << 12) +#define V_OPMVV (0x2 << 12) +#define V_OPIVI (0x3 << 12) +#define V_OPIVX (0x4 << 12) +#define V_OPFVF (0x5 << 12) +#define V_OPMVX (0x6 << 12) +#define V_OPCFG (0x7 << 12) + +#define V_SUMOP (0x0 << 20) +#define V_LUMOP (0x0 << 20) + typedef enum { OPC_ADD = 0x33, OPC_ADDI = 0x13,
@@ -262,6 +274,11 @@ typedef enum { /* Zicond: integer conditional operations */ OPC_CZERO_EQZ = 0x0e005033, OPC_CZERO_NEZ = 0x0e007033, + + /* V: Vector extension 1.0 */ + OPC_VSETVLI = 0x57 | V_OPCFG, + OPC_VSETIVLI = 0xc0000057 | V_OPCFG, + OPC_VSETVL = 0x80000057 | V_OPCFG, } RISCVInsn; /*
@@ -354,6 +371,42 @@ static int32_t encode_uj(RISCVInsn opc, TCGReg rd, uint32_t imm) return opc | (rd & 0x1f) << 7 | encode_ujimm20(imm); } +typedef enum { + VTA_TU = 0, + VTA_TA, +} RISCVVta; + +typedef enum { + VMA_MU = 0, + VMA_MA, +} RISCVVma; + +typedef enum { + VSEW_E8 = 0, /* EW=8b */ + VSEW_E16, /* EW=16b */ + VSEW_E32, /* EW=32b */ + VSEW_E64, /* EW=64b */ +} RISCVVsew; + +typedef enum { + VLMUL_M1 = 0, /* LMUL=1 */ + VLMUL_M2, /* LMUL=2 */ + VLMUL_M4, /* LMUL=4 */ + VLMUL_M8, /* LMUL=8 */ + VLMUL_RESERVED, + VLMUL_MF8, /* LMUL=1/8 */ + VLMUL_MF4, /* LMUL=1/4 */ + VLMUL_MF2, /* LMUL=1/2 */ +} RISCVVlmul; +#define LMUL_MAX 8 + +static int32_t encode_vtype(RISCVVta vta, RISCVVma vma, + RISCVVsew vsew, RISCVVlmul vlmul) +{ + return (vma & 0x1) << 7 | (vta & 0x1) << 6 | (vsew & 0x7) << 3 | + (vlmul & 0x7); +} + /* * RISC-V instruction emitters */
@@ -483,6 +536,12 @@ static void tcg_out_opc_reg_vec_i(TCGContext *s, RISCVInsn opc, tcg_out32(s, encode_r(opc, rd, (imm & 0x1f), vs2) | (vm << 25)); } +static void tcg_out_opc_vec_config(TCGContext *s, RISCVInsn opc, + TCGReg rd, uint32_t avl, int32_t vtypei) +{ + tcg_out32(s, encode_i(opc, rd, avl, vtypei)); +} + /* vm=0 (vm = false) means vector masking ENABLED. */ #define tcg_out_opc_vv(s, opc, vd, vs2, vs1, vm) \ tcg_out_opc_reg_vec(s, opc, vd, vs1, vs2, vm);
@@ -497,12 +556,68 @@ static void tcg_out_opc_reg_vec_i(TCGContext *s, RISCVInsn opc, #define tcg_out_opc_vi(s, opc, vd, vs2, imm, vm) \ tcg_out_opc_reg_vec_i(s, opc, vd, imm, vs2, vm); +#define tcg_out_opc_vconfig(s, opc, rd, avl, vtypei) \ + tcg_out_opc_vec_config(s, opc, rd, avl, vtypei); + /* * Only unit-stride addressing implemented; may extend in future. */ #define tcg_out_opc_ldst_vec(s, opc, vs3_vd, rs1, vm) \ tcg_out_opc_reg_vec(s, opc, vs3_vd, rs1, 0, vm); +static void tcg_out_vsetvl(TCGContext *s, uint32_t avl, RISCVVta vta, + RISCVVma vma, RISCVVsew vsew, + RISCVVlmul vlmul) +{ + int32_t vtypei = encode_vtype(vta, vma, vsew, vlmul); + + if (avl < 32) { + tcg_out_opc_vconfig(s, OPC_VSETIVLI, TCG_REG_ZERO, avl, vtypei); + } else { + tcg_out_opc_imm(s, OPC_ADDI, TCG_REG_TMP0, TCG_REG_ZERO, avl); + tcg_out_opc_vconfig(s, OPC_VSETVLI, TCG_REG_ZERO, TCG_REG_TMP0, vtypei); + } +} + +/* + * TODO: If the vtype value is not supported by the implementation, + * then the vill bit is set in vtype, the remaining bits in + * vtype are set to zero, and the vl register is also set to zero + */ + +static __thread unsigned prev_size; +static __thread unsigned prev_vece = MO_8; +static __thread bool vec_vtpye_init = true; + +#define get_vlmax(vsew) (riscv_vlen / (8 << vsew) * (LMUL_MAX)) +#define get_vec_type_bytes(type) (type >= TCG_TYPE_V64 ? \ + (8 << (type - TCG_TYPE_V64)) : 0) +#define encode_lmul(oprsz, vlenb) (ctzl(oprsz / vlenb)) + +static inline void tcg_target_set_vec_config(TCGContext *s, TCGType type, + unsigned vece) +{ + unsigned oprsz = get_vec_type_bytes(type); + + if (!vec_vtpye_init && (prev_size == oprsz && prev_vece == vece)) { + return ; + } + + RISCVVsew vsew = vece - MO_8 + VSEW_E8; + unsigned avl = oprsz / (1 << vece); + unsigned vlenb = riscv_vlen / 8; + RISCVVlmul lmul = oprsz > vlenb ? + encode_lmul(oprsz, vlenb) : VLMUL_M1; + tcg_debug_assert(avl <= get_vlmax(vsew)); + tcg_debug_assert(lmul <= VLMUL_RESERVED); + + prev_size = oprsz; + prev_vece = vece; + vec_vtpye_init = false; + tcg_out_vsetvl(s, avl, VTA_TA, VMA_MA, vsew, lmul); + return ; +} + /* * TCG intrinsics */
@@ -1914,6 +2029,11 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, const TCGArg args[TCG_MAX_OP_ARGS], const int const_args[TCG_MAX_OP_ARGS]) { + TCGType type = vecl + TCG_TYPE_V64; + + if (vec_vtpye_init) { + tcg_target_set_vec_config(s, type, vece); + } switch (opc) { case INDEX_op_mov_vec: /* Always emitted via tcg_out_mov. */ case INDEX_op_dup_vec: /* Always emitted via tcg_out_dup_vec. */
@@ -2151,6 +2271,7 @@ static void tcg_target_qemu_prologue(TCGContext *s) static void tcg_out_tb_start(TCGContext *s) { + vec_vtpye_init = true; /* nothing to do */ }
Signature · 1 line
2.43.0
Re: [PATCH v1 05/15] tcg/riscv: Add riscv vset{i}vli support
Quoted 76 lines · LIU Zhiwei
From: TANG Tiancheng <[email protected]>
In RISC-V, vector operations require initial configuration using the vset{i}vl{i} instruction.
This instruction:
1. Sets the vector length (vl) in bytes
2. Configures the vtype register, which includes:
SEW (Single Element Width)
LMUL (vector register group multiplier) Other vector operation parametersThis configuration is crucial for defining subsequent vector operation behavior. To optimize performance, the configuration process is managed dynamically:
1. Reconfiguration using vset{i}vl{i} is necessary when SEW or vector register group width changes.
2. The vset instruction can be omitted when configuration remains unchanged.This optimization is only effective within a single TB. Each TB requires reconfiguration at its start, as the current state cannot be obtained from hardware.
Signed-off-by TANG Tiancheng <[email protected]>Signed-off-by Weiwei Li <[email protected]>Reviewed-by Liu Zhiwei <[email protected]>--- tcg/riscv/tcg-target.c.inc | 121 +++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+)tcg/riscv/tcg-target.c.inc +23
-0@@ -167,6 +167,18 @@ static bool tcg_target_const_match(int64_t val, int ct, * RISC-V Base ISA opcodes (IM) */ +#define V_OPIVV (0x0 << 12) +#define V_OPFVV (0x1 << 12) +#define V_OPMVV (0x2 << 12) +#define V_OPIVI (0x3 << 12) +#define V_OPIVX (0x4 << 12) +#define V_OPFVF (0x5 << 12) +#define V_OPMVX (0x6 << 12) +#define V_OPCFG (0x7 << 12) + +#define V_SUMOP (0x0 << 20) +#define V_LUMOP (0x0 << 20) + typedef enum { OPC_ADD = 0x33, OPC_ADDI = 0x13,@@ -262,6 +274,11 @@ typedef enum { /* Zicond: integer conditional operations */ OPC_CZERO_EQZ = 0x0e005033, OPC_CZERO_NEZ = 0x0e007033, + + /* V: Vector extension 1.0 */ + OPC_VSETVLI = 0x57 | V_OPCFG, + OPC_VSETIVLI = 0xc0000057 | V_OPCFG, + OPC_VSETVL = 0x80000057 | V_OPCFG, } RISCVInsn; /*@@ -354,6 +371,42 @@ static int32_t encode_uj(RISCVInsn opc, TCGReg rd, uint32_t imm) return opc | (rd & 0x1f) << 7 | encode_ujimm20(imm); } +typedef enum { + VTA_TU = 0, + VTA_TA, +} RISCVVta; + +typedef enum {
tcg/riscv/tcg-target.c.inc static int32_t encode_uj(RISCVInsn opc, TCGReg rd, uint32_t imm) · line 389
Quoted 10 lines
tcg/riscv/tcg-target.c.inc +10
-0+ VMA_MU = 0, + VMA_MA, +} RISCVVma; + +typedef enum { + VSEW_E8 = 0, /* EW=8b */ + VSEW_E16, /* EW=16b */ + VSEW_E32, /* EW=32b */ + VSEW_E64, /* EW=64b */ +} RISCVVsew;
This exactly aligns with MemOp and vece. Do we really need an enum for this?
Quoted 33 lines
tcg/riscv/tcg-target.c.inc +32
-0+ +typedef enum { + VLMUL_M1 = 0, /* LMUL=1 */ + VLMUL_M2, /* LMUL=2 */ + VLMUL_M4, /* LMUL=4 */ + VLMUL_M8, /* LMUL=8 */ + VLMUL_RESERVED, + VLMUL_MF8, /* LMUL=1/8 */ + VLMUL_MF4, /* LMUL=1/4 */ + VLMUL_MF2, /* LMUL=1/2 */ +} RISCVVlmul; +#define LMUL_MAX 8 + +static int32_t encode_vtype(RISCVVta vta, RISCVVma vma, + RISCVVsew vsew, RISCVVlmul vlmul) +{ + return (vma & 0x1) << 7 | (vta & 0x1) << 6 | (vsew & 0x7) << 3 | + (vlmul & 0x7); +} +static void tcg_out_vsetvl(TCGContext *s, uint32_t avl, RISCVVta vta, + RISCVVma vma, RISCVVsew vsew, + RISCVVlmul vlmul) +{ + int32_t vtypei = encode_vtype(vta, vma, vsew, vlmul); + + if (avl < 32) { + tcg_out_opc_vconfig(s, OPC_VSETIVLI, TCG_REG_ZERO, avl, vtypei); + } else { + tcg_out_opc_imm(s, OPC_ADDI, TCG_REG_TMP0, TCG_REG_ZERO, avl); + tcg_out_opc_vconfig(s, OPC_VSETVLI, TCG_REG_ZERO, TCG_REG_TMP0, vtypei); + } +}
tcg/riscv/tcg-target.c.inc static int32_t encode_uj(RISCVInsn opc, TCGReg rd, uint32_t imm)
Quoted 10 lines
tcg/riscv/tcg-target.c.inc +10
-0+ +/* + * TODO: If the vtype value is not supported by the implementation, + * then the vill bit is set in vtype, the remaining bits in + * vtype are set to zero, and the vl register is also set to zero + */ + +static __thread unsigned prev_size; +static __thread unsigned prev_vece = MO_8; +static __thread bool vec_vtpye_init = true;
Typo in vtpye.
That said, init should be redundant. I think you only need one variable here:
static __thread int prev_vtype;
Since any vtype < 0 is vill, the "uninitialized" value is easily -1.
tcg/riscv/tcg-target.c.inc static void tcg_out_opc_reg_vec_i(TCGContext *s, RISCVInsn opc, · line 604
Quoted 8 lines
tcg/riscv/tcg-target.c.inc +8
-0+static inline void tcg_target_set_vec_config(TCGContext *s, TCGType type, + unsigned vece) +{ + unsigned oprsz = get_vec_type_bytes(type); + + if (!vec_vtpye_init && (prev_size == oprsz && prev_vece == vece)) { + return ; + }
int vtype = encode_vtype(...);
if (vtype != prev_vtype) {
prev_vtype = vtype;
tcg_out_vsetvl(s, vtype);
}tcg/riscv/tcg-target.c.inc static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, · line 2036
Quoted 9 lines
tcg/riscv/tcg-target.c.inc +5
-0@@ -1914,6 +2029,11 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, const TCGArg args[TCG_MAX_OP_ARGS], const int const_args[TCG_MAX_OP_ARGS]) { + TCGType type = vecl + TCG_TYPE_V64; + + if (vec_vtpye_init) { + tcg_target_set_vec_config(s, type, vece); + }
Here is perhaps too early... see patch 8 re logicals.
r~
Re: [PATCH v1 05/15] tcg/riscv: Add riscv vset{i}vli support
Quoted 80 lines · Richard Henderson
Quoted 79 lines · LIU Zhiwei
From: TANG Tiancheng <[email protected]>
In RISC-V, vector operations require initial configuration using the vset{i}vl{i} instruction.
This instruction:
1. Sets the vector length (vl) in bytes
2. Configures the vtype register, which includes:
SEW (Single Element Width)
LMUL (vector register group multiplier) Other vector operation parametersThis configuration is crucial for defining subsequent vector operation behavior. To optimize performance, the configuration process is managed dynamically:
1. Reconfiguration using vset{i}vl{i} is necessary when SEW or vector register group width changes.
2. The vset instruction can be omitted when configuration remains unchanged.This optimization is only effective within a single TB. Each TB requires reconfiguration at its start, as the current state cannot be obtained from hardware.
Signed-off-by TANG Tiancheng <[email protected]>Signed-off-by Weiwei Li <[email protected]>Reviewed-by Liu Zhiwei <[email protected]>---
tcg/riscv/tcg-target.c.inc | 121 +++++++++++++++++++++++++++++++++++++1 file changed, 121 insertions(+)tcg/riscv/tcg-target.c.inc +11
-0@@ -167,6 +167,18 @@ static bool tcg_target_const_match(int64_t val, int ct, * RISC-V Base ISA opcodes (IM) */ +#define V_OPIVV (0x0 << 12) +#define V_OPFVV (0x1 << 12) +#define V_OPMVV (0x2 << 12) +#define V_OPIVI (0x3 << 12) +#define V_OPIVX (0x4 << 12) +#define V_OPFVF (0x5 << 12) +#define V_OPMVX (0x6 << 12) +#define V_OPCFG (0x7 << 12) + +#define V_SUMOP (0x0 << 20) +#define V_LUMOP (0x0 << 20) + typedef enum { OPC_ADD = 0x33,
OPC_ADDI=0x13,tcg/riscv/tcg-target.c.inc +14
-0@@ -262,6 +274,11 @@ typedef enum { /* Zicond: integer conditional operations */ OPC_CZERO_EQZ = 0x0e005033, OPC_CZERO_NEZ = 0x0e007033, + + /* V: Vector extension 1.0 */ + OPC_VSETVLI = 0x57 | V_OPCFG, + OPC_VSETIVLI = 0xc0000057 | V_OPCFG, + OPC_VSETVL = 0x80000057 | V_OPCFG, } RISCVInsn; /*@@ -354,6 +371,42 @@ static int32_t encode_uj(RISCVInsn opc, TCGReg rd, uint32_t imm) return opc | (rd & 0x1f) << 7 | encode_ujimm20(imm); } +typedef enum { + VTA_TU = 0, + VTA_TA, +} RISCVVta; + +typedef enum { + VMA_MU = 0, + VMA_MA, +} RISCVVma; +
Quoted 8 lines
tcg/riscv/tcg-target.c.inc static int32_t encode_uj(RISCVInsn opc, TCGReg rd, uint32_t imm) · line 389
Quoted 6 lines
tcg/riscv/tcg-target.c.inc +6
-0+typedef enum { + VSEW_E8 = 0, /* EW=8b */ + VSEW_E16, /* EW=16b */ + VSEW_E32, /* EW=32b */ + VSEW_E64, /* EW=64b */ +} RISCVVsew;This exactly aligns with MemOp and vece. Do we really need an enum for this?
OK. We will use MemOp enum next version.
Quoted 40 lines
Quoted 40 lines
tcg/riscv/tcg-target.c.inc +27
-0+ +typedef enum { + VLMUL_M1 = 0, /* LMUL=1 */ + VLMUL_M2, /* LMUL=2 */ + VLMUL_M4, /* LMUL=4 */ + VLMUL_M8, /* LMUL=8 */ + VLMUL_RESERVED, + VLMUL_MF8, /* LMUL=1/8 */ + VLMUL_MF4, /* LMUL=1/4 */ + VLMUL_MF2, /* LMUL=1/2 */ +} RISCVVlmul; +#define LMUL_MAX 8 + +static int32_t encode_vtype(RISCVVta vta, RISCVVma vma, + RISCVVsew vsew, RISCVVlmul vlmul) +{ + return (vma & 0x1) << 7 | (vta & 0x1) << 6 | (vsew & 0x7) << 3 | + (vlmul & 0x7); +} +static void tcg_out_vsetvl(TCGContext *s, uint32_t avl, RISCVVta vta, + RISCVVma vma, RISCVVsew vsew, + RISCVVlmul vlmul) +{ + int32_t vtypei = encode_vtype(vta, vma, vsew, vlmul); + + if (avl < 32) { + tcg_out_opc_vconfig(s, OPC_VSETIVLI, TCG_REG_ZERO, avl,vtypei);
+ } else {
+tcg_out_opc_imm(s, OPC_ADDI, TCG_REG_TMP0, TCG_REG_ZERO, avl); +tcg_out_opc_vconfig(s, OPC_VSETVLI, TCG_REG_ZERO, TCG_REG_TMP0, vtypei);
+ }tcg/riscv/tcg-target.c.inc +5
-0+} + +/* + * TODO: If the vtype value is not supported by the implementation, + * then the vill bit is set in vtype, the remaining bits in
Quoted 8 lines
tcg/riscv/tcg-target.c.inc static void tcg_out_opc_reg_vec_i(TCGContext *s, RISCVInsn opc, · line 590
Quoted 6 lines
+ * vtype are set to zero, and the vl register is also set to zero
+ */tcg/riscv/tcg-target.c.inc +4
-0+ +static __thread unsigned prev_size; +static __thread unsigned prev_vece = MO_8; +static __thread bool vec_vtpye_init = true;Typo in vtpye.
OK.
Quoted 4 lines
That said, init should be redundant. I think you only need one variable here:
static
__threadintprev_vtype;
Agree.
Quoted 2 lines
Since any vtype < 0 is vill, the "uninitialized" value is easily -1.
OK. We will set it to -1 in tcg_out_tb_start.
Quoted 17 lines
tcg/riscv/tcg-target.c.inc static void tcg_out_opc_reg_vec_i(TCGContext *s, RISCVInsn opc, · line 604
Quoted 10 lines
+static inline void tcg_target_set_vec_config(TCGContext *s, TCGType type,
+ unsigned vece)tcg/riscv/tcg-target.c.inc +6
-0+{ + unsigned oprsz = get_vec_type_bytes(type); + + if (!vec_vtpye_init && (prev_size == oprsz && prev_vece == vece)) { + return ; + }int vtype =
encode_vtype(...);
if (vtype !=prev_vtype) {
prev_vtype= vtype;
tcg_out_vsetvl(s, vtype);
}
Quoted 12 lines
tcg/riscv/tcg-target.c.inc static void tcg_out_vec_op(TCGContext *s, · line 2037
Quoted 10 lines
tcg/riscv/tcg-target.c.inc +5
-0@@ -1914,6 +2029,11 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, const TCGArg args[TCG_MAX_OP_ARGS], const int const_args[TCG_MAX_OP_ARGS]) { + TCGType type = vecl + TCG_TYPE_V64; + + if (vec_vtpye_init) { + tcg_target_set_vec_config(s, type, vece); + }Here is perhaps too early... see patch 8 re logicals.
I guess you mean we don't have implemented any vector op, so there is no need to set vsetvl in this patch. We will postpone it do really ops need it.
Thanks,
Zhiwei
Quoted 3 lines
r~
Re: [PATCH v1 05/15] tcg/riscv: Add riscv vset{i}vli support
Quoted 12 lines · LIU Zhiwei
Quoted 11 lines
tcg/riscv/tcg-target.c.inc static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, · line 2036
Quoted 9 lines
tcg/riscv/tcg-target.c.inc +5
-0@@ -1914,6 +2029,11 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, const TCGArg args[TCG_MAX_OP_ARGS], const int const_args[TCG_MAX_OP_ARGS]) { + TCGType type = vecl + TCG_TYPE_V64; + + if (vec_vtpye_init) { + tcg_target_set_vec_config(s, type, vece); + }Here is perhaps too early... see patch 8 re logicals.
Quoted 1 line
I guess you mean we don't have implemented any vector op, so there is no need to set vsetvl in this patch. We will postpone it do really ops need it.
What I meant is "too early in the function", i.e. before the switch.
Per my comment in patch 8, there are some vector ops that are agnostic to type and only care about length. Thus perhaps
switch (op) {
case xxx:
set_vec_config_len(s, type);
something;
case yyy:
set_vec_config_len_elt(s, type, vece);
something_else;
...
}Or some other structure that makes sense.
r~
Re: [PATCH v1 05/15] tcg/riscv: Add riscv vset{i}vli support
Quoted 25 lines · Richard Henderson
Quoted 13 lines · LIU Zhiwei
Quoted 4 lines
Quoted 4 lines
tcg/riscv/tcg-target.c.inc +0
-0@@ -1914,6 +2029,11 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, const TCGArg args[TCG_MAX_OP_ARGS], const int const_args[TCG_MAX_OP_ARGS])Quoted 8 lines
tcg/riscv/tcg-target.c.inc static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, · line 2036
Quoted 6 lines
tcg/riscv/tcg-target.c.inc +5
-0{ + TCGType type = vecl + TCG_TYPE_V64; + + if (vec_vtpye_init) { + tcg_target_set_vec_config(s, type, vece); + }Here is perhaps too early... see patch 8 re logicals.
Quoted 1 line
I guess you mean we don't have implemented any vector op, so there is no need to set vsetvl in this patch. We will postpone it do really ops need it.
What I meant is "too early in the function", i.e. before the switch.
Per my comment in patch 8, there are some vector ops that are agnostic to type and only care about length. Thus perhaps
switch (op) {
case xxx:
set_vec_config_len(s, type);
something;
Quoted 8 lines
case yyy:
set_vec_config_len_elt(s, type, vece);
something_else;...
}Or some other structure that makes sense.
Thanks for clarify once again. It's much better to explicitly have two API types.
Thanks,
Zhiwei
Quoted 3 lines
r~
PATCH 6/15 tcg/riscv: Implement vector load/store
In reply to LIU ZhiweiFrom: TANG Tiancheng <[email protected]>
--- tcg/riscv/tcg-target-con-set.h | 2 + tcg/riscv/tcg-target.c.inc | 92 ++++++++++++++++++++++++++++++++-- 2 files changed, 91 insertions(+), 3 deletions(-)
tcg/riscv/tcg-target-con-set.h +2 -0
@@ -21,3 +21,5 @@ C_O1_I2(r, rZ, rZ) C_N1_I2(r, r, rM) C_O1_I4(r, r, rI, rM, rM) C_O2_I4(r, r, rZ, rZ, rM, rM) +C_O0_I2(v, r) +C_O1_I1 (v, r)
tcg/riscv/tcg-target.c.inc +89 -3
138 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v1 06/15] tcg/riscv: Implement vector load/store
Quoted 19 lines · LIU Zhiwei
tcg/riscv/tcg-target.c.inc +14
-1@@ -827,14 +850,59 @@ static void tcg_out_ldst(TCGContext *s, RISCVInsn opc, TCGReg data, static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg arg, TCGReg arg1, intptr_t arg2) { - RISCVInsn insn = type == TCG_TYPE_I32 ? OPC_LW : OPC_LD; + RISCVInsn insn; + + if (type < TCG_TYPE_V64) { + insn = type == TCG_TYPE_I32 ? OPC_LW : OPC_LD; + } else { + tcg_debug_assert(arg >= TCG_REG_V1); + switch (prev_vece) { + case MO_8: + insn = OPC_VLE8_V; + break; + case MO_16: + insn = OPC_VLE16_V; + break; + case MO_32:
tcg/riscv/tcg-target.c.inc static void tcg_out_ldst(TCGContext *s, RISCVInsn opc, TCGReg data, · line 876
Quoted 10 lines
tcg/riscv/tcg-target.c.inc +9
-0+ insn = OPC_VLE32_V; + break; + case MO_64: + insn = OPC_VLE64_V; + break; + default: + g_assert_not_reached(); + } + } tcg_out_ldst(s, insn, arg, arg1, arg2);
tcg_out_ld/st are called directly from register allocation spill/fill. You'll need to set vtype here, and cannot rely on this having been done in tcg_out_vec_op.
That said, with a little-endian host, the selected element size doesn't matter *too* much.
A write of 8 uint16_t or a write of 2 uint64_t produces the same bits in memory.
Therefore you can examine prev_vtype and adjust only if the vector length changes. But we do that -- e.g. load V256, store V256, store V128 to perform a 384-bit store for AArch64 SVE when VQ=3.
Is there an advantage to using the vector load/store whole register insns, if the requested length is not too small? IIRC the NF field can be used to store multiples, but we can't store half of a register with these.
r~
Re: [PATCH v1 06/15] tcg/riscv: Implement vector load/store
Quoted 26 lines · Richard Henderson
Quoted 25 lines · LIU Zhiwei
tcg/riscv/tcg-target.c.inc +19
-1@@ -827,14 +850,59 @@ static void tcg_out_ldst(TCGContext *s, RISCVInsn opc, TCGReg data, static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg arg, TCGReg arg1, intptr_t arg2) { - RISCVInsn insn = type == TCG_TYPE_I32 ? OPC_LW : OPC_LD; + RISCVInsn insn; + + if (type < TCG_TYPE_V64) { + insn = type == TCG_TYPE_I32 ? OPC_LW : OPC_LD; + } else { + tcg_debug_assert(arg >= TCG_REG_V1); + switch (prev_vece) { + case MO_8: + insn = OPC_VLE8_V; + break; + case MO_16: + insn = OPC_VLE16_V; + break; + case MO_32: + insn = OPC_VLE32_V; + break; + case MO_64: + insn = OPC_VLE64_V; + break;
Quoted 8 lines
tcg/riscv/tcg-target.c.inc static void tcg_out_ldst(TCGContext *s, RISCVInsn opc, TCGReg data, · line 876
Quoted 5 lines
tcg/riscv/tcg-target.c.inc +4
-0+ default: + g_assert_not_reached(); + } + } tcg_out_ldst(s, insn, arg, arg1, arg2);tcg_out_ld/st are called directly from register allocation spill/fill. You'll need to set vtype here, and cannot rely on this having been done in
tcg_out_vec_op.
OK.
Quoted 4 lines
That said, with a little-endian host, the selected element size doesn't matter *too* much. A write of 8
uint16_tor a write of 2uint64_tproduces the same bits in memory.Therefore you can examine
prev_vtypeand adjust only if the vector length changes.
OK.
Quoted 3 lines
But we do that -- e.g. load V256, store V256, store V128 to perform a 384-bit store for AArch64 SVE when VQ=3.
Is there an advantage to using the vector load/store whole register insns, if the requested length is not too small?
For vector type equal or bigger than vlen in host, we will use the whole register instructions.
Quoted 1 line
IIRC the NF field can be used to store multiples, but we can't store half of a register with these.
I think we can still use the unit-stride instructions for them.
Thanks,
Zhiwei
Quoted 3 lines
r~
PATCH 7/15 tcg/riscv: Implement vector mov/dup{m/i}
In reply to LIU ZhiweiFrom: TANG Tiancheng <[email protected]>
--- tcg/riscv/tcg-target.c.inc | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+)
tcg/riscv/tcg-target.c.inc +43 -0
@@ -288,6 +288,10 @@ typedef enum { OPC_VSE16_V = 0x5027 | V_SUMOP, OPC_VSE32_V = 0x6027 | V_SUMOP, OPC_VSE64_V = 0x7027 | V_SUMOP, + + OPC_VMV_V_V = 0x5e000057 | V_OPIVV, + OPC_VMV_V_I = 0x5e000057 | V_OPIVI, + OPC_VMV_V_X = 0x5e000057 | V_OPIVX, } RISCVInsn; /*
@@ -641,6 +645,13 @@ static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg) case TCG_TYPE_I64: tcg_out_opc_imm(s, OPC_ADDI, ret, arg, 0); break; + case TCG_TYPE_V64: + case TCG_TYPE_V128: + case TCG_TYPE_V256: + tcg_debug_assert(ret > TCG_REG_V0 && arg > TCG_REG_V0); + tcg_target_set_vec_config(s, type, prev_vece); + tcg_out_opc_vv(s, OPC_VMV_V_V, ret, TCG_REG_V0, arg, true); + break; default: g_assert_not_reached(); }
@@ -977,6 +988,33 @@ static void tcg_out_addsub2(TCGContext *s, } } +static inline bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece, + TCGReg dst, TCGReg src) +{ + tcg_target_set_vec_config(s, type, vece); + tcg_out_opc_vx(s, OPC_VMV_V_X, dst, TCG_REG_V0, src, true); + return true; +} + +static inline bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece, + TCGReg dst, TCGReg base, intptr_t offset) +{ + tcg_out_ld(s, TCG_TYPE_REG, TCG_REG_TMP0, base, offset); + return tcg_out_dup_vec(s, type, vece, dst, TCG_REG_TMP0); +} + +static inline void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece, + TCGReg dst, int64_t arg) +{ + if (arg < 16 && arg >= -16) { + tcg_target_set_vec_config(s, type, vece); + tcg_out_opc_vi(s, OPC_VMV_V_I, dst, TCG_REG_V0, arg, true); + return; + } + tcg_out_movi(s, TCG_TYPE_I64, TCG_REG_TMP0, arg); + tcg_out_dup_vec(s, type, vece, dst, TCG_REG_TMP0); +} + static const struct { RISCVInsn op; bool swap;
@@ -2111,6 +2149,9 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, tcg_target_set_vec_config(s, type, vece); } switch (opc) { + case INDEX_op_dupm_vec: + tcg_out_dupm_vec(s, type, vece, a0, a1, a2); + break; case INDEX_op_ld_vec: tcg_out_ld(s, type, a0, a1, a2); break;
@@ -2282,6 +2323,8 @@ static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op) case INDEX_op_st_vec: return C_O0_I2(v, r); + case INDEX_op_dup_vec: + case INDEX_op_dupm_vec: case INDEX_op_ld_vec: return C_O1_I1(v, r); default:
Signature · 1 line
2.43.0
Re: [PATCH v1 07/15] tcg/riscv: Implement vector mov/dup{m/i}
tcg/riscv/tcg-target.c.inc static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg) · line 653
Quoted 10 lines · LIU Zhiwei
tcg/riscv/tcg-target.c.inc +6
-0@@ -641,6 +645,13 @@ static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg) case TCG_TYPE_I64: tcg_out_opc_imm(s, OPC_ADDI, ret, arg, 0); break; + case TCG_TYPE_V64: + case TCG_TYPE_V128: + case TCG_TYPE_V256: + tcg_debug_assert(ret > TCG_REG_V0 && arg > TCG_REG_V0); + tcg_target_set_vec_config(s, type, prev_vece); + tcg_out_opc_vv(s, OPC_VMV_V_V, ret, TCG_REG_V0, arg, true);
I suggest these asserts be in tcg_out_opc_*
That way you don't need to replicate to all uses.
tcg/riscv/tcg-target.c.inc static void tcg_out_addsub2(TCGContext *s, · line 992
Quoted 2 lines
tcg/riscv/tcg-target.c.inc +2
-0+static inline bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece, + TCGReg dst, TCGReg src)
Oh, please drop all of the inline markup, from all patches. Let the compiler decide.
tcg/riscv/tcg-target.c.inc static void tcg_out_addsub2(TCGContext *s, · line 1004
Quoted 6 lines
tcg/riscv/tcg-target.c.inc +6
-0+static inline bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece, + TCGReg dst, TCGReg base, intptr_t offset) +{ + tcg_out_ld(s, TCG_TYPE_REG, TCG_REG_TMP0, base, offset); + return tcg_out_dup_vec(s, type, vece, dst, TCG_REG_TMP0); +}
Is this really better than using strided load with rs2 = r0?
r~
Re: [PATCH v1 07/15] tcg/riscv: Implement vector mov/dup{m/i}
Quoted 15 lines · Richard Henderson
Quoted 11 lines · LIU Zhiwei
(quoted diff) +0
-0@@ -641,6 +645,13 @@ static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)case
TCG_TYPE_I64:
tcg_out_opc_imm(s, OPC_ADDI, ret, arg, 0); break;
+ caseTCG_TYPE_V64:
+ caseTCG_TYPE_V128:
+ caseTCG_TYPE_V256:
+tcg_debug_assert(ret > TCG_REG_V0 && arg > TCG_REG_V0); +tcg_target_set_vec_config(s, type, prev_vece);
+tcg_out_opc_vv(s, OPC_VMV_V_V, ret, TCG_REG_V0, arg, true);I suggest these asserts be in tcg_out_opc_*
That way you don't need to replicate to all uses.
OK.
Quoted 8 lines
Quoted 4 lines
+static inline bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
+ TCGReg dst, TCGReg src)Oh, please drop all of the inline markup, from all patches. Let the compiler decide.
OK.
Quoted 10 lines
tcg/riscv/tcg-target.c.inc static void tcg_out_addsub2(TCGContext *s, · line 1004
Quoted 8 lines
+static inline bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece,
+ TCGReg dst, TCGReg base,intptr_toffset)tcg/riscv/tcg-target.c.inc +4
-0+{ + tcg_out_ld(s, TCG_TYPE_REG, TCG_REG_TMP0, base, offset); + return tcg_out_dup_vec(s, type, vece, dst, TCG_REG_TMP0); +}Is this really better than using strided load with rs2 = r0?
It depends. For our test board, it is.
Thanks,
Zhiwei
Quoted 3 lines
r~
Re: [PATCH v1 07/15] tcg/riscv: Implement vector mov/dup{m/i}
In reply to LIU Zhiweitcg/riscv/tcg-target.c.inc static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg) · line 654
Quoted 7 lines · LIU Zhiwei
tcg/riscv/tcg-target.c.inc +7
-0+ case TCG_TYPE_V64: + case TCG_TYPE_V128: + case TCG_TYPE_V256: + tcg_debug_assert(ret > TCG_REG_V0 && arg > TCG_REG_V0); + tcg_target_set_vec_config(s, type, prev_vece); + tcg_out_opc_vv(s, OPC_VMV_V_V, ret, TCG_REG_V0, arg, true); + break;
Is it worth using whole register move (vmvNr.v) for the appropriate VLEN?
r~
Re: [PATCH v1 07/15] tcg/riscv: Implement vector mov/dup{m/i}
Quoted 10 lines · Richard Henderson
Quoted 7 lines · LIU Zhiwei
+ case
TCG_TYPE_V64:
+ caseTCG_TYPE_V128:
+ caseTCG_TYPE_V256:
+tcg_debug_assert(ret > TCG_REG_V0 && arg > TCG_REG_V0); +tcg_target_set_vec_config(s, type, prev_vece);
+tcg_out_opc_vv(s, OPC_VMV_V_V, ret, TCG_REG_V0, arg, true); + break;Is it worth using whole register move (vmvNr.v) for the appropriate VLEN?
Yes. We will use vmvNr.v next version. Thanks for your suggestion.
Zhiwei
Quoted 3 lines
r~
PATCH 8/15 tcg/riscv: Add support for basic vector opcodes
In reply to LIU ZhiweiFrom: TANG Tiancheng <[email protected]>
--- tcg/riscv/tcg-target-con-set.h | 1 + tcg/riscv/tcg-target.c.inc | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+)
tcg/riscv/tcg-target-con-set.h +1 -0
@@ -23,3 +23,4 @@ C_O1_I4(r, r, rI, rM, rM) C_O2_I4(r, r, rZ, rZ, rM, rM) C_O0_I2(v, r) C_O1_I1(v, r) +C_O1_I2 (v, v, v)
tcg/riscv/tcg-target.c.inc +33 -0
@@ -289,6 +289,12 @@ typedef enum { OPC_VSE32_V = 0x6027 | V_SUMOP, OPC_VSE64_V = 0x7027 | V_SUMOP, + OPC_VADD_VV = 0x57 | V_OPIVV, + OPC_VSUB_VV = 0x8000057 | V_OPIVV, + OPC_VAND_VV = 0x24000057 | V_OPIVV, + OPC_VOR_VV = 0x28000057 | V_OPIVV, + OPC_VXOR_VV = 0x2c000057 | V_OPIVV, + OPC_VMV_V_V = 0x5e000057 | V_OPIVV, OPC_VMV_V_I = 0x5e000057 | V_OPIVI, OPC_VMV_V_X = 0x5e000057 | V_OPIVX,
@@ -2158,6 +2164,21 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, case INDEX_op_st_vec: tcg_out_st(s, type, a0, a1, a2); break; + case INDEX_op_add_vec: + tcg_out_opc_vv(s, OPC_VADD_VV, a0, a1, a2, true); + break; + case INDEX_op_sub_vec: + tcg_out_opc_vv(s, OPC_VSUB_VV, a0, a1, a2, true); + break; + case INDEX_op_and_vec: + tcg_out_opc_vv(s, OPC_VAND_VV, a0, a1, a2, true); + break; + case INDEX_op_or_vec: + tcg_out_opc_vv(s, OPC_VOR_VV, a0, a1, a2, true); + break; + case INDEX_op_xor_vec: + tcg_out_opc_vv(s, OPC_VXOR_VV, a0, a1, a2, true); + break; case INDEX_op_mov_vec: /* Always emitted via tcg_out_mov. */ case INDEX_op_dup_vec: /* Always emitted via tcg_out_dup_vec. */ default:
@@ -2177,6 +2198,12 @@ void tcg_expand_vec_op(TCGOpcode opc, TCGType type, unsigned vece, int tcg_can_emit_vec_op(TCGOpcode opc, TCGType type, unsigned vece) { switch (opc) { + case INDEX_op_add_vec: + case INDEX_op_sub_vec: + case INDEX_op_and_vec: + case INDEX_op_or_vec: + case INDEX_op_xor_vec: + return 1; default: return 0; }
@@ -2327,6 +2354,12 @@ static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op) case INDEX_op_dupm_vec: case INDEX_op_ld_vec: return C_O1_I1(v, r); + case INDEX_op_add_vec: + case INDEX_op_sub_vec: + case INDEX_op_and_vec: + case INDEX_op_or_vec: + case INDEX_op_xor_vec: + return C_O1_I2(v, v, v); default: g_assert_not_reached(); }
Signature · 1 line
2.43.0
Re: [PATCH v1 08/15] tcg/riscv: Add support for basic vector opcodes
Quoted 44 lines · LIU Zhiwei
From: TANG Tiancheng <[email protected]>
Signed-off-by TANG Tiancheng <[email protected]>Reviewed-by Liu Zhiwei <[email protected]>--- tcg/riscv/tcg-target-con-set.h | 1 + tcg/riscv/tcg-target.c.inc | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+)tcg/riscv/tcg-target-con-set.h +1
-0@@ -23,3 +23,4 @@ C_O1_I4(r, r, rI, rM, rM) C_O2_I4(r, r, rZ, rZ, rM, rM) C_O0_I2(v, r) C_O1_I1(v, r) +C_O1_I2 (v, v, v)tcg/riscv/tcg-target.c.inc +11
-0@@ -289,6 +289,12 @@ typedef enum { OPC_VSE32_V = 0x6027 | V_SUMOP, OPC_VSE64_V = 0x7027 | V_SUMOP, + OPC_VADD_VV = 0x57 | V_OPIVV, + OPC_VSUB_VV = 0x8000057 | V_OPIVV, + OPC_VAND_VV = 0x24000057 | V_OPIVV, + OPC_VOR_VV = 0x28000057 | V_OPIVV, + OPC_VXOR_VV = 0x2c000057 | V_OPIVV, + OPC_VMV_V_V = 0x5e000057 | V_OPIVV, OPC_VMV_V_I = 0x5e000057 | V_OPIVI, OPC_VMV_V_X = 0x5e000057 | V_OPIVX,@@ -2158,6 +2164,21 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, case INDEX_op_st_vec: tcg_out_st(s, type, a0, a1, a2); break; + case INDEX_op_add_vec: + tcg_out_opc_vv(s, OPC_VADD_VV, a0, a1, a2, true); + break; + case INDEX_op_sub_vec: + tcg_out_opc_vv(s, OPC_VSUB_VV, a0, a1, a2, true);
tcg/riscv/tcg-target.c.inc static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, · line 2181
Quoted 10 lines
tcg/riscv/tcg-target.c.inc +10
-0+ break; + case INDEX_op_and_vec: + tcg_out_opc_vv(s, OPC_VAND_VV, a0, a1, a2, true); + break; + case INDEX_op_or_vec: + tcg_out_opc_vv(s, OPC_VOR_VV, a0, a1, a2, true); + break; + case INDEX_op_xor_vec: + tcg_out_opc_vv(s, OPC_VXOR_VV, a0, a1, a2, true); + break;
As with load/store/move, and/or/xor can avoid changing element type. Thus I think the vtype setup before the switch is premature.
r~
Re: [PATCH v1 08/15] tcg/riscv: Add support for basic vector opcodes
Quoted 50 lines · Richard Henderson
Quoted 49 lines · LIU Zhiwei
From: TANG Tiancheng <[email protected]>
Signed-off-by TANG Tiancheng <[email protected]>Reviewed-by Liu Zhiwei <[email protected]>---
tcg/riscv/tcg-target-con-set.h| 1 + tcg/riscv/tcg-target.c.inc | 33 +++++++++++++++++++++++++++++++++2 files changed, 34 insertions(+)tcg/riscv/tcg-target-con-set.h +1
-0@@ -23,3 +23,4 @@ C_O1_I4(r, r, rI, rM, rM) C_O2_I4(r, r, rZ, rZ, rM, rM) C_O0_I2(v, r) C_O1_I1(v, r) +C_O1_I2 (v, v, v)tcg/riscv/tcg-target.c.inc +5
-0@@ -289,6 +289,12 @@ typedef enum { OPC_VSE32_V = 0x6027 | V_SUMOP, OPC_VSE64_V = 0x7027 | V_SUMOP, + OPC_VADD_VV = 0x57 | V_OPIVV, + OPC_VSUB_VV = 0x8000057 | V_OPIVV, + OPC_VAND_VV = 0x24000057 | V_OPIVV, + OPC_VOR_VV = 0x28000057 | V_OPIVV, + OPC_VXOR_VV = 0x2c000057 | V_OPIVV, + OPC_VMV_V_V = 0x5e000057 | V_OPIVV, OPC_VMV_V_I = 0x5e000057 | V_OPIVI, OPC_VMV_V_X = 0x5e000057 | V_OPIVX,@@ -2158,6 +2164,21 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,case
INDEX_op_st_vec:
tcg_out_st(s, type, a0, a1, a2);
break;
+ caseINDEX_op_add_vec:
+tcg_out_opc_vv(s, OPC_VADD_VV, a0, a1, a2, true); + break;
+ caseINDEX_op_sub_vec:
+tcg_out_opc_vv(s, OPC_VSUB_VV, a0, a1, a2, true); + break;
+ caseINDEX_op_and_vec:
+tcg_out_opc_vv(s, OPC_VAND_VV, a0, a1, a2, true); + break;
+ caseINDEX_op_or_vec:
Quoted 8 lines
Quoted 5 lines
+
tcg_out_opc_vv(s, OPC_VOR_VV, a0, a1, a2, true); + break;
+ caseINDEX_op_xor_vec:
+tcg_out_opc_vv(s, OPC_VXOR_VV, a0, a1, a2, true); + break;As with load/store/move, and/or/xor can avoid changing element type. Thus I think the vtype setup before the switch is premature.
Agree. We have implemented this feature on the v2 patch set.
Thanks,
Zhiwei
Quoted 3 lines
r~
Re: [PATCH v1 08/15] tcg/riscv: Add support for basic vector opcodes
In reply to LIU Zhiweitcg/riscv/tcg-target.c.inc typedef enum { · line 296
Quoted 5 lines · LIU Zhiwei
tcg/riscv/tcg-target.c.inc +5
-0+ OPC_VADD_VV = 0x57 | V_OPIVV, + OPC_VSUB_VV = 0x8000057 | V_OPIVV, + OPC_VAND_VV = 0x24000057 | V_OPIVV, + OPC_VOR_VV = 0x28000057 | V_OPIVV, + OPC_VXOR_VV = 0x2c000057 | V_OPIVV,
Immediate operand variants to be handled as a follow-up?
r~
Re: [PATCH v1 08/15] tcg/riscv: Add support for basic vector opcodes
Quoted 8 lines · Richard Henderson
Quoted 5 lines · LIU Zhiwei
+
OPC_VADD_VV=0x57|V_OPIVV,
+OPC_VSUB_VV=0x8000057|V_OPIVV,
+OPC_VAND_VV=0x24000057|V_OPIVV,
+OPC_VOR_VV=0x28000057|V_OPIVV,
+OPC_VXOR_VV=0x2c000057|V_OPIVV,Immediate operand variants to be handled as a follow-up?
Do you mean VXOR_VI? We use vxor.vi for not_vec already in patch 10.
Zhiwei
Quoted 3 lines
r~
Re: [PATCH v1 08/15] tcg/riscv: Add support for basic vector opcodes
Quoted 11 lines · LIU Zhiwei
Quoted 8 lines · Richard Henderson
Quoted 5 lines · LIU Zhiwei
+
OPC_VADD_VV=0x57|V_OPIVV,
+OPC_VSUB_VV=0x8000057|V_OPIVV,
+OPC_VAND_VV=0x24000057|V_OPIVV,
+OPC_VOR_VV=0x28000057|V_OPIVV,
+OPC_VXOR_VV=0x2c000057|V_OPIVV,Immediate operand variants to be handled as a follow-up?
Do you mean
VXOR_VI? We use vxor.vi fornot_vecalready in patch 10.
Yes, and you match the 5-bit signed operand in patch 9. All that is required is to put the two together here.
r~
PATCH 9/15 tcg/riscv: Implement vector cmp ops
In reply to LIU ZhiweiFrom: TANG Tiancheng <[email protected]>
1.Address immediate value constraints in RISC-V Vector Extension 1.0 for comparison instructions.
2.Extend comparison results from mask registers to SEW-width elements,
following recommendations in The RISC-V SPEC Volume I (Version 20240411).
This aligns with TCG's cmp_vec behavior by expanding compare results to full element width: all 1s for true, all 0s for false.
--- tcg/riscv/tcg-target-con-set.h | 2 + tcg/riscv/tcg-target-con-str.h | 1 + tcg/riscv/tcg-target.c.inc | 188 +++++++++++++++++++++++++++++++++ tcg/riscv/tcg-target.opc.h | 3 + 4 files changed, 194 insertions(+)
tcg/riscv/tcg-target-con-set.h +2 -0
@@ -22,5 +22,7 @@ C_N1_I2(r, r, rM) C_O1_I4(r, r, rI, rM, rM) C_O2_I4(r, r, rZ, rZ, rM, rM) C_O0_I2(v, r) +C_O0_I2(v, vK) C_O1_I1(v, r) C_O1_I2(v, v, v) +C_O1_I2 (v, v, vK)
tcg/riscv/tcg-target-con-str.h +1 -0
@@ -17,6 +17,7 @@ REGS('v', ALL_VECTOR_REGS)*/ CONST('I', TCG_CT_CONST_S12) CONST('J', TCG_CT_CONST_J12) +CONST('K', TCG_CT_CONST_S5) CONST('N', TCG_CT_CONST_N12) CONST('M', TCG_CT_CONST_M12)CONST ('Z' ,TCG_CT_CONST_ZERO )
tcg/riscv/tcg-target.c.inc +188 -0
258 lines not on this page · raw message
tcg/riscv/tcg-target.opc.h +3 -0
@@ -10,3 +10,6 @@* emitted by tcg_expand_vec_op. For those familiar with GCC internals, * consider these to be UNSPEC with names. */ + +DEF(rvv_cmpcond_vec, 0, 2, 1, IMPLVEC) +DEF (rvv_merge_vec,1 ,2 ,0 ,IMPLVEC )
Signature · 1 line
2.43.0
Re: [PATCH v1 09/15] tcg/riscv: Implement vector cmp ops
Quoted 150 lines · LIU Zhiwei
From: TANG Tiancheng <[email protected]>
1.Address immediate value constraints in RISC-V Vector Extension 1.0 for comparison instructions.
2.Extend comparison results from mask registers to SEW-width elements,
following recommendations in The RISC-V SPEC Volume I (Version 20240411).This aligns with TCG's
cmp_vecbehavior by expanding compare results to full element width: all 1s for true, all 0s for false.Signed-off-by TANG Tiancheng <[email protected]>Reviewed-by Liu Zhiwei <[email protected]>--- tcg/riscv/tcg-target-con-set.h | 2 + tcg/riscv/tcg-target-con-str.h | 1 + tcg/riscv/tcg-target.c.inc | 188 +++++++++++++++++++++++++++++++++ tcg/riscv/tcg-target.opc.h | 3 + 4 files changed, 194 insertions(+)tcg/riscv/tcg-target-con-set.h +2
-0@@ -22,5 +22,7 @@ C_N1_I2(r, r, rM) C_O1_I4(r, r, rI, rM, rM) C_O2_I4(r, r, rZ, rZ, rM, rM) C_O0_I2(v, r) +C_O0_I2(v, vK) C_O1_I1(v, r) C_O1_I2(v, v, v) +C_O1_I2 (v, v, vK)tcg/riscv/tcg-target-con-str.h +1
-0@@ -17,6 +17,7 @@ REGS('v', ALL_VECTOR_REGS)*/ CONST('I', TCG_CT_CONST_S12) CONST('J', TCG_CT_CONST_J12) +CONST('K', TCG_CT_CONST_S5) CONST('N', TCG_CT_CONST_N12) CONST('M', TCG_CT_CONST_M12)CONST ('Z' ,TCG_CT_CONST_ZERO )tcg/riscv/tcg-target.c.inc +60
-0@@ -113,6 +113,7 @@ static TCGReg tcg_target_call_oarg_reg(TCGCallReturnKind kind, int slot) #define TCG_CT_CONST_N12 0x400 #define TCG_CT_CONST_M12 0x800 #define TCG_CT_CONST_J12 0x1000 +#define TCG_CT_CONST_S5 0x2000 #define ALL_GENERAL_REGS MAKE_64BIT_MASK(0, 32) #define ALL_VECTOR_REGS MAKE_64BIT_MASK(33, 31)@@ -160,6 +161,13 @@ static bool tcg_target_const_match(int64_t val, int ct, if ((ct & TCG_CT_CONST_J12) && ~val >= -0x800 && ~val <= 0x7ff) { return 1; } + /* + * Sign extended from 5 bits: [-0x10, 0x0f]. + * Used for vector-immediate. + */ + if ((ct & TCG_CT_CONST_S5) && val >= -0x10 && val <= 0x0f) { + return 1; + } return 0; }@@ -289,12 +297,39 @@ typedef enum { OPC_VSE32_V = 0x6027 | V_SUMOP, OPC_VSE64_V = 0x7027 | V_SUMOP, + OPC_VMERGE_VIM = 0x5c000057 | V_OPIVI, + OPC_VMERGE_VVM = 0x5c000057 | V_OPIVV, + OPC_VMNAND_MM = 0x74000057 | V_OPMVV, + OPC_VADD_VV = 0x57 | V_OPIVV, OPC_VSUB_VV = 0x8000057 | V_OPIVV, OPC_VAND_VV = 0x24000057 | V_OPIVV, OPC_VOR_VV = 0x28000057 | V_OPIVV, OPC_VXOR_VV = 0x2c000057 | V_OPIVV, + OPC_VMSEQ_VV = 0x60000057 | V_OPIVV, + OPC_VMSEQ_VI = 0x60000057 | V_OPIVI, + OPC_VMSEQ_VX = 0x60000057 | V_OPIVX, + OPC_VMSNE_VV = 0x64000057 | V_OPIVV, + OPC_VMSNE_VI = 0x64000057 | V_OPIVI, + OPC_VMSNE_VX = 0x64000057 | V_OPIVX, + + OPC_VMSLTU_VV = 0x68000057 | V_OPIVV, + OPC_VMSLTU_VX = 0x68000057 | V_OPIVX, + OPC_VMSLT_VV = 0x6c000057 | V_OPIVV, + OPC_VMSLT_VX = 0x6c000057 | V_OPIVX, + OPC_VMSLEU_VV = 0x70000057 | V_OPIVV, + OPC_VMSLEU_VX = 0x70000057 | V_OPIVX, + OPC_VMSLE_VV = 0x74000057 | V_OPIVV, + OPC_VMSLE_VX = 0x74000057 | V_OPIVX, + + OPC_VMSLEU_VI = 0x70000057 | V_OPIVI, + OPC_VMSLE_VI = 0x74000057 | V_OPIVI, + OPC_VMSGTU_VI = 0x78000057 | V_OPIVI, + OPC_VMSGTU_VX = 0x78000057 | V_OPIVX, + OPC_VMSGT_VI = 0x7c000057 | V_OPIVI, + OPC_VMSGT_VX = 0x7c000057 | V_OPIVX, + OPC_VMV_V_V = 0x5e000057 | V_OPIVV, OPC_VMV_V_I = 0x5e000057 | V_OPIVI, OPC_VMV_V_X = 0x5e000057 | V_OPIVX,@@ -575,6 +610,15 @@ static void tcg_out_opc_vec_config(TCGContext *s, RISCVInsn opc, #define tcg_out_opc_vi(s, opc, vd, vs2, imm, vm) \ tcg_out_opc_reg_vec_i(s, opc, vd, imm, vs2, vm); +#define tcg_out_opc_vim_mask(s, opc, vd, vs2, imm) \ + tcg_out_opc_reg_vec_i(s, opc, vd, imm, vs2, false); + +#define tcg_out_opc_vvm_mask(s, opc, vd, vs2, vs1) \ + tcg_out_opc_reg_vec(s, opc, vd, vs1, vs2, false); + +#define tcg_out_opc_mvv(s, opc, vd, vs2, vs1, vm) \ + tcg_out_opc_reg_vec(s, opc, vd, vs1, vs2, vm); + #define tcg_out_opc_vconfig(s, opc, rd, avl, vtypei) \ tcg_out_opc_vec_config(s, opc, rd, avl, vtypei);@@ -1037,6 +1081,22 @@ static const struct { [TCG_COND_GTU] = { OPC_BLTU, true } }; +static const struct { + RISCVInsn opc; + bool swap; +} tcg_cmpcond_to_rvv_vv[] = { + [TCG_COND_EQ] = { OPC_VMSEQ_VV, false }, + [TCG_COND_NE] = { OPC_VMSNE_VV, false }, + [TCG_COND_LT] = { OPC_VMSLT_VV, false }, + [TCG_COND_GE] = { OPC_VMSLE_VV, true }, + [TCG_COND_GT] = { OPC_VMSLT_VV, true }, + [TCG_COND_LE] = { OPC_VMSLE_VV, false }, + [TCG_COND_LTU] = { OPC_VMSLTU_VV, false }, + [TCG_COND_GEU] = { OPC_VMSLEU_VV, true }, + [TCG_COND_GTU] = { OPC_VMSLTU_VV, true }, + [TCG_COND_LEU] = { OPC_VMSLEU_VV, false } +}; + static void tcg_out_brcond(TCGContext *s, TCGCond cond, TCGReg arg1, TCGReg arg2, TCGLabel *l) {
tcg/riscv/tcg-target.c.inc static void tcg_out_brcond(TCGContext *s, TCGCond cond, TCGReg arg1, · line 1119
Quoted 6 lines
tcg/riscv/tcg-target.c.inc +3
-0@@ -1054,6 +1114,79 @@ static void tcg_out_brcond(TCGContext *s, TCGCond cond, TCGReg arg1, tcg_out_opc_branch(s, op, arg1, arg2, 0); } +static const struct { + RISCVInsn op; + bool expand;
invert is probably a better name.
Why are these tables so far apart?
Quoted 5 lines
tcg/riscv/tcg-target.c.inc +5
-0+static void tcg_out_cmp_vec_vx(TCGContext *s, TCGCond cond, TCGReg arg1, + tcg_target_long arg2) +{ + RISCVInsn op; +
tcg/riscv/tcg-target.c.inc static void tcg_out_brcond(TCGContext *s, TCGCond cond, TCGReg arg1, · line 1147
Quoted 10 lines
tcg/riscv/tcg-target.c.inc +10
-0+ tcg_debug_assert((unsigned)cond < ARRAY_SIZE(tcg_cmpcond_to_rvv_vx)); + op = tcg_cmpcond_to_rvv_vx[cond].op; + tcg_debug_assert(op != 0); + + tcg_out_opc_vx(s, op, TCG_REG_V0, arg1, arg2, true); + if (tcg_cmpcond_to_rvv_vx[cond].expand) { + tcg_out_opc_mvv(s, OPC_VMNAND_MM, TCG_REG_V0, TCG_REG_V0, + TCG_REG_V0, false); + } +}
I think you'll be better served by handling the invert during expand, because you can always swap the sense of the predicate in the user.
Compare tcg/i386 expand_vec_cmp_noinv.
tcg/riscv/tcg-target.c.inc static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, · line 2367
Quoted 1 line
tcg/riscv/tcg-target.c.inc +1
-0+ tcg_gen_mov_vec(v0, tcg_constant_vec_matching(v0, vece, 0));
You don't need to copy to v0; just use the tcg_constant_vec directly as
tcg/riscv/tcg-target.c.inc static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, · line 2370
Quoted 3 lines
tcg/riscv/tcg-target.c.inc +3
-0+ vec_gen_3(INDEX_op_rvv_merge_vec, type, vece, + tcgv_vec_arg(v0), tcgv_vec_arg(v0), + tcgv_i64_arg(tcg_constant_i64(-1)));
the first source operand.
You can swap 0 and -1 if the comparison instruction requires the predicate to be inverted.
r~
Re: [PATCH v1 09/15] tcg/riscv: Implement vector cmp ops
Quoted 147 lines · Richard Henderson
Quoted 146 lines · LIU Zhiwei
From: TANG Tiancheng <[email protected]>
1.Address immediate value constraints in RISC-V Vector Extension 1.0 for comparison instructions.
2.Extend comparison results from mask registers to SEW-width elements, following recommendations in The RISC-V SPEC Volume I (Version 20240411).
This aligns with TCG's
cmp_vecbehavior by expanding compare results to full element width: all 1s for true, all 0s for false.Signed-off-by TANG Tiancheng <[email protected]>Reviewed-by Liu Zhiwei <[email protected]>---
tcg/riscv/tcg-target-con-set.h| 2 +tcg/riscv/tcg-target-con-str.h| 1 + tcg/riscv/tcg-target.c.inc | 188 +++++++++++++++++++++++++++++++++ tcg/riscv/tcg-target.opc.h | 3 +4 files changed, 194 insertions(+)tcg/riscv/tcg-target-con-set.h +2
-0@@ -22,5 +22,7 @@ C_N1_I2(r, r, rM) C_O1_I4(r, r, rI, rM, rM) C_O2_I4(r, r, rZ, rZ, rM, rM) C_O0_I2(v, r) +C_O0_I2(v, vK) C_O1_I1(v, r) C_O1_I2(v, v, v) +C_O1_I2 (v, v, vK)tcg/riscv/tcg-target-con-str.h +1
-0@@ -17,6 +17,7 @@ REGS('v', ALL_VECTOR_REGS)*/ CONST('I', TCG_CT_CONST_S12) CONST('J', TCG_CT_CONST_J12) +CONST('K', TCG_CT_CONST_S5) CONST('N', TCG_CT_CONST_N12) CONST('M', TCG_CT_CONST_M12) CONST('Z', TCG_CT_CONST_ZERO)tcg/riscv/tcg-target.c.inc +8
-0@@ -113,6 +113,7 @@ static TCGReg tcg_target_call_oarg_reg(TCGCallReturnKind kind, int slot) #define TCG_CT_CONST_N12 0x400 #define TCG_CT_CONST_M12 0x800 #define TCG_CT_CONST_J12 0x1000 +#define TCG_CT_CONST_S5 0x2000 #define ALL_GENERAL_REGS MAKE_64BIT_MASK(0, 32) #define ALL_VECTOR_REGS MAKE_64BIT_MASK(33, 31)@@ -160,6 +161,13 @@ static bool tcg_target_const_match(int64_t val, int ct, if ((ct & TCG_CT_CONST_J12) && ~val >= -0x800 && ~val <= 0x7ff) { return 1; } + /* + * Sign extended from 5 bits: [-0x10, 0x0f]. + * Used for vector-immediate. + */ + if ((ct & TCG_CT_CONST_S5) && val >= -0x10 && val <= 0x0f) { + return 1; + } return 0; }@@ -289,12 +297,39 @@ typedef enum {
OPC_VSE32_V=0x6027|V_SUMOP,
OPC_VSE64_V=0x7027|V_SUMOP,
+OPC_VMERGE_VIM=0x5c000057|V_OPIVI,
+OPC_VMERGE_VVM=0x5c000057|V_OPIVV,
+OPC_VMNAND_MM=0x74000057|V_OPMVV,
+
OPC_VADD_VV=0x57|V_OPIVV,
OPC_VSUB_VV=0x8000057|V_OPIVV,
OPC_VAND_VV=0x24000057|V_OPIVV,
OPC_VOR_VV=0x28000057|V_OPIVV,
OPC_VXOR_VV=0x2c000057|V_OPIVV,
+OPC_VMSEQ_VV=0x60000057|V_OPIVV,+ OPC_VMSEQ_VI = 0x60000057 | V_OPIVI, + OPC_VMSEQ_VX = 0x60000057 | V_OPIVX, + OPC_VMSNE_VV = 0x64000057 | V_OPIVV, + OPC_VMSNE_VI = 0x64000057 | V_OPIVI, + OPC_VMSNE_VX = 0x64000057 | V_OPIVX,tcg/riscv/tcg-target.c.inc +17
-0+ + OPC_VMSLTU_VV = 0x68000057 | V_OPIVV, + OPC_VMSLTU_VX = 0x68000057 | V_OPIVX, + OPC_VMSLT_VV = 0x6c000057 | V_OPIVV, + OPC_VMSLT_VX = 0x6c000057 | V_OPIVX, + OPC_VMSLEU_VV = 0x70000057 | V_OPIVV, + OPC_VMSLEU_VX = 0x70000057 | V_OPIVX, + OPC_VMSLE_VV = 0x74000057 | V_OPIVV, + OPC_VMSLE_VX = 0x74000057 | V_OPIVX, + + OPC_VMSLEU_VI = 0x70000057 | V_OPIVI, + OPC_VMSLE_VI = 0x74000057 | V_OPIVI, + OPC_VMSGTU_VI = 0x78000057 | V_OPIVI, + OPC_VMSGTU_VX = 0x78000057 | V_OPIVX, + OPC_VMSGT_VI = 0x7c000057 | V_OPIVI, + OPC_VMSGT_VX = 0x7c000057 | V_OPIVX, +OPC_VMV_V_V = 0x5e000057 | V_OPIVV, OPC_VMV_V_I = 0x5e000057 | V_OPIVI, OPC_VMV_V_X = 0x5e000057 | V_OPIVX,tcg/riscv/tcg-target.c.inc +8
-0@@ -575,6 +610,15 @@ static void tcg_out_opc_vec_config(TCGContext *s, RISCVInsn opc, #define tcg_out_opc_vi(s, opc, vd, vs2, imm, vm) \ tcg_out_opc_reg_vec_i(s, opc, vd, imm, vs2, vm); +#define tcg_out_opc_vim_mask(s, opc, vd, vs2, imm) \ + tcg_out_opc_reg_vec_i(s, opc, vd, imm, vs2, false); + +#define tcg_out_opc_vvm_mask(s, opc, vd, vs2, vs1) \ + tcg_out_opc_reg_vec(s, opc, vd, vs1, vs2, false); + +#define tcg_out_opc_mvv(s, opc, vd, vs2, vs1, vm) \ + tcg_out_opc_reg_vec(s, opc, vd, vs1, vs2, vm); + #define tcg_out_opc_vconfig(s, opc, rd, avl, vtypei) \ tcg_out_opc_vec_config(s, opc, rd, avl, vtypei);@@ -1037,6 +1081,22 @@ static const struct {
[TCG_COND_GTU] = {OPC_BLTU, true }
};
+static const struct {
+ RISCVInsn opc;
+ bool swap;tcg/riscv/tcg-target.c.inc +13
-0+} tcg_cmpcond_to_rvv_vv[] = { + [TCG_COND_EQ] = { OPC_VMSEQ_VV, false }, + [TCG_COND_NE] = { OPC_VMSNE_VV, false }, + [TCG_COND_LT] = { OPC_VMSLT_VV, false }, + [TCG_COND_GE] = { OPC_VMSLE_VV, true }, + [TCG_COND_GT] = { OPC_VMSLT_VV, true }, + [TCG_COND_LE] = { OPC_VMSLE_VV, false }, + [TCG_COND_LTU] = { OPC_VMSLTU_VV, false }, + [TCG_COND_GEU] = { OPC_VMSLEU_VV, true }, + [TCG_COND_GTU] = { OPC_VMSLTU_VV, true }, + [TCG_COND_LEU] = { OPC_VMSLEU_VV, false } +}; +static void
tcg_out_brcond(TCGContext *s, TCGCond cond, TCGReg arg1, TCGReg arg2, TCGLabel *l)
{tcg/riscv/tcg-target.c.inc +0
-0@@ -1054,6 +1114,79 @@ static void tcg_out_brcond(TCGContext *s,
Quoted 8 lines
tcg/riscv/tcg-target.c.inc static void tcg_out_brcond(TCGContext *s, TCGCond cond, TCGReg arg1, · line 1119
Quoted 6 lines
tcg/riscv/tcg-target.c.inc +2
-0TCGCond cond, TCGReg arg1, tcg_out_opc_branch(s, op, arg1, arg2, 0); } +static const struct { + RISCVInsn op; + bool expand;invert is probably a better name.
OK.
Quoted 12 lines
Why are these tables so far apart?
Quoted 11 lines
tcg/riscv/tcg-target.c.inc +9
-0+static void tcg_out_cmp_vec_vx(TCGContext *s, TCGCond cond, TCGReg arg1, + tcg_target_long arg2) +{ + RISCVInsn op; + + tcg_debug_assert((unsigned)cond < ARRAY_SIZE(tcg_cmpcond_to_rvv_vx)); + op = tcg_cmpcond_to_rvv_vx[cond].op; + tcg_debug_assert(op != 0); +
Quoted 8 lines
tcg/riscv/tcg-target.c.inc static void tcg_out_brcond(TCGContext *s, TCGCond cond, TCGReg arg1, · line 1147
Quoted 6 lines
tcg/riscv/tcg-target.c.inc +6
-0+ tcg_out_opc_vx(s, op, TCG_REG_V0, arg1, arg2, true); + if (tcg_cmpcond_to_rvv_vx[cond].expand) { + tcg_out_opc_mvv(s, OPC_VMNAND_MM, TCG_REG_V0, TCG_REG_V0, + TCG_REG_V0, false); + } +}I think you'll be better served by handling the invert during expand, because you can always swap the sense of the predicate in the user.
OK. We have implemented this IR in expand way, where we call rvv_cmp_vv/rvv_cmp_vx/ rvv_cmp_vi according to the type and value of arg2.
Quoted 8 lines
Compare tcg/i386
expand_vec_cmp_noinv.Quoted 2 lines
tcg/riscv/tcg-target.c.inc +1
-0+ tcg_gen_mov_vec(v0, tcg_constant_vec_matching(v0, vece,0));
You don't need to copy to v0; just use the
tcg_constant_vecdirectly as
Quoted 7 lines
Quoted 3 lines
+ vec_gen_3(INDEX_op_rvv_merge_vec, type, vece,
+tcgv_vec_arg(v0),tcgv_vec_arg(v0), + tcgv_i64_arg(tcg_constant_i64(-1)));the first source operand.
You can swap 0 and -1 if the comparison instruction requires the predicate to be inverted.
OK.
Thanks,
Zhiwei
Quoted 2 lines
r~
PATCH 10/15 tcg/riscv: Implement vector not/neg ops
In reply to LIU ZhiweiFrom: TANG Tiancheng <[email protected]>
--- tcg/riscv/tcg-target-con-set.h | 1 + tcg/riscv/tcg-target.c.inc | 13 +++++++++++++ tcg/riscv/tcg-target.h | 4 ++-- 3 files changed, 16 insertions(+), 2 deletions(-)
tcg/riscv/tcg-target-con-set.h +1 -0
@@ -24,5 +24,6 @@ C_O2_I4(r, r, rZ, rZ, rM, rM) C_O0_I2(v, r) C_O0_I2(v, vK) C_O1_I1(v, r) +C_O1_I1(v, v) C_O1_I2(v, v, v)C_O1_I2 (v, v, vK)
tcg/riscv/tcg-target.c.inc +13 -0
@@ -306,7 +306,9 @@ typedef enum { OPC_VAND_VV = 0x24000057 | V_OPIVV, OPC_VOR_VV = 0x28000057 | V_OPIVV, OPC_VXOR_VV = 0x2c000057 | V_OPIVV, + OPC_VXOR_VI = 0x2c000057 | V_OPIVI, + OPC_VRSUB_VX = 0xc000057 | V_OPIVX, OPC_VMSEQ_VV = 0x60000057 | V_OPIVV, OPC_VMSEQ_VI = 0x60000057 | V_OPIVI, OPC_VMSEQ_VX = 0x60000057 | V_OPIVX,
@@ -2312,6 +2314,12 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, case INDEX_op_xor_vec: tcg_out_opc_vv(s, OPC_VXOR_VV, a0, a1, a2, true); break; + case INDEX_op_not_vec: + tcg_out_opc_vi(s, OPC_VXOR_VI, a0, a1, -1, true); + break; + case INDEX_op_neg_vec: + tcg_out_opc_vx(s, OPC_VRSUB_VX, a0, a1, TCG_REG_ZERO, true); + break; case INDEX_op_rvv_cmpcond_vec: { RISCVInsn op;
@@ -2384,6 +2392,8 @@ int tcg_can_emit_vec_op(TCGOpcode opc, TCGType type, unsigned vece) case INDEX_op_and_vec: case INDEX_op_or_vec: case INDEX_op_xor_vec: + case INDEX_op_not_vec: + case INDEX_op_neg_vec: return 1; case INDEX_op_cmp_vec: return -1;
@@ -2537,6 +2547,9 @@ static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op) case INDEX_op_dupm_vec: case INDEX_op_ld_vec: return C_O1_I1(v, r); + case INDEX_op_neg_vec: + case INDEX_op_not_vec: + return C_O1_I1(v, v); case INDEX_op_add_vec: case INDEX_op_sub_vec: case INDEX_op_and_vec:
tcg/riscv/tcg-target.h +2 -2
@@ -151,8 +151,8 @@ typedef enum {#define TCG_TARGET_HAS_nand_vec 0#define TCG_TARGET_HAS_nor_vec 0#define TCG_TARGET_HAS_eqv_vec 0 -#define TCG_TARGET_HAS_not_vec 0 -#define TCG_TARGET_HAS_neg_vec 0 +#define TCG_TARGET_HAS_not_vec 1 +#define TCG_TARGET_HAS_neg_vec 1#define TCG_TARGET_HAS_abs_vec 0#define TCG_TARGET_HAS_roti_vec 0#define TCG_TARGET_HAS_rots_vec 0
Signature · 1 line
2.43.0
Re: [PATCH v1 10/15] tcg/riscv: Implement vector not/neg ops
tcg/riscv/tcg-target.c.inc static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, · line 2322
Quoted 10 lines · LIU Zhiwei
tcg/riscv/tcg-target.c.inc +6
-0@@ -2312,6 +2314,12 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, case INDEX_op_xor_vec: tcg_out_opc_vv(s, OPC_VXOR_VV, a0, a1, a2, true); break; + case INDEX_op_not_vec: + tcg_out_opc_vi(s, OPC_VXOR_VI, a0, a1, -1, true); + break; + case INDEX_op_neg_vec: + tcg_out_opc_vx(s, OPC_VRSUB_VX, a0, a1, TCG_REG_ZERO, true); + break;
Any reason not to use vrsub.vi? Not wrong, just surprising.
Obviously, NOT does not require SEW change.
r~
Re: [PATCH v1 10/15] tcg/riscv: Implement vector not/neg ops
Quoted 14 lines · Richard Henderson
Quoted 11 lines · LIU Zhiwei
(quoted diff) +0
-0@@ -2312,6 +2314,12 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,case
INDEX_op_xor_vec:
tcg_out_opc_vv(s, OPC_VXOR_VV, a0, a1, a2, true); break;
+ caseINDEX_op_not_vec:
+tcg_out_opc_vi(s, OPC_VXOR_VI, a0, a1, -1, true); + break;
+ caseINDEX_op_neg_vec:
+tcg_out_opc_vx(s, OPC_VRSUB_VX, a0, a1, TCG_REG_ZERO, true); + break;Any reason not to use vrsub.vi? Not wrong, just surprising.
We will use vrsub.vi.
Quoted 2 lines
Obviously, NOT does not require SEW change.
OK.
Thanks,
Zhiwei
Quoted 2 lines
r~
PATCH 11/15 tcg/riscv: Implement vector sat/mul ops
In reply to LIU ZhiweiFrom: TANG Tiancheng <[email protected]>
--- tcg/riscv/tcg-target.c.inc | 32 ++++++++++++++++++++++++++++++++ tcg/riscv/tcg-target.h | 4 ++-- 2 files changed, 34 insertions(+), 2 deletions(-)
tcg/riscv/tcg-target.c.inc +32 -0
@@ -309,6 +309,13 @@ typedef enum { OPC_VXOR_VI = 0x2c000057 | V_OPIVI, OPC_VRSUB_VX = 0xc000057 | V_OPIVX, + + OPC_VMUL_VV = 0x94000057 | V_OPMVV, + OPC_VSADD_VV = 0x84000057 | V_OPIVV, + OPC_VSSUB_VV = 0x8c000057 | V_OPIVV, + OPC_VSADDU_VV = 0x80000057 | V_OPIVV, + OPC_VSSUBU_VV = 0x88000057 | V_OPIVV, + OPC_VMSEQ_VV = 0x60000057 | V_OPIVV, OPC_VMSEQ_VI = 0x60000057 | V_OPIVI, OPC_VMSEQ_VX = 0x60000057 | V_OPIVX,
@@ -2320,6 +2327,21 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, case INDEX_op_neg_vec: tcg_out_opc_vx(s, OPC_VRSUB_VX, a0, a1, TCG_REG_ZERO, true); break; + case INDEX_op_mul_vec: + tcg_out_opc_vv(s, OPC_VMUL_VV, a0, a1, a2, true); + break; + case INDEX_op_ssadd_vec: + tcg_out_opc_vv(s, OPC_VSADD_VV, a0, a1, a2, true); + break; + case INDEX_op_sssub_vec: + tcg_out_opc_vv(s, OPC_VSSUB_VV, a0, a1, a2, true); + break; + case INDEX_op_usadd_vec: + tcg_out_opc_vv(s, OPC_VSADDU_VV, a0, a1, a2, true); + break; + case INDEX_op_ussub_vec: + tcg_out_opc_vv(s, OPC_VSSUBU_VV, a0, a1, a2, true); + break; case INDEX_op_rvv_cmpcond_vec: { RISCVInsn op;
@@ -2394,6 +2416,11 @@ int tcg_can_emit_vec_op(TCGOpcode opc, TCGType type, unsigned vece) case INDEX_op_xor_vec: case INDEX_op_not_vec: case INDEX_op_neg_vec: + case INDEX_op_mul_vec: + case INDEX_op_ssadd_vec: + case INDEX_op_sssub_vec: + case INDEX_op_usadd_vec: + case INDEX_op_ussub_vec: return 1; case INDEX_op_cmp_vec: return -1;
@@ -2555,6 +2582,11 @@ static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op) case INDEX_op_and_vec: case INDEX_op_or_vec: case INDEX_op_xor_vec: + case INDEX_op_mul_vec: + case INDEX_op_ssadd_vec: + case INDEX_op_sssub_vec: + case INDEX_op_usadd_vec: + case INDEX_op_ussub_vec: return C_O1_I2(v, v, v); case INDEX_op_cmp_vec: case INDEX_op_rvv_merge_vec:
tcg/riscv/tcg-target.h +2 -2
@@ -160,8 +160,8 @@ typedef enum {#define TCG_TARGET_HAS_shi_vec 0#define TCG_TARGET_HAS_shs_vec 0#define TCG_TARGET_HAS_shv_vec 0 -#define TCG_TARGET_HAS_mul_vec 0 -#define TCG_TARGET_HAS_sat_vec 0 +#define TCG_TARGET_HAS_mul_vec 1 +#define TCG_TARGET_HAS_sat_vec 1#define TCG_TARGET_HAS_minmax_vec 0#define TCG_TARGET_HAS_bitsel_vec 0#define TCG_TARGET_HAS_cmpsel_vec 0
Signature · 1 line
2.43.0
PATCH 12/15 tcg/riscv: Implement vector min/max ops
In reply to LIU ZhiweiFrom: TANG Tiancheng <[email protected]>
--- tcg/riscv/tcg-target.c.inc | 25 +++++++++++++++++++++++++ tcg/riscv/tcg-target.h | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-)
tcg/riscv/tcg-target.c.inc +25 -0
@@ -316,6 +316,11 @@ typedef enum { OPC_VSADDU_VV = 0x80000057 | V_OPIVV, OPC_VSSUBU_VV = 0x88000057 | V_OPIVV, + OPC_VMAX_VV = 0x1c000057 | V_OPIVV, + OPC_VMAXU_VV = 0x18000057 | V_OPIVV, + OPC_VMIN_VV = 0x14000057 | V_OPIVV, + OPC_VMINU_VV = 0x10000057 | V_OPIVV, + OPC_VMSEQ_VV = 0x60000057 | V_OPIVV, OPC_VMSEQ_VI = 0x60000057 | V_OPIVI, OPC_VMSEQ_VX = 0x60000057 | V_OPIVX,
@@ -2342,6 +2347,18 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, case INDEX_op_ussub_vec: tcg_out_opc_vv(s, OPC_VSSUBU_VV, a0, a1, a2, true); break; + case INDEX_op_smax_vec: + tcg_out_opc_vv(s, OPC_VMAX_VV, a0, a1, a2, true); + break; + case INDEX_op_smin_vec: + tcg_out_opc_vv(s, OPC_VMIN_VV, a0, a1, a2, true); + break; + case INDEX_op_umax_vec: + tcg_out_opc_vv(s, OPC_VMAXU_VV, a0, a1, a2, true); + break; + case INDEX_op_umin_vec: + tcg_out_opc_vv(s, OPC_VMINU_VV, a0, a1, a2, true); + break; case INDEX_op_rvv_cmpcond_vec: { RISCVInsn op;
@@ -2421,6 +2438,10 @@ int tcg_can_emit_vec_op(TCGOpcode opc, TCGType type, unsigned vece) case INDEX_op_sssub_vec: case INDEX_op_usadd_vec: case INDEX_op_ussub_vec: + case INDEX_op_smax_vec: + case INDEX_op_smin_vec: + case INDEX_op_umax_vec: + case INDEX_op_umin_vec: return 1; case INDEX_op_cmp_vec: return -1;
@@ -2587,6 +2608,10 @@ static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op) case INDEX_op_sssub_vec: case INDEX_op_usadd_vec: case INDEX_op_ussub_vec: + case INDEX_op_smax_vec: + case INDEX_op_smin_vec: + case INDEX_op_umax_vec: + case INDEX_op_umin_vec: return C_O1_I2(v, v, v); case INDEX_op_cmp_vec: case INDEX_op_rvv_merge_vec:
tcg/riscv/tcg-target.h +1 -1
@@ -162,7 +162,7 @@ typedef enum {#define TCG_TARGET_HAS_shv_vec 0#define TCG_TARGET_HAS_mul_vec 1#define TCG_TARGET_HAS_sat_vec 1 -#define TCG_TARGET_HAS_minmax_vec 0 +#define TCG_TARGET_HAS_minmax_vec 1#define TCG_TARGET_HAS_bitsel_vec 0#define TCG_TARGET_HAS_cmpsel_vec 0
Signature · 1 line
2.43.0
PATCH 13/15 tcg/riscv: Implement vector shs/v ops
In reply to LIU ZhiweiFrom: TANG Tiancheng <[email protected]>
--- tcg/riscv/tcg-target-con-set.h | 1 + tcg/riscv/tcg-target.c.inc | 38 ++++++++++++++++++++++++++++++++++ tcg/riscv/tcg-target.h | 4 ++-- 3 files changed, 41 insertions(+), 2 deletions(-)
tcg/riscv/tcg-target-con-set.h +1 -0
@@ -27,3 +27,4 @@ C_O1_I1(v, r) C_O1_I1(v, v) C_O1_I2(v, v, v) C_O1_I2(v, v, vK) +C_O1_I2 (v, v, r)
tcg/riscv/tcg-target.c.inc +38 -0
@@ -344,6 +344,13 @@ typedef enum { OPC_VMSGT_VI = 0x7c000057 | V_OPIVI, OPC_VMSGT_VX = 0x7c000057 | V_OPIVX, + OPC_VSLL_VV = 0x94000057 | V_OPIVV, + OPC_VSLL_VX = 0x94000057 | V_OPIVX, + OPC_VSRL_VV = 0xa0000057 | V_OPIVV, + OPC_VSRL_VX = 0xa0000057 | V_OPIVX, + OPC_VSRA_VV = 0xa4000057 | V_OPIVV, + OPC_VSRA_VX = 0xa4000057 | V_OPIVX, + OPC_VMV_V_V = 0x5e000057 | V_OPIVV, OPC_VMV_V_I = 0x5e000057 | V_OPIVI, OPC_VMV_V_X = 0x5e000057 | V_OPIVX,
@@ -2359,6 +2366,24 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, case INDEX_op_umin_vec: tcg_out_opc_vv(s, OPC_VMINU_VV, a0, a1, a2, true); break; + case INDEX_op_shls_vec: + tcg_out_opc_vx(s, OPC_VSLL_VX, a0, a1, a2, true); + break; + case INDEX_op_shrs_vec: + tcg_out_opc_vx(s, OPC_VSRL_VX, a0, a1, a2, true); + break; + case INDEX_op_sars_vec: + tcg_out_opc_vx(s, OPC_VSRA_VX, a0, a1, a2, true); + break; + case INDEX_op_shlv_vec: + tcg_out_opc_vv(s, OPC_VSLL_VV, a0, a1, a2, true); + break; + case INDEX_op_shrv_vec: + tcg_out_opc_vv(s, OPC_VSRL_VV, a0, a1, a2, true); + break; + case INDEX_op_sarv_vec: + tcg_out_opc_vv(s, OPC_VSRA_VV, a0, a1, a2, true); + break; case INDEX_op_rvv_cmpcond_vec: { RISCVInsn op;
@@ -2442,6 +2467,12 @@ int tcg_can_emit_vec_op(TCGOpcode opc, TCGType type, unsigned vece) case INDEX_op_smin_vec: case INDEX_op_umax_vec: case INDEX_op_umin_vec: + case INDEX_op_shls_vec: + case INDEX_op_shrs_vec: + case INDEX_op_sars_vec: + case INDEX_op_shlv_vec: + case INDEX_op_shrv_vec: + case INDEX_op_sarv_vec: return 1; case INDEX_op_cmp_vec: return -1;
@@ -2612,7 +2643,14 @@ static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op) case INDEX_op_smin_vec: case INDEX_op_umax_vec: case INDEX_op_umin_vec: + case INDEX_op_shlv_vec: + case INDEX_op_shrv_vec: + case INDEX_op_sarv_vec: return C_O1_I2(v, v, v); + case INDEX_op_shls_vec: + case INDEX_op_shrs_vec: + case INDEX_op_sars_vec: + return C_O1_I2(v, v, r); case INDEX_op_cmp_vec: case INDEX_op_rvv_merge_vec: return C_O1_I2(v, v, vK);
tcg/riscv/tcg-target.h +2 -2
@@ -158,8 +158,8 @@ typedef enum {#define TCG_TARGET_HAS_rots_vec 0#define TCG_TARGET_HAS_rotv_vec 0#define TCG_TARGET_HAS_shi_vec 0 -#define TCG_TARGET_HAS_shs_vec 0 -#define TCG_TARGET_HAS_shv_vec 0 +#define TCG_TARGET_HAS_shs_vec 1 +#define TCG_TARGET_HAS_shv_vec 1#define TCG_TARGET_HAS_mul_vec 1#define TCG_TARGET_HAS_sat_vec 1#define TCG_TARGET_HAS_minmax_vec 1
Signature · 1 line
2.43.0
PATCH 14/15 tcg/riscv: Implement vector roti/v/x shi ops
In reply to LIU ZhiweiFrom: TANG Tiancheng <[email protected]>
--- tcg/riscv/tcg-target.c.inc | 107 ++++++++++++++++++++++++++++++++++++- tcg/riscv/tcg-target.h | 8 +-- tcg/riscv/tcg-target.opc.h | 3 ++ 3 files changed, 113 insertions(+), 5 deletions(-)
tcg/riscv/tcg-target.c.inc +106 -1
@@ -345,10 +345,13 @@ typedef enum { OPC_VMSGT_VX = 0x7c000057 | V_OPIVX, OPC_VSLL_VV = 0x94000057 | V_OPIVV, + OPC_VSLL_VI = 0x94000057 | V_OPIVI, OPC_VSLL_VX = 0x94000057 | V_OPIVX, OPC_VSRL_VV = 0xa0000057 | V_OPIVV, + OPC_VSRL_VI = 0xa0000057 | V_OPIVI, OPC_VSRL_VX = 0xa0000057 | V_OPIVX, OPC_VSRA_VV = 0xa4000057 | V_OPIVV, + OPC_VSRA_VI = 0xa4000057 | V_OPIVI, OPC_VSRA_VX = 0xa4000057 | V_OPIVX, OPC_VMV_V_V = 0x5e000057 | V_OPIVV,
@@ -2384,6 +2387,15 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, case INDEX_op_sarv_vec: tcg_out_opc_vv(s, OPC_VSRA_VV, a0, a1, a2, true); break; + case INDEX_op_rvv_shli_vec: + tcg_out_opc_vi(s, OPC_VSLL_VI, a0, a1, a2, true); + break; + case INDEX_op_rvv_shri_vec: + tcg_out_opc_vi(s, OPC_VSRL_VI, a0, a1, a2, true); + break; + case INDEX_op_rvv_sari_vec: + tcg_out_opc_vi(s, OPC_VSRA_VI, a0, a1, a2, true); + break; case INDEX_op_rvv_cmpcond_vec: { RISCVInsn op;
@@ -2422,7 +2434,8 @@ void tcg_expand_vec_op(TCGOpcode opc, TCGType type, unsigned vece, TCGArg a0, ...) { va_list va; - TCGv_vec v0, v1; + TCGv_vec v0, v1, v2, c1, t1; + TCGv_i32 t2; TCGArg a2, a3; va_start(va, a0);
@@ -2442,6 +2455,81 @@ void tcg_expand_vec_op(TCGOpcode opc, TCGType type, unsigned vece, tcgv_i64_arg(tcg_constant_i64(-1))); } break; + case INDEX_op_shli_vec: + if (a2 > 31) { + t2 = tcg_temp_new_i32(); + tcg_gen_movi_i32(t2, (int32_t)a2); + tcg_gen_shls_vec(vece, v0, v1, t2); + tcg_temp_free_i32(t2); + } else { + vec_gen_3(INDEX_op_rvv_shli_vec, type, vece, tcgv_vec_arg(v0), + tcgv_vec_arg(v1), a2); + } + break; + case INDEX_op_shri_vec: + if (a2 > 31) { + t2 = tcg_temp_new_i32(); + tcg_gen_movi_i32(t2, (int32_t)a2); + tcg_gen_shrs_vec(vece, v0, v1, t2); + tcg_temp_free_i32(t2); + } else { + vec_gen_3(INDEX_op_rvv_shri_vec, type, vece, tcgv_vec_arg(v0), + tcgv_vec_arg(v1), a2); + } + break; + case INDEX_op_sari_vec: + if (a2 > 31) { + t2 = tcg_temp_new_i32(); + tcg_gen_movi_i32(t2, (int32_t)a2); + tcg_gen_sars_vec(vece, v0, v1, t2); + tcg_temp_free_i32(t2); + } else { + vec_gen_3(INDEX_op_rvv_sari_vec, type, vece, tcgv_vec_arg(v0), + tcgv_vec_arg(v1), a2); + } + break; + case INDEX_op_rotli_vec: + t1 = tcg_temp_new_vec(type); + tcg_gen_shli_vec(vece, t1, v1, a2); + tcg_gen_shri_vec(vece, v0, v1, (8 << vece) - a2); + tcg_gen_or_vec(vece, v0, v0, t1); + tcg_temp_free_vec(t1); + break; + case INDEX_op_rotls_vec: + t1 = tcg_temp_new_vec(type); + t2 = tcg_temp_new_i32(); + tcg_gen_sub_i32(t2, tcg_constant_i32(8 << vece), + temp_tcgv_i32(arg_temp(a2))); + tcg_gen_shrs_vec(vece, v0, v1, t2); + tcg_gen_shls_vec(vece, t1, v1, temp_tcgv_i32(arg_temp(a2))); + tcg_gen_or_vec(vece, v0, v0, t1); + tcg_temp_free_vec(t1); + tcg_temp_free_i32(t2); + break; + case INDEX_op_rotlv_vec: + v2 = temp_tcgv_vec(arg_temp(a2)); + t1 = tcg_temp_new_vec(type); + c1 = tcg_constant_vec(type, vece, 8 << vece); + tcg_gen_sub_vec(vece, t1, c1, v2); + vec_gen_3(INDEX_op_shrv_vec, type, vece, tcgv_vec_arg(t1), + tcgv_vec_arg(v1), tcgv_vec_arg(t1)); + vec_gen_3(INDEX_op_shlv_vec, type, vece, tcgv_vec_arg(v0), + tcgv_vec_arg(v1), tcgv_vec_arg(v2)); + tcg_gen_or_vec(vece, v0, v0, t1); + tcg_temp_free_vec(t1); + break; + case INDEX_op_rotrv_vec: + v2 = temp_tcgv_vec(arg_temp(a2)); + t1 = tcg_temp_new_vec(type); + c1 = tcg_constant_vec(type, vece, 8 << vece); + tcg_gen_sub_vec(vece, t1, c1, v2); + vec_gen_3(INDEX_op_shlv_vec, type, vece, tcgv_vec_arg(t1), + tcgv_vec_arg(v1), tcgv_vec_arg(t1)); + vec_gen_3(INDEX_op_shrv_vec, type, vece, tcgv_vec_arg(v0), + tcgv_vec_arg(v1), tcgv_vec_arg(v2)); + tcg_gen_or_vec(vece, v0, v0, t1); + tcg_temp_free_vec(t1); + break; default: g_assert_not_reached(); }
@@ -2475,6 +2563,13 @@ int tcg_can_emit_vec_op(TCGOpcode opc, TCGType type, unsigned vece) case INDEX_op_sarv_vec: return 1; case INDEX_op_cmp_vec: + case INDEX_op_shri_vec: + case INDEX_op_shli_vec: + case INDEX_op_sari_vec: + case INDEX_op_rotls_vec: + case INDEX_op_rotlv_vec: + case INDEX_op_rotrv_vec: + case INDEX_op_rotli_vec: return -1; default: return 0;
@@ -2628,6 +2723,13 @@ static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op) return C_O1_I1(v, r); case INDEX_op_neg_vec: case INDEX_op_not_vec: + case INDEX_op_rotli_vec: + case INDEX_op_shli_vec: + case INDEX_op_shri_vec: + case INDEX_op_sari_vec: + case INDEX_op_rvv_shli_vec: + case INDEX_op_rvv_shri_vec: + case INDEX_op_rvv_sari_vec: return C_O1_I1(v, v); case INDEX_op_add_vec: case INDEX_op_sub_vec:
@@ -2646,10 +2748,13 @@ static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op) case INDEX_op_shlv_vec: case INDEX_op_shrv_vec: case INDEX_op_sarv_vec: + case INDEX_op_rotlv_vec: + case INDEX_op_rotrv_vec: return C_O1_I2(v, v, v); case INDEX_op_shls_vec: case INDEX_op_shrs_vec: case INDEX_op_sars_vec: + case INDEX_op_rotls_vec: return C_O1_I2(v, v, r); case INDEX_op_cmp_vec: case INDEX_op_rvv_merge_vec:
tcg/riscv/tcg-target.h +4 -4
@@ -154,10 +154,10 @@ typedef enum {#define TCG_TARGET_HAS_not_vec 1#define TCG_TARGET_HAS_neg_vec 1#define TCG_TARGET_HAS_abs_vec 0 -#define TCG_TARGET_HAS_roti_vec 0 -#define TCG_TARGET_HAS_rots_vec 0 -#define TCG_TARGET_HAS_rotv_vec 0 -#define TCG_TARGET_HAS_shi_vec 0 +#define TCG_TARGET_HAS_roti_vec -1 +#define TCG_TARGET_HAS_rots_vec -1 +#define TCG_TARGET_HAS_rotv_vec -1 +#define TCG_TARGET_HAS_shi_vec -1#define TCG_TARGET_HAS_shs_vec 1#define TCG_TARGET_HAS_shv_vec 1#define TCG_TARGET_HAS_mul_vec 1
tcg/riscv/tcg-target.opc.h +3 -0
@@ -13,3 +13,6 @@ DEF(rvv_cmpcond_vec, 0, 2, 1, IMPLVEC) DEF(rvv_merge_vec, 1, 2, 0, IMPLVEC) +DEF(rvv_shli_vec, 1, 1, 1, IMPLVEC) +DEF(rvv_shri_vec, 1, 1, 1, IMPLVEC) +DEF (rvv_sari_vec,1 ,1 ,1 ,IMPLVEC )
Signature · 1 line
2.43.0
Re: [PATCH v1 14/15] tcg/riscv: Implement vector roti/v/x shi ops
tcg/riscv/tcg-target.c.inc void tcg_expand_vec_op(TCGOpcode opc, TCGType type, unsigned vece, · line 2462
Quoted 5 lines · LIU Zhiwei
tcg/riscv/tcg-target.c.inc +5
-0+ case INDEX_op_shli_vec: + if (a2 > 31) { + t2 = tcg_temp_new_i32(); + tcg_gen_movi_i32(t2, (int32_t)a2); + tcg_gen_shls_vec(vece, v0, v1, t2);
Drop the movi, just pass tcg_constant_i32(a2) as the second source.
tcg/riscv/tcg-target.c.inc void tcg_expand_vec_op(TCGOpcode opc, TCGType type, unsigned vece, · line 2503
Quoted 6 lines
tcg/riscv/tcg-target.c.inc +6
-0+ case INDEX_op_rotls_vec: + t1 = tcg_temp_new_vec(type); + t2 = tcg_temp_new_i32(); + tcg_gen_sub_i32(t2, tcg_constant_i32(8 << vece), + temp_tcgv_i32(arg_temp(a2))); + tcg_gen_shrs_vec(vece, v0, v1, t2);
Only the low lg2(SEW) bits are used; you can just tcg_gen_neg_i32.
tcg/riscv/tcg-target.c.inc void tcg_expand_vec_op(TCGOpcode opc, TCGType type, unsigned vece, · line 2513
Quoted 5 lines
tcg/riscv/tcg-target.c.inc +5
-0+ case INDEX_op_rotlv_vec: + v2 = temp_tcgv_vec(arg_temp(a2)); + t1 = tcg_temp_new_vec(type); + c1 = tcg_constant_vec(type, vece, 8 << vece); + tcg_gen_sub_vec(vece, t1, c1, v2);
Likewise tcg_gen_neg_vec.
tcg/riscv/tcg-target.c.inc void tcg_expand_vec_op(TCGOpcode opc, TCGType type, unsigned vece, · line 2525
Quoted 5 lines
tcg/riscv/tcg-target.c.inc +5
-0+ case INDEX_op_rotrv_vec: + v2 = temp_tcgv_vec(arg_temp(a2)); + t1 = tcg_temp_new_vec(type); + c1 = tcg_constant_vec(type, vece, 8 << vece); + tcg_gen_sub_vec(vece, t1, c1, v2);
Likewise.
r~
Re: [PATCH v1 14/15] tcg/riscv: Implement vector roti/v/x shi ops
Quoted 8 lines · Richard Henderson
Quoted 5 lines · LIU Zhiwei
+ case
INDEX_op_shli_vec:
+ if (a2 > 31) {
+ t2 =tcg_temp_new_i32();
+tcg_gen_movi_i32(t2, (int32_t)a2);
+tcg_gen_shls_vec(vece, v0, v1, t2);Drop the movi, just pass
tcg_constant_i32(a2)as the second source.
OK.
Quoted 9 lines
Quoted 7 lines
+ case
INDEX_op_rotls_vec:
+ t1 =tcg_temp_new_vec(type);
+ t2 =tcg_temp_new_i32();
+ tcg_gen_sub_i32(t2,tcg_constant_i32(8 << vece), + temp_tcgv_i32(arg_temp(a2))); +tcg_gen_shrs_vec(vece, v0, v1, t2);Only the low lg2(SEW) bits are used; you can just
tcg_gen_neg_i32.
Good idea.
Quoted 9 lines
Quoted 6 lines
+ case
INDEX_op_rotlv_vec:
+ v2 = temp_tcgv_vec(arg_temp(a2));
+ t1 =tcg_temp_new_vec(type);
+ c1 =tcg_constant_vec(type, vece, 8 << vece); +tcg_gen_sub_vec(vece, t1, c1, v2);Likewise
tcg_gen_neg_vec.
Quoted 7 lines
Quoted 5 lines
+ case
INDEX_op_rotrv_vec:
+ v2 = temp_tcgv_vec(arg_temp(a2));
+ t1 =tcg_temp_new_vec(type);
+ c1 =tcg_constant_vec(type, vece, 8 << vece); +tcg_gen_sub_vec(vece, t1, c1, v2);Likewise.
Thanks,
Zhiwei
Quoted 3 lines
r~
PATCH 15/15 tcg/riscv: Enable vector TCG host-native
In reply to LIU ZhiweiFrom: TANG Tiancheng <[email protected]>
--- tcg/riscv/tcg-target.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
tcg/riscv/tcg-target.h +7 -3
@@ -143,9 +143,13 @@ typedef enum {#define TCG_TARGET_HAS_tst 0/* vector instructions */ -#define TCG_TARGET_HAS_v64 0 -#define TCG_TARGET_HAS_v128 0 -#define TCG_TARGET_HAS_v256 0 +extern int riscv_vlen; +#define have_rvv ((cpuinfo & CPUINFO_ZVE64X) && \ + (riscv_vlen >= 64)) + +#define TCG_TARGET_HAS_v64 have_rvv +#define TCG_TARGET_HAS_v128 have_rvv +#define TCG_TARGET_HAS_v256 have_rvv#define TCG_TARGET_HAS_andc_vec 0#define TCG_TARGET_HAS_orc_vec 0#define TCG_TARGET_HAS_nand_vec 0
Signature · 1 line
2.43.0
Re: [PATCH v1 15/15] tcg/riscv: Enable vector TCG host-native
Quoted 16 lines · LIU Zhiwei
From: TANG Tiancheng <[email protected]>
Signed-off-by TANG Tiancheng <[email protected]>Reviewed-by Liu Zhiwei <[email protected]>--- tcg/riscv/tcg-target.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)tcg/riscv/tcg-target.h +0
-0@@ -143,9 +143,13 @@ typedef enum {#define TCG_TARGET_HAS_tst 0/* vector instructions */
tcg/riscv/tcg-target.h typedef enum { · line 152
Quoted 10 lines
tcg/riscv/tcg-target.h +7
-3-#define TCG_TARGET_HAS_v64 0 -#define TCG_TARGET_HAS_v128 0 -#define TCG_TARGET_HAS_v256 0 +extern int riscv_vlen; +#define have_rvv ((cpuinfo & CPUINFO_ZVE64X) && \ + (riscv_vlen >= 64)) + +#define TCG_TARGET_HAS_v64 have_rvv +#define TCG_TARGET_HAS_v128 have_rvv +#define TCG_TARGET_HAS_v256 have_rvv
Can ELEN ever be less than 64 for riscv64?
I thought ELEN had to be at least XLEN. Anyway, if ELEN >= 64, then VLEN must also be >= 64.
In any case, I think we should not set CPUINFO_ZVE64X if the vlen is too small. We can initialize both values in util/cpuinfo-riscv.c, rather than initializing vlen in tcg.
tcg/riscv/tcg-target.h typedef enum {
Quoted 5 lines
tcg/riscv/tcg-target.h +5
-0+static void riscv_get_vlenb (void ){ +/* Get vlenb for Vector: csrrs %0, vlenb, zero. */ + asmvolatile ("csrrs %0, 0xc22, x0" :"=r" (riscv_vlen)); + riscv_vlen *=8 ; +}
While this is an interesting and required datum, if ELEN < XLEN is possible, then perhaps
asm("vsetvli %0, r0, e64" : "=r"(vl));is a better probe, verifying that vl != 0, i.e. e64 is supported, and recording vlen as vl * 64, i.e. VLMAX.
r~
Re: [PATCH v1 15/15] tcg/riscv: Enable vector TCG host-native
Quoted 20 lines · Richard Henderson
Quoted 19 lines · LIU Zhiwei
From: TANG Tiancheng <[email protected]>
Signed-off-by TANG Tiancheng <[email protected]>Reviewed-by Liu Zhiwei <[email protected]>---
tcg/riscv/tcg-target.h| 10 +++++++---1 file changed, 7 insertions(+), 3 deletions(-)tcg/riscv/tcg-target.h +1
-3@@ -143,9 +143,13 @@ typedef enum { #define TCG_TARGET_HAS_tst 0 /* vector instructions */ -#define TCG_TARGET_HAS_v64 0 -#define TCG_TARGET_HAS_v128 0 -#define TCG_TARGET_HAS_v256 0 +extern int riscv_vlen;
Quoted 8 lines
tcg/riscv/tcg-target.h typedef enum { · line 152
Quoted 6 lines
tcg/riscv/tcg-target.h +6
-0+#define have_rvv ((cpuinfo & CPUINFO_ZVE64X) && \ + (riscv_vlen >= 64)) + +#define TCG_TARGET_HAS_v64 have_rvv +#define TCG_TARGET_HAS_v128 have_rvv +#define TCG_TARGET_HAS_v256 have_rvvCan ELEN ever be less than 64 for riscv64?
I think so. At least the specification allow this case. According to the specification,
"Any of these extensions can be added to base ISAs with XLEN=32 or XLEN=64."
includes zve32x, where ELEN is 32 and XLEN is 64.
Quoted 2 lines
I thought ELEN had to be at least XLEN. Anyway, if ELEN >= 64, then VLEN must also be >= 64.
YES.
Quoted 2 lines
In any case, I think we should not set
CPUINFO_ZVE64Xif the vlen is too small.
Agree.
Quoted 10 lines
We can initialize both values in
util/cpuinfo-riscv.c, rather than initializing vlen in tcg.tcg/riscv/tcg-target.h typedef enum {
Quoted 5 lines
tcg/riscv/tcg-target.h +5
-0+static void riscv_get_vlenb (void ){ + /* Get vlenb for Vector: csrrs %0, vlenb, zero. */ + asm volatile("csrrs %0, 0xc22, x0" : "=r"(riscv_vlen)); + riscv_vlen *= 8; +}While this is an interesting and required datum, if ELEN < XLEN is possible, then perhaps
Quoted 3 lines
asm("vsetvli %0, r0, e64" : "=r"(vl));is a better probe, verifying that vl != 0, i.e. e64 is supported, and recording vlen as vl * 64, i.e. VLMAX.
We will use this one. But probe the vlen in util/cpuinfo-riscv.c has no meaning as we sometimes use the compiler settings or hw_probe API. In these cases, the vlen detected in util/cpuinfo-riscv.c is zero.
Thanks,
Zhiwei
Quoted 3 lines
r~
Re: [PATCH v1 15/15] tcg/riscv: Enable vector TCG host-native
Quoted 1 line · LIU Zhiwei
We will use this one. But probe the vlen in
util/cpuinfo-riscv.chas no meaning as we sometimes use the compiler settings orhw_probeAPI. In these cases, the vlen detected inutil/cpuinfo-riscv.cis zero.
Pardon?
While you might check __riscv_zve64x at compile-time, you would still fall through to
---
}
+ if (info & CPUINFO_ZVE64X) {
+ unsigned long vl;
+ asm("vsetvli %0, r0, e64" : "=r"(vl));
+ if (vl) {+ riscv_vlen = vl * 8;
+ } else {
+ info &= ~CPUINFO_ZVE64X;
+ }
+ }
+
info |= CPUINFO_ALWAYS;
cpuinfo = info;---
Do not attempt to merge the vsetvli from the SIGILL probe; I expect that path to become unused and eventually vanish.
r~