tcg/optimize: Fix s_mask for rotate by constant
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
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, #7i386, 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.
--- tcg/optimize.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
tcg/optimize.c +5 -2
@@ -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
PATCH 2/3 tests/tcg: Add tests for sign extension after rotate
In reply to Dominic PCover the rotr_i32 (ARM ror) and rotl_i32 (i386 roll) forms fixed by the previous patch. Both tests fail without it.
--- 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
@@ -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
@@ -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
@@ -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
@@ -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
PATCH 3/3 tests/tcg/i386: Build test-i386-opt-shr
In reply to Dominic PThe 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.
--- tests/tcg/i386/meson.build | 1 + 1 file changed, 1 insertion(+)
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': {},
Signature · 1 line
2.55.0
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, which5fe51606146had added in the meantime, so it is no longer built.Fixes b4e3e90d61 ("tests/tcg/i386: user tests")Assisted-by Claude Opus 5 (Anthropic)Signed-off-by Dominic P <[email protected]>--- tests/tcg/i386/meson.build | 1 + 1 file changed, 1 insertion(+)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': {},
Re: [PATCH 3/3] tests/tcg/i386: Build test-i386-opt-shr
In reply to Dominic PQuoted 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, which5fe51606146had added in the meantime, so it is no longer built.Fixes b4e3e90d61 ("tests/tcg/i386: user tests")Assisted-by Claude Opus 5 (Anthropic)Signed-off-by Dominic P <[email protected]>--- tests/tcg/i386/meson.build | 1 + 1 file changed, 1 insertion(+)tests/tcg/i386/meson.build +0
-0
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
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, which5fe51606146had added in the meantime, so it is no longer built.Fixes b4e3e90d61 ("tests/tcg/i386: user tests")Assisted-by Claude Opus 5 (Anthropic)Signed-off-by Dominic P <[email protected]>--- 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@@ -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
Re: [PATCH 3/3] tests/tcg/i386: Build test-i386-opt-shr
In reply to Dominic PQuoted 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, which5fe51606146had added in the meantime, so it is no longer built.Fixes b4e3e90d61 ("tests/tcg/i386: user tests")Assisted-by Claude Opus 5 (Anthropic)Signed-off-by Dominic P <[email protected]>--- tests/tcg/i386/meson.build | 1 + 1 file changed, 1 insertion(+)tests/tcg/i386/meson.build +0
-0
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
Quoted 20 lines · Dominic P
fold_shiftrotatess_maskfor rotl/rotr by a constant. For I32 theINT32_MINOR infold_masks_zosa_intcan 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 like5fe51606146handled 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_64linux-user on9c88811b81, 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.rstdeclines 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 derives_maskfrom rotateds_masktests/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~