Skip to content

Commit 6c32cac

Browse files
committed
Enable LoopRotate on OSSA
1 parent 845e63f commit 6c32cac

File tree

3 files changed

+650
-6
lines changed

3 files changed

+650
-6
lines changed

lib/SILOptimizer/LoopTransforms/LoopRotate.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ using namespace swift;
3939
/// loops in the swift benchmarks).
4040
static llvm::cl::opt<int> LoopRotateSizeLimit("looprotate-size-limit",
4141
llvm::cl::init(20));
42+
static llvm::cl::opt<bool> RotateSingleBlockLoop("looprotate-single-block-loop",
43+
llvm::cl::init(false));
4244

4345
/// Check whether all operands are loop invariant.
4446
static bool
@@ -228,9 +230,9 @@ static bool rotateLoopAtMostUpToLatch(SILLoop *loop, DominanceInfo *domInfo,
228230
return false;
229231
}
230232

231-
bool didRotate =
232-
rotateLoop(loop, domInfo, loopInfo, false /* rotateSingleBlockLoops */,
233-
latch, ShouldVerify);
233+
bool didRotate = rotateLoop(
234+
loop, domInfo, loopInfo,
235+
RotateSingleBlockLoop /* rotateSingleBlockLoops */, latch, ShouldVerify);
234236

