Skip to content

C backend: wide monomorphic records bypass boxing and hit the 255-word CID limit #944

Closed

Description

@jasisz

What you did

I compiled this program to the interpreter, JS, and a native executable:

import Base

type R0 is Data:
  R0{a: U32, b: U32, c: U32, d: U32, e: U32, f: U32, g: U32, h: U32}

type R1 is Data:
  R1{a: R0, b: R0, c: R0, d: R0, e: R0, f: R0, g: R0, h: R0}

type R2 is Data:
  R2{a: R1, b: R1, c: R1, d: R1, e: R1, f: R1, g: R1, h: R1}

def r0(+x: U32) -> R0:
  R0{x, x, x, x, x, x, x, x}

def r1(+x: U32) -> R1:
  R1{r0(x), r0(x), r0(x), r0(x), r0(x), r0(x), r0(x), r0(x)}

def r2(+x: U32) -> R2:
  R2{r1(x), r1(x), r1(x), r1(x), r1(x), r1(x), r1(x), r1(x)}

def first0(x: R0) -> U32:
  match x:
    case R0{a, b, c, d, e, f, g, h}:
      a

def first1(x: R1) -> U32:
  match x:
    case R1{a, b, c, d, e, f, g, h}:
      first0(a)

def first2(x: R2) -> U32:
  match x:
    case R2{a, b, c, d, e, f, g, h}:
      first1(a)

def main() -> U32:
  first2(r2(7))
bun bend2/main.ts repro.bend
bun bend2/main.ts repro.bend -o /tmp/repro.js
bun /tmp/repro.js
bun bend2/main.ts repro.bend -o /tmp/repro

What happened

The interpreter and JS both print 7, but the native C build fails before invoking clang:

Error: an arity over 255

This reproduces on 2.0.23 as well, so it appears to be an uncovered case in the #843 fix rather than a 2.0.24 regression.

R0 is 8 words, R1 is 64 words, and R2 is 512 words. Since #843, a datatype past 256 words is supposed to become a heap node, so R2 should be boxed and compile.

The type layout does become BOX, but the constructor table is computed separately. compile_book stores lay_node(fl.book, c.k).ks.length in CID_ARITY_T; for a monomorphic R2, lay_node flattens the eight R1 fields again and produces 512. CID_ARITY_T is emitted as u8, so compile_tables rejects it.

The existing generic BoxK<BoxK<BoxK<U32>>> regression test does not expose this because lay_node sees the open type parameter of BoxK as a box, leaving the constructor arity at 8. Distinct monomorphic record types take the failing path.

There is also an exact-boundary variant: changing R2 to four R1 fields makes it exactly 256 words and still fails with the same error, while three fields (192 words) build and run as native C. lay_of boxes only layouts greater than WIDE = 256, but the u8 CID arity table can represent at most 255.

Expected

The C backend should box the 512-word R2 constructor consistently with its datatype layout and compile the program. The 256-word boundary should either be boxed as well or otherwise remain representable by the runtime metadata.

Environment

bend 2.0.24 (99f9c6cd)
Darwin arm64
Apple clang version 21.0.0 (clang-2100.0.123.102)

Activity

  1. jasisz commented on Sep 21, 2026

    @jasisz
    ContributorAuthor

    There is a second failure path through the same u8 table guard. Fixing only CID_ARITY_T or the boxed constructor layout will not cover it.

    Starting from the report's R0 and R1 definitions (R1 is 64 words), remove R2 and add:

    def sum4(a: R1, b: R1, c: R1, d: R1) -> U32:
      x y = first1(a) first1(b)
      z w = first1(c) first1(d)
      U32.add(U32.add(x, y), U32.add(z, w))
    
    def main() -> U32:
      U32.add(sum4(r1(1), r1(2), r1(3), r1(4)), sum4(r1(5), r1(6), r1(7), r1(8)))

    The interpreter and JS print 36; the native build fails with Error: an arity over 255. The parallel lets make sum4 non-flat, so it gets a runtime segment with 256 physical parameters (4 * 64) and FID_ARITY_T rejects it. Replacing the body with the equivalent single flat expression makes the native build succeed and print 36, because that path emits a direct C helper instead of a segment-table entry.

  2. VictorTaelin commented on Sep 21, 2026

    @VictorTaelin
    Contributor

    Fixed in 2.0.25. One rule now: any field list past 255 words (the u8 arity tables, which is where the boundary really comes from) has its multi-word fields boxed, and it applies to constructor layouts, constructor nodes and def signatures alike. So R2 is eight boxes of R1, the exact-256 variant and your sum4 segment compile, and all three print 7 / 7 / 36 on every lane. Pinned as tests/compile/record_wide_node.bend.

    One shape stays open, and I would rather say so than hide it: a fork whose join holds several wide results (a b c d = r1(1) r1(2) r1(3) r1(4) with 64-word results) still dies "an arity over 255", because a join keeps its results flat. A layout rule cannot bound that; a wider arity table can. Open a new issue if it bites you and we will look at it. (A wide value held across a non-tail call is the same shape.)

    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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions