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

llvmlistbot at llvm.org llvmlistbot at llvm.org
Wed Feb 4 07:15:44 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir-math

Author: Ayush Kumar Gaur (Ayush3941)

<details>
<summary>Changes</summary>

### 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.

---
Full diff: https://github.com/llvm/llvm-project/pull/179684.diff


2 Files Affected:

- (modified) mlir/lib/Dialect/Math/IR/MathOps.cpp (+1-1) 
- (added) mlir/test/Dialect/SparseTensor/regress-ipowi-i1.mlir (+15) 


``````````diff
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

``````````

</details>


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


More information about the Mlir-commits mailing list