Skip to content

[MLIR][ARITH] Adds missing foldings for truncf #128096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 21 additions & 0 deletions mlir/lib/Dialect/Arith/IR/ArithOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1518,6 +1518,27 @@ LogicalResult arith::TruncIOp::verify() {
/// can be represented without precision loss.
OpFoldResult arith::TruncFOp::fold(FoldAdaptor adaptor) {
auto resElemType = cast<FloatType>(getElementTypeOrSelf(getType()));
if (auto extOp = getOperand().getDefiningOp<arith::ExtFOp>()) {
Value src = extOp.getIn();
auto srcType = cast<FloatType>(getElementTypeOrSelf(src.getType()));
auto intermediateType =
cast<FloatType>(getElementTypeOrSelf(extOp.getType()));
// Check if the srcType is representable in the intermediateType.
if (llvm::APFloatBase::isRepresentableBy(
srcType.getFloatSemantics(),
intermediateType.getFloatSemantics())) {
// truncf(extf(a)) -> truncf(a)
if (srcType.getWidth() > resElemType.getWidth()) {
setOperand(src);
return getResult();
}

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

const llvm::fltSemantics &targetSemantics = resElemType.getFloatSemantics();
return constFoldCastOp<FloatAttr, FloatAttr>(
adaptor.getOperands(), getType(),
Expand Down
39 changes: 39 additions & 0 deletions mlir/test/Dialect/Arith/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,45 @@ 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: @truncExtf1
// CHECK-NOT: truncf
// CHECK: return %arg0
func.func @truncExtf1(%arg0: bf16) -> bf16 {
%extf = arith.extf %arg0 : bf16 to f32
%trunc = arith.truncf %extf : f32 to bf16
return %trunc : bf16
}

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

// CHECK-LABEL: @truncExtf3
// CHECK: %[[ARG0:.+]]: f32
// CHECK: %[[CST:.*]] = arith.truncf %[[ARG0:.+]] : f32 to f16
// CHECK: return %[[CST:.*]]
func.func @truncExtf3(%arg0: f32) -> f16 {
%extf = arith.extf %arg0 : f32 to f64
%truncf = arith.truncf %extf : f64 to f16
return %truncf : 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