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
Workaround shader compiler bugs with degenerate switch statements #5654
Open
Imberflur
wants to merge
6
commits into
gfx-rs:trunk
Choose a base branch
from
Imberflur:degenerate-switches
base: trunk
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
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.
Conversation
This file contains 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
I guess this is one of the nested test cases I added. |
Imberflur
force-pushed
the
degenerate-switches
branch
from
May 3, 2024 06:14
4c594bc
to
d8a33e9
Compare
Imberflur
force-pushed
the
degenerate-switches
branch
from
May 4, 2024 07:01
d8a33e9
to
a90271e
Compare
Found minimal case #5658 and was able to avoid the issue here without affecting the aspects I was trying to test. |
Imberflur
force-pushed
the
degenerate-switches
branch
2 times, most recently
from
May 4, 2024 07:36
631ee78
to
4666d96
Compare
Imberflur
commented
May 4, 2024
Imberflur
force-pushed
the
degenerate-switches
branch
from
May 6, 2024 00:42
4666d96
to
9b9630d
Compare
cwfitzgerald
approved these changes
May 9, 2024
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.
Wgpu side looks amazing, minus a nit
to work around hlsl bug
Includes naga snapshot tests and a wgpu regression test
Imberflur
force-pushed
the
degenerate-switches
branch
from
May 13, 2024 02:53
9b9630d
to
ce64106
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Connections
Fixes #4514
Fixes #4485
Description
As discussed in #4485, FXC has a bug when handling switches with all cases leading to one body (it deletes the switch). It was also mentioned that some glsl consumers might not handle this scenario well either (but I don't know which ones that might be).
So we lower these to
do {} while(false);
loops in hlsl-out and glsl-out.While FXC already can't handle continue statements in switches (see #4485), I ended up including logic to properly forward continue statements to the outer loop here, since there would otherwise be regressions in glsl-out and maybe hlsl-out with DXC.
An alternative solution could be to switch on a known constant and have a dummy case with an empty body that is never hit. This is a bit less cautious in terms of avoiding potential compiler bugs since we are still switching on a constant. But the implementation would be simpler and the only issues I have observed so far with FXC are when all cases lead to a single body.
Testing
Some of these scenarios are already covered by existing naga snapshot tests, I extended
naga/tests/in/control-flow.wgsl
with a few more cases.I also included some regression tests for the linked issues that will execute the shaders and assert on their output. Additionally, a new mechanism is introduced into the test framework for forcing the usage of FXC when on the Dx12 backend.
I ran
cargo xtask test
on a windows machine and a separate linux machine.Checklist
cargo fmt
.cargo clippy
. If applicable, add:--target wasm32-unknown-unknown
--target wasm32-unknown-emscripten
cargo xtask test
to run tests.CHANGELOG.md
. See simple instructions inside file.