-
Notifications
You must be signed in to change notification settings - Fork 14.6k
[X86][ISel][FMA] Get a handle on operand nodes when negating FMA #130176
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 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 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. |
@llvm/pr-subscribers-backend-x86 Author: Vineet Kumar (vntkmr) ChangesWhen negaitng an FMA opcode, a new node created for a negated FMA operand may be deleted while recursively negating another FMA operand. This causes the following assertion to fail:
This patch adds a temporary handle on the new negated nodes to prevent them from being deleted. Full diff: https://github.com/llvm/llvm-project/pull/130176.diff 2 Files Affected:
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index deab638b7e546..1b56826667329 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -55673,7 +55673,12 @@ static SDValue combineFMA(SDNode *N, SelectionDAG &DAG,
// Do not convert the passthru input of scalar intrinsics.
// FIXME: We could allow negations of the lower element only.
bool NegA = invertIfNegative(A);
+ // Create a dummy use for A so that in the process of negating B or C
+ // recursively, it is not deleted.
+ HandleSDNode NegAHandle(A);
bool NegB = invertIfNegative(B);
+ // Similar to A, get a handle on B.
+ HandleSDNode NegBHandle(B);
bool NegC = invertIfNegative(C);
if (!NegA && !NegB && !NegC)
diff --git a/llvm/test/CodeGen/X86/combine-fma-negate.ll b/llvm/test/CodeGen/X86/combine-fma-negate.ll
new file mode 100644
index 0000000000000..945df32781464
--- /dev/null
+++ b/llvm/test/CodeGen/X86/combine-fma-negate.ll
@@ -0,0 +1,25 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
+; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mattr=+avx512f -mattr=+fma | FileCheck %s
+
+define void @fma_neg(<8 x i1> %r280, ptr %pp1, ptr %pp2) {
+; CHECK-LABEL: fma_neg:
+; CHECK: # %bb.0:
+; CHECK-NEXT: vpmovsxwq %xmm0, %zmm0
+; CHECK-NEXT: vpsllq $63, %zmm0, %zmm0
+; CHECK-NEXT: vptestmq %zmm0, %zmm0, %k1
+; CHECK-NEXT: vmovdqu64 (%rdi), %zmm0
+; CHECK-NEXT: vpxorq {{\.?LCPI[0-9]+_[0-9]+}}(%rip){1to8}, %zmm0, %zmm1 {%k1} {z}
+; CHECK-NEXT: vxorpd %xmm2, %xmm2, %xmm2
+; CHECK-NEXT: vfnmadd213pd {{.*#+}} zmm2 = -(zmm0 * zmm2) + zmm1
+; CHECK-NEXT: vmovupd %zmm2, (%rsi)
+; CHECK-NEXT: vzeroupper
+; CHECK-NEXT: retq
+ %r290 = load <8 x double>, ptr %pp1, align 8
+ %r307 = fneg <8 x double> %r290
+ %r309 = select <8 x i1> %r280, <8 x double> %r307, <8 x double> zeroinitializer
+ %r311 = tail call <8 x double> @llvm.x86.avx512.vfmadd.pd.512(<8 x double> %r307, <8 x double> zeroinitializer, <8 x double> %r309, i32 4)
+ store <8 x double> %r311, ptr %pp2, align 8
+ ret void
+}
+
+declare <8 x double> @llvm.x86.avx512.vfmadd.pd.512(<8 x double>, <8 x double>, <8 x double>, i32 immarg)
|
e88fec7
to
18b1978
Compare
You can test this locally with the following command:git-clang-format --diff 1b75b9e665ee3c43de85c25f8d5f10d4efb3ca39 18b197885678e2ae241889447781b12361103696 --extensions cpp -- llvm/lib/Target/X86/X86ISelLowering.cpp View the diff from clang-format here.diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index 1b56826667..e7b75fdf23 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -55673,12 +55673,12 @@ static SDValue combineFMA(SDNode *N, SelectionDAG &DAG,
// Do not convert the passthru input of scalar intrinsics.
// FIXME: We could allow negations of the lower element only.
bool NegA = invertIfNegative(A);
- // Create a dummy use for A so that in the process of negating B or C
- // recursively, it is not deleted.
- HandleSDNode NegAHandle(A);
+ // Create a dummy use for A so that in the process of negating B or C
+ // recursively, it is not deleted.
+ HandleSDNode NegAHandle(A);
bool NegB = invertIfNegative(B);
- // Similar to A, get a handle on B.
- HandleSDNode NegBHandle(B);
+ // Similar to A, get a handle on B.
+ HandleSDNode NegBHandle(B);
bool NegC = invertIfNegative(C);
if (!NegA && !NegB && !NegC)
|
18b1978
to
e46c217
Compare
When negating an FMA opcode, a new node created for a negated FMA operand may be deleted while recursively negating another FMA operand. This commit adds a temporary handle on the new negated nodes to prevent them from being deleted.
e46c217
to
d45f79f
Compare
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.
LGTM - sorry for the noise
Thanks for the approval @RKSimon. Can someone please help me merge this? I do not have the upstream commit access yet. |
@vntkmr 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! |
When negaitng an FMA opcode, a new node created for a negated FMA operand may be deleted while recursively negating another FMA operand. This causes the following assertion to fail:
This patch adds a temporary handle on the new negated nodes to prevent them from being deleted.
For eg. see https://godbolt.org/z/Tq4PvnKM4 .