[InstCombine] Drop wrong ninf for `(-X / Y) + Z` #228684
<!--LLVM NEW CONTRIBUTOR COMMENT-->
Hello @suogesi :wave:
Thank you for submitting a Pull Request (PR) to the LLVM Project. Since this is your first PR, here are a few useful links covering our main contribution policies and review practices.
All contributions to LLVM must follow our LLVM AI Tool Use Policy. In particular, if you used AI while working on this PR, remember to add a note to the PR description.
The LLVM Code-Review Policy and Practices document contains practical information about the PR process, including how patches are reviewed and accepted, and who can review a PR.
Our LLVM Developer Policy describes our expectations for code quality, commit summaries and contains notes on our CI system.
The InstCombine Contributor Guide lays out a series of rules that contributions to InstCombine (and other middle-end components) should follow.
Please reply to this message to confirm that you have read these policies, especially the LLVM AI Tool Use Policy, and that any AI tool usage has been noted in the PR description.
---
Frequently asked questions
How do I add reviewers?
This PR will be automatically labeled, and the relevant teams will be notified. For some parts of the project, reviewers may also be added automatically.
You can also add reviewers manually using the Reviewers section on this page. If you cannot use that section, it is probably because you do not have write permissions for the repository. In that case, you can request a review by tagging reviewers in a comment using @ followed by their GitHub username.
What if there are no comments?
If you have not received any comments on your PR after a week, you can request a review by pinging the PR with a comment such as “Ping”. The common courtesy ping rate is once a week. Please remember that you are asking for volunteer time from other developers.
Are any special GitHub settings required to contribute to LLVM?
We only require contributors to have a public email address associated with their GitHub commits, see this section of LLVM Developer Policy for details.
---
If you have questions, feel free to leave a comment on this PR, or ask on LLVM Discord or LLVM Discourse.
Thank you,
The LLVM Community
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: 伍浩宇 (suogesi)
<details>
<summary>Changes</summary>
When folding (-X / Y) + Z into Z - X / Y, it is treated as a BinaryOperator so final ninf is same as the add instruction. This iswrong when add instruction has ninf but div instruction doesn't have ninf.
Take first operand as FMFSource so the optimized div instruction have correct ninf now.
Fixes #<!-- -->228521
---
Full diff: https://github.com/llvm/llvm-project/pull/228684.diff
2 Files Affected:
````````````diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index 281e3b353f6dd..4622e3f02b87e 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -2167,7 +2167,9 @@ Instruction *InstCombinerImpl::visitFAdd(BinaryOperator &I) {
m_Value(Z))) ||
match(&I, m_c_FAdd(m_OneUse(m_FDiv(m_Value(X), m_FNeg(m_Value(Y)))),
m_Value(Z)))) {
- Value *XY = Builder.CreateFDivFMF(X, Y, &I);
+ Value *Div = I.getOperand(0);
+ auto *DivInst = cast<BinaryOperator>(Div);
+ Value *XY = Builder.CreateFDivFMF(X, Y, DivInst);
return BinaryOperator::CreateFSubFMF(Z, XY, &I);
}
diff --git a/llvm/test/Transforms/InstCombine/fadd.ll b/llvm/test/Transforms/InstCombine/fadd.ll
index c3f5cc742dd28..cce857f83f73b 100644
--- a/llvm/test/Transforms/InstCombine/fadd.ll
+++ b/llvm/test/Transforms/InstCombine/fadd.ll
@@ -45,6 +45,18 @@ define double @fdiv_fneg1(double %x, double %y, double %pz) {
ret double %r
}
+define double @fdiv_fneg1_ninf(double %x, double %y, double %z) {
+; CHECK-LABEL: @fdiv_fneg1_ninf(
+; CHECK-NEXT: [[TMP1:%.*]] = fdiv double [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[R:%.*]] = fsub ninf double [[Z:%.*]], [[TMP1]]
+; CHECK-NEXT: ret double [[R]]
+;
+ %nx = fneg double %x
+ %d = fdiv double %nx, %y
+ %r = fadd ninf double %z, %d
+ ret double %r
+}
+
; Z + (Y / -X) --> Z - (Y / X)
define <2 x double> @fdiv_fneg2(<2 x double> %x, <2 x double> %y, <2 x double> %pz) {
@@ -61,6 +73,18 @@ define <2 x double> @fdiv_fneg2(<2 x double> %x, <2 x double> %y, <2 x double> %
ret <2 x double> %r
}
+define double @fdiv_fneg2_ninf(double %x, double %y, double %z) {
+; CHECK-LABEL: @fdiv_fneg2_ninf(
+; CHECK-NEXT: [[TMP1:%.*]] = fdiv double [[Y:%.*]], [[X:%.*]]
+; CHECK-NEXT: [[R:%.*]] = fsub ninf double [[Z:%.*]], [[TMP1]]
+; CHECK-NEXT: ret double [[R]]
+;
+ %nx = fneg double %x
+ %d = fdiv double %y, %nx
+ %r = fadd ninf double %z, %d
+ ret double %r
+}
+
; Z + (-X * Y) --> Z - (X * Y)
define double @fmul_fneg1(double %x, double %y, double %pz) {
@@ -111,6 +135,18 @@ define double @fdiv_fneg1_commute(double %x, double %y, double %pz) {
ret double %r
}
+define double @fdiv_fneg1_commute_ninf(double %x, double %y, double %z) {
+; CHECK-LABEL: @fdiv_fneg1_commute_ninf(
+; CHECK-NEXT: [[TMP1:%.*]] = fdiv double [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[R:%.*]] = fsub ninf double [[Z:%.*]], [[TMP1]]
+; CHECK-NEXT: ret double [[R]]
+;
+ %nx = fneg double %x
+ %d = fdiv double %nx, %y
+ %r = fadd ninf double %d, %z
+ ret double %r
+}
+
; (Y / -X) + Z --> Z - (Y / X)
define <2 x double> @fdiv_fneg2_commute(<2 x double> %x, <2 x double> %y, <2 x double> %pz) {
@@ -127,6 +163,18 @@ define <2 x double> @fdiv_fneg2_commute(<2 x double> %x, <2 x double> %y, <2 x d
ret <2 x double> %r
}
+define double @fdiv_fneg2_commute_ninf(double %x, double %y, double %z) {
+; CHECK-LABEL: @fdiv_fneg2_commute_ninf(
+; CHECK-NEXT: [[TMP1:%.*]] = fdiv double [[Y:%.*]], [[X:%.*]]
+; CHECK-NEXT: [[R:%.*]] = fsub ninf double [[Z:%.*]], [[TMP1]]
+; CHECK-NEXT: ret double [[R]]
+;
+ %nx = fneg double %x
+ %d = fdiv double %y, %nx
+ %r = fadd ninf double %d, %z
+ ret double %r
+}
+
; (-X * Y) + Z --> Z - (X * Y)
define double @fmul_fneg1_commute(double %x, double %y, double %pz) {
@@ -829,11 +877,11 @@ define float @fadd_reduce_sqr_sum_varA_not_one_use1(float %a, float %b) {
define float @fadd_reduce_sqr_sum_varA_not_one_use2(float %a, float %b) {
; CHECK-LABEL: @fadd_reduce_sqr_sum_varA_not_one_use2(
-; CHECK-NEXT: [[A_SQ:%.*]] = fmul float [[A:%.*]], [[A:%.*]]
-; CHECK-NEXT: [[AB:%.*]] = fadd reassoc nsz float [[A:%.*]], [[B:%.*]]
-; CHECK-NEXT: [[AB_SQ:%.*]] = fmul reassoc nsz float [[AB:%.*]], [[AB:%.*]]
-; CHECK-NEXT: tail call void @fake_func(float [[A_SQ:%.*]])
-; CHECK-NEXT: ret float [[AB_SQ:%.*]]
+; CHECK-NEXT: [[A_SQ:%.*]] = fmul float [[A:%.*]], [[A]]
+; CHECK-NEXT: [[AB:%.*]] = fadd reassoc nsz float [[A]], [[B:%.*]]
+; CHECK-NEXT: [[AB_SQ:%.*]] = fmul reassoc nsz float [[AB]], [[AB]]
+; CHECK-NEXT: tail call void @fake_func(float [[A_SQ]])
+; CHECK-NEXT: ret float [[AB_SQ]]
;
%a_sq = fmul float %a, %a
%two_a = fmul float %a, 2.0
</details>
Could you add a negative test?
There are 4 combinations about ninf:
1. div with ninf, add with ninf.
2. div without ninf, add with ninf.
3. div with ninf, add without ninf.
4. div without ninf, add without ninf.
This PR is trying to fix 2. For this pass, I want to know which of them should be called "positive test" and which of them should be called "negative test"?
Case 1 is the positive test: the new fdiv keeps ninf. Case 2 is the negative test: the fadd's ninf must not be propagated to the new fdiv.
Case 1 is added with name fdiv_fneg_ninf and Case 2 is renamed to fdiv_fneg_ninf_does_not_propagate_to_div now.
It turns out that this fix is bad because match expression can swap operators so the first operator is not always fdiv instruction. I'm using dyn_cast to check LHS and RHS now.
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:white_check_mark: With the latest revision this PR passed the C/C++ code formatter.
ping @harrisonGPU
What if Z is also a BinaryOperator on the LHS? Maybe bind the fdiv directly in the matcher instead.
The AI also found (-X * Y) + Z fold above copies the fadd flags onto the fmul in the same way, which looks wrong too.
I think there should be another PR for this.
That makes sense. Now it is driven by match. Also added more tests for situations that two ops are fdiv.
ping
ping
harrisonGPU
suogesi
When folding (-X / Y) + Z into Z - X / Y, it is treated as a
BinaryOperatorso finalninfis same as the add instruction. This iswrong when add instruction hasninfbut div instruction doesn't haveninf.Take first operand as FMFSource so the optimized div instruction have correct ninf now.
Fixes #228521