[Mlir-commits] [mlir] b9356b0 - [mlir][test] Reject TestWithBoundsOp with mismatched attribute width in verifier (#184093)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Fri Mar 6 07:45:01 PST 2026
Author: Mehdi Amini
Date: 2026-03-06T16:44:56+01:00
New Revision: b9356b047e5a4dc853f8e2b79dcec7b865e5bad6
URL: https://github.com/llvm/llvm-project/commit/b9356b047e5a4dc853f8e2b79dcec7b865e5bad6
DIFF: https://github.com/llvm/llvm-project/commit/b9356b047e5a4dc853f8e2b79dcec7b865e5bad6.diff
LOG: [mlir][test] Reject TestWithBoundsOp with mismatched attribute width in verifier (#184093)
Add a verifier to `TestWithBoundsOp` that rejects bounds attributes
whose
bit width does not match the result type's storage width (e.g., `10 :
i64`
for an `i8` result). This makes the mismatch invalid IR rather than
silently
accepting it, preventing downstream crashes in consumers such as
`TestReflectBoundsOp::inferResultRanges` that assert on matching widths.
`inferResultRanges` is reverted to its original one-liner; the new
`verify()` method enforces the invariant at IR construction time.
A dedicated test file (`infer-int-range-test-ops-invalid.mlir`) uses
`-verify-diagnostics` to check the emitted error message.
Fixes #120882
Assisted-by: Claude Code
Added:
mlir/test/Interfaces/InferIntRangeInterface/infer-int-range-test-ops-invalid.mlir
Modified:
mlir/test/lib/Dialect/Test/TestOpDefs.cpp
mlir/test/lib/Dialect/Test/TestOps.td
Removed:
################################################################################
diff --git a/mlir/test/Interfaces/InferIntRangeInterface/infer-int-range-test-ops-invalid.mlir b/mlir/test/Interfaces/InferIntRangeInterface/infer-int-range-test-ops-invalid.mlir
new file mode 100644
index 0000000000000..7392b9d2ec712
--- /dev/null
+++ b/mlir/test/Interfaces/InferIntRangeInterface/infer-int-range-test-ops-invalid.mlir
@@ -0,0 +1,12 @@
+// RUN: mlir-opt -split-input-file -verify-diagnostics %s
+
+// Verify that test.with_bounds with mismatched attribute width (e.g., i64
+// bounds for an i8 result) is rejected as invalid IR.
+// See: https://github.com/llvm/llvm-project/issues/120882
+func.func @with_bounds_mismatched_width() -> i8 {
+ // expected-error at +1 {{'test.with_bounds' op bound attribute width (64) does not match result type width (8)}}
+ %0 = test.with_bounds { umin = 10 : i64, umax = 15 : i64,
+ smin = 10 : i64, smax = 15 : i64 } : i8
+ %1 = test.reflect_bounds %0 : i8
+ return %1 : i8
+}
diff --git a/mlir/test/lib/Dialect/Test/TestOpDefs.cpp b/mlir/test/lib/Dialect/Test/TestOpDefs.cpp
index b0fc5a6acc647..c2479f9dcc78f 100644
--- a/mlir/test/lib/Dialect/Test/TestOpDefs.cpp
+++ b/mlir/test/lib/Dialect/Test/TestOpDefs.cpp
@@ -876,6 +876,20 @@ LogicalResult TestVerifiersOp::verifyRegions() {
// TestWithBoundsOp
//===----------------------------------------------------------------------===//
+LogicalResult TestWithBoundsOp::verify() {
+ Type type = getElementTypeOrSelf(getResult().getType());
+ unsigned expectedWidth = 0;
+ if (type.isIndex())
+ expectedWidth = IndexType::kInternalStorageBitWidth;
+ else if (auto intTy = llvm::dyn_cast<IntegerType>(type))
+ expectedWidth = intTy.getWidth();
+ if (expectedWidth != 0 && getUmin().getBitWidth() != expectedWidth)
+ return emitOpError("bound attribute width (")
+ << getUmin().getBitWidth() << ") does not match result type width ("
+ << expectedWidth << ")";
+ return success();
+}
+
void TestWithBoundsOp::inferResultRanges(ArrayRef<ConstantIntRanges> argRanges,
SetIntRangeFn setResultRanges) {
setResultRanges(getResult(), {getUmin(), getUmax(), getSmin(), getSmax()});
diff --git a/mlir/test/lib/Dialect/Test/TestOps.td b/mlir/test/lib/Dialect/Test/TestOps.td
index a25b9d270de16..02bac016eeed1 100644
--- a/mlir/test/lib/Dialect/Test/TestOps.td
+++ b/mlir/test/lib/Dialect/Test/TestOps.td
@@ -3219,6 +3219,7 @@ def InferIntRangeType : AnyTypeOf<[AnyInteger, Index, VectorOfNonZeroRankOf<[Any
def TestWithBoundsOp : TEST_Op<"with_bounds",
[DeclareOpInterfaceMethods<InferIntRangeInterface, ["inferResultRanges"]>,
NoMemoryEffect]> {
+ let hasVerifier = 1;
let description = [{
Creates a value with specified [min, max] range for integer range analysis.
More information about the Mlir-commits
mailing list