Repository navigation
fix(jit): test ref.is_null on the ref payload, not the whole boxed word - #230
Merged
Merged
Conversation
A heap slot's null is not always the BoxedNull bit pattern. struct.new_default zero-fills, so an unset KindRef field reads back as Boxed(0), whose Kind() is KindF64. The threaded REF_IS_NULL lowering is boxed.Ref() == 0 -- a test on the ref payload -- and reports both patterns as null. The ARM64 lowering compared the whole 64-bit word against BoxedNull, so it reported a default-initialized ref field as non-null. The effect is a divergence, not just a missed guard: a slot declared with a concrete struct type reaches a native plan, ref.is_null falls through its br_if, and the ref.cast behind that guard traps with ErrTypeMismatch, where threaded execution returns the right answer. Heap values are not meant to exclude null, so the JIT is what changes: mask the ref payload and compare it against zero, matching boxed.Ref(). Regression test builds a small binary tree whose leaves reach ref.is_null through an unset ref field and requires threaded and JIT to agree; it fails with `type mismatch: fn=2 ip=8` before this change. A runTests case covers struct.new_default -> struct.get -> ref.is_null in standalone and fused modes. This unblocks the struct half of #176: StructTreeWalk and BinaryTrees now pass in all three modes with their Node params declared as the concrete struct type. Closes #228
Contributor
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (4)
📝 WalkthroughWalkthroughThe change makes ARM64 ChangesReference null handling
Estimated code review effort: 3 (Moderate) | ~20 minutes ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This was referenced Aug 31, 2026
siyul-park
added a commit
that referenced
this pull request
Aug 31, 2026
…t slots (#231) * test(benchmarks): declare the concrete node type on tree-kernel struct slots StructTreeWalk and BinaryTrees declared every Node-holding local, return, and param as `any` (types.TypeRef), so `struct.get` never reached its fused container path. #229 had to leave them that way: declaring the concrete struct type made the ARM64 JIT fault where threaded execution was correct. #230 fixed that, so the declarations can now be what the Python sources say. `left`/`right` stay `any` because the grammar cannot name a recursive type. The `check`/`item_check` params may hold the null ref at a leaf, which is fine: heap slots do not exclude null, `struct.new_default` leaves unset ref fields null, and both tiers now agree on `ref.is_null` for them. No interpreter code changes. Interleaved A/B, -benchtime=300ms -count=6, Apple M4 Pro: Memory_BinaryTrees/threaded 1.145ms -> 1.059ms -7.55% (p=0.002) Memory_BinaryTrees/default 1.187ms -> 1.104ms -7.01% (p=0.002) Memory_BinaryTrees/jit 1.187ms -> 1.106ms -6.85% (p=0.002) Memory_StructTreeWalk/default 160.5us -> 154.6us -3.69% (p=0.002) Memory_StructTreeWalk/threaded 153.0us -> 147.5us -3.58% (p=0.002) Memory_StructTreeWalk/jit 160.2us -> 155.7us -2.81% (p=0.002) Control_Sieve and Numeric_NBody are flat in all three modes. B/op and allocs/op are identical on every row. * test(benchmarks): drop the redundant ref.cast from the tree walkers `check` and `item_check` each downcast their node param with `ref.cast 0` immediately after a `ref.is_null; br_if` guard. The cast only existed because the param was declared `any`; with the param declared as the node struct type it proves nothing the declaration does not already state, and neither Python source has a cast. It was not free. `REF_CAST` is bridged, so every node walk left native code for the interpreter and came back, and the cast also split `local.get 0` from the `i32.const N; struct.get` behind it, blocking that field read from fusing. Interleaved A/B against the previous commit, -benchtime=300ms -count=6, Apple M4 Pro: Memory_BinaryTrees/jit 1108.1us -> 752.7us -32.08% (p=0.002) Memory_BinaryTrees/default 1102.7us -> 751.8us -31.82% (p=0.002) Memory_StructTreeWalk/jit 156.0us -> 135.0us -13.43% (p=0.002) Memory_StructTreeWalk/default 154.8us -> 134.4us -13.20% (p=0.002) Memory_BinaryTrees/threaded 1055.9us -> 950.9us -9.94% (p=0.002) Memory_StructTreeWalk/threaded 147.7us -> 138.1us -6.52% (p=0.002) Control_Sieve is flat in all three modes. B/op and allocs/op are unchanged. * docs(benchmarks): re-measure the tree-kernel rows StructTreeWalk and BinaryTrees moved when their node slots were declared concretely and the redundant ref.cast was dropped. Both now run fastest under default rather than threaded, so they leave that list, and minivm threaded overtakes CPython on BinaryTrees.
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.
Closes #228.
The divergence
A heap slot's null is not always the
BoxedNullbit pattern.struct.new_defaultzero-fills(
(*types.Struct).Reset->clear(s.Data)), so an unsetKindReffield reads back asBoxed(0),whose
Kind()isKindF64— notBoxRef(0), which carries the NaN tag.The threaded lowering handles both:
REF_IS_NULLisboxed.Ref() == 0, andRef()isint(int32(v & VMask)), so it tests the ref payload. The ARM64 lowering compared the whole64-bit word against
BoxedNull, so it called a default-initialized ref field non-null.That is a wrong answer, not just a missed optimization.
ref.is_null; br_ifis exactly the guard anullable-node walker uses, so native execution fell through it into the
ref.castbehind it andtrapped:
while
threadedreturned the right answer on the same program.The fix
Heap values are not meant to exclude null, so the JIT is what changes. Mask the ref payload and
compare it against zero, matching
boxed.Ref():Same instruction count as the
LDI+CMPit replaces.maskI32is the maskrefOnlyalready usesto extract a ref address from a boxed word.
Nothing else in the backend needed changing.
refOnlygatesretainBox/releaseBoxon the kindtag, so
Boxed(0)is skipped there exactly as the interpreter'sretainBox/releaseBoxskip it.refEq/refNeare full-word compares in both tiers already, so they agree. A null container reachingstructGetfailsguardItaband side-exits, which is what threaded does too.Tests
TestInterpreter_Run/"STRUCT_NEW_DEFAULT's default ref field agrees between threaded and JIT"—builds a small binary tree whose leaves reach
ref.is_nullthrough an unset ref field, and requiresWithThreshold(-1)andWithThreshold(0)to return the same value. Verified it fails on the parentcommit with the trap above and passes here.
runTestscase forstruct.new_default->struct.get->ref.is_null, which the table runs inboth
standaloneandfusedmodes.Docs
docs/memory-model.md, under Heap index 0 is always null: a null ref is not always theBoxedNullbit pattern, null-ness is
Boxed.Ref() == 0, and every consumer must use that rather than awhole-word compare.
docs/jit-internals.md:REF_IS_NULLtests the ref payload, cross-referencing the above.Knock-on
This unblocks the struct half of #176, which #229 had to drop. With the fix in place,
StructTreeWalkandBinaryTreespass inthreaded,default, andjitwith their Node paramsdeclared as the concrete struct type. That retype is not in this PR — it belongs on #229 once this
lands.
Test plan
go test -race ./...— 19 packages greenmake check-generated— cleanmake lint— cleanmake coverage-check— 73.1% vs 72.5% baseline🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Tests
Documentation