-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathThunk.cpp
881 lines (800 loc) · 36.1 KB
/
Thunk.cpp
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
//===--- Thunk.cpp - Automatic differentiation thunks ---------*- C++ -*---===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2019 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Automatic differentiation thunk generation utilities.
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "differentiation"
#include "swift/SILOptimizer/Differentiation/Thunk.h"
#include "swift/SILOptimizer/Differentiation/Common.h"
#include "swift/AST/AnyFunctionRef.h"
#include "swift/AST/Requirement.h"
#include "swift/AST/SubstitutionMap.h"
#include "swift/AST/TypeCheckRequests.h"
#include "swift/Basic/Assertions.h"
#include "swift/SILOptimizer/Utils/SILOptFunctionBuilder.h"
#include "swift/SILOptimizer/Utils/DifferentiationMangler.h"
namespace swift {
namespace autodiff {
//===----------------------------------------------------------------------===//
// Thunk helpers
//===----------------------------------------------------------------------===//
// These helpers are copied/adapted from SILGen. They should be refactored and
// moved to a shared location.
//===----------------------------------------------------------------------===//
CanSILFunctionType buildThunkType(SILFunction *fn,
CanSILFunctionType &sourceType,
CanSILFunctionType &expectedType,
GenericEnvironment *&genericEnv,
SubstitutionMap &interfaceSubs,
bool withoutActuallyEscaping,
DifferentiationThunkKind thunkKind) {
CanType inputSubstType;
CanType outputSubstType;
CanType dynamicSelfType;
return buildSILFunctionThunkType(
fn, sourceType, expectedType, inputSubstType, outputSubstType, genericEnv,
interfaceSubs, dynamicSelfType, withoutActuallyEscaping, thunkKind);
}
/// Forward function arguments, handling ownership convention mismatches.
/// Adapted from `forwardFunctionArguments` in SILGenPoly.cpp.
///
/// Forwarded arguments are appended to `forwardedArgs`.
///
/// Local allocations are appended to `localAllocations`. They need to be
/// deallocated via `dealloc_stack`.
///
/// Local values requiring cleanup are appended to `valuesToCleanup`.
static void forwardFunctionArgumentsConvertingOwnership(
SILBuilder &builder, SILLocation loc, CanSILFunctionType fromTy,
CanSILFunctionType toTy, ArrayRef<SILArgument *> originalArgs,
SmallVectorImpl<SILValue> &forwardedArgs,
SmallVectorImpl<AllocStackInst *> &localAllocations,
SmallVectorImpl<SILValue> &valuesToCleanup) {
auto fromParameters = fromTy->getParameters();
auto toParameters = toTy->getParameters();
assert(fromParameters.size() == toParameters.size());
assert(fromParameters.size() == originalArgs.size());
for (auto index : indices(originalArgs)) {
auto &arg = originalArgs[index];
auto fromParam = fromParameters[index];
auto toParam = toParameters[index];
// To convert guaranteed argument to be owned, create a copy.
if (fromParam.isConsumedInCaller() && !toParam.isConsumedInCallee()) {
// If the argument has an object type, create a `copy_value`.
if (arg->getType().isObject()) {
auto argCopy = builder.emitCopyValueOperation(loc, arg);
forwardedArgs.push_back(argCopy);
continue;
}
// If the argument has an address type, create a local allocation and
// `copy_addr` its contents to the local allocation.
auto *alloc = builder.createAllocStack(loc, arg->getType());
builder.createCopyAddr(loc, arg, alloc, IsNotTake, IsInitialization);
localAllocations.push_back(alloc);
forwardedArgs.push_back(alloc);
continue;
}
// To convert owned argument to be guaranteed, borrow the argument.
if (fromParam.isGuaranteedInCaller() && !toParam.isGuaranteedInCaller()) {
auto bbi = builder.emitBeginBorrowOperation(loc, arg);
forwardedArgs.push_back(bbi);
valuesToCleanup.push_back(bbi);
valuesToCleanup.push_back(arg);
continue;
}
// Otherwise, simply forward the argument.
forwardedArgs.push_back(arg);
}
}
SILFunction *getOrCreateReabstractionThunk(SILOptFunctionBuilder &fb,
SILModule &module, SILLocation loc,
SILFunction *caller,
CanSILFunctionType fromType,
CanSILFunctionType toType) {
assert(!fromType->getCombinedSubstitutions());
assert(!toType->getCombinedSubstitutions());
SubstitutionMap interfaceSubs;
GenericEnvironment *genericEnv = nullptr;
auto thunkType =
buildThunkType(caller, fromType, toType, genericEnv, interfaceSubs,
/*withoutActuallyEscaping*/ false,
DifferentiationThunkKind::Reabstraction);
auto thunkDeclType =
thunkType->getWithExtInfo(thunkType->getExtInfo().withNoEscape(false));
auto fromInterfaceType = fromType->mapTypeOutOfContext()->getCanonicalType();
auto toInterfaceType = toType->mapTypeOutOfContext()->getCanonicalType();
Mangle::ASTMangler mangler(module.getASTContext());
std::string name = mangler.mangleReabstractionThunkHelper(
thunkType, fromInterfaceType, toInterfaceType, Type(), Type(),
module.getSwiftModule());
auto *thunk = fb.getOrCreateSharedFunction(
loc, name, thunkDeclType, IsBare, IsTransparent, IsSerialized,
ProfileCounter(), IsReabstractionThunk, IsNotDynamic, IsNotDistributed,
IsNotRuntimeAccessible);
if (!thunk->empty())
return thunk;
thunk->setGenericEnvironment(genericEnv);
auto *entry = thunk->createBasicBlock();
SILBuilder builder(entry);
createEntryArguments(thunk);
SILFunctionConventions fromConv(fromType, module);
SILFunctionConventions toConv(toType, module);
assert(toConv.useLoweredAddresses());
// Forward thunk arguments, handling ownership convention mismatches.
SmallVector<SILValue, 4> forwardedArgs;
for (auto indRes : thunk->getIndirectResults())
forwardedArgs.push_back(indRes);
SmallVector<AllocStackInst *, 4> localAllocations;
SmallVector<SILValue, 4> valuesToCleanup;
forwardFunctionArgumentsConvertingOwnership(
builder, loc, fromType, toType,
thunk->getArgumentsWithoutIndirectResults().drop_back(), forwardedArgs,
localAllocations, valuesToCleanup);
SmallVector<SILValue, 4> arguments;
auto toArgIter = forwardedArgs.begin();
auto useNextArgument = [&]() { arguments.push_back(*toArgIter++); };
auto createAllocStack = [&](SILType type) {
auto *alloc = builder.createAllocStack(loc, type);
localAllocations.push_back(alloc);
return alloc;
};
// Handle indirect results.
assert(fromType->getNumResults() == toType->getNumResults());
for (unsigned resIdx : range(toType->getNumResults())) {
auto fromRes = fromConv.getResults()[resIdx];
auto toRes = toConv.getResults()[resIdx];
// No abstraction mismatch.
if (fromRes.isFormalIndirect() == toRes.isFormalIndirect()) {
// If result types are indirect, directly pass as next argument.
if (toRes.isFormalIndirect())
useNextArgument();
continue;
}
// Convert indirect result to direct result.
if (fromRes.isFormalIndirect()) {
SILType resultTy =
fromConv.getSILType(fromRes, builder.getTypeExpansionContext());
assert(resultTy.isAddress());
auto *indRes = createAllocStack(resultTy);
arguments.push_back(indRes);
continue;
}
// Convert direct result to indirect result.
// Increment thunk argument iterator; reabstraction handled later.
++toArgIter;
}
// Reabstract parameters.
assert(toType->getNumParameters() == fromType->getNumParameters());
for (unsigned paramIdx : range(toType->getNumParameters())) {
auto fromParam = fromConv.getParameters()[paramIdx];
auto toParam = toConv.getParameters()[paramIdx];
// No abstraction mismatch. Directly use next argument.
if (fromParam.isFormalIndirect() == toParam.isFormalIndirect()) {
useNextArgument();
continue;
}
// Convert indirect parameter to direct parameter.
if (fromParam.isFormalIndirect()) {
auto paramTy = fromConv.getSILType(fromType->getParameters()[paramIdx],
builder.getTypeExpansionContext());
if (!paramTy.hasArchetype())
paramTy = thunk->mapTypeIntoContext(paramTy);
assert(paramTy.isAddress());
auto toArg = *toArgIter++;
auto *buf = createAllocStack(toArg->getType());
toArg = builder.emitCopyValueOperation(loc, toArg);
builder.emitStoreValueOperation(loc, toArg, buf,
StoreOwnershipQualifier::Init);
valuesToCleanup.push_back(buf);
arguments.push_back(buf);
continue;
}
// Convert direct parameter to indirect parameter.
assert(toParam.isFormalIndirect());
auto toArg = *toArgIter++;
auto load = builder.emitLoadBorrowOperation(loc, toArg);
if (isa<LoadBorrowInst>(load))
valuesToCleanup.push_back(load);
arguments.push_back(load);
}
auto *fnArg = thunk->getArgumentsWithoutIndirectResults().back();
auto *apply = builder.createApply(loc, fnArg, SubstitutionMap(), arguments);
// Get return elements.
SmallVector<SILValue, 4> results;
// Extract all direct results.
SmallVector<SILValue, 4> directResults;
extractAllElements(apply, builder, directResults);
auto fromDirResultsIter = directResults.begin();
auto fromIndResultsIter = apply->getIndirectSILResults().begin();
auto toIndResultsIter = thunk->getIndirectResults().begin();
// Reabstract results.
for (unsigned resIdx : range(toType->getNumResults())) {
auto fromRes = fromConv.getResults()[resIdx];
auto toRes = toConv.getResults()[resIdx];
// Check function-typed results.
if (isa<SILFunctionType>(fromRes.getInterfaceType()) &&
isa<SILFunctionType>(toRes.getInterfaceType())) {
auto fromFnType = cast<SILFunctionType>(fromRes.getInterfaceType());
auto toFnType = cast<SILFunctionType>(toRes.getInterfaceType());
auto fromUnsubstFnType = fromFnType->getUnsubstitutedType(module);
auto toUnsubstFnType = toFnType->getUnsubstitutedType(module);
// If unsubstituted function types are not equal, perform reabstraction.
if (fromUnsubstFnType != toUnsubstFnType) {
auto fromFn = *fromDirResultsIter++;
auto newFromFn = reabstractFunction(
builder, fb, loc, fromFn, toFnType,
[](SubstitutionMap substMap) { return substMap; });
results.push_back(newFromFn);
continue;
}
}
// No abstraction mismatch.
if (fromRes.isFormalIndirect() == toRes.isFormalIndirect()) {
// If result types are direct, add call result as direct thunk result.
if (toRes.isFormalDirect())
results.push_back(*fromDirResultsIter++);
// If result types are indirect, increment indirect result iterators.
else {
++fromIndResultsIter;
++toIndResultsIter;
}
continue;
}
// Load direct results from indirect results.
if (fromRes.isFormalIndirect()) {
auto indRes = *fromIndResultsIter++;
auto load = builder.emitLoadValueOperation(loc, indRes,
LoadOwnershipQualifier::Take);
results.push_back(load);
continue;
}
// Store direct results to indirect results.
assert(toRes.isFormalIndirect());
#ifndef NDEBUG
SILType resultTy =
toConv.getSILType(toRes, builder.getTypeExpansionContext());
assert(resultTy.isAddress());
#endif
auto indRes = *toIndResultsIter++;
auto dirRes = *fromDirResultsIter++;
builder.emitStoreValueOperation(loc, dirRes, indRes,
StoreOwnershipQualifier::Init);
}
auto retVal = joinElements(results, builder, loc);
// Clean up local values.
// Guaranteed values need an `end_borrow`.
// Owned values need to be destroyed.
for (auto arg : valuesToCleanup) {
switch (arg->getOwnershipKind()) {
case OwnershipKind::Any:
llvm_unreachable("value with any ownership kind?!");
case OwnershipKind::Guaranteed:
builder.emitEndBorrowOperation(loc, arg);
break;
case OwnershipKind::Owned:
case OwnershipKind::Unowned:
case OwnershipKind::None:
builder.emitDestroyOperation(loc, arg);
break;
}
}
// Deallocate local allocations.
for (auto *alloc : llvm::reverse(localAllocations))
builder.createDeallocStack(loc, alloc);
// Create return.
builder.createReturn(loc, retVal);
LLVM_DEBUG(auto &s = getADDebugStream() << "Created reabstraction thunk.\n";
s << " From type: " << fromType << '\n';
s << " To type: " << toType << '\n'; s << '\n'
<< *thunk);
return thunk;
}
// FIXME: This is pretty rudimentary as of now as there is no proper AST type
// for coroutine and therefore we cannot e.g. store a coroutine into a tuple or
// do other things that are allowed with first-class function types. For now we
// have to unsafely bitcast coroutine to function type and vice versa. This
// function should be rethought when we will have proper AST coroutine types.
SILValue reabstractCoroutine(
SILBuilder &builder, SILOptFunctionBuilder &fb, SILLocation loc,
SILValue fn, CanSILFunctionType toType,
std::function<SubstitutionMap(SubstitutionMap)> remapSubstitutions) {
auto &module = *fn->getModule();
auto fromType = fn->getType().getAs<SILFunctionType>();
auto unsubstFromType = fromType->getUnsubstitutedType(module);
auto unsubstToType = toType->getUnsubstitutedType(module);
LLVM_DEBUG(auto &s = getADDebugStream() << "Converting coroutine\n";
s << " From type: " << fromType << '\n';
s << " To type: " << toType << '\n'; s << '\n');
if (fromType != unsubstFromType)
fn = builder.createConvertFunction(
loc, fn, SILType::getPrimitiveObjectType(unsubstFromType),
/*withoutActuallyEscaping*/ false);
fn = builder.createConvertFunction(loc, fn,
SILType::getPrimitiveObjectType(unsubstToType),
/*withoutActuallyEscaping*/ false);
if (toType != unsubstToType)
fn = builder.createConvertFunction(loc, fn,
SILType::getPrimitiveObjectType(toType),
/*withoutActuallyEscaping*/ false);
return fn;
}
SILValue reabstractFunction(
SILBuilder &builder, SILOptFunctionBuilder &fb, SILLocation loc,
SILValue fn, CanSILFunctionType toType,
std::function<SubstitutionMap(SubstitutionMap)> remapSubstitutions) {
auto &module = *fn->getModule();
auto fromType = fn->getType().getAs<SILFunctionType>();
auto unsubstFromType = fromType->getUnsubstitutedType(module);
auto unsubstToType = toType->getUnsubstitutedType(module);
auto *thunk = getOrCreateReabstractionThunk(fb, module, loc,
/*caller*/ fn->getFunction(),
unsubstFromType, unsubstToType);
auto *thunkRef = builder.createFunctionRef(loc, thunk);
if (fromType != unsubstFromType)
fn = builder.createConvertFunction(
loc, fn, SILType::getPrimitiveObjectType(unsubstFromType),
/*withoutActuallyEscaping*/ false);
fn = builder.createPartialApply(
loc, thunkRef, remapSubstitutions(thunk->getForwardingSubstitutionMap()),
{fn}, fromType->getCalleeConvention());
if (toType != unsubstToType)
fn = builder.createConvertFunction(loc, fn,
SILType::getPrimitiveObjectType(toType),
/*withoutActuallyEscaping*/ false);
return fn;
}
std::pair<SILFunction *, SubstitutionMap>
getOrCreateSubsetParametersThunkForLinearMap(
SILOptFunctionBuilder &fb, SILFunction *parentThunk,
CanSILFunctionType origFnType, CanSILFunctionType linearMapType,
CanSILFunctionType targetType, AutoDiffDerivativeFunctionKind kind,
const AutoDiffConfig &desiredConfig, const AutoDiffConfig &actualConfig,
ADContext &adContext) {
LLVM_DEBUG(getADDebugStream()
<< "Getting a subset parameters thunk for "
<< (kind == AutoDiffDerivativeFunctionKind::JVP ? "jvp" : "vjp")
<< " linear map " << linearMapType
<< " from " << actualConfig << " to " << desiredConfig << '\n');
assert(!linearMapType->getCombinedSubstitutions());
assert(!targetType->getCombinedSubstitutions());
SubstitutionMap interfaceSubs;
GenericEnvironment *genericEnv = nullptr;
auto thunkType = buildThunkType(parentThunk, linearMapType, targetType,
genericEnv, interfaceSubs,
/*withoutActuallyEscaping*/ true,
DifferentiationThunkKind::Reabstraction);
Mangle::DifferentiationMangler mangler(parentThunk->getASTContext());
auto fromInterfaceType =
linearMapType->mapTypeOutOfContext()->getCanonicalType();
auto thunkName = mangler.mangleLinearMapSubsetParametersThunk(
fromInterfaceType, kind.getLinearMapKind(),
actualConfig.parameterIndices, actualConfig.resultIndices,
desiredConfig.parameterIndices);
auto loc = parentThunk->getLocation();
auto *thunk = fb.getOrCreateSharedFunction(
loc, thunkName, thunkType, IsBare, IsTransparent, IsSerialized,
ProfileCounter(), IsThunk, IsNotDynamic, IsNotDistributed,
IsNotRuntimeAccessible);
if (!thunk->empty())
return {thunk, interfaceSubs};
thunk->setGenericEnvironment(genericEnv);
auto *entry = thunk->createBasicBlock();
TangentBuilder builder(entry, adContext);
createEntryArguments(thunk);
// Get arguments.
SmallVector<SILValue, 4> arguments;
SmallVector<AllocStackInst *, 4> localAllocations;
SmallVector<SILValue, 4> valuesToCleanup;
auto cleanupValues = [&]() {
for (auto value : llvm::reverse(valuesToCleanup))
builder.emitDestroyOperation(loc, value);
for (auto *alloc : llvm::reverse(localAllocations))
builder.createDeallocStack(loc, alloc);
};
// Build a `.zero` argument for the given `Differentiable`-conforming type.
auto buildZeroArgument = [&](SILParameterInfo zeroSILParameter) {
auto zeroSILType = zeroSILParameter.getSILStorageInterfaceType();
auto zeroSILObjType = zeroSILType.getObjectType();
auto zeroType = zeroSILType.getASTType();
auto tangentSpace =
zeroType->getAutoDiffTangentSpace(LookUpConformanceInModule());
assert(tangentSpace && "No tangent space for this type");
switch (tangentSpace->getKind()) {
case TangentSpace::Kind::TangentVector: {
auto *buf = builder.createAllocStack(loc, zeroSILObjType);
localAllocations.push_back(buf);
builder.emitZeroIntoBuffer(loc, buf, IsInitialization);
if (zeroSILType.isAddress()) {
arguments.push_back(buf);
if (zeroSILParameter.isGuaranteedInCaller()) {
valuesToCleanup.push_back(buf);
}
} else {
auto arg = builder.emitLoadValueOperation(loc, buf,
LoadOwnershipQualifier::Take);
arguments.push_back(arg);
if (zeroSILParameter.isGuaranteedInCaller()) {
valuesToCleanup.push_back(arg);
}
}
break;
}
case TangentSpace::Kind::Tuple: {
llvm_unreachable("Unimplemented: Handle zero initialization for tuples");
}
}
};
// The indices in `actualConfig` and `desiredConfig` are with respect to the
// original function. However, the differential parameters and pullback
// results may already be w.r.t. a subset. We create a map between the
// original function's actual parameter indices and the linear map's actual
// indices.
// Example:
// Original: (T0, T1, T2) -> R
// Actual indices: 0, 2
// Original differential: (T0, T2) -> R
// Original pullback: R -> (T0, T2)
// Desired indices w.r.t. original: 2
// Desired indices w.r.t. linear map: 1
SmallVector<unsigned, 4> actualParamIndicesMap(
actualConfig.parameterIndices->getCapacity(), UINT_MAX);
{
unsigned indexInBitVec = 0;
for (auto index : actualConfig.parameterIndices->getIndices()) {
actualParamIndicesMap[index] = indexInBitVec;
++indexInBitVec;
}
}
auto mapOriginalParameterIndex = [&](unsigned index) -> unsigned {
auto mappedIndex = actualParamIndicesMap[index];
assert(mappedIndex < actualConfig.parameterIndices->getCapacity());
return mappedIndex;
};
auto toIndirectResultsIter = thunk->getIndirectResults().begin();
auto useNextIndirectResult = [&]() {
assert(toIndirectResultsIter != thunk->getIndirectResults().end());
arguments.push_back(*toIndirectResultsIter++);
};
switch (kind) {
// Differential arguments are:
// - All indirect results, followed by:
// - An interleaving of:
// - Thunk arguments (when parameter index is in both desired and actual
// indices).
// - Zeros (when parameter is not in desired indices).
case AutoDiffDerivativeFunctionKind::JVP: {
unsigned numIndirectResults = linearMapType->getNumIndirectFormalResults();
// Forward desired indirect results
for (unsigned idx : *actualConfig.resultIndices) {
if (idx >= numIndirectResults)
break;
auto resultInfo = linearMapType->getResults()[idx];
assert(idx < linearMapType->getNumResults());
// Forward result argument in case we do not need to thunk it away
if (desiredConfig.resultIndices->contains(idx)) {
useNextIndirectResult();
continue;
}
// Otherwise, allocate and use an uninitialized indirect result
auto *indirectResult = builder.createAllocStack(
loc, resultInfo.getSILStorageInterfaceType());
localAllocations.push_back(indirectResult);
arguments.push_back(indirectResult);
}
assert(toIndirectResultsIter == thunk->getIndirectResults().end());
auto toArgIter = thunk->getArgumentsWithoutIndirectResults().begin();
auto useNextArgument = [&]() { arguments.push_back(*toArgIter++); };
// Iterate over actual indices.
for (unsigned i : actualConfig.parameterIndices->getIndices()) {
// If index is desired, use next argument.
if (desiredConfig.isWrtParameter(i)) {
useNextArgument();
}
// Otherwise, construct and use a zero argument.
else {
auto zeroSILParameter =
linearMapType->getParameters()[mapOriginalParameterIndex(i)];
buildZeroArgument(zeroSILParameter);
}
}
break;
}
// Pullback arguments are:
// - An interleaving of:
// - Thunk indirect results (when parameter index is in both desired and
// actual indices).
// - Zeros (when parameter is not in desired indices).
// - All actual arguments.
case AutoDiffDerivativeFunctionKind::VJP: {
// Collect pullback arguments.
unsigned pullbackResultIndex = 0;
for (unsigned i : actualConfig.parameterIndices->getIndices()) {
auto origParamInfo = origFnType->getParameters()[i];
// Skip original semantic result parameters. All non-indirect-result pullback
// arguments (including semantic result` arguments) are appended to `arguments`
// later.
if (origParamInfo.isAutoDiffSemanticResult())
continue;
auto resultInfo = linearMapType->getResults()[pullbackResultIndex];
assert(pullbackResultIndex < linearMapType->getNumResults());
++pullbackResultIndex;
// Skip pullback direct results. Only indirect results are relevant as
// arguments.
if (resultInfo.isFormalDirect())
continue;
// If index is desired, use next pullback indirect result.
if (desiredConfig.isWrtParameter(i)) {
useNextIndirectResult();
continue;
}
// Otherwise, allocate and use an uninitialized pullback indirect result.
auto *indirectResult = builder.createAllocStack(
loc, resultInfo.getSILStorageInterfaceType());
localAllocations.push_back(indirectResult);
arguments.push_back(indirectResult);
}
// Forward all actual non-indirect-result arguments.
auto thunkArgs = thunk->getArgumentsWithoutIndirectResults();
// Slice out the function to be called
thunkArgs = thunkArgs.slice(0, thunkArgs.size() - 1);
unsigned thunkArg = 0;
for (unsigned idx : *actualConfig.resultIndices) {
// Forward result argument in case we do not need to thunk it away
if (desiredConfig.resultIndices->contains(idx))
arguments.push_back(thunkArgs[thunkArg++]);
else // otherwise, zero it out
buildZeroArgument(linearMapType->getParameters()[arguments.size()]);
}
break;
}
}
// Get the linear map thunk argument and apply it.
auto *linearMap = thunk->getArguments().back();
auto *ai = builder.createApply(loc, linearMap, SubstitutionMap(), arguments);
// If differential thunk, deallocate local allocations and directly return
// `apply` result (if it is desired).
if (kind == AutoDiffDerivativeFunctionKind::JVP) {
SmallVector<SILValue, 8> differentialDirectResults;
extractAllElements(ai, builder, differentialDirectResults);
SmallVector<SILValue, 8> allResults;
collectAllActualResultsInTypeOrder(ai, differentialDirectResults, allResults);
unsigned numResults = thunk->getConventions().getNumDirectSILResults() +
thunk->getConventions().getNumDirectSILResults();
SmallVector<SILValue, 8> results;
for (unsigned idx : *actualConfig.resultIndices) {
if (idx >= numResults)
break;
auto result = allResults[idx];
if (desiredConfig.isWrtResult(idx))
results.push_back(result);
else {
if (result->getType().isAddress())
builder.emitDestroyAddrAndFold(loc, result);
else
builder.emitDestroyValueOperation(loc, result);
}
}
cleanupValues();
auto result = joinElements(results, builder, loc);
builder.createReturn(loc, result);
return {thunk, interfaceSubs};
}
// If pullback thunk, return only the desired results and clean up the
// undesired results.
SmallVector<SILValue, 8> pullbackDirectResults;
extractAllElements(ai, builder, pullbackDirectResults);
SmallVector<SILValue, 8> allResults;
collectAllActualResultsInTypeOrder(ai, pullbackDirectResults, allResults);
// Collect pullback semantic result arguments in type order.
unsigned semanticResultArgIdx = 0;
SILFunctionConventions origConv(origFnType, thunk->getModule());
for (auto paramIdx : actualConfig.parameterIndices->getIndices()) {
auto paramInfo = origConv.getParameters()[paramIdx];
if (!paramInfo.isAutoDiffSemanticResult())
continue;
auto semanticResultArg =
*std::next(ai->getAutoDiffSemanticResultArguments().begin(),
semanticResultArgIdx++);
unsigned mappedParamIdx = mapOriginalParameterIndex(paramIdx);
allResults.insert(allResults.begin() + mappedParamIdx, semanticResultArg);
}
assert(allResults.size() == actualConfig.parameterIndices->getNumIndices() &&
"Number of pullback results should match number of differentiability "
"parameters");
SmallVector<SILValue, 8> results;
for (unsigned i : actualConfig.parameterIndices->getIndices()) {
unsigned mappedIndex = mapOriginalParameterIndex(i);
// If result is desired:
// - Do nothing if result is indirect.
// (It was already forwarded to the `apply` instruction).
// - Push it to `results` if result is direct.
auto result = allResults[mappedIndex];
if (desiredConfig.isWrtParameter(i)) {
if (result->getType().isObject())
results.push_back(result);
}
// Otherwise, cleanup the unused results.
else {
if (result->getType().isAddress())
builder.emitDestroyAddrAndFold(loc, result);
else
builder.emitDestroyValueOperation(loc, result);
}
}
// Deallocate local allocations and return final direct result.
cleanupValues();
auto result = joinElements(results, builder, loc);
builder.createReturn(loc, result);
return {thunk, interfaceSubs};
}
std::pair<SILFunction *, SubstitutionMap>
getOrCreateSubsetParametersThunkForDerivativeFunction(
SILOptFunctionBuilder &fb, SILValue origFnOperand, SILValue derivativeFn,
AutoDiffDerivativeFunctionKind kind, const AutoDiffConfig &desiredConfig,
const AutoDiffConfig &actualConfig, ADContext &adContext) {
LLVM_DEBUG(getADDebugStream()
<< "Getting a subset parameters thunk for derivative "
<< (kind == AutoDiffDerivativeFunctionKind::JVP ? "jvp" : "vjp")
<< " function " << derivativeFn
<< " of the original function " << origFnOperand
<< " from " << actualConfig << " to " << desiredConfig << '\n');
auto origFnType = origFnOperand->getType().castTo<SILFunctionType>();
auto &module = fb.getModule();
auto lookupConformance = LookUpConformanceInModule();
// Compute target type for thunking.
auto derivativeFnType = derivativeFn->getType().castTo<SILFunctionType>();
auto targetType = origFnType->getAutoDiffDerivativeFunctionType(
desiredConfig.parameterIndices, desiredConfig.resultIndices, kind,
module.Types, lookupConformance);
auto *caller = derivativeFn->getFunction();
if (targetType->hasArchetype()) {
auto substTargetType =
caller->mapTypeIntoContext(targetType->mapTypeOutOfContext())
->getCanonicalType();
targetType = SILType::getPrimitiveObjectType(substTargetType)
.castTo<SILFunctionType>();
}
assert(derivativeFnType->getNumParameters() ==
targetType->getNumParameters());
assert(derivativeFnType->getNumResults() == targetType->getNumResults());
// Build thunk type.
SubstitutionMap interfaceSubs;
GenericEnvironment *genericEnv = nullptr;
auto thunkType = buildThunkType(derivativeFn->getFunction(), derivativeFnType,
targetType, genericEnv, interfaceSubs,
/*withoutActuallyEscaping*/ false,
DifferentiationThunkKind::IndexSubset);
// FIXME: The logic for resolving `assocRef` does not reapply function
// conversions, which is problematic if `derivativeFn` is a `partial_apply`
// instruction.
StringRef origName;
if (auto *origFnRef =
peerThroughFunctionConversions<FunctionRefInst>(origFnOperand)) {
origName = origFnRef->getReferencedFunction()->getName();
} else if (auto *origMethodInst =
peerThroughFunctionConversions<MethodInst>(origFnOperand)) {
origName = origMethodInst->getMember()
.getAnyFunctionRef()
->getAbstractFunctionDecl()
->getNameStr();
}
assert(!origName.empty() && "Original function name could not be resolved");
Mangle::DifferentiationMangler mangler(adContext.getASTContext());
auto thunkName = mangler.mangleDerivativeFunctionSubsetParametersThunk(
origName, targetType->mapTypeOutOfContext()->getCanonicalType(),
kind, actualConfig.parameterIndices, actualConfig.resultIndices,
desiredConfig.parameterIndices);
auto loc = origFnOperand.getLoc();
auto *thunk = fb.getOrCreateSharedFunction(
loc, thunkName, thunkType, IsBare, IsTransparent,
caller->getSerializedKind(), ProfileCounter(), IsThunk, IsNotDynamic,
IsNotDistributed, IsNotRuntimeAccessible);
if (!thunk->empty())
return {thunk, interfaceSubs};
thunk->setGenericEnvironment(genericEnv);
auto *entry = thunk->createBasicBlock();
SILBuilder builder(entry);
createEntryArguments(thunk);
SubstitutionMap assocSubstMap;
if (auto *partialApply = dyn_cast<PartialApplyInst>(derivativeFn))
assocSubstMap = partialApply->getSubstitutionMap();
// FIXME: The logic for resolving `assocRef` does not reapply function
// conversions, which is problematic if `derivativeFn` is a `partial_apply`
// instruction.
SILValue assocRef;
if (auto *derivativeFnRef =
peerThroughFunctionConversions<FunctionRefInst>(derivativeFn)) {
auto *assoc = derivativeFnRef->getReferencedFunction();
assocRef = builder.createFunctionRef(loc, assoc);
} else if (auto *assocMethodInst =
peerThroughFunctionConversions<WitnessMethodInst>(
derivativeFn)) {
assocRef = builder.createWitnessMethod(
loc, assocMethodInst->getLookupType(),
assocMethodInst->getConformance(), assocMethodInst->getMember(),
thunk->mapTypeIntoContext(assocMethodInst->getType()));
} else if (auto *assocMethodInst =
peerThroughFunctionConversions<ClassMethodInst>(
derivativeFn)) {
auto classOperand = thunk->getArgumentsWithoutIndirectResults().back();
#ifndef NDEBUG
auto classOperandType = assocMethodInst->getOperand()->getType();
assert(classOperand->getType() == classOperandType);
#endif
assocRef = builder.createClassMethod(
loc, classOperand, assocMethodInst->getMember(),
thunk->mapTypeIntoContext(assocMethodInst->getType()));
} else if (auto *diffWitFn = peerThroughFunctionConversions<
DifferentiabilityWitnessFunctionInst>(derivativeFn)) {
assocRef = builder.createDifferentiabilityWitnessFunction(
loc, diffWitFn->getWitnessKind(), diffWitFn->getWitness());
}
assert(assocRef && "Expected derivative function to be resolved");
assocSubstMap = assocSubstMap.subst(thunk->getForwardingSubstitutionMap());
derivativeFnType = assocRef->getType().castTo<SILFunctionType>();
SmallVector<SILValue, 4> arguments;
arguments.append(thunk->getArguments().begin(), thunk->getArguments().end());
assert(arguments.size() ==
derivativeFnType->getNumParameters() +
derivativeFnType->getNumIndirectFormalResults());
auto *apply = builder.createApply(loc, assocRef, assocSubstMap, arguments);
// Extract all direct results.
SmallVector<SILValue, 8> directResults;
extractAllElements(apply, builder, directResults);
auto linearMap = directResults.back();
directResults.pop_back();
auto linearMapType = linearMap->getType().castTo<SILFunctionType>();
auto linearMapTargetType = targetType->getResults()
.back()
.getSILStorageInterfaceType()
.castTo<SILFunctionType>();
auto unsubstLinearMapType = linearMapType->getUnsubstitutedType(module);
auto unsubstLinearMapTargetType =
linearMapTargetType->getUnsubstitutedType(module);
SILFunction *linearMapThunk;
SubstitutionMap linearMapSubs;
std::tie(linearMapThunk, linearMapSubs) =
getOrCreateSubsetParametersThunkForLinearMap(
fb, thunk, origFnType, unsubstLinearMapType,
unsubstLinearMapTargetType, kind, desiredConfig, actualConfig,
adContext);
auto *linearMapThunkFRI = builder.createFunctionRef(loc, linearMapThunk);
SILValue thunkedLinearMap = linearMap;
if (linearMapType != unsubstLinearMapType) {
thunkedLinearMap = builder.createConvertFunction(
loc, thunkedLinearMap,
SILType::getPrimitiveObjectType(unsubstLinearMapType),
/*withoutActuallyEscaping*/ false);
}
thunkedLinearMap = builder.createPartialApply(
loc, linearMapThunkFRI, linearMapSubs, {thunkedLinearMap},
ParameterConvention::Direct_Guaranteed);
if (linearMapTargetType != unsubstLinearMapTargetType) {
thunkedLinearMap = builder.createConvertFunction(
loc, thunkedLinearMap,
SILType::getPrimitiveObjectType(linearMapTargetType),
/*withoutActuallyEscaping*/ false);
}
assert(origFnType->getNumAutoDiffSemanticResults() > 0);
if (origFnType->getNumResults() > 0 &&
origFnType->getResults().front().isFormalDirect()) {
directResults.push_back(thunkedLinearMap);
auto result = joinElements(directResults, builder, loc);
builder.createReturn(loc, result);
} else {
builder.createReturn(loc, thunkedLinearMap);
}
return {thunk, interfaceSubs};
}
} // end namespace autodiff
} // end namespace swift