[Mlir-commits] [mlir] [MLIR] Fix type incompatibility in BitcastOp fold (PR #125193)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Fri Jan 31 02:04:57 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir-arith
Author: Vadim Curcă (VadimCurca)
<details>
<summary>Changes</summary>
This commit fixes a bug in the `arith::BitcastOp::fold` function where a poisoned constant value was incorrectly cast to `IntegerAttr`, causing a `cast<Ty>() argument of incompatible type!` error.
---
Full diff: https://github.com/llvm/llvm-project/pull/125193.diff
2 Files Affected:
- (modified) mlir/lib/Dialect/Arith/IR/ArithOps.cpp (+4)
- (modified) mlir/test/Dialect/Arith/canonicalize.mlir (+22)
``````````diff
diff --git a/mlir/lib/Dialect/Arith/IR/ArithOps.cpp b/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
index 75d59ba8c1a108..8a9f223089794e 100644
--- a/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
+++ b/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
@@ -1761,6 +1761,10 @@ OpFoldResult arith::BitcastOp::fold(FoldAdaptor adaptor) {
if (llvm::isa<ShapedType>(resType))
return {};
+ /// Bitcast poison.
+ if (llvm::isa<ub::PoisonAttr>(operand))
+ return ub::PoisonAttr::get(getContext());
+
/// Bitcast integer or float to integer or float.
APInt bits = llvm::isa<FloatAttr>(operand)
? llvm::cast<FloatAttr>(operand).getValue().bitcastToAPInt()
diff --git a/mlir/test/Dialect/Arith/canonicalize.mlir b/mlir/test/Dialect/Arith/canonicalize.mlir
index 3a16ee3d4f8fde..e3750bb020cade 100644
--- a/mlir/test/Dialect/Arith/canonicalize.mlir
+++ b/mlir/test/Dialect/Arith/canonicalize.mlir
@@ -1843,6 +1843,28 @@ func.func @bitcastOfBitcast(%arg : i16) -> i16 {
// -----
+// CHECK-LABEL: @bitcastPoisonItoFP(
+func.func @bitcastPoisonItoFP() -> f32 {
+ // CHECK: %[[P:.+]] = ub.poison : f32
+ // CHECK: return %[[P]] : f32
+ %p = ub.poison : i32
+ %res = arith.bitcast %p : i32 to f32
+ return %res : f32
+}
+
+// -----
+
+// CHECK-LABEL: @bitcastPoisonFPtoI(
+func.func @bitcastPoisonFPtoI() -> i32 {
+ // CHECK: %[[P:.+]] = ub.poison : i32
+ // CHECK: return %[[P]] : i32
+ %p = ub.poison : f32
+ %res = arith.bitcast %p : f32 to i32
+ return %res : i32
+}
+
+// -----
+
// CHECK-LABEL: test_maxsi
// CHECK-DAG: %[[C0:.+]] = arith.constant 42
// CHECK-DAG: %[[MAX_INT_CST:.+]] = arith.constant 127
``````````
</details>
https://github.com/llvm/llvm-project/pull/125193
More information about the Mlir-commits
mailing list