Skip to content

[MLIR][Arith] Add rounding mode attribute to truncf #86152

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 7 commits into from
Apr 1, 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
48 changes: 48 additions & 0 deletions mlir/include/mlir/Conversion/ArithCommon/AttrToLLVMConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ convertArithOverflowFlagsToLLVM(arith::IntegerOverflowFlags arithFlags);
LLVM::IntegerOverflowFlagsAttr
convertArithOverflowAttrToLLVM(arith::IntegerOverflowFlagsAttr flagsAttr);

/// Creates an LLVM rounding mode enum value from a given arithmetic rounding
/// mode enum value.
LLVM::RoundingMode
convertArithRoundingModeToLLVM(arith::RoundingMode roundingMode);

/// Creates an LLVM rounding mode attribute from a given arithmetic rounding
/// mode attribute.
LLVM::RoundingModeAttr
convertArithRoundingModeAttrToLLVM(arith::RoundingModeAttr roundingModeAttr);

/// Returns an attribute for the default LLVM FP exception behavior.
LLVM::FPExceptionBehaviorAttr
getLLVMDefaultFPExceptionBehavior(MLIRContext &context);

// Attribute converter that populates a NamedAttrList by removing the fastmath
// attribute from the source operation attributes, and replacing it with an
// equivalent LLVM fastmath attribute.
Expand Down Expand Up @@ -89,6 +103,40 @@ class AttrConvertOverflowToLLVM {
private:
NamedAttrList convertedAttr;
};

template <typename SourceOp, typename TargetOp>
class AttrConverterConstrainedFPToLLVM {
static_assert(TargetOp::template hasTrait<
LLVM::FPExceptionBehaviorOpInterface::Trait>(),
"Target constrained FP operations must implement "
"LLVM::FPExceptionBehaviorOpInterface");

public:
AttrConverterConstrainedFPToLLVM(SourceOp srcOp) {
// Copy the source attributes.
convertedAttr = NamedAttrList{srcOp->getAttrs()};

if constexpr (TargetOp::template hasTrait<
LLVM::RoundingModeOpInterface::Trait>()) {
// Get the name of the rounding mode attribute.
StringRef arithAttrName = srcOp.getRoundingModeAttrName();
// Remove the source attribute.
auto arithAttr =
cast<arith::RoundingModeAttr>(convertedAttr.erase(arithAttrName));
// Set the target attribute.
convertedAttr.set(TargetOp::getRoundingModeAttrName(),
convertArithRoundingModeAttrToLLVM(arithAttr));
}
convertedAttr.set(TargetOp::getFPExceptionBehaviorAttrName(),
getLLVMDefaultFPExceptionBehavior(*srcOp->getContext()));
}

ArrayRef<NamedAttribute> getAttrs() const { return convertedAttr.getAttrs(); }

private:
NamedAttrList convertedAttr;
};

} // namespace arith
} // namespace mlir

Expand Down
25 changes: 25 additions & 0 deletions mlir/include/mlir/Dialect/Arith/IR/ArithBase.td
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,29 @@ def Arith_IntegerOverflowAttr :
let assemblyFormat = "`<` $value `>`";
}

//===----------------------------------------------------------------------===//
// Arith_RoundingMode
//===----------------------------------------------------------------------===//

// These correspond to LLVM's values defined in:
// llvm/include/llvm/ADT/FloatingPointMode.h

def Arith_RToNearestTiesToEven // Round to nearest, ties to even
: I32EnumAttrCase<"to_nearest_even", 0>;
def Arith_RDownward // Round toward -inf
: I32EnumAttrCase<"downward", 1>;
def Arith_RUpward // Round toward +inf
: I32EnumAttrCase<"upward", 2>;
def Arith_RTowardZero // Round toward 0
: I32EnumAttrCase<"toward_zero", 3>;
def Arith_RToNearestTiesAwayFromZero // Round to nearest, ties away from zero
: I32EnumAttrCase<"to_nearest_away", 4>;

def Arith_RoundingModeAttr : I32EnumAttr<
"RoundingMode", "Floating point rounding mode",
[Arith_RToNearestTiesToEven, Arith_RDownward, Arith_RUpward,
Arith_RTowardZero, Arith_RToNearestTiesAwayFromZero]> {
let cppNamespace = "::mlir::arith";
}

