← Back to Pull requests

Keep the address of a replayed store needed in the reverse pass #3946

Closed
unknown closed this pull request without merging on Dec 03, 2026 16:12 UTC
View on GitHub
unknown unknown commented on Dec 01, 2026 09:40 UTC
Author

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

Follow-up to #3890.

A store into an allocation that is rematerialized in the reverse pass is replayed there, and the replay looks up the address it writes to as well as the stored value. When an index of that address is loaded from another allocation, that allocation has to be kept for the reverse pass too. Enzyme lost track of this in two ways, and failed with Illegal replace ficticious phi (MITgcm tutorial_tracer_adjsens, in packages_init_variables) or, in the cases found while looking into it, silently dropped a replayed store.

The first is that #3890 counted the address as needed only when the allocation was already in allocationsToBeRematerialized. That set is filled at the end of computeMinCache from the answers of is_value_needed_in_reverse itself, so the min cut asks with the set empty and the loop that fills it asks with part of it. In the MITgcm case the index never enters the min-cut graph, the cut caches the comparison that uses it instead, and the index load is marked as an unnecessary intermediate. With two allocations the result depends on which one comes first. The first commit asks for the address under the same condition as the stored value: when a load of the allocation, or the allocation itself, is needed. This is still "only for an allocation we rematerialize", but computed directly rather than read from a set that is not complete yet. The second commit removes the lookup of the set from the predicate, which is redundant after that.

The second is older and independent of the set. is_value_needed_in_reverse claims a value unneeded while it looks for a user that needs it, and while an allocation is under that claim the operands of the stores into it are answered as unneeded. Those answers stayed in the seen map after the allocation was found needed. The address of a store is always derived from its allocation, so asking about the allocation first could record the address, and an index loaded from another allocation with it, as unneeded for good. calculateUnusedValuesInFunction uses one seen map for all values and decides from it which stores are replayed, so it could disagree with allocationsToBeRematerialized: the reverse pass recreated an allocation without replaying the store into it and loaded the index from uninitialized memory, or hit the !unnecessaryValues.count(&call) assertion in handleKnownCallDerivatives. The third commit drops the unneeded answers recorded during the query of a rematerializable allocation once that query finds it needed, so they are computed again.

Tests:

  • test/Enzyme/ReverseMode/remat_store_index.ll: a second case, reduced from MITgcm's module (READ iPtr; if (iPtr > 0) igen(iPtr) = 0 else if (igen(1) > 0) iPtr = 0).

  • test/Fortran/ReverseMode/remat_store_read_index.f90: the same READ-index pattern from MITgcm's ctrl_map_ini_genarr.F, through both the opt and the flang plugin routes.

  • test/Enzyme/ReverseMode/remat_store_index_order.ll: two allocations where the index of the store into the second is loaded from the first (fails on main with the ficticious phi), the same with the address of the store placed before the load that makes its allocation needed (main and the first two commits drop the store into the first allocation), and a chain of three allocations (assertion with the first two commits).
  • Generated code is unchanged on the existing ReverseMode, ReverseModeVector and ForwardModeSplit lit inputs. With this PR, MITgcm tutorial_tracer_adjsens builds against main and its gradient matches TAF's (relative difference at most 2.5e-13).

    https://claude.ai/code/session_01HgDfydRieN5tnC5sHP44xW

    ---
    _Generated by Claude Code_

    Diff: 484 lines (+363 / -121) across 6 files Reviewer: vchuravy Review duration: 3271m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 484 lines diff across 6 files
    0.52
    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.370
    Shallow Review Depth shallow_review_depth
    0.289
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #3946"
    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: 52.3% (MEDIUM) · Higher = larger blast radius
    52.3%
    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: 20.7% · Safe to merge
    20.7%