summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-ir-autodiff-transcriber-base.h
blob: 13dbf80ab106b842926d4b6862c92cd08218955e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// slang-ir-autodiff-transcriber-base.h
#pragma once

#include "slang-compiler.h"
#include "slang-ir-autodiff.h"
#include "slang-ir-insts.h"
#include "slang-ir.h"

namespace Slang
{

struct AutoDiffTranscriberBase
{
    // Stores the mapping of arbitrary 'R-value' instructions to instructions that represent
    // their differential values.
    Dictionary<IRInst*, IRInst*> instMapD;

    // Set of insts currently being transcribed. Used to avoid infinite loops.
    HashSet<IRInst*> instsInProgress;

    // Cloning environment to hold mapping from old to new copies for the primal
    // instructions.
    IRCloneEnv cloneEnv;

    // Diagnostic sink for error messages.
    DiagnosticSink* sink;

    // Type conformance information.
    AutoDiffSharedContext* autoDiffSharedContext;

    // Builder to help with creating and accessing the 'DifferentiablePair<T>' struct
    DifferentialPairTypeBuilder* pairBuilder;

    DifferentiableTypeConformanceContext differentiableTypeConformanceContext;

    AutoDiffTranscriberBase(AutoDiffSharedContext* shared, DiagnosticSink* inSink)
        : autoDiffSharedContext(shared), differentiableTypeConformanceContext(shared), sink(inSink)
    {
        cloneEnv.squashChildrenMapping = true;
    }

    DiagnosticSink* getSink();

    // Returns "dp<var-name>" to use as a name hint for parameters.
    // If no primal name is available, returns a blank string.
    //
    String makeDiffPairName(IRInst* origVar);

    void mapDifferentialInst(IRInst* origInst, IRInst* diffInst);

    void mapPrimalInst(IRInst* origInst, IRInst* primalInst);

    IRInst* lookupDiffInst(IRInst* origInst);

    IRInst* lookupDiffInst(IRInst* origInst, IRInst* defaultInst);

    bool hasDifferentialInst(IRInst* origInst);

    bool shouldUseOriginalAsPrimal(IRInst* currentParent, IRInst* origInst);

    IRInst* lookupPrimalInstImpl(IRInst* currentParent, IRInst* origInst);

    IRInst* lookupPrimalInst(IRInst* currentParent, IRInst* origInst, IRInst* defaultInst);

    IRInst* lookupPrimalInstIfExists(IRBuilder* builder, IRInst* origInst)
    {
        return lookupPrimalInst(builder->getInsertLoc().getParent(), origInst, origInst);
    }

    IRInst* lookupPrimalInst(IRBuilder* builder, IRInst* origInst)
    {
        return lookupPrimalInstImpl(builder->getInsertLoc().getParent(), origInst);
    }

    IRInst* lookupPrimalInst(IRBuilder* builder, IRInst* origInst, IRInst* defaultInst)
    {
        return lookupPrimalInst(builder->getInsertLoc().getParent(), origInst, defaultInst);
    }

    bool hasPrimalInst(IRInst* currentParent, IRInst* origInst);

    IRInst* findOrTranscribeDiffInst(IRBuilder* builder, IRInst* origInst);

    IRInst* findOrTranscribePrimalInst(IRBuilder* builder, IRInst* origInst);

    IRInst* maybeCloneForPrimalInst(IRBuilder* builder, IRInst* inst);

    InstPair transcribeExtractExistentialWitnessTable(IRBuilder* builder, IRInst* origInst);

    void maybeMigrateDifferentiableDictionaryFromDerivativeFunc(
        IRBuilder* builder,
        IRInst* origFunc);

    IRInst* tryGetDifferentiableWitness(
        IRBuilder* builder,
        IRInst* originalType,
        DiffConformanceKind kind);

    IRType* getOrCreateDiffPairType(IRBuilder* builder, IRInst* primalType, IRInst* witness);

    IRType* getOrCreateDiffPairType(IRBuilder* builder, IRInst* originalType);

    IRType* differentiateType(IRBuilder* builder, IRType* origType);

    IRType* differentiateExtractExistentialType(
        IRBuilder* builder,
        IRExtractExistentialType* origType,
        IRInst*& witnessTable);

    IRType* tryGetDiffPairType(IRBuilder* builder, IRType* primalType);

    IRInst* getDifferentialZeroOfType(IRBuilder* builder, IRType* primalType);

    InstPair transcribeNonDiffInst(IRBuilder* builder, IRInst* origInst);

    InstPair transcribeReturn(IRBuilder* builder, IRReturn* origReturn);

    InstPair transcribeParam(IRBuilder* builder, IRParam* origParam);

    virtual InstPair transcribeFuncParam(
        IRBuilder* builder,
        IRParam* origParam,
        IRInst* primalType) = 0;

    InstPair transcribeLookupInterfaceMethod(IRBuilder* builder, IRLookupWitnessMethod* lookupInst);

    InstPair transcribeBlockImpl(
        IRBuilder* builder,
        IRBlock* origBlock,
        HashSet<IRInst*>& instsToSkip);

    InstPair transcribeBlock(IRBuilder* builder, IRBlock* origBlock)
    {
        HashSet<IRInst*> ignore;
        for (auto inst = origBlock->getFirstInst(); inst; inst = inst->next)
        {
            if (inst->m_op == kIROp_Unmodified)
                ignore.add(inst);
        }

        return transcribeBlockImpl(builder, origBlock, ignore);
    }

    // Transcribe a generic definition
    InstPair transcribeGeneric(IRBuilder* inBuilder, IRGeneric* origGeneric);

    IRInst* transcribe(IRBuilder* builder, IRInst* origInst);

    InstPair transcribeInst(IRBuilder* builder, IRInst* origInst);

    IRType* _differentiateTypeImpl(IRBuilder* builder, IRType* origType);

    bool isExistentialType(IRType* type);

    void _markInstAsDifferential(
        IRBuilder* builder,
        IRInst* diffInst,
        IRInst* primalInst = nullptr);

    void copyOriginalDecorations(IRInst* origFunc, IRInst* diffFunc);

    virtual IRFuncType* differentiateFunctionType(
        IRBuilder* builder,
        IRInst* func,
        IRFuncType* funcType) = 0;

    // Create an empty func to represent the transcribed func of `origFunc`.
    virtual InstPair transcribeFuncHeader(IRBuilder* inBuilder, IRFunc* origFunc) = 0;

    virtual InstPair transcribeInstImpl(IRBuilder* builder, IRInst* origInst) = 0;

    virtual IROp getInterfaceRequirementDerivativeDecorationOp() = 0;

    void markDiffTypeInst(IRBuilder* builder, IRInst* inst, IRType* primalType);

    void markDiffPairTypeInst(IRBuilder* builder, IRInst* inst, IRType* primalType);
};

} // namespace Slang