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

Backends should not need two passes to handle wrapped functions #7106

Open
jimblandy opened this issue Feb 11, 2025 · 2 comments
Open

Backends should not need two passes to handle wrapped functions #7106

jimblandy opened this issue Feb 11, 2025 · 2 comments
Labels
area: naga back-end Outputs of naga shader conversion good first issue Good for newcomers kind: refactor Making existing function faster or nicer lang: HLSL D3D Shading Language lang: Metal Metal Shading Language lang: SPIR-V Vulkan's Shading Language naga Shader Translator

Comments

@jimblandy
Copy link
Member

jimblandy commented Feb 11, 2025

Once #7012 lands, Naga's Metal Shading Language, HLSL, and SPIR-V backends will make a prepass over each function's expressions to see which wrapped functions it will need, and then generate their definitions before the function's own translation. This means that both the prepass and the actual code generation need to repeat the logic for deciding which expressions need wrapped functions.

Instead, we should generate output for the Module's functions, and note which wrapped functions we needed by adding them to the backend's wrapped_functions table. Then, after all the Module's functions have been translated, we should simply iterate over the wrapped_functions table and generate the definitions we need.

Since MSL and HLSL require the wrapped function definitions to precede their uses, in those backends the two stages should write to separate Strings, which the backend should concatenate as a final step. Those copies should be pretty cheap. SPIR-V allows OpFunctionCall to refer to functions defined later, so in that case no reordering is necessary.

@jimblandy jimblandy added area: naga back-end Outputs of naga shader conversion kind: refactor Making existing function faster or nicer lang: Metal Metal Shading Language naga Shader Translator labels Feb 11, 2025
@jimblandy jimblandy added the good first issue Good for newcomers label Feb 11, 2025
@cwfitzgerald
Copy link
Member

Should hlsl also do this?

@jimblandy
Copy link
Member Author

jimblandy commented Feb 13, 2025

Yes, and in SPIR-V as well. In that case it's trivial, because SPIR-V allows function calls to be forward references.

Edited first comment.

@jimblandy jimblandy changed the title Metal backend should not need two passes to handle wrapped functions Backends should not need two passes to handle wrapped functions Feb 13, 2025
@jimblandy jimblandy added lang: SPIR-V Vulkan's Shading Language lang: HLSL D3D Shading Language labels Feb 13, 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 good first issue Good for newcomers kind: refactor Making existing function faster or nicer lang: HLSL D3D Shading Language lang: Metal Metal Shading Language lang: SPIR-V Vulkan's Shading Language naga Shader Translator
Projects
Status: Todo
Development

No branches or pull requests

2 participants