@@ -1620,8 +1620,8 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
1620
1620
spv::LoopControlMask LoopControl = getLoopControl (Branch, Parameters);
1621
1621
1622
1622
if (Branch->isUnconditional ()) {
1623
- // Usually for "for" and "while" loops llvm.loop metadata is attached to
1624
- // an unconditional branch instruction.
1623
+ // Usually, "for" and "while" loops llvm.loop metadata is attached to an
1624
+ // unconditional branch instruction.
1625
1625
if (LoopControl != spv::LoopControlMaskNone) {
1626
1626
// SuccessorTrue is the loop header BB.
1627
1627
const SPIRVInstruction *Term = SuccessorTrue->getTerminateInstr ();
@@ -1642,16 +1642,23 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
1642
1642
}
1643
1643
return mapValue (V, BM->addBranchInst (SuccessorTrue, BB));
1644
1644
}
1645
- // For "do-while" and in some cases for "for" and "while" loops llvm.loop
1646
- // metadata is attached to a conditional branch instructions
1645
+ // For "do-while" ( and in some cases, for "for" and "while") loops,
1646
+ // llvm.loop metadata is attached to a conditional branch instructions
1647
1647
SPIRVLabel *SuccessorFalse =
1648
1648
static_cast <SPIRVLabel *>(transValue (Branch->getSuccessor (1 ), BB));
1649
1649
if (LoopControl != spv::LoopControlMaskNone) {
1650
1650
Function *Fun = Branch->getFunction ();
1651
1651
DominatorTree DomTree (*Fun);
1652
1652
LoopInfo LI (DomTree);
1653
1653
for (const auto *LoopObj : LI.getLoopsInPreorder ()) {
1654
- // Check whether SuccessorFalse or SuccessorTrue is the loop header BB
1654
+ // Check whether SuccessorFalse or SuccessorTrue is the loop header BB.
1655
+ // For example consider following LLVM IR:
1656
+ // br i1 %compare, label %for.body, label %for.end
1657
+ // <- SuccessorTrue is 'for.body' aka successor(0)
1658
+ // br i1 %compare.not, label %for.end, label %for.body
1659
+ // <- SuccessorTrue is 'for.end' aka successor(1)
1660
+ // meanwhile the true successor (by definition) should be a loop header
1661
+ // aka 'for.body'
1655
1662
if (LoopObj->getHeader () == Branch->getSuccessor (1 ))
1656
1663
// SuccessorFalse is the loop header BB.
1657
1664
BM->addLoopMergeInst (SuccessorTrue->getId (), // Merge Block
0 commit comments