Tarantool development patches archive
 help / color / mirror / Atom feed
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] x86/x64: Don't sink IR_CONV num.int with -O-dce.
Date: Wed, 18 Nov 2026 17:22:42 +0300	[thread overview]
Message-ID: <20261118142242.3718800-1-skaplun@tarantool.org> (raw)

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


                 reply	other threads:[~2026-11-18 14:23 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261118142242.3718800-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] x86/x64: Don'\''t sink IR_CONV num.int with -O-dce.' \
    /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