Naga SPIR-V front end generates lousy code for out-of-scope expressions #6670
Labels
area: naga front-end
kind: refactor
Making existing function faster or nicer
lang: SPIR-V
Vulkan's Shading Language
naga
Shader Translator
The Naga SPIR-V front end generates unnecessary temporary variables and spills for SPIR-V values referenced outside of their natural Naga IR scope.
As explained in the documentation for
naga::front::spv::BlockContext
:Once we've generated a temporary variable and spilled the value to it, subsequent references to that expression have no need to introduce another temporary: they should just use the one that's already there. For example:
For the above SPIR-V, Naga generates the following WGSL:
While it is certainly necessary to spill
_e4
somewhere, so that the NagaExpression::Binary
Add
expressions following the loop can have something to refer to, it is certainly not necessary to introduce a separate temporary for every reference to_e4
. We don't need all oflocal
,local_1
, andlocal_2
; one would suffice.This can probably be fixed in
naga::front::spv::Frontend::get_expr_handle
, by having it record the temporary variables it generates somewhere easy to find - perhaps inLookupExpression
. Then subsequent calls could realize that we've already spilled the value, and re-use the temporary we introduced at the time. Only the call that actually introduced the variable would contribute an entry toBlockContext::phis
.For extra points, we could share
Load
expressions. But it's probably fine to have oneLoad
per use; that's what we do for ordinary variables anyway.The text was updated successfully, but these errors were encountered: