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/pipeline | |
| 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/pipeline')
| -rw-r--r-- | tests/pipeline/ray-tracing/trace-ray-inline.slang.glsl | 68 |
1 files changed, 27 insertions, 41 deletions
diff --git a/tests/pipeline/ray-tracing/trace-ray-inline.slang.glsl b/tests/pipeline/ray-tracing/trace-ray-inline.slang.glsl index fb94867a8..597236122 100644 --- a/tests/pipeline/ray-tracing/trace-ray-inline.slang.glsl +++ b/tests/pipeline/ray-tracing/trace-ray-inline.slang.glsl @@ -85,20 +85,23 @@ void myMiss_0(inout MyRayPayload_0 payload_4) layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in; void main() { - MyRayPayload_0 payload_5; MyProceduralHitAttrs_0 committedProceduralAttrs_0; MyProceduralHitAttrs_0 committedProceduralAttrs_1; - MyRayPayload_0 payload_6; MyProceduralHitAttrs_0 committedProceduralAttrs_2; - MyRayPayload_0 payload_7; MyProceduralHitAttrs_0 committedProceduralAttrs_3; + rayQueryEXT query_0; + + MyRayPayload_0 payload_5; MyRayPayload_0 _S2 = { -1 }; + payload_5 = _S2; + RayDesc_0 ray_1 = { C_0._data.origin_0, C_0._data.tMin_0, C_0._data.direction_0, C_0._data.tMax_0 }; RayQuery_TraceRayInline_0(query_0, myAccelerationStructure_0, C_0._data.rayFlags_0, C_0._data.instanceMask_0, ray_1); + MyProceduralHitAttrs_0 _S3; - payload_5 = _S2; committedProceduralAttrs_0 = _S3; + for(;;) { bool _S4 = rayQueryProceedEXT(query_0); @@ -111,23 +114,21 @@ void main() { case uint(1): { - MyProceduralHitAttrs_0 candidateProceduralAttrs_0 = { 0 }; - float _S6; - _S6 = 0.00000000000000000000; - MyProceduralHitAttrs_0 _S7; - _S7 = candidateProceduralAttrs_0; - bool _S8 = myProceduralIntersection_0(_S6, _S7); - float tHit_1 = _S6; - MyProceduralHitAttrs_0 candidateProceduralAttrs_1 = _S7; - if(_S8) + MyProceduralHitAttrs_0 candidateProceduralAttrs_0; + MyProceduralHitAttrs_0 _S6 = { 0 }; + candidateProceduralAttrs_0 = _S6; + + float tHit_1; + tHit_1 = 0.00000000000000000000; + + bool _S7 = myProceduralIntersection_0(tHit_1, candidateProceduralAttrs_0); + if(_S7) { - MyRayPayload_0 _S9; - _S9 = payload_5; - bool _S10 = myProceduralAnyHit_0(_S9); - MyRayPayload_0 _S11 = _S9; - if(_S10) + bool _S8 = myProceduralAnyHit_0(payload_5); + if(_S8) { rayQueryGenerateIntersectionEXT(query_0, tHit_1); + MyProceduralHitAttrs_0 _S9 = candidateProceduralAttrs_0; if(bool(C_0._data.shouldStopAtFirstHit_0)) { rayQueryTerminateEXT(query_0); @@ -135,31 +136,25 @@ void main() else { } - committedProceduralAttrs_1 = candidateProceduralAttrs_1; + committedProceduralAttrs_1 = _S9; } else { committedProceduralAttrs_1 = committedProceduralAttrs_0; } - payload_6 = _S11; committedProceduralAttrs_2 = committedProceduralAttrs_1; } else { - payload_6 = payload_5; committedProceduralAttrs_2 = committedProceduralAttrs_0; } - payload_7 = payload_6; committedProceduralAttrs_3 = committedProceduralAttrs_2; break; } case uint(0): { - MyRayPayload_0 _S12; - _S12 = payload_5; - bool _S13 = myTriangleAnyHit_0(_S12); - MyRayPayload_0 _S14 = _S12; - if(_S13) + bool _S10 = myTriangleAnyHit_0(payload_5); + if(_S10) { rayQueryConfirmIntersectionEXT(query_0); if(bool(C_0._data.shouldStopAtFirstHit_0)) @@ -173,42 +168,33 @@ void main() else { } - payload_7 = _S14; committedProceduralAttrs_3 = committedProceduralAttrs_0; break; } default: { - payload_7 = payload_5; committedProceduralAttrs_3 = committedProceduralAttrs_0; break; } } - payload_5 = payload_7; committedProceduralAttrs_0 = committedProceduralAttrs_3; } - uint _S15 = (rayQueryGetIntersectionTypeEXT((query_0), true)); - switch(_S15) + uint _S11 = (rayQueryGetIntersectionTypeEXT((query_0), true)); + switch(_S11) { case uint(1): { - MyRayPayload_0 _S16; - _S16 = payload_5; - myTriangleClosestHit_0(_S16); + myTriangleClosestHit_0(payload_5); break; } case uint(2): { - MyRayPayload_0 _S17; - _S17 = payload_5; - myProceduralClosestHit_0(_S17, committedProceduralAttrs_0); + myProceduralClosestHit_0(payload_5, committedProceduralAttrs_0); break; } case uint(0): { - MyRayPayload_0 _S18; - _S18 = payload_5; - myMiss_0(_S18); + myMiss_0(payload_5); break; } default: |
