Repository navigation
The emitted C builds at -O0: segments take preserve_none only when clang optimizes - #1074
Conversation
…ang optimizes
`bend x.bend -o x.c` emits C that a user may build by hand, for a debug
build or a sanitizer, but clang 19+ at -O0 cannot build a program whose
segments carry many words: its register allocator fails on the
preserve_none musttail chain ("ran out of registers during register
allocation in function 'WL_FID_ENTER'"). PRESERVE(A) now applies
preserve_none and preserve_most only when __OPTIMIZE__ is defined; at -O0
the segments take the default convention, the one clang 14-18 and gcc 15+
already build the runtime with (cd3366a).
tests/io at -O0 -g (clang 22.1.8, Linux x86-64): 82 of the 123 tests
emit C (the other 41 pin a parse or check refusal). On main 8 of them
fail to compile: file_binary, process_run, tcp_loopback, tcp_poll,
tcp_short_recv, udp_poll, udp_send_recv and udp_truncate. Here all 82
build (the two audio tests with -lasound, as bend -o links them), and
those 8 print their expected lines; so does tcp_poll built
with -O0 -g -fsanitize=address. At -O1, -O2 and -O3 the assembly is
byte-identical to main's. gates/repo.ts passes 46 / 46.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
… header, where 87a1e9c moved PRESERVE's comment The only conflict was PRESERVE's #if, whose comment 87a1e9c folded into the section's header paragraph; the -O0 sentence goes there too. On this tree the eight tests/io programs main cannot build at -O0 -g (file_binary, process_run, tcp_loopback, tcp_poll, tcp_short_recv, udp_poll, udp_send_recv, udp_truncate) build here and print their expected lines; tcp_poll's and process_run's -O3 assembly is byte-identical to main's; gates/repo.ts passes 46 / 46. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks for the PR and the careful measurements. Before merging, I'd like to understand the use case. bend -o always builds at -O3, so nothing we ship hits this. The PR also says -O1 and up compile fine. For a debug build or a sanitizer run, does -O1 -g (or -O1 -g -fsanitize=address) not cover it? Is there a workflow that specifically needs -O0? One concern: with this change, a -O0 build runs the segments under a different calling convention from the binary bend -o produces. A bug tied to preserve_none, like the x86 tail-call register limit the rp pad works around, wouldn't reproduce in the debug build, which is where you'd go looking for it. So unless there's a concrete case -O1 -g can't handle, maybe it's better to keep one convention. |
|
Thanks for looking at it. You're right about sanitizers: Where
Its bugs live in that C: how At
The same shape can be reproduced from the repo's own tests: append
On keeping one convention:
If you'd still rather a |
|
Thanks, this convinced me. PRESERVE already drops the attributes wherever the compiler can't handle them (clang 14–18, gcc), and musttail keeps every jump a tail call either way, so gating on OPTIMIZE is the same rule applied to -O0. It also keeps -O0 uniform across targets instead of working on arm64 and failing on x86-64. I confirmed it on current main: the 8 fail at -O0 on x86-64 and build with the change, tcp_poll runs, and -O1/-O2/-O3 assembly is byte-identical on x86-64 and arm64. Could you rebase? It conflicts now: the comment moved into the // Dialect header (comp.ts ~3481), so the -O0 note belongs in that paragraph. Please also drop the sanitizer line from the description. Happy to merge after that. |
|
Thank you, Lorenzo, both for pushing on the use case (the answer is better for it) and for checking arm64 yourself. That covered the one box I couldn't. Done:
Re-checked on the merged head (Linux x86-64, clang 22.1.8):
Thanks again for the careful review. |
bend x.bend -o x.cemits the C source (the guide's Tooling section), and building it by hand is how one gets a debug build, to step through a foreign effect's own C in a debugger. With clang 19 or later that fails at-O0once a program's segments carry more than a few words, and it does on eight of the repo's own IO tests:The segments are
preserve_nonefunctions chained bymusttail(386b7fc), and clang's-O0register allocator cannot place that chain.-O1and up are fine, which is whybend -o, always-O3, never sees it.The change
PRESERVE(A)appliespreserve_noneandpreserve_mostonly when__OPTIMIZE__is defined. At-O0the segments take the default calling convention, the one clang 14-18 and gcc 15+ already build the runtime with since cd3366a, andmusttailstill makes every jump a tail call. The note joins the// Dialectheader's paragraph:Evidence (clang 22.1.8, Linux x86-64)
Every test in
tests/io, emitted and built with-O0 -g, on main and on this branch. 82 of the 123 emit C; the other 41 pin a parse or check refusal.-O0 -g#|linesfile_binary,process_run,tcp_loopback,tcp_poll,tcp_short_recv,udp_poll,udp_send_recv,udp_truncate.-O3(my runner is notgates/test.ts), andtcp_send_slow_peeronly needs more than the 10 s I gave it (16.9 s at-O0, against 2.0 s at-O3).-O1,-O2and-O3the assembly oftcp_poll,process_runandudp_truncateis byte-identical to main's, so whatbend -obuilds does not change.-O0 -gand print their#|lines, the-O1,-O2and-O3assembly oftcp_poll,process_runandudp_truncateis still byte-identical, andgates/repo.tspasses 50 / 50.There is no test file: the harness builds a test's binary at
-O3, so a#|test cannot pin a compile flag. Glad to add one in whatever shape you prefer.arm64: @Lorenzobattistela confirmed the
-O1,-O2and-O3assembly is byte-identical there too, and-O0already built there (24 argument registers), so this makes-O0the same on every target. macOS not tested. The device code is untouched: the#ifsits in the host branch.