Skip to content

Commit b77cc53

Browse files
committed
[llvm][CodeGen] Reanalyze corrupted live intervals in window scheduling
Corrupted live interval information can cause window scheduling to crash in some cases. Therefore, it is necessary to reanalyze this information in such situations.
1 parent fb9e685 commit b77cc53

File tree

3 files changed

+221
-0
lines changed

3 files changed

+221
-0
lines changed

llvm/include/llvm/CodeGen/WindowScheduler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ class WindowScheduler {
150150
/// Using the scheduling infrastructure to expand the results of window
151151
/// scheduling. It is usually necessary to add prologue and epilogue MBBs.
152152
virtual void expand();
153+
/// Reanalyze the live intervals in some scenarios.
154+
virtual void reanalyzeLiveIntervals();
153155
/// Update the live intervals for all registers used within MBB.
154156
virtual void updateLiveIntervals();
155157
/// Estimate a II value at which all MIs will be scheduled successfully.

llvm/lib/CodeGen/WindowScheduler.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ bool WindowScheduler::initialize() {
242242
LLVM_DEBUG(dbgs() << "There are too few MIs in the window region!\n");
243243
return false;
244244
}
245+
reanalyzeLiveIntervals();
245246
return true;
246247
}
247248

@@ -643,6 +644,20 @@ void WindowScheduler::expand() {
643644
MSE.cleanup();
644645
}
645646

