← Back to Pull requests

Keep loop-local gradients out of the while remover's carried values #4021

Closed
unknown closed this pull request without merging on Nov 25, 2026 09:51 UTC
View on GitHub
unknown unknown commented on Nov 25, 2026 07:32 UTC
Author

Fixes EnzymeAD/Reactant.jl#3842.

WhileOpEnzymeOpsRemover is meant to skip gradients created inside the loop body, but the check tested the shadowing loop variable op (the enzyme.set) instead of the while, so op->isProperAncestor(initOp) was always false. Such a gradient then became a loop-carried value: an enzyme.get was hoisted before the loop, where its enzyme.init does not dominate.

Since #3971, the reverse body of a checkpointed loop creates these gradients. Most are simplified away before the while remover runs, but one updated inside a stablehlo.if survives. That is what happens with the Oceananigans SplitRungeKutta3 model in the issue when the trip count is not a multiple of the checkpoint period.

The fix skips loop-local gradients in two places: when collecting the values to carry across iterations, and when hoisting gets out of the body.

Testing:

  • New while_local_gradient_removal.mlir fails with the issue's dominance error without the fix.

  • The issue's reproducer, dumped unoptimized with @code_hlo and run through the default raise-first pipeline, now compiles for Nt = 5. Nt = 4 still compiles.

  • The diffrules/{stablehlo,enzymexla}, remove and while lit tests pass.
  • 🤖 Generated with Claude Code

    https://claude.ai/code/session_01DHpsQ6Q1tXwBWWzLEFkpS3

    Diff: 168 lines (+126 / -42) across 2 files Reviewer: wsmoses Review duration: 138m
    All Vouch review confidence checks passed
    Residual risk 0.14 = Change Risk (0.35) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.35 · 168 lines diff across 2 files
    0.35
    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.307
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #4021"
    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: 35.1% (MEDIUM) · Higher = larger blast radius
    35.1%
    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: 13.9% · Safe to merge
    13.9%