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