[Mlir-commits] [mlir] b53bccb - [mlir][Math] Add constant folder for RoundOp.
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Wed Sep 7 23:51:42 PDT 2022
Author: jacquesguan
Date: 2022-09-08T14:51:17+08:00
New Revision: b53bccb18d2a437b04f4259cdcced479c909cbfd
URL: https://github.com/llvm/llvm-project/commit/b53bccb18d2a437b04f4259cdcced479c909cbfd
DIFF: https://github.com/llvm/llvm-project/commit/b53bccb18d2a437b04f4259cdcced479c909cbfd.diff
LOG: [mlir][Math] Add constant folder for RoundOp.
This patch uses round/roundf of libm to fold RoundOp of constant.
Differential Revision: https://reviews.llvm.org/D133401
Added:
Modified:
mlir/include/mlir/Dialect/Math/IR/MathOps.td
mlir/lib/Dialect/Math/IR/MathOps.cpp
mlir/test/Dialect/Math/canonicalize.mlir
Removed:
################################################################################
diff --git a/mlir/include/mlir/Dialect/Math/IR/MathOps.td b/mlir/include/mlir/Dialect/Math/IR/MathOps.td
index eeadf9526a9b3..adbc6378054cc 100644
--- a/mlir/include/mlir/Dialect/Math/IR/MathOps.td
+++ b/mlir/include/mlir/Dialect/Math/IR/MathOps.td
@@ -799,6 +799,7 @@ def Math_RoundOp : Math_FloatUnaryOp<"round"> {
%a = math.round %b : f64
```
}];
+ let hasFolder = 1;
}
//===----------------------------------------------------------------------===//
diff --git a/mlir/lib/Dialect/Math/IR/MathOps.cpp b/mlir/lib/Dialect/Math/IR/MathOps.cpp
index 567e53b4fad7a..9b1b52b56f2f6 100644
--- a/mlir/lib/Dialect/Math/IR/MathOps.cpp
+++ b/mlir/lib/Dialect/Math/IR/MathOps.cpp
@@ -427,6 +427,24 @@ OpFoldResult math::RoundEvenOp::fold(ArrayRef<Attribute> operands) {
});
}
+//===----------------------------------------------------------------------===//
+// RoundOp folder
+//===----------------------------------------------------------------------===//
+
+OpFoldResult math::RoundOp::fold(ArrayRef<Attribute> operands) {
+ return constFoldUnaryOpConditional<FloatAttr>(
+ operands, [](const APFloat &a) -> Optional<APFloat> {
+ switch (a.getSizeInBits(a.getSemantics())) {
+ case 64:
+ return APFloat(round(a.convertToDouble()));
+ case 32:
+ return APFloat(roundf(a.convertToFloat()));
+ default:
+ return {};
+ }
+ });
+}
+
/// Materialize an integer or floating point constant.
Operation *math::MathDialect::materializeConstant(OpBuilder &builder,
Attribute value, Type type,
diff --git a/mlir/test/Dialect/Math/canonicalize.mlir b/mlir/test/Dialect/Math/canonicalize.mlir
index e2e751b6745f7..711ca3a3311bb 100644
--- a/mlir/test/Dialect/Math/canonicalize.mlir
+++ b/mlir/test/Dialect/Math/canonicalize.mlir
@@ -393,3 +393,21 @@ func.func @roundeven_fold_vec() -> (vector<4xf32>) {
%0 = math.roundeven %v1 : vector<4xf32>
return %0 : vector<4xf32>
}
+
+// CHECK-LABEL: @round_fold
+// CHECK-NEXT: %[[cst:.+]] = arith.constant 2.000000e+00 : f32
+// CHECK-NEXT: return %[[cst]]
+func.func @round_fold() -> f32 {
+ %c = arith.constant 1.5 : f32
+ %r = math.round %c : f32
+ return %r : f32
+}
+
+// CHECK-LABEL: @round_fold_vec
+// CHECK-NEXT: %[[cst:.+]] = arith.constant dense<[1.000000e+00, -1.000000e+00, 2.000000e+00, -2.000000e+00]> : vector<4xf32>
+// CHECK-NEXT: return %[[cst]]
+func.func @round_fold_vec() -> (vector<4xf32>) {
+ %v1 = arith.constant dense<[0.5, -0.5, 1.5, -1.5]> : vector<4xf32>
+ %0 = math.round %v1 : vector<4xf32>
+ return %0 : vector<4xf32>
+}
More information about the Mlir-commits
mailing list