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

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Feb 12 01:28:58 PST 2026


Author: Ayush Kumar Gaur
Date: 2026-02-12T10:28:54+01:00
New Revision: c6bb1afbc5086c3f483921c575262bd95c346e5b

URL: https://github.com/llvm/llvm-project/commit/c6bb1afbc5086c3f483921c575262bd95c346e5b
DIFF: https://github.com/llvm/llvm-project/commit/c6bb1afbc5086c3f483921c575262bd95c346e5b.diff

LOG: [mlir][Math] Fix IPowIOp folding crash for i1 (#179684)

Fixes #179380: an assertion/crash in math.ipowi constant folding when the result type is i1. 

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

This commit deactivates folding for `i1`.

---------

Co-authored-by: Jakub Kuderski <kubakuderski at gmail.com>

Added: 
    

Modified: 
    mlir/lib/Dialect/Math/IR/MathOps.cpp
    mlir/test/Dialect/Math/canonicalize.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Dialect/Math/IR/MathOps.cpp b/mlir/lib/Dialect/Math/IR/MathOps.cpp
index bbeef0f6ee9e5..7e9d4acae6822 100644
--- a/mlir/lib/Dialect/Math/IR/MathOps.cpp
+++ b/mlir/lib/Dialect/Math/IR/MathOps.cpp
@@ -370,6 +370,9 @@ OpFoldResult math::IPowIOp::fold(FoldAdaptor adaptor) {
       [](const APInt &base, const APInt &power) -> std::optional<APInt> {
         unsigned width = base.getBitWidth();
         auto zeroValue = APInt::getZero(width);
+        // i1 folding is ambiguous with signed semantics, don't fold.
+        if (width == 1)
+          return {};
         APInt oneValue{width, 1ULL, /*isSigned=*/true};
         APInt minusOneValue{width, -1ULL, /*isSigned=*/true};
 
@@ -380,7 +383,7 @@ OpFoldResult math::IPowIOp::fold(FoldAdaptor adaptor) {
           // Leave 0 raised to negative power not folded.
           if (base.isZero())
             return {};
-          if (base.eq(oneValue))
+          if (base.isOne())
             return oneValue;
           // If abs(base) > 1, then the result is zero.
           if (base.ne(minusOneValue))

diff  --git a/mlir/test/Dialect/Math/canonicalize.mlir b/mlir/test/Dialect/Math/canonicalize.mlir
index 3743768d901e3..c900a24e821b4 100644
--- a/mlir/test/Dialect/Math/canonicalize.mlir
+++ b/mlir/test/Dialect/Math/canonicalize.mlir
@@ -564,3 +564,26 @@ func.func @isnormal_fold_vec() -> (vector<4xi1>) {
   %0 = math.isnormal %v1 : vector<4xf32>
   return %0 : vector<4xi1>
 }
+
+// CHECK-LABEL: func.func @ipowi_i1_const_pos_exp
+// CHECK: %[[T:.+]] = arith.constant true
+// CHECK: %[[F:.+]] = arith.constant false
+// CHECK: %[[R:.+]] = math.ipowi %[[T]], %[[F]] : i1
+// CHECK: return %[[R]] : i1
+func.func @ipowi_i1_const_pos_exp() -> i1 {
+  %b = arith.constant true    
+  %e = arith.constant false   
+  %r = math.ipowi %b, %e : i1
+  return %r : i1
+}
+
+// CHECK-LABEL: func.func @ipowi_i1_const_neg_exp
+// CHECK: %[[T:.+]] = arith.constant true
+// CHECK: %[[R:.+]] = math.ipowi %[[T]], %[[T]] : i1
+// CHECK: return %[[R]] : i1
+func.func @ipowi_i1_const_neg_exp() -> i1 {
+  %b = arith.constant true    
+  %e = arith.constant true    
+  %r = math.ipowi %b, %e : i1
+  return %r : i1
+}


        


More information about the Mlir-commits mailing list