Skip to content

Commit e276dce

Browse files
authored
[mlir][arith] Refine the verifier for arith.constant (#87999)
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> ``` Note: This is a re-upload of #86178
1 parent 3b43ae9 commit e276dce

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,15 @@ LogicalResult arith::ConstantOp::verify() {
213213
return emitOpError(
214214
"value must be an integer, float, or elements attribute");
215215
}
216+
217+
// Note, we could relax this for vectors with 1 scalable dim, e.g.:
218+
// * arith.constant dense<[[3, 3], [1, 1]]> : vector<2 x [2] x i32>
219+
// However, this would most likely require updating the lowerings to LLVM.
220+
auto vecType = dyn_cast<VectorType>(type);
221+
if (vecType && vecType.isScalable() && !isa<SplatElementsAttr>(getValue()))
222+
return emitOpError(
223+
"intializing scalable vectors with elements attribute is not supported"
224+
" unless it's a vector splat");
216225
return success();
217226
}
218227

mlir/test/Dialect/Arith/invalid.mlir

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,24 @@ func.func @constant_out_of_range() {
6464

6565
// -----
6666

67+
func.func @constant_invalid_scalable_1d_vec_initialization() {
68+
^bb0:
69+
// expected-error@+1 {{'arith.constant' op intializing scalable vectors with elements attribute is not supported unless it's a vector splat}}
70+
%c = arith.constant dense<[0, 1]> : vector<[2] x i32>
71+
return
72+
}
73+
74+
// -----
75+
76+
func.func @constant_invalid_scalable_2d_vec_initialization() {
77+
^bb0:
78+
// expected-error@+1 {{'arith.constant' op intializing scalable vectors with elements attribute is not supported unless it's a vector splat}}
79+
%c = arith.constant dense<[[3, 3], [1, 1]]> : vector<2 x [2] x i32>
80+
return
81+
}
82+
83+
// -----
84+
6785
func.func @constant_wrong_type() {
6886
^bb:
6987
%x = "arith.constant"(){value = 10.} : () -> f32 // expected-error {{'arith.constant' op failed to verify that all of {value, result} have same type}}

mlir/test/Dialect/Vector/linearize.mlir

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,3 @@ func.func @test_0d_vector() -> vector<f32> {
153153
// ALL: return %[[CST]]
154154
return %0 : vector<f32>
155155
}
156-
157-
// -----
158-
159-
func.func @test_scalable_no_linearize(%arg0: vector<2x[2]xf32>) -> vector<2x[2]xf32> {
160-
// expected-error@+1 {{failed to legalize operation 'arith.constant' that was explicitly marked illegal}}
161-
%0 = arith.constant dense<[[1., 1.], [3., 3.]]> : vector<2x[2]xf32>
162-
%1 = math.sin %arg0 : vector<2x[2]xf32>
163-
%2 = arith.addf %0, %1 : vector<2x[2]xf32>
164-
165-
return %2 : vector<2x[2]xf32>
166-
}

0 commit comments

Comments
 (0)