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
|
#include "slang-ir-specialize-dispatch.h"
#include "slang-ir-generics-lowering-context.h"
#include "slang-ir-insts.h"
#include "slang-ir.h"
namespace Slang
{
IRInst* findWitnessTableEntry(IRWitnessTable* table, IRInst* key)
{
for (auto entry : table->getEntries())
{
if (entry->getRequirementKey() == key)
return entry->getSatisfyingVal();
}
return nullptr;
}
void specializeDispatchFunction(SharedGenericsLoweringContext* sharedContext, IRFunc* dispatchFunc)
{
auto witnessTableType = cast<IRFuncType>(dispatchFunc->getDataType())->getParamType(0);
// Collect all witness tables of `witnessTableType` in current module.
List<IRWitnessTable*> witnessTables;
for (auto globalInst : sharedContext->module->getGlobalInsts())
{
if (globalInst->op == kIROp_WitnessTable && globalInst->getDataType() == witnessTableType)
{
witnessTables.add(cast<IRWitnessTable>(globalInst));
}
}
SLANG_ASSERT(dispatchFunc->getFirstBlock() == dispatchFunc->getLastBlock());
auto block = dispatchFunc->getFirstBlock();
// The dispatch function before modification must be in the form of
// call(lookup_interface_method(witnessTableParam, interfaceReqKey), args)
// We now find the relavent instructions.
IRCall* callInst = nullptr;
IRLookupWitnessMethod* lookupInst = nullptr;
IRReturn* returnInst = nullptr;
for (auto inst : block->getOrdinaryInsts())
{
switch (inst->op)
{
case kIROp_Call:
callInst = cast<IRCall>(inst);
break;
case kIROp_lookup_interface_method:
lookupInst = cast<IRLookupWitnessMethod>(inst);
break;
case kIROp_ReturnVal:
case kIROp_ReturnVoid:
returnInst = cast<IRReturn>(inst);
break;
default:
break;
}
}
SLANG_ASSERT(callInst && lookupInst && returnInst);
IRBuilder builderStorage;
auto builder = &builderStorage;
builder->sharedBuilder = &sharedContext->sharedBuilderStorage;
builder->setInsertBefore(callInst);
auto witnessTableParam = block->getFirstParam();
auto requirementKey = lookupInst->getRequirementKey();
List<IRInst*> params;
for (auto param = block->getFirstParam()->getNextParam(); param; param = param->getNextParam())
{
params.add(param);
}
// Emit cascaded if statements to call the correct concrete function based on
// the witness table pointer passed in.
auto ifBlock = block;
for (Index i = 0; i < witnessTables.getCount(); i++)
{
auto witnessTable = witnessTables[i];
bool isLast = (i == witnessTables.getCount() - 1);
IRInst* cmpArgs[] =
{
builder->emitBitCast(builder->getUInt64Type(), witnessTableParam),
builder->emitBitCast(builder->getUInt64Type(),(IRInst*)witnessTable)
};
IRInst* condition = nullptr;
IRBlock* trueBlock = nullptr;
if (!isLast)
{
condition = builder->emitIntrinsicInst(builder->getBoolType(), kIROp_Eql, 2, cmpArgs);
trueBlock = builder->emitBlock();
}
auto callee = findWitnessTableEntry(witnessTable, requirementKey);
SLANG_ASSERT(callee);
auto specializedCallInst = builder->emitCallInst(callInst->getFullType(), callee, params);
if (callInst->getDataType()->op == kIROp_VoidType)
builder->emitReturn();
else
builder->emitReturn(specializedCallInst);
if (!isLast)
{
auto falseBlock = builder->emitBlock();
builder->setInsertInto(ifBlock);
builder->emitIf(condition, trueBlock, falseBlock);
builder->setInsertInto(falseBlock);
ifBlock = falseBlock;
}
}
// Remove old implementation.
lookupInst->removeAndDeallocate();
callInst->removeAndDeallocate();
returnInst->removeAndDeallocate();
}
// Ensures every witness table object has been assigned a sequential ID.
// All witness tables will have a SequentialID decoration after this function is run.
// The sequantial ID in the decoration will be the same as the one specified in the Linkage.
// Otherwise, a new ID will be generated and assigned to the witness table object, and
// the sequantial ID map in the Linkage will be updated to include the new ID, so they
// can be looked up by the user via future Slang API calls.
void ensureWitnessTableSequentialIDs(SharedGenericsLoweringContext* sharedContext)
{
auto linkage = sharedContext->targetReq->getLinkage();
for (auto inst : sharedContext->module->getGlobalInsts())
{
if (inst->op == kIROp_WitnessTable)
{
UnownedStringSlice witnessTableMangledName;
if (auto instLinkage = inst->findDecoration<IRLinkageDecoration>())
{
witnessTableMangledName = instLinkage->getMangledName();
}
else
{
// If this witness table entry does not have a linkage,
// don't assign sequential ID for it.
continue;
}
// If the inst already has a SequentialIDDecoration, stop now.
if (inst->findDecoration<IRSequentialIDDecoration>())
continue;
// Get a sequential ID for the witness table using the map from the Linkage.
uint32_t seqID = 0;
if (!linkage->mapMangledNameToRTTIObjectIndex.TryGetValue(
witnessTableMangledName, seqID))
{
auto interfaceType =
cast<IRWitnessTableType>(inst->getDataType())->getConformanceType();
auto interfaceLinkage = interfaceType->findDecoration<IRLinkageDecoration>();
SLANG_ASSERT(
interfaceLinkage && "An interface type does not have a linkage,"
"but a witness table associated with it has one.");
auto interfaceName = interfaceLinkage->getMangledName();
auto idAllocator =
linkage->mapInterfaceMangledNameToSequentialIDCounters.TryGetValue(
interfaceName);
if (!idAllocator)
{
linkage->mapInterfaceMangledNameToSequentialIDCounters[interfaceName] = 0;
idAllocator =
linkage->mapInterfaceMangledNameToSequentialIDCounters.TryGetValue(
interfaceName);
}
seqID = *idAllocator;
++(*idAllocator);
linkage->mapMangledNameToRTTIObjectIndex[witnessTableMangledName] = seqID;
}
// Add a decoration to the inst.
IRBuilder builder;
builder.sharedBuilder = &sharedContext->sharedBuilderStorage;
builder.setInsertBefore(inst);
builder.addSequentialIDDecoration(inst, seqID);
}
}
}
void specializeDispatchFunctions(SharedGenericsLoweringContext* sharedContext)
{
sharedContext->sharedBuilderStorage.deduplicateAndRebuildGlobalNumberingMap();
// First we ensure that all witness table objects has a sequential ID assigned.
ensureWitnessTableSequentialIDs(sharedContext);
for (auto kv : sharedContext->mapInterfaceRequirementKeyToDispatchMethods)
{
auto dispatchFunc = kv.Value;
specializeDispatchFunction(sharedContext, dispatchFunc);
}
}
} // namespace Slang
|