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
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
|
// slang-serialize-container.cpp
#include "slang-serialize-container.h"
#include "../core/slang-byte-encode-util.h"
#include "../core/slang-math.h"
#include "../core/slang-stream.h"
#include "../core/slang-text-io.h"
#include "slang-check-impl.h"
#include "slang-compiler.h"
#include "slang-mangled-lexer.h"
#include "slang-parser.h"
#include "slang-serialize-ast.h"
#include "slang-serialize-factory.h"
#include "slang-serialize-ir.h"
#include "slang-serialize-source-loc.h"
namespace Slang
{
/* static */ SlangResult SerialContainerUtil::write(
Module* module,
const WriteOptions& options,
Stream* stream)
{
RiffContainer container;
{
SerialContainerData data;
SLANG_RETURN_ON_FAIL(SerialContainerUtil::addModuleToData(module, options, data));
SLANG_RETURN_ON_FAIL(SerialContainerUtil::write(data, options, &container));
}
// We now write the RiffContainer to the stream
SLANG_RETURN_ON_FAIL(RiffUtil::write(container.getRoot(), true, stream));
return SLANG_OK;
}
/* static */ SlangResult SerialContainerUtil::write(
FrontEndCompileRequest* frontEndReq,
const WriteOptions& options,
Stream* stream)
{
RiffContainer container;
{
SerialContainerData data;
SLANG_RETURN_ON_FAIL(
SerialContainerUtil::addFrontEndRequestToData(frontEndReq, options, data));
SLANG_RETURN_ON_FAIL(SerialContainerUtil::write(data, options, &container));
}
// We now write the RiffContainer to the stream
SLANG_RETURN_ON_FAIL(RiffUtil::write(container.getRoot(), true, stream));
return SLANG_OK;
}
/* static */ SlangResult SerialContainerUtil::write(
EndToEndCompileRequest* request,
const WriteOptions& options,
Stream* stream)
{
RiffContainer container;
{
SerialContainerData data;
SLANG_RETURN_ON_FAIL(SerialContainerUtil::addEndToEndRequestToData(request, options, data));
SLANG_RETURN_ON_FAIL(SerialContainerUtil::write(data, options, &container));
}
// We now write the RiffContainer to the stream
SLANG_RETURN_ON_FAIL(RiffUtil::write(container.getRoot(), true, stream));
return SLANG_OK;
}
/* static */ SlangResult SerialContainerUtil::addModuleToData(
Module* module,
const WriteOptions& options,
SerialContainerData& outData)
{
if (options.optionFlags & (SerialOptionFlag::ASTModule | SerialOptionFlag::IRModule))
{
SerialContainerData::Module dstModule;
// NOTE: The astBuilder is not set here, as not needed to be scoped for serialization (it is
// assumed the TranslationUnitRequest stays in scope)
if (options.optionFlags & SerialOptionFlag::ASTModule)
{
// Root AST node
auto moduleDecl = module->getModuleDecl();
SLANG_ASSERT(moduleDecl);
dstModule.astRootNode = moduleDecl;
}
if (options.optionFlags & SerialOptionFlag::IRModule)
{
// IR module
dstModule.irModule = module->getIRModule();
SLANG_ASSERT(dstModule.irModule);
}
// Here we assume that the first file in the file dependencies is the module's file path.
// We store the module's file path as a relative path with respect to the first search
// directory that contains the module, and store the paths of dependent files as relative
// paths with respect to the module's path.
// First, we try to extract the module's main file path from the file dependency list.
auto fileDependencies = module->getFileDependencies();
String canonicalModulePath, moduleDir;
if (fileDependencies.getCount() != 0)
{
IncludeSystem includeSystem(
&module->getLinkage()->getSearchDirectories(),
module->getLinkage()->getFileSystemExt(),
module->getLinkage()->getSourceManager());
PathInfo outFoundSourcePath;
// If we can find the first file, use that as the module's path.
if (SLANG_SUCCEEDED(includeSystem.findFile(
fileDependencies[0]->getPathInfo().foundPath,
"",
outFoundSourcePath)))
{
canonicalModulePath = outFoundSourcePath.foundPath;
Path::getCanonical(canonicalModulePath, canonicalModulePath);
moduleDir = Path::getParentDirectory(canonicalModulePath);
}
}
// If we can't find the module's path from the file dependencies list above,
// use the file path stored on the module as a fallback.
// Note that if the module is loaded from a binary precompiled module,
// this path will be pointing to that binary file instead of the original source file.
if (!canonicalModulePath.getLength())
{
if (auto modulePath = module->getFilePath())
{
canonicalModulePath = modulePath;
Path::getCanonical(canonicalModulePath, canonicalModulePath);
moduleDir = Path::getParentDirectory(canonicalModulePath);
}
}
// Find the first search directory that contains the module's main file path,
// so we can store the module's path (the first entry in the dependent files list)
// as a relative path with respect to that directory.
String linkageRoot = ".";
if (auto linkage = module->getLinkage())
{
for (const auto& searchDir : linkage->getSearchDirectories().searchDirectories)
{
String fullSearchDir;
Path::getCanonical(searchDir.path, fullSearchDir);
String relativePath = Path::getRelativePath(fullSearchDir, canonicalModulePath);
if (!Path::hasRelativeElement(relativePath))
{
linkageRoot = searchDir.path;
break;
}
}
}
Path::getCanonical(linkageRoot, linkageRoot);
for (auto file : fileDependencies)
{
if (file->getPathInfo().hasFoundPath())
{
String canonicalFilePath = file->getPathInfo().foundPath;
Path::getCanonical(file->getPathInfo().foundPath, canonicalFilePath);
// If the dependnet file is the same as the module's file path, store it as a
// relative path with respect to the search directory discovered above.
if (file->getPathInfo().hasFileFoundPath())
{
if (canonicalFilePath == canonicalModulePath)
{
auto relativeModulePath =
Path::getRelativePath(linkageRoot, canonicalModulePath);
dstModule.dependentFiles.add(relativeModulePath);
}
else
{
// For all other dependnet files, store them as relative paths with respect
// to the module's path.
canonicalFilePath = Path::getRelativePath(moduleDir, canonicalFilePath);
dstModule.dependentFiles.add(canonicalFilePath);
}
}
else
{
// If the module is coming from string instead of an actual file, store it as
// is.
dstModule.dependentFiles.add(canonicalModulePath);
}
}
else
{
dstModule.dependentFiles.add(file->getPathInfo().getMostUniqueIdentity());
}
}
dstModule.digest = module->computeDigest();
outData.modules.add(dstModule);
}
return SLANG_OK;
}
/* static */ SlangResult SerialContainerUtil::addFrontEndRequestToData(
FrontEndCompileRequest* frontEndReq,
const WriteOptions& options,
SerialContainerData& outData)
{
// Go through translation units, adding modules
for (TranslationUnitRequest* translationUnit : frontEndReq->translationUnits)
{
SLANG_RETURN_ON_FAIL(addModuleToData(translationUnit->module, options, outData));
}
return SLANG_OK;
}
/* static */ SlangResult SerialContainerUtil::addEndToEndRequestToData(
EndToEndCompileRequest* request,
const WriteOptions& options,
SerialContainerData& out)
{
auto linkage = request->getLinkage();
auto sink = request->getSink();
// Output the parsed modules.
addFrontEndRequestToData(request->getFrontEndReq(), options, out);
// If we are skipping code generation, then we are done.
if (request->getOptionSet().getBoolOption(CompilerOptionName::SkipCodeGen))
{
return SLANG_OK;
}
//
auto program = request->getSpecializedGlobalAndEntryPointsComponentType();
// Add all the target modules
{
for (auto target : linkage->targets)
{
auto targetProgram = program->getTargetProgram(target);
auto irModule = targetProgram->getOrCreateIRModuleForLayout(sink);
SerialContainerData::TargetComponent targetComponent;
targetComponent.irModule = irModule;
auto& dstTarget = targetComponent.target;
dstTarget.floatingPointMode = target->getOptionSet().getFloatingPointMode();
dstTarget.profile = target->getOptionSet().getProfile();
dstTarget.flags = target->getOptionSet().getTargetFlags();
dstTarget.codeGenTarget = target->getTarget();
out.targetComponents.add(targetComponent);
}
}
// Entry points
{
auto entryPointCount = program->getEntryPointCount();
for (Index ii = 0; ii < entryPointCount; ++ii)
{
auto entryPoint = program->getEntryPoint(ii);
auto entryPointMangledName = program->getEntryPointMangledName(ii);
SerialContainerData::EntryPoint dstEntryPoint;
dstEntryPoint.name = entryPoint->getName();
dstEntryPoint.mangledName = entryPointMangledName;
dstEntryPoint.profile = entryPoint->getProfile();
out.entryPoints.add(dstEntryPoint);
}
}
return SLANG_OK;
}
/* static */ SlangResult SerialContainerUtil::write(
const SerialContainerData& data,
const WriteOptions& options,
RiffContainer* container)
{
RefPtr<SerialSourceLocWriter> sourceLocWriter;
// The string pool used across the whole of the container
StringSlicePool containerStringPool(StringSlicePool::Style::Default);
RiffContainer::ScopeChunk scopeModule(
container,
RiffContainer::Chunk::Kind::List,
SerialBinary::kContainerFourCc);
// Write the header
{
SerialBinary::ContainerHeader containerHeader;
// Save the compression type if used - as can only serialize with a single compression type
containerHeader.compressionType = uint32_t(options.compressionType);
RiffContainer::ScopeChunk scopeHeader(
container,
RiffContainer::Chunk::Kind::Data,
SerialBinary::kContainerHeaderFourCc);
container->write(&containerHeader, sizeof(containerHeader));
}
if (data.modules.getCount() &&
(options.optionFlags & (SerialOptionFlag::IRModule | SerialOptionFlag::ASTModule)))
{
// Module list
RiffContainer::ScopeChunk moduleListScope(
container,
RiffContainer::Chunk::Kind::List,
SerialBinary::kModuleListFourCc);
if (options.optionFlags & SerialOptionFlag::SourceLocation)
{
sourceLocWriter = new SerialSourceLocWriter(options.sourceManager);
}
RefPtr<SerialClasses> serialClasses;
for (const auto& module : data.modules)
{
// Okay, we need to serialize this module to our container file.
// We currently don't serialize it's name..., but support for that could be added.
// First, we write a header that can be used to verify if the precompiled module is
// up-to-date. The header has: 1) a digest of all compile options and dependent source
// files. 2) a list of source file paths.
//
{
RiffContainer::ScopeChunk scopeHeader(
container,
RiffContainer::Chunk::Kind::Data,
SerialBinary::kModuleHeaderFourCc);
OwnedMemoryStream headerMemStream(FileAccess::Write);
StringBuilder filePathsSB;
for (auto fileDependency : module.dependentFiles)
filePathsSB << fileDependency << "\n";
headerMemStream.write(module.digest.data, sizeof(module.digest.data));
uint32_t fileListLength = (uint32_t)filePathsSB.getLength();
headerMemStream.write(&fileListLength, sizeof(uint32_t));
headerMemStream.write(filePathsSB.getBuffer(), fileListLength);
container->write(
headerMemStream.getContents().getBuffer(),
headerMemStream.getContents().getCount());
}
// Write the IR information
if ((options.optionFlags & SerialOptionFlag::IRModule) && module.irModule)
{
IRSerialData serialData;
IRSerialWriter writer;
SLANG_RETURN_ON_FAIL(writer.write(
module.irModule,
sourceLocWriter,
options.optionFlags,
&serialData));
SLANG_RETURN_ON_FAIL(
IRSerialWriter::writeContainer(serialData, options.compressionType, container));
}
// Write the AST information
if (options.optionFlags & SerialOptionFlag::ASTModule)
{
if (ModuleDecl* moduleDecl = as<ModuleDecl>(module.astRootNode))
{
// Put in AST module
RiffContainer::ScopeChunk scopeASTModule(
container,
RiffContainer::Chunk::Kind::List,
ASTSerialBinary::kSlangASTModuleFourCC);
if (!serialClasses)
{
SLANG_RETURN_ON_FAIL(SerialClassesUtil::create(serialClasses));
}
ModuleSerialFilter filter(moduleDecl);
auto astWriterFlag = SerialWriter::Flag::ZeroInitialize;
if ((options.optionFlags & SerialOptionFlag::ASTFunctionBody) == 0)
astWriterFlag = (SerialWriter::Flag::Enum)(
astWriterFlag | SerialWriter::Flag::SkipFunctionBody);
SerialWriter writer(serialClasses, &filter, astWriterFlag);
writer.getExtraObjects().set(sourceLocWriter);
// Add the module and everything that isn't filtered out in the filter.
writer.addPointer(moduleDecl);
// We can now serialize it into the riff container.
SLANG_RETURN_ON_FAIL(writer.writeIntoContainer(
ASTSerialBinary::kSlangASTModuleDataFourCC,
container));
}
}
}
// TODO:
// Serialization of target component IR is causing the embedded precompiled binary
// feature to fail. The resulting data modules contain both TU IR and TC IR, with only
// one module header. Yong suggested to ignore the TC IR for now, though also that
// OV was using the feature, so disabling this might cause problems.
#if 0
if (data.targetComponents.getCount() && (options.optionFlags & SerialOptionFlag::IRModule))
{
// TODO: in the case where we have specialization, we might need
// to serialize IR related to `program`...
for (const auto& targetComponent : data.targetComponents)
{
IRModule* irModule = targetComponent.irModule;
// Okay, we need to serialize this target program and its IR too...
IRSerialData serialData;
IRSerialWriter writer;
SLANG_RETURN_ON_FAIL(writer.write(irModule, sourceLocWriter, options.optionFlags, &serialData));
SLANG_RETURN_ON_FAIL(IRSerialWriter::writeContainer(serialData, options.compressionType, container));
}
}
#endif
}
if (data.entryPoints.getCount())
{
for (const auto& entryPoint : data.entryPoints)
{
RiffContainer::ScopeChunk entryPointScope(
container,
RiffContainer::Chunk::Kind::Data,
SerialBinary::kEntryPointFourCc);
SerialContainerBinary::EntryPoint dst;
dst.name = uint32_t(containerStringPool.add(entryPoint.name->text));
dst.profile = entryPoint.profile.raw;
dst.mangledName = uint32_t(containerStringPool.add(entryPoint.mangledName));
container->write(&dst, sizeof(dst));
}
}
// We can now output the debug information. This is for all IR and AST
if (sourceLocWriter)
{
// Write out the debug info
SerialSourceLocData debugData;
sourceLocWriter->write(&debugData);
debugData.writeContainer(options.compressionType, container);
}
// Write the container string table
if (containerStringPool.getAdded().getCount() > 0)
{
RiffContainer::ScopeChunk stringTableScope(
container,
RiffContainer::Chunk::Kind::Data,
SerialBinary::kStringTableFourCc);
List<char> encodedTable;
SerialStringTableUtil::encodeStringTable(containerStringPool, encodedTable);
container->write(encodedTable.getBuffer(), encodedTable.getCount());
}
return SLANG_OK;
}
static List<ExtensionDecl*>& _getCandidateExtensionList(
AggTypeDecl* typeDecl,
Dictionary<AggTypeDecl*, RefPtr<CandidateExtensionList>>& mapTypeToCandidateExtensions)
{
RefPtr<CandidateExtensionList> entry;
if (!mapTypeToCandidateExtensions.tryGetValue(typeDecl, entry))
{
entry = new CandidateExtensionList();
mapTypeToCandidateExtensions.add(typeDecl, entry);
}
return entry->candidateExtensions;
}
/* static */ Result SerialContainerUtil::read(
RiffContainer* container,
const ReadOptions& options,
const LoadedModuleDictionary* additionalLoadedModules,
SerialContainerData& out)
{
out.clear();
RiffContainer::ListChunk* containerChunk =
container->getRoot()->findListRec(SerialBinary::kContainerFourCc);
if (!containerChunk)
{
// Must be a container
return SLANG_FAIL;
}
SerialBinary::ContainerHeader* containerHeader =
containerChunk->findContainedData<SerialBinary::ContainerHeader>(
SerialBinary::kContainerHeaderFourCc);
if (!containerHeader)
{
// Didn't find the header
return SLANG_FAIL;
}
const SerialCompressionType containerCompressionType =
SerialCompressionType(containerHeader->compressionType);
StringSlicePool containerStringPool(StringSlicePool::Style::Default);
if (RiffContainer::Data* stringTableData =
containerChunk->findContainedData(SerialBinary::kStringTableFourCc))
{
SerialStringTableUtil::decodeStringTable(
(const char*)stringTableData->getPayload(),
stringTableData->getSize(),
containerStringPool);
}
RefPtr<SerialSourceLocReader> sourceLocReader;
RefPtr<SerialClasses> serialClasses;
// Debug information
if (auto debugChunk = containerChunk->findContainedList(SerialSourceLocData::kDebugFourCc))
{
// Read into data
SerialSourceLocData sourceLocData;
SLANG_RETURN_ON_FAIL(sourceLocData.readContainer(containerCompressionType, debugChunk));
// Turn into DebugReader
sourceLocReader = new SerialSourceLocReader;
SLANG_RETURN_ON_FAIL(sourceLocReader->read(&sourceLocData, options.sourceManager));
}
// Create a source loc representing the binary module.
SourceLoc binaryModuleLoc = SourceLoc();
if (options.modulePath.getLength())
{
auto srcManager = options.linkage->getSourceManager();
auto modulePathInfo = PathInfo::makePath(options.modulePath);
auto srcFile = srcManager->findSourceFileByPathRecursively(modulePathInfo.foundPath);
if (!srcFile)
{
srcFile = srcManager->createSourceFileWithString(modulePathInfo, String());
srcManager->addSourceFile(options.modulePath, srcFile);
}
auto srcView = srcManager->createSourceView(srcFile, &modulePathInfo, SourceLoc());
binaryModuleLoc = srcView->getRange().begin;
}
// Add modules
if (RiffContainer::ListChunk* moduleList =
containerChunk->findContainedList(SerialBinary::kModuleListFourCc))
{
RiffContainer::Chunk* chunk = moduleList->getFirstContainedChunk();
while (chunk)
{
auto startChunk = chunk;
RefPtr<ASTBuilder> astBuilder = options.astBuilder;
NodeBase* astRootNode = nullptr;
RefPtr<IRModule> irModule;
SerialContainerData::Module module;
if (auto headerChunk =
as<RiffContainer::DataChunk>(chunk, SerialBinary::kModuleHeaderFourCc))
{
MemoryStreamBase memStream(
FileAccess::Read,
headerChunk->getSingleData()->getPayload(),
headerChunk->getSingleData()->getSize());
size_t readSize = 0;
memStream.read(module.digest.data, sizeof(SHA1::Digest), readSize);
if (readSize != sizeof(SHA1::Digest))
return SLANG_FAIL;
uint32_t fileListLength = 0;
memStream.read(&fileListLength, sizeof(uint32_t), readSize);
if (readSize != sizeof(uint32_t))
return SLANG_FAIL;
List<uint8_t> fileListContent;
fileListContent.setCount(fileListLength);
memStream.read(fileListContent.getBuffer(), fileListContent.getCount(), readSize);
if (readSize != (size_t)fileListContent.getCount())
return SLANG_FAIL;
UnownedStringSlice fileListString(
(const char*)fileListContent.getBuffer(),
fileListContent.getCount());
List<UnownedStringSlice> fileList;
StringUtil::split(fileListString, '\n', fileList);
for (auto file : fileList)
{
if (file.getLength())
{
module.dependentFiles.add(file);
}
}
// Onto next chunk
chunk = chunk->m_next;
}
if (auto irChunk = as<RiffContainer::ListChunk>(chunk, IRSerialBinary::kIRModuleFourCc))
{
if (!options.readHeaderOnly)
{
IRSerialData serialData;
SLANG_RETURN_ON_FAIL(IRSerialReader::readContainer(
irChunk,
containerCompressionType,
&serialData));
// Read IR back from serialData
IRSerialReader reader;
SLANG_RETURN_ON_FAIL(
reader.read(serialData, options.session, sourceLocReader, irModule));
}
// Onto next chunk
chunk = chunk->m_next;
}
if (auto astChunk =
as<RiffContainer::ListChunk>(chunk, ASTSerialBinary::kSlangASTModuleFourCC))
{
if (!options.readHeaderOnly)
{
RiffContainer::Data* astData =
astChunk->findContainedData(ASTSerialBinary::kSlangASTModuleDataFourCC);
if (astData)
{
if (!serialClasses)
{
SLANG_RETURN_ON_FAIL(SerialClassesUtil::create(serialClasses));
}
// TODO(JS): We probably want to store off better information about each of
// the translation unit including some kind of 'name'. For now we just
// generate a name.
StringBuilder buf;
buf << "tu" << out.modules.getCount();
if (!astBuilder)
{
astBuilder =
new ASTBuilder(options.sharedASTBuilder, buf.produceString());
}
/// We need to make the current ASTBuilder available for access via
/// thread_local global.
SetASTBuilderContextRAII astBuilderRAII(astBuilder);
DefaultSerialObjectFactory objectFactory(astBuilder);
SerialReader reader(serialClasses, &objectFactory);
// Sets up the entry table - one entry for each 'object'.
// No native objects are constructed. No objects are deserialized.
SLANG_RETURN_ON_FAIL(reader.loadEntries(
(const uint8_t*)astData->getPayload(),
astData->getSize()));
// Construct a native object for each table entry (where appropriate).
// Note that this *doesn't* set all object pointers - some are special cased
// and created on demand (strings) and imported symbols will have their
// object pointers unset (they are resolved in next step)
SLANG_RETURN_ON_FAIL(reader.constructObjects(options.namePool));
// Resolve external references if the linkage is specified
if (options.linkage)
{
const auto& entries = reader.getEntries();
auto& objects = reader.getObjects();
const Index entriesCount = entries.getCount();
String currentModuleName;
Module* currentModule = nullptr;
// Index from 1 (0 is null)
for (Index i = 1; i < entriesCount; ++i)
{
const SerialInfo::Entry* entry = entries[i];
if (entry->typeKind == SerialTypeKind::ImportSymbol)
{
// Import symbols are always serialized with a mangled name in
// the form of <module_name>!<symbol_mangled_name>. As
// symbol_mangled_name may not contain the name of its parent
// module in the case of an `extern` or `export` symbol.
//
UnownedStringSlice mangledName =
reader.getStringSlice(SerialIndex(i));
List<UnownedStringSlice> slicesOut;
StringUtil::split(mangledName, '!', slicesOut);
if (slicesOut.getCount() != 2)
return SLANG_FAIL;
auto moduleName = slicesOut[0];
mangledName = slicesOut[1];
// If we already have looked up this module and it has the same
// name just use what we have
Module* readModule = nullptr;
if (currentModule &&
moduleName == currentModuleName.getUnownedSlice())
{
readModule = currentModule;
}
else
{
// The modules are loaded on the linkage.
Linkage* linkage = options.linkage;
NamePool* namePool = linkage->getNamePool();
Name* moduleNameName = namePool->getName(moduleName);
readModule = linkage->findOrImportModule(
moduleNameName,
binaryModuleLoc,
options.sink,
additionalLoadedModules);
if (!readModule)
{
return SLANG_FAIL;
}
// Set the current module and name
currentModule = readModule;
currentModuleName = moduleName;
}
// Look up the symbol
NodeBase* nodeBase =
readModule->findExportFromMangledName(mangledName);
if (!nodeBase)
{
if (options.sink)
{
options.sink->diagnose(
SourceLoc::fromRaw(0),
Diagnostics::unableToFindSymbolInModule,
mangledName,
moduleName);
}
// If didn't find the export then we create an
// UnresolvedDecl node to represent the error.
auto unresolved = astBuilder->create<UnresolvedDecl>();
unresolved->nameAndLoc.name =
options.linkage->getNamePool()->getName(mangledName);
nodeBase = unresolved;
}
// set the result
objects[i] = nodeBase;
}
}
}
// Set the sourceLocReader before doing de-serialize, such can lookup the
// remapped sourceLocs
reader.getExtraObjects().set(sourceLocReader);
// TODO(JS):
// If modules can have more complicated relationships (like a two modules
// can refer to symbols from each other), then we can make this work by 1)
// deserialize *without* the external symbols being set up 2) calculate the
// symbols 3) deserialize the other module (in the same way) 4) run
// deserializeObjects *again* on each module This is less efficient than it
// might be (because deserialize phase is done twice) so if this is
// necessary may want a mechanism that *just* does reference lookups.
//
// For now if we assume a module can only access symbols from another
// module, and not the reverse. So we just need to deserialize and we are
// done
SLANG_RETURN_ON_FAIL(reader.deserializeObjects());
// Get the root node. It's at index 1 (0 is the null value).
astRootNode = reader.getPointer(SerialIndex(1)).dynamicCast<NodeBase>();
// Go through all AST nodes:
// 1) Add the extensions to the module mapTypeToCandidateExtensions cache
// 2) We need to fix the callback pointers for parsing
// 3) Register all `Val`s to the ASTBuilder's deduplication map.
{
ModuleDecl* moduleDecl = as<ModuleDecl>(astRootNode);
// Maps from keyword name name to index in (syntaxParseInfos)
// Will be filled in lazily if needed (for SyntaxDecl setup)
Dictionary<Name*, Index> syntaxKeywordDict;
OrderedDictionary<Val*, List<Val**>> valUses;
// Get the parse infos
const auto syntaxParseInfos = getSyntaxParseInfos();
SLANG_ASSERT(syntaxParseInfos.getCount());
for (auto& obj : reader.getObjects())
{
if (obj.m_kind == SerialTypeKind::NodeBase)
{
NodeBase* nodeBase = (NodeBase*)obj.m_ptr;
SLANG_ASSERT(nodeBase);
if (ExtensionDecl* extensionDecl =
dynamicCast<ExtensionDecl>(nodeBase))
{
if (auto targetDeclRefType =
as<DeclRefType>(extensionDecl->targetType))
{
ShortList<AggTypeDecl*> baseDecls;
getExtensionTargetDeclList(
astBuilder,
targetDeclRefType,
extensionDecl,
baseDecls);
for (auto baseDecl : baseDecls)
{
_getCandidateExtensionList(
baseDecl,
moduleDecl->mapTypeToCandidateExtensions)
.add(extensionDecl);
}
}
}
else if (
SyntaxDecl* syntaxDecl = dynamicCast<SyntaxDecl>(nodeBase))
{
// Set up the dictionary lazily
if (syntaxKeywordDict.getCount() == 0)
{
NamePool* namePool = options.session->getNamePool();
for (Index i = 0; i < syntaxParseInfos.getCount(); ++i)
{
const auto& entry = syntaxParseInfos[i];
syntaxKeywordDict.add(
namePool->getName(entry.keywordName),
i);
}
// Must have something in it at this point
SLANG_ASSERT(syntaxKeywordDict.getCount());
}
// Look up the index
Index* entryIndexPtr =
syntaxKeywordDict.tryGetValue(syntaxDecl->getName());
if (entryIndexPtr)
{
// Set up SyntaxDecl based on the ParseSyntaxIndo
auto& info = syntaxParseInfos[*entryIndexPtr];
syntaxDecl->parseCallback = *info.callback;
syntaxDecl->parseUserData =
const_cast<ReflectClassInfo*>(info.classInfo);
}
else
{
// If we don't find a setup entry, we use
// `parseSimpleSyntax`, and set the parseUserData to the
// ReflectClassInfo (as parseSimpleSyntax needs this)
syntaxDecl->parseCallback = &parseSimpleSyntax;
SLANG_ASSERT(syntaxDecl->syntaxClass.classInfo);
syntaxDecl->parseUserData =
const_cast<ReflectClassInfo*>(
syntaxDecl->syntaxClass.classInfo);
}
}
else if (Val* val = dynamicCast<Val>(nodeBase))
{
val->_setUnique();
}
}
}
}
}
}
// Onto next chunk
chunk = chunk->m_next;
}
if (astBuilder || irModule)
{
module.astBuilder = astBuilder;
module.astRootNode = astRootNode;
module.irModule = irModule;
out.modules.add(module);
}
// If no progress, step to next chunk
chunk = (chunk == startChunk) ? chunk->m_next : chunk;
}
}
// Add all the entry points
{
List<RiffContainer::DataChunk*> entryPointChunks;
containerChunk->findContained(SerialBinary::kEntryPointFourCc, entryPointChunks);
for (auto entryPointChunk : entryPointChunks)
{
auto reader = entryPointChunk->asReadHelper();
SerialContainerBinary::EntryPoint srcEntryPoint;
SLANG_RETURN_ON_FAIL(reader.read(srcEntryPoint));
SerialContainerData::EntryPoint dstEntryPoint;
dstEntryPoint.name = options.namePool->getName(
containerStringPool.getSlice(StringSlicePool::Handle(srcEntryPoint.name)));
dstEntryPoint.profile.raw = srcEntryPoint.profile;
dstEntryPoint.mangledName =
containerStringPool.getSlice(StringSlicePool::Handle(srcEntryPoint.mangledName));
out.entryPoints.add(dstEntryPoint);
}
}
return SLANG_OK;
}
/* static */ SlangResult SerialContainerUtil::verifyIRSerialize(
IRModule* module,
Session* session,
const WriteOptions& options)
{
// Verify if we can stream out with raw source locs
List<IRInst*> originalInsts;
IRSerialWriter::calcInstructionList(module, originalInsts);
IRSerialData irData;
OwnedMemoryStream memoryStream(FileAccess::ReadWrite);
{
RiffContainer riffContainer;
// Need to put all of this in a container
RiffContainer::ScopeChunk containerScope(
&riffContainer,
RiffContainer::Chunk::Kind::List,
SerialBinary::kContainerFourCc);
RefPtr<SerialSourceLocWriter> sourceLocWriter;
if (options.optionFlags & SerialOptionFlag::SourceLocation)
{
sourceLocWriter = new SerialSourceLocWriter(options.sourceManager);
}
{
// Write IR out to serialData - copying over SourceLoc information directly
IRSerialWriter writer;
SLANG_RETURN_ON_FAIL(
writer.write(module, sourceLocWriter, options.optionFlags, &irData));
}
SLANG_RETURN_ON_FAIL(
IRSerialWriter::writeContainer(irData, options.compressionType, &riffContainer));
// Write the debug info Riff container
if (sourceLocWriter)
{
SerialSourceLocData serialData;
sourceLocWriter->write(&serialData);
SLANG_RETURN_ON_FAIL(
serialData.writeContainer(options.compressionType, &riffContainer));
}
SLANG_RETURN_ON_FAIL(RiffUtil::write(&riffContainer, &memoryStream));
}
// Reset stream
memoryStream.seek(SeekOrigin::Start, 0);
SourceManager workSourceManager;
workSourceManager.initialize(options.sourceManager, nullptr);
// The read ir module
RefPtr<IRModule> irReadModule;
{
RiffContainer riffContainer;
SLANG_RETURN_ON_FAIL(RiffUtil::read(&memoryStream, riffContainer));
RiffContainer::ListChunk* rootList = riffContainer.getRoot();
RefPtr<SerialSourceLocReader> sourceLocReader;
// If we have debug info then find and read it
if (options.optionFlags & SerialOptionFlag::SourceLocation)
{
RiffContainer::ListChunk* debugList =
rootList->findContainedList(SerialSourceLocData::kDebugFourCc);
if (!debugList)
{
return SLANG_FAIL;
}
SerialSourceLocData sourceLocData;
SLANG_RETURN_ON_FAIL(sourceLocData.readContainer(options.compressionType, debugList));
sourceLocReader = new SerialSourceLocReader;
SLANG_RETURN_ON_FAIL(sourceLocReader->read(&sourceLocData, &workSourceManager));
}
{
RiffContainer::ListChunk* irList =
rootList->findContainedList(IRSerialBinary::kIRModuleFourCc);
if (!irList)
{
return SLANG_FAIL;
}
{
IRSerialData irReadData;
IRSerialReader reader;
SLANG_RETURN_ON_FAIL(
reader.readContainer(irList, options.compressionType, &irReadData));
// Check the stream read data is the same
if (irData != irReadData)
{
SLANG_ASSERT(!"Streamed in data doesn't match");
return SLANG_FAIL;
}
SLANG_RETURN_ON_FAIL(reader.read(irData, session, sourceLocReader, irReadModule));
}
}
}
List<IRInst*> readInsts;
IRSerialWriter::calcInstructionList(irReadModule, readInsts);
if (readInsts.getCount() != originalInsts.getCount())
{
SLANG_ASSERT(!"Instruction counts don't match");
return SLANG_FAIL;
}
if (options.optionFlags & SerialOptionFlag::RawSourceLocation)
{
SLANG_ASSERT(readInsts[0] == originalInsts[0]);
// All the source locs should be identical
for (Index i = 1; i < readInsts.getCount(); ++i)
{
IRInst* origInst = originalInsts[i];
IRInst* readInst = readInsts[i];
if (origInst->sourceLoc.getRaw() != readInst->sourceLoc.getRaw())
{
SLANG_ASSERT(!"Source locs don't match");
return SLANG_FAIL;
}
}
}
else if (options.optionFlags & SerialOptionFlag::SourceLocation)
{
// They should be on the same line nos
for (Index i = 1; i < readInsts.getCount(); ++i)
{
IRInst* origInst = originalInsts[i];
IRInst* readInst = readInsts[i];
if (origInst->sourceLoc.getRaw() == readInst->sourceLoc.getRaw())
{
continue;
}
// Work out the
SourceView* origSourceView = options.sourceManager->findSourceView(origInst->sourceLoc);
SourceView* readSourceView = workSourceManager.findSourceView(readInst->sourceLoc);
// if both are null we are done
if (origSourceView == nullptr && origSourceView == readSourceView)
{
continue;
}
SLANG_ASSERT(origSourceView && readSourceView);
// The offset should be the same
Index origOffset =
origInst->sourceLoc.getRaw() - origSourceView->getRange().begin.getRaw();
Index readOffset =
readInst->sourceLoc.getRaw() - readSourceView->getRange().begin.getRaw();
if (origOffset != readOffset)
{
SLANG_ASSERT(!"SourceLoc offset debug data didn't match");
return SLANG_FAIL;
}
{
auto origInfo =
origSourceView->getHumaneLoc(origInst->sourceLoc, SourceLocType::Actual);
auto readInfo =
readSourceView->getHumaneLoc(readInst->sourceLoc, SourceLocType::Actual);
if (!(origInfo.line == readInfo.line && origInfo.column == readInfo.column &&
origInfo.pathInfo.foundPath == readInfo.pathInfo.foundPath))
{
SLANG_ASSERT(!"Debug data didn't match");
return SLANG_FAIL;
}
}
// We may have adjusted line numbers -> but they may not match, because we only
// reconstruct one view So for now disable this test
if (false)
{
auto origInfo =
origSourceView->getHumaneLoc(origInst->sourceLoc, SourceLocType::Nominal);
auto readInfo =
readSourceView->getHumaneLoc(readInst->sourceLoc, SourceLocType::Nominal);
if (!(origInfo.line == readInfo.line && origInfo.column == readInfo.column &&
origInfo.pathInfo.foundPath == readInfo.pathInfo.foundPath))
{
SLANG_ASSERT(!"Debug data didn't match");
return SLANG_FAIL;
}
}
}
}
return SLANG_OK;
}
} // namespace Slang
|