From: Sergey Kaplun via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Sergey Bronnikov <sergeyb@tarantool.org>,
Evgeniy Temirgaleev <e.temirgaleev@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH luajit] ARM64: Don't fuse sign extensions into logical operands.
Date: Mon, 16 Nov 2026 15:05:33 +0300 [thread overview]
Message-ID: <20261116120533.2886125-1-skaplun@tarantool.org> (raw)
From: Mike Pall <mike>
Thanks to Peter Cawley.
(cherry picked from commit 90742d91c27d185b70d1b4a6343fb6b7c26002db)
Logical instructions [1] (`orr`, etc.) have no sign extension form
(unlike `ADD Xd, Xn, Wm, SXTW #k`). This makes the fusing sign extension
into them meaningless and leads to the incorrect result in the
JIT-compiled code.
This patch prevents the fusing of sign extension (`sxtw`) into logical
instructions.
[1]: https://cybersandeep.gitbook.io/arm64basicguide/chapter-3-basic-arm64-instructions#id-3.2.2-logical-instructions
Sergey Kaplun:
* added the description and the test for the problem
Part of tarantool/tarantool#13442
---
Branch: https://github.com/tarantool/luajit/tree/skaplun/lj-1076-not-fuse-sign-ext-logical
Related issues:
* https://github.com/LuaJIT/LuaJIT/issues/1076
* https://github.com/tarantool/tarantool/issues/13442
src/lj_asm_arm64.h | 11 ++-
...lj-1076-not-fuse-sign-ext-logical.test.lua | 70 +++++++++++++++++++
2 files changed, 74 insertions(+), 7 deletions(-)
create mode 100644 test/tarantool-tests/lj-1076-not-fuse-sign-ext-logical.test.lua
diff --git a/src/lj_asm_arm64.h b/src/lj_asm_arm64.h
index f731ab05..f080dd6e 100644
--- a/src/lj_asm_arm64.h
+++ b/src/lj_asm_arm64.h
@@ -208,16 +208,13 @@ static Reg asm_fuseahuref(ASMState *as, IRRef ref, int32_t *ofsp, RegSet allow,
static uint32_t asm_fuseopm(ASMState *as, A64Ins ai, IRRef ref, RegSet allow)
{
IRIns *ir = IR(ref);
+ int logical = (ai & 0x1f000000) == 0x0a000000;
if (ra_hasreg(ir->r)) {
ra_noweak(as, ir->r);
return A64F_M(ir->r);
} else if (irref_isk(ref)) {
- uint32_t m;
int64_t k = get_k64val(as, ref);
- if ((ai & 0x1f000000) == 0x0a000000)
- m = emit_isk13(k, irt_is64(ir->t));
- else
- m = emit_isk12(k);
+ uint32_t m = logical ? emit_isk13(k, irt_is64(ir->t)) : emit_isk12(k);
if (m)
return m;
} else if (mayfuse(as, ref)) {
@@ -229,7 +226,7 @@ static uint32_t asm_fuseopm(ASMState *as, A64Ins ai, IRRef ref, RegSet allow)
(IR(ir->op2)->i & (irt_is64(ir->t) ? 63 : 31));
IRIns *irl = IR(ir->op1);
if (sh == A64SH_LSL &&
- irl->o == IR_CONV &&
+ irl->o == IR_CONV && !logical &&
irl->op2 == ((IRT_I64<<IRCONV_DSH)|IRT_INT|IRCONV_SEXT) &&
shift <= 4 &&
canfuse(as, irl)) {
@@ -239,7 +236,7 @@ static uint32_t asm_fuseopm(ASMState *as, A64Ins ai, IRRef ref, RegSet allow)
Reg m = ra_alloc1(as, ir->op1, allow);
return A64F_M(m) | A64F_SH(sh, shift);
}
- } else if (ir->o == IR_CONV &&
+ } else if (ir->o == IR_CONV && !logical &&
ir->op2 == ((IRT_I64<<IRCONV_DSH)|IRT_INT|IRCONV_SEXT)) {
Reg m = ra_alloc1(as, ir->op1, allow);
return A64F_M(m) | A64F_EX(A64EX_SXTW);
diff --git a/test/tarantool-tests/lj-1076-not-fuse-sign-ext-logical.test.lua b/test/tarantool-tests/lj-1076-not-fuse-sign-ext-logical.test.lua
new file mode 100644
index 00000000..b199648d
--- /dev/null
+++ b/test/tarantool-tests/lj-1076-not-fuse-sign-ext-logical.test.lua
@@ -0,0 +1,70 @@
+local tap = require('tap')
+
+-- The test file to demonstrate incorrect LuaJIT's fusing of the
+-- sign extension instruction into logical instructions.
+-- See also: https://github.com/LuaJIT/LuaJIT/issues/1076.
+local test = tap.test('lj-1076-not-fuse-sign-ext-logical'):skipcond({
+ ['Test requires JIT enabled'] = not jit.status(),
+})
+
+test:plan(5)
+
+jit.opt.start('hotloop=1')
+
+local ffi = require('ffi')
+local cast = ffi.cast
+
+local bnot = bit.bnot
+
+local N = -5
+
+-- Test avoiding erroneous fusing of:
+-- | sxtw x1, w4
+-- | mvn x0, x1
+--
+-- to the:
+-- | mov x0, x4, lsl #48
+
+local r = {}
+for i = 1, 4 do
+ r[i] = bnot(cast('int64_t', N))
+end
+
+test:is(r[1], 4LL, 'correct result for bnot')
+test:samevalues(r, 'avoid fusion for explicit CONV with sign extension')
+
+-- Test avoiding erroneous fusing of:
+-- | sxtw x21, w23
+-- | orr x20, x20, x21
+--
+-- to the:
+-- | orn x20, x20, x23, lsl #48
+
+local function logical_shift_unfused(subtest, expected)
+ subtest:plan(2)
+ jit.flush()
+ jit.opt.start('hotloop=1')
+ -- Use code generation to avoid using non-constant variables
+ -- defining functions preventing folding and fusion.
+ local script = string.format([[
+ local bor = bit.bor
+ local arshift, lshift, rshift = bit.arshift, bit.lshift, bit.rshift
+ local N = -5
+ local zero = 0LL
+ local result = {}
+ for i = 1, 4 do
+ result[i] = bor(%s(N, 1), zero)
+ end
+ return result
+ ]], subtest.name)
+ local f = assert(loadstring(script, 'unfused ' .. subtest.name))
+ local result = f()
+ subtest:is(result[1], expected, 'correct result for ' .. subtest.name)
+ subtest:samevalues(result, 'avoid fusion sxtw and orr for ' .. subtest.name)
+end
+
+test:test('arshift', logical_shift_unfused, -3LL)
+test:test('lshift', logical_shift_unfused, -10LL)
+test:test('rshift', logical_shift_unfused, 0x7FFFFFFDLL)
+
+test:done(true)
--
2.56.0
next reply other threads:[~2026-11-16 12:06 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-16 12:05 Sergey Kaplun via Tarantool-patches [this message]
2026-11-17 13:25 ` Evgeniy Temirgaleev via Tarantool-patches
2026-11-18 8:31 ` Sergey Kaplun via Tarantool-patches
2026-11-18 9:42 ` Evgeniy Temirgaleev via Tarantool-patches
2026-11-18 11:39 ` Sergey Kaplun via Tarantool-patches
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261116120533.2886125-1-skaplun@tarantool.org \
--to=tarantool-patches@dev.tarantool.org \
--cc=e.temirgaleev@tarantool.org \
--cc=sergeyb@tarantool.org \
--cc=skaplun@tarantool.org \
--subject='Re: [Tarantool-patches] [PATCH luajit] ARM64: Don'\''t fuse sign extensions into logical operands.' \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox