Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions mlir/lib/Dialect/Arith/IR/ArithOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1517,6 +1517,28 @@ LogicalResult arith::TruncIOp::verify() {
/// Perform safe const propagation for truncf, i.e., only propagate if FP value
/// can be represented without precision loss.
OpFoldResult arith::TruncFOp::fold(FoldAdaptor adaptor) {
if (auto extOp = getOperand().getDefiningOp<arith::ExtFOp>()) {
Value src = extOp.getIn();
Type srcType = getElementTypeOrSelf(src.getType());
Type dstType = getElementTypeOrSelf(getType());
// truncf(extf(a)) -> truncf(a)
if (llvm::cast<FloatType>(srcType).getWidth() >
llvm::cast<FloatType>(dstType).getWidth()) {
setOperand(src);
return getResult();
}

// truncf(extf(a)) -> a
if (srcType == dstType)
return src;
}

// truncf(truncf(a)) -> truncf(a)
if (auto truncOp = getOperand().getDefiningOp<arith::TruncFOp>()) {
setOperand(truncOp.getIn());
return getResult();
}

auto resElemType = cast<FloatType>(getElementTypeOrSelf(getType()));
const llvm::fltSemantics &targetSemantics = resElemType.getFloatSemantics();
return constFoldCastOp<FloatAttr, FloatAttr>(
Expand Down
3 changes: 1 addition & 2 deletions mlir/test/Conversion/ArithToEmitC/arith-to-emitc.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -764,9 +764,8 @@ func.func @arith_extf(%arg0: f16) -> f64 {
func.func @arith_truncf(%arg0: f64) -> f16 {
// CHECK-LABEL: arith_truncf
// CHECK-SAME: (%[[Arg0:[^ ]*]]: f64)
// CHECK: %[[Truncd0:.*]] = emitc.cast %[[Arg0]] : f64 to f32
// CHECK: %[[Truncd0:.*]] = emitc.cast %[[Arg0]] : f64 to f16
%truncd0 = arith.truncf %arg0 : f64 to f32
// CHECK: %[[Truncd1:.*]] = emitc.cast %[[Truncd0]] : f32 to f16
%truncd1 = arith.truncf %truncd0 : f32 to f16

return %truncd1 : f16
Expand Down
29 changes: 29 additions & 0 deletions mlir/test/Dialect/Arith/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,35 @@ func.func @extFPVectorConstant() -> vector<2xf128> {
return %0 : vector<2xf128>
}

// CHECK-LABEL: @truncExtf
// CHECK-NOT: truncf
// CHECK: return %arg0
func.func @truncExtf(%arg0: f32) -> f32 {
%extf = arith.extf %arg0 : f32 to f64
%trunc = arith.truncf %extf : f64 to f32
return %trunc : f32
}

// CHECK-LABEL: @truncExtf2
// CHECK: %[[ARG0:.+]]: f32
// CHECK: %[[CST:.*]] = arith.truncf %[[ARG0:.+]] : f32 to f16
// CHECK: return %[[CST:.*]]
func.func @truncExtf2(%arg0: f32) -> f16 {
%extf = arith.extf %arg0 : f32 to f64
%truncf = arith.truncf %extf : f64 to f16
return %truncf : f16
}

// CHECK-LABEL: @truncTruncf
// CHECK: %[[ARG0:.+]]: f64
// CHECK: %[[CST:.*]] = arith.truncf %[[ARG0:.+]] : f64 to f16
// CHECK: return %[[CST:.*]]
func.func @truncTruncf(%arg0: f64) -> f16 {
%truncf = arith.truncf %arg0 : f64 to f32
%truncf1 = arith.truncf %truncf : f32 to f16
return %truncf1 : f16
}

// TODO: We should also add a test for not folding arith.extf on information loss.
// This may happen when extending f8E5M2FNUZ to f16.

Expand Down
Loading