← Back to Pull requests

Keep large subcall tapes in heap memory #3949

Closed
unknown closed this pull request without merging on Dec 03, 2026 20:26 UTC
View on GitHub
unknown unknown commented on Dec 01, 2026 16:23 UTC
Author

<!-- claude-projects-attribution: {"github_login":"vchuravy"} -->
_Requested by Valentin · project thread_

The augmented forward pass returns the tape of a non-recursive callee by value, and that tape is nested by value into the tape of every enclosing call. In MITgcm's offline_exf_seaice, augmented_forward_step_ ended up returning a struct with ~69.6k scalar fields (8 levels of nesting). That is more than the 65535 operands a SelectionDAG node can hold, so llc asserts; other configurations stall optimizing the resulting insertvalue chains.

With -enzyme-max-tape-fields=N, a tape with more than N scalar fields (struct leaves, array and fixed-vector elements counted) is kept in heap memory, the way the tape of a recursive function already is:

  • CreateAugmentedPrimal treats such a tape like a recursive one: it is allocated with CreateAllocation and the augmented function returns the pointer.

  • At a subcall in reverse mode, the caller caches that pointer and passes it to the subcall's reverse pass with forceAnonymousTape, which loads and frees it. The tape is never loaded into SSA in the caller. ForwardModeSplit keeps the existing load.

  • Top-level split mode with caller-provided tape memory (__enzyme_augmentfwd / __enzyme_reverse with enzyme_allocated) stores the pointer in that memory, and the reverse is requested with an anonymous tape. This drops assert(!aug->tapeType), which also fired for recursive functions there.
  • Because the limit applies per tape, every tape that is still returned by value has at most N scalar fields. The default, -1, never moves a tape to the heap, so behavior is unchanged unless the flag is set. MITgcm used N = 1024.

    This is independent of #3280, which passes already-heap (recursive) tapes by pointer above a configurable threshold. Both touch the same block in AdjointGenerator.h, so whichever lands second needs a small rebase.

    For Enzyme.jl, such a tape is reported like a recursive one (underlying tape type set, i.e. EnzymeTapeToLoad).

    Testing


  • New test/Enzyme/ReverseMode/largetape.ll: with -enzyme-max-tape-fields=1024, a 1280-scalar tape in a loop goes through the heap, a small tape is still returned by value, plus top-level split mode; lli checks the derivative values. A third RUN line checks that without the flag the tape is returned by value.

  • LLVM 15 and 16 lit (test/Enzyme): all pass except ReverseMode/cache_for_reverse_arg.ll, which also fails on main.

  • MITgcm offline_exf_seaice (full LTO, with the local study fixes): builds in 10 min (Enzyme opt 36 → 7 min, llc no longer asserts). The gradient matches TAF exactly for the default and thsice inputs; obcs is 7.5e-3 from TAF and 1.9e-4 from finite differences.
  • 🤖 Generated with Claude Code

    Diff: 367 lines (+275 / -92) across 4 files Reviewer: vchuravy Review duration: 3122m
    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 · 367 lines diff across 4 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.353
    Shallow Review Depth shallow_review_depth
    0.289
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #3949"
    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: 47.7% (MEDIUM) · Higher = larger blast radius
    47.7%
    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: 18.9% · Safe to merge
    18.9%