-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[mlir][math] Simplify pow(2^n, y) to exp2(y) #166183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nurmukhametov
wants to merge
1
commit into
llvm:main
Choose a base branch
from
nurmukhametov:rewrite-mlir-math-pow2y-exp2y
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[mlir][math] Simplify pow(2^n, y) to exp2(y) #166183
nurmukhametov
wants to merge
1
commit into
llvm:main
from
nurmukhametov:rewrite-mlir-math-pow2y-exp2y
+88
−6
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Member
|
@llvm/pr-subscribers-mlir-math @llvm/pr-subscribers-mlir Author: Aleksei Nurmukhametov (nurmukhametov) ChangesThis PR adds a Full diff: https://github.com/llvm/llvm-project/pull/166183.diff 2 Files Affected:
diff --git a/mlir/lib/Dialect/Math/Transforms/AlgebraicSimplification.cpp b/mlir/lib/Dialect/Math/Transforms/AlgebraicSimplification.cpp
index 77b10cec48d8e..03e8a0d020919 100644
--- a/mlir/lib/Dialect/Math/Transforms/AlgebraicSimplification.cpp
+++ b/mlir/lib/Dialect/Math/Transforms/AlgebraicSimplification.cpp
@@ -42,20 +42,24 @@ LogicalResult
PowFStrengthReduction::matchAndRewrite(math::PowFOp op,
PatternRewriter &rewriter) const {
Location loc = op.getLoc();
+ // pow(x, y)
Value x = op.getLhs();
+ Value y = op.getRhs();
- FloatAttr scalarExponent;
- DenseFPElementsAttr vectorExponent;
+ FloatAttr scalarBase, scalarExponent;
+ DenseFPElementsAttr vectorBase, vectorExponent;
- bool isScalar = matchPattern(op.getRhs(), m_Constant(&scalarExponent));
- bool isVector = matchPattern(op.getRhs(), m_Constant(&vectorExponent));
+ bool isScalarBase = matchPattern(x, m_Constant(&scalarBase));
+ bool isVectorBase = matchPattern(x, m_Constant(&vectorBase));
+ bool isScalarExponent = matchPattern(y, m_Constant(&scalarExponent));
+ bool isVectorExponent = matchPattern(y, m_Constant(&vectorExponent));
// Returns true if exponent is a constant equal to `value`.
auto isExponentValue = [&](double value) -> bool {
- if (isScalar)
+ if (isScalarExponent)
return scalarExponent.getValue().isExactlyValue(value);
- if (isVector && vectorExponent.isSplat())
+ if (isVectorExponent && vectorExponent.isSplat())
return vectorExponent.getSplatValue<FloatAttr>()
.getValue()
.isExactlyValue(value);
@@ -120,6 +124,23 @@ PowFStrengthReduction::matchAndRewrite(math::PowFOp op,
return success();
}
+ // Replace `pow(2.0^n, y)` with `exp2(n * y)`
+ if (isScalarBase || (isVectorBase && vectorBase.isSplat())) {
+ APFloat baseValue = isScalarBase
+ ? scalarBase.getValue()
+ : vectorBase.getSplatValue<FloatAttr>().getValue();
+ // Check if base is an exact power of 2
+ int n = baseValue.getExactLog2();
+ if (n != INT_MIN) {
+ Value nValue = rewriter.create<arith::ConstantOp>(
+ loc, rewriter.getFloatAttr(getElementTypeOrSelf(op.getType()), n));
+ Value nTimesY =
+ rewriter.create<arith::MulFOp>(loc, ValueRange({bcast(nValue), y}));
+ rewriter.replaceOpWithNewOp<math::Exp2Op>(op, nTimesY);
+ return success();
+ }
+ }
+
return failure();
}
diff --git a/mlir/test/Dialect/Math/algebraic-simplification.mlir b/mlir/test/Dialect/Math/algebraic-simplification.mlir
index e0e2b9853a2a1..239be5eeeb6ac 100644
--- a/mlir/test/Dialect/Math/algebraic-simplification.mlir
+++ b/mlir/test/Dialect/Math/algebraic-simplification.mlir
@@ -90,6 +90,66 @@ func.func @pow_0_75(%arg0: f32, %arg1 : vector<4xf32>) -> (f32, vector<4xf32>) {
return %0, %1 : f32, vector<4xf32>
}
+// CHECK-LABEL: @pow_of_two
+func.func @pow_of_two(%arg0: f32, %arg1 : vector<4xf32>) -> (f32, vector<4xf32>) {
+ // CHECK: %[[SCALAR:.*]] = math.exp2 %arg0
+ // CHECK: %[[VECTOR:.*]] = math.exp2 %arg1
+ // CHECK: return %[[SCALAR]], %[[VECTOR]]
+ %c = arith.constant 2.0 : f32
+ %v = arith.constant dense <2.0> : vector<4xf32>
+ %0 = math.powf %c, %arg0 : f32
+ %1 = math.powf %v, %arg1 : vector<4xf32>
+ return %0, %1 : f32, vector<4xf32>
+}
+
+// CHECK-LABEL: @pow_of_four
+func.func @pow_of_four(%arg0: f32, %arg1 : vector<4xf32>) -> (f32, vector<4xf32>) {
+ // CHECK-DAG: %[[CST_V:.*]] = arith.constant dense<2.000000e+00> : vector<4xf32>
+ // CHECK-DAG: %[[CST_S:.*]] = arith.constant 2.000000e+00 : f32
+ // CHECK: %[[MUL_S:.*]] = arith.mulf %arg0, %[[CST_S]]
+ // CHECK: %[[SCALAR:.*]] = math.exp2 %[[MUL_S]]
+ // CHECK: %[[MUL_V:.*]] = arith.mulf %arg1, %[[CST_V]]
+ // CHECK: %[[VECTOR:.*]] = math.exp2 %[[MUL_V]]
+ // CHECK: return %[[SCALAR]], %[[VECTOR]]
+ %c = arith.constant 4.0 : f32
+ %v = arith.constant dense <4.0> : vector<4xf32>
+ %0 = math.powf %c, %arg0 : f32
+ %1 = math.powf %v, %arg1 : vector<4xf32>
+ return %0, %1 : f32, vector<4xf32>
+}
+
+// CHECK-LABEL: @pow_of_half
+func.func @pow_of_half(%arg0: f32, %arg1 : vector<4xf32>) -> (f32, vector<4xf32>) {
+ // CHECK-DAG: %[[CST_V:.*]] = arith.constant dense<-1.000000e+00> : vector<4xf32>
+ // CHECK-DAG: %[[CST_S:.*]] = arith.constant -1.000000e+00 : f32
+ // CHECK: %[[MUL_S:.*]] = arith.mulf %arg0, %[[CST_S]]
+ // CHECK: %[[SCALAR:.*]] = math.exp2 %[[MUL_S]]
+ // CHECK: %[[MUL_V:.*]] = arith.mulf %arg1, %[[CST_V]]
+ // CHECK: %[[VECTOR:.*]] = math.exp2 %[[MUL_V]]
+ // CHECK: return %[[SCALAR]], %[[VECTOR]]
+ %c = arith.constant 0.5 : f32
+ %v = arith.constant dense <0.5> : vector<4xf32>
+ %0 = math.powf %c, %arg0 : f32
+ %1 = math.powf %v, %arg1 : vector<4xf32>
+ return %0, %1 : f32, vector<4xf32>
+}
+
+// CHECK-LABEL: @pow_of_quarter
+func.func @pow_of_quarter(%arg0: f32, %arg1 : vector<4xf32>) -> (f32, vector<4xf32>) {
+ // CHECK-DAG: %[[CST_V:.*]] = arith.constant dense<-2.000000e+00> : vector<4xf32>
+ // CHECK-DAG: %[[CST_S:.*]] = arith.constant -2.000000e+00 : f32
+ // CHECK: %[[MUL_S:.*]] = arith.mulf %arg0, %[[CST_S]]
+ // CHECK: %[[SCALAR:.*]] = math.exp2 %[[MUL_S]]
+ // CHECK: %[[MUL_V:.*]] = arith.mulf %arg1, %[[CST_V]]
+ // CHECK: %[[VECTOR:.*]] = math.exp2 %[[MUL_V]]
+ // CHECK: return %[[SCALAR]], %[[VECTOR]]
+ %c = arith.constant 0.25 : f32
+ %v = arith.constant dense <0.25> : vector<4xf32>
+ %0 = math.powf %c, %arg0 : f32
+ %1 = math.powf %v, %arg1 : vector<4xf32>
+ return %0, %1 : f32, vector<4xf32>
+}
+
// CHECK-LABEL: @ipowi_zero_exp(
// CHECK-SAME: %[[ARG0:.+]]: i32
// CHECK-SAME: %[[ARG1:.+]]: vector<4xi32>
|
649b6fb to
690b52b
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds a
pow(2^n, y)toexp2(y)transformation to the algebraic simplifications of the math dialect.