|
| 1 | +// RUN: mlir-opt %s -canonicalize="test-convergence" -split-input-file | FileCheck %s |
| 2 | + |
| 3 | +// This file contains some tests of folding/canonicalizing vector.extract |
| 4 | + |
| 5 | +//----------------------------------------------------------------------------- |
| 6 | +// [Pattern: ExtractOpFromLoad] |
| 7 | +//----------------------------------------------------------------------------- |
| 8 | + |
| 9 | +// CHECK-LABEL: @extract_load_scalar |
| 10 | +// CHECK-SAME: (%[[ARG0:.*]]: memref<?xf32>, %[[ARG1:.*]]: index) |
| 11 | +func.func @extract_load_scalar(%arg0: memref<?xf32>, %arg1: index) -> f32 { |
| 12 | +// CHECK: %[[RES:.*]] = memref.load %[[ARG0]][%[[ARG1]]] : memref<?xf32> |
| 13 | +// CHECK: return %[[RES]] : f32 |
| 14 | + %0 = vector.load %arg0[%arg1] : memref<?xf32>, vector<4xf32> |
| 15 | + %1 = vector.extract %0[0] : f32 from vector<4xf32> |
| 16 | + return %1 : f32 |
| 17 | +} |
| 18 | + |
| 19 | +// CHECK-LABEL: @extract_load_index |
| 20 | +// CHECK-SAME: (%[[ARG0:.*]]: memref<?xindex>, %[[ARG1:.*]]: index) |
| 21 | +func.func @extract_load_index(%arg0: memref<?xindex>, %arg1: index) -> index { |
| 22 | +// CHECK: %[[RES:.*]] = memref.load %[[ARG0]][%[[ARG1]]] : memref<?xindex> |
| 23 | +// CHECK: return %[[RES]] : index |
| 24 | + %0 = vector.load %arg0[%arg1] : memref<?xindex>, vector<4xindex> |
| 25 | + %1 = vector.extract %0[0] : index from vector<4xindex> |
| 26 | + return %1 : index |
| 27 | +} |
| 28 | + |
| 29 | +// CHECK-LABEL: @extract_load_scalar_non_zero_off |
| 30 | +// CHECK-SAME: (%[[ARG0:.*]]: memref<?xf32>, %[[ARG1:.*]]: index) |
| 31 | +func.func @extract_load_scalar_non_zero_off(%arg0: memref<?xf32>, %arg1: index) -> f32 { |
| 32 | +// CHECK: %[[C1:.*]] = arith.constant 1 : index |
| 33 | +// CHECK: %[[OFF:.*]] = arith.addi %[[ARG1]], %[[C1]] overflow<nsw> : index |
| 34 | +// CHECK: %[[RES:.*]] = memref.load %[[ARG0]][%[[OFF]]] : memref<?xf32> |
| 35 | +// CHECK: return %[[RES]] : f32 |
| 36 | + %0 = vector.load %arg0[%arg1] : memref<?xf32>, vector<4xf32> |
| 37 | + %1 = vector.extract %0[1] : f32 from vector<4xf32> |
| 38 | + return %1 : f32 |
| 39 | +} |
| 40 | + |
| 41 | +// CHECK-LABEL: @extract_load_scalar_dyn_off |
| 42 | +// CHECK-SAME: (%[[ARG0:.*]]: memref<?xf32>, %[[ARG1:.*]]: index, %[[ARG2:.*]]: index) |
| 43 | +func.func @extract_load_scalar_dyn_off(%arg0: memref<?xf32>, %arg1: index, %arg2: index) -> f32 { |
| 44 | +// CHECK: %[[OFF:.*]] = arith.addi %[[ARG1]], %[[ARG2]] overflow<nsw> : index |
| 45 | +// CHECK: %[[RES:.*]] = memref.load %[[ARG0]][%[[OFF]]] : memref<?xf32> |
| 46 | +// CHECK: return %[[RES]] : f32 |
| 47 | + %0 = vector.load %arg0[%arg1] : memref<?xf32>, vector<4xf32> |
| 48 | + %1 = vector.extract %0[%arg2] : f32 from vector<4xf32> |
| 49 | + return %1 : f32 |
| 50 | +} |
| 51 | + |
| 52 | +// CHECK-LABEL: @extract_load_vec_non_zero_off |
| 53 | +// CHECK-SAME: (%[[ARG0:.*]]: memref<?x?xf32>, %[[ARG1:.*]]: index, %[[ARG2:.*]]: index) |
| 54 | +func.func @extract_load_vec_non_zero_off(%arg0: memref<?x?xf32>, %arg1: index, %arg2: index) -> vector<4xf32> { |
| 55 | +// CHECK: %[[C1:.*]] = arith.constant 1 : index |
| 56 | +// CHECK: %[[OFF:.*]] = arith.addi %[[ARG1]], %[[C1]] overflow<nsw> : index |
| 57 | +// CHECK: %[[RES:.*]] = vector.load %[[ARG0]][%[[OFF]], %[[ARG2]]] : memref<?x?xf32>, vector<4xf32> |
| 58 | +// CHECK: return %[[RES]] : vector<4xf32> |
| 59 | + %0 = vector.load %arg0[%arg1, %arg2] : memref<?x?xf32>, vector<2x4xf32> |
| 60 | + %1 = vector.extract %0[1] : vector<4xf32> from vector<2x4xf32> |
| 61 | + return %1 : vector<4xf32> |
| 62 | +} |
| 63 | + |
| 64 | +// CHECK-LABEL: @extract_load_scalar_non_zero_off_2d_src_memref |
| 65 | +// CHECK-SAME: (%[[ARG0:.*]]: memref<?x?xf32>, %[[ARG1:.*]]: index, %[[ARG2:.*]]: index) |
| 66 | +func.func @extract_load_scalar_non_zero_off_2d_src_memref(%arg0: memref<?x?xf32>, %arg1: index, %arg2: index) -> f32 { |
| 67 | +// CHECK: %[[C1:.*]] = arith.constant 1 : index |
| 68 | +// CHECK: %[[OFF:.*]] = arith.addi %[[ARG2]], %[[C1]] overflow<nsw> : index |
| 69 | +// CHECK: %[[RES:.*]] = memref.load %[[ARG0]][%[[ARG1]], %[[OFF]]] : memref<?x?xf32> |
| 70 | +// CHECK: return %[[RES]] : f32 |
| 71 | + %0 = vector.load %arg0[%arg1, %arg2] : memref<?x?xf32>, vector<4xf32> |
| 72 | + %1 = vector.extract %0[1] : f32 from vector<4xf32> |
| 73 | + return %1 : f32 |
| 74 | +} |
| 75 | + |
| 76 | +// CHECK-LABEL: @extract_load_vec_high_rank |
| 77 | +// CHECK-SAME: (%[[ARG0:.*]]: memref<?x?x?xf32>, %[[ARG1:.*]]: index, %[[ARG2:.*]]: index, %[[ARG3:.*]]: index) |
| 78 | +func.func @extract_load_vec_high_rank(%arg0: memref<?x?x?xf32>, %arg1: index, %arg2: index, %arg3: index) -> vector<4xf32> { |
| 79 | +// CHECK: %[[C1:.*]] = arith.constant 1 : index |
| 80 | +// CHECK: %[[OFF:.*]] = arith.addi %[[ARG2]], %[[C1]] overflow<nsw> : index |
| 81 | +// CHECK: %[[RES:.*]] = vector.load %[[ARG0]][%[[ARG1]], %[[OFF]], %[[ARG3]]] : memref<?x?x?xf32>, vector<4xf32> |
| 82 | +// CHECK: return %[[RES]] : vector<4xf32> |
| 83 | + %0 = vector.load %arg0[%arg1, %arg2, %arg3] : memref<?x?x?xf32>, vector<2x4xf32> |
| 84 | + %1 = vector.extract %0[1] : vector<4xf32> from vector<2x4xf32> |
| 85 | + return %1 : vector<4xf32> |
| 86 | +} |
| 87 | + |
| 88 | +// CHECK-LABEL: @negative_extract_load_scalar_from_memref_of_vec |
| 89 | +// CHECK-SAME: (%[[ARG0:.*]]: memref<?xvector<4xf32>>, %[[ARG1:.*]]: index) |
| 90 | +func.func @negative_extract_load_scalar_from_memref_of_vec(%arg0: memref<?xvector<4xf32>>, %arg1: index) -> f32 { |
| 91 | +// CHECK: %[[RES:.*]] = vector.load %[[ARG0]][%[[ARG1]]] : memref<?xvector<4xf32>>, vector<4xf32> |
| 92 | +// CHECK: %[[EXT:.*]] = vector.extract %[[RES]][0] : f32 from vector<4xf32> |
| 93 | +// CHECK: return %[[EXT]] : f32 |
| 94 | + %0 = vector.load %arg0[%arg1] : memref<?xvector<4xf32>>, vector<4xf32> |
| 95 | + %1 = vector.extract %0[0] : f32 from vector<4xf32> |
| 96 | + return %1 : f32 |
| 97 | +} |
| 98 | + |
| 99 | +// CHECK-LABEL: @negative_extract_load_scalar_from_memref_of_i1 |
| 100 | +// CHECK-SAME: (%[[ARG0:.*]]: memref<?xi1>, %[[ARG1:.*]]: index) |
| 101 | +func.func @negative_extract_load_scalar_from_memref_of_i1(%arg0: memref<?xi1>, %arg1: index) -> i1 { |
| 102 | +// Subbyte types are tricky, ignore them for now. |
| 103 | +// CHECK: %[[RES:.*]] = vector.load %[[ARG0]][%[[ARG1]]] : memref<?xi1>, vector<8xi1> |
| 104 | +// CHECK: %[[EXT:.*]] = vector.extract %[[RES]][0] : i1 from vector<8xi1> |
| 105 | +// CHECK: return %[[EXT]] : i1 |
| 106 | + %0 = vector.load %arg0[%arg1] : memref<?xi1>, vector<8xi1> |
| 107 | + %1 = vector.extract %0[0] : i1 from vector<8xi1> |
| 108 | + return %1 : i1 |
| 109 | +} |
| 110 | + |
| 111 | +// CHECK-LABEL: @negative_extract_load_no_single_use |
| 112 | +// CHECK-SAME: (%[[ARG0:.*]]: memref<?xf32>, %[[ARG1:.*]]: index) |
| 113 | +func.func @negative_extract_load_no_single_use(%arg0: memref<?xf32>, %arg1: index) -> (f32, vector<4xf32>) { |
| 114 | +// CHECK: %[[RES:.*]] = vector.load %[[ARG0]][%[[ARG1]]] : memref<?xf32>, vector<4xf32> |
| 115 | +// CHECK: %[[EXT:.*]] = vector.extract %[[RES]][0] : f32 from vector<4xf32> |
| 116 | +// CHECK: return %[[EXT]], %[[RES]] : f32, vector<4xf32> |
| 117 | + %0 = vector.load %arg0[%arg1] : memref<?xf32>, vector<4xf32> |
| 118 | + %1 = vector.extract %0[0] : f32 from vector<4xf32> |
| 119 | + return %1, %0 : f32, vector<4xf32> |
| 120 | +} |
| 121 | + |
| 122 | +// CHECK-LABEL: @negative_extract_load_scalable |
| 123 | +// CHECK-SAME: (%[[ARG0:.*]]: memref<?xf32>, %[[ARG1:.*]]: index) |
| 124 | +func.func @negative_extract_load_scalable(%arg0: memref<?xf32>, %arg1: index) -> f32 { |
| 125 | +// CHECK: %[[RES:.*]] = vector.load %[[ARG0]][%[[ARG1]]] : memref<?xf32>, vector<[1]xf32> |
| 126 | +// CHECK: %[[EXT:.*]] = vector.extract %[[RES]][0] : f32 from vector<[1]xf32> |
| 127 | +// CHECK: return %[[EXT]] : f32 |
| 128 | + %0 = vector.load %arg0[%arg1] : memref<?xf32>, vector<[1]xf32> |
| 129 | + %1 = vector.extract %0[0] : f32 from vector<[1]xf32> |
| 130 | + return %1 : f32 |
| 131 | +} |
0 commit comments