vm: compile short-circuit and/or for the dispatcher - #430
Open
shinyobjectz wants to merge 1 commit into
Open
shinyobjectz wants to merge 1 commit into
shinyobjectz wants to merge 1 commit into
Conversation
A function containing `and` or `or` fell back to the instruction interpreter, because the encoder had no lowering for :test_and / :test_or. They now encode to two opcodes, @op_test_and and @op_test_or, that run the right operand as a nested body and resume through the same cont marker :test uses. Tests that used and/or to force a function onto the interpreter now strip its bytecode instead, and the fallback tests use a synthetic prototype.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A function containing
andororwas never compiled for the dispatcher. The encoder had no lowering for:test_and/:test_or, so it returned:fallbackand the whole prototype ran on the instruction interpreter. That is common in real code:x = x or default,return c and a or b, guards inifconditions.Change
@op_test_and(77) and@op_test_or(78), encode{tag, dest, source, body}. When the left operand decides the result it is copied intodest. Otherwisebody(the right operand, ending in a write todest) runs as a nested block and resumes through the samecontmarker:testuses.Tests
test/lua/vm/and_or_engines_test.exs: 14 cases run under both engines. They cover value-returning and/or;nil/false/0/""operands; nesting; right-operand side effects that must not run; and/or inif,whileandrepeatconditions; default-argument assignments to locals, fields and upvalues; multi-return truncation;breakafter an and/or in a loop; a closure built in the right operand; and an error raised in the right operand. Each case also assertsBytecode.fully_compiled?.max_instructions_test(cross-engine mutual recursion) andpeephole_test(:call_selfon the interpreter) now strip that function's bytecode instead. Thebytecode_testfallback and cascade cases use a synthetic prototype with{:set_list, _, _, 0, _}, the one shape the encoder still rejects.max_registers_invariant_testwalks the new opcodes.The full suite passes:
mix testgives 2914 passed, andmix test --include lua53 --include differential --include slowgives 2935 passed.Benchmark
Each number is the median of 15
:timer.tcruns after 3 warmup runs, in ms. The machine is an Apple M4 with OTP 29 and Elixir 1.20.3,MIX_ENV=prod. "budget" meansLua.new(max_instructions: 1_000_000_000).if(control)