From e4611e2e30a3e5969d402f5ed7e72706a0e3b024 Mon Sep 17 00:00:00 2001 From: Yong He Date: Tue, 30 Sep 2025 19:08:23 -0700 Subject: Enhance buffer load specialization pass to specialize past field extracts. (#8547) This allows us to specialize functions whose argument is a sub element of a constant buffer, instead of being only applicable to entire buffer element. Closes #8421. This change also implements a proper heuristic to determine when to specialize the calls and defer the buffer loads. This PR addresses a pathological case exposed in `slangpy\slangpy\benchmarks\test_benchmark_tensor.py`, which used to take 27ms to finish, and now takes 1.25ms. For example, given: ``` struct Bottom { float bigArray[1024]; [mutating] void setVal(int index, float value) { bigArray[index] = value; } } struct Root { Bottom top[2]; [mutating] void setTopVal(int x, int y, float value) { top[x].setVal(y, value); } } RWStructuredBuffer sb; [shader("compute")] [numthreads(1, 1, 1)] void compute_main(uint3 tid: SV_DispatchThreadID) { sb[0].setTopVal(1, 2, 100.0f); } ``` We are now able to specialize the call to `setTopVal` into: ``` void compute_main(uint3 tid: SV_DispatchThreadID) { setTopVal_specialized(0, 1, 2, 100.0f); } void setTopVal_specialized(int sbIdx, int x, int y, float value) { Bottom_setVal_specialized(sbIdx, x, y, value); } void Bottom_setVal_specialized(int sbIdx, int x, int y, float value) { sb[sbIdx].top[x].bigArray[y] = value; } ``` And get rid of all unnecessary loads. Achieving this requires a combination of function call specialization and buffer-load-defer pass. The buffer-load-defer pass has been completely rewritten to be more correct and avoid introducing redundant loads. This PR also adds tests to make sure pointers, bindless handles, and loads from structured buffer or constant buffers works as expected. --- .../buffer-load-defer-user-pointer.slang | 63 ++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 tests/optimization/buffer-load-defer-user-pointer.slang (limited to 'tests/optimization/buffer-load-defer-user-pointer.slang') diff --git a/tests/optimization/buffer-load-defer-user-pointer.slang b/tests/optimization/buffer-load-defer-user-pointer.slang new file mode 100644 index 000000000..58e6386f9 --- /dev/null +++ b/tests/optimization/buffer-load-defer-user-pointer.slang @@ -0,0 +1,63 @@ +//TEST:SIMPLE(filecheck=SPV): -target spirv -O0 +//TEST:SIMPLE(filecheck=CUDA): -target cuda -entry compute_main -stage compute +//TEST:SIMPLE(filecheck=PTX): -target ptx -entry compute_main -stage compute + +// Check that we can specialize buffer loads through user pointers, and +// do not load big struct elements into registers unnecessarily. + +struct Bottom +{ + float bigArray[1024]; + float bottomGetValue(int index) { return bigArray[index]; } +} + +struct Middle +{ + Bottom bottom; + float middleGetValue(int index) { return bottom.bottomGetValue(index); } +} + +struct Top +{ + StructuredBuffer.Handle middle; + + // Calling `middleGetValue` on `middle[0]` should not causing the entire `Middle` + // struct to be loaded into registers. Instead, we should be able to specialize + // `middleGetValue` to take a `Middle*` and recursively specialize `bottomGetValue` + // to only load the `Bottom.bigArray[index]` element. + float topGetValue(int index) { return middle[0].middleGetValue(index); } +} + +struct Root +{ + Top top; +} + +ConstantBuffer cb; + +RWStructuredBuffer outputBuffer; + +// Check that the generated CUDA code never loads a `Middle` or `Bottom` struct into a local var. +// CUDA-NOT: Middle{{[_A-Za-z0-9]*}} {{[a-zA-Z0-9_]+}} = +// CUDA-NOT: Bottom{{[_A-Za-z0-9]*}} {{[a-zA-Z0-9_]+}} = +// CUDA-NOT: Top{{[_A-Za-z0-9]*}} {{[a-zA-Z0-9_]+}} = + +// Check that the generated CUDA code can be compiled by nvrtc correctly into PTX. +// PTX: compute_main + +// Check that the generated (unoptimized) SPIR-V contains a specialized Bottom_bottomGetValue function +// that takes in a Bottom* and use access chain to load the required array element directly, without +// needing to load the entire Bottom struct. +// SPV: %Bottom_bottomGetValue = OpFunction %float None +// SPV: OpFunctionParameter %_ptr_PhysicalStorageBuffer_Middle_natural +// SPV: %[[INDEX:[A-Za-z0-9_]+]] = OpFunctionParameter %int +// SPV: %[[PTR:[A-Za-z0-9_]+]] = OpAccessChain %_ptr_PhysicalStorageBuffer_float %{{.*}} %[[INDEX]] +// SPV: %[[VALUE:[A-Za-z0-9_]+]] = OpLoad %float %[[PTR]] +// SPV: OpReturnValue %[[VALUE]] + +[shader("compute")] +[numthreads(1, 1, 1)] +void compute_main(uint3 tid: SV_DispatchThreadID) +{ + outputBuffer[0] = cb.top.topGetValue(0); +} -- cgit v1.2.3