| 375 | }; |
| 376 | |
| 377 | constexpr MediumKernelPlanSize MediumKernelPlanSize::Build( |
| 378 | const KernelShape& shape, const MediumKernelOptions& options) { |
| 379 | const int shifts_per_swizzle = |
| 380 | shape.unpacked_byte_size() / shape.packed_max_spread_bytes(); |
| 381 | |
| 382 | const int vals_per_swizzle = shifts_per_swizzle * shape.unpacked_per_simd(); |
| 383 | |
| 384 | // Using `unpacked_per_kernel_limit` to influence the number of swizzles per reads. |
| 385 | const auto packed_per_read_for_offset = [&](int bit_offset) -> int { |
| 386 | const int best = (shape.simd_bit_size() - bit_offset) / shape.packed_bit_size(); |
| 387 | const int limit = options.unpacked_per_kernel_limit_; |
| 388 | return (best > limit) && (limit > 0) ? limit : best; |
| 389 | }; |
| 390 | |
| 391 | const auto swizzles_per_read_for_offset = [&](int bit_offset) -> int { |
| 392 | return packed_per_read_for_offset(bit_offset) / vals_per_swizzle; |
| 393 | }; |
| 394 | |
| 395 | // If after a whole swizzle reading iteration we fall byte-unaligned, the remaining |
| 396 | // iterations will start with an byte-unaligned first value, reducing the effective |
| 397 | // capacity of the SIMD batch. |
| 398 | // We must check that our read iteration size still works with subsequent misalignment |
| 399 | // by looping until aligned. This implies that we do the same number of swizzle for each |
| 400 | // read offset. This may be improved a bit for some bit width, but in practice there are |
| 401 | // very few such cases. |
| 402 | // One may think that this algorithm will drive the kernel minimum reading size to |
| 403 | // unreasonably large sizes, but in practice reading iterations increase by factors of 2 |
| 404 | // and are quickly multiple of 8 (and aligned after the first one). |
| 405 | int swizzles_per_read = swizzles_per_read_for_offset(0); |
| 406 | int reads_per_kernel = 0; |
| 407 | int packed_start_bit = 0; |
| 408 | do { |
| 409 | int new_swizzles_per_read = swizzles_per_read_for_offset(packed_start_bit % 8); |
| 410 | if (new_swizzles_per_read < swizzles_per_read) { |
| 411 | swizzles_per_read = new_swizzles_per_read; |
| 412 | packed_start_bit = 0; |
| 413 | reads_per_kernel = 0; |
| 414 | } |
| 415 | int bits_per_read = swizzles_per_read * vals_per_swizzle * shape.packed_bit_size(); |
| 416 | packed_start_bit += bits_per_read; |
| 417 | reads_per_kernel += 1; |
| 418 | } while (packed_start_bit % 8 != 0); |
| 419 | |
| 420 | return { |
| 421 | .reads_per_kernel_ = reads_per_kernel, |
| 422 | .swizzles_per_read_ = swizzles_per_read, |
| 423 | .shifts_per_swizzle_ = shifts_per_swizzle, |
| 424 | }; |
| 425 | } |
| 426 | |
| 427 | /// Utility to map a desired number of bits to the smallest feasible size. |
| 428 | /// |
nothing calls this directly
no test coverage detected