|
| 1 | +//===- MLIRFIRTiledVectorization.mlir -------------------------------------===// |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | +// |
| 17 | +// This file provides the vectorized MLIR FIR function with tiling. |
| 18 | +// |
| 19 | +//===----------------------------------------------------------------------===// |
| 20 | + |
| 21 | +// Tail process for fir vectorization algorithm. |
| 22 | +func.func @tail_processing(%input : memref<?xf32>, %kernel : memref<?xf32>, |
| 23 | + %output : memref<?xf32>, %input_offset : index) -> () { |
| 24 | + // 1. Get the total length of the workload. |
| 25 | + %c0 = arith.constant 0 : index |
| 26 | + %c1 = arith.constant 1 : index |
| 27 | + %input_size = memref.dim %input, %c0 : memref<?xf32> |
| 28 | + %kernel_size = memref.dim %kernel, %c0 : memref<?xf32> |
| 29 | + |
| 30 | + // 2. Set the iteration step (vector size). |
| 31 | + %vl_step = arith.constant 16 : index |
| 32 | + %vl_step_minus_1 = arith.subi %vl_step, %c1 : index |
| 33 | + |
| 34 | + // 3. Calculate the upper bound for vectorized processing |
| 35 | + // - Subtract `vl_step` is to avoid overflow at the vectorization tail. |
| 36 | + // - Add 1 to ensure the final loop runs when the workload length is divisible |
| 37 | + // by the vector size. |
| 38 | + %upbound_ = arith.subi %input_size, %vl_step : index |
| 39 | + %upbound_init = arith.addi %upbound_, %c1 : index |
| 40 | + |
| 41 | + // 4. Loop through each kernel element |
| 42 | + scf.for %n = %c0 to %kernel_size step %c1 |
| 43 | + iter_args(%upbound = %upbound_init) -> (index) { |
| 44 | + %k_elem = memref.load %kernel[%n] : memref<?xf32> |
| 45 | + %k_vec = vector.splat %k_elem : vector<16xf32> |
| 46 | + |
| 47 | + // 5. Perform the vectorization body. |
| 48 | + %iter_idx = scf.for %i = %input_offset to %upbound step %vl_step // 起始点从`0`改为`input_offset` |
| 49 | + iter_args(%iter_init = %input_offset) -> (index) { |
| 50 | + %in_vec = vector.load %input[%i] : memref<?xf32>, vector<16xf32> |
| 51 | + %out_index = arith.addi %i, %n : index |
| 52 | + %out_vec = vector.load %output[%out_index] : memref<?xf32>, vector<16xf32> // 需要计算output的偏移量 |
| 53 | + %fma_vec = vector.fma %k_vec, %in_vec, %out_vec : vector<16xf32> |
| 54 | + vector.store %fma_vec, %output[%out_index] : memref<?xf32>, vector<16xf32> |
| 55 | + %i_next = arith.addi %i, %vl_step : index |
| 56 | + scf.yield %i_next : index |
| 57 | + } |
| 58 | + |
| 59 | + // 6. Process the remainder of the elements with scalar operations. |
| 60 | + %upbound_scalar = arith.addi %upbound, %vl_step_minus_1 : index |
| 61 | + scf.for %i = %iter_idx to %upbound_scalar step %c1 { |
| 62 | + %in_elem = memref.load %input[%i] : memref<?xf32> |
| 63 | + %out_index = arith.addi %i, %n : index |
| 64 | + %out_elem = memref.load %output[%out_index] : memref<?xf32> // ouput index need to change |
| 65 | + %mul_elem = arith.mulf %in_elem, %k_elem : f32 |
| 66 | + %add_elem = arith.addf %mul_elem, %out_elem : f32 |
| 67 | + memref.store %add_elem, %output[%out_index] : memref<?xf32> // change output index |
| 68 | + } |
| 69 | + |
| 70 | + %upbound_next = arith.subi %upbound, %c1 : index |
| 71 | + scf.yield %upbound_next : index |
| 72 | + } |
| 73 | + |
| 74 | + return |
| 75 | +} |
| 76 | + |
| 77 | +func.func @fir_tiled_vectorization(%input : memref<?xf32>, %kernel : memref<?xf32>, |
| 78 | + %output : memref<?xf32>) -> () { |
| 79 | + // 1. Get the total length of the workload. |
| 80 | + %c0 = arith.constant 0 : index |
| 81 | + %c1 = arith.constant 1 : index |
| 82 | + %input_size = memref.dim %input, %c0 : memref<?xf32> |
| 83 | + %kernel_size = memref.dim %kernel, %c0 : memref<?xf32> |
| 84 | + |
| 85 | + // 2. Set the iteration step (vector size). |
| 86 | + %vl_step = arith.constant 16 : index |
| 87 | + %vl_step_minus_1 = arith.subi %vl_step, %c1 : index |
| 88 | + |
| 89 | + %tile_step = arith.constant 2048 : index |
| 90 | + |
| 91 | + // 3. Calculate the upper bound for vectorized processing. |
| 92 | + // The computation times for the last kernel elements(which is the shortest). |
| 93 | + %last_kernel_element_used_input_size_ = arith.subi %input_size, %kernel_size : index |
| 94 | + %last_kernel_element_used_input_size = arith.addi %last_kernel_element_used_input_size_, %c1 : index |
| 95 | + |
| 96 | + %input_upbound_ = arith.subi %last_kernel_element_used_input_size, %tile_step : index |
| 97 | + %input_upbound = arith.addi %input_upbound_, %c1 : index |
| 98 | + |
| 99 | + // 4. Do the tiling process, each tile can be fully computed with vector(remainder is zero) |
| 100 | + // Return the offset address for tail process. |
| 101 | + %input_offset = scf.for %address = %c0 to %input_upbound step %tile_step |
| 102 | + iter_args(%offset = %c0) -> (index) { |
| 103 | + %upbound = arith.addi %address, %tile_step : index |
| 104 | + |
| 105 | + scf.for %n = %c0 to %kernel_size step %c1 { |
| 106 | + %k_elem = memref.load %kernel[%n] : memref<?xf32> |
| 107 | + %k_vec = vector.splat %k_elem : vector<16xf32> |
| 108 | + |
| 109 | + // 5. Perform the vectorization body. |
| 110 | + scf.for %i = %address to %upbound step %vl_step { |
| 111 | + %in_vec = vector.load %input[%i] : memref<?xf32>, vector<16xf32> |
| 112 | + %out_index = arith.addi %i, %n : index |
| 113 | + %out_vec = vector.load %output[%out_index] : memref<?xf32>, vector<16xf32> // 需要计算output的偏移量 |
| 114 | + %fma_vec = vector.fma %k_vec, %in_vec, %out_vec : vector<16xf32> |
| 115 | + vector.store %fma_vec, %output[%out_index] : memref<?xf32>, vector<16xf32> |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + scf.yield %upbound : index |
| 120 | + } |
| 121 | + |
| 122 | + // 6. Tail processing, begin from `input[input_offset]` |
| 123 | + call @tail_processing(%input, %kernel, %output, %input_offset) : (memref<?xf32>, memref<?xf32>, memref<?xf32>, index) -> () |
| 124 | + |
| 125 | + return |
| 126 | +} |
0 commit comments