[Mlir-commits] [mlir] [mlir][Math] Fix IPowIOp folding crash for i1 (PR #179684)

Ayush Kumar Gaur llvmlistbot at llvm.org
Wed Feb 4 10:13:58 PST 2026


https://github.com/Ayush3941 updated https://github.com/llvm/llvm-project/pull/179684

>From 3abf94c05ce754899846afdd27e15e79e8f61d26 Mon Sep 17 00:00:00 2001
From: Ayush3941 <ayushkgaur1 at gmail.com>
Date: Wed, 4 Feb 2026 10:00:18 -0500
Subject: [PATCH 1/2] [mlir][Math] Fix IPowIOp folding crash for i1

---
 mlir/lib/Dialect/Math/IR/MathOps.cpp              |  2 +-
 .../Dialect/SparseTensor/regress-ipowi-i1.mlir    | 15 +++++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)
 create mode 100644 mlir/test/Dialect/SparseTensor/regress-ipowi-i1.mlir

diff --git a/mlir/lib/Dialect/Math/IR/MathOps.cpp b/mlir/lib/Dialect/Math/IR/MathOps.cpp
index bbeef0f6ee9e5..2d460fce39350 100644
--- a/mlir/lib/Dialect/Math/IR/MathOps.cpp
+++ b/mlir/lib/Dialect/Math/IR/MathOps.cpp
@@ -370,7 +370,7 @@ OpFoldResult math::IPowIOp::fold(FoldAdaptor adaptor) {
       [](const APInt &base, const APInt &power) -> std::optional<APInt> {
         unsigned width = base.getBitWidth();
         auto zeroValue = APInt::getZero(width);
-        APInt oneValue{width, 1ULL, /*isSigned=*/true};
+        APInt oneValue{width, 1ULL, /*isSigned=*/false};
         APInt minusOneValue{width, -1ULL, /*isSigned=*/true};
 
         if (power.isZero())
diff --git a/mlir/test/Dialect/SparseTensor/regress-ipowi-i1.mlir b/mlir/test/Dialect/SparseTensor/regress-ipowi-i1.mlir
new file mode 100644
index 0000000000000..1d881206d8d08
--- /dev/null
+++ b/mlir/test/Dialect/SparseTensor/regress-ipowi-i1.mlir
@@ -0,0 +1,15 @@
+// RUN: mlir-opt %s --sparsification-and-bufferization | FileCheck %s
+
+module {
+  func.func @main() {
+    %0 = irdl.c_pred "::llvm::isa<::mlir::IntegerAttr>($_self)"
+    %1 = sparse_tensor.has_runtime_library
+    %2 = math.ctlz %1 : i1
+    %3 = math.ipowi %2, %1 : i1
+    %4 = emitc.unary_minus %3 : (i1) -> i1
+    func.return
+  }
+}
+
+// CHECK-LABEL: func.func @main
+// CHECK: emitc.unary_minus

>From 56ee7ca316785bbcbaca0b7dca3739163b9c34b2 Mon Sep 17 00:00:00 2001
From: Ayush3941 <ayushkgaur1 at gmail.com>
Date: Wed, 4 Feb 2026 12:57:21 -0500
Subject: [PATCH 2/2] [mlir][Math] Fix IPowIOp folding crash for i1 added more
 tests

---
 mlir/test/Dialect/Math/canonicalize.mlir      | 42 +++++++++++++++++++
 .../SparseTensor/regress-ipowi-i1.mlir        | 15 -------
 2 files changed, 42 insertions(+), 15 deletions(-)
 delete mode 100644 mlir/test/Dialect/SparseTensor/regress-ipowi-i1.mlir

diff --git a/mlir/test/Dialect/Math/canonicalize.mlir b/mlir/test/Dialect/Math/canonicalize.mlir
index 3743768d901e3..4a0d1610ab4eb 100644
--- a/mlir/test/Dialect/Math/canonicalize.mlir
+++ b/mlir/test/Dialect/Math/canonicalize.mlir
@@ -564,3 +564,45 @@ func.func @isnormal_fold_vec() -> (vector<4xi1>) {
   %0 = math.isnormal %v1 : vector<4xf32>
   return %0 : vector<4xi1>
 }
+
+// CHECK-LABEL: func.func @ipowi_i1_0_pow_0
+// CHECK: %[[T0:.+]] = arith.constant true
+// CHECK: return %[[T0]] : i1
+func.func @ipowi_i1_0_pow_0() -> i1 {
+  %b = arith.constant false
+  %e = arith.constant false
+  %r = math.ipowi %b, %e : i1
+  return %r : i1
+}
+
+// CHECK-LABEL: func.func @ipowi_i1_0_pow_1
+// CHECK: %false = arith.constant false
+// CHECK: %true = arith.constant true
+// CHECK: %[[R01:.+]] = math.ipowi %false, %true : i1
+// CHECK: return %[[R01]] : i1
+func.func @ipowi_i1_0_pow_1() -> i1 {
+  %b = arith.constant false
+  %e = arith.constant true
+  %r = math.ipowi %b, %e : i1
+  return %r : i1
+}
+
+// CHECK-LABEL: func.func @ipowi_i1_1_pow_0
+// CHECK: %[[T10:.+]] = arith.constant true
+// CHECK: return %[[T10]] : i1
+func.func @ipowi_i1_1_pow_0() -> i1 {
+  %b = arith.constant true
+  %e = arith.constant false
+  %r = math.ipowi %b, %e : i1
+  return %r : i1
+}
+
+// CHECK-LABEL: func.func @ipowi_i1_1_pow_1
+// CHECK: %[[T11:.+]] = arith.constant true
+// CHECK: return %[[T11]] : i1
+func.func @ipowi_i1_1_pow_1() -> i1 {
+  %b = arith.constant true
+  %e = arith.constant true
+  %r = math.ipowi %b, %e : i1
+  return %r : i1
+}
\ No newline at end of file
diff --git a/mlir/test/Dialect/SparseTensor/regress-ipowi-i1.mlir b/mlir/test/Dialect/SparseTensor/regress-ipowi-i1.mlir
deleted file mode 100644
index 1d881206d8d08..0000000000000
--- a/mlir/test/Dialect/SparseTensor/regress-ipowi-i1.mlir
+++ /dev/null
@@ -1,15 +0,0 @@
-// RUN: mlir-opt %s --sparsification-and-bufferization | FileCheck %s
-
-module {
-  func.func @main() {
-    %0 = irdl.c_pred "::llvm::isa<::mlir::IntegerAttr>($_self)"
-    %1 = sparse_tensor.has_runtime_library
-    %2 = math.ctlz %1 : i1
-    %3 = math.ipowi %2, %1 : i1
-    %4 = emitc.unary_minus %3 : (i1) -> i1
-    func.return
-  }
-}
-
-// CHECK-LABEL: func.func @main
-// CHECK: emitc.unary_minus



More information about the Mlir-commits mailing list