Skip to content

Commit befa037

Browse files
authored
[mlir][affine] Guard invalid dim attribute in the test-reify-bound pass (#129013)
Computing the bound of affine op (ValueBoundsConstraintSet::computeBound) crashes due to the invalid dim value given to the op. It is necessary for the pass to check the dim attribute not to be greater than the rank of the input type. Fixes #128807
1 parent fc28f83 commit befa037

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// RUN: mlir-opt -split-input-file %s -pass-pipeline='builtin.module(func.func(test-affine-reify-value-bounds))' -verify-diagnostics
2+
3+
func.func @test_invalid_reify_dim(%size: index) -> (index) {
4+
%zero = arith.constant 0 : index
5+
%tensor_val = tensor.empty(%size) : tensor<?xf32>
6+
7+
// expected-error@+1 {{'test.reify_bound' op invalid dim for shaped type}}
8+
%dim = "test.reify_bound"(%tensor_val) {dim = 1 : i64} : (tensor<?xf32>) -> index
9+
10+
return %dim: index
11+
}
12+
13+
// -----
14+
15+
func.func @test_invalid_reify_negative_dim(%size: index) -> (index) {
16+
%zero = arith.constant 0 : index
17+
%tensor_val = tensor.empty(%size) : tensor<?xf32>
18+
19+
// expected-error@+1 {{'test.reify_bound' op dim must be non-negative}}
20+
%dim = "test.reify_bound"(%tensor_val) {dim = -1 : i64} : (tensor<?xf32>) -> index
21+
22+
return %dim: index
23+
}
24+
25+
// -----
26+
27+
func.func @test_invalid_reify_int_value(%size: index) -> (index) {
28+
%zero = arith.constant 0 : index
29+
%int_val = arith.constant 1 : index
30+
31+
// expected-error@+1 {{'test.reify_bound' op unexpected 'dim' attribute for index variable}}
32+
%dim = "test.reify_bound"(%int_val) {dim = 1 : i64} : (index) -> index
33+
34+
return %dim: index
35+
}
36+
37+
// -----
38+
39+
func.func @test_invalid_reify_without_dim(%size: index) -> (index) {
40+
%zero = arith.constant 0 : index
41+
%tensor_val = tensor.empty(%size) : tensor<?xf32>
42+
43+
// expected-error@+1 {{'test.reify_bound' op expected 'dim' attribute for shaped type variable}}
44+
%dim = "test.reify_bound"(%tensor_val) : (tensor<?xf32>) -> index
45+
46+
return %dim: index
47+
}

Diff for: mlir/test/lib/Dialect/Affine/TestReifyValueBounds.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,27 @@ static LogicalResult testReifyValueBounds(FunctionOpInterface funcOp,
8484
auto boundType = op.getBoundType();
8585
Value value = op.getVar();
8686
std::optional<int64_t> dim = op.getDim();
87+
auto shapedType = dyn_cast<ShapedType>(value.getType());
88+
if (!shapedType && dim.has_value()) {
89+
op->emitOpError("dim specified for non-shaped type");
90+
return WalkResult::interrupt();
91+
}
92+
if (shapedType && !dim.has_value()) {
93+
op->emitOpError("dim not specified for shaped type");
94+
return WalkResult::interrupt();
95+
}
96+
if (shapedType && shapedType.hasRank() && dim.has_value()) {
97+
if (dim.value() < 0) {
98+
op->emitOpError("dim must be non-negative");
99+
return WalkResult::interrupt();
100+
}
101+
102+
if (dim.value() >= shapedType.getRank()) {
103+
op->emitOpError("invalid dim for shaped type rank");
104+
return WalkResult::interrupt();
105+
}
106+
}
107+
87108
bool constant = op.getConstant();
88109
bool scalable = op.getScalable();
89110

0 commit comments

Comments
 (0)