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
|
// slang-ir-witness-table-wrapper.cpp
#include "slang-ir-witness-table-wrapper.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 GenericsLoweringContext;
struct GenerateWitnessTableWrapperContext
{
SharedGenericsLoweringContext* sharedContext;
// Represents a work item for packing `inout` or `out` arguments after a concrete call.
struct ArgumentPackWorkItem
{
// A `AnyValue` typed destination.
IRInst* dstArg = nullptr;
// A concrete value to be packed.
IRInst* concreteArg = nullptr;
};
// Unpack an `arg` of `IRAnyValue` into concrete type if necessary, to make it feedable into the
// parameter. If `arg` represents a AnyValue typed variable passed in to a concrete `out`
// parameter, this function indicates that it needs to be packed after the call by setting
// `packAfterCall`.
IRInst* maybeUnpackArg(
IRBuilder* builder,
IRType* paramType,
IRInst* arg,
ArgumentPackWorkItem& packAfterCall)
{
packAfterCall.dstArg = nullptr;
packAfterCall.concreteArg = nullptr;
// If either paramType or argType is a pointer type
// (because of `inout` or `out` modifiers), we extract
// the underlying value type first.
IRType* paramValType = paramType;
IRType* argValType = arg->getDataType();
IRInst* argVal = arg;
if (auto ptrType = as<IRPtrTypeBase>(paramType))
{
paramValType = ptrType->getValueType();
}
auto argType = arg->getDataType();
if (auto argPtrType = as<IRPtrTypeBase>(argType))
{
argValType = argPtrType->getValueType();
argVal = builder->emitLoad(arg);
}
// Unpack `arg` if the parameter expects concrete type but
// `arg` is an AnyValue.
if (!as<IRAnyValueType>(paramValType) && as<IRAnyValueType>(argValType))
{
auto unpackedArgVal = builder->emitUnpackAnyValue(paramValType, argVal);
// if parameter expects an `out` pointer, store the unpacked val into a
// variable and pass in a pointer to that variable.
if (as<IRPtrTypeBase>(paramType))
{
auto tempVar = builder->emitVar(paramValType);
builder->emitStore(tempVar, unpackedArgVal);
// tempVar needs to be unpacked into original var after the call.
packAfterCall.dstArg = arg;
packAfterCall.concreteArg = tempVar;
return tempVar;
}
else
{
return unpackedArgVal;
}
}
return arg;
}
IRStringLit* _getWitnessTableWrapperFuncName(IRFunc* func)
{
IRBuilder builderStorage(sharedContext->module);
auto builder = &builderStorage;
builder->setInsertBefore(func);
if (auto linkageDecoration = func->findDecoration<IRLinkageDecoration>())
{
return builder->getStringValue(
(String(linkageDecoration->getMangledName()) + "_wtwrapper").getUnownedSlice());
}
if (auto namehintDecoration = func->findDecoration<IRNameHintDecoration>())
{
return builder->getStringValue(
(String(namehintDecoration->getName()) + "_wtwrapper").getUnownedSlice());
}
return nullptr;
}
IRFunc* emitWitnessTableWrapper(IRFunc* func, IRInst* interfaceRequirementVal)
{
auto funcTypeInInterface = cast<IRFuncType>(interfaceRequirementVal);
IRBuilder builderStorage(sharedContext->module);
auto builder = &builderStorage;
builder->setInsertBefore(func);
auto wrapperFunc = builder->createFunc();
wrapperFunc->setFullType((IRType*)interfaceRequirementVal);
if (auto name = _getWitnessTableWrapperFuncName(func))
builder->addNameHintDecoration(wrapperFunc, name);
builder->setInsertInto(wrapperFunc);
auto block = builder->emitBlock();
builder->setInsertInto(block);
ShortList<IRParam*> params;
for (UInt i = 0; i < funcTypeInInterface->getParamCount(); i++)
{
params.add(builder->emitParam(funcTypeInInterface->getParamType(i)));
}
List<IRInst*> args;
List<ArgumentPackWorkItem> argsToPack;
SLANG_ASSERT(params.getCount() == (Index)func->getParamCount());
for (UInt i = 0; i < func->getParamCount(); i++)
{
auto wrapperParam = params[i];
// Type of the parameter in the callee.
auto funcParamType = func->getParamType(i);
// If the implementation expects a concrete type
// (either in the form of a pointer for `out`/`inout` parameters,
// or in the form a value for `in` parameters, while
// the interface exposes an AnyValue type,
// we need to unpack the AnyValue argument to the appropriate
// concerete type.
ArgumentPackWorkItem packWorkItem;
auto newArg = maybeUnpackArg(builder, funcParamType, wrapperParam, packWorkItem);
args.add(newArg);
if (packWorkItem.concreteArg)
argsToPack.add(packWorkItem);
}
auto call = builder->emitCallInst(func->getResultType(), func, args);
// Pack all `out` arguments.
for (auto item : argsToPack)
{
auto anyValType = cast<IRPtrTypeBase>(item.dstArg->getDataType())->getValueType();
auto concreteVal = builder->emitLoad(item.concreteArg);
auto packedVal = builder->emitPackAnyValue(anyValType, concreteVal);
builder->emitStore(item.dstArg, packedVal);
}
// Pack return value if necessary.
if (!as<IRAnyValueType>(call->getDataType()) &&
as<IRAnyValueType>(funcTypeInInterface->getResultType()))
{
auto pack = builder->emitPackAnyValue(funcTypeInInterface->getResultType(), call);
builder->emitReturn(pack);
}
else
{
if (call->getDataType()->getOp() == kIROp_VoidType)
builder->emitReturn();
else
builder->emitReturn(call);
}
return wrapperFunc;
}
void lowerWitnessTable(IRWitnessTable* witnessTable)
{
auto interfaceType = as<IRInterfaceType>(witnessTable->getConformanceType());
if (!interfaceType)
return;
if (isBuiltin(interfaceType))
return;
if (isComInterfaceType(interfaceType))
return;
// We need to consider whether the concrete type that is conforming
// in this witness table actually fits within the declared any-value
// size for the interface.
//
// If the type doesn't fit then it would be invalid to use for dynamic
// dispatch, and the packing/unpacking operations we emit would fail
// to generate valid code.
//
// Such a type might still be useful for static specialization, so
// we can't consider this case a hard error.
//
auto concreteType = witnessTable->getConcreteType();
IRIntegerValue typeSize, sizeLimit;
bool isTypeOpaque = false;
if (!sharedContext->doesTypeFitInAnyValue(
concreteType,
interfaceType,
&typeSize,
&sizeLimit,
&isTypeOpaque))
{
HashSet<IRType*> visited;
if (isTypeOpaque)
{
sharedContext->sink->diagnose(
concreteType,
Diagnostics::typeCannotBePackedIntoAnyValue,
concreteType);
}
else
{
sharedContext->sink->diagnose(
concreteType,
Diagnostics::typeDoesNotFitAnyValueSize,
concreteType);
sharedContext->sink->diagnoseWithoutSourceView(
concreteType,
Diagnostics::typeAndLimit,
concreteType,
typeSize,
sizeLimit);
}
return;
}
for (auto child : witnessTable->getChildren())
{
auto entry = as<IRWitnessTableEntry>(child);
if (!entry)
continue;
auto interfaceRequirementVal = sharedContext->findInterfaceRequirementVal(
interfaceType,
entry->getRequirementKey());
if (auto ordinaryFunc = as<IRFunc>(entry->getSatisfyingVal()))
{
auto wrapper = emitWitnessTableWrapper(ordinaryFunc, interfaceRequirementVal);
entry->satisfyingVal.set(wrapper);
sharedContext->addToWorkList(wrapper);
}
}
}
void processInst(IRInst* inst)
{
if (auto witnessTable = as<IRWitnessTable>(inst))
{
lowerWitnessTable(witnessTable);
}
}
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 generateWitnessTableWrapperFunctions(SharedGenericsLoweringContext* sharedContext)
{
GenerateWitnessTableWrapperContext context;
context.sharedContext = sharedContext;
context.processModule();
}
} // namespace Slang
|