Tarantool development patches archive
 help / color / mirror / Atom feed
From: Sergey Kaplun via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Evgeniy Temirgaleev <e.temirgaleev@tarantool.org>,
	Sergey Bronnikov <sergeyb@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH luajit] ARM64: Fix BC_JFORI on empty FP range.
Date: Thu, 26 Nov 2026 11:41:42 +0300	[thread overview]
Message-ID: <20261126084142.2217311-1-skaplun@tarantool.org> (raw)

From: Mike Pall <mike>

Thanks to denesik.

(cherry picked from commit 2c9f5cf62ca9232d1aa3271f8e9c1799dd0f63d3)

`BC_JFORI` bytecode either jumps to the trace (`BC_JLOOP`) or should
proceed to the next instruction for the empty range. For this the
integer variant of the instruction moves to the `PC` (next bytecode to
be executed) the adjusted bytecode position after jump, which is stored
in `RC`. The floating point path of the instruction misses this fixup,
so the FP loop variant executes the loop body 1 time instead of 0 for an
empty range.

This patch fixes this by adding the missing fixup.

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-1530-fix-jfori-empty-step
Related issues:
* https://github.com/LuaJIT/LuaJIT/issues/1530
* https://github.com/tarantool/tarantool/issues/13442

 src/vm_arm64.dasc                             |  1 +
 .../lj-1530-fix-jfori-empty-step.test.lua     | 38 +++++++++++++++++++
 2 files changed, 39 insertions(+)
 create mode 100644 test/tarantool-tests/lj-1530-fix-jfori-empty-step.test.lua

diff --git a/src/vm_arm64.dasc b/src/vm_arm64.dasc
index 4ffd7378..41688252 100644
--- a/src/vm_arm64.dasc
+++ b/src/vm_arm64.dasc
@@ -3650,6 +3650,7 @@ static void build_ins(BuildCtx *ctx, BCOp op, int defop)
     if (op == BC_FORI) {
       |  csel PC, RC, PC, hi
     } else if (op == BC_JFORI) {
+      |  mov PC, RC
       |  ldrh RCw, [RC, #-4+OFS_RD]
       |  bls =>BC_JLOOP
     } else if (op == BC_IFORL) {
diff --git a/test/tarantool-tests/lj-1530-fix-jfori-empty-step.test.lua b/test/tarantool-tests/lj-1530-fix-jfori-empty-step.test.lua
new file mode 100644
index 00000000..a91780ec
--- /dev/null
+++ b/test/tarantool-tests/lj-1530-fix-jfori-empty-step.test.lua
@@ -0,0 +1,38 @@
+local tap = require('tap')
+
+-- The test file to demonstrate an incorrect LuaJIT handling of
+-- JFORI loop setup for the empty numeric loops. This test fails
+-- on ARM64 in particular, but it is worth checking for all
+-- architectures.
+-- See also: https://github.com/LuaJIT/LuaJIT/issues/1530.
+local test = tap.test('lj-1530-fix-jfori-empty-step'):skipcond({
+  ['Test requires JIT enabled'] = not jit.status(),
+})
+
+test:plan(2)
+
+-- Function to wrap the recorded FORI instruction.
+local function loop_fori(limit)
+  local cnt = 0
+  -- Use `1.1` to force a numeric loop. We may use a non-integer
+  -- limit for this, and then we end up on the same path after
+  -- `lj_meta_for()` casting the loop start index from integer to
+  -- double. But the current path is just simpler.
+  -- The loop body shouldn't be taken due to the empty range in
+  -- this test.
+  for _ = 1.1, limit do cnt = cnt + 1 end
+  return cnt
+end
+
+-- Check the interpreter just in case.
+test:is(loop_fori(0), 0, 'correct interpreter FORI loop skip')
+
+jit.opt.start('hotloop=1')
+
+-- Compile the loop.
+loop_fori(4.1)
+
+-- Run the empty loop range with the compiled instruction.
+test:is(loop_fori(0), 0, 'correct JFORI loop skip')
+
+test:done(true)
-- 
2.56.0


             reply	other threads:[~2026-11-26  8:39 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  8:41 Sergey Kaplun via Tarantool-patches [this message]
2026-11-26 11:46 ` Evgeniy Temirgaleev 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=20261126084142.2217311-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: Fix BC_JFORI on empty FP range.' \
    /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