diff --git a/mlir/lib/Dialect/Math/Transforms/AlgebraicSimplification.cpp b/mlir/lib/Dialect/Math/Transforms/AlgebraicSimplification.cpp --- a/mlir/lib/Dialect/Math/Transforms/AlgebraicSimplification.cpp +++ b/mlir/lib/Dialect/Math/Transforms/AlgebraicSimplification.cpp @@ -80,7 +80,7 @@ return success(); } - // Replace `pow(x, 2.0)` with `x * x * x`. + // Replace `pow(x, 3.0)` with `x * x * x`. if (isExponentValue(3.0)) { Value square = rewriter.create(op.getLoc(), ValueRange({x, x})); rewriter.replaceOpWithNewOp(op, ValueRange({x, square})); @@ -95,12 +95,18 @@ return success(); } - // Replace `pow(x, -2.0)` with `sqrt(x)`. - if (isExponentValue(-1.0)) { + // Replace `pow(x, 0.5)` with `sqrt(x)`. + if (isExponentValue(0.5)) { rewriter.replaceOpWithNewOp(op, x); return success(); } + // Replace `pow(x, -0.5)` with `rsqrt(x)`. + if (isExponentValue(-0.5)) { + rewriter.replaceOpWithNewOp(op, x); + return success(); + } + return failure(); } diff --git a/mlir/test/Dialect/Math/algebraic-simplification.mlir b/mlir/test/Dialect/Math/algebraic-simplification.mlir --- a/mlir/test/Dialect/Math/algebraic-simplification.mlir +++ b/mlir/test/Dialect/Math/algebraic-simplification.mlir @@ -49,3 +49,27 @@ %1 = math.powf %arg1, %v : vector<4xf32> return %0, %1 : f32, vector<4xf32> } + +// CHECK-LABEL: @pow_sqrt +func @pow_sqrt(%arg0: f32, %arg1 : vector<4xf32>) -> (f32, vector<4xf32>) { + // CHECK: %[[SCALAR:.*]] = math.sqrt %arg0 + // CHECK: %[[VECTOR:.*]] = math.sqrt %arg1 + // CHECK: return %[[SCALAR]], %[[VECTOR]] + %c = constant 0.5 : f32 + %v = constant dense <0.5> : vector<4xf32> + %0 = math.powf %arg0, %c : f32 + %1 = math.powf %arg1, %v : vector<4xf32> + return %0, %1 : f32, vector<4xf32> +} + +// CHECK-LABEL: @pow_rsqrt +func @pow_rsqrt(%arg0: f32, %arg1 : vector<4xf32>) -> (f32, vector<4xf32>) { + // CHECK: %[[SCALAR:.*]] = math.rsqrt %arg0 + // CHECK: %[[VECTOR:.*]] = math.rsqrt %arg1 + // CHECK: return %[[SCALAR]], %[[VECTOR]] + %c = constant -0.5 : f32 + %v = constant dense <-0.5> : vector<4xf32> + %0 = math.powf %arg0, %c : f32 + %1 = math.powf %arg1, %v : vector<4xf32> + return %0, %1 : f32, vector<4xf32> +}