-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add intrinsics for float arithmetic with fast
flag enabled
#32256
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
Related to #21690 |
(rust_highfive has picked a reviewer for you, use r? to override) |
cc @rust-lang/libs |
Sounds reasonable to me, and API-wise also seems fine for now! |
So IIUC this "fast" flag allows all transformation: No NaNs, no infinities, no signed zeros, reassociation, etc. C compilers typically expose more fine-grained control, allowing one to enable any subset of those assumptions (e.g., |
@rkruppe One question I have is if any of the flags except |
To answer my question, the |
Perhaps there should be a feature request against LLVM for more fine-grained control. |
The libs team discussed this during triage yesterday and the conclusion was that this seems good to merge. We liked the idea of having tagged types that are "fast math" as opposed to the C mode of globally turning it on/off for now. Note that this is also quite related to the checked arithmetic story! @bors: r+ 04d03a68ce1377b664441aaf164d052b00ee403e |
Thanks. It's a good step to put this into the unstable toolbox so we can find out where using these relaxed semantics helps and where it doesn't matter. |
⌛ Testing commit 04d03a6 with merge 0af284c... |
⛄ The build was interrupted to prioritize another pull request. |
⌛ Testing commit 04d03a6 with merge 379d18e... |
⛄ The build was interrupted to prioritize another pull request. |
⌛ Testing commit 04d03a6 with merge 493dbab... |
💔 Test failed - auto-win-msvc-32-opt |
`fast` a.k.a UnsafeAlgebra is the flag for enabling all "unsafe" (according to llvm) float optimizations. See LangRef for more information http://llvm.org/docs/LangRef.html#fast-math-flags Providing these operations with less precise associativity rules (for example) is useful to numerical applications. For example, the summation loop: let sum = 0.; for element in data { sum += *element; } Using the default floating point semantics, this loop expresses the floats must be added in a sequence, one after another. This constraint is usually completely unintended, and it means that no autovectorization is possible.
rebased @bors r=alexcrichton |
📌 Commit 2dbac1f has been approved by |
Add intrinsics for float arithmetic with `fast` flag enabled Add intrinsics for float arithmetic with `fast` flag enabled `fast` a.k.a UnsafeAlgebra is the flag for enabling all "unsafe" (according to llvm) float optimizations. See LangRef for more information http://llvm.org/docs/LangRef.html#fast-math-flags Providing these operations with less associativity rules (for example) is useful to numerical applications. For example, the summation loop: let sum = 0.; for element in data { sum += *element; } Using the default floating point semantics, this loop expresses that the floats must be added in a sequence, one after another. This constraint is usually completely unintended, and it means that no auto-vectorization is possible.
@@ -254,6 +263,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { | |||
} | |||
} | |||
|
|||
pub fn fsub_fast(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef { | |||
self.count_insn("sub"); |
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.
Should this be 'fsub'?
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.
That seems logical, this just repeats what fsub
did above, both should be fixed.
Does this mean that passing |
This PR just adds intrinsics that you need to use explicitly to get for example an "fadd" instruction with the "fast" flag enabled. For example, replace These are just intrinsics, so it's a small step towards exposing it as a stable feature in libcore/libstd eventually. |
Oh, flag. They are called flags http://llvm.org/docs/LangRef.html#fast-math-flags and it's a configuration put on the instruction itself, nothing about the command line interface. |
Add intrinsics for float arithmetic with
fast
flag enabledfast
a.k.a UnsafeAlgebra is the flag for enabling all "unsafe"(according to llvm) float optimizations.
See LangRef for more information http://llvm.org/docs/LangRef.html#fast-math-flags
Providing these operations with less associativity rules (for example)
is useful to numerical applications.
For example, the summation loop:
Using the default floating point semantics, this loop expresses that the
floats must be added in a sequence, one after another. This constraint
is usually completely unintended, and it means that no auto-vectorization
is possible.