Skip to content

[mlir][vector][nfc] Add clarification on "dim-1" bcast #125425

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

Merged
merged 1 commit into from
Feb 11, 2025
Merged
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
7 changes: 4 additions & 3 deletions mlir/include/mlir/Dialect/Vector/IR/VectorOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,9 @@ def Vector_BroadcastOp :

The source operand is duplicated over all the missing leading dimensions
and stretched over the trailing dimensions where the source has a non-equal
dimension of 1. These rules imply that any scalar broadcast (k=0) to any
shaped vector with the same element type is always legal.
dimension of 1 (stretching a trailing dimension is also referred to as
"dim-1" broadcasting). These rules imply that any scalar broadcast (k=0) to
any shaped vector with the same element type is always legal.

Example:

Expand All @@ -396,7 +397,7 @@ def Vector_BroadcastOp :

/// Broadcast `value` to a vector of `dstShape`, knowing that exactly the
/// `broadcastedDims` dimensions in the dstShape are broadcasted.
/// This requires (and asserts) that the broadcast is free of dim-1
/// This requires (and asserts) that the broadcast is free of "dim-1"
/// broadcasting.
/// Since vector.broadcast only allows expanding leading dimensions, an extra
/// vector.transpose may be inserted to make the broadcast possible.
Expand Down
8 changes: 4 additions & 4 deletions mlir/lib/Dialect/Vector/IR/VectorOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2355,7 +2355,7 @@ computeBroadcastedUnitDims(ArrayRef<int64_t> srcShape,
for (auto [s1, s2] :
llvm::zip_equal(srcShape, dstShape.drop_front(rankDiff))) {
if (s1 != s2) {
assert(s1 == 1 && "expected dim-1 broadcasting");
assert(s1 == 1 && "expected \"dim-1\" broadcasting");
res.insert(dstDim);
}
++dstDim;
Expand All @@ -2374,7 +2374,7 @@ llvm::SetVector<int64_t> BroadcastOp::computeBroadcastedUnitDims() {

/// Broadcast `value` to a vector of `dstShape`, knowing that exactly the
/// `broadcastedDims` dimensions in the dstShape are broadcasted.
/// This requires (and asserts) that the broadcast is free of dim-1
/// This requires (and asserts) that the broadcast is free of "dim-1"
/// broadcasting.
/// Since vector.broadcast only allows expanding leading dimensions, an extra
/// vector.transpose may be inserted to make the broadcast possible.
Expand Down Expand Up @@ -2460,10 +2460,10 @@ Value BroadcastOp::createOrFoldBroadcastOp(
// 3.c. Append the srcShape.
llvm::append_range(broadcastShape, srcVectorType.getShape());

// Ensure there are no dim-1 broadcasts.
// Ensure there are no "dim-1" broadcasts.
assert(::computeBroadcastedUnitDims(srcVectorType.getShape(), broadcastShape)
.empty() &&
"unexpected dim-1 broadcast");
"unexpected \"dim-1\" broadcast");

VectorType broadcastType = VectorType::get(broadcastShape, elementType);
assert(vector::isBroadcastableTo(value.getType(), broadcastType) ==
Expand Down