Skip to content

Commit 7c08a8b

Browse files
committed
[mlir][vector] Clarify the semantics of BroadcastOp
Clarifies the semantics of `vector.broadcast` in the context of scalable vectors. In particular, broadcasting a unit scalable dim, `[1]`, is not valid unless there's a match between the output and the input dims. See the examples below for an illustration: ```mlir // VALID %0 = vector.broadcast %arg0 : vector<[1]xf32> to vector<4x[1]xf32> // INVALID %0 = vector.broadcast %arg0 : vector<[1]xf32> to vector<[4]xf32> // VALID FIXED-WIDTH EQUIVALENT %0 = vector.broadcast %arg0 : vector<1xf32> to vector<4xf32> ``` Documentation, the Op verifier and tests are updated accordingly.
1 parent 0dcada9 commit 7c08a8b

File tree

4 files changed

+56
-11
lines changed

4 files changed

+56
-11
lines changed

mlir/include/mlir/Dialect/Vector/IR/VectorOps.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,13 @@ enum class BroadcastableToResult {
6868
DimensionMismatch = 2,
6969
SourceTypeNotAVector = 3
7070
};
71+
struct VectorDim {
72+
int64_t dim;
73+
bool scalableFlag;
74+
};
7175
BroadcastableToResult
7276
isBroadcastableTo(Type srcType, VectorType dstVectorType,
73-
std::pair<int, int> *mismatchingDims = nullptr);
77+
std::pair<VectorDim, VectorDim> *mismatchingDims = nullptr);
7478

