Skip to content

Compile flat constructors with more than 255 fields - #1015

Closed
oxura wants to merge 1 commit into
bendlang:mainfrom
oxura:fix/wide-constructor-arity
Closed

oxura wants to merge 1 commit into
bendlang:mainfrom
oxura:fix/wide-constructor-arity

Conversation

@oxura

@oxura oxura commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

Fixes #991.

The C backend stores constructor arities in CID_ARITY_T. The table is now emitted as u32 only when a constructor needs more than 255 words; all other tables retain their existing 8-bit limit. A program whose arities fit in 8 bits still emits byte-identical C (verified on the #1007 reproducer).

tests/reg/record_256_fields.bend builds a flat 256-field constructor and checks its first and last fields after matching it. Before this change the checker and interpreter accepted the test but C emission failed with Error: an arity over 255. Afterward the interpreter, emitted JS, and native C all print 255. A separate 512-field repro builds and runs natively (output 42); existing 255-field and boxed wide-record native cases still print 1 and 113.

Verification: local --checkup for the three fixtures passes; bun gates/repo.ts reports PASS: 46 / 46; git diff --check passes. The cluster/GPU gates were not run. Project-wide TypeScript checking still reports four diagnostics in untouched bend2/bend.ts and bend2/docs/gen_*.ts (missing canvas types).

@oxura
oxura force-pushed the fix/wide-constructor-arity branch from 1c9771b to 900859a Compare September 24, 2026 07:57
@nicolas-abril

Copy link
Copy Markdown
Collaborator

Thank you for picking up #991 and for the clear write-up. We're closing this one, because the 255-word limit is there by design. It's not a table that happens to be too small. The runtime is built around a node fitting 255 words: CID_ARITY_T and FID_ARITY_T are u8, and the walkers pack a node's arity into 8 bits of their own words. Widening one table therefore doesn't lift the limit; it only moves the failure somewhere quieter.

Here is what that looks like with this branch. term_drop keeps its position in one word, loc (40 bits) | field index (8) | arity (8) | size class (8). For a 256-field node, 256 << 48 spills into the size-class bits. An instrumented build shows every dropped 256-field node packed as n=0 cls=9 instead of n=256 cls=8: its fields are never visited, and its 256-word block goes back to the 512-word free list. A program that drops a List of six 256-field records and then builds six 300-field records prints 501/401 for the last record on the C lane, where JS and the interpreter print 501/501. On main the same program refuses to build. The branch's test and the rest of the suite pass because they only ever match wide records, and a matched record is freed by the compiler with a size class it knows, never by term_drop.

The fix we'd want for #991 is to refuse such a program cleanly: a user-facing error that names the constructor and the limit, reported the same way on every lane. The checker (at the type declaration) is the natural place, since the interpreter and JS currently accept what C refuses, and the compiler's internal "an arity over 255" message would never be reached. If you'd like to take that on, we'd be glad to review it.

Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.

@nicolas-abril

Copy link
Copy Markdown
Collaborator

A correction to my last comment. I said the 255-word limit is by design and that #991 should be fixed by refusing the program. That was wrong. It misread the direction we set in #944. The 255-word limit is how the runtime stores nodes, not a limit on the language: since 2.0.25, a field list past 255 words has its multi-word fields boxed so that a wide record compiles on every lane. Your goal of making the #991 record compile is the right one. Sorry for the confusion.

The direction we want is more boxing, not wider runtime tables. #991 is the case the current rule cannot reach: every field is already one word, so lay_wide finds nothing to box. Extending the rule would cover it. When a field list is still past 255 words after the multi-word fields are boxed, move a run of the single-word fields into a boxed sub-node, so every node stays within 255 words. CID_ARITY_T, FID_ARITY_T and the runtime walkers then stay as they are.

The part of my earlier comment about widening CID_ARITY_T still holds. term_drop packs a node's arity and field index into 8 bits each, so a 256-word node dropped whole is freed with the wrong size class and its fields are never visited. The repro prints 501/401 on C against 501/501 on JS and the interpreter. Boxing avoids that entirely. A test for the new rule should include a drop case: a whole List of wide records dropped unopened, followed by allocations of the next size up.

If you'd like to take this on, a new push to this branch or a new PR is welcome, and we'll review it.

Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.

@oxura

oxura commented Sep 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the correction and the concrete drop repro. I kept the runtime arity table at 8 bits, boxed runs of one-word fields, and added the unopened-list drop regression in #1044. The old widened-table approach in this closed PR should not be used.

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.

C backend: a constructor with 256 single-word fields still exceeds CID_ARITY_T after #944

2 participants