Skip to content

VM: a for loop's variable takes an Array whole, as |x| does - #300

Merged
284km merged 1 commit into
mainfrom
stage10-vm-for-param
Nov 25, 2026
Merged

284km merged 1 commit into
mainfrom
stage10-vm-for-param

Conversation

@284km

@284km 284km commented Nov 25, 2026

Copy link
Copy Markdown
Owner

On the VM, a for loop's variable took the first element of an Array:

for x in [[1, 2], [3]]; p x; end   # ruby: [1, 2] and [3]; the VM: 1 and 3

Found while taking apart the benchmark furthest from the evaluator (note 558: the VM's speed, from the so_* / app_* benchmarks). so_spectralnorm was 1.13x the evaluator's instructions, and the gap was its for loop, not its calls.

Cause

A for loop's block has one parameter without has_lead, which ruby binds as |x|: an Array whole. Both compilers' export for the VM gives that parameter (lead_num) and did not say the block is a for loop's. vm_params read one required parameter that is not ambiguous_param0 as |x,| and added the *, that spreads an Array. Each turn's binding also went past the one-store path for a single parameter (blk_simple1).

Change

Numbers

VM instructions, the median of three runs alternating main and this branch:

program main here the evaluator
for j in 0..249999; end 4.23 G 3.37 G 2.76 G
so_spectralnorm 614.9 G 577.9 G 545.2 G
so_mandelbrot 561.2 G 529.9 G 524.7 G
so_fasta, so_nbody, so_binary_trees unchanged

Checked

  • Corpus 827 (new): fails on main's VM, matches ruby on both executions here.
  • vm_corpus.sh: 956 programs, 956 on the VM, 0 FAIL. run_corpus.sh: 956/956.
  • vm_check.sh: 246/246 (one snippet added).
  • tools/iseq/native/check.sh corpus/*.rb: 956 the same.

Record: loop/2026-11-26-stage10-vm-for-param.md.

🤖 Generated with Claude Code

Both compilers' export for the VM said a for loop's block's one parameter
(lead_num) and not that it was a for loop's, and vm_params read one required
parameter that is not ambiguous_param0 as |x,|, adding the *, that spreads
an Array: `for x in [[1, 2]]` bound 1. The export now says "for", and
vm_params binds it as |x| -- which also takes the one-store path for a
single parameter. so_spectralnorm on the VM: 614.9 -> 577.9 G instructions.

vm_check.sh runs the reference as $REF_RUBY_BIN, not rbenv's shim (#298's
review).

@284km 284km left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed f1a355bb617de7c62ef94300ed1801b79ad1ef68 against current main 1a3380b0e5738ee3ed4436c1eb94843e94ac9676. No new actionable Critical / Major / Minor findings.

The two compiler exports agree on the VM-only for marker, and the loader suppresses the trailing-comma destructuring only where appropriate. Ordinary |x,| and multi-target/rest loop bindings remain intact. The generated library matches regeneration, and public to_a/disassembly remains compatible.

Independent validation on Darwin arm64, using fresh ordinary O2 builds of both revisions, pinned Mere 9dec2ff687a08433247dbdb7729bc19c8522e538, pinned mgz 661a01d8bb68f5acd68e3192bd47f081d179c436, and reference Ruby 4.0.10:

  • VM corpus: 956/956 on the VM, zero fallback or failure.
  • Strict ISeq snippets: 246/246.
  • Public to_a/disassembly comparison with Ruby: 956/956; native/Ruby VM-export comparison: 956/956.
  • 36 focused corpus files across evaluator/VM and hash index on/off with MERE_RUBY_GC_TIGHT=1: head 144/144, main 142/144. The two main failures are the new corpus 827 on the VM; no passing baseline combination is lost.
  • An additional probe covering zero/multiple/keyword yields, nested loops, nonlocal targets, trailing commas, rest targets and a raising to_ary matches Ruby in all six head VM compiler/hash combinations; all six base combinations reproduce the defect. A separate evaluator discrepancy for for *a with multiple yields is byte-identical on main/head and is pre-existing.
  • All seven source/build gates pass, including GC roots and bracket depth (155 under the unchanged 1024 budget). A harness-only ENV control reproduces the rbenv-shim mismatch on main and passes with this reference-binary change.

The clean prospective integration has exactly the tested head tree. Performance counts were not remeasured; full upstream-file/platform/resource acceptance is not claimed. Hosted build-linux is still executing, with seven macOS/reference jobs queued. Current main's core CI run 45663174315 is pending and exposes zero jobs; its separate load-path workflow has Linux success and an unassigned cancelled macOS job, not a reproduced test failure. The recorded hold_all_merges / new_failure_review_required remain active. No merge: this is not a queue-only situation with equivalent complete coverage.

@284km 284km left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed f1a355b. No Critical / Major / Minor findings. A for loop's block has one parameter and no has_lead; both compilers' VM export now say "for", and vm_params binds it as |x| instead of |x,|. On main's VM, for x in [[1, 2], [3]] gave 1 and 3; ruby gives [1, 2] and [3]. I ran a probe on the author's build, on the evaluator and MERE_RUBY_VM=1 against ruby 4.0.10: Arrays, nil and [] elements; an each yielding 1, 2, [3, 4] and nothing; a Hash; for a, b; a Range; the loop variable after the loop; an empty loop. All identical (main's VM differs). vm_check.sh now uses $REF_RUBY_BIN (#298's Suggestion). The description has vm_corpus 956/956 on the VM, run_corpus 956/956, vm_check 246/246, the native compiler check 956 the same, and spectralnorm 614.9 → 577.9 G on the VM. git merge-tree against main: clean. The bot found nothing actionable. Merging.

@284km
284km merged commit e71406d into main Nov 25, 2026
30 of 36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant