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

Krzysztof Drewniak llvmlistbot at llvm.org
Wed Feb 25 13:55:29 PST 2026


================
@@ -1598,15 +1616,41 @@ def Arith_IndexCastOp
 
 def Arith_IndexCastUIOp
   : Arith_CastOp<"index_castui", IndexCastTypeConstraint, IndexCastTypeConstraint,
-                 [DeclareOpInterfaceMethods<InferIntRangeInterface, ["inferResultRanges"]>]> {
+                 [DeclareOpInterfaceMethods<InferIntRangeInterface, ["inferResultRanges"]>,
+                  DeclareOpInterfaceMethods<ArithNonNegFlagInterface>,
+                  DeclareOpInterfaceMethods<ArithExactFlagInterface>]> {
   let summary = "unsigned cast between index and integer types";
   let description = [{
     Casts between scalar or vector integers and corresponding 'index' scalar or
     vectors. Index is an integer of platform-specific bit width. If casting to
     a wider integer, the value is zero-extended. If casting to a narrower
     integer, the value is truncated.
+
+    When the `nneg` flag is present, the operand is assumed to be non-negative.
+    In this case, zero extension is equivalent to sign extension. When this
+    assumption is violated, the result is poison.
+
+    If the `exact` attribute is present, it is assumed that the index type width
+    is such that the conversion does not lose information. When this assumption
+    is violated, the result is poison.
+
+    Example:
+
+    ```mlir
+      %0 = arith.index_castui %a : i32 to index
+      %1 = arith.index_castui %a nneg : i32 to index
+      %2 = arith.index_castui %a exact : i32 to index
+      %3 = arith.index_castui %a nneg exact : i32 to index
+    ```
   }];
 
+  let arguments = (ins IndexCastTypeConstraint:$in, UnitAttr:$nonNeg,
+                       UnitAttr:$isExact);
+  let results = (outs IndexCastTypeConstraint:$out);
+  let assemblyFormat = [{
+    $in (`nneg` $nonNeg^)? (`exact` $isExact^)? attr-dict
----------------
krzysz00 wrote:

You can use the `oilist` (order independent list) system to make this syntax less fragile. And, having done that, I'd put `exact` first to keep things in nicely alphabetical order

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


More information about the Mlir-commits mailing list