summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-ir-augment-make-existential.cpp
blob: 2fe8207217ae8a76fb1a128a5e5a0c4c2a04c330 (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
#include "slang-ir-augment-make-existential.h"
#include "slang-ir-insts.h"
#include "slang-ir.h"

namespace Slang
{
struct AugmentMakeExistentialContext
{
    IRModule* module;

    SharedIRBuilder sharedBuilderStorage;

    List<IRInst*> workList;
    HashSet<IRInst*> workListSet;

    void addToWorkList(IRInst* inst)
    {
        if (workListSet.Contains(inst))
            return;

        workList.add(inst);
        workListSet.Add(inst);
    }

    void processMakeExistential(IRMakeExistential* inst)
    {
        IRBuilder builderStorage;
        auto builder = &builderStorage;
        builder->sharedBuilder = &sharedBuilderStorage;
        builder->setInsertBefore(inst);

        auto augInst = builder->emitMakeExistentialWithRTTI(
            inst->getFullType(),
            inst->getWrappedValue(),
            inst->getWitnessTable(),
            inst->getWrappedValue()->getDataType());
        inst->replaceUsesWith(augInst);
        inst->removeAndDeallocate();
    }

    void processInst(IRInst* inst)
    {
        switch (inst->getOp())
        {
        case kIROp_MakeExistential:
            processMakeExistential((IRMakeExistential*)inst);
            break;
        default:
            break;
        }
    }

    void processModule()
    {
        SharedIRBuilder* sharedBuilder = &sharedBuilderStorage;
        sharedBuilder->module = module;
        sharedBuilder->session = module->session;

        addToWorkList(module->getModuleInst());

        while (workList.getCount() != 0)
        {
            IRInst* inst = workList.getLast();

            workList.removeLast();
            workListSet.Remove(inst);

            processInst(inst);

            for (auto child = inst->getLastChild(); child; child = child->getPrevInst())
            {
                addToWorkList(child);
            }
        }
    }
};

void augmentMakeExistentialInsts(IRModule* module)
{
    AugmentMakeExistentialContext context;
    context.module = module;
    context.processModule();
}
} // namespace Slang