[Mlir-commits] [mlir] 58862f1 - [MLIR][Arith] Fix BitcastOp fold crashing on unhandled constant attributes (#212483)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Aug 3 01:36:38 PDT 2026


Author: Berke Ates
Date: 2026-08-03T10:36:33+02:00
New Revision: 58862f1adfc9690ddcda8cded008bc0ecda54859

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

LOG: [MLIR][Arith] Fix BitcastOp fold crashing on unhandled constant attributes (#212483)

`BitcastOp::fold` assumed any non-poison scalar operand attribute is a
`FloatAttr` or `IntegerAttr` and hard-casted it.
Constant attributes from other dialects, e.g. the `LLVM::UndefAttr`
produced by `llvm.mlir.undef`'s fold, hit the cast assertion.
This crashed SCCP on IR where `llvm.mlir.undef` feeds `arith.bitcast`.
Bail out on attributes and result types the fold does not handle.

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 b7fdb97aba335..ff6a5d4a0c29a 100644
--- a/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
+++ b/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
@@ -2270,6 +2270,9 @@ OpFoldResult arith::BitcastOp::fold(FoldAdaptor adaptor) {
     return ub::PoisonAttr::get(getContext());
 
   /// Bitcast integer or float to integer or float.
+  if (!llvm::isa<FloatAttr, IntegerAttr>(operand))
+    return {};
+
   APInt bits = llvm::isa<FloatAttr>(operand)
                    ? llvm::cast<FloatAttr>(operand).getValue().bitcastToAPInt()
                    : llvm::cast<IntegerAttr>(operand).getValue();

diff  --git a/mlir/test/Dialect/Arith/canonicalize.mlir b/mlir/test/Dialect/Arith/canonicalize.mlir
index d6ea02e5508dd..0c06aa6e861a4 100644
--- a/mlir/test/Dialect/Arith/canonicalize.mlir
+++ b/mlir/test/Dialect/Arith/canonicalize.mlir
@@ -2651,6 +2651,18 @@ func.func @bitcastChain(%arg: i16) -> f16 {
 
 // -----
 
+// CHECK-LABEL: func @bitcastForeignConstantAttr
+func.func @bitcastForeignConstantAttr() -> f64 {
+  // CHECK: %[[UNDEF:.*]] = llvm.mlir.undef : i64
+  // CHECK: %[[CAST:.*]] = arith.bitcast %[[UNDEF]] : i64 to f64
+  // CHECK: return %[[CAST]] : f64
+  %0 = llvm.mlir.undef : i64
+  %1 = arith.bitcast %0 : i64 to f64
+  return %1 : f64
+}
+
+// -----
+
 // CHECK-LABEL: test_maxsi
 // CHECK-DAG: %[[C0:.+]] = arith.constant 42
 // CHECK-DAG: %[[MAX_INT_CST:.+]] = arith.constant 127


        


More information about the Mlir-commits mailing list