#endif // ARITH_BASE
21 changes: 18 additions & 3 deletions mlir/include/mlir/Dialect/Arith/IR/ArithOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1227,17 +1227,32 @@ def Arith_TruncIOp : Arith_IToICastOp<"trunci"> {
// TruncFOp
//===----------------------------------------------------------------------===//

def Arith_TruncFOp : Arith_FToFCastOp<"truncf"> {
def Arith_TruncFOp :
Arith_Op<"truncf",
[Pure, SameOperandsAndResultShape,
DeclareOpInterfaceMethods<ArithRoundingModeInterface>,
DeclareOpInterfaceMethods<CastOpInterface>]>,
Arguments<(ins FloatLike:$in,
OptionalAttr<Arith_RoundingModeAttr>:$roundingmode)>,
Results<(outs FloatLike:$out)> {
let summary = "cast from floating-point to narrower floating-point";
let description = [{
Truncate a floating-point value to a smaller floating-point-typed value.
The destination type must be strictly narrower than the source type.
If the value cannot be exactly represented, it is rounded using the default
rounding mode. When operating on vectors, casts elementwise.
If the value cannot be exactly represented, it is rounded using the
provided rounding mode or the default one if no rounding mode is provided.
When operating on vectors, casts elementwise.
}];
let builders = [
OpBuilder<(ins "Type":$out, "Value":$in), [{
$_state.addOperands(in);
$_state.addTypes(out);
}]>
];

let hasFolder = 1;
let hasVerifier = 1;
let assemblyFormat = "$in ($roundingmode^)? attr-dict `:` type($in) `to` type($out)";
}

//===----------------------------------------------------------------------===//
Expand Down
33 changes: 33 additions & 0 deletions mlir/include/mlir/Dialect/Arith/IR/ArithOpsInterfaces.td
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,37 @@ def ArithIntegerOverflowFlagsInterface : OpInterface<"ArithIntegerOverflowFlagsI
];
}

def ArithRoundingModeInterface : OpInterface<"ArithRoundingModeInterface"> {
let description = [{
Access to op rounding mode.
}];

let cppNamespace = "::mlir::arith";

let methods = [
InterfaceMethod<
/*desc=*/ "Returns a RoundingModeAttr attribute for the operation",
/*returnType=*/ "RoundingModeAttr",
/*methodName=*/ "getRoundingModeAttr",
/*args=*/ (ins),
/*methodBody=*/ [{}],
/*defaultImpl=*/ [{
auto op = cast<ConcreteOp>(this->getOperation());
return op.getRoundingmodeAttr();
}]
>,
StaticInterfaceMethod<
/*desc=*/ [{Returns the name of the RoundingModeAttr attribute for
the operation}],
/*returnType=*/ "StringRef",
/*methodName=*/ "getRoundingModeAttrName",
/*args=*/ (ins),
/*methodBody=*/ [{}],
/*defaultImpl=*/ [{
return "roundingmode";
}]
>
];
}

#endif // ARITH_OPS_INTERFACES
31 changes: 31 additions & 0 deletions mlir/lib/Conversion/ArithCommon/AttrToLLVMConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,34 @@ LLVM::IntegerOverflowFlagsAttr mlir::arith::convertArithOverflowAttrToLLVM(
return LLVM::IntegerOverflowFlagsAttr::get(
flagsAttr.getContext(), convertArithOverflowFlagsToLLVM(arithFlags));
}

LLVM::RoundingMode
mlir::arith::convertArithRoundingModeToLLVM(arith::RoundingMode roundingMode) {
switch (roundingMode) {
case arith::RoundingMode::downward:
return LLVM::RoundingMode::TowardNegative;
case arith::RoundingMode::to_nearest_away:
return LLVM::RoundingMode::NearestTiesToAway;
case arith::RoundingMode::to_nearest_even:
return LLVM::RoundingMode::NearestTiesToEven;
case arith::RoundingMode::toward_zero:
return LLVM::RoundingMode::TowardZero;
case arith::RoundingMode::upward:
return LLVM::RoundingMode::TowardPositive;
}
llvm_unreachable("Unhandled rounding mode");
}

LLVM::RoundingModeAttr mlir::arith::convertArithRoundingModeAttrToLLVM(
arith::RoundingModeAttr roundingModeAttr) {
assert(roundingModeAttr && "Expecting valid attribute");
return LLVM::RoundingModeAttr::get(
roundingModeAttr.getContext(),
convertArithRoundingModeToLLVM(roundingModeAttr.getValue()));
}