235237
// Keep rotating at most until we hit the original latch.
236238
if (didRotate)
@@ -440,9 +442,6 @@ class LoopRotation : public SILFunctionTransform {
440442

441443
SILFunction *f = getFunction();
442444
assert(f);
443-
// FIXME: Add ownership support.
444-
if (f->hasOwnership())
445-
return;
446445

447446
SILLoopInfo *loopInfo = loopAnalysis->get(f);
448447
assert(loopInfo);
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
// RUN: %target-sil-opt -loop-rotate -looprotate-single-block-loop=true %s | %FileCheck %s
2+
sil_stage canonical
3+
4+
import Builtin
5+
import Swift
6+
7+
class Klass {
8+
9+
}
10+
11+
sil [ossa] @useKlass : $@convention(thin) (@guaranteed Klass) -> ()
12+
13+
sil [ossa] @klassIdentity : $@convention(thin) (@owned Klass) -> @owned Klass
14+
15+
sil [ossa] @increment : $@convention(thin) (Builtin.Int32) -> Builtin.Int32
16+
17+
// CHECK-LABEL: sil [ossa] @testLoopSimple :
18+
// CHECK: bb0(%0 : $Int32, %1 : @owned $Klass):
19+
// CHECK: cond_br {{.*}}, bb2, bb1
20+
// CHECK: bb1:
21+
// CHECK-LABEL: } // end sil function 'testLoopSimple'
22+
sil [ossa] @testLoopSimple : $@convention(thin) (Int32, @owned Klass) -> (){
23+
bb0(%0 : $Int32, %1: @owned $Klass):
24+
%2 = struct_extract %0 : $Int32, #Int32._value
25+
%one = integer_literal $Builtin.Int32, 1
26+
br bb1(%2 : $Builtin.Int32, %1 : $Klass)
27+
28+
bb1(%3 : $Builtin.Int32, %4: @owned $Klass):
29+
%f1 = function_ref @klassIdentity : $@convention(thin) (@owned Klass) -> @owned Klass
30+
%c1 = apply %f1(%4) : $@convention(thin) (@owned Klass) -> @owned Klass
31+
%f2 = function_ref @increment : $@convention(thin) (Builtin.Int32) -> Builtin.Int32
32+
%c2 = apply %f2(%3) : $@convention(thin) (Builtin.Int32) -> Builtin.Int32
33+
%5 = struct $Int32 (%c2 : $Builtin.Int32)
34+
%6 = builtin "cmp_eq_Word"(%5 : $Int32, %one : $Builtin.Int32) : $Builtin.Int1
35+
cond_br %6, bb3, bb2
36+
37+
bb2:
38+
br bb1(%c2 : $Builtin.Int32, %c1: $Klass)
39+
40+
bb3:
41+
destroy_value %c1 : $Klass
42+
%res = tuple ()
43+
return %res : $()
44+
}
45+
46+
// CHECK-LABEL: sil [ossa] @testLoopCopy :
47+
// CHECK: bb0(%0 : $Int32, %1 : @owned $Klass):
48+
// CHECK: cond_br {{.*}}, bb2, bb1
49+
// CHECK: bb1:
50+
// CHECK-LABEL: } // end sil function 'testLoopCopy'
51+
sil [ossa] @testLoopCopy : $@convention(thin) (Int32, @owned Klass) -> (){
52+
bb0(%0 : $Int32, %1: @owned $Klass):
53+
%2 = struct_extract %0 : $Int32, #Int32._value
54+
%one = integer_literal $Builtin.Int32, 1
55+
br bb1(%2 : $Builtin.Int32, %1 : $Klass)
56+
57+
bb1(%3 : $Builtin.Int32, %4: @owned $Klass):
58+
%f1 = function_ref @klassIdentity : $@convention(thin) (@owned Klass) -> @owned Klass
59+
%copy = copy_value %4 : $Klass
60+
destroy_value %4 : $Klass
61+
%c1 = apply %f1(%copy) : $@convention(thin) (@owned Klass) -> @owned Klass
62+
%f2 = function_ref @increment : $@convention(thin) (Builtin.Int32) -> Builtin.Int32
63+
%c2 = apply %f2(%3) : $@convention(thin) (Builtin.Int32) -> Builtin.Int32
64+
%5 = struct $Int32 (%c2 : $Builtin.Int32)
65+
%6 = builtin "cmp_eq_Word"(%5 : $Int32, %one : $Builtin.Int32) : $Builtin.Int1
66+
cond_br %6, bb3, bb2
67+
68+
bb2:
69+
br bb1(%c2 : $Builtin.Int32, %c1: $Klass)
70+
71+
bb3:
72+
destroy_value %c1 : $Klass
73+
%res = tuple ()
74+
return %res : $()
75+
}
76+
77+
// CHECK-LABEL: sil [ossa] @testLoopBorrow1 :
78+
// CHECK: bb0(%0 : $Int32, %1 : @owned $Klass):
79+
// CHECK: cond_br {{.*}}, bb2, bb1
80+
// CHECK: bb1:
81+
// CHECK-LABEL: } // end sil function 'testLoopBorrow1'
82+
sil [ossa] @testLoopBorrow1 : $@convention(thin) (Int32, @owned Klass) -> (){
83+
bb0(%0 : $Int32, %1: @owned $Klass):
84+
%2 = struct_extract %0 : $Int32, #Int32._value
85+
%one = integer_literal $Builtin.Int32, 1
86+
br bb1(%2 : $Builtin.Int32, %1 : $Klass)
87+
88+
bb1(%3 : $Builtin.Int32, %4: @owned $Klass):
89+
%f1 = function_ref @useKlass : $@convention(thin) (@guaranteed Klass) -> ()
90+
%borrow = begin_borrow %4 : $Klass
91+
%c1 = apply %f1(%borrow) : $@convention(thin) (@guaranteed Klass) -> ()
92+
end_borrow %borrow : $Klass
93+
%f2 = function_ref @increment : $@convention(thin) (Builtin.Int32) -> Builtin.Int32
94+
%c2 = apply %f2(%3) : $@convention(thin) (Builtin.Int32) -> Builtin.Int32
95+
%5 = struct $Int32 (%c2 : $Builtin.Int32)
96+
%6 = builtin "cmp_eq_Word"(%5 : $Int32, %one : $Builtin.Int32) : $Builtin.Int1
97+
cond_br %6, bb3, bb2
98+
99+
bb2:
100+
br bb1(%c2 : $Builtin.Int32, %4 : $Klass)
101+
102+
bb3:
103+
destroy_value %4 : $Klass
104+
%res = tuple ()
105+
return %res : $()
106+
}
107+
108+
// CHECK-LABEL: sil [ossa] @testLoopBorrow2 :
109+
// CHECK: bb0(%0 : $Int32, %1 : @owned $Klass):
110+
// CHECK: cond_br {{.*}}, bb2, bb1
111+
// CHECK: bb1:
112+
// CHECK-LABEL: } // end sil function 'testLoopBorrow2'
113+
sil [ossa] @testLoopBorrow2 : $@convention(thin) (Int32, @owned Klass) -> () {
114+
bb0(%0 : $Int32, %1: @owned $Klass):
115+
%2 = struct_extract %0 : $Int32, #Int32._value
116+
%one = integer_literal $Builtin.Int32, 1
117+
%borrow = begin_borrow %1 : $Klass
118+
br bb1(%2 : $Builtin.Int32, %borrow : $Klass)
119+
120+
bb1(%3 : $Builtin.Int32, %reborrow : @guaranteed $Klass):
121+
%f1 = function_ref @useKlass : $@convention(thin) (@guaranteed Klass) -> ()
122+
%c1 = apply %f1(%reborrow) : $@convention(thin) (@guaranteed Klass) -> ()
123+
%f2 = function_ref @increment : $@convention(thin) (Builtin.Int32) -> Builtin.Int32
124+
%c2 = apply %f2(%3) : $@convention(thin) (Builtin.Int32) -> Builtin.Int32
125+
%5 = struct $Int32 (%c2 : $Builtin.Int32)
126+
%6 = builtin "cmp_eq_Word"(%5 : $Int32, %one : $Builtin.Int32) : $Builtin.Int1
127+
cond_br %6, bb3, bb2
128+
129+
bb2:
130+
br bb1(%c2 : $Builtin.Int32, %reborrow : $Klass)
131+
132+
bb3:
133+
end_borrow %reborrow : $Klass
134+
destroy_value %1 : $Klass
135+
%res = tuple ()
136+
return %res : $()
137+
}
138+
139+
// CHECK-LABEL: sil [ossa] @testLoopBorrow3 :
140+
// CHECK: bb0(%0 : $Int32, %1 : @owned $Klass):
141+
// CHECK: cond_br {{.*}}, bb2, bb1
142+
// CHECK: bb1:
143+
// CHECK-LABEL: } // end sil function 'testLoopBorrow3'
144+
sil [ossa] @testLoopBorrow3 : $@convention(thin) (Int32, @owned Klass) -> () {
145+
bb0(%0 : $Int32, %1: @owned $Klass):
146+
%2 = struct_extract %0 : $Int32, #Int32._value
147+
%one = integer_literal $Builtin.Int32, 1
148+
%borrow = begin_borrow %1 : $Klass
149+
br bb1(%2 : $Builtin.Int32, %borrow : $Klass)
150+
151+
bb1(%3 : $Builtin.Int32, %reborrow1 : @guaranteed $Klass):
152+
%f1 = function_ref @useKlass : $@convention(thin) (@guaranteed Klass) -> ()
153+
%c1 = apply %f1(%reborrow1) : $@convention(thin) (@guaranteed Klass) -> ()
154+
%f2 = function_ref @increment : $@convention(thin) (Builtin.Int32) -> Builtin.Int32
155+
%c2 = apply %f2(%3) : $@convention(thin) (Builtin.Int32) -> Builtin.Int32
156+
%5 = struct $Int32 (%c2 : $Builtin.Int32)
157+
%6 = builtin "cmp_eq_Word"(%5 : $Int32, %one : $Builtin.Int32) : $Builtin.Int1
158+
end_borrow %reborrow1 : $Klass
159+
%reborrow2 = begin_borrow %1 : $Klass
160+
cond_br %6, bb3, bb2
161+
162+
bb2:
163+
br bb1(%c2 : $Builtin.Int32, %reborrow2 : $Klass)
164+
165+
bb3:
166+
end_borrow %reborrow2 : $Klass
167+
destroy_value %1 : $Klass
168+
%res = tuple ()
169+
return %res : $()
170+
}
171+

0 commit comments

Comments
 (0)