Skip to content

Commit 70ec2e4

Browse files
authored
[MLIR] Fix type incompatibility in BitcastOp fold (llvm#125193)
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.
1 parent c1163b8 commit 70ec2e4

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

mlir/lib/Dialect/Arith/IR/ArithOps.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,6 +1761,10 @@ OpFoldResult arith::BitcastOp::fold(FoldAdaptor adaptor) {
17611761
if (llvm::isa<ShapedType>(resType))
17621762
return {};
17631763

1764+
/// Bitcast poison.
1765+
if (llvm::isa<ub::PoisonAttr>(operand))
1766+
return ub::PoisonAttr::get(getContext());
1767+
17641768
/// Bitcast integer or float to integer or float.
17651769
APInt bits = llvm::isa<FloatAttr>(operand)
17661770
? llvm::cast<FloatAttr>(operand).getValue().bitcastToAPInt()

mlir/test/Dialect/Arith/canonicalize.mlir

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,6 +1843,28 @@ func.func @bitcastOfBitcast(%arg : i16) -> i16 {
18431843

18441844
// -----
18451845

1846+
// CHECK-LABEL: @bitcastPoisonItoFP(
1847+
func.func @bitcastPoisonItoFP() -> f32 {
1848+
// CHECK: %[[P:.+]] = ub.poison : f32
1849+
// CHECK: return %[[P]] : f32
1850+
%p = ub.poison : i32
1851+
%res = arith.bitcast %p : i32 to f32
1852+
return %res : f32
1853+
}
1854+
1855+
// -----
1856+
1857+
// CHECK-LABEL: @bitcastPoisonFPtoI(
1858+
func.func @bitcastPoisonFPtoI() -> i32 {
1859+
// CHECK: %[[P:.+]] = ub.poison : i32
1860+
// CHECK: return %[[P]] : i32
1861+
%p = ub.poison : f32
1862+
%res = arith.bitcast %p : f32 to i32
1863+
return %res : i32
1864+
}
1865+
1866+
// -----
1867+
18461868
// CHECK-LABEL: test_maxsi
18471869
// CHECK-DAG: %[[C0:.+]] = arith.constant 42
18481870
// CHECK-DAG: %[[MAX_INT_CST:.+]] = arith.constant 127

0 commit comments

Comments
 (0)