Skip to content

Commit

Permalink
Fix phi predecessor removal if given placeholder (rust-lang#610)
Browse files Browse the repository at this point in the history
* Fix phi predecessor removal if given placeholder

* Fix LLVM 7
  • Loading branch information
wsmoses authored Apr 13, 2022
1 parent 7961e78 commit 2cafc87
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion enzyme/Enzyme/EnzymeLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3602,7 +3602,30 @@ Function *EnzymeLogic::CreatePrimalAndGradient(
}

for (auto sucBB : toRemove) {
sucBB->removePredecessor(newBB);
if (sucBB->empty() || !isa<PHINode>(sucBB->begin()))
continue;

SmallVector<PHINode *, 2> phis;
for (PHINode &Phi : sucBB->phis()) {
phis.push_back(&Phi);
}
for (PHINode *Phi : phis) {
unsigned NumPreds = Phi->getNumIncomingValues();
if (NumPreds == 0)
continue;
Phi->removeIncomingValue(newBB);

// If we have a single predecessor, removeIncomingValue may have
// erased the PHI node itself.
if (NumPreds == 1)
continue;

// Try to replace the PHI node with a constant value.
if (Value *PhiConstant = Phi->hasConstantValue()) {
Phi->replaceAllUsesWith(PhiConstant);
Phi->eraseFromParent();
}
}
}

SmallVector<Instruction *, 2> toerase;
Expand Down

0 comments on commit 2cafc87

Please sign in to comment.