Skip to content

An operation's argument nested past 32 brackets goes to a local, so a long chain of operators builds in C (#1123) - #1239

Merged
nicolas-abril merged 1 commit into
bendlang:mainfrom
nicolas-abril:deep-intr
Oct 1, 2026

Conversation

@nicolas-abril

@nicolas-abril nicolas-abril commented Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

Context

emit_intr writes a chain of primitive operations as one C expression. ANF only cuts calls, so nothing bounds the expression's depth.

  • U32: a U32_BIN opens three brackets, so 86 nested U32 operators pass clang's limit of 256.
  • F32: each operation opens two, so the limit is about 130.
  • Very deep chains: about 3000 operators crash clang.

The interpreter and JS print the right value at every depth. Fixes #1123.

Change

An argument whose text nests past TPL_DEEP (32) brackets goes to a local through emit_alias, as an argument a template reads twice already does. It covers the C, Metal, CUDA and JS lanes. -fbracket-depth would only reach the host build, not Metal or NVRTC, and doesn't stop the crash.

tests/reg/intr_chain_deep.bend chains 100 U32 operators and 140 F32 ones. On main each half alone fails to build in C.

Checks

  • Output: for all 1596 .bend files under tests/, bench/ and demos/, the emitted .c (720 files) and .js (741 files) and every error log are byte-identical to main.
  • Emit time: hyperfine over the 16 bench/runtime programs (-o main.c, 15 runs each, Apple M4): −0.1% with main run first and +0.5% with the branch run first.
  • New test: check, interpreter, JS and C all print the expected lines.
  • The repros: the issue's 86 U32 operators and a 3000-operator chain build and print the interpreter's value.
  • Size: comp.ts goes from 63,107 to 63,200 ttok (+93).
  • tsc reports no errors in comp.ts. bun gates/repo.ts passes (50/50).
  • Not run: the cluster gates. Existing programs emit the same code.

Alternatives measured

🤖 Generated with Claude Code

… long chain of operators builds in C (bendlang#1123)

emit_intr wrote a chain of primitive operations as one C expression, so 86
nested U32 operators (three brackets per U32_BIN) passed clang's nesting
limit of 256, and about 3000 crashed it. An argument nested past TPL_DEEP
brackets now goes to a local through emit_alias, as one a template reads
twice already does. Every other program emits the same C and JS.

Co-Authored-By: Claude Opus 5.5 (1M context) <[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.

One expression of 86 nested U32 operators passes clang's bracket nesting limit (U32_BIN)

1 participant