summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-ir-specialize-dynamic-associatedtype-lookup.cpp
blob: 5bbc62bed5a096709ffbe536aaeac534c404040e (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
#include "slang-ir-generics-lowering-context.h"
#include "slang-ir-insts.h"
#include "slang-ir-specialize-dispatch.h"
#include "slang-ir-util.h"
#include "slang-ir.h"

namespace Slang
{

struct AssociatedTypeLookupSpecializationContext
{
    SharedGenericsLoweringContext* sharedContext;

    IRFunc* createWitnessTableLookupFunc(IRInterfaceType* interfaceType, IRInst* key)
    {
        IRBuilder builder(sharedContext->module);
        builder.setInsertBefore(interfaceType);

        auto inputWitnessTableIDType = builder.getWitnessTableIDType(interfaceType);
        auto requirementEntry = sharedContext->findInterfaceRequirementVal(interfaceType, key);

        auto resultWitnessTableType = cast<IRWitnessTableType>(requirementEntry);
        auto resultWitnessTableIDType =
            builder.getWitnessTableIDType((IRType*)resultWitnessTableType->getConformanceType());

        auto funcType =
            builder.getFuncType(1, (IRType**)&inputWitnessTableIDType, resultWitnessTableIDType);
        auto func = builder.createFunc();
        func->setFullType(funcType);

        if (auto linkage = key->findDecoration<IRLinkageDecoration>())
            builder.addNameHintDecoration(func, linkage->getMangledName());

        builder.setInsertInto(func);

        auto block = builder.emitBlock();
        auto witnessTableParam = builder.emitParam(inputWitnessTableIDType);

        // `witnessTableParam` is expected to have `IRWitnessTableID` type, which
        // will later lower into a `uint2`. We only use the first element of the uint2
        // to store the sequential ID and reserve the second 32-bit value for future
        // pointer-compatibility. We insert a member extract inst right now
        // to obtain the first element and use it in our switch statement.
        UInt elemIdx = 0;
        auto witnessTableSequentialID =
            builder.emitSwizzle(builder.getUIntType(), witnessTableParam, 1, &elemIdx);

        // Collect all witness tables of `witnessTableType` in current module.
        List<IRWitnessTable*> witnessTables =
            sharedContext->getWitnessTablesFromInterfaceType(interfaceType);

        // Generate case blocks for each possible witness table.
        IRBlock* defaultBlock = nullptr;
        List<IRInst*> caseBlocks;
        for (Index i = 0; i < witnessTables.getCount(); i++)
        {
            auto witnessTable = witnessTables[i];
            auto seqIdDecoration = witnessTable->findDecoration<IRSequentialIDDecoration>();
            SLANG_ASSERT(seqIdDecoration);

            if (i != witnessTables.getCount() - 1)
            {
                // Create a case block if we are not the last case.
                caseBlocks.add(seqIdDecoration->getSequentialIDOperand());
                builder.setInsertInto(func);
                auto caseBlock = builder.emitBlock();
                caseBlocks.add(caseBlock);
            }
            else
            {
                // Generate code for the last possible value in the `default` block.
                builder.setInsertInto(func);
                defaultBlock = builder.emitBlock();
                builder.setInsertInto(defaultBlock);
            }

            auto resultWitnessTable = findWitnessTableEntry(witnessTable, key);
            auto resultWitnessTableIDDecoration =
                resultWitnessTable->findDecoration<IRSequentialIDDecoration>();
            SLANG_ASSERT(resultWitnessTableIDDecoration);
            // Pack the resulting witness table ID into a `uint2`.
            auto uint2Type = builder.getVectorType(
                builder.getUIntType(),
                builder.getIntValue(builder.getIntType(), 2));
            IRInst* uint2Args[] = {
                resultWitnessTableIDDecoration->getSequentialIDOperand(),
                builder.getIntValue(builder.getUIntType(), 0)};
            auto resultID = builder.emitMakeVector(uint2Type, 2, uint2Args);
            builder.emitReturn(resultID);
        }

        builder.setInsertInto(func);

        if (witnessTables.getCount() == 1)
        {
            // If there is only 1 case, no switch statement is necessary.
            builder.setInsertInto(block);
            builder.emitBranch(defaultBlock);
        }
        else
        {
            // If there are more than 1 cases,
            // emit a switch statement to return the correct witness table ID based on
            // the witness table ID passed in.
            auto breakBlock = builder.emitBlock();
            builder.setInsertInto(breakBlock);
            builder.emitUnreachable();

            builder.setInsertInto(block);
            builder.emitSwitch(
                witnessTableSequentialID,
                breakBlock,
                defaultBlock,
                caseBlocks.getCount(),
                caseBlocks.getBuffer());
        }

        return func;
    }

    void processLookupInterfaceMethodInst(IRLookupWitnessMethod* inst)
    {
        if (isComInterfaceType(inst->getWitnessTable()->getDataType()))
        {
            return;
        }

        // Ignore lookups for RTTI objects for now, since they are not used anywhere.
        if (!as<IRWitnessTableType>(inst->getDataType()))
        {
            IRBuilder builder(sharedContext->module);
            builder.setInsertBefore(inst);
            auto uint2Type = builder.getVectorType(
                builder.getUIntType(),
                builder.getIntValue(builder.getIntType(), 2));
            auto zero = builder.getIntValue(builder.getUIntType(), 0);
            IRInst* args[] = {zero, zero};
            auto zeroUint2 = builder.emitMakeVector(uint2Type, 2, args);
            inst->replaceUsesWith(zeroUint2);
            return;
        }

        // Replace all witness table lookups with calls to specialized functions that directly
        // returns the sequential ID of the resulting witness table, effectively getting rid
        // of actual witness table objects in the target code (they all become IDs).
        auto witnessTableType = inst->getWitnessTable()->getDataType();
        IRInterfaceType* interfaceType = cast<IRInterfaceType>(
            cast<IRWitnessTableTypeBase>(witnessTableType)->getConformanceType());
        if (!interfaceType)
            return;
        auto key = inst->getRequirementKey();
        IRFunc* func = nullptr;
        if (!sharedContext->mapInterfaceRequirementKeyToDispatchMethods.tryGetValue(key, func))
        {
            func = createWitnessTableLookupFunc(interfaceType, key);
            sharedContext->mapInterfaceRequirementKeyToDispatchMethods[key] = func;
        }
        IRBuilder builder(sharedContext->module);
        builder.setInsertBefore(inst);
        auto witnessTableArg = inst->getWitnessTable();
        auto callInst = builder.emitCallInst(func->getResultType(), func, witnessTableArg);
        inst->replaceUsesWith(callInst);
        inst->removeAndDeallocate();
    }

    void processGetSequentialIDInst(IRGetSequentialID* inst)
    {
        // If the operand is a witness table, it is already replaced with a uint2
        // at this point, where the first element in the uint2 is the id of the
        // witness table.
        IRBuilder builder(sharedContext->module);
        builder.setInsertBefore(inst);
        UInt index = 0;
        auto id = builder.emitSwizzle(builder.getUIntType(), inst->getRTTIOperand(), 1, &index);
        inst->replaceUsesWith(id);
        inst->removeAndDeallocate();
    }

    void processModule()
    {
        // Replace all `lookup_interface_method():IRWitnessTable` with call to specialized
        // functions.
        workOnModule(
            sharedContext,
            [this](IRInst* inst)
            {
                if (inst->getOp() == kIROp_LookupWitnessMethod)
                {
                    processLookupInterfaceMethodInst(cast<IRLookupWitnessMethod>(inst));
                }
            });

        // Replace all direct uses of IRWitnessTables with its sequential ID.
        workOnModule(
            sharedContext,
            [this](IRInst* inst)
            {
                if (inst->getOp() == kIROp_WitnessTable)
                {
                    auto seqId = inst->findDecoration<IRSequentialIDDecoration>();
                    if (!seqId)
                        return;
                    // Insert code to pack sequential ID into an uint2 at all use sites.
                    traverseUses(
                        inst,
                        [&](IRUse* use)
                        {
                            if (as<IRCOMWitnessDecoration>(use->getUser()))
                            {
                                return;
                            }
                            IRBuilder builder(sharedContext->module);
                            builder.setInsertBefore(use->getUser());
                            auto uint2Type = builder.getVectorType(
                                builder.getUIntType(),
                                builder.getIntValue(builder.getIntType(), 2));
                            IRInst* uint2Args[] = {
                                seqId->getSequentialIDOperand(),
                                builder.getIntValue(builder.getUIntType(), 0)};
                            auto uint2seqID = builder.emitMakeVector(uint2Type, 2, uint2Args);
                            builder.replaceOperand(use, uint2seqID);
                        });
                }
            });

        // Replace all `IRWitnessTableType`s with `IRWitnessTableIDType`.
        for (auto globalInst : sharedContext->module->getGlobalInsts())
        {
            if (globalInst->getOp() == kIROp_WitnessTableType)
            {
                IRBuilder builder(sharedContext->module);
                builder.setInsertBefore(globalInst);
                auto witnessTableIDType = builder.getWitnessTableIDType(
                    (IRType*)cast<IRWitnessTableType>(globalInst)->getConformanceType());
                traverseUses(
                    globalInst,
                    [&](IRUse* use)
                    {
                        if (use->getUser()->getOp() == kIROp_WitnessTable)
                            return;
                        builder.replaceOperand(use, witnessTableIDType);
                    });
            }
        }

        // `GetSequentialID(WitnessTableIDOperand)` becomes just `WitnessTableIDOperand`.
        workOnModule(
            sharedContext,
            [this](IRInst* inst)
            {
                if (inst->getOp() == kIROp_GetSequentialID)
                {
                    processGetSequentialIDInst(cast<IRGetSequentialID>(inst));
                }
            });
    }
};

void specializeDynamicAssociatedTypeLookup(SharedGenericsLoweringContext* sharedContext)
{
    AssociatedTypeLookupSpecializationContext context;
    context.sharedContext = sharedContext;
    context.processModule();
}

} // namespace Slang