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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
|
// slang-serialize-ir.cpp
#include "slang-serialize-ir.h"
#include "core/slang-blob-builder.h"
#include "core/slang-common.h"
#include "core/slang-dictionary.h"
#include "slang-ir-insts-stable-names.h"
#include "slang-ir-insts.h"
#include "slang-ir-validate.h"
#include "slang-serialize-fossil.h"
#include "slang-serialize-riff.h"
#include "slang-serialize-source-loc.h"
#include "slang-serialize.h"
#include "slang-tag-version.h"
#include "slang.h"
//
#include "slang-serialize-ir.cpp.fiddle"
// If USE_RIFF is set, then we serialize using the RIFF backend, it's the
// slowest option
#define USE_RIFF 0
// If we are serializing using Fossil, DIRECT_FROM_FOSSIL will make it so that
// we unflatten directly from the fossilized representation rather than
// deserializing everything first. It is the fastest option
#define DIRECT_FROM_FOSSIL 0
FIDDLE()
namespace Slang
{
//
// We wrap everything up in an IRModuleInfo, to prepare for the case in which
// we want to serialize some sidecar information to help with on-demand loading
// or backwards compat
//
// For all the aggregate structs here we'll use Fiddle to generate the
// deserialization code.
//
FIDDLE()
struct IRModuleInfo
{
FIDDLE(...)
// Include this here so that if we need to change the way we serialize
// things and maintain backwards compat we can increment this value, for
// example if we introduce more instructions with weird payloads like
// IRModuleInst or IRConstants.
// If we want to support back compat we'll need to change this to a list of
// accepted values, and branch on that later down.
const static UInt64 kSupportedSerializationVersion = 1;
FIDDLE() UInt64 serializationVersion = kSupportedSerializationVersion;
// Include the specific compiler version in serialized output, in case we
// ever need to do any version specific workarounds.
FIDDLE() String fullVersion = SLANG_TAG_VERSION;
FIDDLE() RefPtr<IRModule> module;
};
//
// All the information necessary to allocate an ordinary instruction, if it's a
// string constant we need to get the length of the string from another list
// later on.
//
FIDDLE()
struct InstAllocInfo
{
FIDDLE(...)
FIDDLE() IROp op;
FIDDLE() uint32_t operandCount;
};
FIDDLE()
struct FlatInstTable
{
FIDDLE(...)
// Each IR instruction has:
//
// * An opcode
// * A result type
// * Zero or more operands
// * Zero or more children
//
// Most instructions are entirely defined by those properties.
//
// The instructions that represent simple constants (integers, strings, etc.) are
// unique in that they have "payload" data that holds their value, instead of having
// any operands.
//
// The deserialization logic doesn't interact with any
// systems for deduplication or simplification of instructions.
// All these lists are a flattened representation of these properties of
// instructions as traversed in preorder.
// These are the same length, the number of instructions in the module
// The instAllocInfo list is all that's necessary to allocate an instruction
FIDDLE() List<InstAllocInfo> instAllocInfo;
FIDDLE() List<Int64> childCounts;
FIDDLE() List<SourceLoc> sourceLocs;
// The length of operandIndices is the number of instructions in the module
// (for typeUse) + the number of operands in the module
//
// a nullptr operand is encoded as -1
FIDDLE() List<Int64> operandIndices;
// The length is equal to the number of strings and blobs in the module
FIDDLE() List<Int64> stringLengths;
// The length is the sum of all stringLengths, the contents is the
// concatenation of all their data
FIDDLE() List<uint8_t> stringChars;
// The length is number of integer/floating constants in the module, and
// the contents are the bits of those constants
FIDDLE() List<UInt64> literals;
};
// For debugging
[[maybe_unused]] static void dumpFlatInstTableStats(
const FlatInstTable& table,
const char* label = nullptr)
{
if (label)
{
fprintf(stderr, "=== FlatInstTable Stats: %s ===\n", label);
}
else
{
fprintf(stderr, "=== FlatInstTable Stats ===\n");
}
// Basic instruction counts
auto instCount = table.instAllocInfo.getCount();
fprintf(stderr, "Instruction count: %zu\n", (size_t)instCount);
// Verify consistency
if (table.childCounts.getCount() != instCount)
{
fprintf(
stderr,
"WARNING: childCounts size (%zu) != instruction count (%zu)\n",
(size_t)table.childCounts.getCount(),
(size_t)instCount);
}
if (table.sourceLocs.getCount() != instCount)
{
fprintf(
stderr,
"WARNING: sourceLocs size (%zu) != instruction count (%zu)\n",
(size_t)table.sourceLocs.getCount(),
(size_t)instCount);
}
// Count string/blob instructions
Int64 stringBlobInstCount = 0;
for (const auto& allocInfo : table.instAllocInfo)
{
const IROp op = allocInfo.op;
if (op == kIROp_StringLit || op == kIROp_BlobLit)
{
stringBlobInstCount++;
}
}
fprintf(stderr, "String/blob instruction count: %zu\n", (size_t)stringBlobInstCount);
fprintf(stderr, "stringLengths array size: %zu\n", (size_t)table.stringLengths.getCount());
// Verify string/blob consistency
if (stringBlobInstCount != table.stringLengths.getCount())
{
fprintf(
stderr,
"ERROR: String/blob instruction count (%zu) != stringLengths size (%zu)\n",
(size_t)stringBlobInstCount,
(size_t)table.stringLengths.getCount());
}
// Verify string data consistency
Int64 expectedStringDataSize = 0;
for (auto len : table.stringLengths)
{
expectedStringDataSize += len;
}
fprintf(stderr, "Expected string data size: %zu bytes\n", (size_t)expectedStringDataSize);
fprintf(stderr, "Actual stringChars size: %zu bytes\n", (size_t)table.stringChars.getCount());
if (expectedStringDataSize != table.stringChars.getCount())
{
fprintf(
stderr,
"ERROR: Expected string data size (%zu) != actual stringChars size (%zu)\n",
(size_t)expectedStringDataSize,
(size_t)table.stringChars.getCount());
}
// Operand statistics
auto operandCount = table.operandIndices.getCount() - instCount;
fprintf(stderr, "Total operands: %zu\n", (size_t)operandCount);
if (instCount > 0)
{
fprintf(
stderr,
"Average operands per instruction: %.2f\n",
(double)operandCount / instCount);
}
// Count null operands
Int64 nullOperandCount = 0;
for (auto idx : table.operandIndices)
{
if (idx == -1)
nullOperandCount++;
}
fprintf(
stderr,
"Null operands: %zu (%.1f%%)\n",
(size_t)nullOperandCount,
table.operandIndices.getCount() > 0
? 100.0 * nullOperandCount / table.operandIndices.getCount()
: 0.0);
// String/blob statistics
if (table.stringLengths.getCount() > 0)
{
Int64 maxLength = 0;
for (auto len : table.stringLengths)
{
if (len > maxLength)
maxLength = len;
}
fprintf(
stderr,
"Average string length: %.1f bytes\n",
(double)expectedStringDataSize / table.stringLengths.getCount());
fprintf(stderr, "Max string length: %zu bytes\n", (size_t)maxLength);
}
// Literal constants
fprintf(stderr, "Literal constants: %zu\n", (size_t)table.literals.getCount());
// Memory usage estimation
size_t totalMemory = 0;
totalMemory += table.instAllocInfo.getCount() * sizeof(InstAllocInfo);
totalMemory += table.childCounts.getCount() * sizeof(Int64);
totalMemory += table.sourceLocs.getCount() * sizeof(SourceLoc);
totalMemory += table.operandIndices.getCount() * sizeof(Int64);
totalMemory += table.stringLengths.getCount() * sizeof(Int64);
totalMemory += table.stringChars.getCount() * sizeof(uint8_t);
totalMemory += table.literals.getCount() * sizeof(UInt64);
fprintf(
stderr,
"Estimated memory usage: %zu bytes (%.2f MB)\n",
totalMemory,
totalMemory / (1024.0 * 1024.0));
fprintf(stderr, "===========================\n");
}
//
// We need some small amount of additional context to serialize IR Modules, keep track of that here
//
struct IRSerialReadContext;
struct IRSerialWriteContext;
// Specialize to the reader/writer for the specific backend we're targeting
// instead of ISerializerImpl to avoid some virtual function calls
#if USE_RIFF
using IRWriteSerializer = Serializer_<RIFFSerialWriter, IRSerialWriteContext>;
using IRReadSerializer = Serializer_<RIFFSerialReader, IRSerialReadContext>;
#else
using IRWriteSerializer = Serializer_<Fossil::SerialWriter, IRSerialWriteContext>;
using IRReadSerializer = Serializer_<Fossil::SerialReader, IRSerialReadContext>;
#endif
struct IRSerialWriteContext : SourceLocSerialContext
{
IRSerialWriteContext(SerialSourceLocWriter* sourceLocWriter)
: _sourceLocWriter(sourceLocWriter)
{
}
virtual void handleIRModule(IRWriteSerializer const& serializer, IRModule*& value);
virtual void handleName(IRWriteSerializer const& serializer, Name*& value);
virtual SerialSourceLocWriter* getSourceLocWriter() override { return _sourceLocWriter; }
SerialSourceLocWriter* _sourceLocWriter;
};
struct IRSerialReadContext : SourceLocSerialContext, RefObject
{
IRSerialReadContext(Session* session, SerialSourceLocReader* sourceLocReader)
: _session(session), _sourceLocReader(sourceLocReader)
{
}
virtual void handleIRModule(IRReadSerializer const& serializer, IRModule*& value);
virtual void handleName(IRReadSerializer const& serializer, Name*& value);
virtual SerialSourceLocReader* getSourceLocReader() override { return _sourceLocReader; }
// Used to allocate an IRModule
Session* _session;
//
SerialSourceLocReader* _sourceLocReader;
// The module in which we will allocate our instructions
RefPtr<IRModule> _module;
//
bool _foundUnrecognizedInstructions = false;
};
SLANG_DECLARE_FOSSILIZED_AS(Name, String);
/// Fossilized representation of a `IRModule`
struct Fossilized_IRModule;
SLANG_DECLARE_FOSSILIZED_TYPE(IRModule, Fossilized_IRModule);
// IROps are serialized as integers, and given a stable name
SLANG_DECLARE_FOSSILIZED_AS(IROp, FossilUInt);
template<typename S>
void serialize(S const& serializer, IROp& value)
{
auto stableName = isWriting(serializer) ? getOpcodeStableName(value) : kInvalidStableName;
serializeEnum(serializer, stableName);
// if we're reading
if constexpr (std::is_same_v<S, IRReadSerializer>)
{
value = getStableNameOpcode(stableName);
// It's possible we're reading a module serialized by a future version of
// Slang with as-yet unknown instructions.
// if this is the case, return IRUnrecognized and we can handle it later
if (value == kIROp_Invalid)
{
value = kIROp_Unrecognized;
serializer.getContext()->_foundUnrecognizedInstructions = true;
}
}
}
//
// Serialize Names via the name pool on the session, this is used just for the
// IRModule name member.
//
template<typename S>
void serializeObject(S const& serializer, Name*& value, Name*)
{
serializer.getContext()->handleName(serializer, value);
}
void IRSerialWriteContext::handleName(IRWriteSerializer const& serializer, Name*& value)
{
serialize(serializer, value->text);
}
void IRSerialReadContext::handleName(IRReadSerializer const& serializer, Name*& value)
{
String text;
serialize(serializer, text);
value = _session->getNamePool()->getName(text);
}
//
// This splice handles any aggregate types, a similar splice is well documented
// in slang-serialize-ast.cpp
//
#if 0 // FIDDLE TEMPLATE:
% irStructTypes = {
% Slang.IRModuleInfo,
% Slang.FlatInstTable,
% Slang.InstAllocInfo,
% }
%
% for _,T in ipairs(irStructTypes) do
/// Fossilized representation of a `$T`
struct Fossilized_$T;
SLANG_DECLARE_FOSSILIZED_TYPE($T, Fossilized_$T);
/// Serialize a `$T`
template<typename S>
void serialize(S const& serializer, $T& value);
%end
%for _,T in ipairs(irStructTypes) do
/// Fossilized representation of a value of type `$T`
struct Fossilized_$T
% if T.directSuperClass then
: public Fossilized<$(T.directSuperClass)>
% else
: public FossilizedRecordVal
% end
{
% for i,f in ipairs(T.directFields) do
Fossilized<decltype($T::$f)> $f;
const static Index $(f)_fieldIndex = $(i-1);
% end
};
namespace Fossil{
template<>
struct ValRef<Fossilized_$T> : ValRefBase<Fossilized_$T>
{
public:
using ValRefBase<Fossilized_$T>::ValRefBase;
% for i,f in ipairs(T.directFields) do
AnyValRef get$(tostring(f):gsub("^%l", string.upper))() const
{
return as<FossilizedRecordVal>(getAddress(*this))->getField($(i-1));
}
% end
};
}
%end
% for _,T in ipairs(irStructTypes) do
/// Serialize a `value` of type `$T`
template<typename S>
void serialize(S const& serializer, $T& value)
{
SLANG_UNUSED(value);
SLANG_SCOPED_SERIALIZER_STRUCT(serializer);
% if T.directSuperClass then
serialize(serializer, static_cast<$(T.directSuperClass)&>(value));
% end
% for _,f in ipairs(T.directFields) do
serialize(serializer, value.$f);
% end
}
% end
#else // FIDDLE OUTPUT:
#define FIDDLE_GENERATED_OUTPUT_ID 0
#include "slang-serialize-ir.cpp.fiddle"
#endif // FIDDLE END
struct Fossilized_IRModule : public FossilizedRecordVal
{
Fossilized<String> m_name;
Fossilized<decltype(IRModule::m_version)> m_version;
Fossilized<FlatInstTable> m_moduleInst;
};
////
//
// After that preamble, this is the interesting stuff now
//
////
//
// Handlers for IRModule, there is a little extra setup to do once top level
// entries are deserialized to set up m_mapMangledNameToGlobalInst, this is
// done at the end of readSerializedModuleIR
//
template<typename S>
void serializeObject(S const& serializer, IRModule*& value, IRModule*)
{
serializer.getContext()->handleIRModule(serializer, value);
}
static void serializeAsFlatModule(const IRWriteSerializer& serializer, IRModuleInst* moduleInst)
{
FlatInstTable flat;
Dictionary<IRInst*, Int64> instMap;
instMap.add(nullptr, -1);
List<IRInst*> insts;
traverseInstsInSerializationOrder(
moduleInst,
[&](IRInst* inst)
{
const auto thisInstIndex = flat.instAllocInfo.getCount();
instMap.add(inst, thisInstIndex);
insts.add(inst);
flat.instAllocInfo.add(InstAllocInfo{
.op = inst->m_op,
.operandCount = inst->operandCount,
});
flat.childCounts.add(0);
flat.sourceLocs.add(inst->sourceLoc);
inst->scratchData = thisInstIndex; // Store index for child counting
// Update parent's child count
if (inst->parent)
{
flat.childCounts[inst->parent->scratchData]++;
}
});
for (const auto inst : insts)
{
flat.operandIndices.add(instMap.getValue(inst->typeUse.get()));
for (UInt i = 0; i < inst->getOperandCount(); ++i)
{
const auto& operand = inst->getOperand(i);
flat.operandIndices.add(instMap.getValue(operand));
}
if (const auto& c = as<IRConstant>(inst))
{
switch (inst->m_op)
{
case kIROp_BoolLit:
case kIROp_IntLit:
flat.literals.add(bitCast<UInt64>(c->value.intVal));
break;
case kIROp_FloatLit:
flat.literals.add(bitCast<UInt64>(c->value.floatVal));
break;
case kIROp_PtrLit:
// to avoid complaints on 32 bit wasm
flat.literals.add(UInt64(bitCast<uintptr_t>(c->value.ptrVal)));
break;
case kIROp_StringLit:
case kIROp_BlobLit:
const auto slice = c->getStringSlice();
const auto len = slice.getLength();
flat.stringLengths.add(len);
flat.stringChars.addRange(reinterpret_cast<const uint8_t*>(slice.begin()), len);
break;
}
}
}
// dumpFlatInstTableStats(flat, "serializing");
serialize(serializer, flat);
}
// A helper function to read one thing from a Fossil ref
template<typename T>
static T deserialize1(const IRReadSerializer& serializer, const Fossil::AnyValRef r)
{
T t;
Fossil::ReadContext context;
Fossil::SerialReader reader(
context,
Fossil::getAddress(r),
Fossil::SerialReader::InitialStateType::PseudoPtr);
IRReadSerializer serializer_(&reader, serializer.getContext());
serialize(serializer_, t);
return t;
}
static IRModuleInst* deserializeFromFlatModule(const IRReadSerializer& serializer, IRModule* module)
{
IRSerialReadContext& readContext = *serializer.getContext();
#if DIRECT_FROM_FOSSIL
const auto flatPtr = as<Fossilized<FlatInstTable>>(serializer.getImpl()->readValPtr());
Fossilized<FlatInstTable>& flat = flatPtr->getDataRef();
// Read just the sourceLocs using normal deserialization
const auto sourceLocs = deserialize1<List<SourceLoc>>(serializer, flatPtr->getSourceLocs());
#else
FlatInstTable flat;
serialize(serializer, flat);
const List<SourceLoc>& sourceLocs = flat.sourceLocs;
// dumpFlatInstTableStats(flat, "deserializing");
#endif
Int64 stringLengthIndex = 0;
List<IRInst*> instsList;
#if DIRECT_FROM_FOSSIL
const auto numInsts = flat.instAllocInfo.getElementCount();
#else
const auto numInsts = flat.instAllocInfo.getCount();
#endif
instsList.setCount(numInsts + 1);
// nullptr instructions are represented as `-1`. We can save ourselves a
// branch by just making that index valid.
IRInst** const insts = &instsList[1];
insts[-1] = nullptr;
for (Int64 instIndex = 0; instIndex < numInsts; ++instIndex)
{
const auto& a = flat.instAllocInfo[instIndex];
// The opcode is serialized as the stable name, so if we're reading
// directly we need to destabilize that
#if DIRECT_FROM_FOSSIL
IROp op = getStableNameOpcode(a.op);
#else
IROp op = a.op;
#endif
if (op == kIROp_Invalid) [[unlikely]]
{
readContext._foundUnrecognizedInstructions = true;
op = kIROp_Unrecognized;
}
size_t minSizeInBytes = 0;
switch (op)
{
[[unlikely]] case kIROp_ModuleInst:
minSizeInBytes = offsetof(IRModuleInst, module) +
sizeof(IRModuleInst::module); // NOLINT(bugprone-sizeof-expression)
break;
case kIROp_BoolLit:
case kIROp_IntLit:
case kIROp_FloatLit:
case kIROp_PtrLit:
case kIROp_VoidLit:
minSizeInBytes = offsetof(IRConstant, value) + sizeof(IRConstant::value);
break;
// About 5% of instructions in the core module are strings!
case kIROp_StringLit:
case kIROp_BlobLit:
minSizeInBytes = offsetof(IRConstant, value) +
offsetof(IRConstant::StringValue, chars) +
flat.stringLengths[stringLengthIndex++];
break;
}
insts[instIndex] = module->_allocateInst(op, a.operandCount, minSizeInBytes);
}
Int64 litIndex = 0;
Int64 operandIndex = 0;
Int64 instIndex = 0;
stringLengthIndex = 0;
Int64 stringDataIndex = 0;
const auto go = [&](auto& go, IRInst* parent) -> IRInst*
{
const auto thisInstIndex = instIndex++;
IRInst* inst = insts[thisInstIndex];
// operands and sourcelocs
inst->sourceLoc = sourceLocs[thisInstIndex];
inst->typeUse.init(inst, insts[flat.operandIndices[operandIndex++]]);
for (Int64 o = 0; o < inst->operandCount; ++o)
inst->getOperands()[o].init(inst, insts[flat.operandIndices[operandIndex++]]);
// Handle special instructions
switch (inst->m_op)
{
[[unlikely]] case kIROp_ModuleInst:
cast<IRModuleInst>(inst)->module = module;
break;
case kIROp_BoolLit:
case kIROp_IntLit:
cast<IRConstant>(inst)->value.intVal =
bitCast<IRIntegerValue>(flat.literals[litIndex++]);
break;
case kIROp_FloatLit:
cast<IRConstant>(inst)->value.floatVal = bitCast<double>(flat.literals[litIndex++]);
break;
case kIROp_PtrLit:
// Keep the compiler happy on 32 bit builds
cast<IRConstant>(inst)->value.ptrVal = (void*)(uintptr_t(flat.literals[litIndex++]));
break;
case kIROp_StringLit:
case kIROp_BlobLit:
const auto c = cast<IRConstant>(inst);
const auto len = flat.stringLengths[stringLengthIndex++];
char* const dstChars = c->value.stringVal.chars;
c->value.stringVal.numChars = uint32_t(len);
memcpy(dstChars, flat.stringChars.begin() + stringDataIndex, len);
stringDataIndex += len;
break;
}
// Read in children, and fix up pointers
inst->parent = parent;
IRInst* prev = nullptr;
IRInst* first = nullptr;
IRInst* last = nullptr;
for (Int64 i = 0; i < flat.childCounts[thisInstIndex]; ++i)
{
auto c = go(go, inst);
if (i == 0)
first = c;
last = c;
c->prev = prev;
if (prev)
prev->next = c;
prev = c;
}
if (last)
last->next = nullptr;
inst->m_decorationsAndChildren.first = first;
inst->m_decorationsAndChildren.last = last;
return inst;
};
const auto moduleInst = go(go, nullptr);
return cast<IRModuleInst>(moduleInst);
}
void IRSerialWriteContext::handleIRModule(IRWriteSerializer const& serializer, IRModule*& value)
{
SLANG_SCOPED_SERIALIZER_STRUCT(serializer);
serialize(serializer, value->m_name);
serialize(serializer, value->m_version);
serializeAsFlatModule(serializer, value->m_moduleInst);
}
void IRSerialReadContext::handleIRModule(IRReadSerializer const& serializer, IRModule*& value)
{
SLANG_SCOPED_SERIALIZER_STRUCT(serializer);
value = new IRModule{_session};
SLANG_ASSERT(!_module);
_module = value;
serialize(serializer, value->m_name);
serialize(serializer, value->m_version);
value->m_moduleInst = deserializeFromFlatModule(serializer, value);
}
//
// {write,read}SerializedModuleIR()
//
void writeSerializedModuleIR(
RIFF::BuildCursor& cursor,
IRModule* irModule,
SerialSourceLocWriter* sourceLocWriter)
{
// The flow here is very similar to writeSerializedModuleAST which is very
// well documented.
IRModuleInfo moduleInfo;
moduleInfo.fullVersion = SLANG_TAG_VERSION;
moduleInfo.module = irModule;
#if USE_RIFF
{
RIFFSerialWriter writer(cursor.getCurrentChunk());
IRSerialWriteContext context{sourceLocWriter};
IRWriteSerializer serializer(&writer, &context);
serialize(serializer, moduleInfo);
}
ComPtr<ISlangBlob> blob;
#else
BlobBuilder blobBuilder;
{
Fossil::SerialWriter writer(blobBuilder);
IRSerialWriteContext context{sourceLocWriter};
IRWriteSerializer serializer(&writer, &context);
serialize(serializer, moduleInfo);
}
ComPtr<ISlangBlob> blob;
blobBuilder.writeToBlob(blob.writeRef());
void const* data = blob->getBufferPointer();
size_t size = blob->getBufferSize();
cursor.addDataChunk(PropertyKeys<IRModule>::IRModule, data, size);
#endif
}
Result readSerializedModuleInfo(
RIFF::Chunk const* chunk,
String& compilerVersion,
UInt& version,
String& name)
{
static_assert(!USE_RIFF); // unimplemented
auto dataChunk = as<RIFF::DataChunk>(chunk);
if (!dataChunk)
{
SLANG_UNEXPECTED("invalid format for serialized module IR");
}
Fossil::AnyValPtr rootValPtr =
Fossil::getRootValue(dataChunk->getPayload(), dataChunk->getPayloadSize());
if (!rootValPtr)
{
SLANG_UNEXPECTED("invalid format for serialized module IR");
}
Fossilized<IRModuleInfo>* fossilizedModuleInfo = cast<Fossilized<IRModuleInfo>>(rootValPtr);
Fossilized<IRModule>* fossilizedModule = fossilizedModuleInfo->module;
version = fossilizedModule->m_version;
compilerVersion = fossilizedModuleInfo->fullVersion.get();
name = fossilizedModuleInfo->module->m_name.get();
return SLANG_OK;
}
// A helper to make profiling the actual deserialization work
// easier.
[[nodiscard]] static Result readSerializedModuleIR_(
RIFF::Chunk const* chunk,
Session* session,
SerialSourceLocReader* sourceLocReader,
RefPtr<IRModule>& outIRModule)
{
#if USE_RIFF
auto dataChunk = as<RIFF::ListChunk>(chunk);
if (!dataChunk)
{
SLANG_UNEXPECTED("invalid format for serialized module IR");
}
IRModuleInfo info;
auto sharedDecodingContext = RefPtr(new IRSerialReadContext(session, sourceLocReader));
{
RIFFSerialReader reader(dataChunk);
IRReadSerializer serializer(&reader, sharedDecodingContext);
serialize(serializer, info);
}
#else
auto dataChunk = as<RIFF::DataChunk>(chunk);
if (!dataChunk)
{
SLANG_UNEXPECTED("invalid format for serialized module IR");
}
Fossil::AnyValPtr rootValPtr =
Fossil::getRootValue(dataChunk->getPayload(), dataChunk->getPayloadSize());
if (!rootValPtr)
{
SLANG_UNEXPECTED("invalid format for serialized module IR");
}
Fossilized<IRModuleInfo>* fossilizedModuleInfo = cast<Fossilized<IRModuleInfo>>(rootValPtr);
// Only one version supported so far, if we had multiple versions to
// support this is where we might branch
if (fossilizedModuleInfo->serializationVersion != IRModuleInfo::kSupportedSerializationVersion)
return SLANG_FAIL;
IRModuleInfo info;
auto sharedDecodingContext = RefPtr(new IRSerialReadContext(session, sourceLocReader));
{
Fossil::ReadContext readContext;
Fossil::SerialReader reader(
readContext,
rootValPtr,
Fossil::SerialReader::InitialStateType::Root);
IRReadSerializer serializer(&reader, sharedDecodingContext);
serialize(serializer, info);
}
#endif
SLANG_ASSERT(info.module);
outIRModule = info.module;
if (sharedDecodingContext->_foundUnrecognizedInstructions)
return SLANG_FAIL;
return SLANG_OK;
}
Result readSerializedModuleIR(
RIFF::Chunk const* chunk,
Session* session,
SerialSourceLocReader* sourceLocReader,
RefPtr<IRModule>& outIRModule)
{
SLANG_RETURN_ON_FAIL(readSerializedModuleIR_(chunk, session, sourceLocReader, outIRModule));
//
// Module is finally valid (or at least as much as it was going it) and
// ready to be used
//
outIRModule->buildMangledNameToGlobalInstMap();
return SLANG_OK;
}
} // namespace Slang
|