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

[naga msl-out] Use iteration limits to avoid unbounded loops #6546

Open
jimblandy opened this issue Nov 14, 2024 · 3 comments
Open

[naga msl-out] Use iteration limits to avoid unbounded loops #6546

jimblandy opened this issue Nov 14, 2024 · 3 comments
Labels
area: naga back-end Outputs of naga shader conversion area: performance How fast things go backend: metal Issues with Metal lang: Metal Metal Shading Language naga Shader Translator platform: macos Issues with integration with macos type: enhancement New feature or request

Comments

@jimblandy
Copy link
Member

Rather than injecting branches on volatile bools, Naga's backend for Metal Shading Language should avoid undefined behavior simply by imposing an iteration limit on every loop. Unlike the volatile bools, the optimizer will be able to reason about these limits and eliminate them when possible. Optimizations like unrolling are defeated by the volatile bool, but not by the iteration limit.

See a full description of the idea, with godbolt experiments, here: #6528 (comment)

@DemiMarie
Copy link

Will this cause problems due to slow 64-bit arithmetic? Is it possible for the GPU to actually run 232 loop iterations before the device is lost?

@teoxoy
Copy link
Member

teoxoy commented Nov 15, 2024

I was proposing to add this to the WGSL spec first to avoid UB in both the Metal compiler and DXC. DXC doesn't make the same assumptions as Metal but it does technically change the behavior of the program as written in WGSL #6528 (comment).

My main argument is that given WGSL allows loops to be infinite and that we target MSL & HLSL (both being based on C++), therefore need to prove to downstream compilers that loops are finite. Currently the only way to do that is with volatiles but that's expensive, if we add an upper bound on the nr of loop iterations we should be in a better position performance wise. Loops can even be unrolled as you found in #6528 (comment).

Is it possible for the GPU to actually run 232 loop iterations before the device is lost?

We could run some tests to see if this is the case. A more expensive way to make sure this happens is to add the mechanism ourselves but that's nontrivial. It might be enough to tell developers that loops have the upper bound and that they might hit it.

@DemiMarie
Copy link

A 264 - 1 limit allows assuming that the limit will never actually be reached, because the GPU cannot execute enough cycles to actually hit it in a human lifetime.

@cwfitzgerald cwfitzgerald added type: enhancement New feature or request area: performance How fast things go backend: metal Issues with Metal platform: macos Issues with integration with macos area: naga back-end Outputs of naga shader conversion naga Shader Translator lang: Metal Metal Shading Language labels Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: naga back-end Outputs of naga shader conversion area: performance How fast things go backend: metal Issues with Metal lang: Metal Metal Shading Language naga Shader Translator platform: macos Issues with integration with macos type: enhancement New feature or request
Projects
Status: Todo
Development

No branches or pull requests

4 participants