[Mlir-commits] [mlir] 5bb6210 - [mlir][math] Canonicalization for math.floor op
Kai Sasaki
llvmlistbot at llvm.org
Thu Sep 8 18:22:09 PDT 2022
Author: Kai Sasaki
Date: 2022-09-09T10:21:48+09:00
New Revision: 5bb621056b10c1dd85ad2599e6e14e1bdc6c8896
URL: https://github.com/llvm/llvm-project/commit/5bb621056b10c1dd85ad2599e6e14e1bdc6c8896
DIFF: https://github.com/llvm/llvm-project/commit/5bb621056b10c1dd85ad2599e6e14e1bdc6c8896.diff
LOG: [mlir][math] Canonicalization for math.floor op
Support constant folding for math.floor op as well as math.ceil.
Reviewed By: Mogball
Differential Revision: https://reviews.llvm.org/D133398
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 adbc6378054cc..0e25d1b1554cf 100644
--- a/mlir/include/mlir/Dialect/Math/IR/MathOps.td
+++ b/mlir/include/mlir/Dialect/Math/IR/MathOps.td
@@ -482,6 +482,8 @@ def Math_FloorOp : Math_FloatUnaryOp<"floor"> {
%a = math.floor %b : f64
```
}];
+
+ let hasFolder = 1;
}
//===----------------------------------------------------------------------===//
diff --git a/mlir/lib/Dialect/Math/IR/MathOps.cpp b/mlir/lib/Dialect/Math/IR/MathOps.cpp
index 9b1b52b56f2f6..7b3be5b4e8462 100644
--- a/mlir/lib/Dialect/Math/IR/MathOps.cpp
+++ b/mlir/lib/Dialect/Math/IR/MathOps.cpp
@@ -427,6 +427,18 @@ OpFoldResult math::RoundEvenOp::fold(ArrayRef<Attribute> operands) {
});
}
+//===----------------------------------------------------------------------===//
+// FloorOp folder
+//===----------------------------------------------------------------------===//
+
+OpFoldResult math::FloorOp::fold(ArrayRef<Attribute> operands) {
+ return constFoldUnaryOp<FloatAttr>(operands, [](const APFloat &a) {
+ APFloat result(a);
+ result.roundToIntegral(llvm::RoundingMode::TowardNegative);
+ return result;
+ });
+}
+
//===----------------------------------------------------------------------===//
// RoundOp folder
//===----------------------------------------------------------------------===//
diff --git a/mlir/test/Dialect/Math/canonicalize.mlir b/mlir/test/Dialect/Math/canonicalize.mlir
index 711ca3a3311bb..b0132d6ce0101 100644
--- a/mlir/test/Dialect/Math/canonicalize.mlir
+++ b/mlir/test/Dialect/Math/canonicalize.mlir
@@ -411,3 +411,21 @@ func.func @round_fold_vec() -> (vector<4xf32>) {
%0 = math.round %v1 : vector<4xf32>
return %0 : vector<4xf32>
}
+
+// CHECK-LABEL: @floor_fold
+// CHECK: %[[cst:.+]] = arith.constant 0.000000e+00 : f32
+// CHECK: return %[[cst]]
+func.func @floor_fold() -> f32 {
+ %c = arith.constant 0.3 : f32
+ %r = math.floor %c : f32
+ return %r : f32
+}
+
+// CHECK-LABEL: @floor_fold2
+// CHECK: %[[cst:.+]] = arith.constant 2.000000e+00 : f32
+// CHECK: return %[[cst]]
+func.func @floor_fold2() -> f32 {
+ %c = arith.constant 2.0 : f32
+ %r = math.floor %c : f32
+ return %r : f32
+}
More information about the Mlir-commits
mailing list