Skip to content

Commit 1004865

Browse files
authored
[mlir][Vector] Support 0-d vectors natively in TransferOpReduceRank (#112907)
Since ddf2d62 , 0-d vectors are supported in VectorType. This patch removes 0-d vector handling with scalars for the TransferOpReduceRank pattern. This pattern specifically introduces tensor.extract_slice during vectorization, causing vectorization to not fold transfer_read/transfer_write slices properly. The changes in vectorization test files reflect this. There are other places where lowering patterns are still side-stepping from handling 0-d vectors properly, by turning them into scalars, but this patch only focuses on the vector.transfer_x patterns.
1 parent 4c697f7 commit 1004865

File tree

4 files changed

+14
-35
lines changed

4 files changed

+14
-35
lines changed

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -358,31 +358,10 @@ struct TransferOpReduceRank
358358
op, "map is not a minor identity with broadcasting");
359359
}
360360

361-
// TODO: support zero-dimension vectors natively. See:
362-
// https://llvm.discourse.group/t/should-we-have-0-d-vectors/3097.
363-
// In the meantime, lower these to a scalar load when they pop up.
364-
if (reducedShapeRank == 0) {
365-
Value newRead;
366-
if (isa<TensorType>(op.getShapedType())) {
367-
newRead = rewriter.create<tensor::ExtractOp>(
368-
op.getLoc(), op.getSource(), op.getIndices());
369-
} else {
370-
newRead = rewriter.create<memref::LoadOp>(
371-
op.getLoc(), originalVecType.getElementType(), op.getSource(),
372-
op.getIndices());
373-
}
374-
return rewriter
375-
.create<vector::BroadcastOp>(op.getLoc(), originalVecType, newRead)
376-
.getVector();
377-
}
378-
379361
SmallVector<int64_t> newShape(
380362
originalVecType.getShape().take_back(reducedShapeRank));
381363
SmallVector<bool> newScalableDims(
382364
originalVecType.getScalableDims().take_back(reducedShapeRank));
383-
// Vector rank cannot be zero. Handled by TransferReadToVectorLoadLowering.
384-
if (newShape.empty())
385-
return rewriter.notifyMatchFailure(op, "rank-reduced vector is 0-d");
386365

387366
VectorType newReadType = VectorType::get(
388367
newShape, originalVecType.getElementType(), newScalableDims);

mlir/test/Conversion/VectorToSCF/vector-to-scf.mlir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,8 @@ func.func @transfer_read_within_async_execute(%A : memref<2x2xf32>) -> !async.to
503503

