[Mlir-commits] [mlir] [mlir][arith] Add `exact` to `index_cast{, ui}` (PR #183395)

Razvan Lupusoru llvmlistbot at llvm.org
Tue Mar 3 12:52:13 PST 2026


================
@@ -288,31 +288,38 @@ def SelectI1ToNot :
 // IndexCastOp
 //===----------------------------------------------------------------------===//
 
-// index_cast(index_cast(x)) -> x, if dstType == srcType.
+// index_cast(index_cast(x, exact)) -> x, if dstType == srcType.
----------------
razvanlupusoru wrote:

@amd-eochoalo This pattern update is more restrictive than before and prevents foldings that were previously done.

Concretely, consider the case updated in `mlir/test/Transforms/canonicalize.mlir`:
```
%11 = arith.index_cast %arg0 exact : i16 to index
%12 = arith.index_cast %11 : index to i16
```
Previously, this pair would fold away even without the `exact` flag. After this change, the fold seems to require `exact`, even for this widening/narrowing pattern which is typically non-lossy in practice.

This shows up more often for me because the exact flag isn't added by default, and utilities that create `index_cast` (specifically `arith::getValueOrCreateCastToIndexLike`) don't set it.

For now I'm working around it by adding an `isExact` parameter to `getValueOrCreateCastToIndexLike` and having my callers pass true (I can send a PR if that seems reasonable). Before doing that, I wanted to ask for your thoughts on whether this new requirement is expected, or if there's a preferred way to address this.

https://github.com/llvm/llvm-project/pull/183395


More information about the Mlir-commits mailing list