-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[NVPTX] Lower -1/x to neg.f64(rcp.rn.f64) instead of fdiv #98343
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-backend-nvptx Author: Rajat Bajpai (rajatbajpai) ChangesThe NVPTX backend lowers 1/x to rcp.rn.f64 instruction instead of slower fdiv instruction. However, in the case of -1/x, it uses the slower fdiv instruction. After this change, -1/x will be lowered into neg.f64 (rcp.rn.f64). Full diff: https://github.com/llvm/llvm-project/pull/98343.diff 2 Files Affected:
diff --git a/llvm/lib/Target/NVPTX/NVPTXInstrInfo.td b/llvm/lib/Target/NVPTX/NVPTXInstrInfo.td
index 827febe845a4c..4183d6ac2537c 100644
--- a/llvm/lib/Target/NVPTX/NVPTXInstrInfo.td
+++ b/llvm/lib/Target/NVPTX/NVPTXInstrInfo.td
@@ -1150,6 +1150,16 @@ def DoubleConst1 : PatLeaf<(fpimm), [{
return &N->getValueAPF().getSemantics() == &llvm::APFloat::IEEEdouble() &&
N->getValueAPF().convertToDouble() == 1.0;
}]>;
+// Constant -1.0 (double)
+def DoubleConstNeg1 : PatLeaf<(fpimm), [{
+ return &N->getValueAPF().getSemantics() == &llvm::APFloat::IEEEdouble() &&
+ N->getValueAPF().convertToDouble() == -1.0;
+}]>;
+// Constant -1.0 -> 1.0 (double)
+def NegDoubleConst : SDNodeXForm<fpimm, [{
+ return CurDAG->getTargetConstantFP(-(N->getValueAPF()),
+ SDLoc(N), MVT::f64);
+}]>;
// Loads FP16 constant into a register.
//
@@ -1225,6 +1235,10 @@ def FDIV64ri :
"div.rn.f64 \t$dst, $a, $b;",
[(set Float64Regs:$dst, (fdiv Float64Regs:$a, fpimm:$b))]>;
+// fdiv -1.0, X => fneg (rcp.rn X)
+def : Pat<(fdiv DoubleConstNeg1:$a, Float64Regs:$b),
+ (FNEGf64 (FDIV641r (NegDoubleConst node:$a), Float64Regs:$b))>;
+
//
// F32 Approximate reciprocal
//
diff --git a/llvm/test/CodeGen/NVPTX/rcp-opt.ll b/llvm/test/CodeGen/NVPTX/rcp-opt.ll
new file mode 100644
index 0000000000000..889ba63610881
--- /dev/null
+++ b/llvm/test/CodeGen/NVPTX/rcp-opt.ll
@@ -0,0 +1,13 @@
+; RUN: llc < %s -march=nvptx64 | FileCheck %s
+; RUN: %if ptxas %{ llc < %s -march=nvptx64 | %ptxas-verify %}
+
+;; Check if fdiv -1, X lowers to fneg (rcp.rn X).
+
+; CHECK-LABEL: .func{{.*}}test1
+define double @test1(double %in) {
+; CHECK: rcp.rn.f64 [[RCP:%.*]], [[X:%.*]];
+; CHECK-NEXT: neg.f64 [[FNEG:%.*]], [[RCP]];
+ %div = fdiv double 1.000000e+00, %in
+ %neg = fsub double -0.000000e+00, %div
+ ret double %neg
+}
|
@Artem-B could you please review this PR? |
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 with a comment nit.
43b539b
to
2f071f5
Compare
@Artem-B Looks like I don't have the necessary permissions to merge this PR. Could you please merge this PR on my behalf? Thanks! |
The NVPTX backend lowers 1/x to rcp.rn.f64 instruction instead of slower fdiv instruction. However, in the case of -1/x, it uses the slower fdiv instruction. After this change, -1/x will be lowered into neg.f64 (rcp.rn.f64).
I see Artem has approved and pipeline is also clean. |
@rajatbajpai 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 Please check whether problems have been caused by your change specifically, as 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. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
Thanks @durga4github for merging this change, and everyone else for taking the time to review this change. |
The NVPTX backend lowers 1/x to rcp.rn.f64 instruction instead of slower fdiv instruction. However, in the case of -1/x, it uses the slower fdiv instruction. After this change, -1/x will be lowered into neg.f64 (rcp.rn.f64).
The NVPTX backend lowers 1/x to rcp.rn.f64 instruction instead of slower fdiv instruction. However, in the case of -1/x, it uses the slower fdiv instruction. After this change, -1/x will be lowered into neg.f64 (rcp.rn.f64).