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