#1195·Bend

A generic def over a Data record boxes it on the device heap: a Bool.pick of two records took Metal's compile from 8 s to over 11 minutes

Author: AdrielSantanaCreated Sep 29, 2026Updated Sep 29, 2026

A generic def over a Data record, Bool.pick included, boxes the record on the device heap in flat code. Every call site then inlines the heap's allocation, term_drop's walk and ctr_take. In a large kernel this took Metal's compiler from 8 seconds to more than 11 minutes.

Bend 2.0.34 (777ee0b5), macOS 26.6.2, Apple M5.

Repro

python
import Base

type Tint is Data:
  Tint{r: F32, g: F32, b: F32}

def mixed(a: Tint, b: Tint, +f: F32) -> Tint:
  Tint{+ar, +ag, +ab} = a
  Tint{+br, +bg, +bb} = b
  Tint{(ar + (br - ar) * f : F32), (ag + (bg - ag) * f : F32), (ab + (bb - ab) * f : F32)}

def light(+x: F32) -> Tint:
  Bool.pick(Tint, (x < 0.0 : F32), Tint{0.84, 0.95, 1.2}, mixed(Tint{1.3, 1.06, 0.72}, Tint{1.05, 1.0, 0.93}, x))

def red(t: Tint) -> U32:
  Tint{+r, g, b} = t
  F32.to_u32((r * 100.0 : F32))

def leaf(+i: U32) -> U32:
  red(light((U32.to_f32((i % 1000 : U32)) * 0.001 - 0.5 : F32)))

def tree(+k: Nat, +i: U32) -> U32:
  match k:
    case 0n:
      leaf(i)
    case 1n+e:
      a b = tree(e, (i * 2 : U32)) tree(e, (i * 2 + 1 : U32))
      (a + b : U32)

def main() -> IO(Unit):
  do IO<Unit>:
    IO.print(U32.show(tree!(14n, 0)))

In the emitted C, light's spin builds the computed Tint on the heap with heap_alloc(e, cls_fit(3)) and three stores. It builds the literal one as a static term. It then calls the pick's spin with two Terms. That spin drops the unchosen record through term_sink, which inlines term_drop's heap walk. Last, it unpacks the chosen record with ctr_take and spare_free.

Two variants of light for comparison:

  • match: the same choice as a match in a non-generic def keeps the three fields in registers, as flat records are everywhere else.

    python
    def chosen(c: Bool, +x: F32) -> Tint:
      match c:
        case True{}:
          Tint{0.84, 0.95, 1.2}
        case False{}:
          mixed(Tint{1.3, 1.06, 0.72}, Tint{1.05, 1.0, 0.93}, x)
    
    def light(+x: F32) -> Tint:
      chosen((x < 0.0 : F32), x)
  • generic: a generic def of one's own boxes just as Bool.pick does:

    python
    def choose(-A: Type, c: Bool, a: A, b: A) -> A:
      match c:
        case False{}:
          b
        case True{}:
          a

So the type parameter is what boxes the record, not Bool.pick itself.

The three variants were built to C and to a GPU program. Each ran with --gpu off, and nothing ran on the GPU:

light heap_alloc of a Tint GPU program result
Bool.pick(Tint, ..) 1 96,848 bytes 1690288
generic choose(Tint, ..) 1 96,848 bytes 1690288
non-generic match 0 73,824 bytes 1690288

One pick site adds 23 KB to the GPU program. With the pick inside a flat 60-step loop that leaf calls from eight places, the program is 687 KB against 184 KB.

Where it bit

Bendcraft is a voxel ray caster whose Metal program is 2.2 MB. One change made the light's colour a Bool.pick between two Tints: the sun's, or the moon's once the sun has set. Before, it was a blend by a factor. That def is reached from four sites in a pixel's shading.

With the pick, --gpu-build did not finish. MTLCompilerService was at 99% CPU and 1.2 GB after 11 minutes, when I killed it. The same program with the choice written as a blend by a Bool.pick(F32, ..) builds in 8 seconds.

The tiny repro above still builds in a second or two. The compile time only blows up at the kernel's full size, so it is not a repro of the hang itself. It does show the code that caused it. Had it compiled, that code would also have allocated on the heap once a pixel, a call.

Expected

A pick between two flat records should stay in registers, as a select per field, the way a non-generic match already does. One way would be to specialize a generic def at a flat Data type in flat code. Failing that, the drop of the unchosen value should not inline term_drop's walk at every site.

The workaround is to choose the record in a non-generic def that matches on the Bool, or to pick its scalars.