← Back to Pull requests

[STF] exception_policy: catch a declined exception once and inline the composite dispatch #11889

Closed
unknown closed this pull request without merging on Oct 23, 2026 22:48 UTC
View on GitHub
unknown unknown commented on Oct 06, 2026 08:29 UTC
Author

Description

Stacked on #11888 (the first commit here is that PR's commit; only the second is new).

Two changes to the | and composites in exception_policy.cuh, both found by measuring a guarded call against the hand-written try/catch with the same behaviour.

1. A declined exception is caught once. A hook declines with throw;. The composite then caught everything and re-threw inside a nested try to recover the std::exception funnel pointer for the next arm: two complete unwinds per declined arm. The composite's own try now has typed clauses (const std::exception&, ...) that recover the pointer directly. Same semantics, one unwind per declined arm.
2. Composite dispatch is inlined. GCC 13 and Clang 18 both left __policy_or::operator() and __interpret_answer as outlined functions. One outlined function on the failure path forces the policy aggregate, the source_location, and the addresses of the callable's by-reference captures into memory before the guarded call, on the path where nothing throws. The composite hooks (|,
, &, catch_only, catch_exactly) and the answer interpreter are now _CCCL_FORCEINLINE, and the | body is lambda-free so there is something small enough to inline.

Measured

Neoverse-V2, -O2, GCC 13.3. Median ± standard deviation, at 0.01 ns / 1 ns resolution (the stddev is repeatability, not accuracy; two independent harnesses disagree by up to 0.3 ns on the returning path), over 15 Google Benchmark repetitions (each repetition ≥ 0.5 s, about 330 M calls on the not-taken path and 110–460 k on the taken path, after a discarded warm-up), pinned to one core. Library guard vs hand-written try/catch with identical behaviour; the callee alone costs 2.1 ns and one throw caught once costs ~1.5 µs here. Full protocol, harnesses and raw outputs: Exception-Sinks benchmarks/overhead.

| guard | returns, before → after (ns) | hand (ns) | throws, before → after (µs) | hand (µs) |
|---|---|---|---|---|
| catch_only<E1>(subst(1)) \| subst(0) | 3.83 ± 0.01 → 2.05 ± <0.01 | 2.08 ± <0.01 | 1.539 ± <0.001 → 1.527 ± <0.001 | 1.506 ± <0.001 |
| 2 arms + fallback, match on 2nd | 6.61 ± 0.01 → 2.17 ± <0.01 | 2.09 ± 0.01 | 4.012 ± 0.001 → 2.767 ± 0.001 | 1.329 ± 0.001 |
| 4 arms + fallback, match on 4th | 6.51 ± <0.01 → 2.17 ± <0.01 | 2.08 ± <0.01 | 10.250 ± 0.004 → 6.180 ± 0.001 | 1.503 ± <0.001 |
| when(pred, subst(1)) \| subst(2), pred false | 7.53 ± 0.01 → 2.08 ± <0.01 | 2.07 ± 0.01 | 3.991 ± 0.002 → 2.859 ± 0.001 | 1.341 ± 0.001 |

Not-taken path instruction count (prologue to ret): 40–46 → 5–7 (GCC), 24–28 → 7–9 (Clang); hand-written is 5 and 7. No outlined dispatch functions remain in the object file. Single-rule guards were already identical to hand-written code and are unchanged (2.08 ± 0.01 vs 2.09 ± 0.04 ns). Clang's four-arm alternation keeps a third callee-saved register save (3.56 ± <0.01 vs 2.28 ± <0.01 ns): clang hoists the shared __cxa_begin_catch above the clause-selector test, so one selector per nesting level survives a call. Not reachable from the header. -O3 alone changed nothing; -fno-stack-protector alone removed 16 of GCC's 40 instructions.

Code size moves the other way: inlining duplicates the arms' clauses into each landing pad, so GCC's four-arm alternation grows from 400 to 920 bytes (hand-written: 112). That code is on the failure path only.

The unwind that remains per declined arm (1.240 ± 0.001 µs with two arms, 1.551 ± 0.001 with four) is the hook's own throw;, which is the library's decline protocol.

Verification

  • The header's 22 embedded UNITTESTs compile and pass with nvcc 13.4 (-DUNITTESTED_FILE=...).

  • The Exception Sinks paper's 24 checks and its figure examples pass with GCC 13.3 and Clang 18.1.

  • clang-format 22.1.5 (the pre-commit pin) applied.

  • Not verified here: cudax/test/stf/utility/exception_policy_c_headers.cu and examples/stf/exception_policies_reciprocal.cu fail to build with the local nvcc 13.4 on the unpatched #11888 head as well (cudafe stub errors unrelated to this header), so CI needs to cover them.
  • Checklist


  • New or existing tests cover these changes.

  • The documentation is up to date with these changes.
  • 🤖 Generated with Claude Code

    Diff: 545 lines (+408 / -137) across 6 files Reviewer: andralex Review duration: 25339m
    All Vouch review confidence checks passed
    Residual risk 0.19 = Change Risk (0.48) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.48 · 545 lines diff across 6 files
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.377
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #11889"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 48% (MEDIUM) · Higher = larger blast radius
    48%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19.0% · Safe to merge
    19.0%
    Repository