Skip to content

Fix eraseFictiousPHIs crash due to incorrect Seen map pre-seeding in allocation handling#2781

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-enzyme-crash-issue
Draft

Fix eraseFictiousPHIs crash due to incorrect Seen map pre-seeding in allocation handling#2781
Copilot wants to merge 2 commits intomainfrom
copilot/fix-enzyme-crash-issue

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 9, 2026

GradientUtils::eraseFictiousPHIs() crashes with assert(pp->getNumUses() == 0) when differentiating certain CUDA code. The crash was introduced by commit be435edf which added unnecessaryIntermediates to the Seen map initialization in the allocation handling path of CallDerivatives.cpp.

Root cause

When an allocation &call appears in both knownRecomputeHeuristic and unnecessaryIntermediates, the loop in handleKnownCallDerivatives pre-seeds Seen[{&call, Primal}] = false. The subsequent is_value_needed_in_reverse(gutils, &call, Mode, Seen, ...) call immediately short-circuits at the memoization check:

if (seen.find(idx) != seen.end())
    return seen[idx];  // returns false — skips real analysis entirely

This makes primalNeededInReverse = false even when the allocation's result is genuinely needed in the reverse pass, so a fictitious PHI is emitted in place of a cached value. That PHI retains users in the generated reverse code, triggering the assertion in eraseFictiousPHIs.

Fix

After building Seen, erase &call's own entry before calling is_value_needed_in_reverse:

Seen.erase(UsageKey(&call, QueryType::Primal));

Other pre-seeded entries (other unnecessary intermediates) still propagate correctly through the dependency chain and help the analysis determine &call's neededness — only the self-seeding that short-circuits the query is removed.

Copilot AI linked an issue Apr 9, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Fix enzyme crash for fictitious PHIs in CUDA code Fix eraseFictiousPHIs crash due to incorrect Seen map pre-seeding in allocation handling Apr 9, 2026
Copilot AI requested a review from minansys April 9, 2026 20:30
@minansys
Copy link
Copy Markdown
Collaborator

The fix did not resolve the issue, need a test case and 2nd round fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

eraseFictiousPHIs crash

2 participants