Skip to content

Commit 2cafc87

Browse files
authored
Fix phi predecessor removal if given placeholder (rust-lang#610)
* Fix phi predecessor removal if given placeholder * Fix LLVM 7
1 parent 7961e78 commit 2cafc87

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

enzyme/Enzyme/EnzymeLogic.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3602,7 +3602,30 @@ Function *EnzymeLogic::CreatePrimalAndGradient(
36023602
}
36033603

36043604
for (auto sucBB : toRemove) {
3605-
sucBB->removePredecessor(newBB);
3605+
if (sucBB->empty() || !isa<PHINode>(sucBB->begin()))
3606+
continue;
3607+
3608+
SmallVector<PHINode *, 2> phis;
3609+
for (PHINode &Phi : sucBB->phis()) {
3610+
phis.push_back(&Phi);
3611+
}
3612+
for (PHINode *Phi : phis) {
3613+
unsigned NumPreds = Phi->getNumIncomingValues();
3614+
if (NumPreds == 0)
3615+
continue;
3616+
Phi->removeIncomingValue(newBB);
3617+
3618+
// If we have a single predecessor, removeIncomingValue may have
3619+
// erased the PHI node itself.
3620+
if (NumPreds == 1)
3621+
continue;
3622+
3623+
// Try to replace the PHI node with a constant value.
3624+
if (Value *PhiConstant = Phi->hasConstantValue()) {
3625+
Phi->replaceAllUsesWith(PhiConstant);
3626+
Phi->eraseFromParent();
3627+
}
3628+
}
36063629
}
36073630

36083631
SmallVector<Instruction *, 2> toerase;

0 commit comments

Comments
 (0)