yum-mirror/slang

Making it easier to work with shaders

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

CopilotAdd bounds checking for out-of-bounds array access with constant indices (#7814)85edfb178

master
1.1 KiB35 linesraw
1//TEST:SIMPLE(filecheck=SPV): -target spirv
2//TEST:SIMPLE(filecheck=HLSL): -target hlsl -profile cs_6_0 -entry computeMain
3
4struct MyData
5{
6    int a[0][0][0];
7}
8
9uniform MyData* myData;
10uniform int * output;
11
12[numthreads(1, 1, 1)]
13void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
14{
15    // These are all ill-formed, but we want to still ensure our backend
16    // can handle them gracefully without crashing.
17    // In actual user code, any access to 0-sized arrays should be protected
18    // by a `if` statement that checks the size before accessing.
19    // The condition would then evaluate to false and causing all the accessing
20    // code to be optimized out.
21    
22    // Use runtime values to access the 0-sized array to avoid triggering
23    // the new out-of-bounds diagnostic for constant indices
24    uint runtimeIndex = dispatchThreadID.x;
25    InterlockedAdd(myData.a[runtimeIndex][runtimeIndex][runtimeIndex], 1);
26    myData.a[runtimeIndex][runtimeIndex][runtimeIndex] += 1;
27    output[0] = myData.a[runtimeIndex][runtimeIndex][runtimeIndex];
28}
29
30//SPV: OpEntryPoint
31//SPV-NOT: OpAtomic
32//SPV-NOT: OpStore
33//SPV-NOT: OpLoad
34
35//HLSL: computeMain