target/loongarch: Fixes for endianness problems in vector instruction emulation
cover target/loongarch: Fixes for endianness problems in vector instruction emulation
The current implementation of vector instruction emulation in loongarch target works illy on big endianness hosts. It can be reproduced using the test cases from gcc [1]. One can cross-compile the target loongarch64-linux-user for s390x and use qemu-s390x to run the compiled qemu to test loongarch64 emulation on big endian hosts.
Possible reproduce steps are:
1. Install s390x cross toolchains and qemu-s390x
2. Compile qemu using ./configure --target-list=loongarch64-linux-user --cross-prefix=s390x-linux-gnu-
3. Install loongarch64 cross toolchains to compile the test cases. To
get an accurate result, the test cases should be compiled using -O0.
To make the error output of the test cases readable and meaningful,
printf() should be replaced with a version without any vectorization optimizations, since the endianness problems will prevent printf()
with lsx optimizations from working correctly
4. Use qemu-s390x ./qemu-loongarch64 /path/to/test_case.exe to test the
cases on big endianness hosts.
With this patch series, all the tests in [1] can pass.
[1]: under gcc/testsuite/gcc.target/loongarch/vector in the gcc source
---
Miao Wang (6):
target/loongarch: make VReg aligned to 16 bytes target/loongarch: Fix offset calculation in vector registers target/loongarch: gen_vreplve: Fix offset calculation target/loongarch: gen_xvreplve0: Fix elem0 offset calculation target/loongarch: do_vldrepl_vl: Fix endianness when reading from guest memory target/loongarch: vstelm: Fix reading from vector registers
target/loongarch/cpu.h | 2 +- target/loongarch/tcg/insn_trans/trans_vec.c.inc | 39 +++++++++++++------------ target/loongarch/tcg/translate.c | 6 ++-- 3 files changed, 25 insertions(+), 22 deletions(-)
---
base-commit: 30e8a06b64aa58a3990ba39cb5d09531e7d265e0
change-id: 20260701-loong64-be-adf8a15675f1
Best regards,
Signature · 1 line
Miao Wang <[email protected]>
PATCH 1/6 target/loongarch: make VReg aligned to 16 bytes
When tcg_gen_gvec_* functions are used to operate on 128-bit vector registers, the registers are required to be aligned to 16 bytes. On some platforms, such as s390x, __int128 is not aligned to 16 bytes, which will break the requirement and make tcg_gen_gvec_* functions operate on only 64 bits. Explicitly use aligned 128-bit integer to fix this.
--- target/loongarch/cpu.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
target/loongarch/cpu.h +1 -1
@@ -275,7 +275,7 @@ typedef union VReg {uint16_t UH [LASX_LEN /16 ];uint32_t UW [LASX_LEN /32 ];uint64_t UD [LASX_LEN /64 ]; -Int128 Q [LASX_LEN /128 ]; +Int128Aligned Q [LASX_LEN /128 ]; } VReg;typedef union fpr_t fpr_t ;
Signature · 1 line
2.49.0
Re: [PATCH 1/6] target/loongarch: make VReg aligned to 16 bytes
Hi,
Quoted 10 lines · On 1/7/26 00:15, Miao Wang via B4 Relay wrote:
From: Miao Wang <[email protected]>
When tcg_gen_gvec_* functions are used to operate on 128-bit vector registers, the registers are required to be aligned to 16 bytes. On some platforms, such as s390x,
__int128is not aligned to 16 bytes, which will break the requirement and make tcg_gen_gvec_* functions operate on only 64 bits. Explicitly use aligned 128-bit integer to fix this.Fixes 008a3b1662b6 ("target/loongarch: Add LASX data support")
IIUC that would be 16f5396cec2 ("target/loongarch: Add LSX data type VReg") with the first trigger in 57b4f1ac188 ("target/loongarch:
Implement vadd/vsub").
Quoted 10 lines
Signed-off-by Miao Wang <[email protected]>--- target/loongarch/cpu.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/loongarch/cpu.h +0
-0
target/loongarch/cpu.h typedef union VReg { · line 279
Quoted 6 lines
target/loongarch/cpu.h +1
-1@@ -275,7 +275,7 @@ typedef union VReg {uint16_t UH [LASX_LEN /16 ];uint32_t UW [LASX_LEN /32 ];uint64_t UD [LASX_LEN /64 ]; -Int128 Q [LASX_LEN /128 ]; +Int128Aligned Q [LASX_LEN /128 ]; } VReg;
QEMU_ALIGNED(128) on the whole struct instead?
Quoted 3 lines
target/loongarch/cpu.h +0
-0typedef union fpr_t fpr_t ;
Re: [PATCH 1/6] target/loongarch: make VReg aligned to 16 bytes
Hi,
Quoted 14 lines
2026年7月1日 06:41,Philippe Mathieu-Daudé <[email protected]> 写道:
Hi,
Quoted 8 lines · On 1/7/26 00:15, Miao Wang via B4 Relay wrote:
From: Miao Wang <[email protected]>
When tcg_gen_gvec_* functions are used to operate on 128-bit vector registers, the registers are required to be aligned to 16 bytes. On some platforms, such as s390x,__int128is not aligned to 16 bytes, which will break the requirement and make tcg_gen_gvec_* functions operate on only 64 bits. Explicitly use aligned 128-bit integer to fix this.Fixes 008a3b1662b6 ("target/loongarch: Add LASX data support")
Quoted 3 lines
IIUC that would be
16f5396cec2("target/loongarch: Add LSX data type VReg") with the first trigger in57b4f1ac188("target/loongarch:
Implement vadd/vsub").
Yes, I agree with you on 16f5396cec2, but I think the first trigger should be 2d5f950c054b ("target/loongarch: Implement vaddw/vsubw"), since in 57b4f1ac188, the 128-bit add/sub is implemented by first reading the 128 bit value in two 64 bit halves and storing the result as two 64 bit halves, which does not require the registers are 128-bit aligned. 2d5f950c054b is where MO_128 is first used with tcg_gen_gvec.
Quoted 9 lines
Quoted 9 lines
Signed-off-by Miao Wang <[email protected]>--- target/loongarch/cpu.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/loongarch/cpu.h +0
-0@@ -275,7 +275,7 @@ typedef union VReg {
Quoted 8 lines
target/loongarch/cpu.h typedef union VReg { · line 279
Quoted 6 lines
target/loongarch/cpu.h +1
-1uint16_t UH [LASX_LEN /16 ];uint32_t UW [LASX_LEN /32 ];uint64_t UD [LASX_LEN /64 ]; -Int128 Q [LASX_LEN /128 ]; +Int128Aligned Q [LASX_LEN /128 ]; } VReg;
QEMU_ALIGNED(128)on the whole struct instead?
Ack
Quoted 1 line
target/loongarch/cpu.h typedef union VReg { · line 281
Quoted 1 line
target/loongarch/cpu.h +0
-0typedef union fpr_t fpr_t ;
Cheers,
Miao Wang
PATCH 2/6 target/loongarch: Fix offset calculation in vector registers
In reply to Miao WangWhen vector registers are extended to 256 bits long, the expected layout of the registers is storing two 128-bit integers in host endianness, as indicated in vec.h. So when the registers are visited in the unit of 64-bit, the index should also be reversed.
--- target/loongarch/tcg/translate.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
target/loongarch/tcg/translate.c +3 -3
@@ -41,11 +41,11 @@ static inline int vec_full_offset(int regno)static inline int vec_reg_offset (int regno,int index,MemOp mop) { -const uint8_t size =1 << mop; +const unsigned int size =memop_size (mop);int offs = index * size; -if (HOST_BIG_ENDIAN && size <8 ) { - offs ^= (8 - size); +if (HOST_BIG_ENDIAN && size <16 ) { + offs ^= (16 - size); }return offs +vec_full_offset (regno);
Signature · 1 line
2.49.0
PATCH 3/6 target/loongarch: gen_vreplve: Fix offset calculation
In reply to Miao WangWhen generating tcg instructions for vreplve, the calculation of the offset in the vector registers from given channel in the general register should be done using tcg op codes. The algorithm should be similar as vec_reg_offset().
--- target/loongarch/tcg/insn_trans/trans_vec.c.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
target/loongarch/tcg/insn_trans/trans_vec.c.inc +1 -1
@@ -5036,7 +5036,7 @@ static bool gen_vreplve_vl(DisasContext *ctx, arg_vvr *a, tcg_gen_andi_i64(t0, gpr_src(ctx, a->rk, EXT_NONE), (LSX_LEN / bit) - 1); tcg_gen_shli_i64(t0, t0, vece); if (HOST_BIG_ENDIAN) { - tcg_gen_xori_i64(t0, t0, vece << ((LSX_LEN / bit) - 1)); + tcg_gen_xori_i64(t0, t0, 16 - (bit / 8)); } tcg_gen_trunc_i64_ptr(t1, t0);
Signature · 1 line
2.49.0
PATCH 4/6 target/loongarch: gen_xvreplve0: Fix elem0 offset calculation
In reply to Miao WangWhen xvreplve0 is implemented, it should not be taken for granted that the first element of a vector register is at the starting point of the vector register on big endian hosts, vec_reg_offset() is used to fix this.
--- target/loongarch/tcg/insn_trans/trans_vec.c.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
target/loongarch/tcg/insn_trans/trans_vec.c.inc +1 -1
@@ -5099,7 +5099,7 @@ static bool gen_xvreplve0(DisasContext *ctx, arg_vv *a, MemOp mop) } tcg_gen_gvec_dup_mem(mop, vec_full_offset(a->vd), - vec_full_offset(a->vj), 32, 32); + vec_reg_offset(a->vj, 0, mop), 32, 32); return true; }
Signature · 1 line
2.49.0
Re: [PATCH 4/6] target/loongarch: gen_xvreplve0: Fix elem0 offset calculation
Quoted 7 lines · On 1/7/26 00:15, Miao Wang via B4 Relay wrote:
From: Miao Wang <[email protected]>
When xvreplve0 is implemented, it should not be taken for granted that the first element of a vector register is at the starting point of the vector register on big endian hosts,
vec_reg_offset()is used to fix this.
Quoted 5 lines
Fixes df97f3380760 ("target/loongarch: Implement xvreplve xvinsve0 xvpickve")Signed-off-by Miao Wang <[email protected]>--- target/loongarch/tcg/insn_trans/trans_vec.c.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
PATCH 5/6 target/loongarch: do_vldrepl_vl: Fix endianness when reading from guest memory
In reply to Miao WangWhen reading from guest memory into vector registers, endianness should be considered since the data in guest memory is in guest endianness and the values in vector registers is in host endianness. Add MO_LE flag to tcg_gen_qemu_ld_i64() to fix this.
--- target/loongarch/tcg/insn_trans/trans_vec.c.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
target/loongarch/tcg/insn_trans/trans_vec.c.inc +1 -1
@@ -5397,7 +5397,7 @@ static bool do_vldrepl_vl(DisasContext *ctx, arg_vr_i *a, addr = make_address_i(ctx, addr, a->imm); - tcg_gen_qemu_ld_i64(val, addr, ctx->mem_idx, mop); + tcg_gen_qemu_ld_i64(val, addr, ctx->mem_idx, mop | MO_LE); tcg_gen_gvec_dup_i64(mop, vec_full_offset(a->vd), oprsz, ctx->vl / 8, val); return true;
Signature · 1 line
2.49.0
Re: [PATCH 5/6] target/loongarch: do_vldrepl_vl: Fix endianness when reading from guest memory
Quoted 19 lines · On 1/7/26 00:15, Miao Wang via B4 Relay wrote:
From: Miao Wang <[email protected]>
When reading from guest memory into vector registers, endianness should be considered since the data in guest memory is in guest endianness and the values in vector registers is in host endianness. Add
MO_LEflag totcg_gen_qemu_ld_i64()to fix this.Fixes 4a26512f01e0 ("target/loongarch: Implement xvld xvst")Signed-off-by Miao Wang <[email protected]>--- target/loongarch/tcg/insn_trans/trans_vec.c.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/loongarch/tcg/insn_trans/trans_vec.c.inc +0
-0
target/loongarch/tcg/insn_trans/trans_vec.c.inc static bool do_vldrepl_vl(DisasContext *ctx, arg_vr_i *a, · line 5403
Quoted 8 lines
target/loongarch/tcg/insn_trans/trans_vec.c.inc +1
-1@@ -5397,7 +5397,7 @@ static bool do_vldrepl_vl(DisasContext *ctx, arg_vr_i *a, addr = make_address_i(ctx, addr, a->imm); - tcg_gen_qemu_ld_i64(val, addr, ctx->mem_idx, mop); + tcg_gen_qemu_ld_i64(val, addr, ctx->mem_idx, mop | MO_LE); tcg_gen_gvec_dup_i64(mop, vec_full_offset(a->vd), oprsz, ctx->vl / 8, val); return true;
Missing counterpart in do_vstelm_vl(), otherwise:
Re: [PATCH 5/6] target/loongarch: do_vldrepl_vl: Fix endianness when reading from guest memory
Hi,
Quoted 17 lines
2026年7月1日 06:34,Philippe Mathieu-Daudé <[email protected]> 写道:
Quoted 14 lines · On 1/7/26 00:15, Miao Wang via B4 Relay wrote:
From: Miao Wang <[email protected]>
When reading from guest memory into vector registers, endianness should be considered since the data in guest memory is in guest endianness and the values in vector registers is in host endianness. AddMO_LEflag totcg_gen_qemu_ld_i64()to fix this.Fixes 4a26512f01e0 ("target/loongarch: Implement xvld xvst")Signed-off-by Miao Wang <[email protected]>--- target/loongarch/tcg/insn_trans/trans_vec.c.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/loongarch/tcg/insn_trans/trans_vec.c.inc +0
-0
Quoted 9 lines
target/loongarch/tcg/insn_trans/trans_vec.c.inc static bool do_vldrepl_vl(DisasContext *ctx, arg_vr_i *a, · line 5401
Quoted 6 lines
target/loongarch/tcg/insn_trans/trans_vec.c.inc +1
-0@@ -5397,7 +5397,7 @@ static bool do_vldrepl_vl(DisasContext *ctx, arg_vr_i *a, addr = make_address_i(ctx, addr, a->imm); - tcg_gen_qemu_ld_i64(val, addr, ctx->mem_idx, mop); + tcg_gen_qemu_ld_i64(val, addr, ctx->mem_idx, mop | MO_LE); tcg_gen_gvec_dup_i64(mop, vec_full_offset(a->vd), oprsz, ctx->vl / 8, val); return true;Missing counterpart in
do_vstelm_vl(), otherwise:
The fix for vstelm_vl() is in the next patch in the series.
Cheers,
Miao Wang
PATCH 6/6 target/loongarch: vstelm: Fix reading from vector registers
In reply to Miao WangWhen reading data from vector registers, endianess should be in consideration. Use tcg_gen_ld<len>_i64() functions to read a integer in certain length at a certain offset from vector registers into an i64 temporary variable. tcg_gen_ld_i64() cannot achieve this on big endian hosts. When writing data to guest memory, endianess should also be declared using the MO_LE flag for tcg_gen_qemu_st_i64().
--- target/loongarch/tcg/insn_trans/trans_vec.c.inc | 33 ++++++++++++++----------- 1 file changed, 18 insertions(+), 15 deletions(-)
target/loongarch/tcg/insn_trans/trans_vec.c.inc +18 -15
@@ -5423,7 +5423,8 @@ TRANS(xvldrepl_w, LASX, do_xvldrepl, MO_32) TRANS(xvldrepl_d, LASX, do_xvldrepl, MO_64) static bool do_vstelm_vl(DisasContext *ctx, - arg_vr_ii *a, uint32_t oprsz, MemOp mop) + arg_vr_ii *a, uint32_t oprsz, MemOp mop, + void (*func)(TCGv, TCGv_ptr, tcg_target_long)) { TCGv addr; TCGv_i64 val;
@@ -5436,29 +5437,31 @@ static bool do_vstelm_vl(DisasContext *ctx, val = tcg_temp_new_i64(); addr = make_address_i(ctx, addr, a->imm); - tcg_gen_ld_i64(val, tcg_env, vec_reg_offset(a->vd, a->imm2, mop)); - tcg_gen_qemu_st_i64(val, addr, ctx->mem_idx, mop); + func(val, tcg_env, vec_reg_offset(a->vd, a->imm2, mop)); + tcg_gen_qemu_st_i64(val, addr, ctx->mem_idx, mop | MO_LE); return true; } -static bool do_vstelm(DisasContext *ctx, arg_vr_ii *a, MemOp mop) +static bool do_vstelm(DisasContext *ctx, arg_vr_ii *a, MemOp mop, + void (*func)(TCGv, TCGv_ptr, tcg_target_long)) { - return do_vstelm_vl(ctx, a, 16, mop); + return do_vstelm_vl(ctx, a, 16, mop, func); } -static bool do_xvstelm(DisasContext *ctx, arg_vr_ii *a, MemOp mop) +static bool do_xvstelm(DisasContext *ctx, arg_vr_ii *a, MemOp mop, + void (*func)(TCGv, TCGv_ptr, tcg_target_long)) { - return do_vstelm_vl(ctx, a, 32, mop); + return do_vstelm_vl(ctx, a, 32, mop, func); } -TRANS(vstelm_b, LSX, do_vstelm, MO_8) -TRANS(vstelm_h, LSX, do_vstelm, MO_16) -TRANS(vstelm_w, LSX, do_vstelm, MO_32) -TRANS(vstelm_d, LSX, do_vstelm, MO_64) -TRANS(xvstelm_b, LASX, do_xvstelm, MO_8) -TRANS(xvstelm_h, LASX, do_xvstelm, MO_16) -TRANS(xvstelm_w, LASX, do_xvstelm, MO_32) -TRANS(xvstelm_d, LASX, do_xvstelm, MO_64) +TRANS(vstelm_b, LSX, do_vstelm, MO_8, tcg_gen_ld8u_i64) +TRANS(vstelm_h, LSX, do_vstelm, MO_16, tcg_gen_ld16u_i64) +TRANS(vstelm_w, LSX, do_vstelm, MO_32, tcg_gen_ld32u_i64) +TRANS(vstelm_d, LSX, do_vstelm, MO_64, tcg_gen_ld_i64) +TRANS(xvstelm_b, LASX, do_xvstelm, MO_8, tcg_gen_ld8u_i64) +TRANS(xvstelm_h, LASX, do_xvstelm, MO_16, tcg_gen_ld16u_i64) +TRANS(xvstelm_w, LASX, do_xvstelm, MO_32, tcg_gen_ld32u_i64) +TRANS(xvstelm_d, LASX, do_xvstelm, MO_64, tcg_gen_ld_i64) static bool gen_lasx_memory(DisasContext *ctx, arg_vr_i *a, void (*func)(DisasContext *, int, TCGv))
Signature · 1 line
2.49.0