Skip to content

Commit e2bc3fc

Browse files
committed
[mlir][Vector] Support 0-d vectors natively in TransferOpReduceRank
Since https://reviews.llvm.org/D114086 , 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 c742a5d commit e2bc3fc

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
@@ -109,9 +109,7 @@ func.func @vectorize_nd_tensor_extract_transfer_read_basic(
109109
// CHECK: %[[READ:.*]] = vector.transfer_read %[[ARG0]][%[[IDX1]], %[[IDX2]], %[[C0:.*]]], %[[CST_0]] {in_bounds = [true, true, true]} : tensor<3x3x3xf32>, vector<1x1x3xf32>
110110
// CHECK: vector.transfer_write %[[READ]], %[[ARG1]][%[[C0]], %[[C0]], %[[C0]]] {in_bounds = [true, true, true]} : vector<1x1x3xf32>, tensor<1x1x3xf32>
111111

112-
// Same as example above, but reading into a column tensor. Note that after the
113-
// vectorizatoin, the `TransferOpReduceRank` will replace
114-
// `vector.transfer_read` with `tensor.extract -> scalar`.
112+
// Same as example above, but reading into a column tensor.
115113

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

@@ -135,9 +133,10 @@ func.func @vectorize_nd_tensor_extract_transfer_read_basic_column(
135133
// CHECK-LABEL: func.func @vectorize_nd_tensor_extract_transfer_read_basic_column(
136134
// CHECK-SAME: %[[INPUT:.*]]: tensor<3x3x3xf32>,
137135
// CHECK-SAME: %[[OUTPUT:.*]]: tensor<3x1x1xf32>)
138-
// CHECK: %[[C0:.*]] = arith.constant 0 : index
139-
// CHECK: %[[EXTRACT:.*]] = tensor.extract %[[INPUT]]{{\[}}%[[C0]], %[[C0]], %[[C0]]] : tensor<3x3x3xf32>
140-
// CHECK: %[[BCAST:.*]] = vector.broadcast %[[EXTRACT]] : f32 to vector<3x1x1xf32>
136+
// CHECK-DAG: %[[C0:.*]] = arith.constant 0 : index
137+
// CHECK-DAG: %[[CST_0:.*]] = arith.constant 0.000000e+00 : f32
138+
// CHECK: %[[READ:.*]] = vector.transfer_read %[[INPUT]]{{\[}}%[[C0]], %[[C0]], %[[C0]]], %[[CST_0]] : tensor<3x3x3xf32>, vector<f32>
139+
// CHECK: %[[BCAST:.*]] = vector.broadcast %[[READ]] : vector<f32> to vector<3x1x1xf32>
141140
// CHECK: %[[RES:.*]] = vector.transfer_write %[[BCAST]], %[[OUTPUT]]{{\[}}%[[C0]], %[[C0]], %[[C0]]] {in_bounds = [true, true, true]} : vector<3x1x1xf32>, tensor<3x1x1xf32>
142141
// CHECK: return %[[RES]] : tensor<3x1x1xf32>
143142

@@ -514,8 +513,9 @@ func.func @vectorize_nd_tensor_extract_with_tensor_extract(%input_1: tensor<1x20
514513
// CHECK-SAME: %[[INPUT_2:.*]]: tensor<257x24xf32>,
515514
// CHECK: %[[EXTRACTED_0_IDX_0:.*]] = arith.constant 0 : index
516515
// CHECK: %[[EXTRACTED_0_IDX_1:.*]] = vector.extractelement %{{.*}}[%{{.*}} : i32] : vector<4xindex>
517-
// First `tensor.extract` from the generic Op - loop invariant scalar load.
518-
// CHECK: tensor.extract %[[INPUT_1]][%[[EXTRACTED_0_IDX_0]], %[[EXTRACTED_0_IDX_1]]] : tensor<1x20xi32>
516+
// First `vector.transfer_read` from the generic Op - loop invariant scalar load.
517+
// CHECK: vector.transfer_read %[[INPUT_1]][%[[EXTRACTED_0_IDX_0]], %[[EXTRACTED_0_IDX_1]]]
518+
// CHECK-SAME: tensor<1x20xi32>, vector<i32>
519519
// The following `tensor.extract` from the generic Op s a contiguous load (all Ops used
520520
// for address calculation also satisfy the required conditions).
521521
// CHECK: vector.transfer_read %[[INPUT_2]][%{{.*}}, %{{.*}}, %{{.*}} {in_bounds = [true, true]} : tensor<257x24xf32>, vector<1x4xf32>
@@ -718,8 +718,8 @@ func.func @vectorize_0d_tensor_extract(%arg0: tensor<f32>, %arg2: tensor<1x1x3xf
718718

719719
// CHECK-LABEL: func.func @vectorize_0d_tensor_extract(
720720
// CHECK-SAME: %[[ARG_0:.*]]: tensor<f32>
721-
// CHECK: %[[EXTRACT:.*]] = tensor.extract %[[ARG_0]][] : tensor<f32>
722-
// CHECK: vector.broadcast %[[EXTRACT]] : f32 to vector<1x1x3xf32>
721+
// CHECK: %[[EXTRACT:.*]] = vector.transfer_read %[[ARG_0]][], %{{.+}} : tensor<f32>
722+
// CHECK: vector.broadcast %[[EXTRACT]] : vector<f32> to vector<1x1x3xf32>
723723

724724
module attributes {transform.with_named_sequence} {
725725
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)