Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ffcaeca

Browse files
authored
[CodeGen] Fix partial phi input removal in TailDuplicator. (#158265)
Tail duplicator removes the first PHI income from the predecessor basic block, while it should remove all operands for this block. PHI instructions happen to have duplicated values for the same predecessor block: * `UnreachableMachineBlockElim` assumes that PHI instruction might have duplicates: https://github.com/llvm/llvm-project/blob/7289f2cd0c371b2539faa628ec0eea58fa61892c/llvm/lib/CodeGen/UnreachableBlockElim.cpp#L160 * `AArch64` directly states that PHI instruction might have duplicates: https://github.com/llvm/llvm-project/blob/7289f2cd0c371b2539faa628ec0eea58fa61892c/llvm/lib/Target/AArch64/AArch64ConditionalCompares.cpp#L244 * And `Hexagon`: https://github.com/llvm/llvm-project/blob/7289f2cd0c371b2539faa628ec0eea58fa61892c/llvm/lib/Target/Hexagon/HexagonConstPropagation.cpp#L844 We have caught the bug on custom out-of-tree backend. `TailDuplicator` should remove all operands corresponding to the removing block. Please note, that bug likely does not affect in-tree backends, because: * It happens only in scenario of **partial** tail duplication (i.e. tail block is duplicated in some predecessors, but not in all of them) * It happens in **Pre-RA** tail duplication only (Post-RA does not contain PHIs, obviously) * The only backend (I know) uses Pre-RA tail duplication is X86. It uses tail duplication via `early-tailduplication` pass which declines partial tail duplication via `canCompletelyDuplicateBB` check, because it uses `TailDuplicator::tailDuplicateBlocks` public API. So, bug happens only in the case of pre-ra partial tail duplication if backend uses `TailDuplicator::tailDuplicate` public API directly. That's why I can not add reproducer test for in-tree backends.
1 parent 1180c2c commit ffcaeca

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

llvm/lib/CodeGen/TailDuplicator.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,14 @@ void TailDuplicator::processPHI(
375375
if (!Remove)
376376
return;
377377

378-
// Remove PredBB from the PHI node.
379-
MI->removeOperand(SrcOpIdx + 1);
380-
MI->removeOperand(SrcOpIdx);
378+
// MI might have multiple entries for PredBB. Need to remove them all.
379+
for (unsigned N = MI->getNumOperands(); N > 2; N -= 2) {
380+
if (MI->getOperand(N - 1).getMBB() == PredBB) {
381+
MI->removeOperand(N - 1);
382+
MI->removeOperand(N - 2);
383+
}
384+
}
385+
381386
if (MI->getNumOperands() == 1 && !TailBB->hasAddressTaken())
382387
MI->eraseFromParent();
383388
else if (MI->getNumOperands() == 1)

0 commit comments

Comments
 (0)