feat(ILO-401): VM/JIT codegen for Value::Variant (sum types)#640
Merged
Conversation
…RECFLD Eliminates the ILO-E803 SumTypeNotSupported bailout that previously caused any program containing a sum-type declaration to fall back to the tree interpreter. The VM compiler now lowers variant construction and pattern-matching using standard record opcodes that Cranelift already handles. Representation: each variant is a 2-field heap record of type `__variant_<typename>` with fields ["tag" (numeric ordinal), "payload"]. - Construction: Expr::Call on a payload variant / Expr::Ref on a no-payload variant both emit emit_variant_record() → OP_LOADK tag + OP_RECNEW. - Pattern matching: Pattern::Variant extracts field 0 (OP_RECFLD), compares to the expected ordinal (OP_EQ + OP_JMPF), and optionally binds field 1. - Cranelift AOT/JIT: no changes needed — it already handles OP_RECNEW, OP_RECFLD, OP_EQ, OP_LOADK. Tests: 4 new vm::tests (zero-payload, payload, VM-vs-interpreter parity, no-E803) and 2 new compile_cranelift::tests (object codegen for both variant shapes). examples/sum-types.ilo verified under --vm and --jit. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
danieljohnmorris
added a commit
that referenced
this pull request
May 22, 2026
feat(ILO-401): VM/JIT codegen for Value::Variant (sum types)
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.
Summary
ILO-E803 SumTypeNotSupportedbailout that forced all sum-type programs to fall back to the tree interpreterOP_RECNEW,OP_RECFLD,OP_EQ,OP_LOADK)Codegen strategy
Each variant is stored as a 2-field heap record of type
__variant_<typename>:tag— numeric ordinal (0-based) of the variant within the sum typepayload— constructor argument for payload variants, orNilConstruction (
circle 5→ payload variant,point→ no-payload variant):emit_variant_record(type_id, tag_idx, payload_reg)emitsOP_LOADK(tag constant) +OP_RECNEWExpr::Call(before user-function lookup) andExpr::RefPattern matching (
?s{circle(r):...}):Pattern::VariantemitsOP_RECFLD sub_reg, 0→OP_EQ→OP_JMPF, then optionallyOP_RECFLD sub_reg, 1to bind payloadTest plan
examples/sum-types.iloproduces correct output under--vmand--jitvm::tests::sum_type_vm_zero_payload_variant— no-payload variant constructs and matchesvm::tests::sum_type_vm_payload_variant_match— payload variant carries value through matchvm::tests::sum_type_vm_matches_interpreter— VM result equals tree interpreter resultvm::tests::sum_type_vm_no_e803_bailout— compile no longer returns ILO-E803compile_cranelift::tests::codegen_sum_type_variants_emit_object— Cranelift accepts bytecodecompile_cranelift::tests::codegen_sum_type_zero_payload_variant_emit_object— zero-payload variant codegen🤖 Generated with Claude Code