Skip to content

The emitted C builds at -O0: segments take preserve_none only when clang optimizes - #1074

Merged
Lorenzobattistela merged 5 commits into
bendlang:mainfrom
aldeni:c-builds-at-o0
Oct 1, 2026
Merged

Lorenzobattistela merged 5 commits into
bendlang:mainfrom
aldeni:c-builds-at-o0

Conversation

@aldeni

@aldeni aldeni commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

bend x.bend -o x.c emits 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 -O0 once a program's segments carry more than a few words, and it does on eight of the repo's own IO tests:

$ bun bend2/main.ts tests/io/tcp_poll.bend -o tcp_poll.c
$ clang -std=c11 -O0 -g tcp_poll.c -lpthread -lm -o tcp_poll
error: <unknown>:0:0: ran out of registers during register allocation in function 'WL_FID_ENTER'
error: <unknown>:0:0: ran out of registers during register allocation in function 'WL_FID_IO_PASS_C3'

The segments are preserve_none functions chained by musttail (386b7fc), and clang's -O0 register allocator cannot place that chain. -O1 and up are fine, which is why bend -o, always -O3, never sees it.

The change

PRESERVE(A) applies preserve_none and preserve_most only when __OPTIMIZE__ is defined. At -O0 the segments take the default calling convention, the one clang 14-18 and gcc 15+ already build the runtime with since cd3366a, and musttail still makes every jump a tail call. The note joins the // Dialect header's paragraph:

// ... Only clang 19+ has both
// preserve_none and preserve_most, and compiles preserve_most soundly; at
// -O0 its register allocator cannot place a preserve_none segment, so an
// unoptimized build takes neither. ...
#if __has_attribute(preserve_none) && __has_attribute(preserve_most) \
  && defined(__OPTIMIZE__)

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.

main this PR
compile at -O0 -g 74 of 82 82 of 82
the 8 main cannot build — build and print their #| lines
  • The eight: file_binary, process_run, tcp_loopback, tcp_poll, tcp_short_recv, udp_poll, udp_send_recv, udp_truncate.
  • Every other result matches main's test by test. Nine runs fail under my runner on both trees: eight fail the same way at -O3 (my runner is not gates/test.ts), and tcp_send_slow_peer only needs more than the 10 s I gave it (16.9 s at -O0, against 2.0 s at -O3).
  • At -O1, -O2 and -O3 the assembly of tcp_poll, process_run and udp_truncate is byte-identical to main's, so what bend -o builds does not change.
  • Re-checked on main 675381e, after merging it in: the eight build at -O0 -g and print their #| lines, the -O1, -O2 and -O3 assembly of tcp_poll, process_run and udp_truncate is still byte-identical, and gates/repo.ts passes 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, -O2 and -O3 assembly is byte-identical there too, and -O0 already built there (24 argument registers), so this makes -O0 the same on every target. macOS not tested. The device code is untouched: the #if sits in the host branch.

aldeni and others added 2 commits September 25, 2026 15:15
…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]>
@Lorenzobattistela

Copy link
Copy Markdown
Collaborator

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.

@Lorenzobattistela Lorenzobattistela self-assigned this Oct 1, 2026
@aldeni

aldeni commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for looking at it. You're right about sanitizers: -O1 -g -fsanitize=address builds and runs, and that's how I run them myself. They don't need -O0, and I shouldn't have listed them.

Where -O1 -g falls short is stepping through a foreign effect's own C. The case behind this PR is a TLS client I'm writing as a foreign effect:

  • it loads OpenSSL 3 with dlopen, so a plain bend -o builds it with nothing added to the link line;
  • it drives SSL_connect, SSL_read and SSL_write on the socket's own fd;
  • on WANT_READ or WANT_WRITE it parks with io_wait_on.

Its bugs live in that C: how SSL_get_error and errno are read after a failed call, a truncation told apart from a reset, a fatal error that must not reach SSL_shutdown. The JS lane runs the .js twin, not this C, so a native debug build is the only place to step through it. With the sockets and the effect in one program, main can't build it at -O0 ("ran out of registers during register allocation in function 'WL_FID_ENTER'", clang 22.1.8).

At -O1 -g, I set a break in the read path right after SSL_read returns, while the server's bytes are still arriving. Against openssl s_server on loopback:

-O1 -g (main)                          -O0 -g (this PR)
n      = <optimized out>               n      = -1
sslerr = 2                             sslerr = 2    (SSL_ERROR_WANT_READ)
syserr = <optimized out>               syserr = 11   (EAGAIN)

#0 tls_recv_more (e=..., w=...)        #0 tls_recv_more (e=..., w=...)
#1 io_exec (e=..., w=...)              #1 tls_recv_run (e=..., f=0x7fffffffb8e0, w=...)
                                       #2 io_exec (e=..., w=...)

n is SSL_read's return and syserr the errno taken right after it. Those are the two values that tell a reset from a truncation, and the ones a bug that reads errno too late gets wrong. At -O1, both are gone, and so is the effect's own entry frame.

The same shape can be reproduced from the repo's own tests: append foreign_shared.bend's Tree, shared.make, tree.sum, wrap and both to tests/io/tcp_poll.bend, and print both(shared.make(4)) in its main before the listener.

  • On main, -O0 -g fails the same way.
  • At -O1 -g, a break in shared_make_at shows depth as <optimized out> in every caller frame, and f and w in shared_make_run.
  • foreign_shared on its own, at -O1 -g, even shows a wrong value. The same next pointer is passed down every call, and frame #0 shows it as 0x7fffffffb68c, but two frames of the backtrace show it as 0x200000000000000.

On keeping one convention:

  • The default convention is already a supported runtime. Since cd3366a, PRESERVE is empty wherever clang lacks either attribute, and cc_find takes clang 14 for a CPU build. So bend -o with clang 14–18 builds exactly what this PR gives a -O0 build.
  • A preserve_none failure like the one rp pads around shows up when clang builds the C. Per 386b7fc, "regalloc fails on any dynamic jump with 6+ defined words". That shows at -O1 and up, where this PR changes nothing: on current main, the assembly at -O1, -O2 and -O3 is byte-identical with and without it. A debug build isn't where you'd find it, and today a -O0 build of a program that size can't be made at all.

If you'd still rather a -O0 build be a deliberate choice, I can make it opt-in instead: a define that drops the attributes, so a plain -O0 build fails as it does now. And if a debug build isn't something you want to support, closing it is fine too.

@Lorenzobattistela

Copy link
Copy Markdown
Collaborator

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.

@aldeni

aldeni commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

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:

  • The conflict: main (675381e) is merged into the branch, and the -O0 note now sits in the // Dialect paragraph next to the rest of PRESERVE's story. I merged rather than rebased to avoid a force push. If you'd prefer one commit on top of main, I'm glad to squash it.
  • The description: the sanitizer lines are gone. The snippet shows the comment where it now lives, and your arm64 confirmation replaces my "not tested".

Re-checked on the merged head (Linux x86-64, clang 22.1.8):

  • the eight build at -O0 -g and print their #| lines;
  • the -O1, -O2 and -O3 assembly of tcp_poll, process_run and udp_truncate is byte-identical to main's;
  • gates/repo.ts passes 50 / 50.

Thanks again for the careful review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants