|
| 1 | +//===--- VJPEmitter.h - VJP Generation in differentiation -----*- C++ -*---===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2019 - 2020 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// This file defines a helper class for generating VJP functions for automatic |
| 14 | +// differentiation. |
| 15 | +// |
| 16 | +//===----------------------------------------------------------------------===// |
| 17 | + |
| 18 | +#ifndef SWIFT_SILOPTIMIZER_UTILS_DIFFERENTIATION_VJPEMITTER_H |
| 19 | +#define SWIFT_SILOPTIMIZER_UTILS_DIFFERENTIATION_VJPEMITTER_H |
| 20 | + |
| 21 | +#include "swift/SIL/TypeSubstCloner.h" |
| 22 | +#include "swift/SILOptimizer/Analysis/DifferentiableActivityAnalysis.h" |
| 23 | +#include "swift/SILOptimizer/Utils/Differentiation/DifferentiationInvoker.h" |
| 24 | +#include "swift/SILOptimizer/Utils/Differentiation/LinearMapInfo.h" |
| 25 | +#include "llvm/ADT/DenseMap.h" |
| 26 | + |
| 27 | +namespace swift { |
| 28 | + |
| 29 | +class SILDifferentiabilityWitness; |
| 30 | +class SILBasicBlock; |
| 31 | +class SILFunction; |
| 32 | +class SILInstruction; |
| 33 | + |
| 34 | +namespace autodiff { |
| 35 | + |
| 36 | +class ADContext; |
| 37 | +class PullbackEmitter; |
| 38 | + |
| 39 | +class VJPEmitter final |
| 40 | + : public TypeSubstCloner<VJPEmitter, SILOptFunctionBuilder> { |
| 41 | + friend class PullbackEmitter; |
| 42 | + |
| 43 | +private: |
| 44 | + /// The global context. |
| 45 | + ADContext &context; |
| 46 | + |
| 47 | + /// The original function. |
| 48 | + SILFunction *const original; |
| 49 | + |
| 50 | + /// The differentiability witness. |
| 51 | + SILDifferentiabilityWitness *const witness; |
| 52 | + |
| 53 | + /// The VJP function. |
| 54 | + SILFunction *const vjp; |
| 55 | + |
| 56 | + /// The pullback function. |
| 57 | + SILFunction *pullback; |
| 58 | + |
| 59 | + /// The differentiation invoker. |
| 60 | + DifferentiationInvoker invoker; |
| 61 | + |
| 62 | + /// Info from activity analysis on the original function. |
| 63 | + const DifferentiableActivityInfo &activityInfo; |
| 64 | + |
| 65 | + /// The linear map info. |
| 66 | + LinearMapInfo pullbackInfo; |
| 67 | + |
| 68 | + /// Caches basic blocks whose phi arguments have been remapped (adding a |
| 69 | + /// predecessor enum argument). |
| 70 | + SmallPtrSet<SILBasicBlock *, 4> remappedBasicBlocks; |
| 71 | + |
| 72 | + bool errorOccurred = false; |
| 73 | + |
| 74 | + /// Mapping from original blocks to pullback values. Used to build pullback |
| 75 | + /// struct instances. |
| 76 | + llvm::DenseMap<SILBasicBlock *, SmallVector<SILValue, 8>> pullbackValues; |
| 77 | + |
| 78 | + ASTContext &getASTContext() const { return vjp->getASTContext(); } |
| 79 | + SILModule &getModule() const { return vjp->getModule(); } |
| 80 | + const SILAutoDiffIndices getIndices() const { |
| 81 | + return witness->getSILAutoDiffIndices(); |
| 82 | + } |
| 83 | + |
| 84 | + static SubstitutionMap getSubstitutionMap(SILFunction *original, |
| 85 | + SILFunction *vjp); |
| 86 | + |
| 87 | + static const DifferentiableActivityInfo & |
| 88 | + getActivityInfo(ADContext &context, SILFunction *original, |
| 89 | + SILAutoDiffIndices indices, SILFunction *vjp); |
| 90 | + |
| 91 | +public: |
| 92 | + explicit VJPEmitter(ADContext &context, SILFunction *original, |
| 93 | + SILDifferentiabilityWitness *witness, SILFunction *vjp, |
| 94 | + DifferentiationInvoker invoker); |
| 95 | + |
| 96 | + SILFunction *createEmptyPullback(); |
| 97 | + |
| 98 | + /// Run VJP generation. Returns true on error. |
| 99 | + bool run(); |
| 100 | + |
| 101 | + void postProcess(SILInstruction *orig, SILInstruction *cloned); |
| 102 | + |
| 103 | + /// Remap original basic blocks, adding predecessor enum arguments. |
| 104 | + SILBasicBlock *remapBasicBlock(SILBasicBlock *bb); |
| 105 | + |
| 106 | + /// General visitor for all instructions. If any error is emitted by previous |
| 107 | + /// visits, bail out. |
| 108 | + void visit(SILInstruction *inst); |
| 109 | + |
| 110 | + void visitSILInstruction(SILInstruction *inst); |
| 111 | + |
| 112 | +private: |
| 113 | + /// Get the lowered SIL type of the given AST type. |
| 114 | + SILType getLoweredType(Type type); |
| 115 | + |
| 116 | + /// Get the lowered SIL type of the given nominal type declaration. |
| 117 | + SILType getNominalDeclLoweredType(NominalTypeDecl *nominal); |
| 118 | + |
| 119 | + /// Build a pullback struct value for the original block corresponding to the |
| 120 | + /// given terminator. |
| 121 | + StructInst *buildPullbackValueStructValue(TermInst *termInst); |
| 122 | + |
| 123 | + /// Build a predecessor enum instance using the given builder for the given |
| 124 | + /// original predecessor/successor blocks and pullback struct value. |
| 125 | + EnumInst *buildPredecessorEnumValue(SILBuilder &builder, |
| 126 | + SILBasicBlock *predBB, |
| 127 | + SILBasicBlock *succBB, |
| 128 | + SILValue pbStructVal); |
| 129 | + |
| 130 | +public: |
| 131 | + void visitReturnInst(ReturnInst *ri); |
| 132 | + |
| 133 | + void visitBranchInst(BranchInst *bi); |
| 134 | + |
| 135 | + void visitCondBranchInst(CondBranchInst *cbi); |
| 136 | + |
| 137 | + void visitSwitchEnumInstBase(SwitchEnumInstBase *inst); |
| 138 | + |
| 139 | + void visitSwitchEnumInst(SwitchEnumInst *sei); |
| 140 | + |
| 141 | + void visitSwitchEnumAddrInst(SwitchEnumAddrInst *seai); |
| 142 | + |
| 143 | + // If an `apply` has active results or active inout arguments, replace it |
| 144 | + // with an `apply` of its VJP. |
| 145 | + void visitApplyInst(ApplyInst *ai); |
| 146 | + |
| 147 | + void visitDifferentiableFunctionInst(DifferentiableFunctionInst *dfi); |
| 148 | +}; |
| 149 | + |
| 150 | +} // end namespace autodiff |
| 151 | +} // end namespace swift |
| 152 | + |
| 153 | +#endif // SWIFT_SILOPTIMIZER_UTILS_DIFFERENTIATION_VJPEMITTER_H |
0 commit comments