Skip to contentSkip to conversation

target/arm: Fix SVE2 WHILEWR/WHILERW zero diff boundary case

PATCH In review Richard Henderson 3 participants 13 messages Aug 11 – Aug 18

lore
Share & tools
lore
#0 lore raw

v2 cover target/arm: Fix SVE2 WHILEWR/WHILERW zero diff boundary case

Use tcg_gen_ussub_i64 for WHILEWR.
Merge diff == 0 test with bounds check.

r~

Richard Henderson (3):
tcg: Export tcg_gen_ussub_i{32,64,tl}
target/arm: Fix SVE2 WHILEWR/WHILERW zero diff boundary case tests/tcg/aarch64: Add regression test for whilewr/whilerw

 include/tcg/tcg-op-common.h       |  2 ++
 include/tcg/tcg-op.h              |  2 ++
 target/arm/tcg/translate-sve.c    | 38 +++++++++++++++++++------------
 tcg/tcg-op-gvec.c                 | 14 ------------
 tcg/tcg-op.c                      | 20 ++++++++++++++++
 tests/tcg/aarch64/sve-while-ptr.c | 28 +++++++++++++++++++++++
 tests/tcg/aarch64/Makefile.target |  5 ++--
 7 files changed, 79 insertions(+), 30 deletions(-)
 create mode 100644 tests/tcg/aarch64/sve-while-ptr.c
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/3 tcg: Export tcg_gen_ussub_i{32,64,tl}

Move from tcg-op-gvec.c to tcg-op.c.
Use a temporary, to cover the possibility of operand overlap.

---
 include/tcg/tcg-op-common.h |  2 ++
 include/tcg/tcg-op.h        |  2 ++
 tcg/tcg-op-gvec.c           | 14 --------------
 tcg/tcg-op.c                | 20 ++++++++++++++++++++
 4 files changed, 24 insertions(+), 14 deletions(-)
include/tcg/tcg-op-common.h +2 -0
index 1fe342db0d..c7d1529c4f 100644
@@ -163,6 +163,7 @@ void tcg_gen_smin_i32(TCGv_i32, TCGv_i32 arg1, TCGv_i32 arg2);
 void tcg_gen_smax_i32(TCGv_i32, TCGv_i32 arg1, TCGv_i32 arg2);
 void tcg_gen_umin_i32(TCGv_i32, TCGv_i32 arg1, TCGv_i32 arg2);
 void tcg_gen_umax_i32(TCGv_i32, TCGv_i32 arg1, TCGv_i32 arg2);
+void tcg_gen_ussub_i32(TCGv_i32, TCGv_i32 arg1, TCGv_i32 arg2);
 void tcg_gen_abs_i32(TCGv_i32, TCGv_i32);
 
 /* Replicate a value of size @vece from @in to all the lanes in @out */
@@ -274,6 +275,7 @@ void tcg_gen_smin_i64(TCGv_i64, TCGv_i64 arg1, TCGv_i64 arg2);
 void tcg_gen_smax_i64(TCGv_i64, TCGv_i64 arg1, TCGv_i64 arg2);
 void tcg_gen_umin_i64(TCGv_i64, TCGv_i64 arg1, TCGv_i64 arg2);
 void tcg_gen_umax_i64(TCGv_i64, TCGv_i64 arg1, TCGv_i64 arg2);
+void tcg_gen_ussub_i64(TCGv_i64, TCGv_i64 arg1, TCGv_i64 arg2);
 void tcg_gen_abs_i64(TCGv_i64, TCGv_i64);
 
 /* Replicate a value of size @vece from @in to all the lanes in @out */
include/tcg/tcg-op.h +2 -0
index 96a5af1a29..40ce17eed2 100644
@@ -149,6 +149,7 @@ typedef TCGv_i64 TCGv;
 #define tcg_gen_umin_tl tcg_gen_umin_i64
 #define tcg_gen_smax_tl tcg_gen_smax_i64
 #define tcg_gen_umax_tl tcg_gen_umax_i64
