Skip to content

Commit 811e505

Browse files
authored
[llvm][CodeGen] Update checking method of loop-carried phi in window scheduler (#96288)
Added some logic to check loop-carried phis in the window scheduler. It now includes the scenario where the preceding phi uses the virtual register defined by the succeeding phi.
1 parent 55e60c3 commit 811e505

12 files changed

+104
-30
lines changed

llvm/lib/CodeGen/WindowScheduler.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -192,22 +192,31 @@ bool WindowScheduler::initialize() {
192192
return false;
193193
}
194194
// Check each MI in MBB.
195-
SmallVector<Register, 8> PhiDefs;
195+
SmallSet<Register, 8> PrevDefs;
196+
SmallSet<Register, 8> PrevUses;
197+
auto IsLoopCarried = [&](MachineInstr &Phi) {
198+
// Two cases are checked here: (1)The virtual register defined by the
199+
// preceding phi is used by the succeeding phi;(2)The preceding phi uses the
200+
// virtual register defined by the succeeding phi.
201+
if (PrevUses.count(Phi.getOperand(0).getReg()))
202+
return true;
203+
PrevDefs.insert(Phi.getOperand(0).getReg());
204+
for (unsigned I = 1, E = Phi.getNumOperands(); I != E; I += 2) {
205+
if (PrevDefs.count(Phi.getOperand(I).getReg()))
206+
return true;
207+
PrevUses.insert(Phi.getOperand(I).getReg());
208+
}
209+
return false;
210+
};
196211
auto PLI = TII->analyzeLoopForPipelining(MBB);
197212
for (auto &MI : *MBB) {
198213
if (MI.isMetaInstruction() || MI.isTerminator())
199214
continue;
200215
if (MI.isPHI()) {
201-
for (auto Def : PhiDefs)
202-
if (MI.readsRegister(Def, TRI)) {
203-
LLVM_DEBUG(
204-
dbgs()
205-
<< "Consecutive phis are not allowed in window scheduling!\n");
206-
return false;
207-
}
208-
for (auto Def : MI.defs())
209-
if (Def.isReg())
210-
PhiDefs.push_back(Def.getReg());
216+
if (IsLoopCarried(MI)) {
217+
LLVM_DEBUG(dbgs() << "Loop carried phis are not supported yet!\n");
218+
return false;
219+
}
211220
++SchedPhiNum;
212221
++BestOffset;
213222
} else

llvm/test/CodeGen/Hexagon/swp-ws-dead-def.mir

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# REQUIRES: asserts
22
# RUN: llc --march=hexagon %s -run-pass=pipeliner -debug-only=pipeliner \
3-
# RUN: -window-sched=force -o - 2>&1 | FileCheck %s
3+
# RUN: -window-sched=force -filetype=null -verify-machineinstrs 2>&1 \
4+
# RUN: | FileCheck %s
45

56
# CHECK: Best window offset is {{[0-9]+}} and Best II is {{[0-9]+}}.
67
# CHECK-LABEL: name: exp_approx_top_six

llvm/test/CodeGen/Hexagon/swp-ws-exp-dbg.mir

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# REQUIRES: asserts
22
# RUN: llc --march=hexagon %s -run-pass=pipeliner -debug-only=pipeliner \
3-
# RUN: -window-sched=force -o - 2>&1 | FileCheck %s
4-
#
3+
# RUN: -window-sched=force -filetype=null -verify-machineinstrs 2>&1 \
4+
# RUN: | FileCheck %s
5+
56
# The Window Scheduling algorithm will discard the debug IR, just like the SMS
67
# algorithm does. Additionally, the MMO information in the IR is also preserved
78
# to ensure that no barrier dependencies are generated within the loop body.
8-
#
9+
910
# CHECK: Best window offset is {{[0-9]+}} and Best II is {{[0-9]+}}.
1011
# CHECK-LABEL: name: exp_approx
1112
# CHECK: bb.5.for.body:

llvm/test/CodeGen/Hexagon/swp-ws-exp.mir

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# REQUIRES: asserts
22
# RUN: llc --march=hexagon %s -run-pass=pipeliner -debug-only=pipeliner \
3-
# RUN: -window-sched=force -o - 2>&1 | FileCheck %s
3+
# RUN: -window-sched=force -filetype=null -verify-machineinstrs 2>&1 \
4+
# RUN: | FileCheck %s
45

56
# CHECK: Best window offset is {{[0-9]+}} and Best II is {{[0-9]+}}.
67

llvm/test/CodeGen/Hexagon/swp-ws-fail-0.mir

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# REQUIRES: asserts
22
# RUN: llc --march=hexagon %s -run-pass=pipeliner -debug-only=pipeliner \
3-
# RUN: -window-sched=force -o - 2>&1 | FileCheck %s \
3+
# RUN: -window-sched=force -filetype=null -verify-machineinstrs 2>&1 \
4+
# RUN: | FileCheck %s \
45
# RUN: --check-prefix=CHECK-INITIALIZE
56
# RUN: llc --march=hexagon %s -run-pass=pipeliner -debug-only=pipeliner \
6-
# RUN: -window-sched=force -window-region-limit=1 -window-ii-limit=1 -o - \
7-
# RUN: 2>&1 | FileCheck %s --check-prefix=CHECK-ANALYSE-II
7+
# RUN: -window-sched=force -window-region-limit=1 -window-ii-limit=1 \
8+
# RUN: -filetype=null 2>&1 | FileCheck %s --check-prefix=CHECK-ANALYSE-II
89
# RUN: llc --march=hexagon %s -run-pass=pipeliner -debug-only=pipeliner \
910
# RUN: -window-sched=force -window-region-limit=1 -window-search-ratio=80 \
10-
# RUN: -o - 2>&1 | FileCheck %s --check-prefix=CHECK-SCHED-NOT-NEEDED
11+
# RUN: -filetype=null 2>&1 | FileCheck %s --check-prefix=CHECK-SCHED-NOT-NEEDED
1112

1213
# CHECK-INITIALIZE: There are too few MIs in the window region!
1314
# CHECK-INITIALIZE: The WindowScheduler failed to initialize!

llvm/test/CodeGen/Hexagon/swp-ws-fail-1.mir

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
# REQUIRES: asserts
22
# RUN: llc --march=hexagon %s -run-pass=pipeliner -debug-only=pipeliner \
3-
# RUN: -window-sched=force -o - 2>&1 | FileCheck %s \
4-
# RUN: --check-prefix=CHECK-SUCCESSIVE-PHI
5-
6-
# CHECK-SUCCESSIVE-PHI: Consecutive phis are not allowed in window scheduling!
7-
# CHECK-SUCCESSIVE-PHI: The WindowScheduler failed to initialize!
3+
# RUN: -window-sched=force -filetype=null -verify-machineinstrs 2>&1 \
4+
# RUN: | FileCheck %s
5+
6+
# CHECK: Loop carried phis are not supported yet!
7+
# CHECK: The WindowScheduler failed to initialize!
8+
# CHECK-LABEL: body: |
9+
# CHECK: bb.3 (machine-block-address-taken):
10+
# CHECK: [[REG:%[0-9]+]]:intregs = PHI {{%[0-9]+}}, %bb.1, {{%[0-9]+}}, %bb.3
11+
# CHECK: {{%[0-9]+}}:intregs = PHI {{%[0-9]+}}, %bb.1, [[REG]], %bb.3
812

913
---
1014
name: relu

llvm/test/CodeGen/Hexagon/swp-ws-fail-2.mir

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# REQUIRES: asserts
22
# RUN: llc --march=hexagon %s -run-pass=pipeliner -debug-only=pipeliner \
3-
# RUN: -window-sched=force -o - 2>&1 | FileCheck %s
3+
# RUN: -window-sched=force -filetype=null -verify-machineinstrs 2>&1 \
4+
# RUN: | FileCheck %s
45

56
# CHECK: The WindowScheduler failed to initialize!
67

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# REQUIRES: asserts
2+
# RUN: llc --march=hexagon %s -run-pass=pipeliner -debug-only=pipeliner \
3+
# RUN: -window-sched=force -filetype=null -verify-machineinstrs 2>&1 \
4+
# RUN: | FileCheck %s
5+
6+
# CHECK: Loop carried phis are not supported yet!
7+
# CHECK: The WindowScheduler failed to initialize!
8+
# CHECK-LABEL: body: |
9+
# CHECK: bb.3 (machine-block-address-taken):
10+
# CHECK: {{%[0-9]+}}:intregs = PHI {{%[0-9]+}}, %bb.1, [[REG:%[0-9]+]], %bb.3
11+
# CHECK: [[REG]]:intregs = PHI {{%[0-9]+}}, %bb.1, {{%[0-9]+}}, %bb.3
12+
13+
---
14+
name: relu
15+
tracksRegLiveness: true
16+
body: |
17+
bb.0:
18+
successors: %bb.2(0x30000000), %bb.1(0x50000000)
19+
liveins: $r0, $r1, $r2
20+
21+
%0:intregs = COPY $r2
22+
%1:intregs = COPY $r1
23+
%2:intregs = COPY $r0
24+
%3:predregs = C2_cmpeqi %2, 0
25+
J2_jumpt killed %3, %bb.2, implicit-def dead $pc
26+
J2_jump %bb.1, implicit-def dead $pc
27+
28+
bb.1:
29+
successors: %bb.3(0x80000000)
30+
31+
%4:hvxvr = V6_vd0
32+
%5:intregs = A2_addi %2, 31
33+
%6:intregs = S2_lsr_i_r %5, 5
34+
%7:intregs = COPY %6
35+
J2_loop0r %bb.3, %7, implicit-def $lc0, implicit-def $sa0, implicit-def $usr
36+
J2_jump %bb.3, implicit-def dead $pc
37+
38+
bb.2:
39+
PS_jmpret $r31, implicit-def dead $pc
40+
41+
bb.3 (machine-block-address-taken):
42+
successors: %bb.3(0x7c000000), %bb.2(0x04000000)
43+
44+
%8:intregs = PHI %0, %bb.1, %9, %bb.3
45+
%9:intregs = PHI %1, %bb.1, %10, %bb.3
46+
%11:hvxvr, %10:intregs = V6_vL32b_pi %9, 128
47+
%12:hvxvr = V6_vmaxw killed %11, %4
48+
%13:intregs = V6_vS32b_pi %8, 128, killed %12
49+
ENDLOOP0 %bb.3, implicit-def $pc, implicit-def $lc0, implicit $sa0, implicit $lc0
50+
J2_jump %bb.2, implicit-def dead $pc
51+
52+
...

llvm/test/CodeGen/Hexagon/swp-ws-meta-instr.mir

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# REQUIRES: asserts
22
# RUN: llc --march=hexagon %s -run-pass=pipeliner -debug-only=pipeliner \
3-
# RUN: -window-sched=force -o - 2>&1 | FileCheck %s
3+
# RUN: -window-sched=force -filetype=null -verify-machineinstrs 2>&1 \
4+
# RUN: | FileCheck %s
45

56
# CHECK-NOT: PSEUDO_PROBE
67
# CHECK: Best window offset is {{[0-9]+}} and Best II is {{[0-9]+}}.

llvm/test/CodeGen/Hexagon/swp-ws-phi.mir

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# REQUIRES: asserts
22
# RUN: llc --march=hexagon %s -run-pass=pipeliner -debug-only=pipeliner \
3-
# RUN: -window-sched=force -filetype=null 2>&1 | FileCheck %s
3+
# RUN: -window-sched=force -filetype=null -verify-machineinstrs 2>&1 \
4+
# RUN: | FileCheck %s
45

56
# CHECK: Window scheduling is not needed!
67
# CHECK-LABEL: body: |

llvm/test/CodeGen/Hexagon/swp-ws-sqrt.mir

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# REQUIRES: asserts
22
# RUN: llc --march=hexagon %s -run-pass=pipeliner -debug-only=pipeliner \
3-
# RUN: -window-sched=force -o - 2>&1 | FileCheck %s
3+
# RUN: -window-sched=force -filetype=null -verify-machineinstrs 2>&1 \
4+
# RUN: | FileCheck %s
45

56
# CHECK: Best window offset is {{[0-9]+}} and Best II is {{[0-9]+}}.
67

llvm/test/CodeGen/Hexagon/swp-ws-weak-dep.mir

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# REQUIRES: asserts
22
# RUN: llc --march=hexagon %s -run-pass=pipeliner -debug-only=pipeliner \
3-
# RUN: -window-sched=force -filetype=null 2>&1 | FileCheck %s
3+
# RUN: -window-sched=force -filetype=null -verify-machineinstrs 2>&1 \
4+
# RUN: | FileCheck %s
45

56
# CHECK: SU(3): Ord Latency=0 Weak
67
# CHECK: SU(1): Ord Latency=0 Weak

0 commit comments

Comments
 (0)