Open
Conversation
wsmoses
reviewed
Apr 28, 2026
| @@ -8659,6 +8659,27 @@ void GradientUtils::eraseFictiousPHIs() { | |||
| for (auto pair : phis) { | |||
| auto pp = pair.first; | |||
| if (pp->getNumUses() != 0) { | |||
Member
There was a problem hiding this comment.
we definitely want a test case for this
Collaborator
Author
There was a problem hiding this comment.
@wsmoses I added a IR test here #2700 (comment)
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.
This is to fix the issue #2700
The crash was in [GradientUtils::eraseFictiousPHIs()]— a fictiousPHI created for a [malloc] with !enzyme_fromstack metadata still had live uses (GEPs and loads accessing the 3 float elements of a [std::array<float,3>], causing the assertion [pp->getNumUses() == 0] to fire.
Root cause: When Enzyme decided the primal allocation wasn't needed in reverse mode, it replaced the malloc call with a fictiousPHI placeholder. However, [restoreCache()](which normally resolves fictiousPHIs for enzyme_fromstack mallocs by creating stack allocations) didn't process this particular PHI — likely because the allocation was in an inner function (InverseQuery) and the cache restoration logic didn't cover this case.
Fix: In [eraseFictiousPHIs()], before asserting that a fictiousPHI has no uses, check if the original instruction was a [malloc] with enzyme_fromstack metadata. If so, create a proper stack allocation (alloca) in the entry block with the correct size and alignment, and replace the PHI's uses with it. This mirrors the same enzyme_fromstack handling done elsewhere in the codebase (e.g., at GradientUtils.cpp).