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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
// slang-ir-lower-generic-existential.cpp
#include "slang-ir-lower-witness-lookup.h"
#include "slang-ir-clone.h"
#include "slang-ir-generics-lowering-context.h"
#include "slang-ir-insts.h"
#include "slang-ir-util.h"
#include "slang-ir.h"
namespace Slang
{
struct WitnessLookupLoweringContext
{
IRModule* module;
DiagnosticSink* sink;
Dictionary<IRStructKey*, IRInst*> witnessDispatchFunctions;
void init()
{
// Reconstruct the witness dispatch functions map.
for (auto inst : module->getGlobalInsts())
{
if (auto key = as<IRStructKey>(inst))
{
for (auto decor : key->getDecorations())
{
if (auto witnessDispatchFunc = as<IRDispatchFuncDecoration>(decor))
{
witnessDispatchFunctions.add(key, witnessDispatchFunc->getFunc());
}
}
}
}
}
bool hasAssocType(IRInst* type)
{
if (!type)
return false;
InstHashSet processedSet(type->getModule());
InstWorkList workList(type->getModule());
workList.add(type);
processedSet.add(type);
for (Index i = 0; i < workList.getCount(); i++)
{
auto inst = workList[i];
if (inst->getOp() == kIROp_AssociatedType)
return true;
for (UInt j = 0; j < inst->getOperandCount(); j++)
{
if (!inst->getOperand(j))
continue;
if (processedSet.add(inst->getOperand(j)))
workList.add(inst->getOperand(j));
}
}
return false;
}
IRType* translateType(IRBuilder builder, IRInst* type)
{
if (!type)
return nullptr;
if (auto genType = as<IRGeneric>(type))
{
IRCloneEnv cloneEnv;
builder.setInsertBefore(genType);
auto newGeneric = as<IRGeneric>(cloneInst(&cloneEnv, &builder, genType));
newGeneric->setFullType(builder.getGenericKind());
auto retVal = findGenericReturnVal(newGeneric);
builder.setInsertBefore(retVal);
auto translated = translateType(builder, retVal);
retVal->replaceUsesWith(translated);
return (IRType*)newGeneric;
}
else if (auto thisType = as<IRThisType>(type))
{
return (IRType*)thisType->getConstraintType();
}
else if (auto assocType = as<IRAssociatedType>(type))
{
return assocType;
}
if (as<IRBasicType>(type))
return (IRType*)type;
switch (type->getOp())
{
case kIROp_Param:
case kIROp_VectorType:
case kIROp_MatrixType:
case kIROp_StructType:
case kIROp_ClassType:
case kIROp_InterfaceType:
case kIROp_LookupWitnessMethod:
return (IRType*)type;
default:
{
List<IRInst*> translatedOperands;
for (UInt i = 0; i < type->getOperandCount(); i++)
{
translatedOperands.add(translateType(builder, type->getOperand(i)));
}
auto translated = builder.emitIntrinsicInst(
type->getFullType(),
type->getOp(),
(UInt)translatedOperands.getCount(),
translatedOperands.getBuffer());
return (IRType*)translated;
}
}
}
IRInst* findOrCreateDispatchFunc(IRLookupWitnessMethod* lookupInst)
{
IRInst* func = nullptr;
auto requirementKey = cast<IRStructKey>(lookupInst->getRequirementKey());
if (witnessDispatchFunctions.tryGetValue(requirementKey, func))
{
return func;
}
auto witnessTableOperand = lookupInst->getWitnessTable();
auto witnessTableType = as<IRWitnessTableTypeBase>(witnessTableOperand->getDataType());
SLANG_RELEASE_ASSERT(witnessTableType);
auto interfaceType =
as<IRInterfaceType>(unwrapAttributedType(witnessTableType->getConformanceType()));
SLANG_RELEASE_ASSERT(interfaceType);
if (interfaceType->findDecoration<IRComInterfaceDecoration>())
return nullptr;
auto requirementType = findInterfaceRequirement(interfaceType, requirementKey);
SLANG_RELEASE_ASSERT(requirementType);
// We only lower non-static function requirement lookups for now.
// Our front end will stick a StaticRequirementDecoration on the IRStructKey for static
// member requirements.
if (lookupInst->getRequirementKey()->findDecoration<IRStaticRequirementDecoration>())
return nullptr;
auto interfaceMethodFuncType =
as<IRFuncType>(getResolvedInstForDecorations(requirementType));
if (interfaceMethodFuncType)
{
// Detect cases that we currently does not support and exit.
// If this is a non static function requirement, we should
// make sure the first parameter is the interface type. If not, something has gone
// wrong.
if (interfaceMethodFuncType->getParamCount() == 0)
return nullptr;
if (!as<IRThisType>(unwrapAttributedType(interfaceMethodFuncType->getParamType(0))))
return nullptr;
// The function has any associated type parameter, we currently can't lower it early in
// this pass. We will lower it in the catch all generic lowering pass.
for (UInt i = 1; i < interfaceMethodFuncType->getParamCount(); i++)
{
if (hasAssocType(interfaceMethodFuncType->getParamType(i)))
return nullptr;
}
// If return type is a composite type containing an assoc type, we won't lower it now.
// Supporting general use of assoc type is possible, but would require more complex
// logic in this pass to marshal things to and from existential types.
if (interfaceMethodFuncType->getResultType()->getOp() != kIROp_AssociatedType &&
hasAssocType(interfaceMethodFuncType->getResultType()))
return nullptr;
}
else
{
return nullptr;
}
IRBuilder builder(module);
builder.setInsertBefore(getParentFunc(lookupInst));
// Create a dispatch func.
IRFunc* dispatchFunc = nullptr;
IRFuncType* dispatchFuncType = nullptr;
IRGeneric* parentGeneric = nullptr;
// If requirementType is a generic, we need to create a new generic that has the same
// parameters.
if (auto genericRequirement = as<IRGeneric>(requirementType))
{
IRCloneEnv cloneEnv;
parentGeneric = as<IRGeneric>(cloneInst(&cloneEnv, &builder, genericRequirement));
auto returnInst = as<IRReturn>(parentGeneric->getFirstBlock()->getLastInst());
SLANG_RELEASE_ASSERT(returnInst);
builder.setInsertBefore(returnInst);
auto oldDispatchFuncType = as<IRFuncType>(returnInst->getVal());
if (!oldDispatchFuncType)
return nullptr;
dispatchFuncType = as<IRFuncType>(translateType(builder, oldDispatchFuncType));
SLANG_RELEASE_ASSERT(dispatchFuncType);
dispatchFunc = builder.createFunc();
dispatchFunc->setFullType(dispatchFuncType);
builder.emitReturn(dispatchFunc);
returnInst->removeAndDeallocate();
parentGeneric->setFullType(translateType(builder, requirementType));
}
else
{
dispatchFuncType = as<IRFuncType>(translateType(builder, requirementType));
dispatchFunc = builder.createFunc();
dispatchFunc->setFullType(dispatchFuncType);
}
// We need to inline this function if the requirement is differentiable,
// so that the autodiff pass doesn't need to handle the dispatch function.
if (requirementKey->findDecoration<IRForwardDerivativeDecoration>() ||
requirementKey->findDecoration<IRBackwardDerivativeDecoration>())
{
builder.addForceInlineDecoration(dispatchFunc);
}
// Collect generic params.
List<IRInst*> genericParams;
if (parentGeneric)
{
for (auto param : parentGeneric->getParams())
genericParams.add(param);
}
// Emit the body of the dispatch func.
builder.setInsertInto(dispatchFunc);
auto firstBlock = builder.emitBlock();
auto firstBlockBuilder = builder;
// Emit parameters.
List<IRInst*> params;
for (UInt i = 0; i < dispatchFuncType->getParamCount(); i++)
{
params.add(builder.emitParam(dispatchFuncType->getParamType(i)));
}
auto witness = builder.emitExtractExistentialWitnessTable(params[0]);
auto witnessTables = getWitnessTablesFromInterfaceType(module, interfaceType);
if (witnessTables.getCount() == 0)
{
// If there is no witness table, we should emit an error.
sink->diagnose(
lookupInst,
Diagnostics::noTypeConformancesFoundForInterface,
interfaceType);
return nullptr;
}
else
{
List<IRInst*> cases;
for (auto witnessTable : witnessTables)
{
IRBlock* block = builder.emitBlock();
auto caseValue = firstBlockBuilder.emitGetSequentialIDInst(witnessTable);
cases.add(caseValue);
cases.add(block);
auto entry = findWitnessTableEntry(witnessTable, requirementKey);
SLANG_RELEASE_ASSERT(entry);
// If the entry is a generic, we need to specialize it.
if (const auto genericEntry = as<IRGeneric>(entry))
{
auto specializedFuncType = builder.emitSpecializeInst(
builder.getTypeKind(),
entry->getFullType(),
(UInt)genericParams.getCount(),
genericParams.getBuffer());
entry = builder.emitSpecializeInst(
(IRType*)specializedFuncType,
entry,
(UInt)genericParams.getCount(),
genericParams.getBuffer());
}
auto args = params;
// Reinterpret the first arg into the concrete type.
args[0] = builder.emitReinterpret(
witnessTable->getConcreteType(),
builder.emitExtractExistentialValue(
builder.emitExtractExistentialType(args[0]),
args[0]));
auto calleeFuncType =
as<IRFuncType>(getResolvedInstForDecorations(entry)->getFullType());
auto callReturnType = calleeFuncType->getResultType();
if (callReturnType->getParent() != module->getModuleInst())
{
// the return type is dependent on generic parameter, use the type from
// dispatchFuncType instead.
callReturnType = dispatchFuncType->getResultType();
}
IRInst* ret = builder.emitCallInst(
callReturnType,
entry,
(UInt)args.getCount(),
args.getBuffer());
// If result type is an associated type, we need to pack it into an anyValue.
if (as<IRAssociatedType>(dispatchFuncType->getResultType()))
{
ret = builder.emitPackAnyValue(dispatchFuncType->getResultType(), ret);
}
builder.emitReturn(ret);
}
builder.setInsertInto(firstBlock);
if (witnessTables.getCount() == 1)
{
builder.emitBranch((IRBlock*)cases[1]);
}
else
{
auto witnessId = firstBlockBuilder.emitGetSequentialIDInst(witness);
auto breakLabel = builder.emitBlock();
builder.emitUnreachable();
firstBlockBuilder.emitSwitch(
witnessId,
breakLabel,
(IRBlock*)cases.getLast(),
(UInt)(cases.getCount() - 2),
cases.getBuffer());
}
}
// Stick a decoration on the requirement key so we can find the dispatch func later.
IRInst* resultValue = parentGeneric ? (IRInst*)parentGeneric : dispatchFunc;
builder.addDispatchFuncDecoration(requirementKey, resultValue);
// Register the dispatch func to witnessDispatchFunctions dictionary.
witnessDispatchFunctions[requirementKey] = resultValue;
return resultValue;
}
void rewriteCallSite(IRCall* call, IRInst* dispatchFunc, IRInst* initialExistentialObject)
{
SLANG_RELEASE_ASSERT(call->getArgCount() != 0);
call->setOperand(0, dispatchFunc);
IRBuilder builder(call);
builder.setInsertBefore(call);
auto witnessTable = builder.emitExtractExistentialWitnessTable(initialExistentialObject);
auto newExistentialObject = builder.emitMakeExistential(
initialExistentialObject->getDataType(),
call->getOperand(1),
witnessTable);
call->setOperand(1, newExistentialObject);
}
bool processWitnessLookup(IRLookupWitnessMethod* lookupInst)
{
auto witnessTableOperand = lookupInst->getWitnessTable();
auto extractInst = as<IRExtractExistentialWitnessTable>(witnessTableOperand);
if (!extractInst)
return false;
auto dispatchFunc = findOrCreateDispatchFunc(lookupInst);
if (!dispatchFunc)
return false;
bool changed = false;
auto existentialObject = extractInst->getOperand(0);
IRBuilder builder(lookupInst);
builder.setInsertBefore(lookupInst);
traverseUses(
lookupInst,
[&](IRUse* use)
{
if (auto specialize = as<IRSpecialize>(use->getUser()))
{
builder.setInsertBefore(use->getUser());
List<IRInst*> args;
for (UInt i = 0; i < specialize->getArgCount(); i++)
args.add(specialize->getArg(i));
auto specializedType = builder.emitSpecializeInst(
builder.getTypeKind(),
dispatchFunc->getFullType(),
(UInt)args.getCount(),
args.getBuffer());
auto newSpecialize = builder.emitSpecializeInst(
(IRType*)specializedType,
dispatchFunc,
(UInt)args.getCount(),
args.getBuffer());
traverseUses(
specialize,
[&](IRUse* specializeUse)
{
if (auto call = as<IRCall>(specializeUse->getUser()))
{
changed = true;
rewriteCallSite(call, newSpecialize, existentialObject);
}
});
}
else if (auto call = as<IRCall>(use->getUser()))
{
changed = true;
rewriteCallSite(call, dispatchFunc, existentialObject);
}
});
return changed;
}
bool processFunc(IRFunc* func)
{
bool changed = false;
for (auto bb : func->getBlocks())
{
for (auto inst : bb->getModifiableChildren())
{
if (auto witnessLookupInst = as<IRLookupWitnessMethod>(inst))
{
changed |= processWitnessLookup(witnessLookupInst);
}
}
}
return changed;
}
};
bool lowerWitnessLookup(IRModule* module, DiagnosticSink* sink)
{
bool changed = false;
WitnessLookupLoweringContext context;
context.module = module;
context.sink = sink;
context.init();
for (auto inst : module->getGlobalInsts())
{
// Process all fully specialized functions and look for
// witness lookup instructions. If we see a lookup for a non-static function,
// create a dispatch function and replace the lookup with a call to the dispatch function.
if (auto func = as<IRFunc>(inst))
changed |= context.processFunc(func);
}
return changed;
}
} // namespace Slang
|