-
Notifications
You must be signed in to change notification settings - Fork 89
Optimize num_traits::Float::powi to use GLSL.std.450 Pow #518
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
LegNeato
wants to merge
1
commit into
Rust-GPU:main
Choose a base branch
from
LegNeato:powi
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.
+43
−0
Open
Changes from all commits
Commits
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
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,13 @@ | ||
| // Test that `Float::powi` uses GLSL.std.450 Pow instead of a loop-based implementation. | ||
| // See https://github.com/Rust-GPU/rust-gpu/issues/516 | ||
|
|
||
| // build-pass | ||
| // compile-flags: -C llvm-args=--disassemble-entry=main | ||
|
|
||
| use spirv_std::num_traits::Float; | ||
| use spirv_std::spirv; | ||
|
|
||
| #[spirv(fragment)] | ||
| pub fn main(input: f32, output: &mut f32) { | ||
| *output = input.powi(2); | ||
| } |
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,12 @@ | ||
| %1 = OpFunction %2 None %3 | ||
| %4 = OpLabel | ||
| OpLine %5 11 12 | ||
| %6 = OpLoad %7 %8 | ||
| OpLine %5 12 20 | ||
| %9 = OpConvertSToF %7 %10 | ||
| %11 = OpExtInst %7 %12 26 %6 %9 | ||
| OpLine %5 12 4 | ||
| OpStore %13 %11 | ||
| OpNoLine | ||
| OpReturn | ||
| OpFunctionEnd |
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.
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 have no idea how things work more generally, but why are things like this even necessary? I'd expect the generic machinery of the compiler to optimize it to some canonical form that the target would like the most.
So it shouldn't make any difference if one writes
x.powi(2)orx * xor uses a few layers of zero-cost abstractions in the process, should all result in identical SPIR-V, just like it does with LLVM. What am I missing?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.
The problem is we are using
num_traitsto get the APIs we need off primitives, andnum_traitsknows nothing about the intrinsics available on the platform. It is using a loop as impl https://github.com/rust-num/num-traits/blob/master/src/pow.rs#L179. Pretty much no compiler would be able to tell that code can be replaced with a cast and apowi, andrustcdoesn't. Instead somewhere needs to map the call / the semantics to the underlying fast impl, which is what the backend is supposed to do. It already does it for many ops, we just missed a spot (I guess? This all predates me).One can argue we shouldn't use
num_traitsand instead should just hang our own impls off the types, I have no clue why it was chosen vs that route. Probably a make it work first then make it fast tradeoff (as thenum_traitsimpl does work on spirv).I have a new optimizer where we can control things much more rather than relying on
spirv-opt, but this replacement happens before the optimizer.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.
Ouch, that definitely looks like a problem. Maybe worth creating an issue to look into getting rid of
num_traits? Doing things like looping instead of native exponentiation is definitely going to hurt and there might be more examples like it.