Skip to content

[MLIR][Vector] Fix bug in ExtractStrideSlicesOp canonicalization #147591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
39 changes: 23 additions & 16 deletions mlir/lib/Dialect/Vector/IR/VectorOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4169,28 +4169,35 @@ class StridedSliceBroadcast final
auto dstVecType = llvm::cast<VectorType>(op.getType());
unsigned dstRank = dstVecType.getRank();
unsigned rankDiff = dstRank - srcRank;
// Check if the most inner dimensions of the source of the broadcast are the
// same as the destination of the extract. If this is the case we can just
// use a broadcast as the original dimensions are untouched.
bool lowerDimMatch = true;
// Source dimensions can be broadcasted (1 -> n with n > 1) or sliced
// (n -> m with n > m). If they are originally both broadcasted *and*
// sliced, this can be simplified to just broadcasting.
bool needsSlice = false;
for (unsigned i = 0; i < srcRank; i++) {
if (srcVecType.getDimSize(i) != dstVecType.getDimSize(i + rankDiff)) {
lowerDimMatch = false;
if (srcVecType.getDimSize(i) != 1 &&
srcVecType.getDimSize(i) != dstVecType.getDimSize(i + rankDiff)) {
needsSlice = true;
break;
}
}
Value source = broadcast.getSource();
// If the inner dimensions don't match, it means we need to extract from the
// source of the orignal broadcast and then broadcast the extracted value.
// We also need to handle degenerated cases where the source is effectively
// just a single scalar.
bool isScalarSrc = (srcRank == 0 || srcVecType.getNumElements() == 1);
if (!lowerDimMatch && !isScalarSrc) {
if (needsSlice) {
SmallVector<int64_t> offsets =
getI64SubArray(op.getOffsets(), /*dropFront=*/rankDiff);
SmallVector<int64_t> sizes =
getI64SubArray(op.getSizes(), /*dropFront=*/rankDiff);
for (unsigned i = 0; i < srcRank; i++) {
if (srcVecType.getDimSize(i) == 1) {
// In case this dimension was broadcasted *and* sliced, the offset
// and size need to be updated now that there is no broadcast before
// the slice.
offsets[i] = 0;
sizes[i] = 1;
}
}
source = rewriter.create<ExtractStridedSliceOp>(
op->getLoc(), source,
getI64SubArray(op.getOffsets(), /* dropFront=*/rankDiff),
getI64SubArray(op.getSizes(), /* dropFront=*/rankDiff),
getI64SubArray(op.getStrides(), /* dropFront=*/rankDiff));
op->getLoc(), source, offsets, sizes,
getI64SubArray(op.getStrides(), /*dropFront=*/rankDiff));
}
rewriter.replaceOpWithNewOp<BroadcastOp>(op, op.getType(), source);
return success();
Expand Down
15 changes: 15 additions & 0 deletions mlir/test/Dialect/Vector/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,21 @@ func.func @extract_strided_broadcast4(%arg0: f32) -> vector<1x4xf32> {

// -----

// Check the case where the same dimension is both broadcasted and sliced
// CHECK-LABEL: func @extract_strided_broadcast5
// CHECK-SAME: (%[[ARG:.+]]: vector<2x1xf32>)
// CHECK: %[[V:.+]] = vector.broadcast %[[ARG]] : vector<2x1xf32> to vector<2x4xf32>
// CHECK: return %[[V]]
func.func @extract_strided_broadcast5(%arg0: vector<2x1xf32>) -> vector<2x4xf32> {
%0 = vector.broadcast %arg0 : vector<2x1xf32> to vector<2x8xf32>
%1 = vector.extract_strided_slice %0
{offsets = [0, 4], sizes = [2, 4], strides = [1, 1]}
: vector<2x8xf32> to vector<2x4xf32>
return %1 : vector<2x4xf32>
}

// -----

// CHECK-LABEL: consecutive_shape_cast
// CHECK: %[[C:.*]] = vector.shape_cast %{{.*}} : vector<16xf16> to vector<4x4xf16>
// CHECK-NEXT: return %[[C]] : vector<4x4xf16>
Expand Down
Loading