Skip to content

Commit 6d7a84d

Browse files
authored
[SandboxVec][Scheduler] Fix top of schedule (llvm#126820)
This patch fixes the way the top-of-schedule variable gets set and updated. Before this patch it used to get updated whenever we scheduled a bundle, which is wrong, as the top-of-schedule needs to be maintained across scheduling attempts. It should get reset only when we clear the schedule or when we destroy the current schedule and re-schedule.
1 parent b0d7820 commit 6d7a84d

File tree

5 files changed

+40
-7
lines changed

5 files changed

+40
-7
lines changed

llvm/lib/Transforms/Vectorize/SandboxVectorizer/Scheduler.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,13 @@ bool Scheduler::trySchedule(ArrayRef<Instruction *> Instrs) {
230230
// top-most part of the schedule that includes the instrs in the bundle and
231231
// re-schedule.
232232
trimSchedule(Instrs);
233+
ScheduleTopItOpt = std::nullopt;
233234
[[fallthrough]];
234235
case BndlSchedState::NoneScheduled: {
235236
// TODO: Set the window of the DAG that we are interested in.
236-
// We start scheduling at the bottom instr of Instrs.
237-
ScheduleTopItOpt = std::next(VecUtils::getLowest(Instrs)->getIterator());
237+
if (!ScheduleTopItOpt)
238+
// We start scheduling at the bottom instr of Instrs.
239+
ScheduleTopItOpt = std::next(VecUtils::getLowest(Instrs)->getIterator());
238240

239241
// TODO: For now don't cross BBs.
240242
if (!DAG.getInterval().empty()) {
@@ -262,6 +264,12 @@ bool Scheduler::trySchedule(ArrayRef<Instruction *> Instrs) {
262264
void Scheduler::dump(raw_ostream &OS) const {
263265
OS << "ReadyList:\n";
264266
ReadyList.dump(OS);
267+
OS << "Top of schedule: ";
268+
if (ScheduleTopItOpt)
269+
OS << **ScheduleTopItOpt;
270+
else
271+
OS << "Empty";
272+
OS << "\n";
265273
}
266274
void Scheduler::dump() const { dump(dbgs()); }
267275
#endif // NDEBUG

llvm/test/Transforms/SandboxVectorizer/bottomup_basic.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ define void @store_fadd_load(ptr %ptr) {
7777
; CHECK-NEXT: [[PTR0:%.*]] = getelementptr float, ptr [[PTR]], i32 0
7878
; CHECK-NEXT: [[VECL:%.*]] = load <2 x float>, ptr [[PTR0]], align 4
7979
; CHECK-NEXT: [[VECL1:%.*]] = load <2 x float>, ptr [[PTR0]], align 4
80-
; CHECK-NEXT: [[VEC:%.*]] = fadd <2 x float> [[VECL]], [[VECL1]]
80+
; CHECK-NEXT: [[VEC:%.*]] = fadd <2 x float> [[VECL1]], [[VECL]]
8181
; CHECK-NEXT: store <2 x float> [[VEC]], ptr [[PTR0]], align 4
8282
; CHECK-NEXT: ret void
8383
;
@@ -247,8 +247,8 @@ define void @diamondMultiInput(ptr %ptr, ptr %ptrX) {
247247
; CHECK-LABEL: define void @diamondMultiInput(
248248
; CHECK-SAME: ptr [[PTR:%.*]], ptr [[PTRX:%.*]]) {
249249
; CHECK-NEXT: [[PTR0:%.*]] = getelementptr float, ptr [[PTR]], i32 0
250-
; CHECK-NEXT: [[VECL:%.*]] = load <2 x float>, ptr [[PTR0]], align 4
251250
; CHECK-NEXT: [[LDX:%.*]] = load float, ptr [[PTRX]], align 4
251+
; CHECK-NEXT: [[VECL:%.*]] = load <2 x float>, ptr [[PTR0]], align 4
252252
; CHECK-NEXT: [[VINS:%.*]] = insertelement <2 x float> poison, float [[LDX]], i32 0
253253
; CHECK-NEXT: [[VEXT:%.*]] = extractelement <2 x float> [[VECL]], i32 0
254254
; CHECK-NEXT: [[VINS1:%.*]] = insertelement <2 x float> [[VINS]], float [[VEXT]], i32 1

llvm/test/Transforms/SandboxVectorizer/bottomup_seed_slice_pow2.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ define void @pow2(ptr %ptr, float %val) {
77
; POW2-SAME: ptr [[PTR:%.*]], float [[VAL:%.*]]) {
88
; POW2-NEXT: [[PTR0:%.*]] = getelementptr float, ptr [[PTR]], i32 0
99
; POW2-NEXT: [[PTR2:%.*]] = getelementptr float, ptr [[PTR]], i32 2
10-
; POW2-NEXT: [[VECL:%.*]] = load <2 x float>, ptr [[PTR0]], align 4
1110
; POW2-NEXT: [[LD2:%.*]] = load float, ptr [[PTR2]], align 4
11+
; POW2-NEXT: [[VECL:%.*]] = load <2 x float>, ptr [[PTR0]], align 4
1212
; POW2-NEXT: store <2 x float> [[VECL]], ptr [[PTR0]], align 4
1313
; POW2-NEXT: store float [[LD2]], ptr [[PTR2]], align 4
1414
; POW2-NEXT: ret void

llvm/test/Transforms/SandboxVectorizer/repeated_instrs.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ define i32 @repeated_splat(ptr %ptr, i32 %v) #0 {
55
; CHECK-LABEL: define i32 @repeated_splat(
66
; CHECK-SAME: ptr [[PTR:%.*]], i32 [[V:%.*]]) {
77
; CHECK-NEXT: [[GEP0:%.*]] = getelementptr inbounds i32, ptr [[PTR]], i64 0
8-
; CHECK-NEXT: [[VECL:%.*]] = load <2 x i32>, ptr [[GEP0]], align 4
98
; CHECK-NEXT: [[SPLAT:%.*]] = add i32 [[V]], 0
109
; CHECK-NEXT: [[PACK:%.*]] = insertelement <2 x i32> poison, i32 [[SPLAT]], i32 0
1110
; CHECK-NEXT: [[PACK1:%.*]] = insertelement <2 x i32> [[PACK]], i32 [[SPLAT]], i32 1
11+
; CHECK-NEXT: [[VECL:%.*]] = load <2 x i32>, ptr [[GEP0]], align 4
1212
; CHECK-NEXT: [[VEC:%.*]] = mul <2 x i32> [[VECL]], [[PACK1]]
1313
; CHECK-NEXT: store <2 x i32> [[VEC]], ptr [[GEP0]], align 4
1414
; CHECK-NEXT: ret i32 0
@@ -31,6 +31,7 @@ define i32 @repeated_partial(ptr %ptr, i32 %v) #0 {
3131
; CHECK-NEXT: [[GEP0:%.*]] = getelementptr inbounds i32, ptr [[PTR]], i64 0
3232
; CHECK-NEXT: [[GEP1:%.*]] = getelementptr inbounds i32, ptr [[PTR]], i64 1
3333
; CHECK-NEXT: [[GEP3:%.*]] = getelementptr inbounds i32, ptr [[PTR]], i64 3
34+
; CHECK-NEXT: [[SPLAT:%.*]] = add i32 [[V]], 0
3435
; CHECK-NEXT: [[LD0:%.*]] = load i32, ptr [[GEP0]], align 4
3536
; CHECK-NEXT: [[LD1:%.*]] = load i32, ptr [[GEP1]], align 4
3637
; CHECK-NEXT: [[LD3:%.*]] = load i32, ptr [[GEP3]], align 4
@@ -39,7 +40,6 @@ define i32 @repeated_partial(ptr %ptr, i32 %v) #0 {
3940
; CHECK-NEXT: [[PACK2:%.*]] = insertelement <4 x i32> [[PACK1]], i32 [[LD1]], i32 2
4041
; CHECK-NEXT: [[PACK3:%.*]] = insertelement <4 x i32> [[PACK2]], i32 [[LD3]], i32 3
4142
; CHECK-NEXT: [[VECL:%.*]] = load <4 x i32>, ptr [[GEP0]], align 4
42-
; CHECK-NEXT: [[SPLAT:%.*]] = add i32 [[V]], 0
4343
; CHECK-NEXT: [[VEC:%.*]] = mul <4 x i32> [[VECL]], [[PACK3]]
4444
; CHECK-NEXT: store <4 x i32> [[VEC]], ptr [[GEP0]], align 4
4545
; CHECK-NEXT: ret i32 0

llvm/test/Transforms/SandboxVectorizer/scheduler.ll

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,28 @@ define void @check_dag_scheduler_update(ptr noalias %p, ptr noalias %p1) {
4949
store i32 %add21, ptr %arrayidx23
5050
ret void
5151
}
52+
53+
; This used to generate use-before-def because of a buggy update of the
54+
; top-of-schedule variable.
55+
define <4 x float> @check_top_of_schedule(ptr %0) {
56+
; CHECK-LABEL: define <4 x float> @check_top_of_schedule(
57+
; CHECK-SAME: ptr [[TMP0:%.*]]) {
58+
; CHECK-NEXT: [[INS_1:%.*]] = insertelement <4 x float> zeroinitializer, float poison, i64 0
59+
; CHECK-NEXT: [[TRUNC_1:%.*]] = fptrunc double 0.000000e+00 to float
60+
; CHECK-NEXT: [[INS_2:%.*]] = insertelement <4 x float> [[INS_1]], float [[TRUNC_1]], i64 0
61+
; CHECK-NEXT: [[GEP_1:%.*]] = getelementptr double, ptr [[TMP0]], i64 1
62+
; CHECK-NEXT: store <2 x double> <double 0.000000e+00, double 1.000000e+00>, ptr [[GEP_1]], align 8
63+
; CHECK-NEXT: ret <4 x float> [[INS_2]]
64+
;
65+
%trunc.1 = fptrunc double 0.000000e+00 to float
66+
%trunc.2 = fptrunc double 1.000000e+00 to float
67+
%ins.1 = insertelement <4 x float> zeroinitializer, float poison, i64 0
68+
%ins.2 = insertelement <4 x float> %ins.1, float %trunc.1, i64 0
69+
%ext.1 = fpext float %trunc.1 to double
70+
%gep.1 = getelementptr double, ptr %0, i64 1
71+
store double %ext.1, ptr %gep.1, align 8
72+
%ext.2 = fpext float %trunc.2 to double
73+
%gep.2 = getelementptr double, ptr %0, i64 2
74+
store double %ext.2, ptr %gep.2, align 8
75+
ret <4 x float> %ins.2
76+
}

0 commit comments

Comments
 (0)