diff options
| author | Theresa Foley <tfoleyNV@users.noreply.github.com> | 2021-07-21 12:52:08 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-21 12:52:08 -0700 |
| commit | 23d406f8a3b325f91fecd9ad52bd510ded5f49a7 (patch) | |
| tree | 54d770593e38fcc5e60b9d6188f0a14641e7b002 /tests/vkray/intersection.slang.glsl | |
| parent | e57ea944c4aba0cf385f0f3db6b6ddc7760b8ffa (diff) | |
Work to mitigate SPIR-V bloat (#1914)
* Work to mitigate SPIR-V bloat
SPIR-V is not an especially compact format, but some patterns in how Slang generates code and then runs it through `spirv-opt` lead to many redundant field-by-field copy operations being emitted. This change attempts to address some of the resulting bloat from the Slang side of things.
Note: experimentation shows that the bloat is less pronounced when running either *no* SPIR-V optimizations or *full* SPIR-V optimizations, so it is also likely that the bloat should be addressed by changing which `spirv-opt` passes the Slang compiler runs in default (`-O1`) builds. Such changes should come as a distinct pull request.
This change primarily does two things:
First, the code generation strategy for passing arguments to `out` and `inout` parameters has been changed. In the past, the compiler would *always* copy the argument value into a temporary, then pass the address of the temporary, and then write back the value after the call. The new code generation strategy attempts to identify when an argument value already has a simple address in memory and passes that address directly when possible. This eliminates many copy operations that occur before/after calls to functions with `out`/`inout` parameters.
Second, we introduce an IR optimization pass that detects call sites where the entire contents of a buffer (usually a constant buffer) is being passed to a callee function, such that many bytes are loaded and then passed even if only very few are used in the callee. The pass moves the load operations from the caller to a specialized version of the the callee where possible (e.g., when the constant buffer in question is a global shader parameter). Doing this eliminates another major category of copies.
Notes:
* The IR lowering logic is complicated by the fact that several kinds of l-values (values that are usable as the desitnation of assignment, or for `out`/`inout` arguments) are not actually addressable. An easy example is a non-contiguous swizzle like `v.xwz` on a `float4`, where the value occupies 12 bytes, but not 12 consecutive bytes with a single address. There are many more corner cases like that and the IR lowering pass carries a lot of complexity to deal with them. A more systematic overhaul is due some time soon.
* The IR representation of `out` and `inout` parameters deserves some careful scrutiny when making these kinds of changes. The official semantics of `inout` in HLSL has been "copy in copy out" (and `out` is just "copy out") which is observably different from any solution that passes in the address of an l-value directly. By making this change we are saying that Slang's semantics are not precisely those of legacy HLSL, and that our semantics for `inout` parameters are closer to those of `inout` in Swift or of a mutable borrow in Rust. In the Swift case the implementation can freely pass the underlying storage of an l-value or the address of a temporary, and valid programs may not observe the different. It is thus illegal to observe the value in a storage local while a mutation to that location is "in flight." All of this is way more detailed and technical than 99% of Slang users will ever care about, but importantly it gives us semantic cover to eliminate these copies in the IR, and also to emit output C++ code that implements `out` and `inout` as by-reference parameter passing.
* There was an exsting generic pass for specializing functions based on call sites that uses a "template method" style of pattern to customize its behavior. That pass needed to be generalized to handle this use case because it had previously operated on the assumption that the "desire" to specialize a callee function must be driven by the parameter declarations of that function, and not on the argument values passed in. The code has been slightly refactored to allow the policy for specialization to consider both parameters and arguments.
* Unsurprisingly, a bunch of the GLSL (and thus SPIR-V) generated has changed with this work, so several baseline `.slang.glsl` files needed to be updated.
* This change is incomplete in that it does not address broader cases of buffer loads, including both partial loads from constant buffers (just loading one field, but a field that uses a "large" structure type), and loads from multi-element buffers (a lot from a structured buffer where the element type is "large"). The main question in each of those cases is how to define how "large" a structure needs to be before we decide to try and sink loads into callee functions like this. In the worst case, sinking loads in this way may actually create *more* memory traffic (because the same values get loaded in multiple callee functions).
* fixup: run premake
* fixup: typo
Diffstat (limited to 'tests/vkray/intersection.slang.glsl')
| -rw-r--r-- | tests/vkray/intersection.slang.glsl | 72 |
1 files changed, 24 insertions, 48 deletions
diff --git a/tests/vkray/intersection.slang.glsl b/tests/vkray/intersection.slang.glsl index 66846d993..ac95432dd 100644 --- a/tests/vkray/intersection.slang.glsl +++ b/tests/vkray/intersection.slang.glsl @@ -1,19 +1,8 @@ //TEST_IGNORE_FILE: #version 460 - #extension GL_NV_ray_tracing : require - -#define tmp_ubo _S1 -#define tmp_reportHit _S2 -#define tmp_origin _S3 -#define tmp_direction _S4 -#define tmp_tmin _S5 -#define tmp_tmax _S6 -#define tmp_thit _S7 -#define tmp_hitattrs _S8 -#define tmp_dithit _S9 -#define tmp_reportresult _S10 - +layout(row_major) uniform; +layout(row_major) buffer; struct Sphere_0 { vec3 position_0; @@ -26,12 +15,10 @@ struct SLANG_ParameterGroup_U_0 }; layout(binding = 0) -layout(std140) -uniform tmp_ubo +layout(std140) uniform _S1 { SLANG_ParameterGroup_U_0 _data; } U_0; - struct RayDesc_0 { vec3 Origin_0; @@ -45,54 +32,43 @@ struct SphereHitAttributes_0 vec3 normal_0; }; -bool rayIntersectsSphere_0( - RayDesc_0 ray_0, - Sphere_0 sphere_0, - out float tHit_0, - out SphereHitAttributes_0 attrs_0) +bool rayIntersectsSphere_0(RayDesc_0 ray_0, Sphere_0 sphere_0, out float tHit_0, out SphereHitAttributes_0 attrs_0) { tHit_0 = sphere_0.radius_0; attrs_0.normal_0 = sphere_0.position_0; return tHit_0 >= ray_0.TMin_0; } -hitAttributeNV SphereHitAttributes_0 a_0; +hitAttributeNV +SphereHitAttributes_0 a_0; bool ReportHit_0(float tHit_1, uint hitKind_0, SphereHitAttributes_0 attributes_0) { a_0 = attributes_0; - bool tmp_reportHit = reportIntersectionNV(tHit_1, hitKind_0); - return tmp_reportHit; + bool _S2 = reportIntersectionNV(tHit_1, hitKind_0); + return _S2; } void main() { RayDesc_0 ray_1; - - vec3 tmp_origin = gl_ObjectRayOriginNV; - ray_1.Origin_0 = tmp_origin; - - vec3 tmp_direction = gl_ObjectRayDirectionNV; - ray_1.Direction_0 = tmp_direction; - - float tmp_tmin = gl_RayTminNV; - ray_1.TMin_0 = tmp_tmin; - - float tmp_tmax = gl_RayTmaxNV; - ray_1.TMax_0 = tmp_tmax; - - float tmp_thit; - SphereHitAttributes_0 tmp_hitattrs; - bool tmp_dithit = rayIntersectsSphere_0(ray_1, U_0._data.gSphere_0, tmp_thit, tmp_hitattrs); - - float tHit_2 = tmp_thit; - SphereHitAttributes_0 attrs_1 = tmp_hitattrs; - - if(tmp_dithit) + vec3 _S3 = ((gl_ObjectRayOriginNV)); + ray_1.Origin_0 = _S3; + vec3 _S4 = ((gl_ObjectRayDirectionNV)); + ray_1.Direction_0 = _S4; + float _S5 = ((gl_RayTminNV)); + ray_1.TMin_0 = _S5; + float _S6 = ((gl_RayTmaxNV)); + ray_1.TMax_0 = _S6; + float tHit_2; + SphereHitAttributes_0 attrs_1; + bool _S7 = rayIntersectsSphere_0(ray_1, U_0._data.gSphere_0, tHit_2, attrs_1); + if(_S7) + { + bool _S8 = ReportHit_0(tHit_2, uint(0), attrs_1); + } + else { - bool tmp_reportresult = ReportHit_0(tHit_2, (uint((0))), attrs_1); } - return; } - |
