← Back to Pull requests

Replace local global shadows once a shadow global exists #3924

Closed
unknown closed this pull request without merging on Nov 25, 2026 10:20 UTC
View on GitHub
unknown unknown commented on Nov 24, 2026 14:21 UTC
Author

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

Stacked on #3920 (the revert of #3883). This implements the resolution suggested in https://github.com/EnzymeAD/Enzyme/pull/3883#issuecomment-7128648122.

What

invertPointerM keeps giving a global without enzyme_shadow a zeroed local alloca as its shadow when no call in the function may touch it. EnzymeLogic now records each such alloca and the memset that zeroes it, keyed by the global and vector lane. When a shadow global is later created for that global (in invertPointerM or GetOrCreateShadowConstant), every recorded local shadow is replaced by it and its zeroing memset is erased. The alloca itself is left for later cleanup, since its function may still be being differentiated.

Why

A function differentiated as a callee only checks its own calls. In the restored global-activity-callee-global-shadow.ll, @f stores to @g and calls @leaf, which reads it. @diffeleaf is created first, with a local shadow for @g (it makes no calls), and @diffef then creates @g_shadow and reads the adjoint from it, so the gradient was 0. Now @diffeleaf accumulates into @g_shadow. Found differentiating MITgcm with LLVM flang and full LTO under -enzyme-global-activity.

Behaviour change

Derivatives of one global now share one shadow, also across calls. threadlocal_address.ll used a local shadow of @counter in the combined reverse and forward derivatives and a shadow global in split reverse; they now share @counter_shadow, so the forward call read the adjoint the reverse call left behind (3, 7, 24 instead of 3, 3, 3). The test now declares @counter_shadow and resets it between calls.

Testing

  • LLVM 16: test/Enzyme and test/Integration fail on the same set as main locally (the Integration tests don't run in that environment, plus cache_for_reverse_arg.ll, which also fails on main there).

  • MITgcm tutorial_tracer_adjsens (LLVM df0a91f6, full LTO, global activity), with the #3883 guard replaced by this change: the Enzyme gradient matches TAF at all 5 grdchk points (relative difference at most 2.5e-13), bit-identical to the run with the guard.
  • ---
    _Generated by Claude Code_

    Diff: 293 lines (+219 / -74) across 3 files Reviewer: wsmoses Review duration: 1198m
    All Vouch review confidence checks passed
    Residual risk 0.18 = Change Risk (0.45) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.45 · 293 lines diff across 3 files
    0.45
    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.340
    Shallow Review Depth shallow_review_depth
    0.289
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #3924"
    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: 44.9% (MEDIUM) · Higher = larger blast radius
    44.9%
    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: 17.7% · Safe to merge
    17.7%