Skip to content

fix(comp): a folded call evaluates an argument its chosen arm drops - #2199

Closed
YidaWeng wants to merge 1 commit into
bendlang:mainfrom
YidaWeng:fix/unfold-dropped-arg
Closed

YidaWeng wants to merge 1 commit into
bendlang:mainfrom
YidaWeng:fix/unfold-dropped-arg

Conversation

@YidaWeng

@YidaWeng YidaWeng commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2181.

emit_unfold folds a call whose constant arguments decide its matches. A live, non-constant argument becomes a probe, and the chosen arm body is wrapped in a let for it. When the arm did not use that argument, the binding was dead, the emitter dropped it, and the argument was never evaluated: the C lane printed 100 for pick(boom(k), 0n, 100n) where the interpreter and JS evaluate boom and stop at the Nat wall.

emit_unfold now refuses a fold whose body does not use one of its probes, so the call is emitted normally and evaluates its arguments like the other lanes.

What changed

  • bend2/comp.ts, emit_unfold (+7/-7 lines): the bad predicate that already rejects a fold now also reports a probe the folded body does not use. Four behavior-neutral tightenings in the same function (merged declarations, a while loop over the probe list, && in place of a ternary) keep comp.ts at 65,996 / 66,000 ttok (main is 65,995).
  • tests/reg/unfold_drop_arg.bend: the issue's file; expects bend: a Nat past the largest immediate 2^48-1 and exit 1 on all three lanes.
  • tests/reg/unfold_drop_arg_run.bend: control; the same call with Nat.sub(k, k) as the selector, so no fold happens. It already stopped on every lane before the fix, which isolates the literal selector as the trigger.

Checks run (local, macOS arm64)

  • Before the patch: bun bend2/main.ts and -o .js && bun .js print the Nat wall (exit 1); -o drop && ./drop printed 100 (exit 0). The emitted C had no boom call and only 100ull reached Nat.show.
  • After: all three lanes print bend: a Nat past the largest immediate 2^48-1 (exit 1); the emitted C now calls spin_boom before spin_pick.
  • bun gates/repo.ts -> PASS: 56 / 56.
  • Blast radius: emitted C before/after for every tests/**/*.bend, bench/runtime/*/main.bend and demos/*/main.bend that compiles: 927 identical, 14 differ, plus the 2 new tests. The 14 are tests/check/{convoy_dependent,kind_domain_self_call,kind_generic_none,kind_none_is_type,kind_self_call_eta,meet_uses_in_arms}, tests/compile/loop_peel_countdown, tests/cost/accumulator_loop, tests/io/bool_bridge, tests/proof/fold_assoc, tests/run/{bit_walking,graph_bfs,word_wrap_chains} and bench/runtime/raytrace. Each was built and run on the interpreted, JS and C lanes in both trees: byte-identical output, and their #| lines still match.
  • tests/reg/unfold_arg_type.bend (prints 0) and tests/run/nat_overflow.bend (wall, exit 1) still pass on all three lanes.

The change is in the C emitter only; the interpreter and the JS emitter do not use emit_unfold.

Rebase onto current main (0e2f38f7)

main grew from 65,981 to 65,995 ttok after this PR was opened, so the patch was 66,011 / 66,000. The tightenings above bring it back to 65,996, and the branch is rebased on 0e2f38f7. Re-checked on Linux x86_64:

  • ttok -m gpt-3.5-turbo < bend2/comp.ts -> 65996
  • bun gates/repo.ts -> PASS: 56 / 56
  • tests/reg/unfold_drop_arg.bend and tests/reg/unfold_drop_arg_run.bend: interpreted, JS and C lanes all print the Nat wall and exit 1
  • tests/reg/unfold_arg_type.bend, tests/run/{nat_overflow,bit_walking,graph_bfs}.bend, tests/compile/loop_peel_countdown.bend: expected output on the interpreted lane

Not run

  • bun gates/test.ts, gates/perf.ts, gates/ping.ts, Metal/CUDA: the cluster is unreachable from this checkout (ssh cluster refused). gates/test.ts runs on the cluster, so this needs a cluster run before it merges. No cluster qualification is claimed; the results above are local.

AI-assisted.

@YidaWeng
YidaWeng force-pushed the fix/unfold-dropped-arg branch from 9950495 to c603f49 Compare November 22, 2026 17:02
@YidaWeng
YidaWeng force-pushed the fix/unfold-dropped-arg branch from c603f49 to 425e1dd Compare November 22, 2026 17:06
@YidaWeng

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (0e2f38f7) and brought comp.ts back under the cap. main had grown to 65,995 ttok since this PR was opened, so the patch was 66,011; the behavior-neutral tightenings in emit_unfold now leave it at 65,996 / 66,000. bun gates/repo.ts is PASS: 56 / 56, and both new tests print the Nat wall with exit 1 on the interpreted, JS and C lanes.

I still cannot run the cluster gates from this checkout (ssh cluster does not resolve and BEND_HOC is unset), so bun gates/test.ts, gates/perf.ts and the Metal/CUDA lanes are unrun. Marking this ready in case a maintainer can run them; I'm happy to put it back in draft if you would rather it stay unqueued until then.

@YidaWeng
YidaWeng marked this pull request as ready for review November 22, 2026 17:07
@nicolas-abril nicolas-abril self-assigned this Nov 23, 2026
@nicolas-abril

Copy link
Copy Markdown
Collaborator

Thanks for the careful write-up and the blast-radius check. I think this moves in the wrong direction, though, so I'd rather not merge it.

The C lane's 100 is the right answer. pick(boom(k), 0n, 100n) means 100; the Nat wall is a runtime limit, not part of the program's meaning, and it only fires in a lane that does work the result doesn't need. Unused parameters are erased, and the checker assumes dead code isn't executed: only live code is promised to halt, and dead code may loop. So no lane can be required to evaluate everything it's handed, and the C emitter shouldn't refuse a valid unfold to keep a trap firing.

We've already gone this way before:

If the lanes should agree, the fix belongs in the interpreter and JS lanes, which would skip the argument instead. Whether evaluation should be strict is still open (see #775), so that's a separate decision. I'll note this on #2181 too.

@nicolas-abril

Copy link
Copy Markdown
Collaborator

Closing per the comment above: the C lane's result is the one we want, and #2181 will be fixed on the JS side instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants