Keep the address of a replayed store needed in the reverse pass #3946
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
vchuravy
<!-- 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(MITgcmtutorial_tracer_adjsens, inpackages_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 ofcomputeMinCachefrom the answers ofis_value_needed_in_reverseitself, 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_reverseclaims 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.calculateUnusedValuesInFunctionuses one seen map for all values and decides from it which stores are replayed, so it could disagree withallocationsToBeRematerialized: 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 inhandleKnownCallDerivatives. 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'sctrl_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_adjsensbuilds 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_