-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[InstCombine] Add fold for fabs(-x) -> fabs(x) #94183
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
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-llvm-transforms Author: Vaibhav (VaibhavRumale) ChangesHere is my patch for #94170 Added the missing optimization in Full diff: https://github.com/llvm/llvm-project/pull/94183.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index b6f339da31f7f..1e06296c7721f 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -2517,6 +2517,13 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
}
case Intrinsic::fabs: {
Value *Cond, *TVal, *FVal;
+ Value* Arg = II->getArgOperand(0);
+ Value* X;
+ // fabs (-X) --> fabs (X)
+ if (match(Arg, m_FNeg(m_Value(X)))) {
+ return replaceInstUsesWith(CI, Builder.CreateUnaryIntrinsic(Intrinsic::fabs, X));
+ }
+
if (match(II->getArgOperand(0),
m_Select(m_Value(Cond), m_Value(TVal), m_Value(FVal)))) {
// fabs (select Cond, TrueC, FalseC) --> select Cond, AbsT, AbsF
diff --git a/llvm/test/Transforms/InstCombine/fabs-fneg-fold.ll b/llvm/test/Transforms/InstCombine/fabs-fneg-fold.ll
new file mode 100644
index 0000000000000..f40dbec52e91b
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/fabs-fneg-fold.ll
@@ -0,0 +1,57 @@
+; RUN: opt -S -passes=instcombine %s | FileCheck %s
+
+define float @fabs_fneg_f32(float %x) {
+; CHECK-LABEL: define float @fabs_fneg_f32(
+; CHECK-NEXT: [[FABS:%.*]] = call float @llvm.fabs.f32(float [[X:%.*]])
+; CHECK-NEXT: ret float [[FABS]]
+;
+ %neg = fneg float %x
+ %fabs = call float @llvm.fabs.f32(float %neg)
+ ret float %fabs
+}
+
+define <2 x float> @fabs_fneg_v2f32(<2 x float> %x) {
+; CHECK-LABEL: define <2 x float> @fabs_fneg_v2f32(
+; CHECK-NEXT: [[FABS:%.*]] = call <2 x float> @llvm.fabs.v2f32(<2 x float> [[X:%.*]])
+; CHECK-NEXT: ret <2 x float> [[FABS]]
+;
+ %neg = fneg <2 x float> %x
+ %fabs = call <2 x float> @llvm.fabs.v2f32(<2 x float> %neg)
+ ret <2 x float> %fabs
+}
+
+define double @fabs_fneg_f64(double %x) {
+; CHECK-LABEL: define double @fabs_fneg_f64(
+; CHECK-NEXT: [[FABS:%.*]] = call double @llvm.fabs.f64(double [[X:%.*]])
+; CHECK-NEXT: ret double [[FABS]]
+;
+ %neg = fneg double %x
+ %fabs = call double @llvm.fabs.f64(double %neg)
+ ret double %fabs
+}
+
+define <4 x double> @fabs_fneg_v4f64(<4 x double> %x) {
+; CHECK-LABEL: define <4 x double> @fabs_fneg_v4f64(
+; CHECK-NEXT: [[FABS:%.*]] = call <4 x double> @llvm.fabs.v4f64(<4 x double> [[X:%.*]])
+; CHECK-NEXT: ret <4 x double> [[FABS]]
+;
+ %neg = fneg <4 x double> %x
+ %fabs = call <4 x double> @llvm.fabs.v4f64(<4 x double> %neg)
+ ret <4 x double> %fabs
+}
+
+define half @fabs_fneg_f16(half %x) {
+; CHECK-LABEL: define half @fabs_fneg_f16(
+; CHECK-NEXT: [[FABS:%.*]] = call half @llvm.fabs.f16(half [[X:%.*]])
+; CHECK-NEXT: ret half [[FABS]]
+;
+ %neg = fneg half %x
+ %fabs = call half @llvm.fabs.f16(half %neg)
+ ret half %fabs
+}
+
+declare float @llvm.fabs.f32(float)
+declare <2 x float> @llvm.fabs.v2f32(<2 x float>)
+declare double @llvm.fabs.f64(double)
+declare <4 x double> @llvm.fabs.v4f64(<4 x double>)
+declare half @llvm.fabs.f16(half)
|
@@ -2517,6 +2517,13 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) { | |||
} | |||
case Intrinsic::fabs: { | |||
Value *Cond, *TVal, *FVal; | |||
Value* Arg = II->getArgOperand(0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you replace all following uses of II->getArgOperand(0)
with Arg
?
Value* Arg = II->getArgOperand(0); | ||
Value* X; | ||
// fabs (-X) --> fabs (X) | ||
if (match(Arg, m_FNeg(m_Value(X)))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -0,0 +1,57 @@ | |||
; RUN: opt -S -passes=instcombine %s | FileCheck %s |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please regenerate check lines with llvm/utils/update_test_checks.py
ret half %fabs | ||
} | ||
|
||
declare float @llvm.fabs.f32(float) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These declarations are unnecessary.
You can test this locally with the following command:git-clang-format --diff 718331f55529469586c99a55e4b382a1c7485842 73d52ca113af478a784042d7317e48d7978a9e27 -- llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp View the diff from clang-format here.diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index 1e06296c77..fbcbe77d79 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -2517,11 +2517,12 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
}
case Intrinsic::fabs: {
Value *Cond, *TVal, *FVal;
- Value* Arg = II->getArgOperand(0);
- Value* X;
+ Value *Arg = II->getArgOperand(0);
+ Value *X;
// fabs (-X) --> fabs (X)
if (match(Arg, m_FNeg(m_Value(X)))) {
- return replaceInstUsesWith(CI, Builder.CreateUnaryIntrinsic(Intrinsic::fabs, X));
+ return replaceInstUsesWith(
+ CI, Builder.CreateUnaryIntrinsic(Intrinsic::fabs, X));
}
if (match(II->getArgOperand(0),
|
Thank you for the contribution. You might want to see: https://llvm.org/docs/InstCombineContributorGuide.html Particularly in the case of this PR, you are missing Alive2 Proofs and should Pre-Commit your tests. |
%fabs = call half @llvm.fabs.f16(half %neg) | ||
ret half %fabs | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should test flag preservation behavior
Value* Arg = II->getArgOperand(0); | ||
Value* X; | ||
// fabs (-X) --> fabs (X) | ||
if (match(Arg, m_FNeg(m_Value(X)))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Somewhere is a strip sign operations helper function that also covers copysign
@VaibhavRumale Please don't update your branch if there is no merge conflict. See https://llvm.org/docs/GitHub.html#rebasing-pull-requests-and-force-pushes |
@tstellar Should we turn off |
@dtcxzyw Apologies! I will be more careful going forward. Thank you! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should test flag preservation behavior. Should also add similar copysign tests, since those should be handled just like fabs
Thanks for sharing the guide. It seems like I made a mistake of committing from my main branch. Can I create a new PR with my new changes, as I know we cannot use forced commits? |
It's not that you can't, it's just generally discouraged but not always. But you can always create a new one |
Thanks for the reviews. I have opened a new PR #95627 with the changes. |
Here is my patch for #94170
Added the missing optimization in
visitCallInst
function to foldfabs(-x)
into `fabs(x)'. I also added the required test cases to verify the optimization for various data types, including scalar floats, doubles, and vector types.