Skip to contentSkip to conversation

target/loongarch: Fixes for endianness problems in vector instruction emulation

PATCH In review Miao Wang 3 participants 9 messages Jul 6 – Aug 18

lore
Share & tools
lore

Miao Wang

author
#0 lore raw

v2 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

---
Changes in v2:
- Use QEMU_ALIGNED(128) on the whole VReg struct according to Philippe's suggestion.
- Update Fixes: tag in the first patch.
- Link to v1: https://lore.kernel.org/qemu-devel/[email protected]

---
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]>
author
#1 lore raw

PATCH v2 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.

Fixes 2d5f950c054b ("target/loongarch: Implement vaddw/vsubw")
Fixes 16f5396cec23 ("target/loongarch: Add LSX data type VReg")
---
 target/loongarch/cpu.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/loongarch/cpu.h +1 -1
index ad30c73167ecf6d8dc6faa8c6a18c212fe4fce55..166d831f29c5d239e1c8129fa3448d6242fc562b 100644
@@ -276,7 +276,7 @@ typedef union VReg {
     uint32_t UW[LASX_LEN / 32];
     uint64_t UD[LASX_LEN / 64];
     Int128   Q[LASX_LEN / 128];
-} VReg;
+} QEMU_ALIGNED(16) VReg;
 
 typedef union fpr_t fpr_t;
 union fpr_t {
Signature · 1 line
2.49.0
Reviewed-by on v2 1/6
#2 lore raw

Re: [PATCH v2 1/6] target/loongarch: make VReg aligned to 16 bytes

Quoted 9 lines · On 6/7/26 22:02, 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, __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.

Quoted 6 lines
Fixes 2d5f950c054b ("target/loongarch: Implement vaddw/vsubw")
Fixes 16f5396cec23 ("target/loongarch: Add LSX data type VReg")
---
  target/loongarch/cpu.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
author
#3 lore raw

PATCH v2 2/6 target/loongarch: Fix offset calculation in vector registers

In reply to Miao Wang

When 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.

Fixes f5ce2c8f2c0a ("target/loongarch: Implement xvinsgr2vr xvpickve2gr")
---
 target/loongarch/tcg/translate.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
target/loongarch/tcg/translate.c +3 -3
index 124dce626954a85e5754de847ccba1796aebd8a6..ac4daadfdb5298a0850bdd0b1fce32407f1eeecc 100644
@@ -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
author
#4 lore raw

PATCH v2 3/6 target/loongarch: gen_vreplve: Fix offset calculation

In reply to Miao Wang

When 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().

Fixes df97f3380760 ("target/loongarch: Implement xvreplve xvinsve0 xvpickve")
Fixes 008a3b1662b6 ("target/loongarch: Add LASX data support")
---
 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
index 195b89f78834b208f0ba83930b844e792dc488b6..df0e809d544678490ca3ccfddab3fd4d74f7189a 100644
@@ -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
author
#5 lore raw

PATCH v2 4/6 target/loongarch: gen_xvreplve0: Fix elem0 offset calculation

In reply to Miao Wang

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.

Fixes df97f3380760 ("target/loongarch: Implement xvreplve xvinsve0 xvpickve")
---
 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
index df0e809d544678490ca3ccfddab3fd4d74f7189a..03938b1f052335190cdc437fc2d453b4f4fe16a4 100644
@@ -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
author
#6 lore raw

PATCH v2 5/6 target/loongarch: do_vldrepl_vl: Fix endianness when reading from guest memory

In reply to Miao Wang

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_LE flag to tcg_gen_qemu_ld_i64() to fix this.

Fixes 4a26512f01e0 ("target/loongarch: Implement xvld xvst")
---
 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
index 03938b1f052335190cdc437fc2d453b4f4fe16a4..91b1e15b61cac5d119892df1abd2ecf8a4c8e59d 100644
@@ -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
author
#7 lore raw

PATCH v2 6/6 target/loongarch: vstelm: Fix reading from vector registers

In reply to Miao Wang

When reading data from vector registers, endianess should be in consideration. Use tcg_gen_ld<len>_i64() functions to read an 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().

Fixes 4a26512f01e0 ("target/loongarch: Implement xvld xvst")
Fixes 843b627a395a ("target/loongarch: Implement vld vst")
---
 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
index 91b1e15b61cac5d119892df1abd2ecf8a4c8e59d..3cac981be1dba11db46744f88e851838aea651cd 100644
@@ -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
#8 lore raw
In reply to Miao Wang

Ping?

This should be picked up for qemu-stable too, since this patchset makes basic programs work on loong64-on-s390x for example.

Thanks,

/mjt

Quoted 49 lines · On 7/6/26 23:02, Miao Wang via B4 Relay wrote:

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

---
Changes in v2:
- Use QEMU_ALIGNED(128) on the whole VReg struct according to Philippe's suggestion.
- Update Fixes: tag in the first patch.
- Link to v1: https://lore.kernel.org/qemu-devel/[email protected]

---
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,