[Mlir-commits] [mlir] goldsteinn/mlir infer remu (PR #110169)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Sep 26 13:52:12 PDT 2024


https://github.com/goldsteinn created https://github.com/llvm/llvm-project/pull/110169

- **[MLIR] Add test for inferring range of remu; NFC**
- **[MLIR] Unconditionally take min of max lhs/rhs value in inferRemU**


>From 525a55d77d8d1cc52b2c7dc9d190ab699db00daf Mon Sep 17 00:00:00 2001
From: Noah Goldstein <goldstein.w.n at gmail.com>
Date: Thu, 26 Sep 2024 14:33:54 -0500
Subject: [PATCH 1/2] [MLIR] Add test for inferring range of remu; NFC

---
 mlir/test/Dialect/Arith/int-range-interface.mlir | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/mlir/test/Dialect/Arith/int-range-interface.mlir b/mlir/test/Dialect/Arith/int-range-interface.mlir
index 48fdb1cdced4dc..ebe41b31f19f53 100644
--- a/mlir/test/Dialect/Arith/int-range-interface.mlir
+++ b/mlir/test/Dialect/Arith/int-range-interface.mlir
@@ -266,6 +266,18 @@ func.func @remui_base(%arg0 : index, %arg1 : index ) -> i1 {
     func.return %3 : i1
 }
 
+// CHECK-LABEL: func @remui_base_maybe_zero
+// CHECK: %[[ret:.*]] = arith.cmpi ult
+// CHECK: return %[[ret]]
+func.func @remui_base_maybe_zero(%arg0 : index, %arg1 : index ) -> i1 {
+    %c4 = arith.constant 4 : index
+
+    %0 = arith.minui %arg1, %c4 : index
+    %1 = arith.remui %arg0, %0 : index
+    %2 = arith.cmpi ult, %1, %c4 : index
+    func.return %2 : i1
+}    
+
 // CHECK-LABEL: func @remsi_base
 // CHECK: %[[ret:.*]] = arith.cmpi sge
 // CHECK: return %[[ret]]

>From 2a80c333c71060027bba5f80ad673c71568c117b Mon Sep 17 00:00:00 2001
From: Noah Goldstein <goldstein.w.n at gmail.com>
Date: Thu, 26 Sep 2024 15:27:57 -0500
Subject: [PATCH 2/2] [MLIR] Unconditionally take min of max lhs/rhs value in
 inferRemU

`arith.remu` cannot be larger than (rhs - 1) or lhs.
---
 mlir/lib/Interfaces/Utils/InferIntRangeCommon.cpp | 4 ++--
 mlir/test/Dialect/Arith/int-range-interface.mlir  | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/mlir/lib/Interfaces/Utils/InferIntRangeCommon.cpp b/mlir/lib/Interfaces/Utils/InferIntRangeCommon.cpp
index ca3631d53bda99..ec9ed87723e1cc 100644
--- a/mlir/lib/Interfaces/Utils/InferIntRangeCommon.cpp
+++ b/mlir/lib/Interfaces/Utils/InferIntRangeCommon.cpp
@@ -444,10 +444,10 @@ mlir::intrange::inferRemU(ArrayRef<ConstantIntRanges> argRanges) {
 
   unsigned width = rhsMin.getBitWidth();
   APInt umin = APInt::getZero(width);
-  APInt umax = APInt::getMaxValue(width);
+  // Remainder can't be larger than either of its arguments.
+  APInt umax = llvm::APIntOps::umin((rhsMax - 1), lhs.umax());
 
   if (!rhsMin.isZero()) {
-    umax = rhsMax - 1;
     // Special case: sweeping out a contiguous range in N/[modulus]
     if (rhsMin == rhsMax) {
       const APInt &lhsMin = lhs.umin(), &lhsMax = lhs.umax();
diff --git a/mlir/test/Dialect/Arith/int-range-interface.mlir b/mlir/test/Dialect/Arith/int-range-interface.mlir
index ebe41b31f19f53..88341f3b5ca7d5 100644
--- a/mlir/test/Dialect/Arith/int-range-interface.mlir
+++ b/mlir/test/Dialect/Arith/int-range-interface.mlir
@@ -267,8 +267,8 @@ func.func @remui_base(%arg0 : index, %arg1 : index ) -> i1 {
 }
 
 // CHECK-LABEL: func @remui_base_maybe_zero
-// CHECK: %[[ret:.*]] = arith.cmpi ult
-// CHECK: return %[[ret]]
+// CHECK: %[[true:.*]] = arith.constant true
+// CHECK: return %[[true]]
 func.func @remui_base_maybe_zero(%arg0 : index, %arg1 : index ) -> i1 {
     %c4 = arith.constant 4 : index
 



More information about the Mlir-commits mailing list