Skip to content

[mlir][arith] Improve extf folder #80232

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 3 commits into from
Feb 2, 2024
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
50 changes: 37 additions & 13 deletions mlir/lib/Dialect/Arith/IR/ArithOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "mlir/IR/OpImplementation.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/IR/TypeUtilities.h"
#include "mlir/Support/LogicalResult.h"

#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/APInt.h"
Expand Down Expand Up @@ -1258,6 +1259,20 @@ static bool checkWidthChangeCast(TypeRange inputs, TypeRange outputs) {
srcType.getIntOrFloatBitWidth());
}

/// Attempts to convert `sourceValue` to an APFloat value with
/// `targetSemantics`, without any information loss or rounding.
static FailureOr<APFloat>
convertFloatValue(APFloat sourceValue,
const llvm::fltSemantics &targetSemantics) {
bool losesInfo = false;
auto status = sourceValue.convert(
targetSemantics, llvm::RoundingMode::NearestTiesToEven, &losesInfo);
if (losesInfo || status != APFloat::opOK)
return failure();

return sourceValue;
}

//===----------------------------------------------------------------------===//
// ExtUIOp
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -1321,14 +1336,21 @@ LogicalResult arith::ExtSIOp::verify() {
// ExtFOp
//===----------------------------------------------------------------------===//

/// Always fold extension of FP constants.
/// Fold extension of float constants when there is no information loss due the
/// difference in fp semantics.
OpFoldResult arith::ExtFOp::fold(FoldAdaptor adaptor) {
auto constOperand = llvm::dyn_cast_if_present<FloatAttr>(adaptor.getIn());
if (!constOperand)
return {};

// Convert to target type via 'double'.
return FloatAttr::get(getType(), constOperand.getValue().convertToDouble());
auto resElemType = cast<FloatType>(getElementTypeOrSelf(getType()));
const llvm::fltSemantics &targetSemantics = resElemType.getFloatSemantics();
return constFoldCastOp<FloatAttr, FloatAttr>(
adaptor.getOperands(), getType(),
[&targetSemantics](const APFloat &a, bool &castStatus) {
FailureOr<APFloat> result = convertFloatValue(a, targetSemantics);
if (failed(result)) {
castStatus = false;
return a;
}
return *result;
});
}

bool arith::ExtFOp::areCastCompatible(TypeRange inputs, TypeRange outputs) {
Expand Down Expand Up @@ -1403,12 +1425,13 @@ OpFoldResult arith::TruncFOp::fold(FoldAdaptor adaptor) {
const llvm::fltSemantics &targetSemantics = resElemType.getFloatSemantics();
return constFoldCastOp<FloatAttr, FloatAttr>(
adaptor.getOperands(), getType(),
[&targetSemantics](APFloat a, bool &castStatus) {
bool losesInfo = false;
auto status = a.convert(
targetSemantics, llvm::RoundingMode::NearestTiesToEven, &losesInfo);
castStatus = !losesInfo && status == APFloat::opOK;
return a;
[&targetSemantics](const APFloat &a, bool &castStatus) {
FailureOr<APFloat> result = convertFloatValue(a, targetSemantics);
if (failed(result)) {
castStatus = false;
return a;
}
return *result;
});
}

Expand Down Expand Up @@ -1496,6 +1519,7 @@ OpFoldResult arith::SIToFPOp::fold(FoldAdaptor adaptor) {
return apf;
});
}

//===----------------------------------------------------------------------===//
// FPToUIOp
//===----------------------------------------------------------------------===//
Expand Down
12 changes: 12 additions & 0 deletions mlir/test/Dialect/Arith/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,18 @@ func.func @extFPConstant() -> f64 {
return %0 : f64
}

// CHECK-LABEL: @extFPVectorConstant
// CHECK: %[[cres:.+]] = arith.constant dense<[0.000000e+00, 1.000000e+00]> : vector<2xf128>
// CHECK: return %[[cres]]
func.func @extFPVectorConstant() -> vector<2xf128> {
%cst = arith.constant dense<[0.000000e+00, 1.000000e+00]> : vector<2xf80>
%0 = arith.extf %cst : vector<2xf80> to vector<2xf128>
return %0 : vector<2xf128>
}

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

// CHECK-LABEL: @truncConstant
// CHECK: %[[cres:.+]] = arith.constant -2 : i16
// CHECK: return %[[cres]]
Expand Down