From 28824ad64707eec57417d01dc93c7547685150fe Mon Sep 17 00:00:00 2001 From: Dave Lucia Date: Tue, 2 Jun 2026 16:09:23 -0700 Subject: [PATCH 1/4] perf(vm): drop +16 register buffer on interpreter :call path The interpreter sized every callee register tuple at `max(max_registers, param_count) + 16`, adding 16 slots of pure allocation churn per frame. For deep recursion (fib(28) ~ 1.4M frames) this dominated the dispatcher-vs-interpreter gap: the dispatcher's `init_regs/2` has always run buffer-free. Removing the buffer surfaced why it existed: the interpreter-only instruction-list codegen could undercount `max_registers` for a fixed destination register in deeply nested constructs (a concat chain inside a table-constructor list field nested in several `for` bodies emitted `{:concatenate, 53, _, _}` while reporting `max_registers = 53`). The +16 buffer was masking that off-by-one; the bytecode encoder rejects these multi-return shapes, so they only ever reach the interpreter and never the dispatcher. Rather than keep a fudge buffer, make `max_registers` honest: `instruction_peak/1` walks the emitted stream (recursing into loop and short-circuit bodies) and folds the true high-water register index into `max_registers` at both prototype-finalization sites. Dynamic-range writers (`:call`, unbounded `:vararg`) still grow the tuple lazily via `ensure_regs_capacity/2`; the interpreter's unbounded `:vararg` site now grows before writing, matching the dispatcher's `write_varargs/4`. Correctness preserved: full `mix test` (2153) plus the `:slow` level-4 short-circuit harness (the deepest register-pressure case, previously the overflow source) pass. Benchmarks (Apple M4): dispatcher_vs_interpreter fib(25): interpreter 95.00ms -> 74.83ms; dispatcher/interpreter gap 1.21x -> 1.03x (closed, not inverted). fibonacci fib(30) lua (chunk): 846.9ms -> 808.8ms. Closes #312 --- lib/lua/compiler/codegen.ex | 110 +++++++++++++++++++++++++++++++++++- lib/lua/vm/executor.ex | 20 +++++-- 2 files changed, 124 insertions(+), 6 deletions(-) diff --git a/lib/lua/compiler/codegen.ex b/lib/lua/compiler/codegen.ex index cd1ed6b7..28af4af6 100644 --- a/lib/lua/compiler/codegen.ex +++ b/lib/lua/compiler/codegen.ex @@ -49,7 +49,13 @@ defmodule Lua.Compiler.Codegen do upvalue_names: ["_ENV"], param_count: 0, is_vararg: func_scope.is_vararg, - max_registers: Enum.max([func_scope.max_register, ctx.next_reg, Map.get(ctx, :peak_reg, 0)]), + max_registers: + Enum.max([ + func_scope.max_register, + ctx.next_reg, + Map.get(ctx, :peak_reg, 0), + instruction_peak(instructions) + ]), source: source, lines: lines } @@ -87,6 +93,100 @@ defmodule Lua.Compiler.Codegen do Map.put(ctx, :peak_reg, max(Map.get(ctx, :peak_reg, 0), ctx.next_reg)) end + # Honest peak-register guarantee for `max_registers`. + # + # `peak_reg`/`next_reg` tracking can undercount in deeply nested + # constructs (e.g. a concat chain inside a list-field of a table + # constructor nested in several `for` bodies). Undercounting is benign + # only as long as a fixed register buffer absorbs the slack; the + # executor and dispatcher both run buffer-free, so `max_registers` must + # cover every statically-fixed destination register actually emitted. + # + # This walks the emitted instruction stream (including nested loop / + # short-circuit bodies) and returns the number of register slots needed + # — i.e. the highest fixed destination index plus one. Dynamic-range + # writers (`:call`, `:vararg`) are grown lazily at runtime via + # `ensure_regs_capacity/2`, so only their fixed `base` contributes here. + defp instruction_peak(instructions) do + Enum.reduce(instructions, 0, fn instr, acc -> + max(acc, instruction_size(instr)) + end) + end + + # Returns the register-slot count an instruction proves is needed (its + # highest written register index + 1), recursing into nested bodies. + defp instruction_size({:load_nil, dest, count}), do: dest + count + defp instruction_size({:vararg, base, count}) when is_integer(count) and count > 0, do: base + count + defp instruction_size({:vararg, base, _}), do: base + 1 + defp instruction_size({:self, base, _obj, _name, _hint}), do: base + 2 + defp instruction_size({:call, base, _ac, _rc, _hint}), do: base + 1 + defp instruction_size({:source_line, _line, _src}), do: 0 + defp instruction_size({:close_upvalues, _threshold}), do: 0 + defp instruction_size({:label, _name}), do: 0 + defp instruction_size({:set_list, _table, start, count, _offset}) when is_integer(count), do: start + count + defp instruction_size({:set_list, _table, start, {:multi, init}, _offset}), do: start + init + defp instruction_size({:numeric_for, base, _loop_var, body}), do: max(base + 3, instruction_peak(body)) + defp instruction_size({:generic_for, base, _var_regs, body}), do: max(base + 3, instruction_peak(body)) + + defp instruction_size({:while_loop, cond_body, _reg, body}), + do: max(instruction_peak(cond_body), instruction_peak(body)) + + defp instruction_size({:repeat_loop, body, cond_body, _reg}), + do: max(instruction_peak(body), instruction_peak(cond_body)) + + defp instruction_size({:test, _reg, then_body, else_body}), + do: max(instruction_peak(then_body), instruction_peak(else_body)) + + defp instruction_size({:test_and, dest, _src, body}), do: max(dest + 1, instruction_peak(body)) + defp instruction_size({:test_or, dest, _src, body}), do: max(dest + 1, instruction_peak(body)) + defp instruction_size({:scope, _count, body}), do: instruction_peak(body) + + # Instructions whose first integer operand is the sole written register: + # the destination. Reads never establish a high-water mark, so they are + # ignored. Anything else (set_table/set_field/set_global/set_upvalue/… + # writes to a table or upvalue, not a fresh register; :return/:break/…) + # contributes nothing. + defp instruction_size(instr) + when is_tuple(instr) and + elem(instr, 0) in [ + :load_constant, + :load_boolean, + :move, + :get_upvalue, + :get_open_upvalue, + :get_global, + :load_env, + :new_table, + :get_table, + :get_field, + :add, + :subtract, + :multiply, + :divide, + :floor_divide, + :modulo, + :power, + :negate, + :concatenate, + :bitwise_and, + :bitwise_or, + :bitwise_xor, + :shift_left, + :shift_right, + :bitwise_not, + :equal, + :less_than, + :less_equal, + :not_equal, + :not, + :length, + :closure + ] do + elem(instr, 1) + 1 + end + + defp instruction_size(_instr), do: 0 + defp emit_source_line(%{meta: %{start: %{line: line}}}, ctx) when is_integer(line) do [Instruction.source_line(line, ctx.source)] end @@ -1573,7 +1673,13 @@ defmodule Lua.Compiler.Codegen do upvalue_names: Enum.map(func_scope.upvalue_descriptors, &elem(&1, 2)), param_count: func_scope.param_count, is_vararg: func_scope.is_vararg, - max_registers: Enum.max([func_scope.max_register, body_ctx.next_reg, Map.get(body_ctx, :peak_reg, 0)]), + max_registers: + Enum.max([ + func_scope.max_register, + body_ctx.next_reg, + Map.get(body_ctx, :peak_reg, 0), + instruction_peak(body_instructions) + ]), source: ctx.source, lines: lines } diff --git a/lib/lua/vm/executor.ex b/lib/lua/vm/executor.ex index cdf72e12..27734c77 100644 --- a/lib/lua/vm/executor.ex +++ b/lib/lua/vm/executor.ex @@ -132,7 +132,7 @@ defmodule Lua.VM.Executor do @spec call_function(term(), list(), State.t()) :: {list(), State.t()} def call_function({:lua_closure, callee_proto, callee_upvalues}, args, state) do callee_regs = - Tuple.duplicate(nil, max(callee_proto.max_registers, callee_proto.param_count) + 16) + Tuple.duplicate(nil, max(callee_proto.max_registers, callee_proto.param_count)) callee_regs = args @@ -1111,8 +1111,13 @@ defmodule Lua.VM.Executor do {:lua_closure, callee_proto, callee_upvalues} -> param_count = callee_proto.param_count + # No +N buffer: codegen reports the honest peak register, and the + # only opcodes that can write past it (multi-return result write-back + # in `do_frame_return`/`continue_after_call`, unbounded `:vararg`) + # grow the tuple lazily via `ensure_regs_capacity/2`. This mirrors + # the dispatcher's `init_regs/2`, which has always run with no buffer. callee_regs = - Tuple.duplicate(nil, max(callee_proto.max_registers, param_count) + 16) + Tuple.duplicate(nil, max(callee_proto.max_registers, param_count)) # Copy directly from caller regs[base+1..] into callee regs[0..param_count-1]. # Fast path for the common 0/1/2-arg cases avoids the loop overhead. @@ -1247,12 +1252,19 @@ defmodule Lua.VM.Executor do {regs, state} = if count == 0 do + # Unbounded vararg expansion: `length(varargs)` is runtime-dynamic and + # can exceed the statically reserved register window, so grow lazily + # before writing — the same contract the dispatcher's `write_varargs/4` + # honors now that no fixed register buffer absorbs the overflow. + n = length(varargs) + regs = ensure_regs_capacity(regs, base + n) + regs = Enum.reduce(Enum.with_index(varargs), regs, fn {val, i}, r -> put_elem(r, base + i, val) end) - {regs, %{state | multi_return_count: length(varargs)}} + {regs, %{state | multi_return_count: n}} else regs = Enum.reduce(0..(count - 1), regs, fn i, r -> @@ -2213,7 +2225,7 @@ defmodule Lua.VM.Executor do defp call_value({:lua_closure, callee_proto, callee_upvalues}, args, _proto, state, _line) do callee_regs = - Tuple.duplicate(nil, max(callee_proto.max_registers, callee_proto.param_count) + 16) + Tuple.duplicate(nil, max(callee_proto.max_registers, callee_proto.param_count)) callee_regs = args From 663b5eabfcdae2ee65c2292d6dc80e5ca64fb38d Mon Sep 17 00:00:00 2001 From: Dave Lucia Date: Tue, 2 Jun 2026 16:39:30 -0700 Subject: [PATCH 2/4] perf(vm): count > and >= destinations in max_registers backstop MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit instruction_size/1 is the sound high-water backstop for max_registers now that the +16 register buffer is gone on the interpreter :call path. It already routed :less_than/:less_equal/:equal/:not_equal through the allowlist, but omitted their mirrors :greater_than/:greater_equal, which the executor writes (put_elem at the comparison dest). The omission fell through to the catch-all clause and reported 0 slots, so a > or >= landing on a function's high-water register could under-count max_registers by one and crash with :badarg on the buffer-free path. Add :greater_than/:greater_equal to the allowlist, and expose instruction_peak/1 (@doc false) so the backstop's completeness is pinned directly — peak_reg incidentally covers these dests in organic programs, so the corpus walker alone cannot catch the gap. Closes #312 --- lib/lua/compiler/codegen.ex | 9 ++++++- .../compiler/max_registers_invariant_test.exs | 25 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/lua/compiler/codegen.ex b/lib/lua/compiler/codegen.ex index 28af4af6..53b87a7b 100644 --- a/lib/lua/compiler/codegen.ex +++ b/lib/lua/compiler/codegen.ex @@ -107,7 +107,12 @@ defmodule Lua.Compiler.Codegen do # — i.e. the highest fixed destination index plus one. Dynamic-range # writers (`:call`, `:vararg`) are grown lazily at runtime via # `ensure_regs_capacity/2`, so only their fixed `base` contributes here. - defp instruction_peak(instructions) do + # + # Exposed (`@doc false`) so the backstop's completeness can be pinned + # directly: every statically-fixed destination opcode codegen emits must + # be counted here, independent of whether `peak_reg` happens to cover it. + @doc false + def instruction_peak(instructions) do Enum.reduce(instructions, 0, fn instr, acc -> max(acc, instruction_size(instr)) end) @@ -177,6 +182,8 @@ defmodule Lua.Compiler.Codegen do :equal, :less_than, :less_equal, + :greater_than, + :greater_equal, :not_equal, :not, :length, diff --git a/test/lua/compiler/max_registers_invariant_test.exs b/test/lua/compiler/max_registers_invariant_test.exs index 796becf0..112e15d5 100644 --- a/test/lua/compiler/max_registers_invariant_test.exs +++ b/test/lua/compiler/max_registers_invariant_test.exs @@ -330,4 +330,29 @@ defmodule Lua.Compiler.MaxRegistersInvariantTest do end end end + + describe "instruction_peak backstop counts every comparison destination" do + # `peak_reg`/`next_reg` tracking happens to cover comparison results in + # the organically generated shapes above, so the corpus walker cannot + # catch a comparison opcode that the backstop forgets to count. These + # pin the backstop directly: it must report the destination register of + # every comparison (`<`/`<=`/`>`/`>=`/`==`/`~=`) the executor writes, + # because the dispatcher and interpreter `:call` path size the register + # tuple to exactly `max_registers` with no buffer to absorb a miss. + comparisons = [ + {:less_than, {:less_than, 7, 1, 2}}, + {:less_equal, {:less_equal, 7, 1, 2}}, + {:greater_than, {:greater_than, 7, 1, 2}}, + {:greater_equal, {:greater_equal, 7, 1, 2}}, + {:equal, {:equal, 7, 1, 2}}, + {:not_equal, {:not_equal, 7, 1, 2}} + ] + + for {label, instr} <- comparisons do + test "#{label} destination contributes its slot" do + # Destination register 7 must reserve 8 slots (indices 0..7). + assert Compiler.Codegen.instruction_peak([unquote(Macro.escape(instr))]) == 8 + end + end + end end From 62f8ab39e5d26d0feabf234d6ed8dd2968a3a301 Mon Sep 17 00:00:00 2001 From: Dave Lucia Date: Sun, 14 Jun 2026 05:15:41 -0700 Subject: [PATCH 3/4] perf(vm): unify register sizing on honest max_registers Folds the dispatcher-side register sizing (#359) into the interpreter-side honest-max_registers fix (#312). With codegen's instruction_peak/1 backstop making max_registers a true bound on every statically-fixed destination, the separate per-prototype reg_file_size walker #359 added to work around the codegen undercount is redundant. Remove it: drop the reg_file_size field and the max_register_used/ register_positions walker from Lua.Compiler.Bytecode, and size both engines to max(max_registers, param_count) with no slack. Runtime-dynamic writes (vararg spread, multi-return) still grow on demand (grow_regs/2, ensure_regs_capacity/2). The max_registers invariant test keeps its independent bytecode-level walker as a cross-check against instruction_peak/1 (which walks the pre-encoding instruction stream) and now asserts against max_registers. One register-sizing source of truth across both engines. --- CHANGELOG.md | 30 +-- lib/lua/compiler/bytecode.ex | 171 +----------------- lib/lua/compiler/prototype.ex | 23 +-- lib/lua/vm/dispatcher.ex | 16 +- .../compiler/max_registers_invariant_test.exs | 40 ++-- test/lua/vm/dispatcher_test.exs | 6 +- 6 files changed, 58 insertions(+), 228 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d6ca18e7..210ef39c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,19 +9,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Performance -- **Deep recursion regression recovered (#324).** Call-dense, work-light - workloads (naive `fib(30)` most visibly) were ~25%+ slower than rc.0. - The rc.1/rc.2 changelog attributed this to the call-depth bookkeeping - added in #283, but bisection showed that bookkeeping is now negligible; - the dominant regressor was the blanket `+16` register-slack buffer #347 - reserved on every prototype's register tuple, paid on every call frame - by the dispatcher's `init_callee_regs/4`. The dispatcher now sizes each - register file to an exact `reg_file_size` derived from the emitted - bytecode (authoritative even where codegen's `max_registers` undercounts - the peak); runtime-dynamic expansion still grows the tuple on demand. - `fib(30)` improves from ~1.32× slower than Luerl to ~1.11× (Apple M-series, - drift-controlled), with no other workload slower. Fixes the latent - register undercount the slack had been masking. +- **Register tuples are sized to an honest peak, with no slack buffer, on + both VM engines (#312, #324).** Both the interpreter (`call_function/3`, + the `:call` opcode, `call_value/5`) and the dispatcher + (`init_callee_regs/4`) used to over-allocate every call frame's register + tuple — the interpreter with a `+16` buffer, the dispatcher with the `+16` + slack #347 added. On call-dense, work-light code (naive `fib(30)` most + visibly, ~25%+ slower than rc.0) that per-frame over-allocation dominated. + Both buffers existed to mask a latent codegen bug: `max_registers` could + undercount the true register peak for some deeply-nested expression + shapes. Codegen's new `instruction_peak/1` backstop makes `max_registers` + honest (it counts every statically-fixed destination the emitted stream + writes), so both engines now size to exactly `max(max_registers, + param_count)` and grow on demand only for runtime-dynamic writes (vararg + spread, multi-return distribution). The interpreter path is ~26% faster + on `fib(25)` (closing the dispatcher–interpreter gap to ~1.02×), and + `fib(30)` on the dispatcher improves from ~1.32× slower than Luerl to + ~1.11× (Apple M-series, drift-controlled), with no other workload slower. ### Fixed diff --git a/lib/lua/compiler/bytecode.ex b/lib/lua/compiler/bytecode.ex index 98c7c592..403bb226 100644 --- a/lib/lua/compiler/bytecode.ex +++ b/lib/lua/compiler/bytecode.ex @@ -124,182 +124,13 @@ defmodule Lua.Compiler.Bytecode do case encode_list(proto.instructions, [], 0) do {:ok, encoded} -> - bytecode = List.to_tuple(encoded) - - %{ - proto_with_children - | bytecode: bytecode, - reg_file_size: reg_file_size(bytecode, proto.param_count) - } + %{proto_with_children | bytecode: List.to_tuple(encoded)} :fallback -> proto_with_children end end - # The exact register-file size the dispatcher must allocate to run - # `bytecode`: one past the highest register index any encoded op reads or - # writes, floored at `param_count` so the argument-copy range always fits. - # - # This is derived from the emitted bytecode rather than trusting - # `proto.max_registers`, which codegen can undercount for some expression - # shapes (it lost the peak of a few transient-register chains). The - # dispatcher used to paper over that with a blanket `+16` slack on every - # register tuple — costly on call-dense code that allocates a fresh tuple - # per frame (issue #324). Sizing exactly removes that per-call overhead - # while staying correct: runtime-dynamic expansion (vararg spread, - # multi-return results) is not counted here because the dispatcher grows - # the tuple on demand at those sites (`grow_regs/2`). - defp reg_file_size(bytecode, param_count) do - max(max_register_used(bytecode) + 1, param_count) - end - - # Highest register index referenced anywhere in `bytecode`, or -1 if none. - # Recurses into the nested instruction tuples carried by branch/loop - # opcodes so the bound holds at every level. - defp max_register_used(bytecode) when is_tuple(bytecode) do - bytecode - |> Tuple.to_list() - |> Enum.reduce(-1, fn instr, acc -> max(acc, max_in_instr(instr)) end) - end - - defp max_in_instr(instr) do - case register_positions(:erlang.element(1, instr)) do - :load_nil -> - # {tag, dest, count}: writes dest..dest+count-1. - :erlang.element(2, instr) + :erlang.element(3, instr) - 1 - - :vararg -> - # {tag, base, count}: writes base..base+count-1 when count>0; the - # count==0 spread grows on demand in the dispatcher, so bound by base. - base = :erlang.element(2, instr) - count = :erlang.element(3, instr) - if count == 0, do: base, else: base + count - 1 - - :return_multi -> - # {tag, base, count}: reads base..base+count-1. - :erlang.element(2, instr) + :erlang.element(3, instr) - 1 - - :numeric_for -> - # {tag, base, loop_var, body}: reads base..base+2, writes loop_var. - base = :erlang.element(2, instr) - Enum.max([base + 2, :erlang.element(3, instr), max_register_used(:erlang.element(4, instr))]) - - :while_loop -> - # {tag, test_reg, cond_body, loop_body}. - Enum.max([ - :erlang.element(2, instr), - max_register_used(:erlang.element(3, instr)), - max_register_used(:erlang.element(4, instr)) - ]) - - :repeat_loop -> - # {tag, test_reg, loop_body, cond_body}. - Enum.max([ - :erlang.element(2, instr), - max_register_used(:erlang.element(3, instr)), - max_register_used(:erlang.element(4, instr)) - ]) - - :generic_for -> - # {tag, base, var_regs_tuple, body, line}. - base = :erlang.element(2, instr) - var_max = Enum.reduce(Tuple.to_list(:erlang.element(3, instr)), -1, &max/2) - Enum.max([base + 2, var_max, max_register_used(:erlang.element(4, instr))]) - - :test -> - # {tag, reg, then_body, else_body}. - Enum.max([ - :erlang.element(2, instr), - max_register_used(:erlang.element(3, instr)), - max_register_used(:erlang.element(4, instr)) - ]) - - :self -> - # {tag, base, obj_reg, method, hint}: reads obj_reg, writes the - # resolved method into base and the receiver into base+1 (base+1 - # dominates base). The base+1 write is not a syntactic operand, so it - # must be added explicitly — a zero-arg `obj:m()` call has no later op - # to cover it. - max(:erlang.element(2, instr) + 1, :erlang.element(3, instr)) - - positions when is_list(positions) -> - Enum.reduce(positions, -1, fn pos, acc -> max(acc, :erlang.element(pos + 1, instr)) end) - end - end - - # Register operand positions per opcode, where position 0 is the opcode - # tag. Reads and writes fold together — any out-of-bounds index would - # crash the dispatcher's `:erlang.element/2` or `:erlang.setelement/3`. - # Opcodes whose register footprint needs structural handling (ranges, - # nested bodies) return an atom dispatched above. Mirror this when adding - # an encodable opcode; `max_registers_invariant_test.exs` pins the - # contract across the compilable surface. - defp register_positions(op) do - case op do - @op_load_constant -> [1] - @op_load_boolean -> [1] - @op_load_nil -> :load_nil - @op_move -> [1, 2] - @op_load_env -> [1] - @op_get_upvalue -> [1] - @op_get_global -> [1] - @op_get_field -> [1, 2] - @op_add -> [1, 2, 3] - @op_subtract -> [1, 2, 3] - @op_multiply -> [1, 2, 3] - @op_divide -> [1, 2, 3] - @op_floor_divide -> [1, 2, 3] - @op_modulo -> [1, 2, 3] - @op_power -> [1, 2, 3] - @op_negate -> [1, 2] - @op_bitwise_and -> [1, 2, 3] - @op_bitwise_or -> [1, 2, 3] - @op_bitwise_xor -> [1, 2, 3] - @op_shift_left -> [1, 2, 3] - @op_shift_right -> [1, 2, 3] - @op_bitwise_not -> [1, 2] - @op_less_than -> [1, 2, 3] - @op_less_equal -> [1, 2, 3] - @op_greater_than -> [1, 2, 3] - @op_greater_equal -> [1, 2, 3] - @op_equal -> [1, 2, 3] - @op_not_equal -> [1, 2, 3] - @op_not -> [1, 2] - @op_test -> :test - @op_call_zero -> [1] - @op_call_one -> [1] - @op_call_multi -> [1] - @op_return_one -> [1] - @op_return_zero -> [] - @op_return_collect -> [1] - @op_return_multi -> :return_multi - @op_return_proto_varargs -> [] - @op_new_table -> [1] - @op_get_table -> [1, 2, 3] - @op_set_table -> [1, 2, 3] - @op_set_field -> [1, 3] - @op_set_list -> [1, 2] - @op_set_list_multi -> [1, 2] - @op_length -> [1, 2] - @op_numeric_for -> :numeric_for - @op_closure -> [1] - @op_set_upvalue -> [2] - @op_get_open_upvalue -> [1, 2] - @op_set_open_upvalue -> [1, 2] - @op_vararg -> :vararg - @op_self -> :self - @op_concatenate -> [1, 2, 3] - @op_break -> [] - @op_while_loop -> :while_loop - @op_repeat_loop -> :repeat_loop - @op_generic_for -> :generic_for - # Carries a register *watermark* (pre-block next_register), not an - # operand it touches; the watermark can be one past the last live slot. - @op_close_upvalues -> [] - end - end - # ── Encoding ──────────────────────────────────────────────────────────── # # Walks an instruction list, accumulating opcode tuples in reverse. On diff --git a/lib/lua/compiler/prototype.ex b/lib/lua/compiler/prototype.ex index ae0e4160..ec1ce22d 100644 --- a/lib/lua/compiler/prototype.ex +++ b/lib/lua/compiler/prototype.ex @@ -22,8 +22,7 @@ defmodule Lua.Compiler.Prototype do max_registers: non_neg_integer(), source: binary(), lines: {non_neg_integer(), non_neg_integer()}, - bytecode: tuple() | nil, - reg_file_size: non_neg_integer() + bytecode: tuple() | nil } # `bytecode` is an optional dense encoding produced by `Lua.Compiler.Bytecode`. @@ -33,16 +32,13 @@ defmodule Lua.Compiler.Prototype do # `instructions` list (used by error reporting, debugging, and any future # tooling) survives untouched. # - # `reg_file_size` is the exact number of registers the dispatcher must - # allocate to run `bytecode` — the highest register index any encoded op - # reads or writes, plus one (and never below `param_count`). It is derived - # from the emitted bytecode in `Lua.Compiler.Bytecode.compile/1`, which is - # authoritative even where codegen's `max_registers` undercounts the peak; - # runtime-dynamic expansion (vararg spread, multi-return) grows the tuple - # on demand in the dispatcher. Sizing to this exact value rather than a - # blanket slack buffer keeps call-dense code (deep recursion) off a - # per-call register over-allocation (issue #324). Zero for prototypes that - # fell back to the interpreter, which sizes its own register file. + # Both engines size their register file to `max(max_registers, param_count)` + # with no slack buffer. `max_registers` is the honest register peak — + # codegen's `instruction_peak/1` backstop counts every statically-fixed + # destination the emitted stream writes — and runtime-dynamic expansion + # (vararg spread, multi-return) grows the tuple on demand. Sizing exactly + # keeps call-dense code (deep recursion) off a per-call over-allocation + # (issues #312, #324). defstruct instructions: [], prototypes: [], upvalue_descriptors: [], @@ -53,8 +49,7 @@ defmodule Lua.Compiler.Prototype do source: <<"-no-source-">>, lines: {0, 0}, varargs: [], - bytecode: nil, - reg_file_size: 0 + bytecode: nil @doc """ Creates a new prototype with the given options. diff --git a/lib/lua/vm/dispatcher.ex b/lib/lua/vm/dispatcher.ex index 92116d53..d7c1f204 100644 --- a/lib/lua/vm/dispatcher.ex +++ b/lib/lua/vm/dispatcher.ex @@ -165,15 +165,15 @@ defmodule Lua.VM.Dispatcher do end end - # Register tuples are sized to the prototype's exact `reg_file_size` - # (computed from the emitted bytecode in `Lua.Compiler.Bytecode`). No slack - # buffer: every runtime-dynamic write — vararg spread, multi-return result - # distribution — grows the tuple on demand via `grow_regs/2`, so the static - # size only has to cover the syntactic register peak. This avoids + # Register tuples are sized to the prototype's exact register peak. No + # slack buffer: codegen reports an honest `max_registers` (its + # `instruction_peak/1` backstop counts every statically-fixed destination), + # and every runtime-dynamic write — vararg spread, multi-return result + # distribution — grows the tuple on demand via `grow_regs/2`. This avoids # over-allocating a register tuple on every call frame, which dominated # call-dense workloads like deep recursion (issue #324). defp init_regs(proto, args) do - regs = Tuple.duplicate(nil, proto.reg_file_size) + regs = Tuple.duplicate(nil, max(proto.max_registers, proto.param_count)) copy_args(regs, 0, args, proto.param_count) end @@ -1527,9 +1527,9 @@ defmodule Lua.VM.Dispatcher do defp init_callee_regs(callee_proto, src_regs, src_off, arg_count) do # Exact-sized like `init_regs/2`; runs on every compiled-closure call, - # so sizing to the callee's true register peak (no slack) is what keeps + # so sizing to the callee's honest register peak (no slack) is what keeps # deep recursion off a per-frame over-allocation (issue #324). - regs = Tuple.duplicate(nil, callee_proto.reg_file_size) + regs = Tuple.duplicate(nil, max(callee_proto.max_registers, callee_proto.param_count)) copy_n = min(arg_count, callee_proto.param_count) copy_regs(src_regs, src_off, regs, 0, copy_n) end diff --git a/test/lua/compiler/max_registers_invariant_test.exs b/test/lua/compiler/max_registers_invariant_test.exs index 112e15d5..1aa3d878 100644 --- a/test/lua/compiler/max_registers_invariant_test.exs +++ b/test/lua/compiler/max_registers_invariant_test.exs @@ -1,18 +1,19 @@ defmodule Lua.Compiler.MaxRegistersInvariantTest do @moduledoc """ - Pins the load-bearing invariant that `proto.reg_file_size` is large + Pins the load-bearing invariant that `proto.max_registers` is large enough to hold every register the encoded bytecode references. - The dispatcher sizes its register tuple to exactly `reg_file_size`, with - no slack buffer — runtime-dynamic writes (vararg spread, multi-return - result distribution) grow the tuple on demand instead. `reg_file_size` - is derived from the emitted bytecode in `Lua.Compiler.Bytecode.compile/1` - precisely so it stays correct even where codegen's own `max_registers` - undercounts the peak. Any register index the bytecode reads or writes - beyond `reg_file_size` would raise `:badarg` from `:erlang.element/2` or - `:erlang.setelement/3`, so this test re-derives the peak with an - independent walker and pins that `reg_file_size` bounds it across the - compilable surface. + Both engines size their register tuple to exactly `max(max_registers, + param_count)` with no slack buffer — runtime-dynamic writes (vararg + spread, multi-return result distribution) grow the tuple on demand + instead. `max_registers` is kept honest by codegen's `instruction_peak/1` + backstop, which counts every statically-fixed destination the emitted + stream writes. Any register index the bytecode reads or writes beyond + `max_registers` would raise `:badarg` from `:erlang.element/2` or + `:erlang.setelement/3`, so this test re-derives the peak from the emitted + bytecode with an *independent* walker (a cross-check against + `instruction_peak/1`, which walks the pre-encoding instruction stream) + and pins that `max_registers` bounds it across the compilable surface. The walker recurses into nested branch bodies (`:test`) and nested prototypes so the bound is checked at every level. @@ -201,12 +202,12 @@ defmodule Lua.Compiler.MaxRegistersInvariantTest do bytecode -> max_used = max_register_used(bytecode) - assert max_used < proto.reg_file_size, + assert max_used < max(proto.max_registers, proto.param_count), """ - #{proto.source} declares reg_file_size=#{proto.reg_file_size} + #{proto.source} declares max_registers=#{proto.max_registers} but the encoded bytecode writes/reads index #{max_used}. - The dispatcher sizes its register tuple to exactly - reg_file_size, so this would crash with :badarg. + Both engines size their register tuple to exactly + max(max_registers, param_count), so this would crash with :badarg. """ :ok @@ -299,10 +300,11 @@ defmodule Lua.Compiler.MaxRegistersInvariantTest do end """}, # Short-circuit compositions threaded through concat build deep - # transient-register chains whose peak codegen's `max_registers` - # undercounts. This shape crashed the dispatcher while it sized from - # `max_registers` and only escaped CI because the slack buffer hid it; - # it is the case that drove sizing from `reg_file_size` instead. + # transient-register chains whose peak the `peak_reg`/`next_reg` + # tracking undercounts. This shape crashed once the register-slack + # buffer was dropped and only escaped CI because the buffer hid it; it + # is a case `instruction_peak/1` must cover to keep `max_registers` + # honest. {"short-circuit chain through concat", """ function f(a, b, c) diff --git a/test/lua/vm/dispatcher_test.exs b/test/lua/vm/dispatcher_test.exs index 7c60dfd1..cf5a78ad 100644 --- a/test/lua/vm/dispatcher_test.exs +++ b/test/lua/vm/dispatcher_test.exs @@ -989,11 +989,9 @@ defmodule Lua.VM.DispatcherTest do {Bytecode.op_return_one(), 0} }, param_count: 0, + # The dispatcher sizes its register tuple to max(max_registers, + # param_count); register 0 (the get_upvalue dest) needs one slot. max_registers: 1, - # Hand-forged bytecode bypasses Bytecode.compile/1, so set the - # register-file size the dispatcher allocates from (one slot for - # register 0) explicitly. - reg_file_size: 1, source: "test-synthetic" } From 2d1eb3f098b2bee42d15048cf57638ac5a7b01ee Mon Sep 17 00:00:00 2001 From: Dave Lucia Date: Sun, 14 Jun 2026 05:52:50 -0700 Subject: [PATCH 4/4] perf(vm): derive max_registers backstop from one rule; drop main-chunk buffer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Addresses review feedback on the register-sizing path. instruction_size/1 enumerated ~34 register-writing opcodes in an allowlist with a silent `do: 0` catch-all. The list duplicated opcode knowledge that already lives in the Instruction constructors, the dispatcher, the bytecode encoder, and the invariant test, with nothing keeping them in sync — a new register-writing opcode left off the list fell through to zero, undercounted max_registers with no slack buffer to absorb it, and crashed at runtime (the #312 failure class; how greater_than/greater_equal slipped through earlier). Replace the allowlist with the rule it was spelling out: the destination of every ordinary value-producing opcode is operand 1. The walker now enumerates only the genuine exceptions — opcodes that write a range or a fixed offset off a base, recurse into nested bodies, or write no register (stores, returns, breaks, gotos, labels, source lines) — and defaults everything else to operand 1. A new load/arithmetic/comparison opcode is sized correctly with no change to the walker, and the default can only ever over-count by a slot (benign slack), never undercount into the #312 crash. A new guard test drives a representative instruction for every emitted opcode through the walker and pins its slot count. Lua.VM.execute sized the main chunk's register file to max_registers + 16 with a comment that contradicted the no-slack-buffer thesis. Drop it: size to max(max_registers, param_count) like every other call frame, so the backstop is exercised on the main-chunk path too. Runtime-dynamic writes still grow the tuple lazily. Also removes the dead Instruction.scope/2 constructor and its instruction_size clause (emitted nowhere, no engine handler). --- lib/lua/compiler/codegen.ex | 75 +++++------- lib/lua/compiler/instruction.ex | 1 - lib/lua/vm.ex | 10 +- test/lua/compiler/instruction_size_test.exs | 127 ++++++++++++++++++++ 4 files changed, 163 insertions(+), 50 deletions(-) create mode 100644 test/lua/compiler/instruction_size_test.exs diff --git a/lib/lua/compiler/codegen.ex b/lib/lua/compiler/codegen.ex index 53b87a7b..a0a1b2fd 100644 --- a/lib/lua/compiler/codegen.ex +++ b/lib/lua/compiler/codegen.ex @@ -144,51 +144,36 @@ defmodule Lua.Compiler.Codegen do defp instruction_size({:test_and, dest, _src, body}), do: max(dest + 1, instruction_peak(body)) defp instruction_size({:test_or, dest, _src, body}), do: max(dest + 1, instruction_peak(body)) - defp instruction_size({:scope, _count, body}), do: instruction_peak(body) - - # Instructions whose first integer operand is the sole written register: - # the destination. Reads never establish a high-water mark, so they are - # ignored. Anything else (set_table/set_field/set_global/set_upvalue/… - # writes to a table or upvalue, not a fresh register; :return/:break/…) - # contributes nothing. - defp instruction_size(instr) - when is_tuple(instr) and - elem(instr, 0) in [ - :load_constant, - :load_boolean, - :move, - :get_upvalue, - :get_open_upvalue, - :get_global, - :load_env, - :new_table, - :get_table, - :get_field, - :add, - :subtract, - :multiply, - :divide, - :floor_divide, - :modulo, - :power, - :negate, - :concatenate, - :bitwise_and, - :bitwise_or, - :bitwise_xor, - :shift_left, - :shift_right, - :bitwise_not, - :equal, - :less_than, - :less_equal, - :greater_than, - :greater_equal, - :not_equal, - :not, - :length, - :closure - ] do + + # Stores, control flow, and debug annotations establish no fresh + # destination register. Stores write through an existing table / upvalue + # (the register they read was already counted by whatever wrote it); + # returns, breaks, and gotos write no register; labels and source lines + # are annotations. Reads of an existing register never raise the peak, so + # all of these contribute nothing. + defp instruction_size(:break), do: 0 + defp instruction_size({:goto, _label}), do: 0 + defp instruction_size({:return, _base, _count}), do: 0 + defp instruction_size({:return_vararg}), do: 0 + defp instruction_size({:set_table, _table, _key, _value, _hint}), do: 0 + defp instruction_size({:set_field, _table, _name, _value, _hint}), do: 0 + defp instruction_size({:set_upvalue, _index, _source}), do: 0 + defp instruction_size({:set_open_upvalue, _reg, _source}), do: 0 + + # Everything that reaches here is an ordinary value-producing opcode — + # `{tag, dest, ...}` whose destination is operand 1. That is the rule for + # every load / move / arithmetic / comparison / bitwise / table-read / + # closure opcode, so it is the default rather than an enumerated allowlist: + # a new opcode of that shape needs no change here. The clauses above carve + # out the only exceptions — opcodes that write a range or a fixed offset + # off a base, recurse into nested bodies, or write no register at all. + # + # This default cannot reintroduce the #312 undercount: every value-less + # opcode whose operand 1 is a register index is listed above (a missing + # one would only ever *over*-count by a slot — benign slack, never a + # crash), and any instruction without an integer operand 1 falls through + # to zero. + defp instruction_size(instr) when is_tuple(instr) and tuple_size(instr) > 1 and is_integer(elem(instr, 1)) do elem(instr, 1) + 1 end diff --git a/lib/lua/compiler/instruction.ex b/lib/lua/compiler/instruction.ex index c1505ac2..f5a2aac9 100644 --- a/lib/lua/compiler/instruction.ex +++ b/lib/lua/compiler/instruction.ex @@ -96,7 +96,6 @@ defmodule Lua.Compiler.Instruction do def generic_for(base, var_count, body), do: {:generic_for, base, var_count, body} def break_instr, do: :break - def scope(register_count, body), do: {:scope, register_count, body} # Functions def closure(dest, proto_index), do: {:closure, dest, proto_index} diff --git a/lib/lua/vm.ex b/lib/lua/vm.ex index 80184d9f..e51f7840 100644 --- a/lib/lua/vm.ex +++ b/lib/lua/vm.ex @@ -16,10 +16,12 @@ defmodule Lua.VM do """ @spec execute(Prototype.t(), State.t()) :: {:ok, list(), State.t()} def execute(%Prototype{} = proto, state \\ State.new()) do - # Create register file sized to the prototype's needs. - # The +16 buffer covers multi-return expansion slots that the codegen doesn't - # always track in max_registers (call results can land beyond the stated max). - registers = Tuple.duplicate(nil, proto.max_registers + 16) + # Size the register file to the prototype's honest register peak, with no + # slack buffer — same contract as every other call frame. `max_registers` + # covers every statically-fixed destination (codegen's `instruction_peak/1` + # backstop); runtime-dynamic writes (multi-return result distribution, + # unbounded varargs) grow the tuple lazily via `ensure_regs_capacity/2`. + registers = Tuple.duplicate(nil, max(proto.max_registers, proto.param_count)) # Execute the prototype instructions {results, _final_regs, final_state} = diff --git a/test/lua/compiler/instruction_size_test.exs b/test/lua/compiler/instruction_size_test.exs new file mode 100644 index 00000000..67e7438c --- /dev/null +++ b/test/lua/compiler/instruction_size_test.exs @@ -0,0 +1,127 @@ +defmodule Lua.Compiler.InstructionSizeTest do + @moduledoc """ + Pins `Codegen.instruction_peak/1`'s per-instruction classification. + + `instruction_peak/1` makes `max_registers` honest by reporting the highest + statically-fixed destination register every emitted instruction writes. + Both VM engines size their register tuple to exactly `max(max_registers, + param_count)` with no slack buffer, so an opcode the walker fails to + classify silently undercounts `max_registers` and crashes at runtime — + the #312 failure class. + + The walker enumerates only the exceptions — opcodes that write a range, a + fixed offset off a base, recurse into nested bodies, or write no register + — and defaults everything else to "the destination is operand 1", the rule + every ordinary value-producing opcode obeys. So a new load / arithmetic / + comparison opcode is sized correctly with no change to the walker, and the + default can only ever *over*-count (benign slack), never undercount into + the #312 crash. + + This test drives a representative instruction for every opcode codegen + emits through the walker and pins the slot count it must report. A + destination register of 7 reserves 8 slots (indices 0..7); stores, control + flow, and debug annotations write no register and report 0. + """ + + use ExUnit.Case, async: true + + alias Lua.Compiler.Codegen + + # {instruction, expected slot count}. Covers every tag codegen emits into a + # prototype's instruction stream. + # + # Register-writing opcodes: destination is operand 1, so dest 7 → 8 slots. + @writers [ + {{:load_constant, 7, {:constant, 1}}, 8}, + {{:load_boolean, 7, true}, 8}, + {{:move, 7, 1}, 8}, + {{:get_upvalue, 7, 0}, 8}, + {{:get_open_upvalue, 7, 1}, 8}, + {{:get_global, 7, "x"}, 8}, + {{:load_env, 7}, 8}, + {{:new_table, 7, 0, 0}, 8}, + {{:get_table, 7, 1, 2, nil}, 8}, + {{:get_field, 7, 1, "x", nil}, 8}, + {{:add, 7, 1, 2, nil, nil}, 8}, + {{:subtract, 7, 1, 2, nil, nil}, 8}, + {{:multiply, 7, 1, 2, nil, nil}, 8}, + {{:divide, 7, 1, 2, nil, nil}, 8}, + {{:floor_divide, 7, 1, 2, nil, nil}, 8}, + {{:modulo, 7, 1, 2, nil, nil}, 8}, + {{:power, 7, 1, 2, nil, nil}, 8}, + {{:negate, 7, 1, nil}, 8}, + {{:concatenate, 7, 1, 2}, 8}, + {{:bitwise_and, 7, 1, 2, nil, nil}, 8}, + {{:bitwise_or, 7, 1, 2, nil, nil}, 8}, + {{:bitwise_xor, 7, 1, 2, nil, nil}, 8}, + {{:shift_left, 7, 1, 2, nil, nil}, 8}, + {{:shift_right, 7, 1, 2, nil, nil}, 8}, + {{:bitwise_not, 7, 1, nil}, 8}, + {{:equal, 7, 1, 2}, 8}, + {{:less_than, 7, 1, 2}, 8}, + {{:less_equal, 7, 1, 2}, 8}, + {{:greater_than, 7, 1, 2}, 8}, + {{:greater_equal, 7, 1, 2}, 8}, + {{:not_equal, 7, 1, 2}, 8}, + {{:not, 7, 1}, 8}, + {{:length, 7, 1}, 8}, + {{:closure, 7, 0}, 8} + ] + + # Structural opcodes: write a range, a fixed offset off a base, or recurse + # into nested bodies. + @structural [ + {{:load_nil, 5, 3}, 8}, + {{:vararg, 5, 3}, 8}, + {{:vararg, 5, 0}, 6}, + {{:self, 5, 1, "m", nil}, 7}, + {{:call, 5, 2, 1, nil}, 6}, + {{:set_list, 1, 5, 3, 0}, 8}, + {{:set_list, 1, 5, {:multi, 2}, 0}, 7}, + {{:numeric_for, 4, 7, []}, 7}, + {{:generic_for, 4, {7, 8}, []}, 7}, + {{:while_loop, [{:less_than, 7, 1, 2}], 5, []}, 8}, + {{:repeat_loop, [{:move, 7, 1}], [], 0}, 8}, + {{:test, 1, [{:move, 7, 1}], []}, 8}, + {{:test_and, 7, 1, []}, 8}, + {{:test_or, 7, 1, []}, 8} + ] + + # Stores, control flow, and debug annotations write no fresh register. + @non_writers [ + {:break, 0}, + {{:goto, :l1}, 0}, + {{:return, 5, 1}, 0}, + {{:return_vararg}, 0}, + {{:set_table, 1, 2, 3, nil}, 0}, + {{:set_field, 1, "x", 2, nil}, 0}, + {{:set_upvalue, 0, 3}, 0}, + {{:set_open_upvalue, 2, 3}, 0}, + {{:source_line, 10, "f.lua"}, 0}, + {{:close_upvalues, 3}, 0}, + {{:label, :l1}, 0} + ] + + describe "instruction_peak classifies every emitted opcode" do + for {instr, expected} <- @writers ++ @structural ++ @non_writers do + tag = if is_tuple(instr), do: elem(instr, 0), else: instr + + test "#{inspect(tag)} reserves #{expected} slot(s)" do + assert Codegen.instruction_peak([unquote(Macro.escape(instr))]) == unquote(expected) + end + end + end + + describe "the default sizes new opcodes without maintenance" do + test "an unlisted value-producing opcode is sized from its operand-1 destination" do + # A future opcode of the ordinary `{tag, dest, ...}` shape needs no + # entry in the walker: the default reserves dest + 1 slots. + assert Codegen.instruction_peak([{:some_future_opcode, 7, 1}]) == 8 + end + + test "an instruction with no integer operand-1 destination contributes nothing" do + assert Codegen.instruction_peak([{:some_future_marker, :not_a_register}]) == 0 + assert Codegen.instruction_peak([:some_future_atom]) == 0 + end + end +end