Skip to content

Commit dbc3c1b

Browse files
committed
[mlir][affine] Guard invalid dim attribute in the test-reify-bound pass
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 5d404d7 commit dbc3c1b

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: mlir-opt %s -pass-pipeline='builtin.module(func.func(test-affine-reify-value-bounds))' -verify-diagnostics
2+
3+
// -----
4+
5+
func.func @test_invalid_reify_dim(%size: index) -> (index) {
6+
%zero = arith.constant 0 : index
7+
%tensor_val = tensor.empty(%size) : tensor<?xf32>
8+
9+
// expected-error@+1 {{'test.reify_bound' op invalid dim for shaped type}}
10+
%dim = "test.reify_bound"(%tensor_val) {dim = 1 : i64} : (tensor<?xf32>) -> index
11+
12+
return %dim: index
13+
}

mlir/test/lib/Dialect/Affine/TestReifyValueBounds.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ 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 && shapedType.hasRank() && dim.has_value() &&
89+
dim.value() >= shapedType.getRank()) {
90+
op->emitOpError("invalid dim for shaped type");
91+
return WalkResult::interrupt();
92+
}
93+
8794
bool constant = op.getConstant();
8895
bool scalable = op.getScalable();
8996

0 commit comments

Comments
 (0)