/Volumes/compiler/apple/swift/lib/SILOptimizer/Differentiation/DifferentiationInvoker.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- DifferentiationInvoker.cpp ---------------------------*- 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 | | // Class that represents an invoker of differentiation. |
14 | | // Used to track diagnostic source locations. |
15 | | // |
16 | | //===----------------------------------------------------------------------===// |
17 | | |
18 | | #include "swift/SILOptimizer/Differentiation/DifferentiationInvoker.h" |
19 | | |
20 | | #include "swift/SIL/SILDifferentiabilityWitness.h" |
21 | | #include "swift/SIL/SILFunction.h" |
22 | | #include "swift/SIL/SILInstruction.h" |
23 | | |
24 | | namespace swift { |
25 | | namespace autodiff { |
26 | | |
27 | 0 | SourceLoc DifferentiationInvoker::getLocation() const { |
28 | 0 | switch (kind) { |
29 | 0 | case Kind::DifferentiableFunctionInst: |
30 | 0 | return getDifferentiableFunctionInst()->getLoc().getSourceLoc(); |
31 | 0 | case Kind::LinearFunctionInst: |
32 | 0 | return getLinearFunctionInst()->getLoc().getSourceLoc(); |
33 | 0 | case Kind::IndirectDifferentiation: |
34 | 0 | return getIndirectDifferentiation().first->getLoc().getSourceLoc(); |
35 | 0 | case Kind::SILDifferentiabilityWitnessInvoker: |
36 | 0 | return getSILDifferentiabilityWitnessInvoker() |
37 | 0 | ->getOriginalFunction() |
38 | 0 | ->getLocation() |
39 | 0 | .getSourceLoc(); |
40 | 0 | } |
41 | 0 | llvm_unreachable("Invalid invoker kind"); // silences MSVC C4715 |
42 | 0 | } |
43 | | |
44 | 28 | void DifferentiationInvoker::print(llvm::raw_ostream &os) const { |
45 | 28 | os << "(differentiation_invoker "; |
46 | 28 | switch (kind) { |
47 | 0 | case Kind::DifferentiableFunctionInst: |
48 | 0 | os << "differentiable_function_inst=(" << *getDifferentiableFunctionInst() |
49 | 0 | << ')'; |
50 | 0 | break; |
51 | 0 | case Kind::LinearFunctionInst: |
52 | 0 | os << "linear_function_inst=(" << *getLinearFunctionInst() << ')'; |
53 | 0 | break; |
54 | 0 | case Kind::IndirectDifferentiation: { |
55 | 0 | auto indDiff = getIndirectDifferentiation(); |
56 | 0 | os << "indirect_differentiation=(" << *std::get<0>(indDiff) << ')'; |
57 | | // TODO: Enable printing parent invokers. |
58 | | // May require storing a `DifferentiableInvoker *` in the |
59 | | // `IndirectDifferentiation` case. |
60 | | /* |
61 | | SILInstruction *inst; |
62 | | SILDifferentiableAttr *attr; |
63 | | std::tie(inst, attr) = getIndirectDifferentiation(); |
64 | | auto invokerLookup = invokers.find(attr); // No access to ADContext? |
65 | | assert(invokerLookup != invokers.end() && "Expected parent invoker"); |
66 | | */ |
67 | 0 | break; |
68 | 0 | } |
69 | 28 | case Kind::SILDifferentiabilityWitnessInvoker: { |
70 | 28 | auto witness = getSILDifferentiabilityWitnessInvoker(); |
71 | 28 | os << "sil_differentiability_witness_invoker=(witness=("; |
72 | 28 | witness->print(os); |
73 | 28 | os << ") function=" << witness->getOriginalFunction()->getName(); |
74 | 28 | break; |
75 | 0 | } |
76 | 28 | } |
77 | 28 | os << ')'; |
78 | 28 | } |
79 | | |
80 | | } // end namespace autodiff |
81 | | } // end namespace swift |