gfx-rs/wgpu

Backends should not need two passes to handle wrapped functions

Open

#7,106 opened on Feb 11, 2025

View on GitHub
 (3 comments) (0 reactions) (0 assignees)Rust (9,290 stars) (703 forks)batch import
area: naga back-endgood first issuekind: refactorlang: HLSLlang: Metallang: SPIR-Vnaga

Description

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.

Contributor guide