Skip to content

Commit 7b097a0

Browse files
committed
Rename to InlineArray
1 parent c93f8a0 commit 7b097a0

File tree

3 files changed

+298
-296
lines changed

3 files changed

+298
-296
lines changed

test/SILOptimizer/cse_ossa.sil

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// RUN: %target-sil-opt -sil-print-types -enable-sil-verify-all %s -update-borrowed-from -cse -enable-experimental-feature ValueGenerics | %FileCheck %s
2+
3+
// REQUIRES: swift_feature_ValueGenerics
4+
25
sil_stage canonical
36

47
import Builtin
@@ -1332,8 +1335,8 @@ exit(%proj : @guaranteed $C, %borrow : @guaranteed $S, %owned_s_2 : @owned $S):
13321335
// CHECK: type_value
13331336
// CHECK-NOT: type_value
13341337
// CHECK-LABEL: } // end sil function 'type_value_test'
1335-
sil [ossa] @type_value_test : $@convention(thin) <let N : Int> (@in_guaranteed Slab<N, Int>, Int) -> () {
1336-
bb0(%0 : $*Slab<N, Int>, %1 : $Int):
1338+
sil [ossa] @type_value_test : $@convention(thin) <let N : Int> (@in_guaranteed InlineArray<N, Int>, Int) -> () {
1339+
bb0(%0 : $*InlineArray<N, Int>, %1 : $Int):
13371340
%2 = integer_literal $Builtin.Int64, 0
13381341
%3 = type_value $Int for N
13391342
%4 = struct_extract %1, #Int._value
Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
// RUN: %target-swift-frontend %s -emit-sil -O \
2+
// RUN: -disable-availability-checking \
3+
// RUN: -enable-experimental-feature ValueGenerics | %FileCheck %s --check-prefix=CHECK-SIL
4+
5+
// RUN: %target-swift-frontend %s -emit-ir -O \
6+
// RUN: -disable-availability-checking \
7+
// RUN: -enable-experimental-feature InlineArray \
8+
// RUN: -enable-experimental-feature ValueGenerics | %FileCheck %s --check-prefix=CHECK-IR
9+
10+
// REQUIRES: swift_in_compiler
11+
// REQUIRES: swift_feature_ValueGenerics
12+
// REQUIRES: swift_stdlib_no_asserts, optimized_stdlib
13+
14+
// Bounds check should be eliminated
15+
// SIL removes lower bounds check from the loop
16+
// LLVM removes the upper bounds check from the loop and then vectorizes
17+
// A lower bounds check is left behind in the entry block
18+
19+
// CHECK-SIL-LABEL: sil @$s30inlinearray_bounds_check_tests0A29_sum_iterate_to_count_wo_trapySis11InlineArrayVyxSiGSiRVzlF :
20+
// CHECK-SIL: bb3
21+
// CHECK-SIL-NOT: cond_fail {{.*}}, "Index out of bounds"
22+
// CHECK-SIL: cond_br
23+
// CHECK-SIL-LABEL: } // end sil function '$s30inlinearray_bounds_check_tests0A29_sum_iterate_to_count_wo_trapySis11InlineArrayVyxSiGSiRVzlF'
24+
25+
// CHECK-IR: define {{.*}} @"$s30inlinearray_bounds_check_tests0A29_sum_iterate_to_count_wo_trapySis11InlineArrayVyxSiGSiRVzlF"
26+
// CHECK-IR: @llvm.vector.reduce.add
27+
public func inlinearray_sum_iterate_to_count_wo_trap<let N: Int>(_ v: InlineArray<N, Int>) -> Int {
28+
var sum = 0
29+
for i in 0..<v.count {
30+
sum &+= v[i]
31+
}
32+
return sum
33+
}
34+
35+
// Bounds check should be eliminated
36+
// SIL removes lower bounds check from the loop
37+
// LLVM removes the upper bounds check from the loop
38+
// A lower bounds check is left behind in the entry block
39+
40+
// CHECK-SIL-LABEL: sil @$s30inlinearray_bounds_check_tests0A31_sum_iterate_to_count_with_trapySis11InlineArrayVyxSiGSiRVzlF :
41+
// CHECK-SIL: bb3
42+
// CHECK-SIL-NOT: cond_fail {{.*}}, "Index out of bounds"
43+
// CHECK-SIL: cond_br
44+
// CHECK-SIL-LABEL: } // end sil function '$s30inlinearray_bounds_check_tests0A31_sum_iterate_to_count_with_trapySis11InlineArrayVyxSiGSiRVzlF'
45+
46+
public func inlinearray_sum_iterate_to_count_with_trap<let N: Int>(_ v: InlineArray<N, Int>) -> Int {
47+
var sum = 0
48+
for i in 0..<v.count {
49+
sum += v[i]
50+
}
51+
return sum
52+
}
53+
54+
// Bounds check should be hoisted
55+
// SIL removes lower bounds check from the loop
56+
// LLVM removes the upper bounds check from the loop and then vectorizes
57+
// A lower bounds check is left behind in the entry block
58+
59+
// CHECK-SIL-LABEL: sil @$s30inlinearray_bounds_check_tests0A31_sum_iterate_to_unknown_wo_trapySis11InlineArrayVyxSiG_SitSiRVzlF :
60+
// CHECK-SIL: bb3
61+
// CHECK-SIL: cond_fail {{.*}}, "Index out of bounds"
62+
// CHECK-SIL-NOT: cond_fail {{.*}}, "Index out of bounds"
63+
// CHECK-SIL: cond_br
64+
// CHECK-SIL-LABEL: } // end sil function '$s30inlinearray_bounds_check_tests0A31_sum_iterate_to_unknown_wo_trapySis11InlineArrayVyxSiG_SitSiRVzlF'
65+
66+
// CHECK-IR: define {{.*}} @"$s30inlinearray_bounds_check_tests0A31_sum_iterate_to_unknown_wo_trapySis11InlineArrayVyxSiG_SitSiRVzlF"
67+
// CHECK-IR: @llvm.vector.reduce.add
68+
public func inlinearray_sum_iterate_to_unknown_wo_trap<let N: Int>(_ v: InlineArray<N, Int>, _ n: Int) -> Int {
69+
var sum = 0
70+
for i in 0...n {
71+
sum &+= v[i]
72+
}
73+
return sum
74+
}
75+
76+
// Bounds check should be hoisted
77+
// SIL removes lower bounds check from the loop
78+
// LLVM removes the upper bounds check from the loop
79+
// A lower bounds check is left behind in the entry block
80+
81+
// CHECK-SIL-LABEL: sil @$s30inlinearray_bounds_check_tests0A33_sum_iterate_to_unknown_with_trapySis11InlineArrayVyxSiG_SitSiRVzlF :
82+
// CHECK-SIL: bb3
83+
// CHECK-SIL: cond_fail {{.*}}, "Index out of bounds"
84+
// CHECK-SIL-NOT: cond_fail {{.*}}, "Index out of bounds"
85+
// CHECK-SIL: cond_br
86+
// CHECK-SIL-LABEL: } // end sil function '$s30inlinearray_bounds_check_tests0A33_sum_iterate_to_unknown_with_trapySis11InlineArrayVyxSiG_SitSiRVzlF'
87+
public func inlinearray_sum_iterate_to_unknown_with_trap<let N: Int>(_ v: InlineArray<N, Int>, _ n: Int) -> Int {
88+
var sum = 0
89+
for i in 0...n {
90+
sum += v[i]
91+
}
92+
return sum
93+
}
94+
95+
// Bounds check should be eliminated
96+
// SIL removes lower bounds check from the loop
97+
// LLVM removes the upper bounds check from the loop and then vectorizes
98+
99+
// CHECK-SIL-LABEL: sil @$s30inlinearray_bounds_check_tests0A40_sum_iterate_to_deducible_count1_wo_trapySis11InlineArrayVyxSiG_SitSiRVzlF :
100+
// CHECK-SIL: bb3
101+
// CHECK-SIL: cond_fail {{.*}}, "Index out of bounds"
102+
// CHECK-SIL-NOT: cond_fail {{.*}}, "Index out of bounds"
103+
// CHECK-SIL: cond_br
104+
// CHECK-SIL-LABEL: } // end sil function '$s30inlinearray_bounds_check_tests0A40_sum_iterate_to_deducible_count1_wo_trapySis11InlineArrayVyxSiG_SitSiRVzlF'
105+
106+
// CHECK-IR: define {{.*}} @"$s30inlinearray_bounds_check_tests0A40_sum_iterate_to_deducible_count1_wo_trapySis11InlineArrayVyxSiG_SitSiRVzlF"
107+
// CHECK-IR: @llvm.vector.reduce.add
108+
public func inlinearray_sum_iterate_to_deducible_count1_wo_trap<let N: Int>(_ v: InlineArray<N, Int>, _ n: Int) -> Int {
109+
var sum = 0
110+
precondition(n <= v.count)
111+
for i in 0..<n {
112+
sum &+= v[i]
113+
}
114+
return sum
115+
}
116+
117+
// Bounds check should be eliminated
118+
// SIL removes lower bounds check from the loop
119+
// LLVM does not eliminate redundant bounds check
120+
121+
// CHECK-SIL-LABEL: sil @$s30inlinearray_bounds_check_tests0A42_sum_iterate_to_deducible_count1_with_trapySis11InlineArrayVyxSiG_SitSiRVzlF :
122+
// CHECK-SIL: bb3
123+
// CHECK-SIL: cond_fail {{.*}}, "Index out of bounds"
124+
// CHECK-SIL-NOT: cond_fail {{.*}}, "Index out of bounds"
125+
// CHECK-SIL: cond_br
126+
// CHECK-SIL-LABEL: } // end sil function '$s30inlinearray_bounds_check_tests0A42_sum_iterate_to_deducible_count1_with_trapySis11InlineArrayVyxSiG_SitSiRVzlF'
127+
public func inlinearray_sum_iterate_to_deducible_count1_with_trap<let N: Int>(_ v: InlineArray<N, Int>, _ n: Int) -> Int {
128+
var sum = 0
129+
precondition(n <= v.count)
130+
for i in 0..<n {
131+
sum += v[i]
132+
}
133+
return sum
134+
}
135+
136+
// Bounds check should be eliminated
137+
// SIL removes lower bounds check from the loop
138+
// LLVM removes upper bounds check and vectorizes the loop
139+
140+
// CHECK-SIL-LABEL: sil @$s30inlinearray_bounds_check_tests0A40_sum_iterate_to_deducible_count2_wo_trapySis11InlineArrayVyxSiG_SitSiRVzlF :
141+
// CHECK-SIL: bb3
142+
// CHECK-SIL: cond_fail {{.*}}, "Index out of bounds"
143+
// CHECK-SIL-NOT: cond_fail {{.*}}, "Index out of bounds"
144+
// CHECK-SIL: cond_br
145+
// CHECK-SIL-LABEL: } // end sil function '$s30inlinearray_bounds_check_tests0A40_sum_iterate_to_deducible_count2_wo_trapySis11InlineArrayVyxSiG_SitSiRVzlF'
146+
147+
// CHECK-IR: define {{.*}} @"$s30inlinearray_bounds_check_tests0A40_sum_iterate_to_deducible_count2_wo_trapySis11InlineArrayVyxSiG_SitSiRVzlF"
148+
// CHECK-IR: @llvm.vector.reduce.add
149+
public func inlinearray_sum_iterate_to_deducible_count2_wo_trap<let N: Int>(_ v: InlineArray<N, Int>, _ n: Int) -> Int {
150+
var sum = 0
151+
precondition(n <= v.count)
152+
for i in 0...n {
153+
sum &+= v[i]
154+
}
155+
return sum
156+
}
157+
158+
// Bounds check should be eliminated
159+
// SIL removes lower bounds check from the loop
160+
// LLVM does not eliminate redundant bounds check
161+
162+
// CHECK-SIL-LABEL: sil @$s30inlinearray_bounds_check_tests0A42_sum_iterate_to_deducible_count2_with_trapySis11InlineArrayVyxSiG_SitSiRVzlF :
163+
// CHECK-SIL: bb3
164+
// CHECK-SIL: cond_fail {{.*}}, "Index out of bounds"
165+
// CHECK-SIL-NOT: cond_fail {{.*}}, "Index out of bounds"
166+
// CHECK-SIL: cond_br
167+
// CHECK-SIL-LABEL: } // end sil function '$s30inlinearray_bounds_check_tests0A42_sum_iterate_to_deducible_count2_with_trapySis11InlineArrayVyxSiG_SitSiRVzlF'
168+
public func inlinearray_sum_iterate_to_deducible_count2_with_trap<let N: Int>(_ v: InlineArray<N, Int>, _ n: Int) -> Int {
169+
var sum = 0
170+
precondition(n <= v.count)
171+
for i in 0...n {
172+
sum += v[i]
173+
}
174+
return sum
175+
}
176+
177+
// Bounds check should be eliminated
178+
// SIL removes lower bounds check from the loop
179+
// LLVM removes upper bounds check and vectorizes the loop
180+
181+
// CHECK-SIL-LABEL: sil @$s30inlinearray_bounds_check_tests0A29_iterate_over_indices_wo_trapySis11InlineArrayVyxSiGSiRVzlF :
182+
// CHECK-SIL: bb3
183+
// CHECK-SIL-NOT: cond_fail {{.*}}, "Index out of bounds"
184+
// CHECK-SIL: cond_br
185+
// CHECK-SIL-LABEL: } // end sil function '$s30inlinearray_bounds_check_tests0A29_iterate_over_indices_wo_trapySis11InlineArrayVyxSiGSiRVzlF'
186+
187+
// CHECK-IR: define {{.*}} @"$s30inlinearray_bounds_check_tests0A29_iterate_over_indices_wo_trapySis11InlineArrayVyxSiGSiRVzlF"
188+
// CHECK-IR: @llvm.vector.reduce.add
189+
public func inlinearray_iterate_over_indices_wo_trap<let N: Int>(_ v: InlineArray<N, Int>) -> Int {
190+
var sum = 0
191+
for i in v.indices {
192+
sum &+= v[i]
193+
}
194+
return sum
195+
}
196+
197+
// Bounds check should be eliminated
198+
// SIL removes lower bounds check from the loop
199+
// LLVM does not eliminate redundant bounds check
200+
201+
// CHECK-SIL-LABEL: sil @$s30inlinearray_bounds_check_tests0A31_iterate_over_indices_with_trapySis11InlineArrayVyxSiGSiRVzlF :
202+
// CHECK-SIL: bb3
203+
// CHECK-SIL-NOT: cond_fail {{.*}}, "Index out of bounds"
204+
// CHECK-SIL: cond_br
205+
// CHECK-SIL-LABEL: } // end sil function '$s30inlinearray_bounds_check_tests0A31_iterate_over_indices_with_trapySis11InlineArrayVyxSiGSiRVzlF'
206+
public func inlinearray_iterate_over_indices_with_trap<let N: Int>(_ v: InlineArray<N, Int>) -> Int {
207+
var sum = 0
208+
for i in v.indices {
209+
sum += v[i]
210+
}
211+
return sum
212+
}
213+
214+
// Eliminate duplicate bounds check
215+
216+
// CHECK-SIL-LABEL: sil @$s30inlinearray_bounds_check_tests0A17_element_equalityySbs11InlineArrayVyxSiG_SitSiRVzlF :
217+
// CHECK-SIL: cond_fail {{.*}}, "Index out of bounds"
218+
// CHECK-SIL-NOT: cond_fail {{.*}}, "Index out of bounds"
219+
// CHECK-SIL-LABEL: } // end sil function '$s30inlinearray_bounds_check_tests0A17_element_equalityySbs11InlineArrayVyxSiG_SitSiRVzlF'
220+
221+
public func inlinearray_element_equality<let N: Int>(_ v: InlineArray<N, Int>, _ i: Int) -> Bool {
222+
return v[i] == v[i]
223+
}
224+
225+
// Eliminate duplicate bounds check
226+
227+
// CHECK-SIL-LABEL: sil @$s30inlinearray_bounds_check_tests0A12_element_sumySis11InlineArrayVyxSiG_SitSiRVzlF :
228+
// CHECK-SIL: cond_fail {{.*}}, "Index out of bounds"
229+
// CHECK-SIL-NOT: cond_fail {{.*}}, "Index out of bounds"
230+
// CHECK-SIL-LABEL: } // end sil function '$s30inlinearray_bounds_check_tests0A12_element_sumySis11InlineArrayVyxSiG_SitSiRVzlF'
231+
public func inlinearray_element_sum<let N: Int>(_ v: InlineArray<N, Int>, _ i: Int) -> Int {
232+
return v[i] &+ v[i]
233+
}
234+
235+
// Bounds check should be eliminated
236+
237+
// CHECK-SIL-LABEL: sil @$s30inlinearray_bounds_check_tests0A7_searchySiSgs11InlineArrayVyq_xG_xtSiRV_SQRzr0_lF :
238+
// CHECK-SIL: bb3:
239+
// CHECK-SIL: cond_fail {{.*}}, "Index out of bounds"
240+
// CHECK-SIL: cond_fail {{.*}}, "Index out of bounds"
241+
// CHECK-SIL: cond_br
242+
// CHECK-SIL-LABEL: } // end sil function '$s30inlinearray_bounds_check_tests0A7_searchySiSgs11InlineArrayVyq_xG_xtSiRV_SQRzr0_lF'
243+
public func inlinearray_search<T : Equatable, let N: Int>(_ v: InlineArray<N, T>, _ elem: T) -> Int? {
244+
for i in v.indices {
245+
if v[i] == elem {
246+
return i
247+
}
248+
}
249+
return nil
250+
}
251+
252+
// Bounds check should be eliminated
253+
254+
// CHECK-SIL-LABEL: sil @$s30inlinearray_bounds_check_tests0A11_search_splySiSgs11InlineArrayVyxSiG_SitSiRVzlF :
255+
// CHECK-SIL: bb3:
256+
// CHECK-SIL: cond_fail {{.*}}, "Index out of bounds"
257+
// CHECK-SIL: cond_fail {{.*}}, "Index out of bounds"
258+
// CHECK-SIL: cond_br
259+
// CHECK-SIL-LABEL: } // end sil function '$s30inlinearray_bounds_check_tests0A11_search_splySiSgs11InlineArrayVyxSiG_SitSiRVzlF'
260+
public func inlinearray_search_spl<let N: Int>(_ v: InlineArray<N, Int>, _ elem: Int) -> Int? {
261+
for i in v.indices {
262+
if v[i] == elem {
263+
return i
264+
}
265+
}
266+
return nil
267+
}
268+
269+
// Bounds check should be eliminated
270+
271+
// CHECK-SIL-LABEL: sil @$s30inlinearray_bounds_check_tests0A18_binary_search_splySiSgs11InlineArrayVyxSiG_SitSiRVzlF :
272+
// CHECK-SIL: bb2
273+
// CHECK-SIL: cond_fail {{.*}}, "Index out of bounds"
274+
// CHECK-SIL: cond_br
275+
// CHECK-SIL-LABEL: } // end sil function '$s30inlinearray_bounds_check_tests0A18_binary_search_splySiSgs11InlineArrayVyxSiG_SitSiRVzlF'
276+
public func inlinearray_binary_search_spl<let N: Int>(_ v: InlineArray<N, Int>, _ elem: Int) -> Int? {
277+
var low = 0, high = v.count - 1
278+
while low <= high {
279+
let mid = low + (high - low) / 2
280+
281+
if v[mid] == elem {
282+
return mid
283+
}
284+
else if v[mid] < elem {
285+
low = mid + 1
286+
} else {
287+
high = mid - 1
288+
}
289+
}
290+
291+
return nil;
292+
}
293+

0 commit comments

Comments
 (0)