yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Yong HeEnhance buffer load specialization pass to specialize past field extracts. (#8547)e4611e2e3

master
2.2 KiB63 linesraw
1//TEST:SIMPLE(filecheck=SPV): -target spirv -O0
2//TEST:SIMPLE(filecheck=CUDA): -target cuda -entry compute_main -stage compute
3//TEST:SIMPLE(filecheck=PTX): -target ptx -entry compute_main -stage compute
4
5// Check that we can specialize buffer loads through user pointers, and
6// do not load big struct elements into registers unnecessarily.
7
8struct Bottom
9{
10    float bigArray[1024];
11    float bottomGetValue(int index) { return bigArray[index]; }
12}
13
14struct Middle
15{
16    Bottom bottom;
17    float middleGetValue(int index) { return bottom.bottomGetValue(index); }
18}
19
20struct Top
21{
22    StructuredBuffer<Middle*>.Handle middle;
23
24    // Calling `middleGetValue` on `middle[0]` should not causing the entire `Middle`
25    // struct to be loaded into registers. Instead, we should be able to specialize
26    // `middleGetValue` to take a `Middle*` and recursively specialize `bottomGetValue`
27    // to only load the `Bottom.bigArray[index]` element.
28    float topGetValue(int index) { return middle[0].middleGetValue(index); }
29}
30
31struct Root
32{
33    Top top;
34}
35
36ConstantBuffer<Root> cb;
37
38RWStructuredBuffer<float> outputBuffer;
39
40// Check that the generated CUDA code never loads a `Middle` or `Bottom` struct into a local var.
41// CUDA-NOT: Middle{{[_A-Za-z0-9]*}} {{[a-zA-Z0-9_]+}} =
42// CUDA-NOT: Bottom{{[_A-Za-z0-9]*}} {{[a-zA-Z0-9_]+}} =
43// CUDA-NOT: Top{{[_A-Za-z0-9]*}} {{[a-zA-Z0-9_]+}} =
44
45// Check that the generated CUDA code can be compiled by nvrtc correctly into PTX.
46// PTX: compute_main
47
48// Check that the generated (unoptimized) SPIR-V contains a specialized Bottom_bottomGetValue function
49// that takes in a Bottom* and use access chain to load the required array element directly, without 
50// needing to load the entire Bottom struct.
51// SPV: %Bottom_bottomGetValue = OpFunction %float None
52// SPV: OpFunctionParameter %_ptr_PhysicalStorageBuffer_Middle_natural
53// SPV: %[[INDEX:[A-Za-z0-9_]+]] = OpFunctionParameter %int
54// SPV: %[[PTR:[A-Za-z0-9_]+]] = OpAccessChain %_ptr_PhysicalStorageBuffer_float %{{.*}} %[[INDEX]]
55// SPV: %[[VALUE:[A-Za-z0-9_]+]] = OpLoad %float %[[PTR]]
56// SPV: OpReturnValue %[[VALUE]]
57
58[shader("compute")]
59[numthreads(1, 1, 1)]
60void compute_main(uint3 tid: SV_DispatchThreadID)
61{
62    outputBuffer[0] = cb.top.topGetValue(0);
63}