Skip to content

fix the fictiousPHI crash issue#2793

Open
minansys wants to merge 1 commit intomainfrom
mixu/fix-eraseFictiousPHI2
Open

fix the fictiousPHI crash issue#2793
minansys wants to merge 1 commit intomainfrom
mixu/fix-eraseFictiousPHI2

Conversation

@minansys
Copy link
Copy Markdown
Collaborator

@minansys minansys commented Apr 23, 2026

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).

@@ -8659,6 +8659,27 @@ void GradientUtils::eraseFictiousPHIs() {
for (auto pair : phis) {
auto pp = pair.first;
if (pp->getNumUses() != 0) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we definitely want a test case for this

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wsmoses I added a IR test here #2700 (comment)

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.

2 participants