[Mlir-commits] [mlir] [mlir] fix `maybeReplaceWithConstant` in IntRangeOptimizations (PR #133556)

Maksim Levental llvmlistbot at llvm.org
Fri Mar 28 19:44:26 PDT 2025


https://github.com/makslevental created https://github.com/llvm/llvm-project/pull/133556

If a dialect is caching/reusing constants when materializing then such constants might already have `IntegerValueRangeLattice` associated with them and the range endpoint bit widths might not match the new replacement; I observed this with `%true = arith.constant true` which was materialized but had the range endpoint bit widths:

```
umin: 32
umax: 32
smin: 32
smax: 32
```

Thus, we should be clearing the analysis state each time a replacement is made.

>From d7704af439d632eb025bbda3441ccff73668f7a5 Mon Sep 17 00:00:00 2001
From: Maksim Levental <maksim.levental at gmail.com>
Date: Fri, 28 Mar 2025 22:40:30 -0400
Subject: [PATCH] [mlir] fix `maybeReplaceWithConstant` in
 IntRangeOptimizations

---
 .../lib/Dialect/Arith/Transforms/IntRangeOptimizations.cpp | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/mlir/lib/Dialect/Arith/Transforms/IntRangeOptimizations.cpp b/mlir/lib/Dialect/Arith/Transforms/IntRangeOptimizations.cpp
index 1cb9453ccf3c9..602d80a45993e 100644
--- a/mlir/lib/Dialect/Arith/Transforms/IntRangeOptimizations.cpp
+++ b/mlir/lib/Dialect/Arith/Transforms/IntRangeOptimizations.cpp
@@ -91,8 +91,11 @@ LogicalResult maybeReplaceWithConstant(DataFlowSolver &solver,
   if (!constOp)
     return failure();
 
-  copyIntegerRange(solver, value, constOp->getResult(0));
-  rewriter.replaceAllUsesWith(value, constOp->getResult(0));
+  OpResult res = constOp->getResult(0);
+  if (solver.lookupState<dataflow::IntegerValueRangeLattice>(res))
+    solver.eraseState(res);
+  copyIntegerRange(solver, value, res);
+  rewriter.replaceAllUsesWith(value, res);
   return success();
 }
 } // namespace mlir::dataflow



More information about the Mlir-commits mailing list