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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
|
#include "slang-ir-check-differentiability.h"
#include "slang-ir-autodiff.h"
#include "slang-ir-inst-pass-base.h"
namespace Slang
{
struct CheckDifferentiabilityPassContext : public InstPassBase
{
public:
DiagnosticSink* sink;
AutoDiffSharedContext sharedContext;
enum DifferentiableLevel
{
Forward,
Backward
};
Dictionary<IRInst*, DifferentiableLevel> differentiableFunctions;
CheckDifferentiabilityPassContext(IRModule* inModule, DiagnosticSink* inSink)
: InstPassBase(inModule), sink(inSink), sharedContext(nullptr, inModule->getModuleInst())
{
}
bool _isFuncMarkedForAutoDiff(IRInst* func)
{
func = getResolvedInstForDecorations(func);
if (!func)
return false;
for (auto decorations : func->getDecorations())
{
switch (decorations->getOp())
{
case kIROp_ForwardDifferentiableDecoration:
case kIROp_BackwardDifferentiableDecoration:
return true;
}
}
return false;
}
bool _isDifferentiableFuncImpl(IRInst* func, DifferentiableLevel level)
{
func = getResolvedInstForDecorations(func);
if (!func)
return false;
if (auto substDecor = func->findDecoration<IRPrimalSubstituteDecoration>())
{
func = getResolvedInstForDecorations(substDecor->getPrimalSubstituteFunc());
if (!func)
return false;
}
for (auto decorations : func->getDecorations())
{
switch (decorations->getOp())
{
case kIROp_ForwardDerivativeDecoration:
case kIROp_ForwardDifferentiableDecoration:
if (level == DifferentiableLevel::Forward)
return true;
break;
case kIROp_UserDefinedBackwardDerivativeDecoration:
case kIROp_BackwardDerivativeDecoration:
case kIROp_BackwardDifferentiableDecoration:
return true;
default:
break;
}
}
return false;
}
bool shouldTreatCallAsDifferentiable(IRInst* callInst)
{
SLANG_ASSERT(as<IRCall>(callInst));
return (
callInst->findDecoration<IRTreatCallAsDifferentiableDecoration>() ||
callInst->findDecoration<IRDifferentiableCallDecoration>());
}
// If a function call takes all literals as arguments, it will implies that this function will
// not be expected to any gradients, in this case, this call should be treated as no_diff even
// there is no 'no_diff' decorated on it explicitly. In the actual check, we only need to check
// the argument corresponding to the differentiable parameters, because non-differentiable
// parameter are not expected to produce any gradients anyway.
bool shouldCallImpliesNoDiff(
DifferentiableTypeConformanceContext& diffTypeContext,
IRCall* callInst)
{
if (shouldTreatCallAsDifferentiable(callInst))
{
return true;
}
auto calleeFuncType = as<IRFuncType>(callInst->getCallee()->getFullType());
if (!calleeFuncType)
return false;
SLANG_RELEASE_ASSERT(calleeFuncType->getParamCount() == callInst->getArgCount());
bool doesImplyNoDiff = true;
UInt paramIndex = 0;
for (auto paramType : calleeFuncType->getParamTypes())
{
if (isDifferentiableType(diffTypeContext, paramType))
{
auto arg = callInst->getArg(paramIndex);
if (!as<IRConstant>(arg))
{
doesImplyNoDiff = false;
}
}
paramIndex++;
}
if (doesImplyNoDiff)
{
IRBuilder irBuilder(callInst->getModule());
irBuilder.addDecoration(callInst, kIROp_TreatCallAsDifferentiableDecoration);
}
return doesImplyNoDiff;
}
bool isDifferentiableFunc(IRInst* func, DifferentiableLevel level)
{
switch (func->getOp())
{
case kIROp_ForwardDifferentiate:
if (auto fwdDerivative =
func->getOperand(0)->findDecoration<IRForwardDerivativeDecoration>())
return isDifferentiableFunc(fwdDerivative->getForwardDerivativeFunc(), level);
return isDifferentiableFunc(func->getOperand(0), level);
case kIROp_BackwardDifferentiate:
if (auto bwdDerivative =
func->getOperand(0)
->findDecoration<IRUserDefinedBackwardDerivativeDecoration>())
return isDifferentiableFunc(bwdDerivative->getBackwardDerivativeFunc(), level);
return isDifferentiableFunc(func->getOperand(0), level);
default:
break;
}
func = getResolvedInstForDecorations(func);
if (!func)
return false;
if (auto substDecor = func->findDecoration<IRPrimalSubstituteDecoration>())
{
func = getResolvedInstForDecorations(substDecor->getPrimalSubstituteFunc());
if (!func)
return false;
}
if (auto existingLevel = differentiableFunctions.tryGetValue(func))
return *existingLevel >= level;
if (func->findDecoration<IRTreatAsDifferentiableDecoration>())
return true;
if (auto lookupInterfaceMethod = as<IRLookupWitnessMethod>(func))
{
auto wit = lookupInterfaceMethod->getWitnessTable();
if (!wit)
return false;
auto witType = as<IRWitnessTableTypeBase>(wit->getDataType());
if (!witType)
return false;
auto interfaceType = witType->getConformanceType();
if (!interfaceType)
return false;
if (interfaceType->findDecoration<IRTreatAsDifferentiableDecoration>())
return true;
if (sharedContext.differentiableInterfaceType &&
interfaceType == sharedContext.differentiableInterfaceType)
return true;
if (lookupInterfaceMethod->getRequirementKey()
->findDecoration<IRBackwardDerivativeDecoration>())
return true;
if (lookupInterfaceMethod->getRequirementKey()
->findDecoration<IRForwardDerivativeDecoration>())
return level == DifferentiableLevel::Forward;
}
for (; func; func = func->parent)
{
if (as<IRGeneric>(func))
{
if (auto existingLevel = differentiableFunctions.tryGetValue(func))
{
if (*existingLevel >= level)
return true;
}
}
}
return false;
}
bool isInstInFunc(IRInst* inst, IRInst* func)
{
while (inst)
{
if (inst == func)
return true;
inst = inst->parent;
}
return false;
}
bool canAddressHoldDerivative(
DifferentiableTypeConformanceContext& diffTypeContext,
IRInst* addr)
{
if (!addr)
return false;
while (addr)
{
switch (addr->getOp())
{
case kIROp_Var:
case kIROp_Param:
return isDifferentiableType(diffTypeContext, addr->getDataType());
case kIROp_FieldAddress:
if (!as<IRFieldAddress>(addr)->getField() ||
as<IRFieldAddress>(addr)
->getField()
->findDecoration<IRDerivativeMemberDecoration>() == nullptr)
return false;
addr = as<IRFieldAddress>(addr)->getBase();
break;
case kIROp_GetElementPtr:
if (!isDifferentiableType(
diffTypeContext,
as<IRGetElementPtr>(addr)->getBase()->getDataType()))
return false;
addr = as<IRGetElementPtr>(addr)->getBase();
break;
default:
return false;
}
}
return false;
}
bool instHasNonTrivialDerivative(
DifferentiableTypeConformanceContext& diffTypeContext,
IRInst* inst)
{
switch (inst->getOp())
{
case kIROp_DetachDerivative:
return false;
case kIROp_Call:
{
auto call = as<IRCall>(inst);
return isDifferentiableFunc(
call->getCallee(),
CheckDifferentiabilityPassContext::DifferentiableLevel::Forward);
}
default:
return isDifferentiableType(diffTypeContext, inst->getDataType());
}
}
bool checkType(IRInst* type)
{
type = unwrapAttributedType(type);
if (as<IRTorchTensorType>(type))
return false;
else if (auto arrayType = as<IRArrayTypeBase>(type))
return checkType(arrayType->getElementType());
else if (auto structType = as<IRStructType>(type))
{
for (auto field : structType->getFields())
{
if (!checkType(field->getFieldType()))
return false;
}
}
return true;
}
void checkForInvalidHostTypeUsage(IRGlobalValueWithCode* funcInst)
{
auto outerFuncInst = maybeFindOuterGeneric(funcInst);
if (outerFuncInst->findDecoration<IRCudaHostDecoration>())
return;
if (outerFuncInst->findDecoration<IRTorchEntryPointDecoration>())
return;
bool isSynthesizeConstructor = false;
if (auto constructor = funcInst->findDecoration<IRConstructorDecoration>())
isSynthesizeConstructor = constructor->getSynthesizedStatus();
// This is a kernel function, we don't allow using TorchTensor type here.
for (auto b : funcInst->getBlocks())
{
for (auto inst : b->getChildren())
{
if (!checkType(inst->getDataType()))
{
if (isSynthesizeConstructor)
{
IRBuilder irBuilder(funcInst);
irBuilder.addDecoration(funcInst, kIROp_CudaHostDecoration);
return;
}
auto loc = inst->sourceLoc;
if (!loc.isValid())
loc = funcInst->sourceLoc;
sink->diagnose(loc, Diagnostics::invalidUseOfTorchTensorTypeInDeviceFunc);
return;
}
}
}
}
void processFunc(IRGlobalValueWithCode* funcInst)
{
checkForInvalidHostTypeUsage(funcInst);
if (!_isFuncMarkedForAutoDiff(funcInst))
return;
if (!funcInst->getFirstBlock())
return;
DifferentiableTypeConformanceContext diffTypeContext(&sharedContext);
diffTypeContext.setFunc(funcInst);
// We compute and track three different set of insts to complete our
// data flow analysis.
// `produceDiffSet` represents a set of insts that can provide a diff. This is conservative
// on the positive side: a float literal is considered to be able to provide a diff.
// `carryNonTrivialDiffSet` represents a set of insts that may carry a non-zero diff. This
// is conservative on the negative side: if the inst does not provide a diff, or if we can
// prove the diff is zero, we exclude the inst from the set. This makes
// `carryNonTrivialDiffSet` a strict subset of `produceDiffSet`. `expectDiffSet` is a set of
// insts that expects their operands to produce a diff. It is an error if they don't.
InstHashSet produceDiffSet(funcInst->getModule());
InstHashSet expectDiffSet(funcInst->getModule());
InstHashSet carryNonTrivialDiffSet(funcInst->getModule());
bool isDifferentiableReturnType = false;
for (auto param : funcInst->getFirstBlock()->getParams())
{
if (isDifferentiableType(diffTypeContext, param->getFullType()))
{
produceDiffSet.add(param);
carryNonTrivialDiffSet.add(param);
}
}
if (auto funcType = as<IRFuncType>(funcInst->getDataType()))
{
if (isDifferentiableType(diffTypeContext, funcType->getResultType()))
{
isDifferentiableReturnType = true;
}
}
DifferentiableLevel requiredDiffLevel = DifferentiableLevel::Forward;
if (isBackwardDifferentiableFunc(funcInst))
requiredDiffLevel = DifferentiableLevel::Backward;
auto isInstProducingDiff = [&](IRInst* inst) -> bool
{
switch (inst->getOp())
{
case kIROp_FloatLit:
return true;
case kIROp_Call:
return shouldTreatCallAsDifferentiable(inst) ||
isDifferentiableFunc(as<IRCall>(inst)->getCallee(), requiredDiffLevel) &&
isDifferentiableType(diffTypeContext, inst->getFullType());
case kIROp_Load:
// We don't have more knowledge on whether diff is available at the destination
// address. Just assume it is producing diff if the dest address can hold a
// derivative.
// TODO: propagate the info if this is a load of a temporary variable intended
// to receive result from an `out` parameter.
return canAddressHoldDerivative(diffTypeContext, as<IRLoad>(inst)->getPtr());
default:
// default case is to assume the inst produces a diff value if any
// of its operands produces a diff value.
if (!isDifferentiableType(diffTypeContext, inst->getFullType()))
return false;
for (UInt i = 0; i < inst->getOperandCount(); i++)
{
if (produceDiffSet.contains(inst->getOperand(i)))
{
return true;
}
}
return false;
}
};
auto isInstCarryingOverDiff = [&](IRInst* inst) -> bool
{
switch (inst->getOp())
{
case kIROp_DetachDerivative:
return false;
case kIROp_Call:
if (shouldTreatCallAsDifferentiable(inst))
return false;
return isDifferentiableFunc(as<IRCall>(inst)->getCallee(), requiredDiffLevel) &&
isDifferentiableType(diffTypeContext, inst->getFullType());
case kIROp_Load:
// We don't have more knowledge on whether diff is available at the destination
// address. Just assume it is producing diff if the dest address can hold a
// derivative.
// TODO: propagate the info if this is a load of a temporary variable intended
// to receive result from an `out` parameter.
return canAddressHoldDerivative(diffTypeContext, as<IRLoad>(inst)->getPtr());
default:
// default case is to assume the inst produces a diff value if any
// of its operands produces a diff value.
if (!isDifferentiableType(diffTypeContext, inst->getFullType()))
return false;
for (UInt i = 0; i < inst->getOperandCount(); i++)
{
if (carryNonTrivialDiffSet.contains(inst->getOperand(i)))
{
return true;
}
}
return false;
}
};
List<IRInst*> expectDiffInstWorkList;
OrderedHashSet<IRInst*> expectDiffInstWorkListSet;
auto addToExpectDiffWorkList = [&](IRInst* inst)
{
if (isInstInFunc(inst, funcInst))
{
if (expectDiffInstWorkListSet.add(inst))
{
expectDiffInstWorkList.add(inst);
}
}
};
// Run data flow analysis and generate `produceDiffSet` and an intial `expectDiffSet`.
Index lastProduceDiffCount = 0;
do
{
lastProduceDiffCount = produceDiffSet.getCount();
for (auto block : funcInst->getBlocks())
{
if (block != funcInst->getFirstBlock())
{
UInt paramIndex = 0;
for (auto param : block->getParams())
{
for (auto p : block->getPredecessors())
{
// A Phi Node is producing diff if any of its candidate values are
// producing diff.
if (auto branch = as<IRUnconditionalBranch>(p->getTerminator()))
{
if (branch->getArgCount() > paramIndex)
{
auto arg = branch->getArg(paramIndex);
if (produceDiffSet.contains(arg))
produceDiffSet.add(param);
if (carryNonTrivialDiffSet.contains(arg))
carryNonTrivialDiffSet.add(param);
}
}
}
paramIndex++;
}
}
for (auto inst : block->getChildren())
{
if (isInstProducingDiff(inst))
produceDiffSet.add(inst);
if (isInstCarryingOverDiff(inst))
carryNonTrivialDiffSet.add(inst);
switch (inst->getOp())
{
case kIROp_Call:
if (isDifferentiableFunc(as<IRCall>(inst)->getCallee(), requiredDiffLevel))
{
addToExpectDiffWorkList(inst);
}
break;
case kIROp_Store:
{
auto storeInst = as<IRStore>(inst);
if (canAddressHoldDerivative(diffTypeContext, storeInst->getPtr()) &&
isDifferentiableType(
diffTypeContext,
as<IRStore>(inst)->getPtr()->getDataType()))
{
addToExpectDiffWorkList(storeInst->getVal());
}
}
break;
case kIROp_Return:
if (auto returnVal = as<IRReturn>(inst)->getVal())
{
if (isDifferentiableReturnType &&
isDifferentiableType(diffTypeContext, returnVal->getDataType()))
{
addToExpectDiffWorkList(inst);
}
}
break;
default:
break;
}
}
}
} while (produceDiffSet.getCount() != lastProduceDiffCount);
// Reverse propagate `expectDiffSet`.
for (int i = 0; i < expectDiffInstWorkList.getCount(); i++)
{
auto inst = expectDiffInstWorkList[i];
// Is inst in produceDiffSet?
if (!produceDiffSet.contains(inst))
{
if (auto call = as<IRCall>(inst))
{
const auto callee = call->getCallee();
// If inst's type is differentiable, and it is in expectDiffInstWorkList,
// then some user is expecting the result of the call to produce a derivative.
// In this case we need to issue a diagnostic.
if (isDifferentiableType(diffTypeContext, inst->getFullType()) &&
!isDifferentiableFunc(callee, requiredDiffLevel))
{
// No need to fail here if the function is no_diff in
// both inputs and all outputs, this is equivalent of
// inserting no_diff on this inst.
if (!isNeverDiffFuncType(cast<IRFuncType>(callee->getDataType())) &&
!shouldCallImpliesNoDiff(diffTypeContext, call))
{
sink->diagnose(
inst,
Diagnostics::lossOfDerivativeDueToCallOfNonDifferentiableFunction,
getResolvedInstForDecorations(call->getCallee()),
requiredDiffLevel == DifferentiableLevel::Forward ? "forward"
: "backward");
}
}
}
}
switch (inst->getOp())
{
case kIROp_Param:
{
auto block = as<IRBlock>(inst->getParent());
if (block != funcInst->getFirstBlock())
{
auto paramIndex = getParamIndexInBlock(
as<IRParam, IRDynamicCastBehavior::NoUnwrap>(inst));
if (paramIndex != -1)
{
for (auto p : block->getPredecessors())
{
// A Phi Node is producing diff if any of its candidate values are
// producing diff.
if (auto branch = as<IRUnconditionalBranch>(p->getTerminator()))
{
if (branch->getArgCount() > (UInt)paramIndex)
{
auto arg = branch->getArg(paramIndex);
addToExpectDiffWorkList(arg);
}
}
}
}
}
break;
}
case kIROp_Call:
{
auto callInst = as<IRCall>(inst);
if (callInst->findDecoration<IRTreatCallAsDifferentiableDecoration>())
continue;
auto calleeFuncType = as<IRFuncType>(callInst->getCallee()->getFullType());
if (!calleeFuncType)
continue;
if (calleeFuncType->getParamCount() != callInst->getArgCount())
continue;
for (UInt a = 0; a < callInst->getArgCount(); a++)
{
auto arg = callInst->getArg(a);
auto paramType = calleeFuncType->getParamType(a);
if (!isDifferentiableType(diffTypeContext, paramType))
continue;
addToExpectDiffWorkList(arg);
}
break;
}
default:
// Default behavior is to request all differentiable operands to provide
// differential.
for (UInt opIndex = 0; opIndex < inst->getOperandCount(); opIndex++)
{
auto operand = inst->getOperand(opIndex);
if (isDifferentiableType(diffTypeContext, operand->getFullType()))
{
addToExpectDiffWorkList(operand);
}
}
}
}
// Make sure all loops are marked with either [MaxIters] or [ForceUnroll].
for (auto block : funcInst->getBlocks())
{
auto loop = as<IRLoop>(block->getTerminator());
if (!loop)
continue;
bool hasBackEdge = false;
for (auto use = loop->getTargetBlock()->firstUse; use; use = use->nextUse)
{
if (use->getUser() != loop)
{
hasBackEdge = true;
break;
}
}
if (!hasBackEdge)
continue;
if (loop->findDecoration<IRLoopMaxItersDecoration>() ||
loop->findDecoration<IRForceUnrollDecoration>())
{
// We are good.
}
else
{
sink->diagnose(loop->sourceLoc, Diagnostics::loopInDiffFuncRequireUnrollOrMaxIters);
}
}
// Make sure all stores of differentiable values are into addresses that can hold
// derivatives. If we are assigning a value to a non-differentiable location, we need to
// make sure that value doesn't carray a non-zero diff.
for (auto block : funcInst->getBlocks())
{
for (auto inst : block->getChildren())
{
if (auto storeInst = as<IRStore>(inst))
{
if (carryNonTrivialDiffSet.contains(storeInst->getVal()) &&
!canAddressHoldDerivative(diffTypeContext, storeInst->getPtr()))
{
sink->diagnose(
storeInst->sourceLoc,
Diagnostics::lossOfDerivativeAssigningToNonDifferentiableLocation);
}
}
else if (auto callInst = as<IRCall>(inst))
{
if (!isDifferentiableFunc(callInst->getCallee(), DifferentiableLevel::Forward))
continue;
auto calleeFuncType = as<IRFuncType>(callInst->getCallee()->getFullType());
if (!calleeFuncType)
continue;
if (calleeFuncType->getParamCount() != callInst->getArgCount())
continue;
for (UInt a = 0; a < callInst->getArgCount(); a++)
{
auto arg = callInst->getArg(a);
auto paramType = calleeFuncType->getParamType(a);
if (!isDifferentiableType(diffTypeContext, paramType))
continue;
if (as<IROutParamTypeBase>(paramType))
{
if (!canAddressHoldDerivative(diffTypeContext, arg))
{
sink->diagnose(
arg->sourceLoc,
Diagnostics::
lossOfDerivativeUsingNonDifferentiableLocationAsOutArg);
}
}
}
}
}
}
}
void processModule()
{
// Collect set of differentiable functions.
HashSet<UnownedStringSlice> fwdDifferentiableSymbolNames, bwdDifferentiableSymbolNames;
for (auto inst : module->getGlobalInsts())
{
if (_isDifferentiableFuncImpl(inst, DifferentiableLevel::Backward))
{
if (auto linkageDecor = inst->findDecoration<IRLinkageDecoration>())
bwdDifferentiableSymbolNames.add(linkageDecor->getMangledName());
differentiableFunctions.add(inst, DifferentiableLevel::Backward);
}
else if (_isDifferentiableFuncImpl(inst, DifferentiableLevel::Forward))
{
if (auto linkageDecor = inst->findDecoration<IRLinkageDecoration>())
fwdDifferentiableSymbolNames.add(linkageDecor->getMangledName());
differentiableFunctions.add(inst, DifferentiableLevel::Forward);
}
}
for (auto inst : module->getGlobalInsts())
{
if (auto linkageDecor = inst->findDecoration<IRLinkageDecoration>())
{
if (bwdDifferentiableSymbolNames.contains(linkageDecor->getMangledName()))
differentiableFunctions[inst] = DifferentiableLevel::Backward;
else if (fwdDifferentiableSymbolNames.contains(linkageDecor->getMangledName()))
differentiableFunctions.addIfNotExists(inst, DifferentiableLevel::Forward);
}
}
if (!sharedContext.isInterfaceAvailable && !sharedContext.isPtrInterfaceAvailable)
return;
for (auto inst : module->getGlobalInsts())
{
if (auto genericInst = as<IRGeneric>(inst))
{
if (auto innerFunc =
as<IRGlobalValueWithCode>(findInnerMostGenericReturnVal(genericInst)))
processFunc(innerFunc);
}
else if (auto funcInst = as<IRGlobalValueWithCode>(inst))
{
processFunc(funcInst);
}
}
}
};
void checkAutoDiffUsages(IRModule* module, DiagnosticSink* sink)
{
CheckDifferentiabilityPassContext context(module, sink);
context.processModule();
}
} // namespace Slang
|