[Mlir-commits] [mlir] 6fa26f6 - [mlir][arith] Fold on extension of FP constants using arith.extf
Victor Perez
llvmlistbot at llvm.org
Thu Feb 23 04:40:15 PST 2023
Author: Victor Perez
Date: 2023-02-23T12:38:55Z
New Revision: 6fa26f60c55d4d7ba0bfc5353a8f7583da9d4b9a
URL: https://github.com/llvm/llvm-project/commit/6fa26f60c55d4d7ba0bfc5353a8f7583da9d4b9a
DIFF: https://github.com/llvm/llvm-project/commit/6fa26f60c55d4d7ba0bfc5353a8f7583da9d4b9a.diff
LOG: [mlir][arith] Fold on extension of FP constants using arith.extf
It is safe to fold when extending, as we will not lose precision.
Differential Revision: https://reviews.llvm.org/D144251
Added:
Modified:
mlir/include/mlir/Dialect/Arith/IR/ArithOps.td
mlir/lib/Dialect/Arith/IR/ArithOps.cpp
mlir/test/Dialect/Arith/canonicalize.mlir
Removed:
################################################################################
diff --git a/mlir/include/mlir/Dialect/Arith/IR/ArithOps.td b/mlir/include/mlir/Dialect/Arith/IR/ArithOps.td
index fb790c13e39ab..35f4d0761db93 100644
--- a/mlir/include/mlir/Dialect/Arith/IR/ArithOps.td
+++ b/mlir/include/mlir/Dialect/Arith/IR/ArithOps.td
@@ -1047,6 +1047,7 @@ def Arith_ExtFOp : Arith_FToFCastOp<"extf"> {
When operating on vectors, casts elementwise.
}];
let hasVerifier = 1;
+ let hasFolder = 1;
}
//===----------------------------------------------------------------------===//
diff --git a/mlir/lib/Dialect/Arith/IR/ArithOps.cpp b/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
index d5fb08fd8b40d..f6308a6b000b0 100644
--- a/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
+++ b/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
@@ -1224,6 +1224,16 @@ LogicalResult arith::ExtSIOp::verify() {
// ExtFOp
//===----------------------------------------------------------------------===//
+/// Always fold extension of FP constants.
+OpFoldResult arith::ExtFOp::fold(FoldAdaptor adaptor) {
+ auto constOperand = adaptor.getIn().dyn_cast_or_null<FloatAttr>();
+ if (!constOperand)
+ return {};
+
+ // Convert to target type via 'double'.
+ return FloatAttr::get(getType(), constOperand.getValue().convertToDouble());
+}
+
bool arith::ExtFOp::areCastCompatible(TypeRange inputs, TypeRange outputs) {
return checkWidthChangeCast<std::greater, FloatType>(inputs, outputs);
}
diff --git a/mlir/test/Dialect/Arith/canonicalize.mlir b/mlir/test/Dialect/Arith/canonicalize.mlir
index 355e7a8753ff6..eaafa9e93ceaa 100644
--- a/mlir/test/Dialect/Arith/canonicalize.mlir
+++ b/mlir/test/Dialect/Arith/canonicalize.mlir
@@ -502,6 +502,15 @@ func.func @unsignedExtendConstantVector() -> vector<4xi16> {
return %ext : vector<4xi16>
}
+// CHECK-LABEL: @extFPConstant
+// CHECK: %[[cres:.+]] = arith.constant 1.000000e+00 : f64
+// CHECK: return %[[cres]]
+func.func @extFPConstant() -> f64 {
+ %cst = arith.constant 1.000000e+00 : f32
+ %0 = arith.extf %cst : f32 to f64
+ return %0 : f64
+}
+
// CHECK-LABEL: @truncConstant
// CHECK: %[[cres:.+]] = arith.constant -2 : i16
// CHECK: return %[[cres]]
More information about the Mlir-commits
mailing list