target/riscv: rvv: Use wider accesses for unit stride load/store
v7 cover target/riscv: rvv: Use wider accesses for unit stride load/store
Thanks Richard Henderson for the review on v6.
Changes since v6:
- Use atomic.h and atomic128.h functions
- Fix incorrect indentation in ok_for_atomic
Previous versions:
- v1: https://lore.kernel.org/all/[email protected]/
- v2: https://lore.kernel.org/all/[email protected]/
- v3: https://lore.kernel.org/all/[email protected]/
- v4: https://lore.kernel.org/all/[email protected]/
- v5: https://lore.kernel.org/all/[email protected]/
- v6: https://lore.kernel.org/all/[email protected]/
Craig Blackmore (1):
target/riscv: rvv: Use wider accesses for unit stride load/store
target/riscv/vector_helper.c | 107 +++++++++++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 6 deletions(-)
Signature · 1 line
2.43.0
PATCH v7 1/1 target/riscv: rvv: Use wider accesses for unit stride load/store
Use atomic load/store functions to access multiple elements from host.
--- target/riscv/vector_helper.c | 107 +++++++++++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 6 deletions(-)
target/riscv/vector_helper.c +101 -6
@@ -206,10 +206,102 @@ vext_continus_ldst_tlb(CPURISCVState *env, vext_ldst_elem_fn_tlb *ldst_tlb, } } +#if !HOST_BIG_ENDIAN +/* Atomic operations for load/store */ + +/* +* Return true if there are enough elements for this size access and the +* pointer is aligned to the required atomicity. +*/ + +static inline QEMU_ALWAYS_INLINE bool +ok_for_atomic (uint32_t size,void *host,uint32_t reg_start,uint32_t evl, +uint32_t log2_esz) +{ +return (reg_start + (size >> log2_esz)) <= evl + && ((uintptr_t ) host % size) ==0 ; +} + +#define GEN_VEXT_LDST_ATOMIC_HOST (SIZE ,TYPE ) \ +static inline QEMU_ALWAYS_INLINE void \ +vext_ldst_atom_##SIZE##_host(void *vd, uint32_t byte_offset, TYPE *host, \ + bool is_load) \ +{ \ + TYPE *vd_ptr = (TYPE *) (vd + byte_offset); \ + if (is_load) { \ + *vd_ptr = qatomic_read__nocheck(host); \ + } else { \ + qatomic_set__nocheck(host, *vd_ptr); \ + } \ +} \ + +GEN_VEXT_LDST_ATOMIC_HOST(2, uint16_t) +GEN_VEXT_LDST_ATOMIC_HOST(4, uint32_t) +#ifdef CONFIG_ATOMIC64 +GEN_VEXT_LDST_ATOMIC_HOST(8, uint64_t) +#endif + +#ifdef CONFIG_INT128_TYPE +static inline QEMU_ALWAYS_INLINE void +vext_ldst_atom_16_host (void *vd,uint32_t byte_offset,Int128 *host, +bool is_load) +{ +Int128 *vd_ptr = (Int128 *) (vd + byte_offset); +if (is_load) { + *vd_ptr =atomic16_read_ro (host); + }else { +atomic16_set (host, *vd_ptr); + } +} +#endif +#endif + +/* Perform the largest atomic load/store possible for the evl and alignment. */ + +static inline QEMU_ALWAYS_INLINE uint32_t +vext_ldst_atomic_chunk_host (CPURISCVState *env, +vext_ldst_elem_fn_host *ldst_host, +void *vd,uint32_t evl,uint32_t reg_start, +void *host,uint32_t esz,bool is_load, +uint32_t log2_esz) +{ +#if !HOST_BIG_ENDIAN +uint32_t byte_offset = reg_start * esz; + +#ifdef CONFIG_INT128_TYPE +if (((is_load &&HAVE_ATOMIC128_RO ) || (!is_load &&HAVE_ATOMIC128_RW )) + &&ok_for_atomic (16 , host, reg_start, evl, log2_esz)) { +vext_ldst_atom_16_host (vd, byte_offset, host, is_load); +return 16 ; + } +#endif + +#ifdef CONFIG_ATOMIC64 +if (ok_for_atomic (8 , host, reg_start, evl, log2_esz)) { +vext_ldst_atom_8_host (vd, byte_offset, host, is_load); +return 8 ; + } +#endif + +if (ok_for_atomic (4 , host, reg_start, evl, log2_esz)) { +vext_ldst_atom_4_host (vd, byte_offset, host, is_load); +return 4 ; + } + +if (ok_for_atomic (2 , host, reg_start, evl, log2_esz)) { +vext_ldst_atom_2_host (vd, byte_offset, host, is_load); +return 2 ; + } +#endif + +ldst_host (vd, reg_start, host); +return 1 ; +} + static inline QEMU_ALWAYS_INLINE voidvext_continus_ldst_host (CPURISCVState *env,vext_ldst_elem_fn_host *ldst_host,void *vd,uint32_t evl,uint32_t reg_start,void *host, -uint32_t esz,bool is_load) +uint32_t esz,bool is_load,uint32_t log2_esz) {#if HOST_BIG_ENDIAN for (; reg_start < evl; reg_start++, host += esz) {
@@ -225,10 +317,13 @@ vext_continus_ldst_host(CPURISCVState *env, vext_ldst_elem_fn_host *ldst_host, } else {memcpy (host, vd + byte_offset, size); } - }else { -for (; reg_start < evl; reg_start++, host += esz) { -ldst_host (vd, reg_start, host); - } +return ; + } +uint32_t chunk =0 ; +for (; reg_start < evl; reg_start += (chunk >> log2_esz), + host += chunk) { + chunk =vext_ldst_atomic_chunk_host (env, ldst_host, vd, evl, reg_start, + host, esz, is_load, log2_esz); }#endif }
@@ -343,7 +438,7 @@ vext_page_ldst_us(CPURISCVState *env, void *vd, target_ulong addr,if (flags ==0 ) {if (nf ==1 ) {vext_continus_ldst_host (env, ldst_host, vd, evl, env->vstart, host, - esz, is_load); + esz, is_load, log2_esz); }else {for (i = env->vstart; i < evl; ++i) { k =0 ;
Signature · 1 line
2.43.0
Quoted 57 lines · Craig Blackmore
Use atomic load/store functions to access multiple elements from host.
Co-authored-by Paolo Savini <[email protected]>Signed-off-by Paolo Savini <[email protected]>Signed-off-by Craig Blackmore <[email protected]>--- target/riscv/vector_helper.c | 107 +++++++++++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 6 deletions(-)target/riscv/vector_helper.c +39
-0@@ -206,10 +206,102 @@ vext_continus_ldst_tlb(CPURISCVState *env, vext_ldst_elem_fn_tlb *ldst_tlb, } } +#if !HOST_BIG_ENDIAN +/* Atomic operations for load/store */ + +/* +* Return true if there are enough elements for this size access and the +* pointer is aligned to the required atomicity. +*/ + +static inline QEMU_ALWAYS_INLINE bool +ok_for_atomic (uint32_t size,void *host,uint32_t reg_start,uint32_t evl, +uint32_t log2_esz) +{ +return (reg_start + (size >> log2_esz)) <= evl + && ((uintptr_t ) host % size) ==0 ; +} + +#define GEN_VEXT_LDST_ATOMIC_HOST (SIZE ,TYPE ) \ +static inline QEMU_ALWAYS_INLINE void \ +vext_ldst_atom_##SIZE##_host(void *vd, uint32_t byte_offset, TYPE *host, \ + bool is_load) \ +{ \ + TYPE *vd_ptr = (TYPE *) (vd + byte_offset); \ + if (is_load) { \ + *vd_ptr = qatomic_read__nocheck(host); \ + } else { \ + qatomic_set__nocheck(host, *vd_ptr); \ + } \ +} \ + +GEN_VEXT_LDST_ATOMIC_HOST(2, uint16_t) +GEN_VEXT_LDST_ATOMIC_HOST(4, uint32_t) +#ifdef CONFIG_ATOMIC64 +GEN_VEXT_LDST_ATOMIC_HOST(8, uint64_t) +#endif + +#ifdef CONFIG_INT128_TYPE +static inline QEMU_ALWAYS_INLINE void +vext_ldst_atom_16_host (void *vd,uint32_t byte_offset,Int128 *host, +bool is_load)
target/riscv/vector_helper.c vext_continus_ldst_tlb(CPURISCVState *env, vext_ldst_elem_fn_tlb *ldst_tlb, · line 257
Quoted 10 lines
target/riscv/vector_helper.c +10
-0+{ +Int128 *vd_ptr = (Int128 *) (vd + byte_offset); +if (is_load) { + *vd_ptr =atomic16_read_ro (host); + }else { +atomic16_set (host, *vd_ptr); + } +} +#endif +#endif
Using CONFIG_INT128_TYPE is peeking under the hood a bit too far.
As it happens, the ifdef should not be required, because the atomic16_{read,set} symbols are always present.
target/riscv/vector_helper.c vext_continus_ldst_tlb(CPURISCVState *env, vext_ldst_elem_fn_tlb *ldst_tlb, · line 277
Quoted 7 lines
target/riscv/vector_helper.c +7
-0+#ifdef CONFIG_INT128_TYPE +if (((is_load &&HAVE_ATOMIC128_RO ) || (!is_load &&HAVE_ATOMIC128_RW )) + &&ok_for_atomic (16 , host, reg_start, evl, log2_esz)) { +vext_ldst_atom_16_host (vd, byte_offset, host, is_load); +return 16 ; + } +#endif
The tricky part is that if !HAVE_ATOMIC128_RO, then atomic16_set may be implemented with a 16-byte compare and store loop. This is expensive, and you certainly don't want to use that. You'd prefer two 8-byte stores.
So I think the condition should be
if (HAVE_ATOMIC128_RO && (is_load || HAVE_ATOMIC128_RW))
with the above paragraph added as a comment.
That said, RVV does not have 128-bit elements, so at no point do you actually require 128-bit atomicity.
Quoted 37 lines
target/riscv/vector_helper.c +24
-3+ +#ifdef CONFIG_ATOMIC64 +if (ok_for_atomic (8 , host, reg_start, evl, log2_esz)) { +vext_ldst_atom_8_host (vd, byte_offset, host, is_load); +return 8 ; + } +#endif + +if (ok_for_atomic (4 , host, reg_start, evl, log2_esz)) { +vext_ldst_atom_4_host (vd, byte_offset, host, is_load); +return 4 ; + } + +if (ok_for_atomic (2 , host, reg_start, evl, log2_esz)) { +vext_ldst_atom_2_host (vd, byte_offset, host, is_load); +return 2 ; + } +#endif + +ldst_host (vd, reg_start, host); +return 1 ; +} + static inline QEMU_ALWAYS_INLINE voidvext_continus_ldst_host (CPURISCVState *env,vext_ldst_elem_fn_host *ldst_host,void *vd,uint32_t evl,uint32_t reg_start,void *host, -uint32_t esz,bool is_load) +uint32_t esz,bool is_load,uint32_t log2_esz) {#if HOST_BIG_ENDIAN for (; reg_start < evl; reg_start++, host += esz) {@@ -225,10 +317,13 @@ vext_continus_ldst_host(CPURISCVState *env, vext_ldst_elem_fn_host *ldst_host, }else {memcpy (host, vd + byte_offset, size); } - }else { -for (; reg_start < evl; reg_start++, host += esz) {
target/riscv/vector_helper.c vext_continus_ldst_host(CPURISCVState *env, vext_ldst_elem_fn_host *ldst_host, · line 327
Quoted 10 lines
target/riscv/vector_helper.c +7
-2-ldst_host (vd, reg_start, host); - } +return ; + } +uint32_t chunk =0 ; +for (; reg_start < evl; reg_start += (chunk >> log2_esz), + host += chunk) { + chunk =vext_ldst_atomic_chunk_host (env, ldst_host, vd, evl, reg_start, + host, esz, is_load, log2_esz); }
My guess is the arrangement of the loop is not great, in that ok_for_atomic is buried at the bottom of the call stack within this loop.
It would be better to test for alignment once, here at the outside. If the alignment is below esz, then the individual elements are misaligned, at which point the architecture does not require *any* atomicity. At which point, I believe you could use the memcpy path just above.
Perhaps something like
uint32_t byte_offset = reg_start * esz;
uint32_t size = (evl - reg_start) * esz;
uint32_t test = (uintptr_t)host;
if (esz == 1 || unlikely(test % esz != 0)) {
if (is_load) {
memcpy(vd + byte_offset, host, size);
} else {
memcpy(host, vd + byte_offset, size);
}
return;
}
/* Test that both alignment and size are multiples. */
test |= size;
if (HAVE_ATOMIC128_RO && (is_load || HAVE_ATOMIC128_RW) && (test % 16 == 0) {
do {
vext_ldst_atom_16_host(vd, reg_offset, host, is_load);
reg_offset += 16 >> log2_esz;
host += 16;
} while (reg_offset < evl);
return;
}
if (esz < 8 && test % 8 == 0) {
...
}
...
for (; reg_start < evl; reg_start++, host += esz) {
ldst_host(vd, reg_start, host);
}r~