Skip to content

Commit 304b6c9

Browse files
Merge pull request #8943 from aschwaighofer/loop_unroller_sge_bound
LoopUnroller: Teach the loop unroller about >= terminated loops
2 parents 777d794 + cafe582 commit 304b6c9

File tree

2 files changed

+88
-3
lines changed

2 files changed

+88
-3
lines changed

lib/SILOptimizer/LoopTransforms/LoopUnroll.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,23 @@ static Optional<uint64_t> getMaxLoopTripCount(SILLoop *Loop,
126126
IntegerLiteralInst *End;
127127
SILValue RecNext;
128128

129+
unsigned Adjust = 0;
130+
129131
if (!match(CondBr->getCondition(),
130132
m_BuiltinInst(BuiltinValueKind::ICMP_EQ, m_SILValue(RecNext),
131-
m_IntegerLiteralInst(End))))
132-
return None;
133+
m_IntegerLiteralInst(End))) &&
134+
!match(CondBr->getCondition(),
135+
m_BuiltinInst(BuiltinValueKind::ICMP_SGE, m_SILValue(RecNext),
136+
m_IntegerLiteralInst(End)))) {
137+
if (!match(CondBr->getCondition(),
138+
m_BuiltinInst(BuiltinValueKind::ICMP_SGT, m_SILValue(RecNext),
139+
m_IntegerLiteralInst(End))))
140+
return None;
141+
// Otherwise, we have a greater than comparison.
142+
else
143+
Adjust = 1;
144+
}
145+
133146
if (!match(RecNext,
134147
m_TupleExtractInst(m_ApplyInst(BuiltinValueKind::SAddOver,
135148
m_SILPHIArgument(RecArg), m_One()),
@@ -159,7 +172,7 @@ static Optional<uint64_t> getMaxLoopTripCount(SILLoop *Loop,
159172
if (Dist == 0)
160173
return None;
161174

162-
return Dist.getZExtValue();
175+
return Dist.getZExtValue() + Adjust;
163176
}
164177

165178
/// Check whether we can duplicate the instructions in the loop and use a

test/SILOptimizer/loop_unroll.sil

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,78 @@ bb3:
7171
return %8 : $()
7272
}
7373

74+
// CHECK-LABEL: sil @loop_unroll_3
75+
// CHECK: bb0:
76+
// CHECK: br bb1
77+
// CHECK: bb1
78+
// CHECK: = builtin "sadd_with_overflow_Int64
79+
// CHECK: cond_br {{.*}}, bb3, bb2
80+
// CHECK: bb2:
81+
// CHECK: br bb4
82+
// CHECK: bb3:
83+
// CHECK: return
84+
// CHECK: bb4
85+
// CHECK: = builtin "sadd_with_overflow_Int64
86+
// CHECK: br bb3
87+
88+
sil @loop_unroll_3 : $@convention(thin) () -> () {
89+
bb0:
90+
%0 = integer_literal $Builtin.Int64, 0
91+
%1 = integer_literal $Builtin.Int64, 1
92+
%2 = integer_literal $Builtin.Int64, 2
93+
%3 = integer_literal $Builtin.Int1, 1
94+
br bb1(%0 : $Builtin.Int64)
95+
96+
bb1(%4 : $Builtin.Int64):
97+
%5 = builtin "sadd_with_overflow_Int64"(%4 : $Builtin.Int64, %1 : $Builtin.Int64, %3 : $Builtin.Int1) : $(Builtin.Int64, Builtin.Int1)
98+
%6 = tuple_extract %5 : $(Builtin.Int64, Builtin.Int1), 0
99+
%7 = builtin "cmp_sge_Int64"(%6 : $Builtin.Int64, %2 : $Builtin.Int64) : $Builtin.Int1
100+
cond_br %7, bb3, bb2
101+
102+
bb2:
103+
br bb1(%6 : $Builtin.Int64)
104+
105+
bb3:
106+
%8 = tuple()
107+
return %8 : $()
108+
}
109+
110+
// CHECK-LABEL: sil @loop_unroll_4
111+
// CHECK: bb0:
112+
// CHECK: br bb1
113+
// CHECK: bb1
114+
// CHECK: = builtin "sadd_with_overflow_Int64
115+
// CHECK: cond_br {{.*}}, bb3, bb2
116+
// CHECK: bb2:
117+
// CHECK: br bb4
118+
// CHECK: bb3:
119+
// CHECK: return
120+
// CHECK: bb4
121+
// CHECK: = builtin "sadd_with_overflow_Int64
122+
// CHECK: br bb3
123+
124+
sil @loop_unroll_4 : $@convention(thin) () -> () {
125+
bb0:
126+
%0 = integer_literal $Builtin.Int64, 0
127+
%1 = integer_literal $Builtin.Int64, 1
128+
%2 = integer_literal $Builtin.Int64, 1
129+
%3 = integer_literal $Builtin.Int1, 1
130+
br bb1(%0 : $Builtin.Int64)
131+
132+
bb1(%4 : $Builtin.Int64):
133+
%5 = builtin "sadd_with_overflow_Int64"(%4 : $Builtin.Int64, %1 : $Builtin.Int64, %3 : $Builtin.Int1) : $(Builtin.Int64, Builtin.Int1)
134+
%6 = tuple_extract %5 : $(Builtin.Int64, Builtin.Int1), 0
135+
%7 = builtin "cmp_sgt_Int64"(%6 : $Builtin.Int64, %2 : $Builtin.Int64) : $Builtin.Int1
136+
cond_br %7, bb3, bb2
137+
138+
bb2:
139+
br bb1(%6 : $Builtin.Int64)
140+
141+
bb3:
142+
%8 = tuple()
143+
return %8 : $()
144+
}
145+
74146
class B {}
75147

76148
// CHECK-LABEL: sil @unroll_with_stack_allocation

0 commit comments

Comments
 (0)