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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
#include "slang-ir-use-uninitialized-values.h"
#include "slang-ir-insts.h"
#include "slang-ir-reachability.h"
#include "slang-ir.h"
namespace Slang
{
static bool isMetaOp(IRInst* inst)
{
switch (inst->getOp())
{
// These instructions only look at the parameter's type,
// so passing an undefined value to them is permissible
case kIROp_IsBool:
case kIROp_IsInt:
case kIROp_IsUnsignedInt:
case kIROp_IsSignedInt:
case kIROp_IsHalf:
case kIROp_IsFloat:
case kIROp_IsVector:
case kIROp_GetNaturalStride:
case kIROp_TypeEquals:
return true;
default:
break;
}
return false;
}
static bool isUninitializedValue(IRInst* inst)
{
// Also consider var since it does not
// automatically mean it will be initialized
// (at least not as the user may have intended)
return (inst->m_op == kIROp_undefined)
|| (inst->m_op == kIROp_Var);
}
static bool isUndefinedParam(IRParam* param)
{
auto outType = as<IROutType>(param->getFullType());
if (!outType)
return false;
// Don't check `out Vertices<T>` or `out Indices<T>` parameters
// in mesh shaders.
// TODO: we should find a better way to represent these mesh shader
// parameters so they conform to the initialize before use convention.
// For example, we can use a `OutputVetices` and `OutputIndices` type
// to represent an output, like `OutputPatch` in domain shader.
// For now, we just skip the check for these parameters.
switch (outType->getValueType()->getOp())
{
case kIROp_VerticesType:
case kIROp_IndicesType:
case kIROp_PrimitivesType:
return false;
default:
break;
}
return true;
}
static bool isAliasable(IRInst* inst)
{
switch (inst->getOp())
{
// These instructions generate (implicit) references to inst
case kIROp_FieldExtract:
case kIROp_FieldAddress:
case kIROp_GetElement:
case kIROp_GetElementPtr:
return true;
default:
break;
}
return false;
}
static bool isDifferentiableFunc(IRInst* func)
{
for (auto decor = func->getFirstDecoration(); decor; decor = decor->getNextDecoration())
{
switch (decor->getOp())
{
case kIROp_ForwardDerivativeDecoration:
case kIROp_ForwardDifferentiableDecoration:
case kIROp_BackwardDerivativeDecoration:
case kIROp_BackwardDifferentiableDecoration:
case kIROp_UserDefinedBackwardDerivativeDecoration:
return true;
default:
break;
}
}
return false;
}
static IRInst* resolveSpecialization(IRSpecialize* spec)
{
IRInst* base = spec->getBase();
IRGeneric* generic = as<IRGeneric>(base);
return findInnerMostGenericReturnVal(generic);
}
// The `upper` field contains the struct that the type is
// is contained in. It is used to check for empty structs.
static bool canIgnoreType(IRType* type, IRType* upper)
{
// In case specialization returns a function instead
if (!type)
return true;
if (as<IRVoidType>(type))
return true;
// For structs, ignore if its empty
if (auto str = as<IRStructType>(type))
{
int count = 0;
for (auto field : str->getFields())
{
IRType* ftype = field->getFieldType();
count += !canIgnoreType(ftype, type);
}
return (count == 0);
}
// Nothing to initialize for a pure interface
if (as<IRInterfaceType>(type))
return true;
// For pointers, check the value type (primarily for globals)
if (auto ptr = as<IRPtrType>(type))
{
// Avoid the recursive step if its a
// recursive structure like a linked list
IRType* ptype = ptr->getValueType();
return (ptype != upper) && canIgnoreType(ptype, upper);
}
// In the case of specializations, check returned type
if (auto spec = as<IRSpecialize>(type))
{
IRInst* inner = resolveSpecialization(spec);
IRType* innerType = as<IRType>(inner);
return canIgnoreType(innerType, upper);
}
return false;
}
static List<IRInst*> getAliasableInstructions(IRInst* inst)
{
List<IRInst*> addresses;
addresses.add(inst);
for (auto use = inst->firstUse; use; use = use->nextUse)
{
IRInst* user = use->getUser();
// Meta instructions only use the argument type
if (isMetaOp(user) || !isAliasable(user))
continue;
addresses.addRange(getAliasableInstructions(user));
}
return addresses;
}
static void checkCallUsage(List<IRInst*>& stores, List<IRInst*>& loads, IRCall* call, IRInst* inst)
{
IRInst* callee = call->getCallee();
// Resolve the actual function
IRFunc* ftn = nullptr;
IRFuncType* ftype = nullptr;
if (auto spec = as<IRSpecialize>(callee))
ftn = as<IRFunc>(resolveSpecialization(spec));
else if (auto fwd = as<IRForwardDifferentiate>(callee))
ftn = as<IRFunc>(fwd->getBaseFn());
else if (auto rev = as<IRBackwardDifferentiate>(callee))
ftn = as<IRFunc>(rev->getBaseFn());
else if (auto wit = as<IRLookupWitnessMethod>(callee))
ftype = as<IRFuncType>(wit->getFullType());
else
ftn = as<IRFunc>(callee);
// Find the argument index so we can fetch the type
int index = 0;
auto args = call->getArgsList();
for (int i = 0; i < args.getCount(); i++)
{
if (args[i] == inst)
{
index = i;
break;
}
}
if (ftn)
ftype = as<IRFuncType>(ftn->getFullType());
if (!ftype)
return;
// Consider it as a store if its passed
// as an out/inout/ref parameter
IRType* type = ftype->getParamType(index);
if (as<IROutType>(type) || as<IRInOutType>(type) || as<IRRefType>(type))
stores.add(call);
else
loads.add(call);
}
static void collectLoadStore(List<IRInst*>& stores, List<IRInst*>& loads, IRInst* user, IRInst* inst)
{
// Meta intrinsics (which evaluate on type) do nothing
if (isMetaOp(user))
return;
// Ignore instructions generating more aliases
if (isAliasable(user))
return;
switch (user->getOp())
{
case kIROp_loop:
case kIROp_unconditionalBranch:
// TODO: Ignore branches for now
return;
case kIROp_Call:
// Function calls can be either
// stores or loads depending on
// whether the callee takes it
// in as a out parameter or not
return checkCallUsage(stores, loads, as<IRCall>(user), inst);
// These instructions will store data...
case kIROp_Store:
case kIROp_SwizzledStore:
case kIROp_SPIRVAsm:
case kIROp_GenericAsm:
// For now assume that __intrinsic_asm blocks will do the right thing...
stores.add(user);
break;
case kIROp_SPIRVAsmOperandInst:
// For SPIRV asm instructions, need to check out the entire
// block when doing reachability checks
stores.add(user->getParent());
break;
case kIROp_MakeExistential:
case kIROp_MakeExistentialWithRTTI:
// For specializing generic structs
stores.add(user);
break;
// Miscellaenous cases
case kIROp_ManagedPtrAttach:
stores.add(user);
break;
// ... and the rest will load/use them
default:
loads.add(user);
break;
}
}
static void cancelLoads(ReachabilityContext &reachability, const List<IRInst*>& stores, List<IRInst*>& loads)
{
// Remove all loads which are reachable from stores
for (auto store : stores)
{
for (Index i = 0; i < loads.getCount(); )
{
if (reachability.isInstReachable(store, loads[i]))
loads.fastRemoveAt(i);
else
i++;
}
}
}
static List<IRInst*> getUnresolvedParamLoads(ReachabilityContext &reachability, IRFunc* func, IRInst* inst)
{
// Collect all aliasable addresses
auto addresses = getAliasableInstructions(inst);
// Partition instructions
List<IRInst*> stores;
List<IRInst*> loads;
for (auto alias : addresses)
{
// TODO: Mark specific parts assigned to for partial initialization checks
for (auto use = alias->firstUse; use; use = use->nextUse)
{
IRInst* user = use->getUser();
collectLoadStore(stores, loads, user, alias);
}
}
// Only for out params we shall add all returns
for (const auto& b : func->getBlocks())
{
auto t = as<IRReturn>(b->getTerminator());
if (!t)
continue;
loads.add(t);
}
cancelLoads(reachability, stores, loads);
return loads;
}
static List<IRInst*> getUnresolvedVariableLoads(ReachabilityContext &reachability, IRInst* inst)
{
auto addresses = getAliasableInstructions(inst);
// Partition instructions
List<IRInst*> stores;
List<IRInst*> loads;
for (auto alias : addresses)
{
for (auto use = alias->firstUse; use; use = use->nextUse)
{
IRInst* user = use->getUser();
collectLoadStore(stores, loads, user, alias);
}
}
cancelLoads(reachability, stores, loads);
return loads;
}
static void checkUninitializedValues(IRFunc* func, DiagnosticSink* sink)
{
if (isDifferentiableFunc(func))
return;
auto firstBlock = func->getFirstBlock();
if (!firstBlock)
return;
ReachabilityContext reachability(func);
// Check out parameters
for (auto param : firstBlock->getParams())
{
if (!isUndefinedParam(param))
continue;
auto loads = getUnresolvedParamLoads(reachability, func, param);
for (auto load : loads)
{
sink->diagnose(load,
as <IRReturn> (load)
? Diagnostics::returningWithUninitializedOut
: Diagnostics::usingUninitializedOut,
param);
}
}
// Check ordinary instructions
for (auto inst = firstBlock->getFirstInst(); inst; inst = inst->getNextInst())
{
if (!isUninitializedValue(inst))
continue;
IRType* type = inst->getFullType();
if (canIgnoreType(type, nullptr))
continue;
auto loads = getUnresolvedVariableLoads(reachability, inst);
for (auto load : loads)
{
sink->diagnose(load,
Diagnostics::usingUninitializedVariable,
inst);
}
}
}
static void checkUninitializedGlobals(IRGlobalVar* variable, DiagnosticSink* sink)
{
IRType* type = variable->getFullType();
if (canIgnoreType(type, nullptr))
return;
// Check for semantic decorations
// (e.g. globals like gl_GlobalInvocationID)
if (variable->findDecoration<IRSemanticDecoration>())
return;
// Check for initialization blocks
for (auto inst : variable->getChildren())
{
if (as<IRBlock>(inst))
return;
}
auto addresses = getAliasableInstructions(variable);
List<IRInst*> stores;
List<IRInst*> loads;
for (auto alias : addresses)
{
for (auto use = alias->firstUse; use; use = use->nextUse)
{
IRInst* user = use->getUser();
collectLoadStore(stores, loads, user, alias);
// Disregard if there is at least one store,
// since we cannot tell what the control flow is
if (stores.getCount())
return;
// TODO: see if we can do better here (another kind of reachability check?)
}
}
for (auto load : loads)
{
sink->diagnose(load,
Diagnostics::usingUninitializedGlobalVariable,
variable);
}
}
void checkForUsingUninitializedValues(IRModule* module, DiagnosticSink* sink)
{
for (auto inst : module->getGlobalInsts())
{
if (auto func = as<IRFunc>(inst))
{
checkUninitializedValues(func, sink);
}
else if (auto generic = as<IRGeneric>(inst))
{
auto retVal = findGenericReturnVal(generic);
if (auto funcVal = as<IRFunc>(retVal))
checkUninitializedValues(funcVal, sink);
}
else if (auto global = as<IRGlobalVar>(inst))
{
checkUninitializedGlobals(global, sink);
}
}
}
}
|