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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
|
#include "slang-ir-autodiff.h"
#include "slang-ir-address-analysis.h"
#include "slang-ir-autodiff-rev.h"
#include "slang-ir-autodiff-fwd.h"
#include "slang-ir-autodiff-pairs.h"
#include "slang-ir-single-return.h"
#include "slang-ir-ssa-simplification.h"
#include "slang-ir-validate.h"
namespace Slang
{
bool isBackwardDifferentiableFunc(IRInst* func)
{
for (auto decorations : func->getDecorations())
{
switch (decorations->getOp())
{
case kIROp_BackwardDifferentiableDecoration:
case kIROp_UserDefinedBackwardDerivativeDecoration:
return true;
}
}
return false;
}
IRInst* _lookupWitness(IRBuilder* builder, IRInst* witness, IRInst* requirementKey)
{
if (auto witnessTable = as<IRWitnessTable>(witness))
{
for (auto entry : witnessTable->getEntries())
{
if (entry->getRequirementKey() == requirementKey)
return entry->getSatisfyingVal();
}
}
else
{
return builder->emitLookupInterfaceMethodInst(
builder->getTypeKind(),
witness,
requirementKey);
}
return nullptr;
}
bool isNoDiffType(IRType* paramType)
{
while (auto ptrType = as<IRPtrTypeBase>(paramType))
paramType = ptrType->getValueType();
while (auto attrType = as<IRAttributedType>(paramType))
{
if (attrType->findAttr<IRNoDiffAttr>())
{
return true;
}
}
return false;
}
IRInst* lookupForwardDerivativeReference(IRInst* primalFunction)
{
if (auto jvpDefinition = primalFunction->findDecoration<IRForwardDerivativeDecoration>())
return jvpDefinition->getForwardDerivativeFunc();
return nullptr;
}
IRStructField* DifferentialPairTypeBuilder::findField(IRInst* type, IRStructKey* key)
{
if (auto irStructType = as<IRStructType>(type))
{
for (auto field : irStructType->getFields())
{
if (field->getKey() == key)
{
return field;
}
}
}
else if (auto irSpecialize = as<IRSpecialize>(type))
{
if (auto irGeneric = as<IRGeneric>(irSpecialize->getBase()))
{
if (auto irGenericStructType = as<IRStructType>(findInnerMostGenericReturnVal(irGeneric)))
{
return findField(irGenericStructType, key);
}
}
}
return nullptr;
}
IRInst* DifferentialPairTypeBuilder::findSpecializationForParam(IRInst* specializeInst, IRInst* genericParam)
{
// Get base generic that's being specialized.
auto genericType = as<IRGeneric>(as<IRSpecialize>(specializeInst)->getBase());
SLANG_ASSERT(genericType);
// Find the index of genericParam in the base generic.
int paramIndex = -1;
int currentIndex = 0;
for (auto param : genericType->getParams())
{
if (param == genericParam)
paramIndex = currentIndex;
currentIndex ++;
}
SLANG_ASSERT(paramIndex >= 0);
// Return the corresponding operand in the specialization inst.
return specializeInst->getOperand(1 + paramIndex);
}
IRInst* DifferentialPairTypeBuilder::emitFieldAccessor(IRBuilder* builder, IRInst* baseInst, IRStructKey* key)
{
IRInst* pairType = nullptr;
if (auto basePtrType = as<IRPtrTypeBase>(baseInst->getDataType()))
{
auto loweredType = lowerDiffPairType(builder, basePtrType->getValueType());
pairType = builder->getPtrType(kIROp_PtrType, (IRType*)loweredType);
}
else
{
auto loweredType = lowerDiffPairType(builder, baseInst->getDataType());
pairType = loweredType;
}
if (auto basePairStructType = as<IRStructType>(pairType))
{
return as<IRFieldExtract>(builder->emitFieldExtract(
findField(basePairStructType, key)->getFieldType(),
baseInst,
key
));
}
else if (auto ptrType = as<IRPtrTypeBase>(pairType))
{
if (auto ptrInnerSpecializedType = as<IRSpecialize>(ptrType->getValueType()))
{
auto genericType = findInnerMostGenericReturnVal(as<IRGeneric>(ptrInnerSpecializedType->getBase()));
if (auto genericBasePairStructType = as<IRStructType>(genericType))
{
return as<IRFieldAddress>(builder->emitFieldAddress(
builder->getPtrType((IRType*)
findSpecializationForParam(
ptrInnerSpecializedType,
findField(ptrInnerSpecializedType, key)->getFieldType())),
baseInst,
key
));
}
}
else if (auto ptrBaseStructType = as<IRStructType>(ptrType->getValueType()))
{
return as<IRFieldAddress>(builder->emitFieldAddress(
builder->getPtrType((IRType*)
findField(ptrBaseStructType, key)->getFieldType()),
baseInst,
key));
}
}
else if (auto specializedType = as<IRSpecialize>(pairType))
{
// TODO: Stopped here -> The type being emitted is incorrect. don't emit the generic's
// type, emit the specialization type.
//
auto genericType = findInnerMostGenericReturnVal(as<IRGeneric>(specializedType->getBase()));
if (auto genericBasePairStructType = as<IRStructType>(genericType))
{
return as<IRFieldExtract>(builder->emitFieldExtract(
(IRType*)findSpecializationForParam(
specializedType,
findField(genericBasePairStructType, key)->getFieldType()),
baseInst,
key
));
}
else if (auto genericPtrType = as<IRPtrTypeBase>(genericType))
{
if (auto genericPairStructType = as<IRStructType>(genericPtrType->getValueType()))
{
return as<IRFieldAddress>(builder->emitFieldAddress(
builder->getPtrType((IRType*)
findSpecializationForParam(
specializedType,
findField(genericPairStructType, key)->getFieldType())),
baseInst,
key
));
}
}
}
else
{
SLANG_UNEXPECTED("Unrecognized field. Cannot emit field accessor");
}
return nullptr;
}
IRInst* DifferentialPairTypeBuilder::emitPrimalFieldAccess(IRBuilder* builder, IRInst* baseInst)
{
return emitFieldAccessor(builder, baseInst, this->globalPrimalKey);
}
IRInst* DifferentialPairTypeBuilder::emitDiffFieldAccess(IRBuilder* builder, IRInst* baseInst)
{
return emitFieldAccessor(builder, baseInst, this->globalDiffKey);
}
IRStructKey* DifferentialPairTypeBuilder::_getOrCreateDiffStructKey()
{
if (!this->globalDiffKey)
{
IRBuilder builder(sharedContext->sharedBuilder);
// Insert directly at top level (skip any generic scopes etc.)
builder.setInsertInto(sharedContext->moduleInst);
this->globalDiffKey = builder.createStructKey();
builder.addNameHintDecoration(this->globalDiffKey , UnownedTerminatedStringSlice("differential"));
}
return this->globalDiffKey;
}
IRStructKey* DifferentialPairTypeBuilder::_getOrCreatePrimalStructKey()
{
if (!this->globalPrimalKey)
{
// Insert directly at top level (skip any generic scopes etc.)
IRBuilder builder(sharedContext->sharedBuilder);
builder.setInsertInto(sharedContext->moduleInst);
this->globalPrimalKey = builder.createStructKey();
builder.addNameHintDecoration(this->globalPrimalKey , UnownedTerminatedStringSlice("primal"));
}
return this->globalPrimalKey;
}
IRInst* DifferentialPairTypeBuilder::_createDiffPairType(IRType* origBaseType, IRType* diffType)
{
switch (origBaseType->getOp())
{
case kIROp_LookupWitness:
case kIROp_Specialize:
case kIROp_Param:
return nullptr;
default:
break;
}
IRBuilder builder(sharedContext->sharedBuilder);
builder.setInsertBefore(diffType);
auto pairStructType = builder.createStructType();
builder.createStructField(pairStructType, _getOrCreatePrimalStructKey(), origBaseType);
builder.createStructField(pairStructType, _getOrCreateDiffStructKey(), (IRType*)diffType);
return pairStructType;
}
IRInst* DifferentialPairTypeBuilder::getDiffTypeFromPairType(IRBuilder* builder, IRDifferentialPairType* type)
{
auto witnessTable = type->getWitness();
return _lookupWitness(builder, witnessTable, sharedContext->differentialAssocTypeStructKey);
}
IRInst* DifferentialPairTypeBuilder::getDiffTypeWitnessFromPairType(IRBuilder* builder, IRDifferentialPairType* type)
{
auto witnessTable = type->getWitness();
return _lookupWitness(builder, witnessTable, sharedContext->differentialAssocTypeWitnessStructKey);
}
IRInst* DifferentialPairTypeBuilder::lowerDiffPairType(
IRBuilder* builder, IRType* originalPairType)
{
IRInst* result = nullptr;
if (pairTypeCache.TryGetValue(originalPairType, result))
return result;
auto pairType = as<IRDifferentialPairType>(originalPairType);
if (!pairType)
{
result = originalPairType;
return result;
}
auto primalType = pairType->getValueType();
if (as<IRParam>(primalType))
{
result = nullptr;
return result;
}
auto diffType = getDiffTypeFromPairType(builder, pairType);
if (!diffType)
return result;
result = _createDiffPairType(pairType->getValueType(), (IRType*)diffType);
pairTypeCache.Add(originalPairType, result);
return result;
}
AutoDiffSharedContext::AutoDiffSharedContext(IRModuleInst* inModuleInst)
: moduleInst(inModuleInst)
{
differentiableInterfaceType = as<IRInterfaceType>(findDifferentiableInterface());
if (differentiableInterfaceType)
{
differentialAssocTypeStructKey = findDifferentialTypeStructKey();
differentialAssocTypeWitnessStructKey = findDifferentialTypeWitnessStructKey();
zeroMethodStructKey = findZeroMethodStructKey();
addMethodStructKey = findAddMethodStructKey();
mulMethodStructKey = findMulMethodStructKey();
if (differentialAssocTypeStructKey)
isInterfaceAvailable = true;
}
}
IRInst* AutoDiffSharedContext::findDifferentiableInterface()
{
if (auto module = as<IRModuleInst>(moduleInst))
{
for (auto globalInst : module->getGlobalInsts())
{
// TODO: This seems like a particularly dangerous way to look for an interface.
// See if we can lower IDifferentiable to a separate IR inst.
//
if (auto intf = as<IRInterfaceType>(globalInst))
{
if (auto decor = intf->findDecoration<IRNameHintDecoration>())
{
if (decor->getName() == toSlice("IDifferentiable"))
{
return globalInst;
}
}
}
}
}
return nullptr;
}
IRStructKey* AutoDiffSharedContext::getIDifferentiableStructKeyAtIndex(UInt index)
{
if (as<IRModuleInst>(moduleInst) && differentiableInterfaceType)
{
// Assume for now that IDifferentiable has exactly five fields.
SLANG_ASSERT(differentiableInterfaceType->getOperandCount() == 5);
if (auto entry = as<IRInterfaceRequirementEntry>(differentiableInterfaceType->getOperand(index)))
return as<IRStructKey>(entry->getRequirementKey());
else
{
SLANG_UNEXPECTED("IDifferentiable interface entry unexpected type");
}
}
return nullptr;
}
void DifferentiableTypeConformanceContext::setFunc(IRGlobalValueWithCode* func)
{
parentFunc = func;
auto decor = func->findDecoration<IRDifferentiableTypeDictionaryDecoration>();
SLANG_RELEASE_ASSERT(decor);
// Build lookup dictionary for type witnesses.
for (auto child = decor->getFirstChild(); child; child = child->next)
{
if (auto item = as<IRDifferentiableTypeDictionaryItem>(child))
{
auto existingItem = differentiableWitnessDictionary.TryGetValue(item->getConcreteType());
if (existingItem)
{
*existingItem = item->getWitness();
}
else
{
differentiableWitnessDictionary.Add((IRType*)item->getConcreteType(), item->getWitness());
}
}
}
}
IRInst* DifferentiableTypeConformanceContext::lookUpConformanceForType(IRInst* type)
{
IRInst* foundResult = nullptr;
differentiableWitnessDictionary.TryGetValue(type, foundResult);
return foundResult;
}
IRInst* DifferentiableTypeConformanceContext::lookUpInterfaceMethod(IRBuilder* builder, IRType* origType, IRStructKey* key)
{
if (auto conformance = lookUpConformanceForType(origType))
{
return _lookupWitness(builder, conformance, key);
}
return nullptr;
}
IRInst* DifferentiableTypeConformanceContext::getDifferentialTypeFromDiffPairType(
IRBuilder* builder, IRDifferentialPairType* diffPairType)
{
auto witness = diffPairType->getWitness();
SLANG_RELEASE_ASSERT(witness);
return _lookupWitness(builder, witness, sharedContext->differentialAssocTypeStructKey);
}
void DifferentiableTypeConformanceContext::buildGlobalWitnessDictionary()
{
for (auto globalInst : sharedContext->moduleInst->getChildren())
{
if (auto pairType = as<IRDifferentialPairType>(globalInst))
{
differentiableWitnessDictionary.AddIfNotExists(pairType->getValueType(), pairType->getWitness());
}
}
}
void stripDerivativeDecorations(IRInst* inst)
{
for (auto decor = inst->getFirstDecoration(); decor; )
{
auto next = decor->getNextDecoration();
switch (decor->getOp())
{
case kIROp_ForwardDerivativeDecoration:
case kIROp_DerivativeMemberDecoration:
case kIROp_BackwardDerivativeDecoration:
case kIROp_BackwardDerivativeIntermediateTypeDecoration:
case kIROp_BackwardDerivativePropagateDecoration:
case kIROp_BackwardDerivativePrimalDecoration:
case kIROp_UserDefinedBackwardDerivativeDecoration:
case kIROp_AutoDiffOriginalValueDecoration:
decor->removeAndDeallocate();
break;
default:
break;
}
decor = next;
}
}
void stripAutoDiffDecorationsFromChildren(IRInst* parent)
{
for (auto inst : parent->getChildren())
{
for (auto decor = inst->getFirstDecoration(); decor; )
{
auto next = decor->getNextDecoration();
switch (decor->getOp())
{
case kIROp_ForwardDerivativeDecoration:
case kIROp_DerivativeMemberDecoration:
case kIROp_DifferentiableTypeDictionaryDecoration:
case kIROp_DifferentialInstDecoration:
case kIROp_MixedDifferentialInstDecoration:
case kIROp_BackwardDerivativeDecoration:
case kIROp_BackwardDerivativeIntermediateTypeDecoration:
case kIROp_BackwardDerivativePropagateDecoration:
case kIROp_BackwardDerivativePrimalDecoration:
case kIROp_BackwardDerivativePrimalContextDecoration:
case kIROp_BackwardDerivativePrimalReturnDecoration:
case kIROp_AutoDiffOriginalValueDecoration:
case kIROp_UserDefinedBackwardDerivativeDecoration:
decor->removeAndDeallocate();
break;
default:
break;
}
decor = next;
}
if (inst->getFirstChild() != nullptr)
{
stripAutoDiffDecorationsFromChildren(inst);
}
}
}
void stripAutoDiffDecorations(IRModule* module)
{
stripAutoDiffDecorationsFromChildren(module->getModuleInst());
}
void stripTempDecorations(IRInst* inst)
{
for (auto decor = inst->getFirstDecoration(); decor; )
{
auto next = decor->getNextDecoration();
switch (decor->getOp())
{
case kIROp_DifferentialInstDecoration:
case kIROp_MixedDifferentialInstDecoration:
case kIROp_AutoDiffOriginalValueDecoration:
case kIROp_BackwardDerivativePrimalReturnDecoration:
case kIROp_PrimalValueStructKeyDecoration:
decor->removeAndDeallocate();
break;
default:
break;
}
decor = next;
}
for (auto child : inst->getChildren())
{
stripTempDecorations(child);
}
}
struct StripNoDiffTypeAttributePass : InstPassBase
{
StripNoDiffTypeAttributePass(IRModule* module) :
InstPassBase(module)
{
}
void processModule()
{
processInstsOfType<IRAttributedType>(kIROp_AttributedType, [&](IRAttributedType* attrType)
{
if (attrType->getAllAttrs().getCount() == 1)
{
if (attrType->findAttr<IRNoDiffAttr>())
{
attrType->replaceUsesWith(attrType->getBaseType());
attrType->removeAndDeallocate();
}
}
});
sharedBuilderStorage.init(module);
sharedBuilderStorage.deduplicateAndRebuildGlobalNumberingMap();
}
};
void stripNoDiffTypeAttribute(IRModule* module)
{
StripNoDiffTypeAttributePass pass(module);
pass.processModule();
}
bool isDifferentiableType(DifferentiableTypeConformanceContext& context, IRInst* typeInst)
{
if (context.isDifferentiableType((IRType*)typeInst))
return true;
// Look for equivalent types.
for (auto type : context.differentiableWitnessDictionary)
{
if (isTypeEqual(type.Key, (IRType*)typeInst))
{
context.differentiableWitnessDictionary[(IRType*)typeInst] = type.Value;
return true;
}
}
return false;
}
struct AutoDiffPass : public InstPassBase
{
DiagnosticSink* getSink()
{
return sink;
}
bool processModule()
{
// TODO(sai): Move this call.
forwardTranscriber.differentiableTypeConformanceContext.buildGlobalWitnessDictionary();
IRBuilder builderStorage(&sharedBuilderStorage);
IRBuilder* builder = &builderStorage;
// Process all ForwardDifferentiate and BackwardDifferentiate instructions by
// generating derivative code for the referenced function.
//
bool modified = processReferencedFunctions(builder);
return modified;
}
IRInst* processIntermediateContextTypeBase(IRBuilder* builder, IRInst* base)
{
if (auto spec = as<IRSpecialize>(base))
{
List<IRInst*> args;
auto subBase = processIntermediateContextTypeBase(builder, spec->getBase());
for (UInt a = 0; a < spec->getArgCount(); a++)
args.add(spec->getArg(a));
auto actualType = builder->emitSpecializeInst(
builder->getTypeKind(),
subBase,
args.getCount(),
args.getBuffer());
return actualType;
}
else if (auto baseGeneric = as<IRGeneric>(base))
{
auto inner = findGenericReturnVal(baseGeneric);
if (auto typeDecor = inner->findDecoration<IRBackwardDerivativeIntermediateTypeDecoration>())
{
return typeDecor->getBackwardDerivativeIntermediateType();
}
}
else if (auto func = as<IRFunc>(base))
{
if (auto typeDecor = func->findDecoration<IRBackwardDerivativeIntermediateTypeDecoration>())
{
return typeDecor->getBackwardDerivativeIntermediateType();
}
}
else if (auto lookup = as<IRLookupWitnessMethod>(base))
{
auto key = lookup->getRequirementKey();
if (auto typeDecor = key->findDecoration<IRBackwardDerivativeIntermediateTypeDecoration>())
{
auto typeKey = typeDecor->getBackwardDerivativeIntermediateType();
auto typeLookup = builder->emitLookupInterfaceMethodInst(builder->getTypeKind(), lookup->getWitnessTable(), typeKey);
return typeLookup;
}
}
return nullptr;
}
bool lowerIntermediateContextType(IRBuilder* builder)
{
bool changed = false;
processAllInsts([&](IRInst* inst)
{
switch (inst->getOp())
{
case kIROp_BackwardDiffIntermediateContextType:
{
auto differentiateInst = as<IRBackwardDiffIntermediateContextType>(inst);
auto baseFunc = differentiateInst->getOperand(0);
IRBuilder subBuilder = *builder;
subBuilder.setInsertBefore(inst);
auto type = processIntermediateContextTypeBase(&subBuilder, baseFunc);
if (type)
{
inst->replaceUsesWith(type);
inst->removeAndDeallocate();
changed = true;
}
}
break;
default:
break;
}
});
return changed;
}
// Process all differentiate calls, and recursively generate code for forward and backward
// derivative functions.
//
bool processReferencedFunctions(IRBuilder* builder)
{
bool hasChanges = false;
for (;;)
{
bool changed = false;
List<IRInst*> autoDiffWorkList;
// Collect all `ForwardDifferentiate`/`BackwardDifferentiate` insts from the module.
autoDiffWorkList.clear();
processAllInsts([&](IRInst* inst)
{
switch (inst->getOp())
{
case kIROp_ForwardDifferentiate:
case kIROp_BackwardDifferentiate:
case kIROp_BackwardDifferentiatePrimal:
case kIROp_BackwardDifferentiatePropagate:
case kIROp_BackwardDiffIntermediateContextType:
// Only process now if the operand is a materialized function.
switch (inst->getOperand(0)->getOp())
{
case kIROp_Func:
case kIROp_Specialize:
case kIROp_LookupWitness:
autoDiffWorkList.add(inst);
break;
default:
break;
}
break;
default:
break;
}
});
// Process collected differentiate insts and replace them with placeholders for
// differentiated functions.
for (Index i = 0; i < autoDiffWorkList.getCount(); i++)
{
auto differentiateInst = autoDiffWorkList[i];
IRInst* diffFunc = nullptr;
IRBuilder subBuilder(*builder);
subBuilder.setInsertBefore(differentiateInst);
switch (differentiateInst->getOp())
{
case kIROp_ForwardDifferentiate:
{
auto baseFunc = as<IRForwardDifferentiate>(differentiateInst)->getBaseFn();
diffFunc = forwardTranscriber.transcribe(&subBuilder, baseFunc);
}
break;
case kIROp_BackwardDifferentiatePrimal:
{
auto baseFunc = differentiateInst->getOperand(0);
diffFunc = backwardPrimalTranscriber.transcribe(&subBuilder, baseFunc);
}
break;
case kIROp_BackwardDifferentiatePropagate:
{
auto baseFunc = differentiateInst->getOperand(0);
diffFunc = backwardPropagateTranscriber.transcribe(&subBuilder, baseFunc);
}
break;
case kIROp_BackwardDifferentiate:
{
auto baseFunc = differentiateInst->getOperand(0);
diffFunc = backwardTranscriber.transcribe(&subBuilder, baseFunc);
}
break;
default:
break;
}
if (diffFunc)
{
SLANG_ASSERT(diffFunc);
differentiateInst->replaceUsesWith(diffFunc);
differentiateInst->removeAndDeallocate();
changed = true;
}
}
// Run transcription logic to generate the body of forward/backward derivatives functions.
// While doing so, we may discover new functions to differentiate, so we keep running until
// the worklist goes dry.
List<IRFunc*> autodiffCleanupList;
while (autodiffContext->followUpFunctionsToTranscribe.getCount() != 0)
{
changed = true;
auto followUpWorkList = _Move(autodiffContext->followUpFunctionsToTranscribe);
for (auto task : followUpWorkList)
{
auto diffFunc = as<IRFunc>(task.resultFunc);
SLANG_ASSERT(diffFunc);
// We're running in to some situations where the follow-up task
// has already been completed (diffFunc has been generated, processed,
// and deallocated). Skip over these for now.
//
if (!diffFunc->getDataType())
continue;
auto primalFunc = as<IRFunc>(task.originalFunc);
SLANG_ASSERT(primalFunc);
switch (task.type)
{
case FuncBodyTranscriptionTaskType::Forward:
forwardTranscriber.transcribeFunc(builder, primalFunc, diffFunc);
break;
case FuncBodyTranscriptionTaskType::BackwardPrimal:
backwardPrimalTranscriber.transcribeFunc(builder, primalFunc, diffFunc);
break;
case FuncBodyTranscriptionTaskType::BackwardPropagate:
backwardPropagateTranscriber.transcribeFunc(builder, primalFunc, diffFunc);
break;
default:
break;
}
autodiffCleanupList.add(diffFunc);
}
}
// Get rid of block-level decorations that are used to keep track of
// different block types. These don't work well with the IR simplification
// passes since they don't expect decorations in blocks.
//
for (auto diffFunc : autodiffCleanupList)
stripTempDecorations(diffFunc);
autodiffCleanupList.clear();
#if _DEBUG
validateIRModule(module, sink);
#endif
if (!changed)
break;
hasChanges |= changed;
}
if (lowerIntermediateContextType(builder))
{
sharedBuilderStorage.deduplicateAndRebuildGlobalNumberingMap();
hasChanges = true;
}
return hasChanges;
}
IRStringLit* getDerivativeFuncName(IRInst* func, const char* postFix)
{
IRBuilder builder(&sharedBuilderStorage);
builder.setInsertBefore(func);
IRStringLit* name = nullptr;
if (auto linkageDecoration = func->findDecoration<IRLinkageDecoration>())
{
name = builder.getStringValue((String(linkageDecoration->getMangledName()) + postFix).getUnownedSlice());
}
else if (auto namehintDecoration = func->findDecoration<IRNameHintDecoration>())
{
name = builder.getStringValue((String(namehintDecoration->getName()) + postFix).getUnownedSlice());
}
return name;
}
IRStringLit* getForwardDerivativeFuncName(IRInst* func)
{
return getDerivativeFuncName(func, "_fwd_diff");
}
IRStringLit* getBackwardDerivativeFuncName(IRInst* func)
{
return getDerivativeFuncName(func, "_bwd_diff");
}
AutoDiffPass(AutoDiffSharedContext* context, DiagnosticSink* sink) :
InstPassBase(context->moduleInst->getModule()),
sink(sink),
forwardTranscriber(context, &sharedBuilderStorage, sink),
backwardPrimalTranscriber(context, &sharedBuilderStorage, sink),
backwardPropagateTranscriber(context, &sharedBuilderStorage, sink),
backwardTranscriber(context, &sharedBuilderStorage, sink),
pairBuilderStorage(context),
autodiffContext(context)
{
// We start by initializing our shared IR building state,
// since we will re-use that state for any code we
// generate along the way.
//
sharedBuilderStorage.init(module);
sharedBuilderStorage.deduplicateAndRebuildGlobalNumberingMap();
context->sharedBuilder = &sharedBuilderStorage;
forwardTranscriber.pairBuilder = &pairBuilderStorage;
backwardPrimalTranscriber.pairBuilder = &pairBuilderStorage;
backwardPropagateTranscriber.pairBuilder = &pairBuilderStorage;
backwardTranscriber.pairBuilder = &pairBuilderStorage;
// Make the transcribers available to all sub passes via shared context.
context->transcriberSet.primalTranscriber = &backwardPrimalTranscriber;
context->transcriberSet.propagateTranscriber = &backwardPropagateTranscriber;
context->transcriberSet.forwardTranscriber = &forwardTranscriber;
context->transcriberSet.backwardTranscriber = &backwardTranscriber;
}
protected:
// A transcriber object that handles the main job of
// processing instructions while maintaining state.
//
ForwardDiffTranscriber forwardTranscriber;
BackwardDiffPrimalTranscriber backwardPrimalTranscriber;
BackwardDiffPropagateTranscriber backwardPropagateTranscriber;
BackwardDiffTranscriber backwardTranscriber;
// Diagnostic object from the compile request for
// error messages.
DiagnosticSink* sink;
// Shared context.
AutoDiffSharedContext* autodiffContext;
// Builder for dealing with differential pair types.
DifferentialPairTypeBuilder pairBuilderStorage;
};
bool processAutodiffCalls(
IRModule* module,
DiagnosticSink* sink,
IRAutodiffPassOptions const&)
{
bool modified = false;
// Create shared context for all auto-diff related passes
AutoDiffSharedContext autodiffContext(module->getModuleInst());
AutoDiffPass pass(&autodiffContext, sink);
modified |= pass.processModule();
return modified;
}
bool finalizeAutoDiffPass(IRModule* module)
{
bool modified = false;
// Create shared context for all auto-diff related passes
AutoDiffSharedContext autodiffContext(module->getModuleInst());
SharedIRBuilder sharedBuilder;
sharedBuilder.init(module);
sharedBuilder.deduplicateAndRebuildGlobalNumberingMap();
autodiffContext.sharedBuilder = &sharedBuilder;
// Replaces IRDifferentialPairType with an auto-generated struct,
// IRDifferentialPairGetDifferential with 'differential' field access,
// IRDifferentialPairGetPrimal with 'primal' field access, and
// IRMakeDifferentialPair with an IRMakeStruct.
//
modified |= processPairTypes(&autodiffContext);
stripNoDiffTypeAttribute(module);
// Remove auto-diff related decorations.
stripAutoDiffDecorations(module);
return false;
}
}
|