Skip to content
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

add fastmath for min and max #104

Merged
merged 2 commits into from
Jan 13, 2023
Merged

add fastmath for min and max #104

merged 2 commits into from
Jan 13, 2023

Conversation

KristofferC
Copy link
Collaborator

Should fix #103

@Seelengrab, can you test this?

@codecov-commenter
Copy link

codecov-commenter commented Jan 13, 2023

Codecov Report

Base: 88.57% // Head: 91.90% // Increases project coverage by +3.33% 🎉

Coverage data is based on head (2bbdaa0) compared to base (75e47c9).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #104      +/-   ##
==========================================
+ Coverage   88.57%   91.90%   +3.33%     
==========================================
  Files           5        5              
  Lines         525      531       +6     
==========================================
+ Hits          465      488      +23     
+ Misses         60       43      -17     
Impacted Files Coverage Δ
src/LLVM_intrinsics.jl 99.00% <100.00%> (+1.05%) ⬆️
src/simdvec.jl 79.12% <0.00%> (+2.19%) ⬆️
src/SIMD.jl 100.00% <0.00%> (+100.00%) ⬆️
src/precompile.jl 100.00% <0.00%> (+100.00%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@eschnett
Copy link
Owner

This is a somewhat "unreliable" implementation: It implements min in terms of other functions (<, vifelse), and then expects LLVM to find a good translation. A different approach would be to lower min to the respective LLVM intrinsic (fmin) and then to apply the respective flags (fastmath etc.).

@KristofferC
Copy link
Collaborator Author

Yes, I was lazy :P. The intrinsic is minnum:

.

We currently call that as a ccall which doesn't support passing any fastmath flags. I can rewrite it to LLVM IR and add the fast math flags and see how it goes.

@eschnett
Copy link
Owner

Don't let the better be the enemy of the good: If it works and if it improves the code, then thank you for your work. You could add a respective comment that what you suggest above is the way to go for the future, and that there is no particular reason to implement fastmath min in this way.

@KristofferC
Copy link
Collaborator Author

I think this approach should be better:

julia> mymin(a, b) = @fastmath min(a, b)
mymin (generic function with 1 method)

julia> v = SIMD.Vec(1.0,2.0,3.0,4.0)
<4 x Float64>[1.0, 2.0, 3.0, 4.0]

julia> @code_llvm mymin(v,v)
;  @ REPL[2]:1 within `mymin`
  %3 = getelementptr inbounds [1 x <4 x double>], [1 x <4 x double>]* %1, i64 0, i64 0
  %4 = getelementptr inbounds [1 x <4 x double>], [1 x <4 x double>]* %2, i64 0, i64 0
  %5 = load <4 x double>, <4 x double>* %3, align 16
  %6 = load <4 x double>, <4 x double>* %4, align 16
  %res.i = call fast <4 x double> @llvm.minnum.v4f64(<4 x double> %5, <4 x double> %6)
  %7 = getelementptr inbounds [1 x <4 x double>], [1 x <4 x double>]* %0, i64 0, i64 0
  store <4 x double> %res.i, <4 x double>* %7, align 32
  ret void
}

@Seelengrab
Copy link
Contributor

I can confirm that this PR works 👍 Thanks for the quick turnaround! The assembly is now exactly what I'd hope it to be.

@KristofferC KristofferC merged commit 5463df0 into master Jan 13, 2023
@KristofferC KristofferC deleted the kc/fast_min_max branch January 13, 2023 18:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support @fastmath min
4 participants