-
Notifications
You must be signed in to change notification settings - Fork 1.2k
(Naga) Implement OpSpecConstantOp for the SPIR-V frontend #8308
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
base: trunk
Are you sure you want to change the base?
Conversation
4bf5d79 to
e857d2b
Compare
7f7910b to
958fc57
Compare
|
I added a second commit to the PR that removes a workaround and instead uses the same approach as the GLSL frontend, by properly using the This would enable us in a future PR to extend the |
|
Since no reviewer has assigned the PR to them, I took the liberty and added a third commit that polishes the implementation: Implicit type conversions are now made explicit, so that we can now properly convert OpSpecConstantOp created by GLSL, that contain implicit type conversion properly into stricter forms, so that WGSL can validate and evaluate the overrides correctly. We now also properly use the best available name for the override and also detect and optimize type conversion patterns that compiler like GLSL love to create. This improves debuggability of the produced shaders greatly. I will update the PR text accordingly. |
3adaa74 to
f1c1b50
Compare
|
Hi! It has been 4 weeks since I pushed this PR. Is there any way I could help get this PR faster through the pipeline? |
|
Just wanted to say, thanks for putting up this PR and thanks for your patience! This seems very valuable to have. It has been reassigned so hopefully it will get in soon. |
teoxoy
left a comment
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 implementation looks good overall but I have 2 main concerns:
- We now have 2 places where we have to handle the same operation type and make sure that their semantics get translated to our IR's semantics in the same way. Please try to refactor the code to make both code-paths use the same logic.
- About the
try_optimize_xfunctions; naga is not an optimizing compiler and we don't usually have such logic. Also, since these expressions will be processed by the constant evaluator they will get handled anyway.
| true, | ||
| ); | ||
| evaluator | ||
| .try_eval_and_append(expr, span) |
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.
Actually, is it worth trying to eval here? Since we are dealing with OpSpecX I would imagine the input to not contain fully const expressions.
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 tried skipping evaluation entirely and just appending expressions directly, but ran into validation failures because the expression kind tracking relies on the evaluator's logic to correctly propagate Override vs Const status through chained expressions. Because of this I couldn't get it to work without try_eval_and_append().
naga/src/front/spv/mod.rs
Outdated
| // Wrap regular constants in overrides to avoid creating Expression::Constant | ||
| // nodes, which would mark the entire expression tree as Const and fail |
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 whole expression tree shouldn't get marked as const. It should be ok to have override expressions contain const expressions. If that's not the case it's a bug. We use .to_expr() in existing code:
wgpu/naga/src/front/spv/mod.rs
Lines 5852 to 5855 in ed6b789
| let constant = self.lookup_constant.lookup(component_id)?; | |
| let expr = module | |
| .global_expressions | |
| .append(constant.inner.to_expr(), span); |
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.
This was fixed and removed in the "(Naga) Remove override workaround and properly track expression kind in the SPIR-V frontend" commit.
|
Thanks for the review! I removed the But I don't properly understand this point:
What do you mean by this? Currently we have two code paths |
|
@hasenbanck Can you resolve the conflicts and merge in the latest commits from trunk? The PR is quite old at this point and you will likely have to make a few tweaks. |
…in the SPIR-V frontend
We also now also try to select the best name for the override We also handle optimization of common conversion pattern to `As` instructions and with a proper, derived name. This makes the constants of the final shader of a GLSL compiled SPIR-V highly readable.
e8ced00 to
94eb412
Compare
Done. |
Connections
#4324
Description
Implement OpSpecConstantOp for the SPIR-V frontend.
This implementation solves all my problems that I had with SLANGC output.
But I couldn't implement all operations. The following are missing:
We also properly convert implicit type conversions to explicit type conversions, convert signed shifts to unsigned shifts and also optimize common type conversion patterns produced by the GLSL compiler or SLANG.
Testing
I added two snapshot tests. One based on GLSL code and one in pure SPVASM, that tests more complex chained OpSpecConstantOps.
Squash or Rebase?
Rebase
Checklist
cargo fmt.taplo format.cargo clippy --tests. If applicable, add:cargo xtask testto run tests.CHANGELOG.mdentry.