+#define tcg_gen_ussub_tl tcg_gen_ussub_i64
 #define tcg_gen_atomic_cmpxchg_tl tcg_gen_atomic_cmpxchg_i64
 #define tcg_gen_atomic_xchg_tl tcg_gen_atomic_xchg_i64
 #define tcg_gen_atomic_fetch_add_tl tcg_gen_atomic_fetch_add_i64
@@ -268,6 +269,7 @@ typedef TCGv_i64 TCGv;
 #define tcg_gen_umin_tl tcg_gen_umin_i32
 #define tcg_gen_smax_tl tcg_gen_smax_i32
 #define tcg_gen_umax_tl tcg_gen_umax_i32
+#define tcg_gen_ussub_tl tcg_gen_ussub_i32
 #define tcg_gen_atomic_cmpxchg_tl tcg_gen_atomic_cmpxchg_i32
 #define tcg_gen_atomic_xchg_tl tcg_gen_atomic_xchg_i32
 #define tcg_gen_atomic_fetch_add_tl tcg_gen_atomic_fetch_add_i32
tcg/tcg-op-gvec.c +0 -14
index bc323e2500..27f3807b00 100644
@@ -2399,20 +2399,6 @@ void tcg_gen_gvec_usadd(unsigned vece, uint32_t dofs, uint32_t aofs,
     tcg_gen_gvec_3(dofs, aofs, bofs, oprsz, maxsz, &g[vece]);
 }
 
-static void tcg_gen_ussub_i32(TCGv_i32 d, TCGv_i32 a, TCGv_i32 b)
-{
-    TCGv_i32 min = tcg_constant_i32(0);
-    tcg_gen_sub_i32(d, a, b);
-    tcg_gen_movcond_i32(TCG_COND_LTU, d, a, b, min, d);
-}
-
-static void tcg_gen_ussub_i64(TCGv_i64 d, TCGv_i64 a, TCGv_i64 b)
-{
-    TCGv_i64 min = tcg_constant_i64(0);
-    tcg_gen_sub_i64(d, a, b);
-    tcg_gen_movcond_i64(TCG_COND_LTU, d, a, b, min, d);
-}
-
 void tcg_gen_gvec_ussub(unsigned vece, uint32_t dofs, uint32_t aofs,
                         uint32_t bofs, uint32_t oprsz, uint32_t maxsz)
 {
tcg/tcg-op.c +20 -0
index bbcb510c76..4d15d1abbd 100644
@@ -1264,6 +1264,16 @@ void tcg_gen_umax_i32(TCGv_i32 ret, TCGv_i32 a, TCGv_i32 b)
     tcg_gen_movcond_i32(TCG_COND_LTU, ret, a, b, b, a);
 }
 
+void tcg_gen_ussub_i32(TCGv_i32 ret, TCGv_i32 a, TCGv_i32 b)
+{
+    TCGv_i32 t = tcg_temp_ebb_new_i32();
+    TCGv_i32 z = tcg_constant_i32(0);
+
+    tcg_gen_sub_i32(t, a, b);
+    tcg_gen_movcond_i32(TCG_COND_LTU, ret, a, b, z, t);
+    tcg_temp_free_i32(t);
+}
+
 void tcg_gen_abs_i32(TCGv_i32 ret, TCGv_i32 a)
 {
     TCGv_i32 t = tcg_temp_ebb_new_i32();
@@ -2433,6 +2443,16 @@ void tcg_gen_umax_i64(TCGv_i64 ret, TCGv_i64 a, TCGv_i64 b)
     tcg_gen_movcond_i64(TCG_COND_LTU, ret, a, b, b, a);
 }
 
