Skip to content

Commit 1919db9

Browse files
authored
[mlir][vector] Clarify the semantics of BroadcastOp (#101928)
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 3423470 commit 1919db9

File tree

4 files changed

+66
-14
lines changed

4 files changed

+66
-14
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,14 @@ enum class BroadcastableToResult {
6868
DimensionMismatch = 2,
6969
SourceTypeNotAVector = 3
7070
};
71+
72+
struct VectorDim {
73+
int64_t dim;
74+
bool isScalable;
75+
};
7176
BroadcastableToResult
7277
isBroadcastableTo(Type srcType, VectorType dstVectorType,
73-
std::pair<int, int> *mismatchingDims = nullptr);
78+
std::pair<VectorDim, VectorDim> *mismatchingDims = nullptr);
7479

7580
/// Collect a set of vector-to-vector canonicalization patterns.
7681
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+
* in addition, any scalable unit dimension, `[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: 37 additions & 13 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))
@@ -2390,13 +2390,31 @@ mlir::vector::isBroadcastableTo(Type srcType, VectorType dstVectorType,
23902390
// Source has an exact match or singleton value for all trailing dimensions
23912391
// (all leading dimensions are simply duplicated).
23922392
int64_t lead = dstRank - srcRank;
2393-
for (int64_t r = 0; r < srcRank; ++r) {
2394-
int64_t srcDim = srcVectorType.getDimSize(r);
2395-
int64_t dstDim = dstVectorType.getDimSize(lead + r);
2396-
if (srcDim != 1 && srcDim != dstDim) {
2397-
if (mismatchingDims) {
2398-
mismatchingDims->first = srcDim;
2399-
mismatchingDims->second = dstDim;
2393+
for (int64_t dimIdx = 0; dimIdx < srcRank; ++dimIdx) {
2394+
// Have mismatching dims (in the sense of vector.broadcast semantics) been
2395+
// encountered?
2396+
bool foundMismatchingDims = false;
2397+
2398+
// Check fixed-width dims.
2399+
int64_t srcDim = srcVectorType.getDimSize(dimIdx);
2400+
int64_t dstDim = dstVectorType.getDimSize(lead + dimIdx);
2401+
if (srcDim != 1 && srcDim != dstDim)
2402+
foundMismatchingDims = true;
2403+
2404+
// Check scalable flags.
2405+
bool srcDimScalableFlag = srcVectorType.getScalableDims()[dimIdx];
2406+
bool dstDimScalableFlag = dstVectorType.getScalableDims()[lead + dimIdx];
2407+
if ((srcDim == 1 && srcDimScalableFlag && dstDim != 1) ||
2408+
(srcDimScalableFlag != dstDimScalableFlag))
2409+
foundMismatchingDims = true;
2410+
2411+
if (foundMismatchingDims) {
2412+
if (mismatchingDims != nullptr) {
2413+
mismatchingDims->first.dim = srcDim;
2414+
mismatchingDims->first.isScalable = srcDimScalableFlag;
2415+
2416+
mismatchingDims->second.dim = dstDim;
2417+
mismatchingDims->second.isScalable = dstDimScalableFlag;
24002418
}
24012419
return BroadcastableToResult::DimensionMismatch;
24022420
}
@@ -2406,16 +2424,22 @@ mlir::vector::isBroadcastableTo(Type srcType, VectorType dstVectorType,
24062424
}
24072425

24082426
LogicalResult BroadcastOp::verify() {
2409-
std::pair<int, int> mismatchingDims;
2427+
std::pair<VectorDim, VectorDim> mismatchingDims;
24102428
BroadcastableToResult res = isBroadcastableTo(
24112429
getSourceType(), getResultVectorType(), &mismatchingDims);
24122430
if (res == BroadcastableToResult::Success)
24132431
return success();
24142432
if (res == BroadcastableToResult::SourceRankHigher)
24152433
return emitOpError("source rank higher than destination rank");
2416-
if (res == BroadcastableToResult::DimensionMismatch)
2434+
if (res == BroadcastableToResult::DimensionMismatch) {
24172435
return emitOpError("dimension mismatch (")
2418-
<< mismatchingDims.first << " vs. " << mismatchingDims.second << ")";
2436+
<< (mismatchingDims.first.isScalable ? "[" : "")
2437+
<< mismatchingDims.first.dim
2438+
<< (mismatchingDims.first.isScalable ? "]" : "") << " vs. "
2439+
<< (mismatchingDims.second.isScalable ? "[" : "")
2440+
<< mismatchingDims.second.dim
2441+
<< (mismatchingDims.second.isScalable ? "]" : "") << ")";
2442+
}
24192443
if (res == BroadcastableToResult::SourceTypeNotAVector)
24202444
return emitOpError("source type is not a vector");
24212445
llvm_unreachable("unexpected vector.broadcast op error");

mlir/test/Dialect/Vector/invalid.mlir

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,27 @@ 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_fixed_to_scalable(%arg0: vector<2xf32>) {
46+
// expected-error@+1 {{'vector.broadcast' op dimension mismatch (2 vs. [2])}}
47+
%0 = vector.broadcast %arg0 : vector<2xf32> to vector<[2]xf32>
48+
}
49+
50+
// -----
51+
52+
func.func @broadcast_scalable_to_fixed(%arg0: vector<[1]xf32>) {
53+
// expected-error@+1 {{'vector.broadcast' op dimension mismatch ([1] vs. 1)}}
54+
%0 = vector.broadcast %arg0 : vector<[1]xf32> to vector<4x1xf32>
55+
}
56+
57+
// -----
58+
3859
func.func @broadcast_unknown(%arg0: memref<4x8xf32>) {
3960
// expected-error@+1 {{'vector.broadcast' op source type is not a vector}}
4061
%1 = vector.broadcast %arg0 : memref<4x8xf32> to vector<1x8xf32>

0 commit comments

Comments
 (0)