7579
/// Collect a set of vector-to-vector canonicalization patterns.
7680
void populateVectorToVectorCanonicalizationPatterns(RewritePatternSet &patterns,

mlir/include/mlir/Dialect/Vector/IR/VectorOps.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,8 @@ def Vector_BroadcastOp :
367367
s_1 x .. x s_j x .. x s_k
368368
<duplication> <potential stretch>
369369
```
370+
* a scalable unit dimeension, `[1]`, must match exactly.
371+
370372
The source operand is duplicated over all the missing leading dimensions
371373
and stretched over the trailing dimensions where the source has a non-equal
372374
dimension of 1. These rules imply that any scalar broadcast (k=0) to any

mlir/lib/Dialect/Vector/IR/VectorOps.cpp

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,9 +2371,9 @@ Value BroadcastOp::createOrFoldBroadcastOp(
23712371
return res;
23722372
}
23732373

2374-
BroadcastableToResult
2375-
mlir::vector::isBroadcastableTo(Type srcType, VectorType dstVectorType,
2376-
std::pair<int, int> *mismatchingDims) {
2374+
BroadcastableToResult mlir::vector::isBroadcastableTo(
2375+
Type srcType, VectorType dstVectorType,
2376+
std::pair<VectorDim, VectorDim> *mismatchingDims) {
23772377
// Broadcast scalar to vector of the same element type.
23782378
if (srcType.isIntOrIndexOrFloat() && dstVectorType &&
23792379
getElementTypeOrSelf(srcType) == getElementTypeOrSelf(dstVectorType))
@@ -2391,12 +2391,28 @@ mlir::vector::isBroadcastableTo(Type srcType, VectorType dstVectorType,
23912391
// (all leading dimensions are simply duplicated).
23922392
int64_t lead = dstRank - srcRank;
23932393
for (int64_t r = 0; r < srcRank; ++r) {
2394+
bool mismatch = false;
2395+
2396+
// Check fixed-width dims
23942397
int64_t srcDim = srcVectorType.getDimSize(r);
23952398
int64_t dstDim = dstVectorType.getDimSize(lead + r);
2396-
if (srcDim != 1 && srcDim != dstDim) {
2399+
if ((srcDim != 1 && srcDim != dstDim))
2400+
mismatch = true;
2401+
2402+
// Check scalable flags
2403+
bool srcDimScalableFlag = srcVectorType.getScalableDims()[r];
2404+
bool dstDimScalableFlag = dstVectorType.getScalableDims()[lead + r];
2405+
if ((srcDim == 1 && srcDimScalableFlag && dstDim != 1) ||
2406+
(srcDimScalableFlag && !dstDimScalableFlag))
2407+
mismatch = true;
2408+
2409+
if (mismatch) {
23972410
if (mismatchingDims) {
2398-
mismatchingDims->first = srcDim;
2399-
mismatchingDims->second = dstDim;
2411+
mismatchingDims->first.dim = srcDim;
2412+
mismatchingDims->first.scalableFlag = srcDimScalableFlag;
2413+
2414+
mismatchingDims->second.dim = dstDim;
2415+
mismatchingDims->second.scalableFlag = dstDimScalableFlag;
24002416
}
24012417
return BroadcastableToResult::DimensionMismatch;
24022418
}
@@ -2406,16 +2422,25 @@ mlir::vector::isBroadcastableTo(Type srcType, VectorType dstVectorType,
24062422
}
24072423

24082424
LogicalResult BroadcastOp::verify() {
2409-
std::pair<int, int> mismatchingDims;
2425+
std::pair<VectorDim, VectorDim> mismatchingDims;
24102426
BroadcastableToResult res = isBroadcastableTo(
24112427
getSourceType(), getResultVectorType(), &mismatchingDims);
24122428
if (res == BroadcastableToResult::Success)
24132429
return success();
24142430
if (res == BroadcastableToResult::SourceRankHigher)
24152431
return emitOpError("source rank higher than destination rank");
2416-
if (res == BroadcastableToResult::DimensionMismatch)
2417-
return emitOpError("dimension mismatch (")
2418-
<< mismatchingDims.first << " vs. " << mismatchingDims.second << ")";
2432+
if (res == BroadcastableToResult::DimensionMismatch) {
2433+
std::string msg =
2434+
(Twine("dimension mismatch (") +
2435+
(mismatchingDims.first.scalableFlag ? "[" : "") +
2436+
std::to_string(mismatchingDims.first.dim) +
2437+
(mismatchingDims.first.scalableFlag ? "]" : "") + " vs. " +
2438+
(mismatchingDims.second.scalableFlag ? "[" : "") +
2439+
std::to_string(mismatchingDims.second.dim) +
2440+
(mismatchingDims.second.scalableFlag ? "]" : "") + ")")
2441+
.str();
2442+
return emitOpError(msg);
2443+
}
24192444
if (res == BroadcastableToResult::SourceTypeNotAVector)
24202445
return emitOpError("source type is not a vector");
24212446
llvm_unreachable("unexpected vector.broadcast op error");

mlir/test/Dialect/Vector/invalid.mlir

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ func.func @broadcast_dim2_mismatch(%arg0: vector<4x8xf32>) {
3535

3636
// -----
3737

38+
func.func @broadcast_scalable_unit_dim(%arg0: vector<[1]xf32>) {
39+
// expected-error@+1 {{'vector.broadcast' op dimension mismatch ([1] vs. [4])}}
40+
%0 = vector.broadcast %arg0 : vector<[1]xf32> to vector<[4]xf32>
41+
}
42+
43+
// -----
44+
45+
func.func @broadcast_scalable_to_fixed(%arg0: vector<[1]xf32>) {
46+
// expected-error@+1 {{'vector.broadcast' op dimension mismatch ([1] vs. 1)}}
47+
%0 = vector.broadcast %arg0 : vector<[1]xf32> to vector<4x1xf32>
48+
}
49+
50+
// -----
51+
3852
func.func @broadcast_unknown(%arg0: memref<4x8xf32>) {
3953
// expected-error@+1 {{'vector.broadcast' op source type is not a vector}}
4054
%1 = vector.broadcast %arg0 : memref<4x8xf32> to vector<1x8xf32>

0 commit comments

Comments
 (0)