[Mlir-commits] [mlir] 3a60a1e - [mlir][Arith] Fix crash when folding operations with dynamic-shaped tensors (#178428)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Wed Jan 28 07:25:56 PST 2026


Author: puneeth_aditya_5656
Date: 2026-01-28T10:25:51-05:00
New Revision: 3a60a1e6fe561eb97f9a7404ee112211bc003af4

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

LOG: [mlir][Arith] Fix crash when folding operations with dynamic-shaped tensors (#178428)

- Add static shape check in `getBoolAttribute` to prevent crash when
folding comparison operations with dynamic-shaped tensor types
- Add static shape check in `SelectOp::fold` before creating
`DenseElementsAttr` for the result
- Add test case to verify the fix

Fixes #178415.

Co-authored-by: Claude Opus 4.5 <noreply at anthropic.com>

Added: 
    

Modified: 
    mlir/lib/Dialect/Arith/IR/ArithOps.cpp
    mlir/test/Dialect/Arith/canonicalize.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Dialect/Arith/IR/ArithOps.cpp b/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
index 565a537616971..c61bd01fd5e9b 100644
--- a/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
+++ b/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
@@ -151,6 +151,9 @@ static Attribute getBoolAttribute(Type type, bool value) {
   ShapedType shapedType = dyn_cast_or_null<ShapedType>(type);
   if (!shapedType)
     return boolAttr;
+  // DenseElementsAttr requires a static shape.
+  if (!shapedType.hasStaticShape())
+    return {};
   return DenseElementsAttr::get(shapedType, boolAttr);
 }
 
@@ -2527,6 +2530,9 @@ OpFoldResult arith::SelectOp::fold(FoldAdaptor adaptor) {
   // select %cst_vec, %cst0, %cst1 => %cst2
   if (auto cond =
           dyn_cast_if_present<DenseElementsAttr>(adaptor.getCondition())) {
+    // DenseElementsAttr by construction always has a static shape.
+    assert(cond.getType().hasStaticShape() &&
+           "DenseElementsAttr must have static shape");
     if (auto lhs =
             dyn_cast_if_present<DenseElementsAttr>(adaptor.getTrueValue())) {
       if (auto rhs =

diff  --git a/mlir/test/Dialect/Arith/canonicalize.mlir b/mlir/test/Dialect/Arith/canonicalize.mlir
index 3ad1530248809..18e0d2d2ea3c4 100644
--- a/mlir/test/Dialect/Arith/canonicalize.mlir
+++ b/mlir/test/Dialect/Arith/canonicalize.mlir
@@ -3403,3 +3403,14 @@ func.func @unreachable() {
   cf.br ^unreachable
 }
 
+// -----
+
+// Verify that cmpi with dynamic-shaped tensors does not crash during folding.
+// The fold cannot create a DenseElementsAttr for dynamic shapes.
+// CHECK-LABEL: @cmpi_dynamic_shape_no_fold
+//       CHECK:   arith.cmpi eq
+func.func @cmpi_dynamic_shape_no_fold(%arg0: tensor<?xi32>) -> tensor<?xi1> {
+  %0 = arith.cmpi eq, %arg0, %arg0 : tensor<?xi32>
+  return %0 : tensor<?xi1>
+}
+


        


More information about the Mlir-commits mailing list