Tarantool development patches archive
 help / color / mirror / Atom feed
* [Tarantool-patches] [PATCH luajit] x86/x64: Use scratch register for IR_TOBIT and IR_CONV u64.num.
@ 2026-11-18  8:48 Sergey Kaplun via Tarantool-patches
  0 siblings, 0 replies; only message in thread
From: Sergey Kaplun via Tarantool-patches @ 2026-11-18  8:48 UTC (permalink / raw)
  To: Sergey Bronnikov, Evgeniy Temirgaleev; +Cc: tarantool-patches

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

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
+-- 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)
-- 
2.56.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-18  8:48 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-18  8:48 [Tarantool-patches] [PATCH luajit] x86/x64: Use scratch register for IR_TOBIT and IR_CONV u64.num Sergey Kaplun via Tarantool-patches

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox