Skip to content

Commit c9588f5

Browse files
mralephkyukhin
authored andcommitted
fold: keep type of emitted CONV in sync with its mode
When emitting CONV make sure that its type matches its destination IRType. This keeps IR fully internally consistent with respect to types - i.e. if we push narrowing CONV Dt.St upwards through an arithmetic operation of type St we end up with arithmetic operation of type Dt and two convertions CONV Dt.St which narrow the operands. Igor Munkin: * added a test for the problem * backported the original patch to tarantool/luajit repo * stripped some words not related to the patch itself Fixes LuaJIT#524 Signed-off-by: Vyacheslav Egorov <[email protected]> Signed-off-by: Igor Munkin <[email protected]>
1 parent a171d01 commit c9588f5

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/lj_opt_fold.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,8 +1261,8 @@ LJFOLDF(simplify_conv_narrow)
12611261
IRType t = irt_type(fins->t);
12621262
IRRef op1 = fleft->op1, op2 = fleft->op2, mode = fins->op2;
12631263
PHIBARRIER(fleft);
1264-
op1 = emitir(IRTI(IR_CONV), op1, mode);
1265-
op2 = emitir(IRTI(IR_CONV), op2, mode);
1264+
op1 = emitir(IRT(IR_CONV, t), op1, mode);
1265+
op2 = emitir(IRT(IR_CONV, t), op2, mode);
12661266
fins->ot = IRT(op, t);
12671267
fins->op1 = op1;
12681268
fins->op2 = op2;

test/fold_bug_LuaJIT_524.test.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env tarantool
2+
3+
local tap = require('tap')
4+
local ffi = require('ffi')
5+
6+
local test = tap.test("LuaJIT 524")
7+
test:plan(1)
8+
9+
-- Test file to demonstrate LuaJIT folding machinery incorrect behaviour,
10+
-- details:
11+
-- https://github.com/LuaJIT/LuaJIT/issues/524
12+
-- https://github.com/moonjit/moonjit/issues/37
13+
14+
jit.opt.start(0, "fold", "cse", "fwd", "hotloop=1")
15+
16+
local sq = ffi.cast("uint32_t", 42)
17+
18+
for _ = 1, 3 do
19+
sq = ffi.cast("uint32_t", sq * sq)
20+
end
21+
22+
test:is(tonumber(sq), math.fmod(math.pow(42, 8), math.pow(2, 32)))
23+
24+
test:check()

0 commit comments

Comments
 (0)