647+
void WindowScheduler::reanalyzeLiveIntervals() {
648+
auto SlotIndex = Context->LIS->getSlotIndexes();
649+
// Check if the SlotIndex infomation is missing.
650+
for (auto &MBB : *MF)
651+
for (auto &MI : MBB)
652+
if (!SlotIndex->hasIndex(MI)) {
653+
// The slot index and live intervals of MF have been corrupted and need
654+
// to be reanalyzed.
655+
SlotIndex->reanalyze(*MF);
656+
Context->LIS->reanalyze(*MF);
657+
return;
658+
}
659+
}
660+
646661
void WindowScheduler::updateLiveIntervals() {
647662
SmallVector<Register, 128> UsedRegs;
648663
for (MachineInstr &MI : *MBB)
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
# REQUIRES: asserts
2+
#
3+
# RUN: llc --march=hexagon %s -run-pass=pipeliner -debug-only=pipeliner \
4+
# RUN: -window-sched=force -filetype=null -window-search-num=100 \
5+
# RUN: -window-search-ratio=100 -window-diff-limit=0 -verify-machineinstrs \
6+
# RUN: 2>&1 | FileCheck %s
7+
8+
# The bug was reported at https://github.com/llvm/llvm-project/pull/99454.
9+
# It is caused by the corruption of live intervals in certain scenarios.
10+
#
11+
# CHECK: Best window offset is {{[0-9]+}} and Best II is {{[0-9]+}}.
12+
# CHECK: Best window offset is {{[0-9]+}} and Best II is {{[0-9]+}}.
13+
14+
--- |
15+
target triple = "hexagon"
16+
17+
@_dp_ctrl_calc_tu_temp2_fp = global i64 0
18+
@_dp_ctrl_calc_tu_temp1_fp = global i32 0
19+
@dp_panel_update_tu_timings___trans_tmp_5 = global i64 0
20+
@_dp_ctrl_calc_tu___trans_tmp_8 = global i64 0
21+
22+
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture)
23+
declare i8 @div64_u64_rem(i32, ptr)
24+
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture)
25+
26+
define void @dp_ctrl_calc_tu_parameters() {
27+
if.end.i:
28+
%rem.i11.i = alloca i64, align 8
29+
%rem.i.i = alloca i64, align 8
30+
call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %rem.i11.i)
31+
%call.i.i = call i8 @div64_u64_rem(i32 0, ptr nonnull %rem.i11.i)
32+
%conv1.i.i = zext i8 %call.i.i to i64
33+
%rem.promoted.i.i = load i64, ptr %rem.i11.i, align 8
34+
br label %do.body.i.i
35+
36+
do.body.i.i:
37+
%lsr.iv1 = phi i32 [ %lsr.iv.next2, %do.body.i.i ], [ -32, %if.end.i ]
38+
%sub9.i.i = phi i64 [ %rem.promoted.i.i, %if.end.i ], [ %sub8.i.i.7, %do.body.i.i ]
39+
%res_abs.0.i.i = phi i64 [ %conv1.i.i, %if.end.i ], [ %res_abs.1.i.i.7, %do.body.i.i ]
40+
%cmp.not.i.i = icmp ne i64 %sub9.i.i, 0
41+
%sub.i.neg.i = sext i1 %cmp.not.i.i to i64
42+
%sub8.i.i = add i64 %sub9.i.i, %sub.i.neg.i
43+
%0 = shl i64 %res_abs.0.i.i, 2
44+
%1 = select i1 %cmp.not.i.i, i64 2, i64 0
45+
%shl.i.i.5 = or disjoint i64 %0, %1
46+
%cmp.not.i.i.5 = icmp ne i64 %sub8.i.i, 0
47+
%sub.i.neg.i.5 = sext i1 %cmp.not.i.i.5 to i64
48+
%sub8.i.i.5 = add i64 %sub8.i.i, %sub.i.neg.i.5
49+
%or.i.i.5 = zext i1 %cmp.not.i.i.5 to i64
50+
%res_abs.1.i.i.5 = or disjoint i64 %shl.i.i.5, %or.i.i.5
51+
%cmp.not.i.i.6 = icmp ne i64 %sub8.i.i.5, 0
52+
%sub.i.neg.i.6 = sext i1 %cmp.not.i.i.6 to i64
53+
%sub8.i.i.6 = add i64 %sub8.i.i.5, %sub.i.neg.i.6
54+
%2 = shl i64 %res_abs.1.i.i.5, 2
55+
%3 = select i1 %cmp.not.i.i.6, i64 2, i64 0
56+
%shl.i.i.7 = or disjoint i64 %2, %3
57+
%cmp.not.i.i.7 = icmp ne i64 %sub8.i.i.6, 0
58+
%sub.i.neg.i.7 = sext i1 %cmp.not.i.i.7 to i64
59+
%sub8.i.i.7 = add i64 %sub8.i.i.6, %sub.i.neg.i.7
60+
%or.i.i.7 = zext i1 %cmp.not.i.i.7 to i64
61+
%res_abs.1.i.i.7 = or disjoint i64 %shl.i.i.7, %or.i.i.7
62+
%lsr.iv.next2 = add nsw i32 %lsr.iv1, 8
63+
%tobool.not.i.i.7 = icmp eq i32 %lsr.iv.next2, 0
64+
br i1 %tobool.not.i.i.7, label %fec_check.i, label %do.body.i.i
65+
66+
fec_check.i:
67+
call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %rem.i11.i)
68+
store i64 %res_abs.1.i.i.7, ptr @_dp_ctrl_calc_tu_temp2_fp, align 8
69+
call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %rem.i11.i)
70+
%call.i12.i = call i8 @div64_u64_rem(i32 0, ptr nonnull %rem.i11.i)
71+
%conv1.i13.i = zext i8 %call.i12.i to i64
72+
%rem.promoted.i14.i = load i64, ptr %rem.i11.i, align 8
73+
br label %do.body.i15.i
74+
75+
do.body.i15.i:
76+
%lsr.iv = phi i32 [ %lsr.iv.next, %do.body.i15.i ], [ -32, %fec_check.i ]
77+
%sub9.i16.i = phi i64 [ %rem.promoted.i14.i, %fec_check.i ], [ %sub8.i22.i.7, %do.body.i15.i ]
78+
%res_abs.0.i17.i = phi i64 [ %conv1.i13.i, %fec_check.i ], [ %res_abs.1.i24.i.7, %do.body.i15.i ]
79+
%cmp.not.i20.i = icmp ugt i64 %sub9.i16.i, 999
80+
%sub.i21.neg.i = select i1 %cmp.not.i20.i, i64 -1000, i64 0
81+
%sub8.i22.i = add i64 %sub.i21.neg.i, %sub9.i16.i
82+
%4 = shl i64 %res_abs.0.i17.i, 2
83+
%5 = select i1 %cmp.not.i20.i, i64 2, i64 0
84+
%shl.i19.i.7 = or disjoint i64 %4, %5
85+
%cmp.not.i20.i.7 = icmp ugt i64 %sub8.i22.i, 999
86+
%sub.i21.neg.i.7 = select i1 %cmp.not.i20.i.7, i64 -1000, i64 0
87+
%sub8.i22.i.7 = add i64 %sub.i21.neg.i.7, %sub8.i22.i
88+
%or.i23.i.7 = zext i1 %cmp.not.i20.i.7 to i64
89+
%res_abs.1.i24.i.7 = or disjoint i64 %shl.i19.i.7, %or.i23.i.7
90+
%lsr.iv.next = add nsw i32 %lsr.iv, 8
91+
%tobool.not.i26.i.7 = icmp eq i32 %lsr.iv.next, 0
92+
br i1 %tobool.not.i26.i.7, label %_dp_ctrl_calc_tu.exit, label %do.body.i15.i
93+
94+
_dp_ctrl_calc_tu.exit:
95+
call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %rem.i11.i)
96+
%conv.i = trunc i64 %res_abs.1.i24.i.7 to i32
97+
store i32 %conv.i, ptr @_dp_ctrl_calc_tu_temp1_fp, align 4
98+
%conv5.i = and i64 %res_abs.1.i24.i.7, 4294967295
99+
store i64 %conv5.i, ptr @dp_panel_update_tu_timings___trans_tmp_5, align 8
100+
store i64 %res_abs.1.i.i.7, ptr @_dp_ctrl_calc_tu___trans_tmp_8, align 8
101+
ret void
102+
}
103+
104+
...
105+
---
106+
name: dp_ctrl_calc_tu_parameters
107+
tracksRegLiveness: true
108+
stack:
109+
- { id: 0, name: rem.i11.i, type: default, offset: 0, size: 8, alignment: 8}
110+
body: |
111+
bb.0:
112+
successors: %bb.1(0x80000000)
113+
114+
%0:intregs = A2_tfrsi 0
115+
%1:intregs = PS_fi %stack.0.rem.i11.i, 0
116+
%2:intregs = A2_tfrsi 0
117+
%3:doubleregs = A4_combineir 0, %2
118+
%4:doubleregs = L2_loadrd_io %stack.0.rem.i11.i, 0
119+
%5:doubleregs = A2_tfrpi 0
120+
J2_loop0i %bb.1, 4, implicit-def $lc0, implicit-def $sa0, implicit-def $usr
121+
122+
bb.1 (machine-block-address-taken):
123+
successors: %bb.2(0x04000000), %bb.1(0x7c000000)
124+
125+
%6:doubleregs = PHI %4, %bb.0, %7, %bb.1
126+
%8:doubleregs = PHI %3, %bb.0, %9, %bb.1
127+
%10:predregs = C2_cmpeqp %6, %5
128+
%11:intregs = C2_muxii %10, 0, -1
129+
%12:doubleregs = A2_addsp %11, %6
130+
%13:doubleregs = S2_asl_i_p %8, 2
131+
%14:intregs = S2_setbit_i %13.isub_lo, 1
132+
%15:intregs = C2_mux %10, %13.isub_lo, %14
133+
%16:predregs = C2_cmpeqp %12, %5
134+
%17:intregs = C2_muxii %16, 0, -1
135+
%18:doubleregs = A2_addsp %17, %12
136+
%19:intregs = S2_setbit_i %15, 0
137+
%20:intregs = C2_mux %16, %15, %19
138+
%21:predregs = C2_cmpeqp %18, %5
139+
%22:intregs = C2_muxii %21, 0, -1
140+
%23:doubleregs = A2_addsp %22, %18
141+
%24:intregs = S2_asl_i_r %20, 2
142+
%25:intregs = S2_extractu %8.isub_lo, 2, 28
143+
%26:intregs = S2_asl_i_r_or %25, %13.isub_hi, 2
144+
%27:intregs = S2_setbit_i %24, 1
145+
%28:intregs = C2_mux %21, %24, %27
146+
%29:predregs = C2_cmpeqp %23, %5
147+
%30:intregs = C2_muxii %29, 0, -1
148+
%7:doubleregs = A2_addsp %30, %23
149+
%31:intregs = S2_setbit_i %28, 0
150+
%32:intregs = C2_mux %29, %28, %31
151+
%9:doubleregs = REG_SEQUENCE %26, %subreg.isub_hi, %32, %subreg.isub_lo
152+
ENDLOOP0 %bb.1, implicit-def $pc, implicit-def $lc0, implicit $sa0, implicit $lc0
153+
J2_jump %bb.2, implicit-def dead $pc
154+
155+
bb.2:
156+
successors: %bb.3(0x80000000)
157+
158+
S2_storerdgp @_dp_ctrl_calc_tu_temp2_fp, %9, implicit $gp
159+
%33:intregs = A2_tfrsi 0
160+
%34:intregs = PS_fi %stack.0.rem.i11.i, 0
161+
%35:intregs = A2_tfrsi 0
162+
%36:doubleregs = L2_loadrd_io %stack.0.rem.i11.i, 0
163+
%37:doubleregs = A2_tfrpi 124
164+
%38:intregs = A2_tfrsi -1000
165+
%39:intregs = A2_tfrsi -1
166+
J2_loop0i %bb.3, 4, implicit-def $lc0, implicit-def $sa0, implicit-def $usr
167+
168+
bb.3 (machine-block-address-taken):
169+
successors: %bb.4(0x04000000), %bb.3(0x7c000000)
170+
171+
%40:doubleregs = PHI %36, %bb.2, %41, %bb.3
172+
%42:intregs = PHI %35, %bb.2, %43, %bb.3
173+
%44:intregs = PHI %33, %bb.2, %45, %bb.3
174+
%46:doubleregs = S2_lsr_i_p %40, 3
175+
%47:predregs = C2_cmpgtup %46, %37
176+
%48:intregs = C2_mux %47, %38, %33
177+
%49:intregs = C2_mux %47, %39, %33
178+
%50:doubleregs = REG_SEQUENCE %49, %subreg.isub_hi, %48, %subreg.isub_lo
179+
%51:doubleregs = A2_addp %50, %40
180+
%52:intregs = S2_asl_i_r %42, 2
181+
%53:intregs = S2_extractu %42, 2, 30
182+
%45:intregs = S2_asl_i_r_or %53, %44, 2
183+
%54:intregs = S2_setbit_i %52, 1
184+
%55:intregs = C2_mux %47, %54, %52
185+
%56:doubleregs = S2_lsr_i_p %51, 3
186+
%57:predregs = C2_cmpgtup %56, %37
187+
%58:intregs = C2_mux %57, %38, %33
188+
%59:intregs = C2_mux %57, %39, %33
189+
%60:doubleregs = REG_SEQUENCE %59, %subreg.isub_hi, %58, %subreg.isub_lo
190+
%41:doubleregs = A2_addp %60, %51
191+
%61:intregs = S2_setbit_i %55, 0
192+
%43:intregs = C2_mux %57, %61, %55
193+
ENDLOOP0 %bb.3, implicit-def $pc, implicit-def $lc0, implicit $sa0, implicit $lc0
194+
J2_jump %bb.4, implicit-def dead $pc
195+
196+
bb.4:
197+
S2_storerigp @_dp_ctrl_calc_tu_temp1_fp, %43, implicit $gp
198+
%62:intregs = A2_tfrsi 0
199+
%63:doubleregs = REG_SEQUENCE %43, %subreg.isub_lo, %62, %subreg.isub_hi
200+
S2_storerdgp @dp_panel_update_tu_timings___trans_tmp_5, %63, implicit $gp
201+
S2_storerdgp @_dp_ctrl_calc_tu___trans_tmp_8, %9, implicit $gp
202+
PS_jmpret $r31, implicit-def dead $pc
203+
204+
...

0 commit comments

Comments
 (0)