Tarantool development patches archive
 help / color / mirror / Atom feed
* [Tarantool-patches] [PATCH luajit] x86/x64: Don't sink IR_CONV num.int with -O-dce.
@ 2026-11-18 14:22 Sergey Kaplun via Tarantool-patches
  0 siblings, 0 replies; only message in thread
From: Sergey Kaplun via Tarantool-patches @ 2026-11-18 14:22 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 592f9e3f549c7f176a9a7a0d70189559305f2eda)

The CONV num.int is sunk even with disabled DCE optimization (which is
required for sink optimization). Hence, the register allocated for CONV
IR is the same as for the next IR (ADD, for example). This leads to the
incorrect results when exiting to the interpreter since the register
content is modified by the next instruction.

This patch fixes this by disabling sinking of the `IR_CONV` with
disabled DCE.

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-1537-conv-sink-without-dce
Related issues:
* https://github.com/LuaJIT/LuaJIT/issues/1537
* https://github.com/tarantool/tarantool/issues/13442

Side note: macOS (x86_64) CI is red due to runner problems.

 src/lj_asm.c                                  |  3 +-
 .../lj-1537-conv-sink-without-dce.test.lua    | 38 +++++++++++++++++++
 2 files changed, 40 insertions(+), 1 deletion(-)
 create mode 100644 test/tarantool-tests/lj-1537-conv-sink-without-dce.test.lua

diff --git a/src/lj_asm.c b/src/lj_asm.c
index f163b2e3..a4d6cf3f 100644
--- a/src/lj_asm.c
+++ b/src/lj_asm.c
@@ -933,7 +933,8 @@ static void asm_snap_alloc1(ASMState *as, IRRef ref)
       }
     } else {
       RegSet allow;
-      if (ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT) {
+      if (ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT &&
+	  (as->flags & JIT_F_OPT_DCE)) {
 	IRIns *irc;
 	for (irc = IR(as->curins); irc > ir; irc--)
 	  if ((irc->op1 == ref || irc->op2 == ref) &&
diff --git a/test/tarantool-tests/lj-1537-conv-sink-without-dce.test.lua b/test/tarantool-tests/lj-1537-conv-sink-without-dce.test.lua
new file mode 100644
index 00000000..4b332acb
--- /dev/null
+++ b/test/tarantool-tests/lj-1537-conv-sink-without-dce.test.lua
@@ -0,0 +1,38 @@
+local tap = require('tap')
+
+-- Test file to demonstrate LuaJIT incorrect sink of the `IR_CONV`
+-- with disabled DCE optimization.
+-- See also: https://github.com/LuaJIT/LuaJIT/issues/1537.
+local test = tap.test('lj-1537-conv-sink-without-dce'):skipcond({
+  ['Test requires JIT enabled'] = not jit.status(),
+})
+
+test:plan(1)
+
+jit.opt.start('hotloop=1', '-dce')
+
+-- Before the patch, the CONV num.int is sunk even with disabled
+-- DCE optimization (which is required for sink optimization).
+-- Hence, the register allocated for CONV IR is the same as for
+-- the next ADD IR, leading to the incorrect results when
+-- exiting to the interpreter.
+-- | 0004 xmm7     num CONV   0003  num.int
+-- | 0005 xmm7  >  num SLOAD  #4    T
+-- | 0006 xmm7   + num ADD    0005  +1
+-- | ...
+-- | 0009 ------------ LOOP ------------
+-- | 0010 xmm7   + num ADD    0006  +1
+-- | 0011 rbp    + int ADD    0007  +1
+-- | ....              SNAP   #3   [ ---- ---- ---- 0004 0010 ]
+
+local t = {}
+local conv, n = 0, 0
+for _ = 1, 4 do
+  -- Need preserve the conversion, not int value in a snap.
+  conv = #t * 1
+  n = n + 1 -- Occupies the same register.
+end
+
+test:is(conv, 0, 'correct value is restored from snapshot')
+
+test:done(true)
-- 
2.56.0


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

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

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-18 14:22 [Tarantool-patches] [PATCH luajit] x86/x64: Don't sink IR_CONV num.int with -O-dce 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