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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
|
#include "slang-ir-any-value-marshalling.h"
#include "slang-ir-generics-lowering-context.h"
#include "slang-ir.h"
#include "slang-ir-insts.h"
namespace Slang
{
// This is a subpass of generics lowering IR transformation.
// This pass generates packing/unpacking functions for `AnyValue`s,
// and replaces all `IRPackAnyValue` and `IRUnpackAnyValue` with calls to these
// functions.
struct AnyValueMarshallingContext
{
SharedGenericsLoweringContext* sharedContext;
// Stores information about generated `AnyValue` struct types.
struct AnyValueTypeInfo : RefObject
{
IRType* type; // The generated IR value for the `AnyValue<N>` struct type.
List<IRStructKey*> fieldKeys; // `IRStructKey`s for the fields of the generated type.
};
Dictionary<IRIntegerValue, RefPtr<AnyValueTypeInfo>> generatedAnyValueTypes;
struct MarshallingFunctionKey
{
IRType* originalType;
IRIntegerValue anyValueSize;
bool operator ==(MarshallingFunctionKey other)
{
return originalType == other.originalType && anyValueSize == other.anyValueSize;
}
HashCode getHashCode() const
{
return combineHash(Slang::getHashCode(originalType), Slang::getHashCode(anyValueSize));
}
};
struct MarshallingFunctionSet
{
IRFunc* packFunc;
IRFunc* unpackFunc;
};
// Stores the generated packing/unpacking functions for lookup.
Dictionary<MarshallingFunctionKey, MarshallingFunctionSet> mapTypeMarshalingFunctions;
AnyValueTypeInfo* ensureAnyValueType(IRAnyValueType* type)
{
auto size = getIntVal(type->getSize());
if (auto typeInfo = generatedAnyValueTypes.TryGetValue(size))
return typeInfo->Ptr();
RefPtr<AnyValueTypeInfo> info = new AnyValueTypeInfo();
IRBuilder builder;
builder.sharedBuilder = &sharedContext->sharedBuilderStorage;
builder.setInsertBefore(type);
auto structType = builder.createStructType();
info->type = structType;
StringBuilder nameSb;
nameSb << "AnyValue" << size;
builder.addExportDecoration(structType, nameSb.getUnownedSlice());
auto fieldCount = (size + sizeof(uint32_t) - 1) / sizeof(uint32_t);
for (decltype(fieldCount) i = 0; i < fieldCount; i++)
{
auto key = builder.createStructKey();
nameSb.Clear();
nameSb << "field" << i;
builder.addNameHintDecoration(key, nameSb.getUnownedSlice());
nameSb << "_anyVal" << size;
builder.addExportDecoration(key, nameSb.getUnownedSlice());
builder.createStructField(structType, key, builder.getUIntType());
info->fieldKeys.add(key);
}
generatedAnyValueTypes[size] = info;
return info.Ptr();
}
struct TypeMarshallingContext
{
AnyValueTypeInfo* anyValInfo;
uint32_t fieldOffset;
uint32_t intraFieldOffset;
IRType* uintPtrType;
IRInst* anyValueVar;
// Defines what to do with basic typed data elements.
virtual void marshalBasicType(IRBuilder* builder, IRType* dataType, IRInst* concreteTypedVar) = 0;
// Defines what to do with resource handle elements.
virtual void marshalResourceHandle(IRBuilder* builder, IRType* dataType, IRInst* concreteTypedVar) = 0;
// Validates that the type fits in the given AnyValueSize.
// After calling emitMarshallingCode, `fieldOffset` will be increased to the required `AnyValue` size.
// If this is larger than the provided AnyValue size, report a dianogstic. We might want to front load
// this in a separate IR validation pass in the future, but this is the easiest way to report the
// diagnostic now.
void validateAnyTypeSize(DiagnosticSink* sink, IRType* concreteType)
{
if (fieldOffset > static_cast<uint32_t>(anyValInfo->fieldKeys.getCount()))
{
sink->diagnose(concreteType->sourceLoc, Diagnostics::typeDoesNotFitAnyValueSize, concreteType);
}
}
void ensureOffsetAt4ByteBoundary()
{
if (intraFieldOffset)
{
fieldOffset++;
intraFieldOffset = 0;
}
}
void ensureOffsetAt2ByteBoundary()
{
if (intraFieldOffset == 0)
return;
if (intraFieldOffset <= 2)
{
intraFieldOffset = 2;
return;
}
fieldOffset++;
intraFieldOffset = 0;
return;
}
void advanceOffset(uint32_t bytes)
{
intraFieldOffset += bytes;
fieldOffset += intraFieldOffset / 4;
intraFieldOffset = intraFieldOffset % 4;
}
};
void emitMarshallingCode(
IRBuilder* builder,
TypeMarshallingContext* context,
IRInst* concreteTypedVar)
{
auto dataType = cast<IRPtrTypeBase>(concreteTypedVar->getDataType())->getValueType();
switch (dataType->getOp())
{
case kIROp_IntType:
case kIROp_FloatType:
case kIROp_UIntType:
case kIROp_UInt64Type:
case kIROp_Int64Type:
case kIROp_DoubleType:
case kIROp_Int8Type:
case kIROp_Int16Type:
case kIROp_UInt8Type:
case kIROp_UInt16Type:
case kIROp_HalfType:
case kIROp_BoolType:
context->marshalBasicType(builder, dataType, concreteTypedVar);
break;
case kIROp_VectorType:
{
auto vectorType = static_cast<IRVectorType*>(dataType);
auto elementType = vectorType->getElementType();
auto elementCount = getIntVal(vectorType->getElementCount());
auto elementPtrType = builder->getPtrType(elementType);
for (IRIntegerValue i = 0; i < elementCount; i++)
{
auto elementAddr = builder->emitElementAddress(
elementPtrType,
concreteTypedVar,
builder->getIntValue(builder->getIntType(), i));
emitMarshallingCode(builder, context, elementAddr);
}
break;
}
case kIROp_MatrixType:
{
auto matrixType = static_cast<IRMatrixType*>(dataType);
auto elementType = matrixType->getElementType();
auto colCount = getIntVal(matrixType->getColumnCount());
auto rowCount = getIntVal(matrixType->getRowCount());
for (IRIntegerValue i = 0; i < colCount; i++)
{
auto col = builder->emitElementAddress(
elementType,
concreteTypedVar,
builder->getIntValue(builder->getIntType(), i));
for (IRIntegerValue j = 0; j < rowCount; j++)
{
auto element = builder->emitElementExtract(
elementType,
col,
builder->getIntValue(builder->getIntType(), i));
emitMarshallingCode(builder, context, element);
}
}
break;
}
case kIROp_StructType:
{
auto structType = cast<IRStructType>(dataType);
for (auto field : structType->getFields())
{
auto fieldAddr = builder->emitFieldAddress(
builder->getPtrType(field->getFieldType()),
concreteTypedVar,
field->getKey());
emitMarshallingCode(builder, context, fieldAddr);
}
break;
}
case kIROp_ArrayType:
{
auto arrayType = cast<IRArrayType>(dataType);
auto elementPtrType = builder->getPtrType(arrayType->getElementType());
for (IRIntegerValue i = 0; i < getIntVal(arrayType->getElementCount()); i++)
{
auto fieldAddr = builder->emitElementAddress(
elementPtrType,
concreteTypedVar,
builder->getIntValue(builder->getIntType(), i));
emitMarshallingCode(builder, context, fieldAddr);
}
break;
}
default:
if (as<IRTextureTypeBase>(dataType) || as<IRSamplerStateTypeBase>(dataType))
{
context->marshalResourceHandle(builder, dataType, concreteTypedVar);
return;
}
SLANG_UNIMPLEMENTED_X("Unimplemented type packing");
break;
}
}
struct TypePackingContext : TypeMarshallingContext
{
virtual void marshalBasicType(IRBuilder* builder, IRType* dataType, IRInst* concreteVar) override
{
switch (dataType->getOp())
{
case kIROp_IntType:
case kIROp_FloatType:
case kIROp_BoolType:
{
ensureOffsetAt4ByteBoundary();
if (fieldOffset < static_cast<uint32_t>(anyValInfo->fieldKeys.getCount()))
{
auto srcVal = builder->emitLoad(concreteVar);
auto dstVal = builder->emitBitCast(builder->getUIntType(), srcVal);
auto dstAddr = builder->emitFieldAddress(
uintPtrType,
anyValueVar,
anyValInfo->fieldKeys[fieldOffset]);
builder->emitStore(dstAddr, dstVal);
}
advanceOffset(4);
break;
}
case kIROp_UIntType:
{
ensureOffsetAt4ByteBoundary();
if (fieldOffset < static_cast<uint32_t>(anyValInfo->fieldKeys.getCount()))
{
auto srcVal = builder->emitLoad(concreteVar);
auto dstAddr = builder->emitFieldAddress(
uintPtrType,
anyValueVar,
anyValInfo->fieldKeys[fieldOffset]);
builder->emitStore(dstAddr, srcVal);
}
advanceOffset(4);
break;
}
case kIROp_Int16Type:
case kIROp_UInt16Type:
case kIROp_HalfType:
{
ensureOffsetAt2ByteBoundary();
if (fieldOffset < static_cast<uint32_t>(anyValInfo->fieldKeys.getCount()))
{
auto srcVal = builder->emitLoad(concreteVar);
if (dataType->getOp() == kIROp_HalfType)
{
srcVal = builder->emitBitCast(builder->getType(kIROp_UInt16Type), srcVal);
}
srcVal = builder->emitConstructorInst(builder->getType(kIROp_UIntType), 1, &srcVal);
auto dstAddr = builder->emitFieldAddress(
uintPtrType,
anyValueVar,
anyValInfo->fieldKeys[fieldOffset]);
auto dstVal = builder->emitLoad(dstAddr);
if (intraFieldOffset == 0)
{
dstVal = builder->emitBitAnd(
dstVal->getFullType(), dstVal,
builder->getIntValue(builder->getUIntType(), 0xFFFF0000));
}
else
{
srcVal = builder->emitShl(
srcVal->getFullType(), srcVal,
builder->getIntValue(builder->getUIntType(), 16));
dstVal = builder->emitBitAnd(
dstVal->getFullType(), dstVal,
builder->getIntValue(builder->getUIntType(), 0xFFFF));
}
dstVal = builder->emitBitOr(dstVal->getFullType(), dstVal, srcVal);
builder->emitStore(dstAddr, dstVal);
}
advanceOffset(2);
break;
}
case kIROp_Int8Type:
case kIROp_UInt8Type:
case kIROp_UInt64Type:
case kIROp_Int64Type:
case kIROp_DoubleType:
SLANG_UNIMPLEMENTED_X("AnyValue type packing for non 32-bit elements");
break;
default:
SLANG_UNREACHABLE("unknown basic type");
}
}
virtual void marshalResourceHandle(IRBuilder* builder, IRType* dataType, IRInst* concreteVar) override
{
SLANG_UNUSED(dataType);
ensureOffsetAt4ByteBoundary();
if (fieldOffset + 1 < static_cast<uint32_t>(anyValInfo->fieldKeys.getCount()))
{
auto srcVal = builder->emitLoad(concreteVar);
auto uint64Val = builder->emitBitCast(builder->getUInt64Type(), srcVal);
auto lowBits = builder->emitConstructorInst(builder->getUIntType(), 1, &uint64Val);
auto shiftedBits = builder->emitShr(
builder->getUInt64Type(),
uint64Val,
builder->getIntValue(builder->getIntType(), 32));
auto highBits = builder->emitBitCast(builder->getUIntType(), shiftedBits);
auto dstAddr1 = builder->emitFieldAddress(
uintPtrType, anyValueVar, anyValInfo->fieldKeys[fieldOffset]);
builder->emitStore(dstAddr1, lowBits);
auto dstAddr2 = builder->emitFieldAddress(
uintPtrType, anyValueVar, anyValInfo->fieldKeys[fieldOffset + 1]);
builder->emitStore(dstAddr2, highBits);
advanceOffset(8);
}
}
};
IRFunc* generatePackingFunc(IRType* type, IRAnyValueType* anyValueType)
{
IRBuilder builder;
builder.sharedBuilder = &sharedContext->sharedBuilderStorage;
builder.setInsertBefore(type);
auto anyValInfo = ensureAnyValueType(anyValueType);
auto func = builder.createFunc();
StringBuilder nameSb;
nameSb << "packAnyValue" << getIntVal(anyValueType->getSize());
builder.addNameHintDecoration(func, nameSb.getUnownedSlice());
// Currently we don't add linkage to the generated func, since we
// do not have a way to compute mangled names from an IR entity.
// This will leads to duplicate packing functions in linked code
// but there won't be correctness issues.
auto funcType = builder.getFuncType(1, &type, anyValInfo->type);
func->setFullType(funcType);
builder.setInsertInto(func);
builder.emitBlock();
auto param = builder.emitParam(type);
auto concreteTypedVar = builder.emitVar(type);
builder.emitStore(concreteTypedVar, param);
auto resultVar = builder.emitVar(anyValInfo->type);
// Initialize fields to 0 to prevent downstream compiler error.
for (uint32_t offset = 0; offset < (uint32_t)anyValInfo->fieldKeys.getCount(); offset++)
{
auto fieldAddr = builder.emitFieldAddress(builder.getUIntType(), resultVar, anyValInfo->fieldKeys[offset]);
builder.emitStore(fieldAddr, builder.getIntValue(builder.getUIntType(), 0));
}
TypePackingContext context;
context.anyValInfo = anyValInfo;
context.fieldOffset = context.intraFieldOffset = 0;
context.uintPtrType = builder.getPtrType(builder.getUIntType());
context.anyValueVar = resultVar;
emitMarshallingCode(&builder, &context, concreteTypedVar);
context.validateAnyTypeSize(sharedContext->sink, type);
auto load = builder.emitLoad(resultVar);
builder.emitReturn(load);
return func;
}
struct TypeUnpackingContext : TypeMarshallingContext
{
virtual void marshalBasicType(IRBuilder* builder, IRType* dataType, IRInst* concreteVar) override
{
switch (dataType->getOp())
{
case kIROp_IntType:
case kIROp_FloatType:
case kIROp_BoolType:
{
ensureOffsetAt4ByteBoundary();
if (fieldOffset < static_cast<uint32_t>(anyValInfo->fieldKeys.getCount()))
{
auto srcAddr = builder->emitFieldAddress(
uintPtrType,
anyValueVar,
anyValInfo->fieldKeys[fieldOffset]);
auto srcVal = builder->emitLoad(srcAddr);
srcVal = builder->emitBitCast(dataType, srcVal);
builder->emitStore(concreteVar, srcVal);
}
advanceOffset(4);
break;
}
case kIROp_UIntType:
{
ensureOffsetAt4ByteBoundary();
if (fieldOffset < static_cast<uint32_t>(anyValInfo->fieldKeys.getCount()))
{
auto srcAddr = builder->emitFieldAddress(
uintPtrType,
anyValueVar,
anyValInfo->fieldKeys[fieldOffset]);
auto srcVal = builder->emitLoad(srcAddr);
builder->emitStore(concreteVar, srcVal);
}
advanceOffset(4);
break;
}
case kIROp_Int16Type:
case kIROp_UInt16Type:
case kIROp_HalfType:
{
ensureOffsetAt2ByteBoundary();
if (fieldOffset < static_cast<uint32_t>(anyValInfo->fieldKeys.getCount()))
{
auto srcAddr = builder->emitFieldAddress(
uintPtrType,
anyValueVar,
anyValInfo->fieldKeys[fieldOffset]);
auto srcVal = builder->emitLoad(srcAddr);
if (intraFieldOffset == 0)
{
srcVal = builder->emitBitAnd(
srcVal->getFullType(), srcVal,
builder->getIntValue(builder->getUIntType(), 0xFFFF));
}
else
{
srcVal = builder->emitShr(
srcVal->getFullType(), srcVal,
builder->getIntValue(builder->getUIntType(), 16));
}
if (dataType->getOp() == kIROp_Int16Type)
{
srcVal = builder->emitConstructorInst(builder->getType(kIROp_Int16Type), 1, &srcVal);
}
else
{
srcVal = builder->emitConstructorInst(builder->getType(kIROp_UInt16Type), 1, &srcVal);
}
if (dataType->getOp() == kIROp_HalfType)
{
srcVal = builder->emitBitCast(dataType, srcVal);
}
builder->emitStore(concreteVar, srcVal);
}
advanceOffset(2);
break;
}
case kIROp_UInt64Type:
case kIROp_Int64Type:
case kIROp_DoubleType:
case kIROp_Int8Type:
case kIROp_UInt8Type:
SLANG_UNIMPLEMENTED_X("AnyValue type packing for non 32-bit elements");
break;
default:
SLANG_UNREACHABLE("unknown basic type");
}
}
virtual void marshalResourceHandle(
IRBuilder* builder, IRType* dataType, IRInst* concreteVar) override
{
ensureOffsetAt4ByteBoundary();
if (fieldOffset + 1 < static_cast<uint32_t>(anyValInfo->fieldKeys.getCount()))
{
auto srcAddr = builder->emitFieldAddress(
uintPtrType, anyValueVar, anyValInfo->fieldKeys[fieldOffset]);
auto lowBits = builder->emitLoad(srcAddr);
auto srcAddr1 = builder->emitFieldAddress(
uintPtrType, anyValueVar, anyValInfo->fieldKeys[fieldOffset + 1]);
auto highBits = builder->emitLoad(srcAddr1);
auto combinedBits = builder->emitMakeUInt64(lowBits, highBits);
combinedBits = builder->emitBitCast(dataType, combinedBits);
builder->emitStore(concreteVar, combinedBits);
advanceOffset(8);
}
}
};
IRFunc* generateUnpackingFunc(IRType* type, IRAnyValueType* anyValueType)
{
IRBuilder builder;
builder.sharedBuilder = &sharedContext->sharedBuilderStorage;
builder.setInsertBefore(type);
auto anyValInfo = ensureAnyValueType(anyValueType);
auto func = builder.createFunc();
StringBuilder nameSb;
nameSb << "unpackAnyValue" << getIntVal(anyValueType->getSize());
builder.addNameHintDecoration(func, nameSb.getUnownedSlice());
auto funcType = builder.getFuncType(1, &anyValInfo->type, type);
func->setFullType(funcType);
builder.setInsertInto(func);
builder.emitBlock();
auto param = builder.emitParam(anyValInfo->type);
auto anyValueVar = builder.emitVar(anyValInfo->type);
builder.emitStore(anyValueVar, param);
auto resultVar = builder.emitVar(type);
TypeUnpackingContext context;
context.anyValInfo = anyValInfo;
context.fieldOffset = context.intraFieldOffset = 0;
context.uintPtrType = builder.getPtrType(builder.getUIntType());
context.anyValueVar = anyValueVar;
emitMarshallingCode(&builder, &context, resultVar);
auto load = builder.emitLoad(resultVar);
builder.emitReturn(load);
return func;
}
// Ensures the marshalling functions between `type` and `anyValueType` are already generated.
// Returns the generated marshalling functions.
MarshallingFunctionSet ensureMarshallingFunc(IRType* type, IRAnyValueType* anyValueType)
{
auto size = getIntVal(anyValueType->getSize());
MarshallingFunctionKey key;
key.originalType = type;
key.anyValueSize = size;
MarshallingFunctionSet funcSet;
if (mapTypeMarshalingFunctions.TryGetValue(key, funcSet))
return funcSet;
funcSet.packFunc = generatePackingFunc(type, anyValueType);
funcSet.unpackFunc = generateUnpackingFunc(type, anyValueType);
mapTypeMarshalingFunctions[key] = funcSet;
return funcSet;
}
void processPackInst(IRPackAnyValue* packInst)
{
auto operand = packInst->getValue();
auto func = ensureMarshallingFunc(
operand->getDataType(),
cast<IRAnyValueType>(packInst->getDataType()));
IRBuilder builderStorage;
auto builder = &builderStorage;
builder->sharedBuilder = &sharedContext->sharedBuilderStorage;
builder->setInsertBefore(packInst);
auto callInst = builder->emitCallInst(packInst->getDataType(), func.packFunc, 1, &operand);
packInst->replaceUsesWith(callInst);
packInst->removeAndDeallocate();
}
void processUnpackInst(IRUnpackAnyValue* unpackInst)
{
auto operand = unpackInst->getValue();
auto func = ensureMarshallingFunc(
unpackInst->getDataType(),
cast<IRAnyValueType>(operand->getDataType()));
IRBuilder builderStorage;
auto builder = &builderStorage;
builder->sharedBuilder = &sharedContext->sharedBuilderStorage;
builder->setInsertBefore(unpackInst);
auto callInst = builder->emitCallInst(unpackInst->getDataType(), func.unpackFunc, 1, &operand);
unpackInst->replaceUsesWith(callInst);
unpackInst->removeAndDeallocate();
}
void processAnyValueType(IRAnyValueType* type)
{
auto info = ensureAnyValueType(type);
type->replaceUsesWith(info->type);
}
void processInst(IRInst* inst)
{
if (auto packInst = as<IRPackAnyValue>(inst))
{
processPackInst(packInst);
}
else if (auto unpackInst = as<IRUnpackAnyValue>(inst))
{
processUnpackInst(unpackInst);
}
}
void processModule()
{
// We start by initializing our shared IR building state,
// since we will re-use that state for any code we
// generate along the way.
//
SharedIRBuilder* sharedBuilder = &sharedContext->sharedBuilderStorage;
sharedBuilder->module = sharedContext->module;
sharedBuilder->session = sharedContext->module->session;
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);
}
}
// Finally, replace all `AnyValueType` with the actual struct type that implements it.
for (auto inst : sharedContext->module->getModuleInst()->getChildren())
{
if (auto anyValueType = as<IRAnyValueType>(inst))
processAnyValueType(anyValueType);
}
// Because we replaced all `AnyValueType` uses, some old type definitions (e.g. PtrType(AnyValueType))
// will become duplicates with new types we introduced (e.g. PtrType(AnyValueStruct)), and therefore
// invalidates our `globalValueNumberingMap` hash map. We need to rebuild it.
sharedContext->sharedBuilderStorage.deduplicateAndRebuildGlobalNumberingMap();
}
};
void generateAnyValueMarshallingFunctions(SharedGenericsLoweringContext* sharedContext)
{
AnyValueMarshallingContext context;
context.sharedContext = sharedContext;
context.processModule();
}
SlangInt alignUp(SlangInt x, SlangInt alignment)
{
return (x + alignment - 1) / alignment * alignment;
}
SlangInt _getAnyValueSizeRaw(IRType* type, SlangInt offset)
{
switch (type->getOp())
{
case kIROp_IntType:
case kIROp_FloatType:
case kIROp_UIntType:
return alignUp(offset, 4) + 4;
case kIROp_UInt64Type:
case kIROp_Int64Type:
case kIROp_DoubleType:
return -1;
case kIROp_Int16Type:
case kIROp_UInt16Type:
case kIROp_HalfType:
return alignUp(offset, 2) + 2;
case kIROp_UInt8Type:
case kIROp_Int8Type:
return -1;
case kIROp_VectorType:
{
auto vectorType = static_cast<IRVectorType*>(type);
auto elementType = vectorType->getElementType();
auto elementCount = getIntVal(vectorType->getElementCount());
for (IRIntegerValue i = 0; i < elementCount; i++)
{
offset = _getAnyValueSizeRaw(elementType, offset);
if (offset < 0) return offset;
}
return offset;
}
case kIROp_MatrixType:
{
auto matrixType = static_cast<IRMatrixType*>(type);
auto elementType = matrixType->getElementType();
auto colCount = getIntVal(matrixType->getColumnCount());
auto rowCount = getIntVal(matrixType->getRowCount());
for (IRIntegerValue i = 0; i < colCount; i++)
{
for (IRIntegerValue j = 0; j < rowCount; j++)
{
offset = _getAnyValueSizeRaw(elementType, offset);
if (offset < 0) return offset;
}
}
return offset;
}
case kIROp_StructType:
{
auto structType = cast<IRStructType>(type);
for (auto field : structType->getFields())
{
offset = _getAnyValueSizeRaw(field->getFieldType(), offset);
if (offset < 0) return offset;
}
return offset;
}
case kIROp_ArrayType:
{
auto arrayType = cast<IRArrayType>(type);
for (IRIntegerValue i = 0; i < getIntVal(arrayType->getElementCount()); i++)
{
offset = _getAnyValueSizeRaw(arrayType->getElementType(), offset);
if (offset < 0) return offset;
}
return offset;
}
case kIROp_InterfaceType:
{
// TODO: implement anyValue packing for interface types.
return -1;
}
default:
if (as<IRTextureTypeBase>(type) || as<IRSamplerStateTypeBase>(type))
{
return alignUp(offset, 4) + 8;
}
return -1;
}
}
SlangInt getAnyValueSize(IRType* type)
{
auto rawSize = _getAnyValueSizeRaw(type, 0);
if (rawSize < 0) return rawSize;
return alignUp(rawSize, 4);
}
}
|