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 @@ -126,29 +126,25 @@ }); } -OpFoldResult math::SqrtOp::fold(ArrayRef operands) { - auto constOperand = operands.front(); - if (!constOperand) - return {}; - - auto attr = constOperand.dyn_cast(); - if (!attr) - return {}; - - auto ft = getType().cast(); - - APFloat apf = attr.getValue(); - - if (apf.isNegative()) - return {}; - - if (ft.getWidth() == 64) - return FloatAttr::get(getType(), sqrt(apf.convertToDouble())); +//===----------------------------------------------------------------------===// +// SqrtOp folder +//===----------------------------------------------------------------------===// - if (ft.getWidth() == 32) - return FloatAttr::get(getType(), sqrtf(apf.convertToFloat())); +OpFoldResult math::SqrtOp::fold(ArrayRef operands) { + return constFoldUnaryOpConditional( + operands, [](const APFloat &a) -> Optional { + if (a.isNegative()) + return {}; - return {}; + switch (a.getSizeInBits(a.getSemantics())) { + case 64: + return APFloat(sqrt(a.convertToDouble())); + case 32: + return APFloat(sqrtf(a.convertToFloat())); + default: + return {}; + } + }); } /// Materialize an integer or floating point constant. 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 @@ -111,6 +111,15 @@ return %r : f32 } +// CHECK-LABEL: @sqrt_fold_vec +// CHECK: %[[cst:.+]] = arith.constant dense<[1.000000e+00, 1.41421354, 1.73205078, 2.000000e+00]> : vector<4xf32> +// CHECK: return %[[cst]] +func.func @sqrt_fold_vec() -> (vector<4xf32>) { + %v1 = arith.constant dense<[1.0, 2.0, 3.0, 4.0]> : vector<4xf32> + %0 = math.sqrt %v1 : vector<4xf32> + return %0 : vector<4xf32> +} + // CHECK-LABEL: @abs_fold // CHECK: %[[cst:.+]] = arith.constant 4.000000e+00 : f32 // CHECK: return %[[cst]]