Repository navigation
Exceptions: error-return model (global slot checked after may-throw calls), unwinder removed - #670
Merged
Merged
Conversation
…calls) to replace zero-cost unwinding
… after may-throw calls (MANTICORE_EH=ret)
… slot when a C-callable PHP function may throw
…call judged nothrow
…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
… 0.42 µs, happy-path cost
…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
force-pushed
the
exc-error-return
branch
from
November 13, 2026 11:52
e2b5f3b to
2fe52dd
Compare
This was referenced Nov 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replaces zero-cost (Itanium DWARF) exceptions with an error-return model: one process-wide slot
@__mc_excholds 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 lowersinvoke/landingpad/resumeinto plain calls followed by a slot check. A fixpoint over the IR call graph decides which calls may throw: indirect calls and@__mc_throwmay throw; a@manticore_*callee is nothrow only if the nothrow summary says so; C callees never throw. Emitters are unchanged. Lowering runs beforePruneIr, so@__mc_uncaughtsurvives._Unwind/unw_declares and-static-libgcc. The slot holds the Throwable itself.fn_to_ptrthat 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 indocs/ffi.md.MANTICORE_EH_ASSERT=1: a debug build mode. After every call judged nothrow it checks the slot and traps withEH 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: thepollbinding now returns Cint. It had conflicted withRuntime\Libc\sys_poll.Evidence
bin/build, these-ksets are green:exc_ret_, throw, catch, finally, fiber, generator, uncaught, ffi, exception, destruct.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_throwbinary size: 7.25 → 6.91 MB.Not yet done
MANTICORE_EH_ASSERT=1suite, difftest and the Linux gate. This PR is up partly to get CI's answer.fiber_switch(+14.5%) andnet_lines(+38%) regressed. A nothrow string-release twin is planned to fix that: today__mir_rc_release_strmay throw because it can destroy a Generator.-fno-asynchronous-unwind-tables, which would drop the ~424 KB of__eh_framethat remains.Fixes #648
Update
Every function is now
nounwind, so__eh_frameis gone. The nothrow string release__mir_rc_release_sis used at sites that are strings by construction.MANTICORE_EH_WHY=1says why a helper may throw.Measured on macOS under load:
Final-review fixes:
ob_*functions close the level when the handler throws;MANTICORE_EH_ASSERT=1is an opt-in debug build (documented in AGENTS.md), not part of the normal build.