Skip to content

refactor(comp): the intrinsic table is keyed by Base's def name, intr_of has no flag, a spare carries its size class, and a fold walks its body once - #2129

Merged
nicolas-abril merged 3 commits into
mainfrom
ai/comp-simplify
Nov 12, 2026

Conversation

@nicolas-abril

@nicolas-abril nicolas-abril commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

A simplification pass over bend2/comp.ts: 65,952 → 65,913 ttok (−39; the cap is 66k), in that file only.

What changes

  • The intrinsic table is keyed by Base's def name. OPERATIONS had keys like u32_add, reached by lowercasing U32.add and replacing dots, with a cache (FL.ops) over that translation. The keys are now the Bend names ("U32.add", "Array.atomic.cas", "Map.msb.u"), so intr_of is a direct lookup and op_name and its cache are gone. No other cache is touched.
  • intr_of has no js flag. An op is an intrinsic in a lane exactly when it has that lane's text. Before, a JS-only op (the String ones) counted as an ordinary def in the JS lane unless the caller passed true.
  • A spare carries its size class, not a word count. The emitted free names the class as a number (heap_free(e, 2, sp)) instead of calling cls_fit(3) in C, as every emitted alloc already did.
  • emit_fold walks an unfolded body once. It charged fuel in one walk and folded the inner calls in a second; one walk does both. When a body runs the fuel out, the inner calls visited before that point now stay folded, where the two walks discarded them; the folds are valid either way, and no program in the corpus changed.
  • The compiler's cls_fit gives class 0 for no words, as the runtime's does (it gave 32). No caller reaches it with zero today; lay_arr drops its own guard for that case.

Effect on emitted programs

Compared on all 1,944 test, demo and bench programs, -o f.c and -o f.js, against main (8ec4c88):

  • C: every .c is identical once cls_fit(n) is read as its number. Every compile error is identical.
  • JS: 46 of the 990 programs that compile to JS differ, all from the intr_of change:
    • Base's bodies of the String ops, and helpers reached only through them (String.order, Cmp.is_eq, String.starts_with), are no longer emitted, or are emitted at a different position.
    • Some closures take their captured variables in a different order, consistently at the definition and the call.

One edge not tested directly: in the JS lane, a U32 match whose arm goes through a String op no longer constant-folds through Base's body when building a lookup table. No program in the corpus changed its tables.

Verification

  • Machine code: the host objects (clang -O3) were byte-identical to main's for all 938 programs that build. This was run on the first two changes, before the rebase over fix(cli): only a stack overflow prints the stack overflow message (#2120) #2122, and not repeated; the later changes leave the C output as it was.
  • Gates: gates/repo.ts 56 / 56 and gates/test.ts 1,853 / 1,853, run with the first three changes. The emit_fold and cls_fit changes came after and leave every .c and .js in the corpus byte-identical to that gated state; gates/repo.ts was rerun on the final commit. An earlier test run had one miss, a JS-lane timeout in io_chan_reclaim_limit, which did not repeat.
  • Not run: gates/perf.ts on this branch, and CUDA.

🤖 Generated with Claude Code

nicolas-abril and others added 3 commits November 12, 2026 09:44
…_of has no flag, a spare carries its size class, and a fold walks its body once

intr_of looks a def up by its own name, so op_name and its cache are gone,
and an op is an intrinsic in a lane exactly when it has that lane's text.
In JS, Base's bodies of the String ops are no longer emitted. A spare
records its class instead of a word count, so the emitted free names the
class as a number, as every alloc already did. emit_fold charges fuel in
the walk that folds the inner calls, instead of a walk of its own.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@nicolas-abril
nicolas-abril merged commit b596cac into main Nov 12, 2026
1 check passed
@nicolas-abril
nicolas-abril deleted the ai/comp-simplify branch November 24, 2026 11:44
raymond-w-ko added a commit to raymond-w-ko/bend that referenced this pull request Nov 29, 2026
Two conflicts in bend2/comp.ts:
- OPERATIONS: upstream keys intrinsics by Base's def name (bendlang#2129), so
  the fork's vec_* entries become "Vec.length", "Vec.get", ...; and
  bool_pick calls intr_of(k), which no longer takes a JS flag.
- loop_of: keep the fork's walk through Bool.and/or and picks in tail
  position, and drop the done_live check on the callee as upstream did
  (bendlang#2036).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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