Repository navigation
Description
What you did
bend repro.bend -o r && ./r 1 && ./r 2
What happened
bend 2.0.20 (release binary; 2.0.16 and 2.0.18 behave the same), Darwin
arm64 (Apple M4 Pro), Apple clang 21.0.0, macOS 26.5. The checker
accepts the file and the interpreter prints the right answer at every
depth.
The file
Mat(r, c) is a family of 2^r x 2^c floats: a leaf holds one float, a node is split in two by rows, in two by columns, or in four. Mat.matmul is the product over three depths (rows of a, the shared side, columns of b), eight cases, its operands shared with +. main reads the depth d from the command line and prints the sum of ones times ones at 2^d x 2^d, which is 8^d: 8, 64, 512, 4096 for d = 1, 2, 3, 4.
Full file:
import Base
# Mat(r, c): 2^r rows and 2^c columns of F32, a leaf holding one float,
# a node split in two by rows, in two by columns, or in four
law Mat:
for r: Nat
for c: Nat
Data
type Mat.Leaf is Data:
MatB{x: F32}
type Mat.Rows<-p: Nat, -c: Nat> is Data:
MatR{top: Mat(p, c), bot: Mat(p, c)}
type Mat.Cols<-r: Nat, -q: Nat> is Data:
MatC{left: Mat(r, q), right: Mat(r, q)}
type Mat.Quad<-p: Nat, -q: Nat> is Data:
MatQ{tl: Mat(p, q), tr: Mat(p, q), bl: Mat(p, q), br: Mat(p, q)}
def Mat(r, c):
match r c:
case 0n 0n:
Mat.Leaf
case 1n+p 0n:
Mat.Rows<p, 0n>
case 0n 1n+q:
Mat.Cols<0n, q>
case 1n+p 1n+q:
Mat.Quad<p, q>
def Mat.full(+r: Nat, +c: Nat, +x: F32) -> Mat(r, c):
match r c:
case 0n 0n:
MatB{x}
case 1n+p 0n:
t b = Mat.full(p, 0n, x) Mat.full(p, 0n, x)
MatR{t, b}
case 0n 1n+q:
l r2 = Mat.full(0n, q, x) Mat.full(0n, q, x)
MatC{l, r2}
case 1n+p 1n+q:
a b c2 d = Mat.full(p, q, x) Mat.full(p, q, x) Mat.full(p, q, x) Mat.full(p, q, x)
MatQ{a, b, c2, d}
def Mat.add(+r: Nat, +c: Nat, m: Mat(r, c), n: Mat(r, c)) -> Mat(r, c):
match r c:
case 0n 0n:
MatB{x} = m
MatB{y} = n
MatB{(x + y : F32)}
case 1n+p 0n:
MatR{mt, mb} = m
MatR{nt, nb} = n
x y = Mat.add(p, 0n, mt, nt) Mat.add(p, 0n, mb, nb)
MatR{x, y}
case 0n 1n+q:
MatC{ml, mr} = m
MatC{nl, nr} = n
x y = Mat.add(0n, q, ml, nl) Mat.add(0n, q, mr, nr)
MatC{x, y}
case 1n+p 1n+q:
MatQ{ma, mb, mc, md} = m
MatQ{na, nb, nc, nd} = n
w x y z = Mat.add(p, q, ma, na) Mat.add(p, q, mb, nb) Mat.add(p, q, mc, nc) Mat.add(p, q, md, nd)
MatQ{w, x, y, z}
def Mat.add4(+p: Nat, +q: Nat, a0: Mat(p, q), b0: Mat(p, q), a1: Mat(p, q), b1: Mat(p, q),
a2: Mat(p, q), b2: Mat(p, q), a3: Mat(p, q), b3: Mat(p, q)) -> Mat(1n+p, 1n+q):
w x y z = Mat.add(p, q, a0, b0) Mat.add(p, q, a1, b1) Mat.add(p, q, a2, b2) Mat.add(p, q, a3, b3)
MatQ{w, x, y, z}
def Mat.add2r(+p: Nat, a0: Mat(p, 0n), b0: Mat(p, 0n), a1: Mat(p, 0n), b1: Mat(p, 0n)) -> Mat(1n+p, 0n):
x y = Mat.add(p, 0n, a0, b0) Mat.add(p, 0n, a1, b1)
MatR{x, y}
def Mat.add2c(+q: Nat, a0: Mat(0n, q), b0: Mat(0n, q), a1: Mat(0n, q), b1: Mat(0n, q)) -> Mat(0n, 1n+q):
x y = Mat.add(0n, q, a0, b0) Mat.add(0n, q, a1, b1)
MatC{x, y}
def Mat.sum(+r: Nat, +c: Nat, m: Mat(r, c)) -> F32:
match r c:
case 0n 0n:
MatB{x} = m
x
case 1n+p 0n:
MatR{t, b} = m
x y = Mat.sum(p, 0n, t) Mat.sum(p, 0n, b)
(x + y : F32)
case 0n 1n+q:
MatC{l, r2} = m
x y = Mat.sum(0n, q, l) Mat.sum(0n, q, r2)
(x + y : F32)
case 1n+p 1n+q:
MatQ{a, b, c2, d} = m
w x y z = Mat.sum(p, q, a) Mat.sum(p, q, b) Mat.sum(p, q, c2) Mat.sum(p, q, d)
((w + x) + (y + z) : F32)
# a * b over three depths: r rows of a, k the shared side, c columns of b
def Mat.matmul(+r: Nat, +k: Nat, +c: Nat, +a: Mat(r, k), +b: Mat(k, c)) -> Mat(r, c):
match r k c:
case 0n 0n 0n:
MatB{x} = a
MatB{y} = b
MatB{(x * y : F32)}
case 1n+p 0n 0n:
MatR{at, ab} = a
x y = Mat.matmul(p, 0n, 0n, at, b) Mat.matmul(p, 0n, 0n, ab, b)
MatR{x, y}
case 0n 0n 1n+s:
MatC{bl, br} = b
x y = Mat.matmul(0n, 0n, s, a, bl) Mat.matmul(0n, 0n, s, a, br)
MatC{x, y}
case 0n 1n+q 0n:
MatC{al, ar} = a
MatR{bt, bb} = b
x y = Mat.matmul(0n, q, 0n, al, bt) Mat.matmul(0n, q, 0n, ar, bb)
Mat.add(0n, 0n, x, y)
case 1n+p 1n+q 0n:
MatQ{a0, a1, a2, a3} = a
MatR{bt, bb} = b
p0 q0 p1 q1 = Mat.matmul(p, q, 0n, a0, bt) Mat.matmul(p, q, 0n, a1, bb) Mat.matmul(p, q, 0n, a2, bt) Mat.matmul(p, q, 0n, a3, bb)
Mat.add2r(p, p0, q0, p1, q1)
case 0n 1n+q 1n+s:
MatC{al, ar} = a
MatQ{b0, b1, b2, b3} = b
p0 q0 p1 q1 = Mat.matmul(0n, q, s, al, b0) Mat.matmul(0n, q, s, ar, b2) Mat.matmul(0n, q, s, al, b1) Mat.matmul(0n, q, s, ar, b3)
Mat.add2c(s, p0, q0, p1, q1)
case 1n+p 0n 1n+s:
MatR{at, ab} = a
MatC{bl, br} = b
w x y z = Mat.matmul(p, 0n, s, at, bl) Mat.matmul(p, 0n, s, at, br) Mat.matmul(p, 0n, s, ab, bl) Mat.matmul(p, 0n, s, ab, br)
MatQ{w, x, y, z}
case 1n+p 1n+q 1n+s:
MatQ{a0, a1, a2, a3} = a
MatQ{b0, b1, b2, b3} = b
p0 q0 p1 q1 p2 q2 p3 q3 = Mat.matmul(p, q, s, a0, b0) Mat.matmul(p, q, s, a1, b2) Mat.matmul(p, q, s, a0, b1) Mat.matmul(p, q, s, a1, b3) Mat.matmul(p, q, s, a2, b0) Mat.matmul(p, q, s, a3, b2) Mat.matmul(p, q, s, a2, b1) Mat.matmul(p, q, s, a3, b3)
Mat.add4(p, s, p0, q0, p1, q1, p2, q2, p3, q3)
def depth.of(m: Maybe<&2, U32>) -> Nat:
match m:
case None{}:
2n
case Some{k}:
U32.to_nat(k)
def depth(args: List<String>) -> Nat:
match args:
case []:
2n
case a <> rest:
depth.of(U32.read(a))
# the sum of ones * ones at 2^d x 2^d: 8^d
def run(+d: Nat) -> F32:
Mat.sum(d, d, Mat.matmul(d, d, d, Mat.full(d, d, 1.0), Mat.full(d, d, 1.0)))
def main() -> IO(Unit):
do IO<Unit>:
args : List<String> <- IO.args()
IO.print(F32.show(run(depth(args))))
Output:
$ bend repro.bend -o r
$ ./r 1
8
$ ./r 2
bend: memory fault (machine stack overflow?)
$ ./r 3
bend: memory fault (machine stack overflow?)
$ ./r 4
bend: memory fault (machine stack overflow?)
$ bend repro.bend 3 # the interpreter
512
On 2.0.18 the depth-2 run sometimes printed nan or out of memory
instead; on 2.0.20 it is the memory fault every time.
What makes it run
Add a second recursion over the same constructors and call it once.
withsq.bend is repro.bend with these lines inserted before
depth.of:
# the square product over one depth, walking the same nodes
def Mat.matmul.sq(+d: Nat, +a: Mat(d, d), +b: Mat(d, d)) -> Mat(d, d):
match d:
case 0n:
MatB{x} = a
MatB{y} = b
MatB{(x * y : F32)}
case 1n+p:
MatQ{a0, a1, a2, a3} = a
MatQ{b0, b1, b2, b3} = b
p0 q0 p1 q1 p2 q2 p3 q3 = Mat.matmul.sq(p, a0, b0) Mat.matmul.sq(p, a1, b2) Mat.matmul.sq(p, a0, b1) Mat.matmul.sq(p, a1, b3) Mat.matmul.sq(p, a2, b0) Mat.matmul.sq(p, a3, b2) Mat.matmul.sq(p, a2, b1) Mat.matmul.sq(p, a3, b3)
Mat.add4(p, p, p0, q0, p1, q1, p2, q2, p3, q3)
def run.sq(+d: Nat) -> F32:
Mat.sum(d, d, Mat.matmul.sq(d, Mat.full(d, d, 1.0), Mat.full(d, d, 1.0)))
and this main instead of the one above, printing the square product's
sum before the general one's:
def main() -> IO(Unit):
do IO<Unit>:
args : List<String> <- IO.args()
+d : Nat = depth(args)
IO.print(F32.show(run.sq(d)))
IO.print(F32.show(run(d)))
Both products are now right at every depth, the general one included:
$ bend withsq.bend -o s
$ ./s 1 ; ./s 2 ; ./s 3 ; ./s 4
8
8
64
64
512
512
4096
4096
Mat.matmul.sq walks the same MatQ nodes through the same Mat.add4
as the general product's last case. Making a different def reachable
instead (a transpose over the same types) does not help. With a literal
depth in main (run(2n) in place of run(depth(args))) the general
product is right even without the square one, so the failure needs a
depth the compiler cannot see.
Guess / possible directions
The ownership analysis of Mat.matmul (three depths, eight arms, +
operands opened by several arms) comes out different when the same
constructors are also opened by another recursive def, and the first
outcome is wrong. In the emitted C the arms open their operands with
ctr_take in the failing build and term_peek in the working one. Might be related to #853
bend --version
2.0.20
uname -sm
Darwin arm64
clang --version (the first line)
Apple clang 21.0.0
Activity
jasisz commentedon Sep 20, 2026
I found a stronger closed form of this on current
main(6018e28e, Bend 2.0.21). It removes F32, command-line input and IO from the failure, and puts the expected result in a checked theorem in the same file.Starting from the file in the issue, replace every
F32withU32, replace both1.0literals with1, removedepth.of,depthand the oldmain, then use:The parallel let keeps the closed
2nopaque to the compiler while remaining definitionally equal to2nfor the checker. Results:Repeated runs of the exact same C binary alternate between
False{}andbend: memory fault (machine stack overflow?)on my M4 Pro. So this is reproducible with a fully closed program, integer leaves and a checked proposition whose runtime value the C backend flips. There are no templates,@unsafe, FFI, holes or open user laws involved.jasisz commentedon Sep 20, 2026
I reduced this to a much smaller closed repro and found the specific missed fact. This is the same root cause as the matrix failure.
The result is deterministic on both the 2.0.21 tag and current
main(6018e28e). The file is closed and pure. It has no templates,@unsafe, FFI, holes or open user laws.result_okprovesresult() == 29, andruntime_tag_okproves theTrue{}that native C changes toFalse{}.Carrier<0n, 0n>is shared only to make its recursive constructor hot at an instantiation whose payload isU32.packthen builds the same hot constructor at the open typeCarrier<n, 1n>, whose payload isF(1n, n).RTmust become hot before that payload is sealed.The missed fact is in
facts_fam. ForF(1n, n), it callsmat_armsonly on the outer match overt. Those arm bodies are still matches overn:They reach
facts_hotasMatterms, and the walk stops one level aboveRT.CID_CARRIERis therefore hot whileCID_RTstays cold. The hot build seals theRTpayload;usetakes it as a cold raw three-word record and reads the one-word seal cell as its fields.A semantic control splits the inner match into
G(n). Thenfacts_famseesZero -> RTandSucc -> RT,CID_RTbecomes hot, and C printsTrue{}. In a temporary copy of the compiler, recursively walking a forcedMatinsidefacts_hotalso makes this repro printTrue{}; the same change makes the closed U32 matrix repro from my previous comment printTrue{}in 8/8 runs instead of alternating betweenFalse{}and a memory fault. That ties this directly to #901 rather than a second backend bug.The C lane opens a node shared through a two-index family with ctr_take
VictorTaelin commentedon Sep 20, 2026
Fixed in 2.0.22 (
bend update). The C emitter decides per bind whether a shared node is read throughctr_takeor read bare and freed; to heat a family's constructors it walks the family'smatch, and a family with two indices is a match whose arms are matches, which fell into the unknown-type fallback, soMatQnever became hot andMat.matmulfreed a shared pointer as an owned node.Mat.matmul.sq"fixed" it only because its rebind reduces to the ADT and heats it by accident. Nowfacts_hotwalks a match arm's arms. Your program prints 8 / 64 / 512 at every depth; pinned astests/compile/family_two_indices.bend.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.
The C lane opens a node shared through a two-index family with ctr_take
A type under a rewrite stuck on an open proof is read as its body, an…