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

Ayush Kumar Gaur llvmlistbot at llvm.org
Wed Feb 4 07:15:09 PST 2026


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

### What this problem

Fixes an assertion/crash in math.ipowi constant folding when the result type is i1, observed via mlir-opt --sparsification-and-bufferization.

### Why it happened

IPowIOp::fold constructed APInt(width=1, val=1, isSigned=true). Signed i1 cannot represent +1 (range [-1, 0]), so APInt asserts (isIntN).

### Whats the Fix

Construct oneValue without signed range checking (isSigned=false) so it represents the bitpattern 1 for the given width.

Add a regression test under Dialect/SparseTensor that runs the same pipeline and ensures it no longer crashes.

>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] [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



More information about the Mlir-commits mailing list