Skip to content

Commit 3dbb496

Browse files
committed
JIT: reorder reduction decrement before register reads in apply
Move `decrement_reductions_and_maybe_schedule_next` before reading extended registers and is_atom checks in OP_APPLY and OP_APPLY_LAST. This matches the interpreter's order and is required for WASM. Signed-off-by: Paul Guyot <pguyot@kallisys.net>
1 parent 8125935 commit 3dbb496

1 file changed

Lines changed: 14 additions & 14 deletions

File tree

libs/jit/src/jit.erl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,12 +1186,12 @@ first_pass(<<?OP_RAISE, Rest0/binary>>, MMod, MSt0, State0) ->
11861186
first_pass(<<?OP_APPLY, Rest0/binary>>, MMod, MSt0, State0) ->
11871187
?ASSERT_ALL_NATIVE_FREE(MSt0),
11881188
{Arity, Rest1} = decode_literal(Rest0),
1189-
{MSt1, Module} = read_any_xreg(Arity, MMod, MSt0),
1190-
{MSt2, Function} = read_any_xreg(Arity + 1, MMod, MSt1),
11911189
?TRACE("OP_APPLY ~p\n", [Arity]),
1192-
MSt3 = verify_is_atom(Module, 0, MMod, MSt2),
1193-
MSt4 = verify_is_atom(Function, 0, MMod, MSt3),
1194-
MSt5 = MMod:decrement_reductions_and_maybe_schedule_next(MSt4),
1190+
MSt1 = MMod:decrement_reductions_and_maybe_schedule_next(MSt0),
1191+
{MSt2, Module} = read_any_xreg(Arity, MMod, MSt1),
1192+
{MSt3, Function} = read_any_xreg(Arity + 1, MMod, MSt2),
1193+
MSt4 = verify_is_atom(Module, 0, MMod, MSt3),
1194+
MSt5 = verify_is_atom(Function, 0, MMod, MSt4),
11951195
MSt6 = MMod:call_primitive_with_cp(MSt5, ?PRIM_APPLY, [
11961196
ctx, jit_state, offset, {free, Module}, {free, Function}, Arity
11971197
]),
@@ -1202,12 +1202,12 @@ first_pass(<<?OP_APPLY_LAST, Rest0/binary>>, MMod, MSt0, State0) ->
12021202
?ASSERT_ALL_NATIVE_FREE(MSt0),
12031203
{Arity, Rest1} = decode_literal(Rest0),
12041204
{NWords, Rest2} = decode_literal(Rest1),
1205-
{MSt1, Module} = read_any_xreg(Arity, MMod, MSt0),
1206-
{MSt2, Function} = read_any_xreg(Arity + 1, MMod, MSt1),
12071205
?TRACE("OP_APPLY_LAST ~p, ~p\n", [Arity, NWords]),
1208-
MSt3 = verify_is_atom(Module, 0, MMod, MSt2),
1209-
MSt4 = verify_is_atom(Function, 0, MMod, MSt3),
1210-
MSt5 = MMod:decrement_reductions_and_maybe_schedule_next(MSt4),
1206+
MSt1 = MMod:decrement_reductions_and_maybe_schedule_next(MSt0),
1207+
{MSt2, Module} = read_any_xreg(Arity, MMod, MSt1),
1208+
{MSt3, Function} = read_any_xreg(Arity + 1, MMod, MSt2),
1209+
MSt4 = verify_is_atom(Module, 0, MMod, MSt3),
1210+
MSt5 = verify_is_atom(Function, 0, MMod, MSt4),
12111211
MSt6 = MMod:move_to_cp(MSt5, {y_reg, NWords}),
12121212
MSt7 = MMod:increment_sp(MSt6, NWords + 1),
12131213
MSt8 = MMod:call_primitive_last(MSt7, ?PRIM_APPLY, [
@@ -2442,11 +2442,11 @@ first_pass(<<?OP_CALL_FUN2, Rest0/binary>>, MMod, MSt0, State0) ->
24422442
?ASSERT_ALL_NATIVE_FREE(MSt0),
24432443
{MSt1, Tag, Rest1} = decode_compact_term(Rest0, MMod, MSt0, State0),
24442444
{ArgsCount, Rest2} = decode_literal(Rest1),
2445-
{MSt2, Fun, Rest3} = decode_typed_compact_term(Rest2, MMod, MSt1, State0),
2446-
?TRACE("OP_CALL_FUN2 ~p, ~p, ~p\n", [Tag, ArgsCount, Fun]),
2445+
?TRACE("OP_CALL_FUN2 ~p, ~p\n", [Tag, ArgsCount]),
24472446
% We ignore Tag (could be literal 0 or atom unsafe)
2448-
MSt3 = MMod:free_native_registers(MSt2, [Tag]),
2449-
MSt4 = MMod:decrement_reductions_and_maybe_schedule_next(MSt3),
2447+
MSt2 = MMod:free_native_registers(MSt1, [Tag]),
2448+
MSt3 = MMod:decrement_reductions_and_maybe_schedule_next(MSt2),
2449+
{MSt4, Fun, Rest3} = decode_typed_compact_term(Rest2, MMod, MSt3, State0),
24502450
{MSt5, Reg} = verify_is_function(Fun, MMod, MSt4),
24512451
MSt6 = MMod:call_primitive_with_cp(MSt5, ?PRIM_CALL_FUN, [
24522452
ctx, jit_state, offset, {free, Reg}, ArgsCount

0 commit comments

Comments
 (0)