summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-ir-lower-existential.cpp
blob: ea7f906a9a66c716f8364ef4fd6da2316e11f4c2 (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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// slang-ir-lower-generic-existential.cpp

#include "slang-ir-lower-existential.h"

#include "slang-ir-generics-lowering-context.h"
#include "slang-ir-insts.h"
#include "slang-ir-util.h"
#include "slang-ir.h"

namespace Slang
{
bool isCPUTarget(TargetRequest* targetReq);
bool isCUDATarget(TargetRequest* targetReq);

struct ExistentialLoweringContext
{
    SharedGenericsLoweringContext* sharedContext;

    void processMakeExistential(IRMakeExistentialWithRTTI* inst)
    {
        IRBuilder builderStorage(sharedContext->module);
        auto builder = &builderStorage;
        builder->setInsertBefore(inst);
        auto value = inst->getWrappedValue();
        auto valueType = sharedContext->lowerType(builder, value->getDataType());
        if (valueType->getOp() == kIROp_ComPtrType)
            return;
        auto witnessTableType =
            cast<IRWitnessTableTypeBase>(inst->getWitnessTable()->getDataType());
        auto interfaceType = witnessTableType->getConformanceType();
        if (interfaceType->findDecoration<IRComInterfaceDecoration>())
            return;
        auto witnessTableIdType = builder->getWitnessTableIDType((IRType*)interfaceType);
        auto anyValueSize = sharedContext->getInterfaceAnyValueSize(interfaceType, inst->sourceLoc);
        auto anyValueType = builder->getAnyValueType(anyValueSize);
        auto rttiType = builder->getRTTIHandleType();
        auto tupleType = builder->getTupleType(rttiType, witnessTableIdType, anyValueType);

        IRInst* rttiObject = inst->getRTTI();
        if (auto type = as<IRType>(rttiObject))
        {
            rttiObject = sharedContext->maybeEmitRTTIObject(type);
            rttiObject = builder->emitGetAddress(rttiType, rttiObject);
        }
        IRInst* packedValue = value;
        if (valueType->getOp() != kIROp_AnyValueType)
            packedValue = builder->emitPackAnyValue(anyValueType, value);
        IRInst* tupleArgs[] = {rttiObject, inst->getWitnessTable(), packedValue};
        auto tuple = builder->emitMakeTuple(tupleType, 3, tupleArgs);
        inst->replaceUsesWith(tuple);
        inst->removeAndDeallocate();
    }

    // Translates `createExistentialObject` insts, which takes a user defined
    // type id and user defined value and turns into an existential value,
    // into a `makeTuple` inst that makes the tuple representing the lowered
    // existential value.
    void processCreateExistentialObject(IRCreateExistentialObject* inst)
    {
        IRBuilder builderStorage(sharedContext->module);
        auto builder = &builderStorage;
        builder->setInsertBefore(inst);

        // The result type of this `createExistentialObject` inst should already
        // be lowered into a `TupleType(rttiType, WitnessTableIDType, AnyValueType)`
        // in the previous `lowerGenericType` pass.
        auto tupleType = inst->getDataType();
        auto witnessTableIdType = cast<IRWitnessTableIDType>(tupleType->getOperand(1));
        auto anyValueType = cast<IRAnyValueType>(tupleType->getOperand(2));

        // Create a standin value for `rttiObject` for now since it will not be used
        // other than test for null in the case of `Optional<IFoo>`.
        auto uint2Type = builder->getVectorType(
            builder->getUIntType(),
            builder->getIntValue(builder->getIntType(), 2));
        IRInst* standinVal = builder->getIntValue(builder->getUIntType(), 0xFFFFFFFF);
        IRInst* zero = builder->getIntValue(builder->getUIntType(), 0);
        IRInst* standinRTTIVectorArgs[] = {standinVal, zero};
        IRInst* rttiObject = builder->emitMakeVector(uint2Type, 2, standinRTTIVectorArgs);

        // Pack the user provided value into `AnyValue`.
        IRInst* packedValue = inst->getValue();
        if (packedValue->getDataType()->getOp() != kIROp_AnyValueType)
            packedValue = builder->emitPackAnyValue(anyValueType, packedValue);

        // Use the user provided `typeID` value as the witness table ID field in the
        // newly constructed tuple.
        // All `WitnessTableID` types are lowered into `uint2`s, so we need to create
        // a `uint2` value from `typeID` to stay consistent with the convention.
        IRInst* vectorArgs[2] = {
            inst->getTypeID(),
            builder->getIntValue(builder->getUIntType(), 0)};

        IRInst* typeIdValue = builder->emitMakeVector(uint2Type, 2, vectorArgs);
        typeIdValue = builder->emitBitCast(witnessTableIdType, typeIdValue);
        IRInst* tupleArgs[] = {rttiObject, typeIdValue, packedValue};
        auto tuple = builder->emitMakeTuple(tupleType, 3, tupleArgs);
        inst->replaceUsesWith(tuple);
        inst->removeAndDeallocate();
    }

    IRInst* extractTupleElement(IRBuilder* builder, IRInst* value, UInt index)
    {
        auto tupleType = cast<IRTupleType>(sharedContext->lowerType(builder, value->getDataType()));
        auto getElement =
            builder->emitGetTupleElement((IRType*)tupleType->getOperand(index), value, index);
        return getElement;
    }

    void processExtractExistentialElement(IRInst* extractInst, UInt elementId)
    {
        IRBuilder builderStorage(sharedContext->module);
        auto builder = &builderStorage;
        builder->setInsertBefore(extractInst);

        IRInst* element = nullptr;
        if (isComInterfaceType(extractInst->getOperand(0)->getDataType()))
        {
            // If this is an COM interface, the elements (witness table/rtti) are just the interface
            // value itself.
            element = extractInst->getOperand(0);
        }
        else
        {
            element = extractTupleElement(builder, extractInst->getOperand(0), elementId);
        }
        extractInst->replaceUsesWith(element);
        extractInst->removeAndDeallocate();
    }

    void processExtractExistentialValue(IRExtractExistentialValue* inst)
    {
        processExtractExistentialElement(inst, 2);
    }

    void processIsNullExistential(IRIsNullExistential* inst)
    {
        IRBuilder builder(sharedContext->module);
        builder.setInsertBefore(inst);

        auto rttiElement = extractTupleElement(&builder, inst->getOperand(0), 0);
        auto isNull = builder.emitNeq(
            builder.emitGetElement(builder.getUIntType(), rttiElement, 0),
            builder.getIntValue(builder.getUIntType(), 0));
        inst->replaceUsesWith(isNull);
        inst->removeAndDeallocate();
    }

    void processExtractExistentialWitnessTable(IRExtractExistentialWitnessTable* inst)
    {
        processExtractExistentialElement(inst, 1);
    }

    void processExtractExistentialType(IRExtractExistentialType* extractInst)
    {
        IRBuilder builderStorage(sharedContext->module);
        auto builder = &builderStorage;
        builder->setInsertBefore(extractInst);

        IRInst* element = nullptr;
        IRInst* anyValueType = nullptr;
        if (isComInterfaceType(extractInst->getOperand(0)->getDataType()))
        {
            // If this is an COM interface, the elements (witness table/rtti) are just the interface
            // value itself.
            element = extractInst->getOperand(0);
        }
        else
        {
            element = extractTupleElement(builder, extractInst->getOperand(0), 0);
            if (auto tupleType = as<IRTupleType>(extractInst->getOperand(0)->getDataType()))
            {
                anyValueType = tupleType->getOperand(2);
            }
        }

        // If this instruction is used as a type, we need to replace it with the lowered type,
        // which should be an AnyValueType.
        // If it is used as a value, then we can replace it with the extracted element.
        auto isTypeUse = [](IRUse* use) -> bool
        {
            auto user = use->getUser();
            if (as<IRType>(user))
                return true;
            if (use == &use->getUser()->typeUse)
                return true;
            return false;
        };
        traverseUses(
            extractInst,
            [&](IRUse* use)
            {
                if (anyValueType && isTypeUse(use))
                {
                    builder->replaceOperand(use, anyValueType);
                    return;
                }
                builder->replaceOperand(use, element);
            });
    }

    void processGetValueFromBoundInterface(IRGetValueFromBoundInterface* inst)
    {
        IRBuilder builderStorage(sharedContext->module);
        auto builder = &builderStorage;
        builder->setInsertBefore(inst);
        if (inst->getDataType()->getOp() == kIROp_ClassType)
        {
            return;
        }
        // A value of interface will lower as a tuple, and
        // the third element of that tuple represents the
        // concrete value that was put into the existential.
        //
        auto element = extractTupleElement(builder, inst->getOperand(0), 2);
        auto elementType = element->getDataType();

        // There are two cases we expect to see for that
        // tuple element.
        //
        IRInst* replacement = nullptr;
        if (as<IRPseudoPtrType>(elementType))
        {
            // The first case is when legacy static specialization
            // is applied, and the element is a "pseudo-pointer."
            //
            // Semantically, we should emit a (pseudo-)load from the pseudo-pointer
            // to go from `PseudoPtr<T>` to `T`.
            //
            // TODO: Actually introduce and emit a "psedudo-load" instruction
            // here. For right now we are just using the value directly and
            // downstream passes seem okay with it, but it isn't really
            // type-correct to be doing this.
            //
            replacement = element;
        }
        else
        {
            // The second case is when the dynamic-dispatch layout is
            // being used, and the element is an "any-value."
            //
            // In this case we need to emit an unpacking operation
            // to get from `AnyValue` to `T`.
            //
            SLANG_ASSERT(as<IRAnyValueType>(elementType));
            replacement = builder->emitUnpackAnyValue(inst->getFullType(), element);
        }

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

    void processInst(IRInst* inst)
    {
        if (auto makeExistential = as<IRMakeExistentialWithRTTI>(inst))
        {
            processMakeExistential(makeExistential);
        }
        else if (auto createExistentialObject = as<IRCreateExistentialObject>(inst))
        {
            processCreateExistentialObject(createExistentialObject);
        }
        else if (auto getValueFromBoundInterface = as<IRGetValueFromBoundInterface>(inst))
        {
            processGetValueFromBoundInterface(getValueFromBoundInterface);
        }
        else if (auto extractExistentialVal = as<IRExtractExistentialValue>(inst))
        {
            processExtractExistentialValue(extractExistentialVal);
        }
        else if (auto extractExistentialType = as<IRExtractExistentialType>(inst))
        {
            processExtractExistentialType(extractExistentialType);
        }
        else if (auto extractExistentialWitnessTable = as<IRExtractExistentialWitnessTable>(inst))
        {
            processExtractExistentialWitnessTable(extractExistentialWitnessTable);
        }
        else if (auto isNullExistential = as<IRIsNullExistential>(inst))
        {
            processIsNullExistential(isNullExistential);
        }
    }

    void processModule()
    {
        sharedContext->addToWorkList(sharedContext->module->getModuleInst());

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

            sharedContext->workList.removeLast();
            sharedContext->workListSet.remove(inst);

            processInst(inst);

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


void lowerExistentials(SharedGenericsLoweringContext* sharedContext)
{
    ExistentialLoweringContext context;
    context.sharedContext = sharedContext;
    context.processModule();
}
} // namespace Slang