|
| 1 | +//===--- SILDifferentiabilityWitness.h - Differentiability witnesses ------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 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 the SILDifferentiabilityWitness class, which maps an |
| 14 | +// original SILFunction and derivative configuration (parameter indices, result |
| 15 | +// indices, derivative generic signature) to derivative functions (JVP and VJP). |
| 16 | +// |
| 17 | +// SIL differentiability witnesses are generated from the `@differentiable` |
| 18 | +// and `@derivative` AST declaration attributes. |
| 19 | +// |
| 20 | +// Differentiability witnesses are canonicalized by the SIL differentiation |
| 21 | +// transform, which fills in missing derivative functions. |
| 22 | +// |
| 23 | +//===----------------------------------------------------------------------===// |
| 24 | + |
| 25 | +#ifndef SWIFT_SIL_SILDIFFERENTIABILITYWITNESS_H |
| 26 | +#define SWIFT_SIL_SILDIFFERENTIABILITYWITNESS_H |
| 27 | + |
| 28 | +#include "swift/AST/Attr.h" |
| 29 | +#include "swift/AST/AutoDiff.h" |
| 30 | +#include "swift/AST/GenericSignature.h" |
| 31 | +#include "swift/SIL/SILAllocated.h" |
| 32 | +#include "swift/SIL/SILLinkage.h" |
| 33 | +#include "llvm/ADT/ilist.h" |
| 34 | +#include "llvm/ADT/ilist_node.h" |
| 35 | + |
| 36 | +namespace swift { |
| 37 | + |
| 38 | +class SILPrintContext; |
| 39 | + |
| 40 | +class SILDifferentiabilityWitness |
| 41 | + : public llvm::ilist_node<SILDifferentiabilityWitness>, |
| 42 | + public SILAllocated<SILDifferentiabilityWitness> { |
| 43 | +private: |
| 44 | + /// The module which contains the differentiability witness. |
| 45 | + SILModule &Module; |
| 46 | + /// The linkage of the differentiability witness. |
| 47 | + SILLinkage Linkage; |
| 48 | + /// The original function. |
| 49 | + SILFunction *OriginalFunction; |
| 50 | + /// The derivative configuration: parameter indices, result indices, and |
| 51 | + /// derivative generic signature (optional). The derivative generic signature |
| 52 | + /// may contain same-type requirements such that all generic parameters are |
| 53 | + /// bound to concrete types. |
| 54 | + AutoDiffConfig Config; |
| 55 | + /// The JVP (Jacobian-vector products) derivative function. |
| 56 | + SILFunction *JVP; |
| 57 | + /// The VJP (vector-Jacobian products) derivative function. |
| 58 | + SILFunction *VJP; |
| 59 | + /// Whether or not this differentiability witness is a declaration. |
| 60 | + bool IsDeclaration; |
| 61 | + /// Whether or not this differentiability witness is serialized, which allows |
| 62 | + /// devirtualization from another module. |
| 63 | + bool IsSerialized; |
| 64 | + /// The AST `@differentiable` or `@derivative` attribute from which the |
| 65 | + /// differentiability witness is generated. Used for diagnostics. |
| 66 | + /// Null if the differentiability witness is parsed from SIL or if it is |
| 67 | + /// deserialized. |
| 68 | + const DeclAttribute *Attribute = nullptr; |
| 69 | + |
| 70 | + SILDifferentiabilityWitness( |
| 71 | + SILModule &module, SILLinkage linkage, SILFunction *originalFunction, |
| 72 | + IndexSubset *parameterIndices, IndexSubset *resultIndices, |
| 73 | + GenericSignature derivativeGenSig, SILFunction *jvp, SILFunction *vjp, |
| 74 | + bool isDeclaration, bool isSerialized, const DeclAttribute *attribute) |
| 75 | + : Module(module), Linkage(linkage), OriginalFunction(originalFunction), |
| 76 | + Config(parameterIndices, resultIndices, derivativeGenSig.getPointer()), |
| 77 | + JVP(jvp), VJP(vjp), IsDeclaration(isDeclaration), |
| 78 | + IsSerialized(isSerialized), Attribute(attribute) {} |
| 79 | + |
| 80 | +public: |
| 81 | + static SILDifferentiabilityWitness * |
| 82 | + createDeclaration(SILModule &module, SILLinkage linkage, |
| 83 | + SILFunction *originalFunction, |
| 84 | + IndexSubset *parameterIndices, IndexSubset *resultIndices, |
| 85 | + GenericSignature derivativeGenSig, |
| 86 | + const DeclAttribute *attribute = nullptr); |
| 87 | + |
| 88 | + static SILDifferentiabilityWitness *createDefinition( |
| 89 | + SILModule &module, SILLinkage linkage, SILFunction *originalFunction, |
| 90 | + IndexSubset *parameterIndices, IndexSubset *resultIndices, |
| 91 | + GenericSignature derivativeGenSig, SILFunction *jvp, SILFunction *vjp, |
| 92 | + bool isSerialized, const DeclAttribute *attribute = nullptr); |
| 93 | + |
| 94 | + void convertToDefinition(SILFunction *jvp, SILFunction *vjp, |
| 95 | + bool isSerialized); |
| 96 | + |
| 97 | + SILDifferentiabilityWitnessKey getKey() const; |
| 98 | + SILModule &getModule() const { return Module; } |
| 99 | + SILLinkage getLinkage() const { return Linkage; } |
| 100 | + SILFunction *getOriginalFunction() const { return OriginalFunction; } |
| 101 | + const AutoDiffConfig &getConfig() const { return Config; } |
| 102 | + IndexSubset *getParameterIndices() const { return Config.parameterIndices; } |
| 103 | + IndexSubset *getResultIndices() const { return Config.resultIndices; } |
| 104 | + GenericSignature getDerivativeGenericSignature() const { |
| 105 | + return Config.derivativeGenericSignature; |
| 106 | + } |
| 107 | + SILFunction *getJVP() const { return JVP; } |
| 108 | + SILFunction *getVJP() const { return VJP; } |
| 109 | + SILFunction *getDerivative(AutoDiffDerivativeFunctionKind kind) const { |
| 110 | + switch (kind) { |
| 111 | + case AutoDiffDerivativeFunctionKind::JVP: |
| 112 | + return JVP; |
| 113 | + case AutoDiffDerivativeFunctionKind::VJP: |
| 114 | + return VJP; |
| 115 | + } |
| 116 | + } |
| 117 | + void setJVP(SILFunction *jvp) { JVP = jvp; } |
| 118 | + void setVJP(SILFunction *vjp) { VJP = vjp; } |
| 119 | + void setDerivative(AutoDiffDerivativeFunctionKind kind, |
| 120 | + SILFunction *derivative) { |
| 121 | + switch (kind) { |
| 122 | + case AutoDiffDerivativeFunctionKind::JVP: |
| 123 | + JVP = derivative; |
| 124 | + break; |
| 125 | + case AutoDiffDerivativeFunctionKind::VJP: |
| 126 | + VJP = derivative; |
| 127 | + break; |
| 128 | + } |
| 129 | + } |
| 130 | + bool isDeclaration() const { return IsDeclaration; } |
| 131 | + bool isDefinition() const { return !IsDeclaration; } |
| 132 | + bool isSerialized() const { return IsSerialized; } |
| 133 | + const DeclAttribute *getAttribute() const { return Attribute; } |
| 134 | + |
| 135 | + /// Verify that the differentiability witness is well-formed. |
| 136 | + void verify(const SILModule &module) const; |
| 137 | + |
| 138 | + void print(llvm::raw_ostream &os, bool verbose = false) const; |
| 139 | + void dump() const; |
| 140 | +}; |
| 141 | + |
| 142 | +} // end namespace swift |
| 143 | + |
| 144 | +namespace llvm { |
| 145 | + |
| 146 | +//===----------------------------------------------------------------------===// |
| 147 | +// ilist_traits for SILDifferentiabilityWitness |
| 148 | +//===----------------------------------------------------------------------===// |
| 149 | + |
| 150 | +template <> |
| 151 | +struct ilist_traits<::swift::SILDifferentiabilityWitness> |
| 152 | + : public ilist_node_traits<::swift::SILDifferentiabilityWitness> { |
| 153 | + using SILDifferentiabilityWitness = ::swift::SILDifferentiabilityWitness; |
| 154 | + |
| 155 | +public: |
| 156 | + static void deleteNode(SILDifferentiabilityWitness *DW) { |
| 157 | + DW->~SILDifferentiabilityWitness(); |
| 158 | + } |
| 159 | + |
| 160 | +private: |
| 161 | + void createNode(const SILDifferentiabilityWitness &); |
| 162 | +}; |
| 163 | + |
| 164 | +} // namespace llvm |
| 165 | + |
| 166 | +#endif // SWIFT_SIL_SILDIFFERENTIABILITYWITNESS_H |
0 commit comments