Skip to content

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

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

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

Conversation

@YidaWeng

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 (+2/-1 lines): the bad predicate that already rejects a fold now also reports a probe the folded body does not use. comp.ts measures 65,997 / 66,000 ttok (main is 65,981), so no cap change.
  • 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.

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 is a draft until it can run. No cluster qualification is claimed; the results above are local.

AI-assisted.

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

Labels

None yet

Projects

None yet

1 participant