|
22 | 22 | #include "swift/SILOptimizer/Differentiation/DifferentiationInvoker.h" |
23 | 23 | #include "swift/SILOptimizer/Differentiation/LinearMapInfo.h" |
24 | 24 |
|
25 | | -#include "swift/SIL/TypeSubstCloner.h" |
26 | | -#include "llvm/ADT/DenseMap.h" |
27 | | - |
28 | 25 | namespace swift { |
29 | | - |
30 | | -class SILDifferentiabilityWitness; |
31 | | -class SILBasicBlock; |
32 | | -class SILFunction; |
33 | | -class SILInstruction; |
34 | | - |
35 | 26 | namespace autodiff { |
36 | 27 |
|
37 | 28 | class ADContext; |
38 | 29 | class PullbackCloner; |
39 | 30 |
|
40 | | -class VJPCloner final |
41 | | - : public TypeSubstCloner<VJPCloner, SILOptFunctionBuilder> { |
42 | | - friend class PullbackCloner; |
43 | | - |
44 | | -private: |
45 | | - /// The global context. |
46 | | - ADContext &context; |
47 | | - |
48 | | - /// The original function. |
49 | | - SILFunction *const original; |
50 | | - |
51 | | - /// The differentiability witness. |
52 | | - SILDifferentiabilityWitness *const witness; |
53 | | - |
54 | | - /// The VJP function. |
55 | | - SILFunction *const vjp; |
56 | | - |
57 | | - /// The pullback function. |
58 | | - SILFunction *pullback; |
59 | | - |
60 | | - /// The differentiation invoker. |
61 | | - DifferentiationInvoker invoker; |
62 | | - |
63 | | - /// Info from activity analysis on the original function. |
64 | | - const DifferentiableActivityInfo &activityInfo; |
65 | | - |
66 | | - /// The linear map info. |
67 | | - LinearMapInfo pullbackInfo; |
68 | | - |
69 | | - /// Caches basic blocks whose phi arguments have been remapped (adding a |
70 | | - /// predecessor enum argument). |
71 | | - SmallPtrSet<SILBasicBlock *, 4> remappedBasicBlocks; |
72 | | - |
73 | | - bool errorOccurred = false; |
74 | | - |
75 | | - /// Mapping from original blocks to pullback values. Used to build pullback |
76 | | - /// struct instances. |
77 | | - llvm::DenseMap<SILBasicBlock *, SmallVector<SILValue, 8>> pullbackValues; |
78 | | - |
79 | | - ASTContext &getASTContext() const { return vjp->getASTContext(); } |
80 | | - SILModule &getModule() const { return vjp->getModule(); } |
81 | | - const SILAutoDiffIndices getIndices() const { |
82 | | - return witness->getSILAutoDiffIndices(); |
83 | | - } |
84 | | - |
85 | | - static SubstitutionMap getSubstitutionMap(SILFunction *original, |
86 | | - SILFunction *vjp); |
87 | | - |
88 | | - static const DifferentiableActivityInfo & |
89 | | - getActivityInfo(ADContext &context, SILFunction *original, |
90 | | - SILAutoDiffIndices indices, SILFunction *vjp); |
| 31 | +/// A helper class for generating VJP functions. |
| 32 | +class VJPCloner final { |
| 33 | + class Implementation; |
| 34 | + Implementation &impl; |
91 | 35 |
|
92 | 36 | public: |
| 37 | + /// Creates a VJP cloner. |
| 38 | + /// |
| 39 | + /// The parent VJP cloner stores the original function and an empty |
| 40 | + /// to-be-generated pullback function. |
93 | 41 | explicit VJPCloner(ADContext &context, SILFunction *original, |
94 | 42 | SILDifferentiabilityWitness *witness, SILFunction *vjp, |
95 | 43 | DifferentiationInvoker invoker); |
96 | | - |
97 | | - SILFunction *createEmptyPullback(); |
98 | | - |
99 | | - /// Run VJP generation. Returns true on error. |
| 44 | + ~VJPCloner(); |
| 45 | + |
| 46 | + ADContext &getContext() const; |
| 47 | + SILModule &getModule() const; |
| 48 | + SILFunction &getOriginal() const; |
| 49 | + SILFunction &getVJP() const; |
| 50 | + SILFunction &getPullback() const; |
| 51 | + SILDifferentiabilityWitness *getWitness() const; |
| 52 | + const SILAutoDiffIndices getIndices() const; |
| 53 | + DifferentiationInvoker getInvoker() const; |
| 54 | + LinearMapInfo &getPullbackInfo() const; |
| 55 | + const DifferentiableActivityInfo &getActivityInfo() const; |
| 56 | + |
| 57 | + /// Performs VJP generation on the empty VJP function. Returns true if any |
| 58 | + /// error occurs. |
100 | 59 | bool run(); |
101 | | - |
102 | | - void postProcess(SILInstruction *orig, SILInstruction *cloned); |
103 | | - |
104 | | - /// Remap original basic blocks, adding predecessor enum arguments. |
105 | | - SILBasicBlock *remapBasicBlock(SILBasicBlock *bb); |
106 | | - |
107 | | - /// General visitor for all instructions. If any error is emitted by previous |
108 | | - /// visits, bail out. |
109 | | - void visit(SILInstruction *inst); |
110 | | - |
111 | | - void visitSILInstruction(SILInstruction *inst); |
112 | | - |
113 | | -private: |
114 | | - /// Get the lowered SIL type of the given AST type. |
115 | | - SILType getLoweredType(Type type); |
116 | | - |
117 | | - /// Get the lowered SIL type of the given nominal type declaration. |
118 | | - SILType getNominalDeclLoweredType(NominalTypeDecl *nominal); |
119 | | - |
120 | | - // Creates a trampoline block for given original terminator instruction, the |
121 | | - // pullback struct value for its parent block, and a successor basic block. |
122 | | - // |
123 | | - // The trampoline block has the same arguments as and branches to the remapped |
124 | | - // successor block, but drops the last predecessor enum argument. |
125 | | - // |
126 | | - // Used for cloning branching terminator instructions with specific |
127 | | - // requirements on successor block arguments, where an additional predecessor |
128 | | - // enum argument is not acceptable. |
129 | | - SILBasicBlock *createTrampolineBasicBlock(TermInst *termInst, |
130 | | - StructInst *pbStructVal, |
131 | | - SILBasicBlock *succBB); |
132 | | - |
133 | | - /// Build a pullback struct value for the given original terminator |
134 | | - /// instruction. |
135 | | - StructInst *buildPullbackValueStructValue(TermInst *termInst); |
136 | | - |
137 | | - /// Build a predecessor enum instance using the given builder for the given |
138 | | - /// original predecessor/successor blocks and pullback struct value. |
139 | | - EnumInst *buildPredecessorEnumValue(SILBuilder &builder, |
140 | | - SILBasicBlock *predBB, |
141 | | - SILBasicBlock *succBB, |
142 | | - SILValue pbStructVal); |
143 | | - |
144 | | -public: |
145 | | - void visitReturnInst(ReturnInst *ri); |
146 | | - |
147 | | - void visitBranchInst(BranchInst *bi); |
148 | | - |
149 | | - void visitCondBranchInst(CondBranchInst *cbi); |
150 | | - |
151 | | - void visitSwitchEnumInstBase(SwitchEnumInstBase *inst); |
152 | | - |
153 | | - void visitSwitchEnumInst(SwitchEnumInst *sei); |
154 | | - |
155 | | - void visitSwitchEnumAddrInst(SwitchEnumAddrInst *seai); |
156 | | - |
157 | | - void visitCheckedCastBranchInst(CheckedCastBranchInst *ccbi); |
158 | | - |
159 | | - void visitCheckedCastValueBranchInst(CheckedCastValueBranchInst *ccvbi); |
160 | | - |
161 | | - void visitCheckedCastAddrBranchInst(CheckedCastAddrBranchInst *ccabi); |
162 | | - |
163 | | - // If an `apply` has active results or active inout arguments, replace it |
164 | | - // with an `apply` of its VJP. |
165 | | - void visitApplyInst(ApplyInst *ai); |
166 | | - |
167 | | - void visitDifferentiableFunctionInst(DifferentiableFunctionInst *dfi); |
168 | 60 | }; |
169 | 61 |
|
170 | 62 | } // end namespace autodiff |
|
0 commit comments