Skip to content

Commit 06c013f

Browse files
committed
[MachinePipeliner] Detect a cycle in PHI dependencies early on
Abort pipelining in the below case %1 = phi i32 [ %a, %entry ], [ %3, %loop ] %2 = phi i32 [ %a, %entry ], [ %1, %loop ] %3 = phi i32 [ %b, %entry ], [ %2, %loop ]
1 parent de4aa9c commit 06c013f

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

llvm/lib/CodeGen/MachinePipeliner.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,55 @@ void MachinePipeliner::setPragmaPipelineOptions(MachineLoop &L) {
485485
}
486486
}
487487

488+
bool hasPHICycle(const MachineBasicBlock *LoopHeader, const MachineRegisterInfo &MRI) {
489+
DenseMap<unsigned, SmallVector<unsigned, 2>> PhiDeps;
490+
SmallSet<unsigned, 8> PhiRegs;
491+
492+
// Collect PHI nodes and their dependencies
493+
for (const MachineInstr &MI : *LoopHeader) {
494+
if (!MI.isPHI())
495+
continue;
496+
497+
unsigned DefReg = MI.getOperand(0).getReg();
498+
PhiRegs.insert(DefReg);
499+
500+
for (unsigned i = 1; i < MI.getNumOperands(); i += 2) {
501+
unsigned SrcReg = MI.getOperand(i).getReg();
502+
PhiDeps[DefReg].push_back(SrcReg);
503+
}
504+
}
505+
506+
// DFS to detect cycles
507+
SmallSet<unsigned, 8> Visited, RecStack;
508+
509+
std::function<bool(unsigned)> DFS = [&](unsigned Reg) -> bool {
510+
if (!PhiRegs.count(Reg))
511+
return false;
512+
if (RecStack.count(Reg))
513+
return true;
514+
if (Visited.count(Reg))
515+
return false;
516+
517+
Visited.insert(Reg);
518+
RecStack.insert(Reg);
519+
520+
for (unsigned Dep : PhiDeps[Reg]) {
521+
if (DFS(Dep))
522+
return true;
523+
}
524+
525+
RecStack.erase(Reg);
526+
return false;
527+
};
528+
529+
for (unsigned Reg : PhiRegs) {
530+
if (DFS(Reg))
531+
return true;
532+
}
533+
534+
return false;
535+
}
536+
488537
/// Return true if the loop can be software pipelined. The algorithm is
489538
/// restricted to loops with a single basic block. Make sure that the
490539
/// branch in the loop can be analyzed.
@@ -499,6 +548,11 @@ bool MachinePipeliner::canPipelineLoop(MachineLoop &L) {
499548
return false;
500549
}
501550

551+
if (hasPHICycle(L.getHeader(), MF->getRegInfo())) {
552+
LLVM_DEBUG(dbgs() << "Cannot pipeline loop due to PHI cycle\n");
553+
return false;
554+
}
555+
502556
if (disabledByPragma) {
503557
ORE->emit([&]() {
504558
return MachineOptimizationRemarkAnalysis(DEBUG_TYPE, "canPipelineLoop",
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
; RUN: llc -mtriple=hexagon-unknown-linux-gnu -enable-pipeliner -debug-only=pipeliner < %s 2>&1 | FileCheck %s
2+
3+
; CHECK: Cannot pipeline loop due to PHI cycle
4+
5+
define void @phi_cycle_loop(i32 %a, i32 %b) {
6+
entry:
7+
br label %loop
8+
9+
loop:
10+
%1 = phi i32 [ %a, %entry ], [ %3, %loop ]
11+
%2 = phi i32 [ %a, %entry ], [ %1, %loop ]
12+
%3 = phi i32 [ %b, %entry ], [ %2, %loop ]
13+
14+
; Prevent PHI elimination by using all values
15+
%add1 = add i32 %1, %2
16+
%add2 = add i32 %add1, %3
17+
%cmp = icmp slt i32 %add2, 100
18+
br i1 %cmp, label %loop, label %exit
19+
20+
exit:
21+
ret void
22+
}

0 commit comments

Comments
 (0)