summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-ir-call-graph.cpp
blob: a709bd4183d99ba4edb5c1b418921d7a3556283e (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
#include "slang-ir-call-graph.h"

#include "slang-ir-clone.h"
#include "slang-ir-insts.h"

namespace Slang
{

void buildEntryPointReferenceGraph(
    Dictionary<IRInst*, HashSet<IRFunc*>>& referencingEntryPoints,
    IRModule* module)
{
    struct WorkItem
    {
        IRFunc* entryPoint;
        IRInst* inst;

        HashCode getHashCode() const
        {
            return combineHash(Slang::getHashCode(entryPoint), Slang::getHashCode(inst));
        }
        bool operator==(const WorkItem& other) const
        {
            return entryPoint == other.entryPoint && inst == other.inst;
        }
    };
    HashSet<WorkItem> workListSet;
    List<WorkItem> workList;
    auto addToWorkList = [&](WorkItem item)
    {
        if (workListSet.add(item))
            workList.add(item);
    };

    auto registerEntryPointReference = [&](IRFunc* entryPoint, IRInst* inst)
    {
        if (auto set = referencingEntryPoints.tryGetValue(inst))
            set->add(entryPoint);
        else
        {
            HashSet<IRFunc*> newSet;
            newSet.add(entryPoint);
            referencingEntryPoints.add(inst, _Move(newSet));
        }
    };
    auto visit = [&](IRFunc* entryPoint, IRInst* inst)
    {
        if (auto code = as<IRGlobalValueWithCode>(inst))
        {
            registerEntryPointReference(entryPoint, inst);
            for (auto child : code->getChildren())
            {
                addToWorkList({entryPoint, child});
            }
            return;
        }
        switch (inst->getOp())
        {
        case kIROp_GlobalParam:
        case kIROp_SPIRVAsmOperandBuiltinVar:
            registerEntryPointReference(entryPoint, inst);
            break;
        case kIROp_Block:
        case kIROp_SPIRVAsm:
            for (auto child : inst->getChildren())
            {
                addToWorkList({entryPoint, child});
            }
            break;
        case kIROp_Call:
            {
                auto call = as<IRCall>(inst);
                addToWorkList({entryPoint, call->getCallee()});
            }
            break;
        case kIROp_SPIRVAsmOperandInst:
            {
                auto operand = as<IRSPIRVAsmOperandInst>(inst);
                addToWorkList({entryPoint, operand->getValue()});
            }
            break;
        }
        for (UInt i = 0; i < inst->getOperandCount(); i++)
        {
            auto operand = inst->getOperand(i);
            switch (operand->getOp())
            {
            case kIROp_GlobalParam:
            case kIROp_GlobalVar:
            case kIROp_SPIRVAsmOperandBuiltinVar:
            case kIROp_Generic:
                addToWorkList({entryPoint, operand});
                break;
            }
        }
    };

    for (auto globalInst : module->getGlobalInsts())
    {
        if (globalInst->getOp() == kIROp_Func &&
            globalInst->findDecoration<IREntryPointDecoration>())
        {
            visit(as<IRFunc>(globalInst), globalInst);
        }
    }
    for (Index i = 0; i < workList.getCount(); i++)
        visit(workList[i].entryPoint, workList[i].inst);
}

HashSet<IRFunc*>* getReferencingEntryPoints(
    Dictionary<IRInst*, HashSet<IRFunc*>>& m_referencingEntryPoints,
    IRInst* inst)
{
    auto* referencingEntryPoints = m_referencingEntryPoints.tryGetValue(inst);
    if (!referencingEntryPoints)
        return nullptr;
    return referencingEntryPoints;
}

} // namespace Slang