Skip to content

[InstCombine] Add fold for fabs(-x) -> fabs(x) #95627

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

Merged
merged 6 commits into from
Jun 27, 2024

Conversation

VaibhavRumale
Copy link
Contributor

@VaibhavRumale VaibhavRumale commented Jun 15, 2024

This patch folds fabs(-x) -> fabs(x)

Closes #94170

Proofs: https://alive2.llvm.org/ce/z/gjzmgf

@VaibhavRumale VaibhavRumale requested a review from nikic as a code owner June 15, 2024 02:36
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

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
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

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.

@llvmbot
Copy link
Member

llvmbot commented Jun 15, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Vaibhav (VaibhavRumale)

Changes

Here is my patch for #94170 for fold transformation for fabs(-x) -> fabs(x)

  • [InstCombine] Add tests for fold: fabs(-x) -> fabs(x)
  • [InstCombine] Add fold transformation: fabs(-x) -> fabs(x)

Proofs: https://alive2.llvm.org/ce/z/pGzLts


Full diff: https://github.com/llvm/llvm-project/pull/95627.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp (+7)
  • (added) llvm/test/Transforms/InstCombine/fabs-fneg-fold.ll (+138)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index 436cdbff75669..f91aea35d1aab 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -2511,6 +2511,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..bc239f207ef42
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/fabs-fneg-fold.ll
@@ -0,0 +1,138 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S -passes=instcombine %s | FileCheck %s
+
+define float @fabs_fneg_basic(float %x) {
+; CHECK-LABEL: define float @fabs_fneg_basic(
+; CHECK-SAME: float [[X:%.*]]) {
+; 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-SAME: <2 x float> [[X:%.*]]) {
+; 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-SAME: double [[X:%.*]]) {
+; 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-SAME: <4 x double> [[X:%.*]]) {
+; 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-SAME: half [[X:%.*]]) {
+; 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
+}
+
+define float @fabs_fneg_nnan(float %x) {
+; CHECK-LABEL: define float @fabs_fneg_nnan(
+; CHECK-SAME: float [[X:%.*]]) {
+; CHECK-NEXT:    [[FABS:%.*]] = call float @llvm.fabs.f32(float [[X]])
+; CHECK-NEXT:    ret float [[FABS]]
+;
+  %neg = fneg nnan float %x
+  %fabs = call nnan float @llvm.fabs.f32(float %neg)
+  ret float %fabs
+}
+
+define float @fabs_fneg_ninf(float %x) {
+; CHECK-LABEL: define float @fabs_fneg_ninf(
+; CHECK-SAME: float [[X:%.*]]) {
+; CHECK-NEXT:    [[FABS:%.*]] = call float @llvm.fabs.f32(float [[X]])
+; CHECK-NEXT:    ret float [[FABS]]
+;
+  %neg = fneg ninf float %x
+  %fabs = call ninf float @llvm.fabs.f32(float %neg)
+  ret float %fabs
+}
+
+define float @fabs_fneg_no_fabs(float %x) {
+; CHECK-LABEL: define float @fabs_fneg_no_fabs(
+; CHECK-SAME: float [[X:%.*]]) {
+; CHECK-NEXT:    [[NEG:%.*]] = fneg float [[X]]
+; CHECK-NEXT:    ret float [[NEG]]
+;
+  %neg = fneg float %x
+  ret float %neg
+}
+
+define <2 x float> @fabs_fneg_splat_v2f32(<2 x float> %x) {
+; CHECK-LABEL: define <2 x float> @fabs_fneg_splat_v2f32(
+; CHECK-SAME: <2 x float> [[X:%.*]]) {
+; CHECK-NEXT:    ret <2 x float> <float 2.000000e+00, float 2.000000e+00>
+;
+  %neg = fneg <2 x float> <float -2.0, float -2.0>
+  %fabs = call <2 x float> @llvm.fabs.v2f32(<2 x float> %neg)
+  ret <2 x float> %fabs
+}
+
+
+define <2 x float> @fabs_fneg_splat_poison_v2f32(<2 x float> %x) {
+; CHECK-LABEL: define <2 x float> @fabs_fneg_splat_poison_v2f32(
+; CHECK-SAME: <2 x float> [[X:%.*]]) {
+; CHECK-NEXT:    [[FABS:%.*]] = call <2 x float> @llvm.fabs.v2f32(<2 x float> <float -2.000000e+00, float poison>)
+; CHECK-NEXT:    ret <2 x float> [[FABS]]
+;
+  %neg = fneg <2 x float> <float 2.0, float poison>
+  %fabs = call <2 x float> @llvm.fabs.v2f32(<2 x float> %neg)
+  ret <2 x float> %fabs
+}
+
+define <2 x float> @fabs_fneg_non_splat_v2f32(<2 x float> %x) {
+; CHECK-LABEL: define <2 x float> @fabs_fneg_non_splat_v2f32(
+; CHECK-SAME: <2 x float> [[X:%.*]]) {
+; CHECK-NEXT:    ret <2 x float> <float 2.000000e+00, float 3.000000e+00>
+;
+  %neg = fneg <2 x float> <float 2.0, float 3.0>
+  %fabs = call <2 x float> @llvm.fabs.v2f32(<2 x float> %neg)
+  ret <2 x float> %fabs
+}
+
+declare void @use(float)
+
+define float @fabs_fneg_multi_use(float %x) {
+; CHECK-LABEL: define float @fabs_fneg_multi_use(
+; CHECK-SAME: float [[X:%.*]]) {
+; CHECK-NEXT:    [[NEG:%.*]] = fneg float [[X]]
+; CHECK-NEXT:    call void @use(float [[NEG]])
+; CHECK-NEXT:    [[FABS:%.*]] = call float @llvm.fabs.f32(float [[X]])
+; CHECK-NEXT:    ret float [[FABS]]
+;
+  %neg = fneg float %x
+  call void @use(float %neg)
+  %fabs = call float @llvm.fabs.f32(float %neg)
+  ret float %fabs
+}
+

Value* Arg = II->getArgOperand(0);
Value* X;
// fabs (-X) --> fabs (X)
if (match(Arg, m_FNeg(m_Value(X))))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment #94183 (comment) is not resolved.

Copy link

github-actions bot commented Jun 15, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@arsenm
Copy link
Contributor

arsenm commented Jun 25, 2024

Reverse ping?

Copy link
Member

@dtcxzyw dtcxzyw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you!

Value *Arg = II->getArgOperand(0);
Value *X;
// fabs (-X) --> fabs (X)
if (match(Arg, m_FNeg(m_Value(X)))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can merge this fold with fabs(copysign(x, y)) -> fabs(x).

```suggestion
    if (match(Arg, m_CombineOr(m_FNeg(m_Value(X)), m_CopySign(m_Value(X), m_Value(Unused))))) {

@VaibhavRumale
Copy link
Contributor Author

Reverse ping?

Sorry for the delay; I was busy with my graduation. All requested changes have been made. Thank you!

@arsenm arsenm merged commit ea68668 into llvm:main Jun 27, 2024
7 checks passed
Copy link

@VaibhavRumale Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested
by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as
the builds can include changes from many authors. It is not uncommon for your
change to be included in a build that fails due to someone else's changes, or
infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself.
This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

lravenclaw pushed a commit to lravenclaw/llvm-project that referenced this pull request Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

missing fold: fabs(-x) -> fabs(x)
4 participants