summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-ir-specialize-stage-switch.cpp
blob: f65aa4d4cd24379e203a05a8ec0adae2305e4831 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "slang-ir-specialize-stage-switch.h"

#include "slang-capability.h"
#include "slang-compiler.h"
#include "slang-ir-call-graph.h"
#include "slang-ir-insts.h"
#include "slang-ir-util.h"
#include "slang-ir.h"

namespace Slang
{
bool funcHasGetCurrentStageInst(IRGlobalValueWithCode* func)
{
    for (auto block : func->getBlocks())
    {
        for (auto inst : block->getChildren())
        {
            if (inst->getOp() == kIROp_GetCurrentStage)
            {
                return true;
            }
        }
    }
    return false;
}

void discoverStageSpecificFunctions(HashSet<IRInst*>& stageSpecificFunctions, IRModule* module)
{
    List<IRInst*> workList;
    for (auto inst : module->getGlobalInsts())
    {
        if (auto func = as<IRGlobalValueWithCode>(inst))
        {
            if (funcHasGetCurrentStageInst(func))
            {
                workList.add(inst);
                stageSpecificFunctions.add(func);
            }
        }
    }
    for (Index i = 0; i < workList.getCount(); i++)
    {
        auto callee = workList[i];
        traverseUses(
            callee,
            [&](IRUse* use)
            {
                if (use->getUser()->getOp() == kIROp_Call)
                {
                    auto parentFunc = getParentFunc(use->getUser());
                    if (parentFunc && stageSpecificFunctions.add(parentFunc))
                    {
                        workList.add(parentFunc);
                    }
                }
            });
    }
}

// Given a func, replace all `GetCurrentStage` insts with the given stage, and rewrite all calls to
// stage specific functions to the specialized function for the given stage.
//
void specializeFuncToStage(
    Stage stage,
    IRGlobalValueWithCode* func,
    Dictionary<IRInst*, Dictionary<Stage, IRInst*>>& mapFuncToStageSpecializedFunc)
{
    // Collect all insts that may need to be modified.
    List<IRInst*> instsToModify;
    for (auto block : func->getBlocks())
    {
        for (auto inst : block->getChildren())
        {
            switch (inst->getOp())
            {
            case kIROp_GetCurrentStage:
            case kIROp_Call:
                instsToModify.add(inst);
                break;
            }
        }
    }

    IRInst* stageVal = nullptr;
    IRBuilder builder(func);
    for (auto inst : instsToModify)
    {
        builder.setInsertBefore(inst);

        switch (inst->getOp())
        {
        case kIROp_GetCurrentStage:
            {
                // Replace `GetCurrentStage` with the stage it is specialized to.
                if (!stageVal)
                {
                    stageVal = builder.getIntValue((IRIntegerValue)stage);
                }
                inst->replaceUsesWith(stageVal);
                inst->removeAndDeallocate();
                break;
            }
        case kIROp_Call:
            {
                // Replace calls to stage specific functions with the specialized function for the
                // given stage.
                auto callInst = static_cast<IRCall*>(inst);
                auto callee = callInst->getCallee();
                auto specializedFuncs = mapFuncToStageSpecializedFunc.tryGetValue(callee);
                if (specializedFuncs)
                {
                    auto specializedFunc = specializedFuncs->tryGetValue(stage);
                    if (specializedFunc)
                    {
                        builder.replaceOperand(callInst->getCalleeUse(), *specializedFunc);
                    }
                }
                break;
            }
        }
    }
}

void specializeStageSwitch(IRModule* module)
{
    Dictionary<IRInst*, HashSet<IRFunc*>> mapInstToReferencingEntryPoints;
    buildEntryPointReferenceGraph(mapInstToReferencingEntryPoints, module);

    HashSet<IRInst*> stageSpecificFunctions;
    discoverStageSpecificFunctions(stageSpecificFunctions, module);

    // Clone all stage specific functions for each stage they are used in.
    Dictionary<IRInst*, Dictionary<Stage, IRInst*>> mapFuncToStageSpecializedFunc;
    for (auto func : stageSpecificFunctions)
    {
        auto referencingEntryPoints = mapInstToReferencingEntryPoints.tryGetValue(func);
        if (!referencingEntryPoints)
            continue;
        if (func->findDecoration<IREntryPointDecoration>())
            continue;
        Dictionary<Stage, IRInst*> specializedFuncs;
        for (auto entryPoint : *referencingEntryPoints)
        {
            auto entryPointDecor = entryPoint->findDecoration<IREntryPointDecoration>();
            if (!entryPointDecor)
                continue;
            auto stage = entryPointDecor->getProfile().getStage();
            auto stageSpecializedFunc = specializedFuncs.tryGetValue(stage);
            if (stageSpecializedFunc)
                continue;
            IRCloneEnv cloneEnv;
            IRBuilder builder(func);
            builder.setInsertBefore(func);
            auto clonedFunc = cloneInst(&cloneEnv, &builder, func);
            specializedFuncs[stage] = clonedFunc;
        }
        mapFuncToStageSpecializedFunc.add(func, _Move(specializedFuncs));
    }

    // Rewrite entrypoint and cloned functions to replace `GetCurrentStage` with the stage they are
    // specialized to.
    for (auto func : stageSpecificFunctions)
    {
        // Is this an entrypoint?
        if (auto entryPointDecor = func->findDecoration<IREntryPointDecoration>())
        {
            auto stage = entryPointDecor->getProfile().getStage();
            specializeFuncToStage(
                stage,
                as<IRGlobalValueWithCode>(func),
                mapFuncToStageSpecializedFunc);
        }
        else
        {
            // Is this a cloned function?
            auto specializedFuncs = mapFuncToStageSpecializedFunc.tryGetValue(func);
            if (!specializedFuncs)
                continue;
            for (auto pair : *specializedFuncs)
            {
                auto stage = pair.first;
                auto specializedFunc = pair.second;
                specializeFuncToStage(
                    stage,
                    as<IRGlobalValueWithCode>(specializedFunc),
                    mapFuncToStageSpecializedFunc);
            }
        }
    }

    // Remove all original stage specific functions.
    for (auto f : mapFuncToStageSpecializedFunc)
    {
        f.first->removeAndDeallocate();
    }
}

} // namespace Slang