Open
Conversation
This was referenced Feb 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix nested inactive-outer / active-inner predicate taping bug (#2629)
Summary
This PR fixes an incorrect reverse CFG reconstruction in Enzyme for nested branch patterns where an inner active predicate is only computed under an outer inactive guard (e.g.,
if (fan) { ... if (radius > 0) ... }withfanpassed asenzyme_const). In such cases, Enzyme could materialize / read the inner predicate unconditionally during reverse CFG reconstruction, leading to undef/poison reads, wrong control flow, and incorrect gradients (especially at-O0/-O1).The fix ensures the inner predicate is only materialized under the outer guard by using a staging block in reverse CFG reconstruction and properly registering that synthetic block in
reverseBlockToPrimal. As an additional safety net, i1 predicate caches are zero-initialized to represent the correct “not executed” default.Motivation / Problem
fix the issue #2629
Enzyme’s 3-target merge handling in
GradientUtils::branchToCorrespondingTarget()included a fast-path that:cond1) and the second split predicate (cond2) eagerly, andIn the nested-guard scenario from #2629,
cond2is only defined/taped when the outer guard is true. Eagerly materializingcond2on paths where the outer guard is false can cause reads of uninitialized cached predicates and lead to incorrect reverse control flow.Approach
Key ideas
cond2)stagingblock and only computecond2insidestaging, which is reached only whencond1holds.lookupM()expects reverse-only blocks to be present inreverseBlockToPrimal.stagingto the appropriate primal/original block to satisfy invariants and avoid assertions.replacePHIsmodereplacePHIs != nullptrpath cannot introduce control flow. Attempting to express a 3-way split purely via PHI rewriting can still force eagercond2materialization.false, preventing reverse from taking the inner path spuriously.Changes
Functional changes
GradientUtils::branchToCorrespondingTargetcond1to astagingblock when needed,cond2insidestaging, andstaginginreverseBlockToPrimalto avoidoriginalForReverseBlockassertions whenlookupM()is called in that block.replacePHIs != nullptr, falling back to the safe path.CacheUtility::createCacheForScopei1), force zero initialization so that missing stores do not leave the cache uninitialized.Tests
enzyme_const),Reproduction (before)
A minimal reproducer is a kernel with:
if (fan) { ... radius = sqrt(...); if (radius > 0) ... }fanpassed asenzyme_constfvecregardless offanObserved behavior:
-O0/-O1and/or crashes/assertions during Enzyme transformation due to reverse CFG reconstruction reading an uninitialized predicate.Result (after)
-O0and-O1.originalForReverseBlockwhen building reverse CFG.Performance / Risk Assessment
replacePHIs != nullptr.i1caches is low risk and aligns with the correct semantic default (false) when a predicate was never computed.Checklist
-O0and-O1