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 hlsl-out] Improve workaround for infinite loops causing undefined behaviour #6929

Merged
merged 2 commits into from
Jan 31, 2025

Conversation

jamienicol
Copy link
Contributor

@jamienicol jamienicol commented Jan 16, 2025

Connections
Partially solves #6572 (only hlsl and msl)

Description

We must ensure that all loops emitted by the naga backends will terminate, in order to avoid undefined behaviour. This was previously implemented for the msl backend in #6545. However, the usage of volatile prevents the compiler from making other important optimizations. This patch improves the msl workaround and additionally implements it for hlsl. The spv implementation will be left for a follow up.

Rather than using volatile, this patch increments a counter on every loop iteration, breaking from the loop after 2^64 iterations. This ensures the compiler treats the loop as finite thereby avoiding undefined behaviour, whilst at the same time allowing for other optimizations and in reality not actually affecting execution.

Testing

Modified the hello_compute example to reproduce the undefined behaviour then ensured this patch avoids it.

Inspected metal and hlsl output in shaderplayground to ensure simple loop unrolling still works as expected

Checklist

  • Run cargo fmt.
  • Run taplo format.
  • Run cargo clippy. If applicable, add:
    • --target wasm32-unknown-unknown
    • --target wasm32-unknown-emscripten
  • Run cargo xtask test to run tests.
  • Add change to CHANGELOG.md. See simple instructions inside file.

@jamienicol jamienicol requested a review from a team January 16, 2025 14:35
@jamienicol jamienicol requested a review from a team as a code owner January 16, 2025 14:35
@cwfitzgerald
Copy link
Member

Hey @rudderbucky can you try this PR to see how the shader performance is with the workaround in place?

@rudderbucky
Copy link
Contributor

So it turns out I was able to find a couple workarounds to avoid the performance hit... I will give this a whirl around the weekend though, please remind me if I forget @cwfitzgerald

@Imberflur
Copy link
Contributor

The checks probably need to be moved to the beginning of the loop as mentioned in #6528 (comment)

@jamienicol
Copy link
Contributor Author

The checks probably need to be moved to the beginning of the loop as mentioned in #6528 (comment)

Ah of course. I'll update the patch

… undefined behaviour

We must ensure that all loops emitted by the naga backends will
terminate, in order to avoid undefined behaviour. This was previously
implemented for the msl backend in 6545. However, the usage of
`volatile` prevents the compiler from making other important
optimizations. This patch improves the msl workaround and additionally
implements it for hlsl. The spv implementation will be left for a
follow up.

Rather than using volatile, this patch increments a counter on every
loop iteration, breaking from the loop after 2^64 iterations. This
ensures the compiler treats the loop as finite thereby avoiding
undefined behaviour, whilst at the same time allowing for other
optimizations and in reality not actually affecting execution.
@jamienicol
Copy link
Contributor Author

The checks probably need to be moved to the beginning of the loop as mentioned in #6528 (comment)

I think this may have been the cause of the additional macos failures. At least with the checks at the top of the loop we no longer have any new failures on CI. So I have removed the patch that updated the test expectations

@jamienicol
Copy link
Contributor Author

@rudderbucky did you have a chance to test the performance with this PR?

Copy link
Member

@cwfitzgerald cwfitzgerald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on the form of the workaround

naga/tests/out/hlsl/boids.hlsl Show resolved Hide resolved
@rudderbucky
Copy link
Contributor

Hi... I tried setting my game up to hit this performance snag but haven't been able to get performance to significantly drop even after disabling a few performance improvements I made around the same time... maybe something else in wgpu 23 helped improve performance again... sorry about that.

Copy link
Member

@teoxoy teoxoy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@teoxoy teoxoy requested a review from cwfitzgerald January 30, 2025 08:46
@cwfitzgerald cwfitzgerald merged commit 4e7d892 into gfx-rs:trunk Jan 31, 2025
31 checks passed
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.

5 participants