summaryrefslogtreecommitdiff
path: root/tests/diagnostics/uninitialized.slang
blob: 4779f45c94051e7d657274d34890edf1ef63f6a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//TEST:SIMPLE(filecheck=CHK): -target spirv

// TODO:
// * warn potentially uninitialized variables (control flow)
// * warn partially uninitialized variables (structs, arrays, etc.)
// * warn uninitialized fields in constructors

///////////////////////////////////
// Uninitialized local variables //
///////////////////////////////////

// Should not warn here (unconditionalBranch)
float3 unconditional(int mode)
{
    float f(float) { return 1; }

    float k0;
    float k1;

    if (mode == 1)
    {
        k1 = 1;
        k0 = 1;

        const float w = k1 * f(1);
        k0 = 4.0f * k0 * w;
        k1 = 2.0f * k1 * w;
    }

    return k0 + k1;
}

// Warn here for branches using the variables
int conditional()
{
    int k;
    //CHK-DAG: warning 41016: use of uninitialized variable 'k'
    return (k > 0);
}

// Using unitialized values
int use_undefined_value(int k)
{
    int x;
    x += k;
    //CHK-DAG: warning 41016: use of uninitialized variable 'x'
    return x;
}

// Returning uninitialized values
__generic<T>
T generic_undefined_return()
{
    T x;
    //CHK-DAG: warning 41016: use of uninitialized variable 'x'
    return x;
}

// Array variables
float undefined_array()
{
    float array[2];
    //CHK-DAG: warning 41016: use of uninitialized variable 'array'
    return array[0];
}

float filled_array(int mode)
{
    float array[2];
    array[0] = 1.0f;
    return array[0];
}

// Structs and nested structs
struct Data
{
    float value;
};

struct NestedData
{
    Data data;
};

// No warnings here, even thought autodiff generates
// IR which frequently returns undefined values
struct DiffStruct : IDifferentiable
{
    Data data;
    float x;
}

// Same story here
[ForwardDifferentiable]
DiffStruct differentiable(float x)
{
    DiffStruct ds;
    ds.x = x;
    return ds;
}

// Empty structures should not generate diagnostics
// for empty default constructors
struct EmptyStruct
{
    __init() {}
};

// No warnings for empty structs even without __init()
struct NonEmptyStruct
{
    int field;

    __init()
    {
        field = 1;
    }
};

// No warnings even when __init() is not specified
struct NoDefault
{
    int f(int i)
    {
        return i;
    }
};

// Constructing the above structs
int constructors()
{
    EmptyStruct empty;
    NoDefault no_default;
    return no_default.f(1);
}

// Using struct fields and nested structs
float structs()
{
    Data inputData = Data(1.0);

    float undefVar;
    Data undefData;
    NestedData nestedData;

    float result = inputData.value;

    //CHK-DAG: warning 41016: use of uninitialized variable 'undefVar'
    result += undefVar;

    //CHK-DAG: warning 41016: use of uninitialized variable 'undefData'
    result += undefData.value;

    //CHK-DAG: warning 41016: use of uninitialized variable 'nestedData'
    result += nestedData.data.value;

    return result;
}

////////////////////////////////////
// Uninitialized global variables //
////////////////////////////////////

// Using groupshared variables
groupshared float4 gsConstexpr = float4(1.0f);
groupshared float4 gsUndefined;

// OK
float use_constexpr_initialized_gs()
{
    return gsConstexpr.x;
}

float use_undefined_gs()
{
    //CHK-DAG: warning 41017: use of uninitialized global variable 'gsUndefined'
    return gsUndefined.x;
}

// Using static variables
static const float cexprInitialized = 1.0f;
static float writtenNever;
static float writtenLater;

// OK
float use_initialized_static()
{
    return cexprInitialized;
}

// Should detect this and treat it as a store
void write_to_later()
{
    writtenLater = 1.0f;
}

float use_never_written()
{
    //CHK-DAG: warning 41017: use of uninitialized global variable 'writtenNever'
    return writtenNever;
}

// OK because of prior store
float use_later_writte()
{
    return writtenLater;
}

//////////////////////////////////
// Uninitialized out parameters //
//////////////////////////////////

// Using before assigning
float regular_undefined_use(out float3 v)
{
    //CHK-DAG: warning 41015: use of uninitialized out parameter 'v'
    float r = v.x + 1.0;
    
    //CHK-DAG: warning 41018: returning without initializing out parameter 'v'
    return r;
}

// Returning before assigning
float returning_undefined_use(out float x)
{
    //CHK-DAG: warning 41018: returning without initializing out parameter 'x'
    return 0;
}

// Implicit, still returning before assigning
void implicit_undefined_use(out float x) 
{
    //CHK-DAG: warning 41018: returning without initializing out parameter 'x'
}

// Warn on potential return paths
void control_flow_undefined(bool b, out float y)
{
    if(b)
    {
        //CHK-DAG: warning 41018: returning without initializing out parameter 'y'
        return;
    }
    y = 0;
    return;
}

// No warnings if all paths are fine
void control_flow_defined(bool b, out float y)
{
    if(b)
    {
        unused(y);
        return;
    }
    y = 0;
    return;
}

//CHK-NOT: warning 41015
//CHK-NOT: warning 41016
//CHK-NOT: warning 41017
//CHK-NOT: warning 41018