From: Sergey Bronnikov via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Sergey Kaplun <skaplun@tarantool.org>,
Evgeniy Temirgaleev <e.temirgaleev@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: Re: [Tarantool-patches] [PATCH luajit] x86/x64: Use scratch register for IR_TOBIT and IR_CONV u64.num.
Date: Mon, 23 Nov 2026 18:33:43 +0300 [thread overview]
Message-ID: <79a76da9-a60f-41f6-b3bc-dcae4faa1ed1@tarantool.org> (raw)
In-Reply-To: <20261118084808.3591198-1-skaplun@tarantool.org>
Hi, Sergey,
thanks for the patch! Please see my comments below.
Sergey
On 11/18/26 11:48, Sergey Kaplun wrote:
> From: Mike Pall <mike>
>
> Reported by aleksisch. Thanks to Sergey Kaplun.
>
> (cherry picked from commit 5585364753b52ac7de4695026fc28be2480c82bd)
>
> If `asm_tobit()` allocates a register for the `op1` in the TOBIT IR, its
> value is modified by adding the TOBIT bias (the second operand). Hence,
> the restoring value of the source IR is incorrect due to this
> modification.
>
> This patch uses a scratch register instead. The `IR_CONV` case is
> similar but is unobservable since the adding value `LJ_K64_M2P64`
> (-2^64) is overlapped in the uint64_t representation.
>
> Sergey Kaplun:
> * added the description for the problem
and a regression test, right?
>
> Part of tarantool/tarantool#13442
> ---
>
> Branch: https://github.com/tarantool/luajit/tree/skaplun/lj-1536-tobit-regalloc
> Related issues:
> * https://github.com/LuaJIT/LuaJIT/issues/1536
> * https://github.com/tarantool/tarantool/issues/13442
>
> Side note: macOS (x86_64) CI is red due to runner problems. The metrics
> CI is red due to problems with configuration.
>
> src/lj_asm_x86.h | 7 ++--
> .../lj-1536-tobit-regalloc.test.lua | 36 +++++++++++++++++++
> 2 files changed, 38 insertions(+), 5 deletions(-)
> create mode 100644 test/tarantool-tests/lj-1536-tobit-regalloc.test.lua
>
> diff --git a/src/lj_asm_x86.h b/src/lj_asm_x86.h
> index 0e3a473c..8e2156eb 100644
> --- a/src/lj_asm_x86.h
> +++ b/src/lj_asm_x86.h
> @@ -802,9 +802,7 @@ static void asm_tointg(ASMState *as, IRIns *ir, Reg left)
> static void asm_tobit(ASMState *as, IRIns *ir)
> {
> Reg dest = ra_dest(as, ir, RSET_GPR);
> - Reg tmp = ra_noreg(IR(ir->op1)->r) ?
> - ra_alloc1(as, ir->op1, RSET_FPR) :
> - ra_scratch(as, RSET_FPR);
> + Reg tmp = ra_scratch(as, RSET_FPR);
> Reg right;
> emit_rr(as, XO_MOVDto, tmp, dest);
> right = asm_fuseload(as, ir->op2, rset_exclude(RSET_FPR, tmp));
> @@ -867,8 +865,7 @@ static void asm_conv(ASMState *as, IRIns *ir)
> if (LJ_64 ? irt_isu64(ir->t) : irt_isu32(ir->t)) {
> /* LJ_64: For inputs >= 2^63 add -2^64, convert again. */
> /* LJ_32: For inputs >= 2^31 add -2^31, convert again and add 2^31. */
> - Reg tmp = ra_noreg(IR(lref)->r) ? ra_alloc1(as, lref, RSET_FPR) :
> - ra_scratch(as, RSET_FPR);
> + Reg tmp = ra_scratch(as, RSET_FPR);
> MCLabel l_end = emit_label(as);
> if (LJ_32)
> emit_gri(as, XG_ARITHi(XOg_ADD), dest, (int32_t)0x80000000);
> diff --git a/test/tarantool-tests/lj-1536-tobit-regalloc.test.lua b/test/tarantool-tests/lj-1536-tobit-regalloc.test.lua
> new file mode 100644
> index 00000000..120fa1b0
> --- /dev/null
> +++ b/test/tarantool-tests/lj-1536-tobit-regalloc.test.lua
> @@ -0,0 +1,36 @@
> +local tap = require('tap')
> +
> +-- Test file to demonstrate incorrect register allocation for
> +-- `IR_TOBIT` on x86/x64.
> +-- See also: https://github.com/LuaJIT/LuaJIT/issues/1536.
> +local test = tap.test('lj-1536-tobit-regalloc'):skipcond({
> + ['Test requires JIT enabled'] = not jit.status(),
> +})
> +
> +test:plan(1)
> +
> +local tobit = bit.tobit
> +
> +jit.opt.start('hotloop=1')
> +
> +local res
> +local VALUE = 0x100
Why exactly this value?
> +-- Use a table for the `noreg` of the tobit operand.
> +local t = {k = VALUE}
> +-- 5 iterations to hit invariant loop part.
> +for _ = 1, 5 do
> + -- Generates the following IRs in the LOOP part:
> + -- 0011 xmm0 > num SLOAD #4 T
> + -- ...
> + -- 0015 ------------ LOOP ------------
> + -- 0016 rbp + int TOBIT 0011 bias
> + -- The resulting machine code adding the bias to the allocated
> + -- for the SLOAD (before the patch) register corrupts it and
> + -- hence restores an incorrect value to the interpreter.
> + res = tobit(t.k)
> + t.k = VALUE -- `noreg`.
> +end
> +
> +test:is(res, VALUE, 'correct tobit result')
> +
> +test:done(true)
prev parent reply other threads:[~2026-11-23 15:33 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 8:48 Sergey Kaplun via Tarantool-patches
2026-11-23 15:33 ` Sergey Bronnikov via Tarantool-patches [this message]
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=79a76da9-a60f-41f6-b3bc-dcae4faa1ed1@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] x86/x64: Use scratch register for IR_TOBIT and IR_CONV u64.num.' \
/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