Skip to content

Exceptions: error-return model (global slot checked after may-throw calls), unwinder removed - #670

Merged
blacktrs merged 11 commits into
mainfrom
exc-error-return
Nov 13, 2026
Merged

blacktrs merged 11 commits into
mainfrom
exc-error-return

Conversation

@blacktrs

@blacktrs blacktrs commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Replaces zero-cost (Itanium DWARF) exceptions with an error-return model: one process-wide slot @__mc_exc holds the Throwable in flight, and every call that may throw is followed by a test of it. Design: docs/design/error-return-exceptions.md.

Why

A throw cost 1.35 µs on Linux and 3.1 µs on macOS (php: 0.3–0.6 µs), because it went through the personality routine, the LSDA parser and a two-phase unwind, plus a malloc'd exception object per throw. PHP code throws far more often than C++ code does, so the zero-cost trade-off was the wrong one for us.

What changes

  • EhLowerIr (new): a final-text IR pass that lowers invoke / landingpad / resume into plain calls followed by a slot check. A fixpoint over the IR call graph decides which calls may throw: indirect calls and @__mc_throw may throw; a @manticore_* callee is nothrow only if the nothrow summary says so; C callees never throw. Emitters are unchanged. Lowering runs before PruneIr, so @__mc_uncaught survives.
  • The unwinder is gone: personality, LSDA, the Darwin fast path from Darwin: 6x faster throw/catch; @serial test cases build in parallel #649, typeinfo, the _Unwind / unw_ declares and -static-libgcc. The slot holds the Throwable itself.
  • Memory ABI v26: the exception header constants are removed, so the compiler refuses to link a v25 stdlib.
  • FFI callbacks: a PHP callable passed to C through fn_to_ptr that throws now has its exception deferred until the C call returns. The C frames do not unwind. FFI wrappers poll the slot only when some ccallable function may throw. The new semantics are written up in docs/ffi.md.
  • MANTICORE_EH_ASSERT=1: a debug build mode. After every call judged nothrow it checks the slot and traps with EH ASSERT: exception set after nothrow call to @f. A wrong "nothrow" verdict is now a correctness bug, not just a missed optimisation, and this mode is how we catch one.
  • prelude/io_poll.php: the poll binding now returns C int. It had conflicted with Runtime\Libc\sys_poll.

Evidence

  • Before implementation, a spike of the same model on macOS passed the full AOT suite: 2820 / 0.
  • On this rebased branch (macOS), after a self-host bin/build, these -k sets are green: exc_ret_, throw, catch, finally, fiber, generator, uncaught, ffi, exception, destruct.
  • New cases: exc_ret_{deep_propagate,phi_after_check,dtor_throws_during_unwind,ffi_callback_defers,uncaught_after_dtors}. New benchmark: bench/cases/throw_catch.php.
  • fiber_throw binary size: 7.25 → 6.91 MB.

Not yet done

  • Full suite on this exact tree, the MANTICORE_EH_ASSERT=1 suite, difftest and the Linux gate. This PR is up partly to get CI's answer.
  • Throw latency and bench/self-build timings against main have not been measured; the machine was too loaded. In the spike, fiber_switch (+14.5%) and net_lines (+38%) regressed. A nothrow string-release twin is planned to fix that: today __mir_rc_release_str may throw because it can destroy a Generator.
  • Follow-up: -fno-asynchronous-unwind-tables, which would drop the ~424 KB of __eh_frame that remains.

Fixes #648

Update

  • Every function is now nounwind, so __eh_frame is gone. The nothrow string release __mir_rc_release_s is used at sites that are strings by construction. MANTICORE_EH_WHY=1 says why a helper may throw.

  • Measured on macOS under load:

    • one throw costs ~0.05 µs (php 8.5: ~0.42 µs; main: 5–8 µs);
    • bench corpus without throw_catch: −2.8%;
    • happy path in instructions retired: +2–6% on json/bcmath/fiber, +8% generator_yield.
    • Details are in the design doc, "As built".
  • Final-review fixes:

    • an exception handler that throws now prints its fatal (before: silent abort);
    • check numbering is per function, so shared-generics bodies stay identical;
    • C callbacks go through a trampoline that ignores a pending exception and answers 0;
    • a shutdown function or ob callback that throws at exit reaches the handler, as in php;
    • ob_* functions close the level when the handler throws;
    • the FFI wrapper text no longer depends on the module.
  • MANTICORE_EH_ASSERT=1 is an opt-in debug build (documented in AGENTS.md), not part of the normal build.

… slot when a C-callable PHP function may throw
…th, exception object, libgcc_eh link (memory ABI v26)
…-only releases skip the exception check via nothrow __mir_rc_release_s; MANTICORE_EH_WHY=1
…s go through a trampoline that ignores a pending exception; per-function check numbering keeps shared bodies identical
…eaches the handler (else the fatal); ob_* close the level when a handler throws; FFI wrapper text module-independent; runFile fails on a short write; a frame in __mir_str_free traps
@blacktrs
blacktrs merged commit 229938c into main Nov 13, 2026
102 of 104 checks passed
@blacktrs
blacktrs deleted the exc-error-return branch November 13, 2026 13:36
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.

Throwing and catching an exception is ~44× slower than php (20 µs vs 0.47 µs)

1 participant