Tarantool development patches archive
 help / color / mirror / Atom feed
* [Tarantool-patches] [PATCH luajit] ARM64: Fix BC_JFORI on empty FP range.
@ 2026-11-26  8:41 Sergey Kaplun via Tarantool-patches
  2026-11-26 11:46 ` Evgeniy Temirgaleev via Tarantool-patches
  0 siblings, 1 reply; 2+ messages in thread
From: Sergey Kaplun via Tarantool-patches @ 2026-11-26  8:41 UTC (permalink / raw)
  To: Evgeniy Temirgaleev, Sergey Bronnikov; +Cc: tarantool-patches

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-26 11:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-26  8:41 [Tarantool-patches] [PATCH luajit] ARM64: Fix BC_JFORI on empty FP range Sergey Kaplun via Tarantool-patches
2026-11-26 11:46 ` Evgeniy Temirgaleev 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