Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions mlir/lib/Dialect/Vector/Transforms/LowerVectorTransfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,17 +360,35 @@ struct TransferOpReduceRank
SmallVector<bool> newScalableDims(
originalVecType.getScalableDims().take_back(reducedShapeRank));

VectorType newReadType = VectorType::get(
newShape, originalVecType.getElementType(), newScalableDims);
ArrayAttr newInBoundsAttr =
op.getInBounds()
? rewriter.getArrayAttr(
op.getInBoundsAttr().getValue().take_back(reducedShapeRank))
: ArrayAttr();
Value newRead = vector::TransferReadOp::create(
rewriter, op.getLoc(), newReadType, op.getBase(), op.getIndices(),
AffineMapAttr::get(newMap), op.getPadding(), op.getMask(),
newInBoundsAttr);
Value newRead;
if (newShape.size() == 0 && newScalableDims.size() == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no rank-0 scalable vectors, so you can skip the 2nd check.

// Handle the scalar case.
// Convert
// %val = vector.transfer_read %base[] : memref<dtype> to
// vector<d0 x d1 x dtype>
// into
// %scalar = memref.load %base[] : memref<dtype>
// %val = vector.broadcast %scalar : dtype to vector<d0 x d1 x dtype>
Type baseType = op.getBase().getType();
if (isa<MemRefType>(baseType)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not support this for Tensors as well?

newRead = memref::LoadOp::create(rewriter, op.getLoc(), op.getBase(),
op.getIndices());
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Handle the non-scalar case.

(and then, above if (newShape.size() == 0 && newScalableDims.size() == 0) {, // Handle the scalar case).

if (!newRead) {
VectorType newReadType = VectorType::get(
newShape, originalVecType.getElementType(), newScalableDims);
ArrayAttr newInBoundsAttr =
op.getInBounds()
? rewriter.getArrayAttr(
op.getInBoundsAttr().getValue().take_back(reducedShapeRank))
: ArrayAttr();
newRead = vector::TransferReadOp::create(
rewriter, op.getLoc(), newReadType, op.getBase(), op.getIndices(),
AffineMapAttr::get(newMap), op.getPadding(), op.getMask(),
newInBoundsAttr);
}
return vector::BroadcastOp::create(rewriter, op.getLoc(), originalVecType,
newRead)
.getVector();
Expand Down
18 changes: 18 additions & 0 deletions mlir/test/Dialect/Vector/vector-transfer-permutation-lowering.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,24 @@ func.func @xfer_read_minor_identitiy_bcast_dims(
return %res : vector<8x4x2x3xf32>
}

// CHECK-LABEL: func.func @xfer_read_minor_identitiy_bcast_scalar
// CHECK-SAME: %[[MEM:.*]]: memref<f32>) -> vector<8x4x2x3xf32> {
// CHECK: %[[LOAD:.*]] = memref.load %[[MEM]][] : memref<f32>
// CHECK: %[[BC:.*]] = vector.broadcast %[[LOAD]] : f32 to vector<8x4x2x3xf32>
// CHECK: return %[[BC]] : vector<8x4x2x3xf32>
func.func @xfer_read_minor_identitiy_bcast_scalar(
%mem: memref<f32>) -> vector<8x4x2x3xf32> {

%pad = arith.constant 0.000000e+00 : f32

%res = vector.transfer_read %mem[], %pad {
in_bounds = [true, true, true, true],
permutation_map = affine_map<() -> (0, 0, 0, 0)>
} : memref<f32>, vector<8x4x2x3xf32>

return %res : vector<8x4x2x3xf32>
}

// CHECK-LABEL: func.func @xfer_read_minor_identitiy_bcast_dims_scalable
// CHECK-SAME: %[[MEM:.*]]: memref<?x?x?x?xf32>, %[[IDX:.*]]: index) -> vector<8x[4]x2x3xf32> {
// CHECK: %[[T_READ:.*]] = vector.transfer_read %[[MEM]][%[[IDX]], %[[IDX]], %[[IDX]], %[[IDX]]]{{.*}} permutation_map = #[[$MAP]]} : memref<?x?x?x?xf32>, vector<[4]x2x3xf32>
Expand Down