Skip to content

Commit f27e6ca

Browse files
Mike PallBuristan
authored andcommitted
x86/x64: Change ipairs_aux to match JIT backend behavior.
Thanks to Sergey Kaplun. (cherry picked from commit 64b1f10) In the single-number VM, `ipairs_aux()` first adds the 1 to the given number and only after casts it to an integer. This leads to results different from PUC Rio Lua 5.1 and inconsistent with JIT engine recording. This patch fixes it by casting the value to an integer before addition. Sergey Kaplun: * added the description and the test for the problem Part of tarantool/tarantool#12480 Reviewed-by: Evgeniy Temirgaleev <e.temirgaleev@tarantool.org> Reviewed-by: Sergey Bronnikov <sergeyb@tarantool.org> Signed-off-by: Sergey Kaplun <skaplun@tarantool.org>
1 parent 74d1f91 commit f27e6ca

3 files changed

Lines changed: 38 additions & 9 deletions

File tree

src/vm_x64.dasc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,17 +1476,15 @@ static void build_subroutines(BuildCtx *ctx)
14761476
| checkint RA, ->fff_fallback
14771477
|.else
14781478
| checknumtp [BASE+8], ->fff_fallback
1479-
| movsd xmm0, qword [BASE+8]
1479+
| cvttsd2si RAd, qword [BASE+8]
14801480
|.endif
14811481
| mov PC, [BASE-8]
1482-
|.if DUALNUM
14831482
| add RAd, 1
1483+
|.if DUALNUM
14841484
| setint ITYPE, RA
14851485
| mov [BASE-16], ITYPE
14861486
|.else
1487-
| sseconst_1 xmm1, TMPR
1488-
| addsd xmm0, xmm1
1489-
| cvttsd2si RAd, xmm0
1487+
| cvtsi2sd xmm0, RAd
14901488
| movsd qword [BASE-16], xmm0
14911489
|.endif
14921490
| cmp RAd, TAB:RB->asize; jae >2 // Not in array part?

src/vm_x86.dasc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1853,10 +1853,9 @@ static void build_subroutines(BuildCtx *ctx)
18531853
| mov dword [BASE-4], LJ_TISNUM
18541854
| mov dword [BASE-8], RD
18551855
|.else
1856-
| movsd xmm0, qword [BASE+8]
1857-
| sseconst_1 xmm1, RBa
1858-
| addsd xmm0, xmm1
1859-
| cvttsd2si RD, xmm0
1856+
| cvttsd2si RD, qword [BASE+8]
1857+
| add RD, 1
1858+
| cvtsi2sd xmm0, RD
18601859
| movsd qword [BASE-8], xmm0
18611860
|.endif
18621861
| mov TAB:RB, [BASE]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
local tap = require('tap')
2+
3+
-- The test file to demonstrate the inconsistent behaviour between
4+
-- the JIT compiler and the VM for the `ipairs_aux()` function on
5+
-- x86 and x86_64 arches.
6+
-- See also: https://github.com/LuaJIT/LuaJIT/issues/1463.
7+
8+
local test = tap.test('lj-1463-ipairs-aux-consistency'):skipcond({
9+
['Test requires JIT enabled'] = not jit.status(),
10+
})
11+
12+
test:plan(4)
13+
14+
jit.opt.start('hotloop=1')
15+
16+
local ipairs_aux = ipairs({})
17+
18+
local rkeys = {}
19+
local rvals = {}
20+
21+
for i = 1, 4 do
22+
local key, val = ipairs_aux({[0] = 0, [1] = 1}, -0.1)
23+
rkeys[i] = key
24+
rvals[i] = val
25+
end
26+
27+
test:is(rkeys[1], 1, 'correct key result')
28+
test:is(rvals[1], 1, 'correct value result')
29+
test:samevalues(rkeys, 'consistent JIT and VM behaviour for keys')
30+
test:samevalues(rvals, 'consistent JIT and VM behaviour for values')
31+
32+
test:done(true)

0 commit comments

Comments
 (0)