504504
// CHECK-LABEL: transfer_read_with_tensor
505505
func.func @transfer_read_with_tensor(%arg: tensor<f32>) -> vector<1xf32> {
506-
// CHECK: %[[EXTRACTED:.*]] = tensor.extract %{{.*}}[] : tensor<f32>
507-
// CHECK-NEXT: %[[RESULT:.*]] = vector.broadcast %[[EXTRACTED]] : f32 to vector<1xf32>
506+
// CHECK: %[[EXTRACTED:.*]] = vector.transfer_read %{{.*}}[], %{{.*}} : tensor<f32>, vector<f32>
507+
// CHECK-NEXT: %[[RESULT:.*]] = vector.broadcast %[[EXTRACTED]] : vector<f32> to vector<1xf32>
508508
// CHECK-NEXT: return %[[RESULT]] : vector<1xf32>
509509
%f0 = arith.constant 0.0 : f32
510510
%0 = vector.transfer_read %arg[], %f0 {permutation_map = affine_map<()->(0)>} :

mlir/test/Dialect/Linalg/vectorize-tensor-extract.mlir

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,7 @@ func.func @vectorize_nd_tensor_extract_transfer_read_basic(
136136
// CHECK: %[[READ:.*]] = vector.transfer_read %[[ARG0]][%[[IDX1]], %[[IDX2]], %[[C0:.*]]], %[[CST_0]] {in_bounds = [true, true, true]} : tensor<3x3x3xf32>, vector<1x1x3xf32>
137137
// CHECK: vector.transfer_write %[[READ]], %[[ARG1]][%[[C0]], %[[C0]], %[[C0]]] {in_bounds = [true, true, true]} : vector<1x1x3xf32>, tensor<1x1x3xf32>
138138

139-
// Same as example above, but reading into a column tensor. Note that after the
140-
// vectorizatoin, the `TransferOpReduceRank` will replace
141-
// `vector.transfer_read` with `tensor.extract -> scalar`.
139+
// Same as example above, but reading into a column tensor.
142140

143141
// TODO: Currently this fails to vectorise when the indices are non-constant.
144142

@@ -162,9 +160,10 @@ func.func @vectorize_nd_tensor_extract_transfer_read_basic_column(
162160
// CHECK-LABEL: func.func @vectorize_nd_tensor_extract_transfer_read_basic_column(
163161
// CHECK-SAME: %[[INPUT:.*]]: tensor<3x3x3xf32>,
164162
// CHECK-SAME: %[[OUTPUT:.*]]: tensor<3x1x1xf32>)
165-
// CHECK: %[[C0:.*]] = arith.constant 0 : index
166-
// CHECK: %[[EXTRACT:.*]] = tensor.extract %[[INPUT]]{{\[}}%[[C0]], %[[C0]], %[[C0]]] : tensor<3x3x3xf32>
167-
// CHECK: %[[BCAST:.*]] = vector.broadcast %[[EXTRACT]] : f32 to vector<3x1x1xf32>
163+
// CHECK-DAG: %[[C0:.*]] = arith.constant 0 : index
164+
// CHECK-DAG: %[[CST_0:.*]] = arith.constant 0.000000e+00 : f32
165+
// CHECK: %[[READ:.*]] = vector.transfer_read %[[INPUT]]{{\[}}%[[C0]], %[[C0]], %[[C0]]], %[[CST_0]] : tensor<3x3x3xf32>, vector<f32>
166+
// CHECK: %[[BCAST:.*]] = vector.broadcast %[[READ]] : vector<f32> to vector<3x1x1xf32>
168167
// CHECK: %[[RES:.*]] = vector.transfer_write %[[BCAST]], %[[OUTPUT]]{{\[}}%[[C0]], %[[C0]], %[[C0]]] {in_bounds = [true, true, true]} : vector<3x1x1xf32>, tensor<3x1x1xf32>
169168
// CHECK: return %[[RES]] : tensor<3x1x1xf32>
170169

@@ -541,8 +540,9 @@ func.func @vectorize_nd_tensor_extract_with_tensor_extract(%input_1: tensor<1x20
541540
// CHECK-SAME: %[[INPUT_2:.*]]: tensor<257x24xf32>,
542541
// CHECK: %[[EXTRACTED_0_IDX_0:.*]] = arith.constant 0 : index
543542
// CHECK: %[[EXTRACTED_0_IDX_1:.*]] = vector.extractelement %{{.*}}[%{{.*}} : i32] : vector<4xindex>
544-
// First `tensor.extract` from the generic Op - loop invariant scalar load.
545-
// CHECK: tensor.extract %[[INPUT_1]][%[[EXTRACTED_0_IDX_0]], %[[EXTRACTED_0_IDX_1]]] : tensor<1x20xi32>
543+
// First `vector.transfer_read` from the generic Op - loop invariant scalar load.
544+
// CHECK: vector.transfer_read %[[INPUT_1]][%[[EXTRACTED_0_IDX_0]], %[[EXTRACTED_0_IDX_1]]]
545+
// CHECK-SAME: tensor<1x20xi32>, vector<i32>
546546
// The following `tensor.extract` from the generic Op s a contiguous load (all Ops used
547547
// for address calculation also satisfy the required conditions).
548548
// CHECK: vector.transfer_read %[[INPUT_2]][%{{.*}}, %{{.*}}, %{{.*}} {in_bounds = [true, true]} : tensor<257x24xf32>, vector<1x4xf32>
@@ -745,8 +745,8 @@ func.func @vectorize_0d_tensor_extract(%arg0: tensor<f32>, %arg2: tensor<1x1x3xf
745745

746746
// CHECK-LABEL: func.func @vectorize_0d_tensor_extract(
747747
// CHECK-SAME: %[[ARG_0:.*]]: tensor<f32>
748-
// CHECK: %[[EXTRACT:.*]] = tensor.extract %[[ARG_0]][] : tensor<f32>
749-
// CHECK: vector.broadcast %[[EXTRACT]] : f32 to vector<1x1x3xf32>
748+
// CHECK: %[[EXTRACT:.*]] = vector.transfer_read %[[ARG_0]][], %{{.+}} : tensor<f32>
749+
// CHECK: vector.broadcast %[[EXTRACT]] : vector<f32> to vector<1x1x3xf32>
750750

751751
module attributes {transform.with_named_sequence} {
752752
transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {

mlir/test/Dialect/Vector/vector-transfer-to-vector-load-store.mlir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ func.func @vector_transfer_ops_0d_memref(%mem: memref<f32>, %vec: vector<1x1x1xf
2626
func.func @vector_transfer_ops_0d_tensor(%src: tensor<f32>) -> vector<1xf32> {
2727
%f0 = arith.constant 0.0 : f32
2828

29-
// CHECK-NEXT: %[[S:.*]] = tensor.extract %[[SRC]][] : tensor<f32>
30-
// CHECK-NEXT: %[[V:.*]] = vector.broadcast %[[S]] : f32 to vector<1xf32>
29+
// CHECK: %[[S:.*]] = vector.transfer_read %[[SRC]][]
30+
// CHECK: %[[V:.*]] = vector.broadcast %[[S]] : vector<f32> to vector<1xf32>
3131
%res = vector.transfer_read %src[], %f0 {in_bounds = [true], permutation_map = affine_map<()->(0)>} :
3232
tensor<f32>, vector<1xf32>
3333

0 commit comments

Comments
 (0)