Skip to content

[mlir][vector] Fix mask rank extension for TransferWriteNonPermutationLowering #144893

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
45 changes: 26 additions & 19 deletions mlir/lib/Dialect/Vector/Transforms/LowerVectorTransfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ inverseTransposeInBoundsAttr(OpBuilder &builder, ArrayAttr attr,

/// Extend the rank of a vector Value by `addedRanks` by adding outer unit
/// dimensions.
static Value extendVectorRank(OpBuilder &builder, Location loc, Value vec,
int64_t addedRank) {
static TypedValue<VectorType> extendVectorRank(OpBuilder &builder, Location loc,
Value vec, int64_t addedRank) {
auto originalVecType = cast<VectorType>(vec.getType());
SmallVector<int64_t> newShape(addedRank, 1);
newShape.append(originalVecType.getShape().begin(),
Expand All @@ -53,16 +53,21 @@ static Value extendVectorRank(OpBuilder &builder, Location loc, Value vec,
/// Extend the rank of a vector Value by `addedRanks` by adding inner unit
/// dimensions.
static Value extendMaskRank(OpBuilder &builder, Location loc, Value vec,
int64_t addedRank) {
Value broadcasted = extendVectorRank(builder, loc, vec, addedRank);
SmallVector<int64_t> permutation;
for (int64_t i = addedRank,
e = cast<VectorType>(broadcasted.getType()).getRank();
i < e; ++i)
permutation.push_back(i);
for (int64_t i = 0; i < addedRank; ++i)
permutation.push_back(i);
return builder.create<vector::TransposeOp>(loc, broadcasted, permutation);
ArrayRef<int64_t> missingInnerDims) {
TypedValue<VectorType> broadcasted =
extendVectorRank(builder, loc, vec, missingInnerDims.size());
SmallVector<bool> missing(broadcasted.getType().getRank(), false);
SmallVector<int64_t> inversePerm;
for (int64_t i : missingInnerDims) {
inversePerm.push_back(i);
missing[i] = true;
}
for (auto [i, used] : llvm::enumerate(missing)) {
if (!used)
inversePerm.push_back(i);
}
return builder.create<vector::TransposeOp>(
loc, broadcasted, invertPermutationVector(inversePerm));
}

//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -268,28 +273,30 @@ struct TransferWriteNonPermutationLowering
for (AffineExpr exp : map.getResults())
foundDim[cast<AffineDimExpr>(exp).getPosition()] = true;
SmallVector<AffineExpr> exprs;
bool foundFirstDim = false;
std::optional<int64_t> firstDim = std::nullopt;
SmallVector<int64_t> missingInnerDim;
for (size_t i = 0; i < foundDim.size(); i++) {
if (foundDim[i]) {
foundFirstDim = true;
if (!firstDim) {
firstDim = i;
}
continue;
}
if (!foundFirstDim)
if (!firstDim)
continue;
// Once we found one outer dimension existing in the map keep track of all
// the missing dimensions after that.
missingInnerDim.push_back(i);
missingInnerDim.push_back(i - firstDim.value());
exprs.push_back(rewriter.getAffineDimExpr(i));
}
// Vector: add unit dims at the beginning of the shape.
Value newVec = extendVectorRank(rewriter, op.getLoc(), op.getVector(),
missingInnerDim.size());
// Mask: add unit dims at the end of the shape.
// Mask: add unit dims at the positions of the missing dimensions.
Value newMask;
if (op.getMask())
newMask = extendMaskRank(rewriter, op.getLoc(), op.getMask(),
missingInnerDim.size());
newMask =
extendMaskRank(rewriter, op.getLoc(), op.getMask(), missingInnerDim);
exprs.append(map.getResults().begin(), map.getResults().end());
AffineMap newMap =
AffineMap::get(map.getNumDims(), 0, exprs, op.getContext());
Expand Down
25 changes: 25 additions & 0 deletions mlir/test/Dialect/Vector/vector-transfer-permutation-lowering.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,31 @@ func.func @xfer_write_non_minor_identity_with_mask_out_of_bounds(
return
}

// CHECK-LABEL: func.func @xfer_write_non_minor_identity_with_mask_broadcast(
// CHECK-SAME: %[[MEM:.*]]: memref<?x?x?xf32>,
// CHECK-SAME: %[[VEC:.*]]: vector<7x8xf32>,
// CHECK-SAME: %[[MASK:.*]]: vector<7x8xi1>,
// CHECK-SAME: %[[IDX_1:.*]]: index, %[[IDX_2:.*]]: index, %[[IDX_3:.*]]: index) {
// CHECK: %[[BC:.*]] = vector.broadcast %[[VEC]] : vector<7x8xf32> to vector<1x7x8xf32>
// CHECK: %[[MBC:.*]] = vector.broadcast %[[MASK]] : vector<7x8xi1> to vector<1x7x8xi1>
// CHECK: %[[MTR:.*]] = vector.transpose %[[MBC]], [1, 0, 2] : vector<1x7x8xi1> to vector<7x1x8xi1>
// CHECK: %[[TR:.*]] = vector.transpose %[[BC]], [1, 0, 2] : vector<1x7x8xf32> to vector<7x1x8xf32>
// CHECK: vector.transfer_write %[[TR]], %[[MEM]]{{\[}}%[[IDX_1]], %[[IDX_2]], %[[IDX_3]]], %[[MTR]] {in_bounds = [false, true, false]} : vector<7x1x8xf32>, memref<?x?x?xf32>
func.func @xfer_write_non_minor_identity_with_mask_broadcast_and_transpose(
Copy link
Contributor

Choose a reason for hiding this comment

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

Based on the name (@xfer_write_non_minor_identity_with_mask_broadcast_and_transpose), I would expect to see explicit broadcast when comparing this to @xfer_write_non_minor_identity_with_mask_out_of_bounds. However, the only difference I can see are input ranks.

Don't want to come across as nit-picking, just trying to figure out what cases this PR fixes. Thanks!

%mem : memref<?x?x?xf32>,
%vec : vector<7x8xf32>,
%mask : vector<7x8xi1>,
%idx_1 : index,
%idx_2 : index,
%idx_3 : index) {

vector.transfer_write %vec, %mem[%idx_1, %idx_2, %idx_3], %mask {
permutation_map = affine_map<(d0, d1, d2) -> (d0, d2)>
} : vector<7x8xf32>, memref<?x?x?xf32>

return
}

// CHECK-LABEL: func.func @xfer_write_non_minor_identity_with_mask_scalable(
// CHECK-SAME: %[[VEC:.*]]: vector<4x[8]xi16>,
// CHECK-SAME: %[[MEM:.*]]: memref<1x4x?x1xi16>,
Expand Down
Loading