Skip to content

C backend: a def whose last parameter is erased compiles its parallel let as two sequential calls #1374

Closed

Description

@nuxyel

What you did

bend erased_last_bad.bend -o bad && time ./bad --threads 1 && time ./bad --threads 16

What happened

$ time ./bad --threads 1
3942408568
real 1.013s  user 1.010s
$ time ./bad --threads 16
3942408568
real 1.010s  user 1.006s
 
No speedup with 16 threads. The only difference from a program that scales is
one ERASED parameter, -z, at the end of tree's parameter list, passed unchanged:
 
                                           --threads 1   --threads 16
erased_last_ok.bend    tree(+d, +i)            1.01 s        0.17 s
erased_last_bad.bend   tree(+d, +i, -z)        1.01 s        1.01 s
erased_middle_ok.bend  tree(+d, -z, +i)        1.02 s        0.17 s
 
All three print 3942408568.
 
In the emitted C (bend X.bend -o x.c), erased_last_ok and erased_middle_ok have
the join FID_TREE_J2 for the parallel let; erased_last_bad has only the
continuations FID_TREE_K2 and FID_TREE_K3, so the second call runs after the
first returns.
 
Also reproduced on main at 0ad47fc (bun bend2/main.ts).
 
Files: https://github.com/nuxyel/bend-ml/blob/0a875a59dbb412956fdc662e76ffadb1df2958f1/docs/upstream/erased_last_ok.bend
       https://github.com/nuxyel/bend-ml/blob/0a875a59dbb412956fdc662e76ffadb1df2958f1/docs/upstream/erased_last_bad.bend
       https://github.com/nuxyel/bend-ml/blob/0a875a59dbb412956fdc662e76ffadb1df2958f1/docs/upstream/erased_middle_ok.bend
 
Where it bit us: in bend-ml, Bands.matvec carries an erased proof
(-e: {rr == r : Nat}); with -e last the bands of a matrix-vector product ran one
after another. With a runtime parameter after it, GPT-2's 50257 x 768 logits
product went from 26.7 ms to 3.3 ms on 16 threads.

The file

# The same program with one extra ERASED parameter, -z, at the end of tree's parameter list,
# passed unchanged. The parallel let is compiled as two sequential calls: --threads 16 takes
# as long as --threads 1. Moving -z before the runtime parameter i (erased_middle_ok.bend),
# or adding any runtime parameter after it, restores the fork.
#
#   bend erased_last_bad.bend -o bad && time ./bad --threads 1 && time ./bad --threads 16
import Base
 
def work(n: Nat, acc: U32) -> U32:
  match n:
    case 0n:
      acc
    case 1n+p:
      work(p, (acc * 1103515245 + 12345 : U32))
 
def tree(+d: Nat, +i: U32, -z: Nat) -> U32:
  match d:
    case 0n:
      work(600000000n, i)
    case 1n+q:
      a b = tree(q, (i * 2 : U32), z) tree(q, (i * 2 + 1 : U32), z)
      (a + b : U32)
 
def main() -> U32:
  tree(4n, 1, 0n)

bend --version

bend 2.0.35

uname -sm

Linux x86_64

clang --version (the first line)

clang version 22.1.8

Activity

  1. added
    bug
    on Oct 7, 2026
  2. added a commit that references this issue on Oct 7, 2026
  3. nuxyel commented on Oct 7, 2026

    @nuxyel
    ContributorAuthor

    I traced this to the call-prefix cut in anf (comp.ts): with the erased argument last, the prefix tree(q, i) already has every live argument, so it is cut into a sequential let before the parallel let sees it. I have a 5-line fix with a test, checked against main on every test, bench and demo: #1377.

  4. added a commit that references this issue on Oct 7, 2026
  5. added 2 commits that reference this issue on Oct 17, 2026
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions