diff --git a/mlir/include/mlir/Dialect/Math/IR/MathOps.td b/mlir/include/mlir/Dialect/Math/IR/MathOps.td --- a/mlir/include/mlir/Dialect/Math/IR/MathOps.td +++ b/mlir/include/mlir/Dialect/Math/IR/MathOps.td @@ -482,6 +482,8 @@ %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 --- a/mlir/lib/Dialect/Math/IR/MathOps.cpp +++ b/mlir/lib/Dialect/Math/IR/MathOps.cpp @@ -427,6 +427,18 @@ }); } +//===----------------------------------------------------------------------===// +// FloorOp folder +//===----------------------------------------------------------------------===// + +OpFoldResult math::FloorOp::fold(ArrayRef operands) { + return constFoldUnaryOp(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 --- a/mlir/test/Dialect/Math/canonicalize.mlir +++ b/mlir/test/Dialect/Math/canonicalize.mlir @@ -411,3 +411,21 @@ %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 +}