Skip to contentSkip to conversation

tcg/optimize: Fix s_mask for rotate by constant

PATCH In review Dominic P 4 participants 9 messages Sep 30 – Oct 10 Arm/Amlogic Meson SoC support

lore
Share & tools
lore

Dominic P

author
#0 lore raw

cover tcg/optimize: Fix s_mask for rotate by constant

fold_shift rotates s_mask for rotl/rotr by a constant. For I32 the INT32_MIN OR in fold_masks_zosa_int can turn the result into a bogus sign mask, and the next sign extension is dropped (ARM sxtb; ror #1; sxtb, i386 movsbl; roll $31; movsbl). Patch 1 handles rotates like 5fe51606146 handled the logical right shift, patch 2 adds tests, patch 3 builds the test-i386-opt-shr test that the meson conversion dropped.

Found by differential testing of GCC output under qemu-arm.

Tested: make check-tcg for arm, aarch64, i386 and x86_64 linux-user on 9c88811b81, 312/312 pass. The two new tests fail without patch 1.

AI disclosure: these patches were prepared with an AI assistant (Claude, Anthropic); each commit carries an Assisted-by: trailer. I know docs/devel/code-provenance.rst declines such contributions and that checkpatch.pl rejects the trailer. I am sending them openly and leave acceptance to the maintainers. If they cannot be taken, please treat this as a bug report; the reproducers are in patch 1.

Dominic P (3):
tcg/optimize: Do not derive s_mask from rotated s_mask tests/tcg: Add tests for sign extension after rotate tests/tcg/i386: Build test-i386-opt-shr

 tcg/optimize.c                     |  7 +++++--
 tests/tcg/arm/meson.build          |  1 +
 tests/tcg/arm/test-arm-opt-ror.c   | 21 +++++++++++++++++++++
 tests/tcg/i386/meson.build         |  2 ++
 tests/tcg/i386/test-i386-opt-rol.c | 21 +++++++++++++++++++++
 5 files changed, 50 insertions(+), 2 deletions(-)
 create mode 100644 tests/tcg/arm/test-arm-opt-ror.c
 create mode 100644 tests/tcg/i386/test-i386-opt-rol.c
Signature · 1 line
2.55.0
author
#1 lore raw

PATCH 1/3 tcg/optimize: Do not derive s_mask from rotated s_mask

fold_shift computes s_mask for a rotate by a constant by rotating the input s_mask. The result is not a sign mask. For TCG_TYPE_I32, fold_masks_zosa_int then ORs in INT32_MIN, which can make it look like
one: after sextract_i32 x,0,8 (s_mask ...ffffff80), rotr_i32 by 1 gives 0x7fffffc0, which becomes ...ffffffc0. A following sextract_i32 0,8 is then folded to a mov. rotl_i32 by 31 fails the same way.

ARM, qemu-arm exits 255 instead of 7 (correct with -one-insn-per-tb):

    ldr   r1, [r2]        @ *r2 == 3
    sxtb  r1, r1
    ror   r1, r1, #1
    sxtb  r1, r1          @ dropped
    cmp   r1, #0
    movle r0, #255
    movgt r0, #7

i386, the same with rotl_i32:

movsbl %al, %eax
roll $31, %eax
movsbl %al, %eax # dropped

Handle rotates like the logical right shift and let fold_masks_zo recompute s_mask from z_mask and o_mask.

Fixes 93a967fbb57 ("tcg/optimize: Propagate sign info for shifting")
Assisted-by Claude Opus 5 (Anthropic)
---
 tcg/optimize.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
tcg/optimize.c +5 -2
index 1e4d2a0a42..27b7682bd5 100644
@@ -2703,9 +2703,12 @@ static bool fold_shift(OptContext *ctx, TCGOp *op)
         z_mask = do_constant_folding(op->opc, ctx->type, z_mask, sh);
         o_mask = do_constant_folding(op->opc, ctx->type, o_mask, sh);
 
-        if (op->opc == INDEX_op_shr) {
+        if (op->opc == INDEX_op_shr ||
+            op->opc == INDEX_op_rotl ||
+            op->opc == INDEX_op_rotr) {
             /*
-             * Logical right shift will force the sign bit zero.
+             * Logical right shift will force the sign bit zero, and
+             * a rotate may move bits unrelated to the sign into it.
              * Don't bother computing s_mask and let fold_masks
              * recompute from z_mask.
              */
Signature · 1 line
2.55.0
author
#2 lore raw

PATCH 2/3 tests/tcg: Add tests for sign extension after rotate

In reply to Dominic P

Cover the rotr_i32 (ARM ror) and rotl_i32 (i386 roll) forms fixed by the previous patch. Both tests fail without it.

Assisted-by Claude Opus 5 (Anthropic)
---
 tests/tcg/arm/meson.build          |  1 +
 tests/tcg/arm/test-arm-opt-ror.c   | 21 +++++++++++++++++++++
 tests/tcg/i386/meson.build         |  1 +
 tests/tcg/i386/test-i386-opt-rol.c | 21 +++++++++++++++++++++
 4 files changed, 44 insertions(+)
 create mode 100644 tests/tcg/arm/test-arm-opt-ror.c
 create mode 100644 tests/tcg/i386/test-i386-opt-rol.c
tests/tcg/arm/meson.build +1 -0
index 8f70411dfd..fd82b5c7a6 100644
@@ -39,6 +39,7 @@ tests += {
   },
   'pcalign-a32.c': {'cflags': ['-marm']},
   'issue4448.c': {},
+  'test-arm-opt-ror.c': {'cflags': ['-marm']},
 }
 
 # Vector SHA1
tests/tcg/arm/test-arm-opt-ror.c new file +21 -0
new file mode 100644
index 0000000000..2579168a13
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/* Regression test for tcg optimize vs sign bit repetitions after rotate. */
+
+#include <assert.h>
+
+int main(void)
+{
+    /* Load from memory so that TCG does not see a constant. */
+    volatile int v = 3;
+    int x = v, gt;
+
+    asm("sxtb %1, %1\n\t"
+        "ror %1, %1, #1\n\t"
+        "sxtb %1, %1\n\t"
+        "cmp %1, #0\n\t"
+        "movle %0, #0\n\t"
+        "movgt %0, #1"
+        : "=r"(gt), "+r"(x) : : "cc");
+    assert(gt);
+    return 0;
+}
tests/tcg/i386/meson.build +1 -0
index 04d438a062..4152371a3f 100644
@@ -28,6 +28,7 @@ tests += {
   'test-i386-fxtract.c': {},
   'test-i386-fyl2x.c': {},
   'test-i386-fyl2xp1.c': {},
+  'test-i386-opt-rol.c': {},
   'test-i386-pcmpistri.c': {'cflags': ['-msse4.2']},
   'test-i386-pseudo-denormal.c': {},
   'test-i386-snan-convert.c': {},
tests/tcg/i386/test-i386-opt-rol.c new file +21 -0
new file mode 100644
index 0000000000..e0393147e1
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/* Regression test for tcg optimize vs sign bit repetitions after rotate. */
+
+#include <assert.h>
+
+int main(void)
+{
+    /* Load from memory so that TCG does not see a constant. */
+    volatile int v = 3;
+    int x = v;
+    char test;
+
+    asm("movsbl %b1, %1\n\t"
+        "roll $31, %1\n\t"
+        "movsbl %b1, %1\n\t"
+        "testl %1, %1\n\t"
+        "setg %0"
+        : "=q"(test), "+q"(x));
+    assert(test);
+    return 0;
+}
Signature · 1 line
2.55.0
author
#3 lore raw

PATCH 3/3 tests/tcg/i386: Build test-i386-opt-shr

In reply to Dominic P

The old Makefile.target built every tests/tcg/i386/*.c. The meson conversion lists the tests explicitly and missed test-i386-opt-shr.c, which 5fe51606146 had added in the meantime, so it is no longer built.

Fixes b4e3e90d61 ("tests/tcg/i386: user tests")
Assisted-by Claude Opus 5 (Anthropic)
---
 tests/tcg/i386/meson.build | 1 +
 1 file changed, 1 insertion(+)
tests/tcg/i386/meson.build +1 -0
index 4152371a3f..26bd85c05e 100644
@@ -28,6 +28,7 @@ tests += {
   'test-i386-fxtract.c': {},
   'test-i386-fyl2x.c': {},
   'test-i386-fyl2xp1.c': {},
+  'test-i386-opt-shr.c': {},
   'test-i386-opt-rol.c': {},
   'test-i386-pcmpistri.c': {'cflags': ['-msse4.2']},
   'test-i386-pseudo-denormal.c': {},
Signature · 1 line
2.55.0
#4 lore raw

Re: [PATCH 3/3] tests/tcg/i386: Build test-i386-opt-shr

Cc'ing Pierrick

Quoted 23 lines · Dominic P

The old Makefile.target built every tests/tcg/i386/*.c. The meson conversion lists the tests explicitly and missed test-i386-opt-shr.c, which 5fe51606146 had added in the meantime, so it is no longer built.

Fixes b4e3e90d61 ("tests/tcg/i386: user tests")
Assisted-by Claude Opus 5 (Anthropic)
---
  tests/tcg/i386/meson.build | 1 +
  1 file changed, 1 insertion(+)
tests/tcg/i386/meson.build +1 -0
index 4152371a3f..26bd85c05e 100644
@@ -28,6 +28,7 @@ tests += {
    'test-i386-fxtract.c': {},
    'test-i386-fyl2x.c': {},
    'test-i386-fyl2xp1.c': {},
+  'test-i386-opt-shr.c': {},
    'test-i386-opt-rol.c': {},
    'test-i386-pcmpistri.c': {'cflags': ['-msse4.2']},
    'test-i386-pseudo-denormal.c': {},
#5 lore raw

Re: [PATCH 3/3] tests/tcg/i386: Build test-i386-opt-shr

In reply to Dominic P
Quoted 16 lines · Dominic P

The old Makefile.target built every tests/tcg/i386/*.c. The meson conversion lists the tests explicitly and missed test-i386-opt-shr.c, which 5fe51606146 had added in the meantime, so it is no longer built.

Fixes b4e3e90d61 ("tests/tcg/i386: user tests")
Assisted-by Claude Opus 5 (Anthropic)
---
 tests/tcg/i386/meson.build | 1 +
 1 file changed, 1 insertion(+)
tests/tcg/i386/meson.build +0 -0
index 4152371a3f..26bd85c05e 100644

tests/tcg/i386/meson.build tests += { · line 34

Quoted 7 lines
tests/tcg/i386/meson.build +1 -0
@@ -28,6 +28,7 @@ tests += {
   'test-i386-fxtract.c': {},
   'test-i386-fyl2x.c': {},
   'test-i386-fyl2xp1.c': {},
+  'test-i386-opt-shr.c': {},
   'test-i386-opt-rol.c': {},
   'test-i386-pcmpistri.c': {'cflags': ['-msse4.2']},
   'test-i386-pseudo-denormal.c': {},

Thanks Dominic, this indeed has slipped through the cracks.
I double checked all the rest, and found one missing test for hexagon too.

I'll pull this patch in upcoming tcg tests PR today.

Regards,
Pierrick

#6 lore raw

Re: [PATCH 3/3] tests/tcg/i386: Build test-i386-opt-shr

Quoted 12 lines · Pierrick Bouvier
Quoted 11 lines · Dominic P

The old Makefile.target built every tests/tcg/i386/*.c. The meson conversion lists the tests explicitly and missed test-i386-opt-shr.c, which 5fe51606146 had added in the meantime, so it is no longer built.

Fixes b4e3e90d61 ("tests/tcg/i386: user tests")
Assisted-by Claude Opus 5 (Anthropic)
---
 tests/tcg/i386/meson.build | 1 +
 1 file changed, 1 insertion(+)
Quoted 16 lines

tests/tcg/i386/meson.build tests += { · line 34

Quoted 12 lines
tests/tcg/i386/meson.build +1 -0
index 4152371a3f..26bd85c05e 100644
@@ -28,6 +28,7 @@ tests += {
   'test-i386-fxtract.c': {},
   'test-i386-fyl2x.c': {},
   'test-i386-fyl2xp1.c': {},
+  'test-i386-opt-shr.c': {},
   'test-i386-opt-rol.c': {},
   'test-i386-pcmpistri.c': {'cflags': ['-msse4.2']},
   'test-i386-pseudo-denormal.c': {},

Thanks Dominic, this indeed has slipped through the cracks.
I double checked all the rest, and found one missing test for hexagon too.

For information, this will unfortunately conflict with current series, since opt-shr is added after opt-rol.

Quoted 4 lines

I'll pull this patch in upcoming tcg tests PR today.

Regards,
Pierrick

#7 lore raw

Re: [PATCH 3/3] tests/tcg/i386: Build test-i386-opt-shr

In reply to Dominic P
Quoted 16 lines · Dominic P

The old Makefile.target built every tests/tcg/i386/*.c. The meson conversion lists the tests explicitly and missed test-i386-opt-shr.c, which 5fe51606146 had added in the meantime, so it is no longer built.

Fixes b4e3e90d61 ("tests/tcg/i386: user tests")
Assisted-by Claude Opus 5 (Anthropic)
---
 tests/tcg/i386/meson.build | 1 +
 1 file changed, 1 insertion(+)
tests/tcg/i386/meson.build +0 -0
index 4152371a3f..26bd85c05e 100644

tests/tcg/i386/meson.build tests += { · line 34

Quoted 7 lines
tests/tcg/i386/meson.build +1 -0
@@ -28,6 +28,7 @@ tests += {
   'test-i386-fxtract.c': {},
   'test-i386-fyl2x.c': {},
   'test-i386-fyl2xp1.c': {},
+  'test-i386-opt-shr.c': {},
   'test-i386-opt-rol.c': {},
   'test-i386-pcmpistri.c': {'cflags': ['-msse4.2']},
   'test-i386-pseudo-denormal.c': {},

This specific patch was merged into master
(2a0ac9c8ac2d303ca515dc819043409aef18cab5).
Thank you for your contribution!

Regards,
Pierrick

#8 lore raw
In reply to Dominic P
Quoted 20 lines · Dominic P

fold_shift rotates s_mask for rotl/rotr by a constant. For I32 the INT32_MIN OR in fold_masks_zosa_int can turn the result into a bogus sign mask, and the next sign extension is dropped (ARM sxtb; ror #1; sxtb, i386 movsbl; roll $31; movsbl). Patch 1 handles rotates like 5fe51606146 handled the logical right shift, patch 2 adds tests, patch 3 builds the test-i386-opt-shr test that the meson conversion dropped.

Found by differential testing of GCC output under qemu-arm.

Tested: make check-tcg for arm, aarch64, i386 and x86_64 linux-user on 9c88811b81, 312/312 pass. The two new tests fail without patch 1.

AI disclosure: these patches were prepared with an AI assistant (Claude, Anthropic); each commit carries an Assisted-by: trailer. I know docs/devel/code-provenance.rst declines such contributions and that checkpatch.pl rejects the trailer. I am sending them openly and leave acceptance to the maintainers. If they cannot be taken, please treat this as a bug report; the reproducers are in patch 1.

Quoted 4 lines

Dominic P (3):
tcg/optimize: Do not derive s_mask from rotated s_mask tests/tcg: Add tests for sign extension after rotate tests/tcg/i386: Build test-i386-opt-shr

Thanks. I've taken the first two patches into tcg-next, since the third has already been merged. I tweaked the if condition into a switch.

r~