summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-ir-lower-optional-type.cpp
blob: c4a40774c0a4a188f628d678306d11aca2249a47 (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// slang-ir-lower-optional-type.cpp

#include "slang-ir-lower-optional-type.h"
#include "slang-ir.h"
#include "slang-ir-insts.h"
#include "slang-ir-util.h"

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

        SharedIRBuilder sharedBuilderStorage;

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

        struct LoweredOptionalTypeInfo : public RefObject
        {
            IRType* optionalType = nullptr;
            IRType* valueType = nullptr;
            IRType* loweredType = nullptr;
            IRStructField* valueField = nullptr;
            IRStructField* hasValueField = nullptr;
        };
        Dictionary<IRInst*, RefPtr<LoweredOptionalTypeInfo>> mapLoweredTypeToOptionalTypeInfo;
        Dictionary<IRInst*, RefPtr<LoweredOptionalTypeInfo>> loweredOptionalTypes;

        IRType* maybeLowerOptionalType(IRBuilder* builder, IRType* type)
        {
            if (auto info = getLoweredOptionalType(builder, type))
                return info->loweredType;
            else
                return type;
        }

        bool typeHasNullValue(IRInst* type)
        {
            switch (type->getOp())
            {
            case kIROp_ComPtrType:
            case kIROp_NativePtrType:
            case kIROp_NativeStringType:
            case kIROp_PtrType:
            case kIROp_ClassType:
                return true;
            case kIROp_InterfaceType:
                return isComInterfaceType((IRType*)type);
            default:
                return false;
            }
        }

        LoweredOptionalTypeInfo* getLoweredOptionalType(IRBuilder* builder, IRInst* type)
        {
            if (auto loweredInfo = loweredOptionalTypes.TryGetValue(type))
                return loweredInfo->Ptr();
            if (auto loweredInfo = mapLoweredTypeToOptionalTypeInfo.TryGetValue(type))
                return loweredInfo->Ptr();

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

            RefPtr<LoweredOptionalTypeInfo> info = new LoweredOptionalTypeInfo();
            auto optionalType = cast<IROptionalType>(type);
            auto valueType = optionalType->getValueType();
            info->optionalType = (IRType*)type;
            info->valueType = valueType;
            if (typeHasNullValue(valueType))
            {
                info->loweredType = valueType;
            }
            else
            {
                auto structType = builder->createStructType();
                info->loweredType = structType;
                builder->addNameHintDecoration(structType, UnownedStringSlice("OptionalType"));

                info->valueType = valueType;
                auto valueKey = builder->createStructKey();
                builder->addNameHintDecoration(valueKey, UnownedStringSlice("value"));
                info->valueField = builder->createStructField(structType, valueKey, (IRType*)valueType);

                auto boolType = builder->getBoolType();
                auto hasValueKey = builder->createStructKey();
                builder->addNameHintDecoration(hasValueKey, UnownedStringSlice("hasValue"));
                info->hasValueField = builder->createStructField(structType, hasValueKey, (IRType*)boolType);
            }
            mapLoweredTypeToOptionalTypeInfo[info->loweredType] = info;
            loweredOptionalTypes[type] = info;
            return info.Ptr();
        }

        void addToWorkList(
            IRInst* inst)
        {
            for (auto ii = inst->getParent(); ii; ii = ii->getParent())
            {
                if (as<IRGeneric>(ii))
                    return;
            }

            if (workListSet.Contains(inst))
                return;

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

        void processMakeOptionalValue(IRMakeOptionalValue* inst)
        {
            IRBuilder builderStorage(sharedBuilderStorage);
            auto builder = &builderStorage;
            builder->setInsertBefore(inst);

            auto info = getLoweredOptionalType(builder, inst->getDataType());
            if (info->loweredType != info->valueType)
            {
                List<IRInst*> operands;
                operands.add(inst->getOperand(0));
                operands.add(builder->getBoolValue(true));
                auto makeStruct = builder->emitMakeStruct(info->loweredType, operands);
                inst->replaceUsesWith(makeStruct);
                inst->removeAndDeallocate();
            }
            else
            {
                inst->replaceUsesWith(inst->getOperand(0));
                inst->removeAndDeallocate();
            }
        }

        void processMakeOptionalNone(IRMakeOptionalNone* inst)
        {
            IRBuilder builderStorage(sharedBuilderStorage);
            auto builder = &builderStorage;
            builder->setInsertBefore(inst);

            auto info = getLoweredOptionalType(builder, inst->getDataType());
            if (info->loweredType != info->valueType)
            {
                List<IRInst*> operands;
                operands.add(inst->getDefaultValue());
                operands.add(builder->getBoolValue(false));
                auto makeStruct = builder->emitMakeStruct(info->loweredType, operands);
                inst->replaceUsesWith(makeStruct);
                inst->removeAndDeallocate();
            }
            else
            {
                inst->replaceUsesWith(builder->getNullPtrValue(info->valueType));
                inst->removeAndDeallocate();
            }
        }

        IRInst* getOptionalHasValue(IRBuilder* builder, IRInst* optionalInst)
        {
            auto loweredOptionalTypeInfo = getLoweredOptionalType(builder, optionalInst->getDataType());
            SLANG_ASSERT(loweredOptionalTypeInfo);
            IRInst* result = nullptr;
            if (loweredOptionalTypeInfo->loweredType != loweredOptionalTypeInfo->valueType)
            {
                result = builder->emitFieldExtract(
                    builder->getBoolType(),
                    optionalInst,
                    loweredOptionalTypeInfo->hasValueField->getKey());
            }
            else
            {
                result = builder->emitCastPtrToBool(optionalInst);
            }
            return result;
        }

        void processGetOptionalHasValue(IROptionalHasValue* inst)
        {
            IRBuilder builderStorage(sharedBuilderStorage);
            auto builder = &builderStorage;
            builder->setInsertBefore(inst);

            auto optionalValue = inst->getOptionalOperand();
            auto hasVal = getOptionalHasValue(builder, optionalValue);
            inst->replaceUsesWith(hasVal);
            inst->removeAndDeallocate();
        }

        void processGetOptionalValue(IRGetOptionalValue* inst)
        {
            IRBuilder builderStorage(sharedBuilderStorage);
            auto builder = &builderStorage;
            builder->setInsertBefore(inst);

            auto base = inst->getOptionalOperand();
            auto loweredOptionalTypeInfo = getLoweredOptionalType(builder, base->getDataType());
            if (loweredOptionalTypeInfo->loweredType != loweredOptionalTypeInfo->valueType)
            {
                SLANG_ASSERT(loweredOptionalTypeInfo);
                SLANG_ASSERT(loweredOptionalTypeInfo->valueField);
                auto getElement = builder->emitFieldExtract(
                    loweredOptionalTypeInfo->valueType,
                    base,
                    loweredOptionalTypeInfo->valueField->getKey());
                inst->replaceUsesWith(getElement);
            }
            else
            {
                inst->replaceUsesWith(base);
            }
            inst->removeAndDeallocate();
        }

        void processOptionalType(IROptionalType* inst)
        {
            IRBuilder builderStorage(sharedBuilderStorage);
            auto builder = &builderStorage;
            builder->setInsertBefore(inst);

            auto loweredOptionalTypeInfo = getLoweredOptionalType(builder, inst);
            SLANG_ASSERT(loweredOptionalTypeInfo);
            SLANG_UNUSED(loweredOptionalTypeInfo);
        }

        void processInst(IRInst* inst)
        {
            switch (inst->getOp())
            {
            case kIROp_MakeOptionalValue:
                processMakeOptionalValue((IRMakeOptionalValue*)inst);
                break;
            case kIROp_MakeOptionalNone:
                processMakeOptionalNone((IRMakeOptionalNone*)inst);
                break;
            case kIROp_OptionalHasValue:
                processGetOptionalHasValue((IROptionalHasValue*)inst);
                break;
            case kIROp_GetOptionalValue:
                processGetOptionalValue((IRGetOptionalValue*)inst);
                break;
            case kIROp_OptionalType:
                processOptionalType((IROptionalType*)inst);
                break;
            default:
                break;
            }
        }

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

            // Deduplicate equivalent types.
            sharedBuilder->deduplicateAndRebuildGlobalNumberingMap();

            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 optional types with lowered struct types.
            for (auto kv : loweredOptionalTypes)
            {
                kv.Key->replaceUsesWith(kv.Value->loweredType);
            }
        }
    };
    
    void lowerOptionalType(IRModule* module, DiagnosticSink* sink)
    {
        OptionalTypeLoweringContext context;
        context.module = module;
        context.sink = sink;
        context.processModule();
    }
}