LLVM::FPExceptionBehaviorAttr
mlir::arith::getLLVMDefaultFPExceptionBehavior(MLIRContext &context) {
return LLVM::FPExceptionBehaviorAttr::get(&context,
LLVM::FPExceptionBehavior::Ignore);
}
3 changes: 3 additions & 0 deletions mlir/lib/Conversion/ArithToAMDGPU/ArithToAMDGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ static Value clampInput(PatternRewriter &rewriter, Location loc,
}

LogicalResult TruncFToFloat8RewritePattern::match(arith::TruncFOp op) const {
// Only supporting default rounding mode as of now.
if (op.getRoundingmodeAttr())
return failure();
Type outType = op.getOut().getType();
if (auto outVecType = outType.dyn_cast<VectorType>()) {
if (outVecType.isScalable())
Expand Down
32 changes: 31 additions & 1 deletion mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,31 @@ using namespace mlir;

namespace {

/// Operations whose conversion will depend on whether they are passed a
/// rounding mode attribute or not.
///
/// `SourceOp` is the source operation; `TargetOp`, the operation it will lower
/// to; `AttrConvert` is the attribute conversion to convert the rounding mode
/// attribute.
template <typename SourceOp, typename TargetOp, bool Constrained,
template <typename, typename> typename AttrConvert =
AttrConvertPassThrough>
struct ConstrainedVectorConvertToLLVMPattern
: public VectorConvertToLLVMPattern<SourceOp, TargetOp, AttrConvert> {
using VectorConvertToLLVMPattern<SourceOp, TargetOp,
AttrConvert>::VectorConvertToLLVMPattern;

LogicalResult
matchAndRewrite(SourceOp op, typename SourceOp::Adaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
if (Constrained != static_cast<bool>(op.getRoundingModeAttr()))
return failure();
return VectorConvertToLLVMPattern<SourceOp, TargetOp,
AttrConvert>::matchAndRewrite(op, adaptor,
rewriter);
}
};

//===----------------------------------------------------------------------===//
// Straightforward Op Lowerings
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -112,7 +137,11 @@ using SubIOpLowering =
VectorConvertToLLVMPattern<arith::SubIOp, LLVM::SubOp,
arith::AttrConvertOverflowToLLVM>;
using TruncFOpLowering =
VectorConvertToLLVMPattern<arith::TruncFOp, LLVM::FPTruncOp>;
ConstrainedVectorConvertToLLVMPattern<arith::TruncFOp, LLVM::FPTruncOp,
false>;
using ConstrainedTruncFOpLowering = ConstrainedVectorConvertToLLVMPattern<
arith::TruncFOp, LLVM::ConstrainedFPTruncIntr, true,
arith::AttrConverterConstrainedFPToLLVM>;
using TruncIOpLowering =
VectorConvertToLLVMPattern<arith::TruncIOp, LLVM::TruncOp>;
using UIToFPOpLowering =
Expand Down Expand Up @@ -537,6 +566,7 @@ void mlir::arith::populateArithToLLVMConversionPatterns(
SubFOpLowering,
SubIOpLowering,
TruncFOpLowering,
ConstrainedTruncFOpLowering,
TruncIOpLowering,
UIToFPOpLowering,
XOrIOpLowering
Expand Down
9 changes: 9 additions & 0 deletions mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,15 @@ struct TypeCastingOpPattern final : public OpConversionPattern<Op> {
} else {
rewriter.template replaceOpWithNewOp<SPIRVOp>(op, dstType,
adaptor.getOperands());
if (auto roundingModeOp =
dyn_cast<arith::ArithRoundingModeInterface>(*op)) {
if (arith::RoundingModeAttr roundingMode =
roundingModeOp.getRoundingModeAttr()) {
// TODO: Perform rounding mode attribute conversion and attach to new
// operation when defined in the dialect.
return failure();
}
}
}
return success();
}
Expand Down
46 changes: 36 additions & 10 deletions mlir/lib/Dialect/Arith/IR/ArithOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,29 @@ arith::CmpIPredicate arith::invertPredicate(arith::CmpIPredicate pred) {
llvm_unreachable("unknown cmpi predicate kind");
}

/// Equivalent to
/// convertRoundingModeToLLVM(convertArithRoundingModeToLLVM(roundingMode)).
///
/// Not possible to implement as chain of calls as this would introduce a
/// circular dependency with MLIRArithAttrToLLVMConversion and make arith depend
/// on the LLVM dialect and on translation to LLVM.
static llvm::RoundingMode
convertArithRoundingModeToLLVMIR(RoundingMode roundingMode) {
switch (roundingMode) {
case RoundingMode::downward:
return llvm::RoundingMode::TowardNegative;
case RoundingMode::to_nearest_away:
return llvm::RoundingMode::NearestTiesToAway;
case RoundingMode::to_nearest_even:
return llvm::RoundingMode::NearestTiesToEven;
case RoundingMode::toward_zero:
return llvm::RoundingMode::TowardZero;
case RoundingMode::upward:
return llvm::RoundingMode::TowardPositive;
}
llvm_unreachable("Unhandled rounding mode");
}

static arith::CmpIPredicateAttr invertPredicate(arith::CmpIPredicateAttr pred) {
return arith::CmpIPredicateAttr::get(pred.getContext(),
invertPredicate(pred.getValue()));
Expand Down Expand Up @@ -1233,13 +1256,12 @@ static bool checkWidthChangeCast(TypeRange inputs, TypeRange outputs) {
}

/// 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) {
/// `targetSemantics` and `roundingMode`, without any information loss.
static FailureOr<APFloat> convertFloatValue(
APFloat sourceValue, const llvm::fltSemantics &targetSemantics,
llvm::RoundingMode roundingMode = llvm::RoundingMode::NearestTiesToEven) {
bool losesInfo = false;
auto status = sourceValue.convert(
targetSemantics, llvm::RoundingMode::NearestTiesToEven, &losesInfo);
auto status = sourceValue.convert(targetSemantics, roundingMode, &losesInfo);
if (losesInfo || status != APFloat::opOK)
return failure();

Expand Down Expand Up @@ -1391,15 +1413,19 @@ LogicalResult arith::TruncIOp::verify() {
//===----------------------------------------------------------------------===//

/// Perform safe const propagation for truncf, i.e., only propagate if FP value
/// can be represented without precision loss or rounding. This is because the
/// semantics of `arith.truncf` do not assume a specific rounding mode.
/// can be represented without precision loss.
OpFoldResult arith::TruncFOp::fold(FoldAdaptor adaptor) {
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);
[this, &targetSemantics](const APFloat &a, bool &castStatus) {
RoundingMode roundingMode =
getRoundingmode().value_or(RoundingMode::to_nearest_even);
llvm::RoundingMode llvmRoundingMode =
convertArithRoundingModeToLLVMIR(roundingMode);
FailureOr<APFloat> result =
convertFloatValue(a, targetSemantics, llvmRoundingMode);
if (failed(result)) {
castStatus = false;
return a;
Expand Down
5 changes: 5 additions & 0 deletions mlir/lib/Dialect/Arith/Transforms/ExpandOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ struct BFloat16TruncFOpConverter : public OpRewritePattern<arith::TruncFOp> {
return rewriter.notifyMatchFailure(op, "not a trunc of f32 to bf16.");
}

if (op.getRoundingmodeAttr()) {
return rewriter.notifyMatchFailure(
op, "only applicable to default rounding mode.");
}

Type i16Ty = b.getI16Type();
Type i32Ty = b.getI32Type();
Type f32Ty = b.getF32Type();
Expand Down
15 changes: 15 additions & 0 deletions mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,21 @@ func.func @fptrunc_vector(%arg0 : vector<2xf32>, %arg1 : vector<2xf64>) {
return
}

// CHECK-LABEL: experimental_constrained_fptrunc
func.func @experimental_constrained_fptrunc(%arg0 : f64) {
// CHECK-NEXT: = llvm.intr.experimental.constrained.fptrunc {{.*}} tonearest ignore : f64 to f32
%0 = arith.truncf %arg0 to_nearest_even : f64 to f32
// CHECK-NEXT: = llvm.intr.experimental.constrained.fptrunc {{.*}} downward ignore : f64 to f32
%1 = arith.truncf %arg0 downward : f64 to f32
// CHECK-NEXT: = llvm.intr.experimental.constrained.fptrunc {{.*}} upward ignore : f64 to f32
%2 = arith.truncf %arg0 upward : f64 to f32
// CHECK-NEXT: = llvm.intr.experimental.constrained.fptrunc {{.*}} towardzero ignore : f64 to f32
%3 = arith.truncf %arg0 toward_zero : f64 to f32
// CHECK-NEXT: = llvm.intr.experimental.constrained.fptrunc {{.*}} tonearestaway ignore : f64 to f32
%4 = arith.truncf %arg0 to_nearest_away : f64 to f32
return
}

// Check sign and zero extension and truncation of integers.
// CHECK-LABEL: @integer_extension_and_truncation
func.func @integer_extension_and_truncation(%arg0 : i3) {
Expand Down
Loading