-
Notifications
You must be signed in to change notification settings - Fork 222
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
WGSL: Functions cannot be called at module scope #5607
Comments
@csyonghe I haven't looked into this yet, but from the name it sounds like Slang is emitting code where Is this something Slang does? WGSL has some support for something similar to "constexpr", but I think they have to be explicitly marked as such. |
I think this is supposed to be inlined and evaluated by slang so it never actually appears in the generated wgsl code. |
For some reason I cannot reproduce by the problematic code by just doing a stand-alone compilation:
This produces the following code:
I'll debug a bit to see why slang-test somehow comes up with different code, and what the complete generated code looks like. |
This is the code that actually gets used to create the WebGPU shader module.
|
I can reproduce with |
After some debugging based on the above, I have discovered that running The basic issue is that It seems to me that whatever code is hoisting this variable should trigger some kind of more targeted simplification to avoid illegal function calls at global scope level. I noticed that in HLSL we do end up with a global function call, though it's allowed: For GLSL, it seems the required simplification is running even with |
This call here may be difference between WGSL and GLSL in this case: At least, if I comment this out then GLSL generates code with similar problem (call expression at global scope). |
Running the same code for WGSL gets rid of the function call at global scope if I just have I believe slang-test is compiling in debug mode. Indeed if I add Debugging further, I found that WGSL emit logic works differently in these two cases: the call is deemed to possibly have side effects when I run via slang-test, and so I also noticed that -g1 causes GLSL to generate the call at global scope, so I don't expect to find a solution by looking at how GLSL handles this, because it seemingly doesn't! |
I assume it's correct that the call may have side effects: in debug mode any call could have the side effect of e.g. breaking into the debugger. |
The test passes if I comment out the
The function has kIROp_ReadNoneDecoration in slangc, which is why |
Yong pointed out yesterday that static const values get lowered to global constants right away when producing the initial IR, and also that the SPIR-V backend doesn't have this problem. The reason is that SPIR-V IR legalization contains some code that will inline such global constants. |
When I try to reuse the same kind of legalization as SPIR-V does (https://github.com/aleino-nv/slang/tree/aleino/wgpu), the reference to the
I tried adding the same kind of "legalization cleanup" that's done for SPIR-V, but the @csyonghe I guess I need some tweaks to the way that inlining is done in this case.. |
You will need to delete the global insts after they have been inlined. |
Sure, in one way or another... but this is now the same logic as for the SPIR-V legalization. |
The spirv emit logic will not emit anything not referenced by the EntryPoint. But wgsl backend will simply emit everything in the module so it doesn’t hurt to just remove the unused global for both backends. |
Ok, I added some code to remove any globals that are left unused after the inlining, and now the test is passing. |
Here is a PR #5768 |
There is an issue where after removing the inlined IRInst's we end up deleting an SPIRVAsm instruction for tests/spirv/global-compute.slang. The instruction is representing Then later on, for some reason spvtools ends up crashing (dereferencing the result type, which is nullptr) when optimizing this instruction. |
I think I have a proper fix for the issue in the above PR now -- it's now in review. |
This closes shader-slang#5607.
Found out in review that another fix is needed, so this required another attempt which I've just submitted, so the patch is in review again. |
This closes shader-slang#5607.
This closes shader-slang#5607.
This closes shader-slang#5607.
It turned out there was a problem with the existing inlining code, that only became apparent when I added my change to delete the inlined nodes. The inlining problem should now be solved, as well. |
Failing tests:
tests/language-feature/constants/static-const-in-generic-interface.slang
Example output:
The text was updated successfully, but these errors were encountered: