summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-ir-lower-enum-type.cpp
blob: 548c29a51827fef373364c7a70ef0bc8545ae27b (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
// slang-ir-lower-enum-type.cpp

#include "slang-ir-lower-enum-type.h"

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

namespace Slang
{
struct EnumTypeLoweringContext
{
    IRModule* module;
    DiagnosticSink* sink;

    InstWorkList workList;
    InstHashSet workListSet;

    IRGeneric* genericOptionalStructType = nullptr;
    IRStructKey* valueKey = nullptr;
    IRStructKey* hasValueKey = nullptr;

    EnumTypeLoweringContext(IRModule* inModule)
        : module(inModule), workList(inModule), workListSet(inModule)
    {
    }

    struct LoweredEnumTypeInfo : public RefObject
    {
        IRType* enumType = nullptr;
        IRType* loweredType = nullptr;
    };
    Dictionary<IRInst*, RefPtr<LoweredEnumTypeInfo>> loweredEnumTypes;

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

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

    LoweredEnumTypeInfo* getLoweredEnumType(IRInst* type)
    {
        if (auto loweredInfo = loweredEnumTypes.tryGetValue(type))
            return loweredInfo->Ptr();

        if (!type)
            return nullptr;

        if (type->getOp() != kIROp_EnumType)
            return nullptr;

        RefPtr<LoweredEnumTypeInfo> info = new LoweredEnumTypeInfo();
        auto enumType = cast<IREnumType>(type);
        auto valueType = enumType->getTagType();
        info->enumType = (IRType*)type;
        info->loweredType = valueType;
        loweredEnumTypes[type] = info;
        return info.Ptr();
    }

    void processEnumType(IREnumType* inst)
    {
        auto loweredEnumTypeInfo = getLoweredEnumType(inst);
        SLANG_ASSERT(loweredEnumTypeInfo);
        SLANG_UNUSED(loweredEnumTypeInfo);
    }

    void processEnumCast(IRInst* inst)
    {
        IRBuilder builderStorage(module);
        auto builder = &builderStorage;
        builder->setInsertBefore(inst);

        auto value = inst->getOperand(0);
        if (auto enumType = getLoweredEnumType(value->getDataType()))
        {
            auto rate = value->getRate();
            auto type = enumType->loweredType;
            if (rate)
            {
                type = builder->getRateQualifiedType(rate, type);
            }

            value->setFullType(type);
        }

        auto type = inst->getDataType();
        if (auto enumType = getLoweredEnumType(type))
        { // Cast was into enum, so use tag type instead.
            type = enumType->loweredType;
        }

        auto cast = builder->emitCast(type, value);

        inst->replaceUsesWith(cast);
        inst->removeAndDeallocate();
    }

    void processInst(IRInst* inst)
    {
        switch (inst->getOp())
        {
        case kIROp_EnumType:
            processEnumType((IREnumType*)inst);
            break;
        case kIROp_CastEnumToInt:
        case kIROp_CastIntToEnum:
        case kIROp_EnumCast:
            processEnumCast(inst);
            break;
        default:
            break;
        }
    }

    void processModule()
    {
        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);
            }
        }

        // Replace all enum types with their lowered equivalent types.
        for (const auto& [key, value] : loweredEnumTypes)
            key->replaceUsesWith(value->loweredType);
    }
};

void lowerEnumType(IRModule* module, DiagnosticSink* sink)
{
    EnumTypeLoweringContext context(module);
    context.sink = sink;
    context.processModule();
}
} // namespace Slang