Repository navigation
comp.ts back under its cap by removing mechanisms at their root: a global File, one way to read a shared cell, and effects that park themselves - #1281
Conversation
5745591 to
a4c7b0f
Compare
391d842 to
47d055a
Compare
Review of 47d055aSummary: the gates pass, but there is one silent miscompile in C, a compile-time and memory blow-up, and some changes to IO behaviour the PR doesn't mention. I'd hold the merge until the first five items below are fixed. Gates (cluster, PR merged onto main)
What holds up
Must fix1. C miscompile from removing the
c9.bendimport Base
type AuditTree is Data:
AuditEnd{}
AuditNode{n: U32, tail: AuditTree}
type Hidden<P: Data> is Data:
Hide{-B: Data, value: P}
More{h: Hidden<P>}
type Ex is Data:
Ex{tag: U32, -A: Data, v: A, eq: {A == AuditTree : Type}}
def audit_head(t: AuditTree) -> U32:
match t:
case AuditEnd{}:
0
case AuditNode{n, tail}:
n
def count(k: Nat, acc: U32) -> U32:
match k:
case 0n:
acc
case 1n+p:
count(p, (acc + 1 : U32))
def build(k: Nat, +n: U32) -> AuditTree:
match k:
case 0n:
AuditEnd{}
case 1n+p:
AuditNode{n, build(p, (n + 1 : U32))}
def cast(-A: Data, v: A, eq: {A == AuditTree : Type}) -> AuditTree:
%eq : _
v
def unhide(-P: Data, x: Hidden<P>) -> P:
match x:
case Hide{B, value}:
value
case More{h}:
unhide(P, h)
def twice(-P: Data, +x: Hidden<P>) -> P & P:
a = unhide(P, x)
b = unhide(P, x)
(a, b)
def bsum(p: Bool & Bool) -> U32:
match p:
case Tuple{a, b}:
2
def f(e: Ex, -A: Data) -> U32:
match e:
case Ex{tag, A, v, eq}:
audit_head(cast(A, unhide(A, More{Hide{Bool, v}}), eq))
def main() -> IO(Unit):
t = build(3n, count(7n, 0))
n = f(Ex{1, AuditTree, t, {==}}, Bool)
m = bsum(twice(Bool, More{Hide{U32, True{}}}))
IO.print(U32.show((n + m : U32)))2. Compile-time and memory blow-up from removing
t6_k10_d300_small.bend (bend -o x.c)import Base
def f(n: Nat) -> Nat:
match n:
case 0n:
0n
case 300n:
1n
case 1n+p:
f(p)
def g0(x: Nat) -> Nat:
Nat.add(f(100000n), x)
# ... g1..g9 the same with 100001n..100009n
def main() -> List<Nat>:
[g0(0n), g1(1n), g2(2n), g3(3n), g4(4n), g5(5n), g6(6n), g7(7n), g8(8n), g9(9n)]3.
4. A wildcard arm in an IO.OP match now takes requests silently (e52ce32,
t1_wild.bendimport Base
law intercept.at:
for op: IO.OP<U32>
IO(U32)
def intercept.at(op):
match op:
case Emit{v}:
do IO<U32>:
u : Unit <- IO.print("emitted")
return v
case _:
do IO<U32>:
u : Unit <- IO.print("wildcard arm taken")
return 42
law intercept:
IO(U32)
def intercept():
intercept.at(IO.print("SECRET")(U32, x => Emit{7}))
law main:
IO(Unit)
def main():
do IO<Unit>:
u : Unit <- IO.print("before")
r : U32 <- intercept()
IO.print(U32.show(r))5. The JS effect API change is silent.
Should fix
Nits
RebaseThis branch conflicts with main a3b0dfe: 5 hunks in |
9c408f5 to
4699313
Compare
Lorenzobattistela
left a comment
There was a problem hiding this comment.
Re-review: 4699313 plus a merge with main and two small fixes (now 2a811ba)
Thanks for the fixes. The five must-fix items and the binary size from the first review are all resolved, and the gates are green.
Merge with #1324 (472bfc5)
#1324 conflicted in 7 hunks of comp.ts. It resets the module-global layout caches before show_main. This PR removes those globals and builds a fresh FL first, so I kept this branch's side throughout. #1324's own case (T = C{a, b}, then C{a}, then C{a, b} again, in one process) emits B's C byte-identical before and after A on this branch. Main before #1324 throws on it. That case isn't in the suite, so it would be worth adding as a test.
Two small fixes (2a811ba)
- JS
io_eff(k, run)with a third argument now throws. An effect still written for the old need API fails at load. Before, it loaded and stopped waiting; C already fails to compile it. - A request that no effect registers now names its def: "bend: no effect registers ", the old load-time check's words, instead of an unnamed "an alien request". tests/io/effect_unregistered pins it. It still fails when the request runs, as this PR intends.
Gates (cluster)
- 2a811ba: repo 50/50, ping 46/46, test 1549/1549, perf 124/124.
- 472bfc5: test 1548/1548, perf 121/124, and safe matches main file for file, with the 2 new tests agreeing.
Perf also ran in three alternating full-table rounds against main 1159f0b. No measured cell went over 1.15x, and best-of-3 geomeans match main in every column. bfs compile time is back to main's level.
Fixes confirmed with the first review's repros
- c9.bend prints 9 at 1 and 8 threads.
- The big-literal program compiles in 0.32 s with 273 MB, against 7.3 s and 4.4 GB before.
- The 5 ms timer fires before a UDP backlog drains.
- A request reaching a user's
case _fail-stops. - io_hello_world is 70 KB again.
Notes, not blocking
mat_opshard-codes IO.OP's constructors as["Emit", "Halt"], so a third constructor would need updating there too (and in io_step and io_run).- On the device,
term_peekis now a plain 64-bit load where it was volatile 32-bit loads. CUDA hasn't been run, so it should get a run before release.
…ds the book and no cache is cleared by hand The caches were module globals cleared per book, so the compiler already held one book at a time; 40 functions still took `book` or `fl` only to reach it. Now FL is the book being compiled and all that is memoized about it: compile_book and js_lib make a new File first, so show_main reads this book's caches rather than the last one's, and FL.memo holds what one def's walk memoizes (memo_gc clears it as a whole). type File lists every field, FL.memo's too. The root segment mints no C id: no name is empty, so its FID_ never collided with anything. Emitted C, JS and .mjs are byte-identical for all 1626 .bend files under tests/, bench/ and demos/. comp.ts: 63980 -> 63502 ttok. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Intr.call meant three things: an array op stays intrinsic in C with no C text, emit_intr and js_expr send it to arr_op, and emit_fold does not rebuild a call with folded arguments. The last was redundant (emit_intr folds each argument as it emits it), so the flag only said 'arr_op lays this out', which C: null now says. Emitted C, JS and .mjs are byte-identical for all 1626 files. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…t_open and emit_put compile_book named a foreign def's parameters, opened its segment and wrote its return by hand, which is what emit_open and emit_put do; the one difference is the continuation, a parameter past the live ones, which emit_open now names k. Emitted C, JS and .mjs are byte-identical for all 1626 files. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…l flag
facts_hot matches an erased Var against the current def's telescope by
name and index; a constructor's own erased binder (AuditHide{-A, value:
A}, 6200daa) unbound as Var(A, i) could pass for the def's parameter, so
a local flag ran through facts_hot and facts_ctr to switch the
match off under such a constructor. ctr_tail now opens each binder as
DUMMY, named ~, which no parameter can match: the collision cannot arise,
for erased and live dependent fields alike, and the flag goes. Emitted C,
JS and .mjs are byte-identical for all 1626 files (seal_var_scope and
seal_clone_heat among them). A constructor's own telescope, which the
teles memo served through tele_unbind, is memoized in FL.ctrs, so a
1000-arm Nat literal match emits in the same time as on main.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…_nat shim goes 942573f made a literal one Lit node and kept the old parser's desugar of a Nat past NAT_LITERAL_MAX, U32.to_nat of its word, so the emitted C and JS stayed the same. Nothing needs it: ctr_adt reads a Nat literal in one step, as it reads U32 and F32 ones, and FOLD_FUEL caps any unrolling. A big literal now counts as constant: 32 C files change, most only from ((u64)300ull) to 300ull; a constant list of big Nats becomes a static image (walkers_test), and calls on big literals fold (literal_rows_cubic, array_map_loop). nat_pattern_deep, which pinned the default arm's one checked sum and reached it through the shim, takes its 300 from U32.to_nat, so down still runs its match: its C again holds nat_chk(e, _305_0 + 300). The 22 changed tests pass interpreted, in JS and in C; the 5 changed benches print their pins in C and JS. JS and .mjs are byte-identical everywhere. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ses a leaked one A JS undefined leaked into the generated C is an undeclared name in a segment, a spin or a table, all of which clang compiles on the host, so the build fails there; the scan only reworded that failure, and had a false positive of its own (effect_undefined_word, whose comment now says so). Emitted C, JS and .mjs are byte-identical for all 1626 files. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
… impossible states go - corpus_eval: work_loop returning 0, a seam delivery returning 0 and a pool turn all end when the root is done, so the three exits and their two 'delivery lost' messages are one loop and one check after it. - gpu_make (CUDA) took a NULL path no caller passes; ring_rewind skipped a ring holding more than RING_LEN, which a host push's err_fail and the device's error check after its pass both rule out; run_tail wrapped its one argument in an array that run_loop spread back. Full local suite (gates/test.ts's shards on this Mac, every lane, Metal for the bang tests): every test passes but the two display-dependent gfx tests, as on main. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
- term_peek reads a redirect cell with one relaxed 64-bit load (plain on the device), as bendlang#1155 made blk_loc do: the target bits never change, so a torn read still finds them, and a shared node's fields never change either (span_fade only bumps its children). rfc_view's two atomic halves and its acquire go; ctr_take reads the count itself after copying and acquires only when it is the last holder, before it frees the cell and owns the node. blk_loc is term_peek for a shared block. FID_EXIT's continuation fast path stays: without it, code that applies a closure which may fork retires 28% more instructions at 8 threads. Full local suite on every lane: all pass but the two display-dependent gfx tests, as on main. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
… IO.OP match keeps its default arm in both lanes, so $FFI goes
C refuses a request reaching a Bend match over IO.OP through the match's
kept default arm (ERR_TAGS); JS threw the request object and io_run's
catch turned it into the fail-stop. mat_ctrs now keeps the default arm
for IO.OP itself, so JS takes the same arm, and io_run's catch only maps
a stack overflow. A foreign op is { $: name, args, kont }, built like
C's constructor; the loop looks its effect up by tag and refuses an
alien request at run time, as C does, so js_lib's load-time registry
check goes with $FFI. request_out_of_band's header says so; its lines are
unchanged. Full local suite on every lane: all pass but the two
display-dependent gfx tests, as on main.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
… has one way to run a request The loop could park a request before its effect ran, from a need given at registration (IO_READ, IO_TIME in C; a need function in JS), and only IO.sleep, TCP.accept and UDP.recv_from had one. Each effect already parks itself the other way (io_wait_on in C, io_park_on in JS): accept and recv_from on EAGAIN, so they just try first; sleep now parks on its deadline. So io_eff takes (cid, run), IoEff and io_exec go, and io_step takes a request's fields once for Halt and for an effect. Nothing an effect could do is lost: guide/EFFECTS.md now says an effect that waits parks itself, and how to wait on time alone. Breaking for effects outside Base: a C io_eff with a third argument no longer compiles, and JS ignores one. All 63 C registrations in bend2/effs and tests drop their third argument. effect_need_collision keeps its point (two effect files define the same function name) with the helper as a plain function. All 136 io tests pass on every lane. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…he helper tests stop saying need - file_book takes the root file and returns nothing; compile_book and js_lib bind it once (const fl = FL = file_new(...)) and say fl, where they mixed fl and FL for the same object. - js_lib's comment registered effects with io_eff(CID(k), run, need?), which takes no need now: it says io_eff(CID(k), run). - effect_need_collision(_reverse) and their two .js files become effect_helper_*: with needs gone they pin that two effect sources may define the same function name, and the name says so. Emitted C, JS and .mjs are byte-identical for every file but the renamed tests, which pass on every lane. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
… compiled, a Scope is where the emitter writes
File held both what lives for a book (its facts, its output, its caches)
and what lives for a scope (the segment written, the locals' names, the
bindings and spare nodes held, the def), so every { ...fl } copy for a
match arm, closure or fork carried the book's dozen caches, and fl.hot
and FL.hot named the same set. Now file_new builds the File (FL) and
scope_new a Scope, which a nested scope copies; functions read the book
through FL and take a scope (sc) only to write. Six that took the file
only to reach the book (arr_q, brw_of, c_ids, effect_srcs, js_host,
js_marshal) take none. Emitted C, JS and .mjs are byte-identical
for all 1626 files.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…nrolls recursion bounded by one Reading a big literal as a constant let the folder try to unroll any recursion bounded by it: a 43-line program took 7.3 s and 4.4 GB to compile against 0.3 s and 324 MB on main, and a failed unroll blocked the later folds in its def (array_map_loop lost one). lit_call, term_const, ctr_adt and nat_pattern_deep are main's again. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…nder is not the def's parameter Opening a constructor's binders as DUMMY did not do what the flag did: on a type whose constructor binds its own erased type, the C binary printed 67108867 where every other lane printed 9, and corrupted the heap. facts_hot, facts_ctr and ctr_tail are main's again, over Scope and FL. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…g cannot starve the timers With the need gone, accept and recv_from tried their socket first, and while datagrams or connections were queued every request answered at once: the loop never polled, so a 5 ms timer fired after 101 ms in C and 603 ms in JS. Each now parks until its socket is readable, as the IO_READ need made it, and re-parks on EAGAIN as before. udp_backlog_timer pins it: its 0 ms tick prints before 40 queued datagrams drain (after them before the fix, in C and in JS). guide/EFFECTS.md says when an effect parks first, and that io_wait_on keeps its fd in w->word. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…reaching it fail-stops in every lane A user's case _ in an IO.OP match took a request in every lane once $FFI went (main refused it in JS and bend run, and took it in C). mat_ctrs now splits such a default arm: the user's arm runs when the scrutinee is an Emit or a Halt, and anything else, a request, fail-stops as the kept default arm already did in a match without one. request_wildcard pins it: a purely built Halt still reaches case _, and the request is refused in C, JS and bend run. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…every binary clang placed the static io_eff_rows in __DATA_CONST, so every native binary carried its 65536 null pointers: io_hello_world was 598 KB on this Mac (main's IoEff table, twice as wide, made it 1.1 MB). A non-static table is zero fill: 70 KB. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…mments say what holds - compile_book opens each def in scope_new(), which is what it built by hand; mat_ctrs names its IO.OP test once. - FL.memo's consts and lits are ground and steps, apart from FL.consts and FL.lits, which hold other things and clear at other times; js_marshal keeps its names in FL.marsh, not in the C lane's FL.spun. - File's comment says brwl is the scope's; term_peek's says the host's load cannot tear (only the device's can), and why ctr_take may copy a shared node's fields before it acquires. JS and .mjs are byte-identical for all 1628 files, and C differs only in term_peek's comment, which the C runtime carries. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…es its def With the need gone, a JS io_eff(k, run, need) loaded and ran its effect at once, so an effect written for the old API stopped waiting with no error, where C refuses to compile one. io_eff now throws when it is given a third argument. And with js_lib's load-time check gone, a foreign def whose source registers no effect failed with "an alien request", naming nothing; the loop now says "no effect registers <def>", the old check's words. effect_unregistered pins it in the interpreter and the JS lane. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ime-only park Co-Authored-By: Claude Opus 5.5 <[email protected]>
…arve the timers udp_backlog_timer pins it for UDP.recv_from; TCP.accept had the same fix and no test. Twenty connections wait in the listener's queue while a 0 ms timer is due: the tick prints before the last accept, in C, JS and bend run. With the accept trying its listener before it parks, it printed after them, in C and JS. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
f6ee050 to
19187ab
Compare
…end is rebuilt FORK.md states the rules this fork follows: main is upstream plus U64, F64 and the F64 min/max fix, replayed when upstream moves, with each old main kept as a tag archive/main-*; BendCAD pins are tags numeric/<date> on the pin branch numeric-on-upstream, whose conformance/ receipts name the tree each pin tested; numeric is the earlier pin branch. AGENTS.md points to it and warns that `bend update` drops U64 and F64. This main replays U64, F64 and the min/max fix onto upstream bf96827. Upstream's bendlang#1281 replaced the threaded File with a global FL and a per-scope Scope and dropped the intrinsics' call flag; the port keeps every behavior: erased-argument layouts in fun_of, two-word arguments and results in intr_c, soft natives in place behind #ifndef __METAL_VERSION__, and the U64 and F64 show and read natives. Checked on WSL2 (Ryzen 7 7800X3D, RTX 4070 SUPER): repo gate 50/50; test gate 1561/1564, where import_Word and io_process_run fail the same way on upstream bf96827 (1548/1551) and the two failures that differ between the runs pass 3/3 in isolation on both trees; conformance/run.sh at D=20 on this bend2 tree (1a3b0a4), harness from numeric-on-upstream with the defs lanes' edit matching the new file handle: 0 mismatches on js, c, cuda, cuda-defs, c-defs and the interpreter, and no line a Metal compile keeps uses a double. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… either file handle cuda.sh's FLIP edit looked for file_push(fl, "#ifndef __METAL_VERSION__"). Upstream bf96827 (bendlang#1281) names the handle sc, so on fork main the edit did not apply and both defs lanes failed to build. It now matches any handle name; on this tree it changes the same line as before. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Context
comp.tssits just under its permanent 64,000 cap: main is at 63,971 ttok. This PR brings it to 62,978.The cuts are simplifications at the root rather than trimming: each removes a mechanism that existed only because another part behaved a certain way, or a second way of doing one job. Each was traced to the commit that added it, to check the reason no longer holds.
Changes (one commit each)
Fileis global and holds every cache. The caches were module globals cleared per book, so the compiler already held one book at a time, yet 40 functions tookbookorflonly to reach it.FLis now the book being compiled and everything memoized about it;FL.memoholds what one def's walk memoizes.type Filelists every field,FL.memo's too.show_mainnow reads this book's caches, not the previous book's (it ran before the caches were cleared): this restores Initialize compiler caches before building printer layouts #961's fix, which main has lost. Two books compiled in one process now each emit what a fresh compile emits; on main the second either gets the first's stale layout or throws.Intr.callgoes: an intrinsic thatarr_oplays out hasC: null; the flag's third use (blocking a fold rebuild) was redundant.emit_open/emit_put, like any def's.undefinedin the emitted C: a leaked one is an undeclared name, which clang refuses.corpus_evalis one loop with one post-condition, and three guards for states that cannot occur go.term_peek(one relaxed 64-bit load, as A shared Array's redirect is read without a race and without an atomic on the device, once per spin (#975) #1155 madeblk_locdo), andrfc_viewgoes;ctr_takeacquires only as the last holder, before it owns the node. Shared Array redirect lookup races with atomic reference-count updates #975's reproducer under ThreadSanitizer: 0 reports in 10 runs on both trees (and 6 with the read made plain, as a control).$FFIgoes. A user'scase _in an IO.OP match takes only an Emit or a Halt: a request reaching it fail-stops in every lane.io_efftakes no need, and the loop runs every request one way.IO.sleepparks on its deadline;TCP.acceptandUDP.recv_frompark until their socket is readable before their first try, as the need made them, so a backlog cannot starve the timers. The C effect table is notstatic, so it is zero fill rather than 512 KB of every binary.FLis the File being compiled, and aScope(segment, local names, bindings, spare nodes, def) is where the emitter writes, so a nested arm, closure or fork copies only its scope.The series also tried opening a constructor's own binders as
DUMMYin place offacts_hot'slocalflag, and reading Nat literals past 256 withoutlit_call'sU32.to_natshim. Review found a C miscompile from the first and a compile-time blow-up from the second (7.3 s and 4.4 GB against 0.3 s and 324 MB on main), so both are main's again in later commits; a squash leaves neither.Breaking
io_eff(cid, run)takes no need. A third argument is a compile error in C, and in JSio_effthrows ("<k>takes no need: an effect that waits parks itself").case _in an IO.OP match fail-stops in C too. Main already refused it in JS andbend run, but ran the user's arm on it in C. Every waiting effect already had the self-parking form (io_wait_on/io_park_on), so nothing becomes impossible;guide/EFFECTS.mdsays so and shows how to wait on time alone. All 63 C registrations inbend2/effsandtestsare updated.<def>"; C says "an alien request"), not at load.Checks
On main 1a2e5cd (after the review fixes and rebases), on an Apple M4:
.bendfiles undertests/,bench/anddemos/, each commit that says its output is byte-identical emits byte-identical C, JS and.mjsagainst its parent. Each review fix changes only what it says. Every rebase was checked file by file: an output main's new commits leave alone is unchanged, and the ones they change hold both main's change and this PR's.udp_backlog_timer(a 0 ms timer fires before 40 queued datagrams drain),tcp_backlog_timer(the same before 20 queued connections are accepted) andrequest_wildcard(an IO.OPcase _takes a Halt but refuses a request) fail without their fixes and pass with them, in C and JS;effect_unregisteredpins the named refusal.gates/test.ts's shard script on this Mac): 1548/1550. The two failures aregfx_clicksandgfx_window, which need a display.gates/_run.ts): repo 50/50, test 1550/1550, perf 121/124 and 124/124 on reruns, against main's 88/124 to 124/124 on the same minis. The cells over 1.15x were PAR-GPU times under 0.1 s, on both trees. ping was skipped because the site repo is not checked out here.The original series, before the rebases and the review fixes, on an Apple M4 against main (fb6f32a):
.mjsbyte-identical for all 1608.bendfiles undertests/,bench/anddemos/; the rest change it as described in their messages.gates/test.ts's shard script run on this Mac; every lane, Metal for the!tests): all pass butgfx_clicksandgfx_window, which need no display and fail on main too.within_busy0.75.FID_EXIT's continuation fast path (87a1e9c) cost 28% more instructions at 8 threads in code that applies a closure which may fork (9.03G -> 11.54G; no bench takes that path), so the fast path stays. The 34 commits that aimed at performance are all preserved or untouched.FID_EXIT's fast path (above), element-wise block copy (10% slower on a block-heavy micro-benchmark), a shared device/host drain loop (Metal compiled the work loop twice), JS Nat matches through==(a host's raw BigInt), droppingfl.consts(b84b004's 8 s compile guard), and two emitter shortcuts that relied on term identity.gpu_makechange are untested) and Linux.🤖 Generated with Claude Code