yum-mirror/slang

Making it easier to work with shaders

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

Jay KwakFix incorrect resolve of specialization instance (#6162)a9ce7520e

master
3.2 KiB185 linesraw
1//TEST:SIMPLE(filecheck=CHK): -target spirv -entry computeMain
2
3float f(float) { return 1; }
4
5// Should not warn here (unconditionalBranch)
6float3 unconditional(int mode)
7{
8    float k0;
9    float k1;
10
11    if (mode == 1)
12    {
13        k1 = 1;
14        k0 = 1;
15
16        const float w = k1 * f(1);
17        k0 = 4.0f * k0 * w;
18        k1 = 2.0f * k1 * w;
19    }
20
21    return k0 + k1;
22}
23
24// Warn here for branches using the variables
25int conditional()
26{
27    int k;
28    //CHK-DAG: warning 41016: use of uninitialized variable 'k'
29    return (k > 0);
30}
31
32// Using unitialized values
33int use_undefined_value(int k)
34{
35    int x;
36    x += k;
37    //CHK-DAG: warning 41016: use of uninitialized variable 'x'
38    return x;
39}
40
41// We don't know the exact type of T yet.
42// T may not have any members, and it may not need any initialization.
43__generic<T>
44T generic_undefined_return()
45{
46    T y;
47    //CHK-NOT: warning 41016: use of uninitialized variable 'y'
48    return y;
49}
50
51// Array variables
52float undefined_array()
53{
54    float array[2];
55    //CHK-DAG: warning 41016: use of uninitialized variable 'array'
56    return array[0];
57}
58
59float filled_array(int mode)
60{
61    float array[2];
62    array[0] = 1.0f;
63    return array[0];
64}
65
66// Structs and nested structs
67struct Data
68{
69    float value;
70};
71
72struct NestedData
73{
74    Data data;
75};
76
77// No warnings here, even thought autodiff generates
78// IR which frequently returns undefined values
79struct DiffStruct : IDifferentiable
80{
81    Data data;
82    float x;
83}
84
85// Same story here
86[ForwardDifferentiable]
87DiffStruct differentiable(float x)
88{
89    DiffStruct ds;
90    ds.x = x;
91    return ds;
92}
93
94// Empty structures should not generate diagnostics
95// for empty default constructors
96struct EmptyStruct
97{
98    __init() {}
99};
100
101// No warnings for empty structs even without __init()
102struct NonEmptyStruct
103{
104    int field;
105
106    __init()
107    {
108        field = 1;
109    }
110};
111
112// No warnings even when __init() is not specified
113struct NoDefault
114{
115    int f(int i)
116    {
117        return i;
118    }
119};
120
121// Constructing the above structs
122int constructors()
123{
124    EmptyStruct empty;
125    NoDefault no_default;
126    return no_default.f(1);
127}
128
129// Using struct fields and nested structs
130float structs()
131{
132    Data inputData = Data(1.0);
133
134    float undefVar;
135    Data undefData;
136    NestedData nestedData;
137
138    float result = inputData.value;
139
140    //CHK-DAG: warning 41016: use of uninitialized variable 'undefVar'
141    result += undefVar;
142
143    //CHK-DAG: warning 41016: use of uninitialized variable 'undefData'
144    result += undefData.value;
145
146    //CHK-DAG: warning 41016: use of uninitialized variable 'nestedData'
147    result += nestedData.data.value;
148
149    return result;
150}
151
152// Warnings even in nested scopes
153float nested_scopes(int x, inout float p)
154{
155    if (x == 0)
156    {
157        float y;
158        //CHK-DAG: warning 41016: use of uninitialized variable 'y'
159        return y;
160    }
161    else if (x == 1)
162    {
163        float y;
164        //CHK-DAG: warning 41016: use of uninitialized variable 'y'
165        p = y + 1;
166
167        if (x == 2)
168        {
169            float z;
170            //CHK-DAG: warning 41016: use of uninitialized variable 'z'
171            p += z;
172        }
173    }
174
175    return 1.0;
176}
177
178//CHK-NOT: warning 41016
179
180[Shader("compute")]
181[NumThreads(4, 1, 1)]
182void computeMain(int3 dispatchThreadID : SV_DispatchThreadID)
183{
184}
185