Skip to content

Commit ee9a5cb

Browse files
committed
[mlir][Vector] Improve vector.mask verifier
This PR improves the verifier for the `vector.mask` operation to make sure it's not applying masking semantics to operations defined outside of the `vector.mask` region. Documentation is updated to emphasize that and make it clearer, even though it already stated that. As part of this change, the logic that ensures that a terminator is present in the region mask has been simplified to make it less surprising to the user when a `vector.yield` is explicitly provided.
1 parent 286ab11 commit ee9a5cb

File tree

3 files changed

+59
-19
lines changed

3 files changed

+59
-19
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2482,8 +2482,13 @@ def Vector_MaskOp : Vector_Op<"mask", [
24822482
masked. Values used within the region are captured from above. Only one
24832483
*maskable* operation can be masked with a `vector.mask` operation at a time.
24842484
An operation is *maskable* if it implements the `MaskableOpInterface`. The
2485-
terminator yields all results of the maskable operation to the result of
2486-
this operation.
2485+
terminator yields all results from the maskable operation to the result of
2486+
this operation. No other values are allowed to be yielded.
2487+
2488+
An empty `vector.mask` operation is considered ill-formed but legal to
2489+
facilitate optimizations across the `vector.mask` operation. It is considered
2490+
a no-op regardless of its returned values and will be removed by the
2491+
canonicalizer.
24872492

24882493
The vector mask argument holds a bit for each vector lane and determines
24892494
which vector lanes should execute the maskable operation and which ones

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

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6543,29 +6543,31 @@ void mlir::vector::MaskOp::print(OpAsmPrinter &p) {
65436543
}
65446544

65456545
void MaskOp::ensureTerminator(Region &region, Builder &builder, Location loc) {
6546-
OpTrait::SingleBlockImplicitTerminator<vector::YieldOp>::Impl<
6547-
MaskOp>::ensureTerminator(region, builder, loc);
6548-
// Keep the default yield terminator if the number of masked operations is not
6549-
// the expected. This case will trigger a verification failure.
6550-
Block &block = region.front();
6551-
if (block.getOperations().size() != 2)
6546+
// Create default terminator if there are no ops to mask.
6547+
if (region.empty() || region.front().empty()) {
6548+
OpTrait::SingleBlockImplicitTerminator<vector::YieldOp>::Impl<
6549+
MaskOp>::ensureTerminator(region, builder, loc);
65526550
return;
6551+
}
65536552

6554-
// Replace default yield terminator with a new one that returns the results
6555-
// from the masked operation.
6556-
OpBuilder opBuilder(builder.getContext());
6557-
Operation *maskedOp = &block.front();
6558-
Operation *oldYieldOp = &block.back();
6559-
assert(isa<vector::YieldOp>(oldYieldOp) && "Expected vector::YieldOp");
6553+
// If region has an explicit terminator, we don't modify it.
6554+
Block &block = region.front();
6555+
if (isa<vector::YieldOp>(block.back()))
6556+
return;
65606557

6561-
// Empty vector.mask op.
6562-
if (maskedOp == oldYieldOp)
6558+
// Create default terminator if the number of masked operations is not
6559+
// one. This case will trigger a verification failure.
6560+
if (block.getOperations().size() != 1) {
6561+
OpTrait::SingleBlockImplicitTerminator<vector::YieldOp>::Impl<
6562+
MaskOp>::ensureTerminator(region, builder, loc);
65636563
return;
6564+
}
65646565

6565-
opBuilder.setInsertionPoint(oldYieldOp);
6566+
// Create a terminator that yields the results from the masked operation.
6567+
OpBuilder opBuilder(builder.getContext());
6568+
Operation *maskedOp = &block.front();
6569+
opBuilder.setInsertionPointToEnd(&block);
65666570
opBuilder.create<vector::YieldOp>(loc, maskedOp->getResults());
6567-
oldYieldOp->dropAllReferences();
6568-
oldYieldOp->erase();
65696571
}
65706572

65716573
LogicalResult MaskOp::verify() {
@@ -6600,6 +6602,11 @@ LogicalResult MaskOp::verify() {
66006602
return emitOpError("expects number of results to match maskable operation "
66016603
"number of results");
66026604

6605+
if (!llvm::equal(maskableOp->getResults(), terminator.getOperands()))
6606+
return emitOpError(
6607+
"expects all the results from the MaskableOpInterface to "
6608+
"be returned by the terminator");
6609+
66036610
if (!llvm::equal(maskableOp->getResultTypes(), getResultTypes()))
66046611
return emitOpError(
66056612
"expects result type to match maskable operation result type");

mlir/test/Dialect/Vector/invalid.mlir

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,34 @@ func.func @vector_mask_0d_mask(%arg0: tensor<2x4xi32>,
17471747

17481748
// -----
17491749

1750+
func.func @vector_mask_non_empty_external_return(%t0: tensor<?xf32>, %idx: index,
1751+
%m0: vector<16xi1>, %ext: vector<16xf32>) -> vector<16xf32> {
1752+
%ft0 = arith.constant 0.0 : f32
1753+
// expected-error@+1 {{'vector.mask' op expects all the results from the MaskableOpInterface to be returned by the terminator}}
1754+
%0 = vector.mask %m0 {
1755+
%1 =vector.transfer_read %t0[%idx], %ft0 : tensor<?xf32>, vector<16xf32>
1756+
vector.yield %ext : vector<16xf32>
1757+
} : vector<16xi1> -> vector<16xf32>
1758+
1759+
return %0 : vector<16xf32>
1760+
}
1761+
1762+
// -----
1763+
1764+
func.func @vector_mask_non_empty_mixed_return(%t0: tensor<?xf32>, %idx: index,
1765+
%m0: vector<16xi1>, %ext: vector<16xf32>) -> (vector<16xf32>, vector<16xf32>) {
1766+
%ft0 = arith.constant 0.0 : f32
1767+
// expected-error@+1 {{'vector.mask' op expects number of results to match maskable operation number of results}}
1768+
%0:2 = vector.mask %m0 {
1769+
%1 =vector.transfer_read %t0[%idx], %ft0 : tensor<?xf32>, vector<16xf32>
1770+
vector.yield %1, %ext : vector<16xf32>, vector<16xf32>
1771+
} : vector<16xi1> -> (vector<16xf32>, vector<16xf32>)
1772+
1773+
return %0#0, %0#1 : vector<16xf32>, vector<16xf32>
1774+
}
1775+
1776+
// -----
1777+
17501778
func.func @vector_scalable_insert_unaligned(%subv: vector<4xi32>, %vec: vector<[16]xi32>) {
17511779
// expected-error@+1 {{op failed to verify that position is a multiple of the source length.}}
17521780
%0 = vector.scalable.insert %subv, %vec[2] : vector<4xi32> into vector<[16]xi32>

0 commit comments

Comments
 (0)