+void tcg_gen_ussub_i64(TCGv_i64 ret, TCGv_i64 a, TCGv_i64 b)
+{
+    TCGv_i64 t = tcg_temp_ebb_new_i64();
+    TCGv_i64 z = tcg_constant_i64(0);
+
+    tcg_gen_sub_i64(t, a, b);
+    tcg_gen_movcond_i64(TCG_COND_LTU, ret, a, b, z, t);
+    tcg_temp_free_i64(t);
+}
+
 void tcg_gen_abs_i64(TCGv_i64 ret, TCGv_i64 a)
 {
     TCGv_i64 t = tcg_temp_ebb_new_i64();
Signature · 1 line
2.43.0
Reviewed-by on v2 1/3
#3 lore raw

Re: [PATCH v2 1/3] tcg: Export tcg_gen_ussub_i{32,64,tl}

In reply to Richard Henderson
Quoted 10 lines · Richard Henderson

Move from tcg-op-gvec.c to tcg-op.c.
Use a temporary, to cover the possibility of operand overlap.

---
  include/tcg/tcg-op-common.h |  2 ++
  include/tcg/tcg-op.h        |  2 ++
  tcg/tcg-op-gvec.c           | 14 --------------
  tcg/tcg-op.c                | 20 ++++++++++++++++++++
  4 files changed, 24 insertions(+), 14 deletions(-)
#4 lore raw

PATCH v2 2/3 target/arm: Fix SVE2 WHILEWR/WHILERW zero diff boundary case

In reply to Richard Henderson

The trans_WHILE_ptr function incorrectly handles the case where the address difference divided by ESIZE results in zero. This happens when the address difference is less than ESIZE but greater than zero.

Fix by dropping direct comparisons of op0 vs op1, and instead testing the scaled diff vs 0. Merge with the bounding to the maximum vector length via wrapping arithmetic.

---
 target/arm/tcg/translate-sve.c | 38 +++++++++++++++++++++-------------
 1 file changed, 24 insertions(+), 14 deletions(-)
target/arm/tcg/translate-sve.c +24 -14
index fc4cc8c479..0becef16a9 100644
@@ -3697,7 +3697,7 @@ TRANS_FEAT(WHILE_gt_cnt4, aa64_sme2_or_sve2p1, do_WHILE,
 
 static bool trans_WHILE_ptr(DisasContext *s, arg_WHILE_ptr *a)
 {
-    TCGv_i64 op0, op1, diff, t1, tmax;
+    TCGv_i64 op0, op1, diff, t1;
     TCGv_i32 t2;
     TCGv_ptr ptr;
     unsigned vsz = vec_full_reg_size(s);
@@ -3713,7 +3713,6 @@ static bool trans_WHILE_ptr(DisasContext *s, arg_WHILE_ptr *a)
     op0 = read_cpu_reg(s, a->rn, 1);
     op1 = read_cpu_reg(s, a->rm, 1);
 
-    tmax = tcg_constant_i64(vsz >> a->esz);
     diff = tcg_temp_new_i64();
 
     if (a->rw) {
@@ -3723,25 +3722,36 @@ static bool trans_WHILE_ptr(DisasContext *s, arg_WHILE_ptr *a)
         tcg_gen_sub_i64(diff, op0, op1);
         tcg_gen_sub_i64(t1, op1, op0);
         tcg_gen_movcond_i64(TCG_COND_GEU, diff, op0, op1, diff, t1);
-        /* Divide, rounding down, by ESIZE.  */
-        tcg_gen_shri_i64(diff, diff, a->esz);
-        /* If op1 == op0, diff == 0, and the condition is always true. */
-        tcg_gen_movcond_i64(TCG_COND_EQ, diff, op0, op1, tmax, diff);
     } else {
         /* WHILEWR */
-        tcg_gen_sub_i64(diff, op1, op0);
-        /* Divide, rounding down, by ESIZE.  */
-        tcg_gen_shri_i64(diff, diff, a->esz);
-        /* If op0 >= op1, diff <= 0, the condition is always true. */
-        tcg_gen_movcond_i64(TCG_COND_GEU, diff, op0, op1, tmax, diff);
+        /* Saturating subtraction maps diff <= 0 to diff == 0. */
+        tcg_gen_ussub_i64(diff, op1, op0);
     }
 
-    /* Bound to the maximum.  */
-    tcg_gen_umin_i64(diff, diff, tmax);
+    /* Divide, rounding down, by ESIZE.  */
+    tcg_gen_shri_i64(diff, diff, a->esz);
 
-    /* Since we're bounded, pass as a 32-bit type.  */
+    /*
+     * If diff == 0, the condition is always true.  Also, bound to max.
+     * Simplify
+     *    diff = diff ? diff : max;
+     *    diff = umin(diff, max);
+     * via
+     *    diff -= 1;
+     *    diff = umin(diff, max - 1);
+     *    diff += 1;
+     * via 0 - 1 == UINT64_MAX.
+     */
+    tcg_gen_addi_i64(diff, diff, -1);
+    tcg_gen_umin_i64(diff, diff, tcg_constant_i64((vsz >> a->esz) - 1));
+
+    /*
+     * Since we're bounded, pass as a 32-bit type.
+     * Sink the diff += 1 from above into the 32-bit type.
+     */
     t2 = tcg_temp_new_i32();
     tcg_gen_extrl_i64_i32(t2, diff);
+    tcg_gen_addi_i32(t2, t2, 1);
 
     desc = FIELD_DP32(desc, PREDDESC, OPRSZ, vsz / 8);
     desc = FIELD_DP32(desc, PREDDESC, ESZ, a->esz);
Signature · 1 line
2.43.0
#5 lore raw
Quoted 24 lines · Richard Henderson

The trans_WHILE_ptr function incorrectly handles the case where the address difference divided by ESIZE results in zero. This happens when the address difference is less than ESIZE but greater than zero.

Fix by dropping direct comparisons of op0 vs op1, and instead testing the scaled diff vs 0. Merge with the bounding to the maximum vector length via wrapping arithmetic.

---
 target/arm/tcg/translate-sve.c | 38 +++++++++++++++++++++-------------
 1 file changed, 24 insertions(+), 14 deletions(-)
target/arm/tcg/translate-sve.c +7 -1
-    /* Since we're bounded, pass as a 32-bit type.  */
+    /*
+     * If diff == 0, the condition is always true.  Also, bound to max.
+     * Simplify
+     *    diff = diff ? diff : max;
+     *    diff = umin(diff, max);
+     * via
+     *    diff -= 1;

target/arm/tcg/translate-sve.c static bool trans_WHILE_ptr(DisasContext *s, arg_WHILE_ptr *a) · line 3750

Quoted 10 lines
target/arm/tcg/translate-sve.c +10 -0
+     *    diff = umin(diff, max - 1);
+     *    diff += 1;
+     * via 0 - 1 == UINT64_MAX.
+     */
+    tcg_gen_addi_i64(diff, diff, -1);
+    tcg_gen_umin_i64(diff, diff, tcg_constant_i64((vsz >> a->esz) - 1));
+
+    /*
+     * Since we're bounded, pass as a 32-bit type.
+     * Sink the diff += 1 from above into the 32-bit type.

Is it worth doing that? Hosts are all 64-bits now, and surely there's not going to be a perf or codesize difference between "add 1" on a 32-bit type vs a 64-bit type ?

target/arm/tcg/translate-sve.c static bool trans_WHILE_ptr(DisasContext *s, arg_WHILE_ptr *a) · line 3757

Quoted 7 lines
target/arm/tcg/translate-sve.c +2 -0
+     */
     t2 = tcg_temp_new_i32();
     tcg_gen_extrl_i64_i32(t2, diff);
+    tcg_gen_addi_i32(t2, t2, 1);
 
     desc = FIELD_DP32(desc, PREDDESC, OPRSZ, vsz / 8);
     desc = FIELD_DP32(desc, PREDDESC, ESZ, a->esz);

-- PMM

Reviewed-by
#6 lore raw
Quoted 38 lines · Peter Maydell
Quoted 24 lines · Richard Henderson

The trans_WHILE_ptr function incorrectly handles the case where the address difference divided by ESIZE results in zero. This happens when the address difference is less than ESIZE but greater than zero.

Fix by dropping direct comparisons of op0 vs op1, and instead testing the scaled diff vs 0. Merge with the bounding to the maximum vector length via wrapping arithmetic.

---
 target/arm/tcg/translate-sve.c | 38 +++++++++++++++++++++-------------
 1 file changed, 24 insertions(+), 14 deletions(-)
target/arm/tcg/translate-sve.c +7 -1
-    /* Since we're bounded, pass as a 32-bit type.  */
+    /*
+     * If diff == 0, the condition is always true.  Also, bound to max.
+     * Simplify
+     *    diff = diff ? diff : max;
+     *    diff = umin(diff, max);
+     * via
+     *    diff -= 1;

target/arm/tcg/translate-sve.c static bool trans_WHILE_ptr(DisasContext *s, arg_WHILE_ptr *a) · line 3750

Quoted 10 lines
target/arm/tcg/translate-sve.c +10 -0
+     *    diff = umin(diff, max - 1);
+     *    diff += 1;
+     * via 0 - 1 == UINT64_MAX.
+     */
+    tcg_gen_addi_i64(diff, diff, -1);
+    tcg_gen_umin_i64(diff, diff, tcg_constant_i64((vsz >> a->esz) - 1));
+
+    /*
+     * Since we're bounded, pass as a 32-bit type.
+     * Sink the diff += 1 from above into the 32-bit type.
Quoted 3 lines

Is it worth doing that? Hosts are all 64-bits now, and surely there's not going to be a perf or codesize difference between "add 1" on a 32-bit type vs a 64-bit type ?

Other than that

thanks
-- PMM

#7 lore raw
In reply to Peter Maydell
Quoted 10 lines · Peter Maydell

target/arm/tcg/translate-sve.c static bool trans_WHILE_ptr(DisasContext *s, arg_WHILE_ptr *a) · line 3750

Quoted 6 lines
target/arm/tcg/translate-sve.c +6 -0
+    tcg_gen_addi_i64(diff, diff, -1);
+    tcg_gen_umin_i64(diff, diff, tcg_constant_i64((vsz >> a->esz) - 1));
+
+    /*
+     * Since we're bounded, pass as a 32-bit type.
+     * Sink the diff += 1 from above into the 32-bit type.

Is it worth doing that? Hosts are all 64-bits now, and surely there's not going to be a perf or codesize difference between "add 1" on a 32-bit type vs a 64-bit type ?

Technically one byte difference on x86_64.
But we could drop the whole truncation thing it you like.

r~

#8 lore raw
Quoted 9 lines · Richard Henderson
Quoted 7 lines · Peter Maydell
Quoted 6 lines
target/arm/tcg/translate-sve.c +6 -0
+    tcg_gen_addi_i64(diff, diff, -1);
+    tcg_gen_umin_i64(diff, diff, tcg_constant_i64((vsz >> a->esz) - 1));
+
+    /*
+     * Since we're bounded, pass as a 32-bit type.
+     * Sink the diff += 1 from above into the 32-bit type.
Quoted 5 lines
Quoted 3 lines

Is it worth doing that? Hosts are all 64-bits now, and surely there's not going to be a perf or codesize difference between "add 1" on a 32-bit type vs a 64-bit type ?

Technically one byte difference on x86_64.
But we could drop the whole truncation thing it you like.

I think mostly my feeling was that since the rearrangement of the umin() expressions above was already quite complicated, moving the last part of that down into the truncate-to-32-bits was adding extra complexity on top. Just doing the subtract-one in 64 bits and keeping the truncate would be fine, as then the two steps aren't interleaved with each other.

-- PMM

#9 lore raw
Quoted 18 lines · Peter Maydell
Quoted 9 lines · Richard Henderson
Quoted 7 lines · Peter Maydell
Quoted 6 lines
target/arm/tcg/translate-sve.c +6 -0
+    tcg_gen_addi_i64(diff, diff, -1);
+    tcg_gen_umin_i64(diff, diff, tcg_constant_i64((vsz >> a->esz) - 1));
+
+    /*
+     * Since we're bounded, pass as a 32-bit type.
+     * Sink the diff += 1 from above into the 32-bit type.
Quoted 5 lines
Quoted 3 lines

Is it worth doing that? Hosts are all 64-bits now, and surely there's not going to be a perf or codesize difference between "add 1" on a 32-bit type vs a 64-bit type ?

Technically one byte difference on x86_64.
But we could drop the whole truncation thing it you like.

Quoted 6 lines

I think mostly my feeling was that since the rearrangement of the umin() expressions above was already quite complicated, moving the last part of that down into the truncate-to-32-bits was adding extra complexity on top. Just doing the subtract-one in 64 bits and keeping the truncate would be fine, as then the two steps aren't interleaved with each other.

...but thinking more about it I don't think it's worth respinning the series just for this, so I've added it to target-arm.next (with some cc:stable tags).

-- PMM

#10 lore raw

PATCH v2 3/3 tests/tcg/aarch64: Add regression test for whilewr/whilerw

In reply to Richard Henderson
---
 tests/tcg/aarch64/sve-while-ptr.c | 28 ++++++++++++++++++++++++++++
 tests/tcg/aarch64/Makefile.target |  5 +++--
 2 files changed, 31 insertions(+), 2 deletions(-)
 create mode 100644 tests/tcg/aarch64/sve-while-ptr.c
tests/tcg/aarch64/sve-while-ptr.c new file +28 -0
new file mode 100644
index 0000000000..7db3b9cc03
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/* WHILEWR / WHILERW regression test */
+
+#include <sys/prctl.h>
+#include <assert.h>
+
+int main(int argc, char **argv)
+{
+    unsigned short p;
+    int set_vl_ret;
+
+    set_vl_ret = prctl(PR_SVE_SET_VL, 16, 0, 0, 0, 0);
+    assert(set_vl_ret == 16);
+
+    p = 0xdead;
+    asm("whilewr p0.s, %0, %1\n\t"
+        "str p0, [%2]"
+        : : "r"(8), "r"(11), "r"(&p) : "memory", "p0");
+    assert(p == 0x1111);
+
+    p = 0xdead;
+    asm("whilerw p0.s, %0, %1\n\t"
+        "str p0, [%2]"
+        : : "r"(8), "r"(11), "r"(&p) : "memory", "p0");
+    assert(p == 0x1111);
+
+    return 0;
+}
tests/tcg/aarch64/Makefile.target +3 -2
index 32f2689273..6cc81bf854 100644
@@ -203,8 +203,9 @@ endif
 endif
 
 ifneq ($(CROSS_CC_HAS_SVE2),)
-AARCH64_TESTS += test-826
-test-826: CFLAGS += $(CROSS_CC_HAS_SVE2)
+SVE2_TESTS = test-826 sve-while-ptr
+$(SVE2_TESTS): CFLAGS += $(CROSS_CC_HAS_SVE2)
+AARCH64_TESTS += $(SVE2_TESTS)
 endif
 
 TESTS += $(AARCH64_TESTS)
Signature · 1 line
2.43.0
Reviewed-by on v2 3/3
#11 lore raw

Re: [PATCH v2 3/3] tests/tcg/aarch64: Add regression test for whilewr/whilerw

Quoted 7 lines · Richard Henderson
---
 tests/tcg/aarch64/sve-while-ptr.c | 28 ++++++++++++++++++++++++++++
 tests/tcg/aarch64/Makefile.target |  5 +++--
 2 files changed, 31 insertions(+), 2 deletions(-)
 create mode 100644 tests/tcg/aarch64/sve-while-ptr.c

thanks
-- PMM

#12 lore raw
In reply to Richard Henderson
Quoted 6 lines · Richard Henderson

Use tcg_gen_ussub_i64 for WHILEWR.
Merge diff == 0 test with bounds check.

r~

I guess we should have a Fixes: <whatever> and a cc: stable ?

thanks
-- PMM