Skip to content

[mlir][affine] Guard invalid dim attribute in the test-reify-bound pass #129013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions mlir/test/Dialect/Affine/invalid-reify-bound-dim.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// RUN: mlir-opt -split-input-file %s -pass-pipeline='builtin.module(func.func(test-affine-reify-value-bounds))' -verify-diagnostics

func.func @test_invalid_reify_dim(%size: index) -> (index) {
%zero = arith.constant 0 : index
%tensor_val = tensor.empty(%size) : tensor<?xf32>

// expected-error@+1 {{'test.reify_bound' op invalid dim for shaped type}}
%dim = "test.reify_bound"(%tensor_val) {dim = 1 : i64} : (tensor<?xf32>) -> index

return %dim: index
}

// -----

func.func @test_invalid_reify_negative_dim(%size: index) -> (index) {
%zero = arith.constant 0 : index
%tensor_val = tensor.empty(%size) : tensor<?xf32>

// expected-error@+1 {{'test.reify_bound' op dim must be non-negative}}
%dim = "test.reify_bound"(%tensor_val) {dim = -1 : i64} : (tensor<?xf32>) -> index

return %dim: index
}

// -----

func.func @test_invalid_reify_int_value(%size: index) -> (index) {
%zero = arith.constant 0 : index
%int_val = arith.constant 1 : index

// expected-error@+1 {{'test.reify_bound' op unexpected 'dim' attribute for index variable}}
%dim = "test.reify_bound"(%int_val) {dim = 1 : i64} : (index) -> index

return %dim: index
}

// -----

func.func @test_invalid_reify_without_dim(%size: index) -> (index) {
%zero = arith.constant 0 : index
%tensor_val = tensor.empty(%size) : tensor<?xf32>

// expected-error@+1 {{'test.reify_bound' op expected 'dim' attribute for shaped type variable}}
%dim = "test.reify_bound"(%tensor_val) : (tensor<?xf32>) -> index

return %dim: index
}
21 changes: 21 additions & 0 deletions mlir/test/lib/Dialect/Affine/TestReifyValueBounds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,27 @@ static LogicalResult testReifyValueBounds(FunctionOpInterface funcOp,
auto boundType = op.getBoundType();
Value value = op.getVar();
std::optional<int64_t> dim = op.getDim();
auto shapedType = dyn_cast<ShapedType>(value.getType());
if (!shapedType && dim.has_value()) {
op->emitOpError("dim specified for non-shaped type");
return WalkResult::interrupt();
}
if (shapedType && !dim.has_value()) {
op->emitOpError("dim not specified for shaped type");
return WalkResult::interrupt();
}
if (shapedType && shapedType.hasRank() && dim.has_value()) {
if (dim.value() < 0) {
op->emitOpError("dim must be non-negative");
return WalkResult::interrupt();
}

if (dim.value() >= shapedType.getRank()) {
op->emitOpError("invalid dim for shaped type rank");
return WalkResult::interrupt();
}
}

bool constant = op.getConstant();
bool scalable = op.getScalable();

Expand Down