Skip to content

[InstCombine] Fix a cycle when folding fneg(select) with scalable vector types #112465

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 3 commits into from
Oct 25, 2024

Conversation

ssijaric-nv
Copy link
Contributor

@ssijaric-nv ssijaric-nv commented Oct 16, 2024

The two folding operations are causing a cycle for the following case with
scalable vector types:

define <vscale x 2 x double> @test_fneg_select_abs(<vscale x 2 x i1> %cond, <vscale x 2 x double> %b) {
%1 = select <vscale x 2 x i1> %cond, <vscale x 2 x double> zeroinitializer, <vscale x 2 x double> %b
%2 = fneg fast <vscale x 2 x double> %1
ret <vscale x 2 x double> %2
}

  1. fold fneg: -(Cond ? C : Y) -> Cond ? -C : -Y

  2. fold select: (Cond ? -X : -Y) -> -(Cond ? X : Y)

  3. results in the following since '<vscale x 2 x double> zeroinitializer' passes
    the check for the immediate constant:

%.neg = fneg fast <vscale x 2 x double> zeroinitializer
%b.neg = fneg fast <vscale x 2 x double> %b
%1 = select fast <vscale x 2 x i1> %cond, <vscale x 2 x double> %.neg, <vscale x 2 x double> %b.neg

and so we end up going back and forth between 1) and 2).

Attempt to fold scalable vector constants, so that we end up with a splat instead:

define <vscale x 2 x double> @test_fneg_select_abs(<vscale x 2 x i1> %cond, <vscale x 2 x double> %b) {
%b.neg = fneg fast <vscale x 2 x double> %b
%1 = select fast <vscale x 2 x i1> %cond, <vscale x 2 x double> shufflevector (<vscale x 2 x double> insertelement (<vscale x 2 x double> poison, double -0.000000e+00, i64 0), <vscale x 2 x double> poison, <vscale x 2 x i32> zeroinitializer), <vscale x 2 x double> %b.neg
ret <vscale x 2 x double> %1
}

@ssijaric-nv ssijaric-nv requested a review from nikic as a code owner October 16, 2024 03:25
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 Oct 16, 2024

@llvm/pr-subscribers-llvm-ir

@llvm/pr-subscribers-llvm-transforms

Author: None (ssijaric-nv)

Changes

The two folding operations are causing a cycle for the following case with
scalable vector types:

define <vscale x 2 x double> @test_fneg_select_abs(<vscale x 2 x i1> %cond, <vscale x 2 x double> %b) {
%1 = select <vscale x 2 x i1> %cond, <vscale x 2 x double> zeroinitializer, <vscale x 2 x double> %b
%2 = fneg fast <vscale x 2 x double> %1
ret <vscale x 2 x double> %2
}

  1. fold fneg: -(Cond ? C : Y) -> Cond ? -C : -Y

  2. fold select: (Cond ? -X : -Y) -> -(Cond ? X : Y)

  3. results in the following since '<vscale x 2 x double> zeroinitializer' passes
    the check for the immediate constant:

%.neg = fneg fast <vscale x 2 x double> zeroinitializer
%b.neg = fneg fast <vscale x 2 x double> %b
%1 = select fast <vscale x 2 x i1> %cond, <vscale x 2 x double> %.neg, <vscale x 2 x double> %b.neg

and so we end up going back and forth between 1) and 2).

