Skip to content

Commit b2e8f75

Browse files
committed
[mlir][vector] Restrict DropInnerMostUnitDimsTransferWrite
Restrict `DropInnerMostUnitDimsTransferWrite` so that it fails when one of the indices to be dropped could be != 0, e.g. ```mlir func.func @negative_example( %arg0: memref<16x1xf32>, %arg1: vector<8x1xf32>, %idx_1: index, %idx_2: index) { %c0 = arith.constant 0 : index vector.transfer_write %arg1, %arg0[%idx_1, %idx_2] {in_bounds = [true, true]} : vector<8x1xf32>, memref<16x1xf32> return } ``` This is an edge case that could represent an out-of-bounds access, though that will depend on the actual value of `%i`. Importantly, _without this change_ it would be transformed as follows: ```mlir func.func @negative_example( %arg0: memref<16x1xf32>, %arg1: vector<8x1xf32>, %idx_1: index, %idx_2: index) { %subview = memref.subview %arg0[0, 0] [16, 1] [1, 1] : memref<16x1xf32> to memref<16xf32, strided<[1]>> %0 = vector.shape_cast %arg1 : vector<8x1xf32> to vector<8xf32> vector.transfer_write %0, %subview[%idx_1] {in_bounds = [true]} : vector<8xf32>, memref<16xf32, strided<[1]>> return } ``` This is incorrect - `%idx_2` is ignored. Hence the extra restriction to avoid such cases. NOTE: This PR is limited to `vector.transfer_write`. Similar patch for `vector.transfer_read`: #94904
1 parent b14d305 commit b2e8f75

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,11 @@ class DropInnerMostUnitDimsTransferWrite
13951395
if (dimsToDrop == 0)
13961396
return failure();
13971397

1398+
// Make sure that the indices to be dropped are equal 0.
1399+
// TODO: Deal with cases when the indices are not 0.
1400+
if (!llvm::all_of(writeOp.getIndices().take_back(dimsToDrop), isZeroIndex))
1401+
return failure();
1402+
13981403
auto resultTargetVecType =
13991404
VectorType::get(targetType.getShape().drop_back(dimsToDrop),
14001405
targetType.getElementType(),

mlir/test/Dialect/Vector/vector-transfer-collapse-inner-most-dims.mlir

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,33 @@ func.func @contiguous_inner_most_dynamic_outer_scalable_inner_dim(%a: index, %b:
367367

368368
// -----
369369

370+
func.func @contiguous_inner_most_non_zero_idxs(%arg0: memref<16x1xf32>, %arg1: vector<8x1xf32>, %i: index) {
371+
%c0 = arith.constant 0 : index
372+
vector.transfer_write %arg1, %arg0[%i, %c0] {in_bounds = [true, true]} : vector<8x1xf32>, memref<16x1xf32>
373+
return
374+
}
375+
// CHECK-LABEL: func.func @contiguous_inner_most_non_zero_idxs(
376+
// CHECK-SAME: %[[MEM:.*]]: memref<16x1xf32>,
377+
// CHECK-SAME: %[[VEC:.*]]: vector<8x1xf32>,
378+
// CHECK-SAME: %[[IDX:.*]]: index) {
379+
// CHECK: %[[SV:.*]] = memref.subview %[[MEM]][0, 0] [16, 1] [1, 1] : memref<16x1xf32> to memref<16xf32, strided<[1]>>
380+
// CHECK: %[[SC:.*]] = vector.shape_cast %[[VEC]] : vector<8x1xf32> to vector<8xf32>
381+
// CHECK: vector.transfer_write %[[SC]], %[[SV]]{{\[}}%[[IDX]]] {in_bounds = [true]} : vector<8xf32>, memref<16xf32, strided<[1]>>
382+
383+
// The index to be dropped is != 0 - this is currently not supported.
384+
385+
func.func @negative_contiguous_inner_most_dim_non_zero_idxs(%arg0: memref<16x1xf32>, %arg1: vector<8x1xf32>, %i: index) {
386+
%c0 = arith.constant 0 : index
387+
vector.transfer_write %arg1, %arg0[%i, %i] {in_bounds = [true, true]} : vector<8x1xf32>, memref<16x1xf32>
388+
return
389+
}
390+
// CHECK-LABEL: func @negative_contiguous_inner_most_dim_non_zero_idxs
391+
// CHECK-NOT: memref.subview
392+
// CHECK-NOT: memref.shape_cast
393+
// CHECK: vector.transfer_write
394+
395+
// -----
396+
370397
func.func @drop_inner_most_dim(%arg0: memref<1x512x16x1xf32, strided<[8192, 16, 1, 1], offset: ?>>, %arg1: vector<1x16x16x1xf32>, %arg2: index) {
371398
%c0 = arith.constant 0 : index
372399
vector.transfer_write %arg1, %arg0[%c0, %arg2, %c0, %c0]

0 commit comments

Comments
 (0)