Skip to content

Commit ed1eb3a

Browse files
committed
[mlir][arith] Refine the verifier for arith.constant
Disallows initialization of scalable vectors with an attribute of arbitrary values, e.g.: ```mlir %c = arith.constant dense<[0, 1]> : vector<[2] x i32> ``` Initialization using vector splats remains allowed (i.e. when all the init values are identical): ```mlir %c = arith.constant dense<[1, 1]> : vector<[2] x i32> ```
1 parent 1678785 commit ed1eb3a

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

mlir/lib/Dialect/Arith/IR/ArithOps.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,14 @@ LogicalResult arith::ConstantOp::verify() {
190190
return emitOpError(
191191
"value must be an integer, float, or elements attribute");
192192
}
193+
194+
// Intializing scalable vectors with elements attribute is not supported
195+
// unless it's a vector splot.
196+
auto vecType = dyn_cast<VectorType>(type);
197+
auto val = dyn_cast<DenseElementsAttr>(getValue());
198+
if ((vecType && val) && vecType.isScalable() && !val.isSplat())
199+
return emitOpError(
200+
"using elements attribute to initialize a scalable vector");
193201
return success();
194202
}
195203

mlir/test/Dialect/Arith/invalid.mlir

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,14 @@ func.func @func_with_ops() {
215215

216216
// -----
217217

218+
func.func @func_with_ops() {
219+
^bb0:
220+
// expected-error@+1 {{op failed to verify that result type has i1 element type and same shape as operands}}
221+
%c = arith.constant dense<[0, 1]> : vector<[2] x i32>
222+
}
223+
224+
// -----
225+
218226
func.func @invalid_cmp_shape(%idx : () -> ()) {
219227
// expected-error@+1 {{'lhs' must be signless-integer-like, but got '() -> ()'}}
220228
%cmp = arith.cmpi eq, %idx, %idx : () -> ()

0 commit comments

Comments
 (0)