-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[InstCombine] Fold tan(x) * cos(x) => sin(x) #136319
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
Open
amordo
wants to merge
9
commits into
llvm:main
Choose a base branch
from
amordo:fold_tan_mul_cos
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1f2fb83
[InstCombine] Pre-commit tests (NFC)
amordo f06b8aa
[InstCombine] Fold tan(x) * cos(x) => sin(x)
amordo 3ce32a0
Apply recommendations
amordo 52e45f7
Use m_Deferred instead m_Specific
amordo ac3ab9a
Add commuted and negative tests
amordo d23c71f
Preserve fpmath flag; add tests
amordo e39ab3f
Merge branch 'llvm:main' into fold_tan_mul_cos
amordo 4715cb8
Check contract flag instead of reassoc
amordo afb912f
Merge branch 'main' into fold_tan_mul_cos
amordo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 | ||
amordo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
; RUN: opt -S -passes=instcombine < %s | FileCheck %s | ||
|
||
define double @fmul_tan_cos(double %a) { | ||
; CHECK-LABEL: define double @fmul_tan_cos( | ||
; CHECK-SAME: double [[A:%.*]]) { | ||
; CHECK-NEXT: [[TAN:%.*]] = call double @llvm.tan.f64(double [[A]]) | ||
; CHECK-NEXT: [[COS:%.*]] = call double @llvm.cos.f64(double [[A]]) | ||
; CHECK-NEXT: [[RES:%.*]] = fmul double [[TAN]], [[COS]] | ||
; CHECK-NEXT: ret double [[RES]] | ||
; | ||
%tan = call double @llvm.tan.f64(double %a) | ||
%cos = call double @llvm.cos.f64(double %a) | ||
%res = fmul double %tan, %cos | ||
ret double %res | ||
} | ||
|
||
amordo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
define double @fmul_strict_tan_strict_cos_reassoc(double %a) { | ||
; CHECK-LABEL: define double @fmul_strict_tan_strict_cos_reassoc( | ||
; CHECK-SAME: double [[A:%.*]]) { | ||
; CHECK-NEXT: [[TAN:%.*]] = call double @llvm.tan.f64(double [[A]]) | ||
; CHECK-NEXT: [[COS:%.*]] = call reassoc double @llvm.cos.f64(double [[A]]) | ||
; CHECK-NEXT: [[RES:%.*]] = fmul double [[TAN]], [[COS]] | ||
; CHECK-NEXT: ret double [[RES]] | ||
; | ||
%tan = call double @llvm.tan.f64(double %a) | ||
%cos = call reassoc double @llvm.cos.f64(double %a) | ||
%res = fmul double %tan, %cos | ||
ret double %res | ||
} | ||
|
||
define double @fmul_reassoc_tan_strict_cos_strict(double %a) { | ||
; CHECK-LABEL: define double @fmul_reassoc_tan_strict_cos_strict( | ||
; CHECK-SAME: double [[A:%.*]]) { | ||
; CHECK-NEXT: [[RES:%.*]] = call reassoc double @llvm.sin.f64(double [[A]]) | ||
; CHECK-NEXT: ret double [[RES]] | ||
; | ||
%tan = call double @llvm.tan.f64(double %a) | ||
%cos = call double @llvm.cos.f64(double %a) | ||
%res = fmul reassoc double %tan, %cos | ||
ret double %res | ||
} | ||
|
||
define double @fmul_reassoc_tan_reassoc_cos_strict(double %a) { | ||
; CHECK-LABEL: define double @fmul_reassoc_tan_reassoc_cos_strict( | ||
; CHECK-SAME: double [[A:%.*]]) { | ||
; CHECK-NEXT: [[RES:%.*]] = call reassoc double @llvm.sin.f64(double [[A]]) | ||
; CHECK-NEXT: ret double [[RES]] | ||
; | ||
%tan = call reassoc double @llvm.tan.f64(double %a) | ||
%cos = call double @llvm.cos.f64(double %a) | ||
%res = fmul reassoc double %tan, %cos | ||
ret double %res | ||
} | ||
|
||
define double @fmul_tan_cos_reassoc_multiple_uses(double %a) { | ||
; CHECK-LABEL: define double @fmul_tan_cos_reassoc_multiple_uses( | ||
; CHECK-SAME: double [[A:%.*]]) { | ||
; CHECK-NEXT: [[TAN:%.*]] = call reassoc double @llvm.tan.f64(double [[A]]) | ||
; CHECK-NEXT: [[COS:%.*]] = call reassoc double @llvm.cos.f64(double [[A]]) | ||
; CHECK-NEXT: [[RES:%.*]] = fmul reassoc double [[TAN]], [[COS]] | ||
; CHECK-NEXT: call void @use(double [[COS]]) | ||
; CHECK-NEXT: ret double [[RES]] | ||
; | ||
%tan = call reassoc double @llvm.tan.f64(double %a) | ||
%cos = call reassoc double @llvm.cos.f64(double %a) | ||
%res = fmul reassoc double %tan, %cos | ||
call void @use(double %cos) | ||
ret double %res | ||
} | ||
|
||
define double @fmul_tan_cos_reassoc(double %a) { | ||
; CHECK-LABEL: define double @fmul_tan_cos_reassoc( | ||
; CHECK-SAME: double [[A:%.*]]) { | ||
; CHECK-NEXT: [[RES:%.*]] = call reassoc double @llvm.sin.f64(double [[A]]) | ||
; CHECK-NEXT: ret double [[RES]] | ||
; | ||
%tan = call reassoc double @llvm.tan.f64(double %a) | ||
%cos = call reassoc double @llvm.cos.f64(double %a) | ||
%res = fmul reassoc double %tan, %cos | ||
ret double %res | ||
} | ||
|
||
define float @fmul_tanf_cosf_reassoc(float %a) { | ||
; CHECK-LABEL: define float @fmul_tanf_cosf_reassoc( | ||
; CHECK-SAME: float [[A:%.*]]) { | ||
; CHECK-NEXT: [[RES:%.*]] = call reassoc float @llvm.sin.f32(float [[A]]) | ||
; CHECK-NEXT: ret float [[RES]] | ||
; | ||
%tan = call reassoc float @llvm.tan.f32(float %a) | ||
%cos = call reassoc float @llvm.cos.f32(float %a) | ||
%res = fmul reassoc float %tan, %cos | ||
ret float %res | ||
} | ||
|
||
define fp128 @fmul_tanfp128_cosfp128_reassoc(fp128 %a) { | ||
; CHECK-LABEL: define fp128 @fmul_tanfp128_cosfp128_reassoc( | ||
; CHECK-SAME: fp128 [[A:%.*]]) { | ||
; CHECK-NEXT: [[RES:%.*]] = call reassoc fp128 @llvm.sin.f128(fp128 [[A]]) | ||
; CHECK-NEXT: ret fp128 [[RES]] | ||
; | ||
%tan = call reassoc fp128 @llvm.tan.fp128(fp128 %a) | ||
%cos = call reassoc fp128 @llvm.cos.fp128(fp128 %a) | ||
%res = fmul reassoc fp128 %tan, %cos | ||
ret fp128 %res | ||
} | ||
|
||
; commutativity | ||
define double @commutativity_cos_tan(double %a) { | ||
; CHECK-LABEL: define double @commutativity_cos_tan( | ||
; CHECK-SAME: double [[A:%.*]]) { | ||
; CHECK-NEXT: [[RES:%.*]] = call reassoc double @llvm.sin.f64(double [[A]]) | ||
; CHECK-NEXT: ret double [[RES]] | ||
; | ||
%cos = call reassoc double @llvm.cos.f64(double %a) | ||
%tan = call reassoc double @llvm.tan.f64(double %a) | ||
%res = fmul reassoc double %cos, %tan | ||
ret double %res | ||
} | ||
|
||
; negative test with mismatched value | ||
define double @tan_cos_value_mismatch(double %a, double %b) { | ||
; CHECK-LABEL: define double @tan_cos_value_mismatch( | ||
; CHECK-SAME: double [[A:%.*]], double [[B:%.*]]) { | ||
; CHECK-NEXT: [[TAN:%.*]] = call reassoc double @llvm.tan.f64(double [[A]]) | ||
; CHECK-NEXT: [[COS:%.*]] = call reassoc double @llvm.cos.f64(double [[B]]) | ||
; CHECK-NEXT: [[RES:%.*]] = fmul reassoc double [[TAN]], [[COS]] | ||
; CHECK-NEXT: ret double [[RES]] | ||
; | ||
%tan = call reassoc double @llvm.tan.f64(double %a) | ||
%cos = call reassoc double @llvm.cos.f64(double %b) | ||
%res = fmul reassoc double %tan, %cos | ||
ret double %res | ||
} | ||
|
||
declare void @use(double) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
reassoc/afn may be required, but I am not sure.
Uh oh!
There was an error while loading. Please reload this page.
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.
I think this would fall under contract, and I expect it to be precision improving
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.
Do I understand correctly the instruction should only be checked for the
contract
flag?The diff will be 9d090b2
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.
I don't like
contract
being used for this purpose. I thinkcontract
should be more predictable and tied to hardware behavior. Historically, we have usedreassoc
for algebraic transformations. If the user has enabled unsafe math optimizations broadly, this sort of optimization is what they'd expect, but if they've only enabledcontract
I don't think it is.Uh oh!
There was an error while loading. Please reload this page.
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.
This should have no relation to hardware behavior whatsoever. Hardware dependent semantics are simply not useful.
This is also inline with the proposal to fix the definition for the contract flag: https://discourse.llvm.org/t/rfc-fast-math-flags-semantics-contract/84478
contract should not be limited to the FMA case. If that were the intended only case to handle, it should not be a fast math flag. It needs broader applicability, and should cover cases that may increase precision
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.
It's how the contract flag is already interpreted, see https://discourse.llvm.org/t/rfc-fast-math-flags-semantics-contract/84478
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.
Are there other optimizations using it this way?
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.
In the backend we have some x / sqrt -> rsqrt contractions I wrote, I'd have to look at what other call optimizers are doing
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.
If we're going to interpret the flag this way, the LangRef should be updated. I guess we can just deal with the fallout in clang if users complain.
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.
Applied changes in 4715cb8
Is it resolved and PR could be merged?