Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions enzyme/Enzyme/GradientUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6725,6 +6725,24 @@ Value *GradientUtils::invertPointerM(Value *const oval, IRBuilder<> &BuilderM,
which->addIncoming(val, cast<BasicBlock>(getNewFromOriginal(
phi->getIncomingBlock(i))));
}
// Inverting an incoming value can emit instructions into this block
// above `which` (e.g. materializing the shadow of a value defined in
// the header), which leaves the phis no longer contiguous at the block
// start and produces IR the verifier rejects. Restore the invariant.
{
BasicBlock *PB = which->getParent();
SmallVector<PHINode *, 4> phis;
for (auto &I : *PB)
if (auto *P = dyn_cast<PHINode>(&I))
phis.push_back(P);
Instruction *at = &*PB->begin();
for (auto *P : phis) {
if (P != at)
P->moveBefore(at);
else
at = P->getNextNode();
}
}
return which;
}
}
Expand Down
Loading