-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[SimplifyLibCalls] Merge sqrt into the power of exp #79146
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,7 +88,127 @@ define float @sqrt_call_fabs_f32(float %x) { | |
ret float %sqrt | ||
} | ||
|
||
define double @sqrt_exp(double %x) { | ||
; CHECK-LABEL: @sqrt_exp( | ||
; CHECK-NEXT: [[MERGED_SQRT:%.*]] = fmul reassoc double [[X:%.*]], 5.000000e-01 | ||
; CHECK-NEXT: [[E:%.*]] = call reassoc double @llvm.exp.f64(double [[MERGED_SQRT]]) | ||
; CHECK-NEXT: ret double [[E]] | ||
; | ||
%e = call reassoc double @llvm.exp.f64(double %x) | ||
%res = call reassoc double @llvm.sqrt.f64(double %e) | ||
ret double %res | ||
} | ||
|
||
define double @sqrt_exp_2(double %x) { | ||
; CHECK-LABEL: @sqrt_exp_2( | ||
; CHECK-NEXT: [[MERGED_SQRT:%.*]] = fmul reassoc double [[X:%.*]], 5.000000e-01 | ||
; CHECK-NEXT: [[E:%.*]] = call reassoc double @exp(double [[MERGED_SQRT]]) | ||
; CHECK-NEXT: ret double [[E]] | ||
; | ||
%e = call reassoc double @exp(double %x) | ||
%res = call reassoc double @sqrt(double %e) | ||
ret double %res | ||
} | ||
|
||
define double @sqrt_exp2(double %x) { | ||
; CHECK-LABEL: @sqrt_exp2( | ||
; CHECK-NEXT: [[MERGED_SQRT:%.*]] = fmul reassoc double [[X:%.*]], 5.000000e-01 | ||
; CHECK-NEXT: [[E:%.*]] = call reassoc double @exp2(double [[MERGED_SQRT]]) | ||
; CHECK-NEXT: ret double [[E]] | ||
; | ||
%e = call reassoc double @exp2(double %x) | ||
%res = call reassoc double @sqrt(double %e) | ||
ret double %res | ||
} | ||
|
||
define double @sqrt_exp10(double %x) { | ||
; CHECK-LABEL: @sqrt_exp10( | ||
; CHECK-NEXT: [[MERGED_SQRT:%.*]] = fmul reassoc double [[X:%.*]], 5.000000e-01 | ||
; CHECK-NEXT: [[E:%.*]] = call reassoc double @exp10(double [[MERGED_SQRT]]) | ||
; CHECK-NEXT: ret double [[E]] | ||
; | ||
%e = call reassoc double @exp10(double %x) | ||
%res = call reassoc double @sqrt(double %e) | ||
ret double %res | ||
} | ||
|
||
; Negative test | ||
define double @sqrt_exp_nofast_1(double %x) { | ||
; CHECK-LABEL: @sqrt_exp_nofast_1( | ||
; CHECK-NEXT: [[E:%.*]] = call double @llvm.exp.f64(double [[X:%.*]]) | ||
; CHECK-NEXT: [[RES:%.*]] = call reassoc double @llvm.sqrt.f64(double [[E]]) | ||
; CHECK-NEXT: ret double [[RES]] | ||
; | ||
%e = call double @llvm.exp.f64(double %x) | ||
%res = call reassoc double @llvm.sqrt.f64(double %e) | ||
ret double %res | ||
} | ||
|
||
; Negative test | ||
define double @sqrt_exp_nofast_2(double %x) { | ||
; CHECK-LABEL: @sqrt_exp_nofast_2( | ||
; CHECK-NEXT: [[E:%.*]] = call reassoc double @llvm.exp.f64(double [[X:%.*]]) | ||
; CHECK-NEXT: [[RES:%.*]] = call double @llvm.sqrt.f64(double [[E]]) | ||
; CHECK-NEXT: ret double [[RES]] | ||
; | ||
%e = call reassoc double @llvm.exp.f64(double %x) | ||
%res = call double @llvm.sqrt.f64(double %e) | ||
ret double %res | ||
} | ||
|
||
define double @sqrt_exp_merge_constant(double %x, double %y) { | ||
; CHECK-LABEL: @sqrt_exp_merge_constant( | ||
; CHECK-NEXT: [[MERGED_SQRT:%.*]] = fmul reassoc nsz double [[X:%.*]], 5.000000e+00 | ||
; CHECK-NEXT: [[E:%.*]] = call reassoc double @llvm.exp.f64(double [[MERGED_SQRT]]) | ||
; CHECK-NEXT: ret double [[E]] | ||
; | ||
%mul = fmul reassoc nsz double %x, 10.0 | ||
%e = call reassoc double @llvm.exp.f64(double %mul) | ||
%res = call reassoc nsz double @llvm.sqrt.f64(double %e) | ||
ret double %res | ||
} | ||
|
||
define double @sqrt_exp_intr_and_libcall(double %x) { | ||
; CHECK-LABEL: @sqrt_exp_intr_and_libcall( | ||
; CHECK-NEXT: [[MERGED_SQRT:%.*]] = fmul reassoc double [[X:%.*]], 5.000000e-01 | ||
; CHECK-NEXT: [[E:%.*]] = call reassoc double @exp(double [[MERGED_SQRT]]) | ||
; CHECK-NEXT: ret double [[E]] | ||
; | ||
%e = call reassoc double @exp(double %x) | ||
%res = call reassoc double @llvm.sqrt.f64(double %e) | ||
ret double %res | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should reduce test flags. Also, can you add the tests with libcall exp + intrinsic sqrt and intrinsic exp + libcall sqrt? We shouldn't introduce new libcalls from intrinsics There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've slightly simplified tests and added libcall + intrinsic tests. Fast-flags weren't modified. I'll adjust them when we decide on the correct set of flags that controls transformation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed fast flag to reassoc. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like the use of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jcranmer-intel, in your oppinion, what is the correct set of flags? Since you said that it is consistent with the existing practice, I won't change them in this PR. But we may start a discussion at discourse and systematically change all places. |
||
define double @sqrt_exp_intr_and_libcall_2(double %x) { | ||
; CHECK-LABEL: @sqrt_exp_intr_and_libcall_2( | ||
; CHECK-NEXT: [[MERGED_SQRT:%.*]] = fmul reassoc double [[X:%.*]], 5.000000e-01 | ||
; CHECK-NEXT: [[E:%.*]] = call reassoc double @llvm.exp.f64(double [[MERGED_SQRT]]) | ||
; CHECK-NEXT: ret double [[E]] | ||
; | ||
%e = call reassoc double @llvm.exp.f64(double %x) | ||
%res = call reassoc double @sqrt(double %e) | ||
ret double %res | ||
} | ||
|
||
define <2 x float> @sqrt_exp_vec(<2 x float> %x) { | ||
; CHECK-LABEL: @sqrt_exp_vec( | ||
; CHECK-NEXT: [[MERGED_SQRT:%.*]] = fmul reassoc <2 x float> [[X:%.*]], <float 5.000000e-01, float 5.000000e-01> | ||
; CHECK-NEXT: [[E:%.*]] = call reassoc <2 x float> @llvm.exp.v2f32(<2 x float> [[MERGED_SQRT]]) | ||
; CHECK-NEXT: ret <2 x float> [[E]] | ||
; | ||
%e = call reassoc <2 x float> @llvm.exp.v2f32(<2 x float> %x) | ||
%res = call reassoc <2 x float> @llvm.sqrt.v2f32(<2 x float> %e) | ||
ret <2 x float> %res | ||
} | ||
|
||
declare i32 @foo(double) | ||
declare double @sqrt(double) readnone | ||
declare float @sqrtf(float) | ||
declare float @llvm.fabs.f32(float) | ||
declare double @llvm.exp.f64(double) | ||
declare double @llvm.sqrt.f64(double) | ||
declare double @exp(double) | ||
declare double @exp2(double) | ||
declare double @exp10(double) | ||
declare <2 x float> @llvm.exp.v2f32(<2 x float>) | ||
declare <2 x float> @llvm.sqrt.v2f32(<2 x float>) |
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 we not have a better way of handling intrinsic-or-libcall?
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 couldn't find any other approaches to this. All code in this file seems to be written in the same way.