Don't perform 1) if we are dealing with scalable vector constants as there is no
immediate representation for a negative scalable zeroinitializer.


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp (+2-1)
  • (modified) llvm/test/Transforms/InstCombine/fneg.ll (+12)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index 21588aca512758..645fad14c1999c 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -2878,7 +2878,8 @@ Instruction *InstCombinerImpl::visitFNeg(UnaryOperator &I) {
 
     // -(Cond ? X : C) --> Cond ? -X : -C
     // -(Cond ? C : Y) --> Cond ? -C : -Y
-    if (match(X, m_ImmConstant()) || match(Y, m_ImmConstant())) {
+    if ((match(X, m_ImmConstant()) && !isa<ScalableVectorType>(X->getType())) ||
+        (match(Y, m_ImmConstant()) && !isa<ScalableVectorType>(Y->getType()))) {
       Value *NegX = Builder.CreateFNegFMF(X, &I, X->getName() + ".neg");
       Value *NegY = Builder.CreateFNegFMF(Y, &I, Y->getName() + ".neg");
       SelectInst *NewSel = SelectInst::Create(Cond, NegX, NegY);
diff --git a/llvm/test/Transforms/InstCombine/fneg.ll b/llvm/test/Transforms/InstCombine/fneg.ll
index 3c4088832feaaa..643c8fd8d8a742 100644
--- a/llvm/test/Transforms/InstCombine/fneg.ll
+++ b/llvm/test/Transforms/InstCombine/fneg.ll
@@ -1109,4 +1109,16 @@ define float @test_fneg_select_maxnum(float %x) {
   ret float %neg
 }
 
+; Check that there's no infinite loop.
+define <vscale x 2 x double> @test_fneg_select_svec(<vscale x 2 x i1> %cond, <vscale x 2 x double> %b) {
+; CHECK-LABEL: @test_fneg_select_svec(
+; CHECK-NEXT:    [[TMP1:%.*]] = select <vscale x 2 x i1> [[COND:%.*]], <vscale x 2 x double> zeroinitializer, <vscale x 2 x double> [[B:%.*]]
+; CHECK-NEXT:    [[TMP2:%.*]] = fneg fast <vscale x 2 x double> [[TMP1]]
+; CHECK-NEXT:    ret <vscale x 2 x double> [[TMP2]]
+;
+  %1 = select <vscale x 2 x i1> %cond, <vscale x 2 x double> zeroinitializer, <vscale x 2 x double> %b
+  %2 = fneg fast <vscale x 2 x double> %1
+  ret <vscale x 2 x double> %2
+}
+
 !0 = !{}

@goldsteinn goldsteinn requested review from arsenm and dtcxzyw October 16, 2024 04:14
@arsenm
Copy link
Contributor

arsenm commented Oct 16, 2024

  1. if we are dealing with scalable vector constants as there is no
    immediate representation for a negative scalable zeroinitializer.

I thought there was a splat constant for these, this would just be a splat of -0

%2 = fneg fast <vscale x 2 x double> %1
ret <vscale x 2 x double> %2
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Test with zeroinitializer in both positions

@@ -2878,7 +2878,8 @@ Instruction *InstCombinerImpl::visitFNeg(UnaryOperator &I) {

// -(Cond ? X : C) --> Cond ? -X : -C
// -(Cond ? C : Y) --> Cond ? -C : -Y
if (match(X, m_ImmConstant()) || match(Y, m_ImmConstant())) {
if ((match(X, m_ImmConstant()) && !isa<ScalableVectorType>(X->getType())) ||
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 use ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL) instead of using IRBuilder to fix this issue.

if (match(X, m_ImmConstant(XC)) || match(Y, m_ImmConstant(YC))) {
Value *NegX = ConstantFoldUnaryOpOperand(Instruction::FNeg, XC, DL);
Value *NegY = ConstantFoldUnaryOpOperand(Instruction::FNeg, YC, DL);
if (NegX || NegY) {
if (!NegX) NegX = Builder.CreateFNeg...;
if (!NegY) NegY = Builder.CreateFNeg...;
SelectInst *NewSel = SelectInst::Create(Cond, NegX, NegY);
propagateSelectFMF(NewSel, /CommonOperand=/true);
return NewSel;
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for having a look, Yingwei. I'll test and upload the revised version.

Copy link
Contributor

Choose a reason for hiding this comment

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

The IRBuilder should do this like in other cases?

@ssijaric-nv
Copy link
Contributor Author

  1. if we are dealing with scalable vector constants as there is no
    immediate representation for a negative scalable zeroinitializer.

I thought there was a splat constant for these, this would just be a splat of -0

Thanks for having a look, Matt. Would a better approach be to generate the following, instead of giving up?

define <vscale x 2 x double> @test_fneg_select_svec(<vscale x 2 x i1> %cond, <vscale x 2 x double> %b) {
%1 = fneg fast <vscale x 2 x double> %b
%2 = select <vscale x 2 x i1> %cond, <vscale x 2 x double> splat(double -0.0), <vscale x 2 x double> %1
ret <vscale x 2 x double> %2
}

@nikic
Copy link
Contributor

nikic commented Oct 16, 2024

  1. if we are dealing with scalable vector constants as there is no
    immediate representation for a negative scalable zeroinitializer.

I thought there was a splat constant for these, this would just be a splat of -0

Thanks for having a look, Matt. Would a better approach be to generate the following, instead of giving up?

define <vscale x 2 x double> @test_fneg_select_svec(<vscale x 2 x i1> %cond, <vscale x 2 x double> %b) { %1 = fneg fast <vscale x 2 x double> %b %2 = select <vscale x 2 x i1> %cond, <vscale x 2 x double> splat(double -0.0), <vscale x 2 x double> %1 ret <vscale x 2 x double> %2 }

Yes. It looks like ConstantFoldUnaryInstruction is currently missing splat support for scalable vectors. Adding that should resolve the issue.

Comment on lines 2884 to 2887
if (XC)
NegX = ConstantFoldUnaryOpOperand(Instruction::FNeg, XC, DL);
if (YC)
NegY = ConstantFoldUnaryOpOperand(Instruction::FNeg, YC, DL);
Copy link
Contributor

Choose a reason for hiding this comment

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

Generally the attempt to constant fold should be hidden inside the IRBuilder Create* functions

Copy link
Contributor

Choose a reason for hiding this comment

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

Right, just the changes to ConstantFold should be sufficient, after that there should not be a need to touch this InstCombine code anymore...

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

LGTM

@ssijaric-nv
Copy link
Contributor Author

ssijaric-nv commented Oct 24, 2024

Thank you, all. Can someone please merge this change, as I don't have write access? I'll rebase and push again, to get a clean build before merging.

I ran the test-suite locally with -O3 on AArch64 and x86 machines:

AArch64:
Total Discovered Tests: 3425
Passed: 3425 (100.00%)

With -mcpu=neoverse-v2
Total Discovered Tests: 3425
Passed: 3425 (100.00%)

x86:
Total Discovered Tests: 2441
Passed: 2441 (100.00%)

ninja check-llvm passed as well.

…tor types

The two folding operations are causing a cycle for the following case with
scalable vector types:

define <vscale x 2 x double> @test_fneg_select_abs(<vscale x 2 x i1> %cond, <vscale x 2 x double> %b) {
  %1 = select <vscale x 2 x i1> %cond, <vscale x 2 x double> zeroinitializer, <vscale x 2 x double> %b
  %2 = fneg fast <vscale x 2 x double> %1
  ret <vscale x 2 x double> %2
}

1) fold fneg:  -(Cond ? C : Y) -> Cond ? -C : -Y

2) fold select: (Cond ? -X : -Y) -> -(Cond ? X : Y)

1) results in the following since '<vscale x 2 x double> zeroinitializer' passes
the check for the immediate constant:

%.neg = fneg fast <vscale x 2 x double> zeroinitializer
%b.neg = fneg fast <vscale x 2 x double> %b
%1 = select fast <vscale x 2 x i1> %cond, <vscale x 2 x double> %.neg, <vscale x 2 x double> %b.neg

and so we end up going back and forth between 1) and 2).

Don't perform 1) if we are dealing with scalable vector constants as there is no
immediate representation for a negative scalable zeroinitializer.
@ssijaric-nv ssijaric-nv force-pushed the fneg_select_sve_fold_imm branch from a0944b5 to f2e4e8b Compare October 24, 2024 21:06
@arsenm arsenm merged commit 14db069 into llvm:main Oct 25, 2024
8 checks passed
Copy link

@ssijaric-nv 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!

NoumanAmir657 pushed a commit to NoumanAmir657/llvm-project that referenced this pull request Nov 4, 2024
…tor types (llvm#112465)

The two folding operations are causing a cycle for the following case
with
scalable vector types:

define <vscale x 2 x double> @test_fneg_select_abs(<vscale x 2 x i1>
%cond, <vscale x 2 x double> %b) {
%1 = select <vscale x 2 x i1> %cond, <vscale x 2 x double>
zeroinitializer, <vscale x 2 x double> %b
  %2 = fneg fast <vscale x 2 x double> %1
  ret <vscale x 2 x double> %2
}

1) fold fneg:  -(Cond ? C : Y) -> Cond ? -C : -Y

2) fold select: (Cond ? -X : -Y) -> -(Cond ? X : Y)

1) results in the following since '<vscale x 2 x double>
zeroinitializer' passes
the check for the immediate constant:

%.neg = fneg fast <vscale x 2 x double> zeroinitializer
%b.neg = fneg fast <vscale x 2 x double> %b
%1 = select fast <vscale x 2 x i1> %cond, <vscale x 2 x double> %.neg,
<vscale x 2 x double> %b.neg

and so we end up going back and forth between 1) and 2).

Attempt to fold scalable vector constants, so that we end up with a
splat instead:

define <vscale x 2 x double> @test_fneg_select_abs(<vscale x 2 x i1>
%cond, <vscale x 2 x double> %b) {
  %b.neg = fneg fast <vscale x 2 x double> %b
%1 = select fast <vscale x 2 x i1> %cond, <vscale x 2 x double>
shufflevector (<vscale x 2 x double> insertelement (<vscale x 2 x
double> poison, double -0.000000e+00, i64 0), <vscale x 2 x double>
poison, <vscale x 2 x i32> zeroinitializer), <vscale x 2 x double>
%b.neg
  ret <vscale x 2 x double> %1
}
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.

5 participants