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
|
// slang-compile-request.cpp
#include "slang-compile-request.h"
#include "../core/slang-performance-profiler.h"
#include "compiler-core/slang-artifact-desc-util.h"
#include "compiler-core/slang-artifact-util.h"
#include "slang-ast-dump.h"
#include "slang-check-impl.h"
#include "slang-compiler.h"
#include "slang-emit-source-writer.h"
#include "slang-lower-to-ir.h"
#include "slang-parser.h"
#include "slang-serialize-container.h"
namespace Slang
{
//
// FrontEndEntryPointRequest
//
FrontEndEntryPointRequest::FrontEndEntryPointRequest(
FrontEndCompileRequest* compileRequest,
int translationUnitIndex,
Name* name,
Profile profile)
: m_compileRequest(compileRequest)
, m_translationUnitIndex(translationUnitIndex)
, m_name(name)
, m_profile(profile)
{
}
TranslationUnitRequest* FrontEndEntryPointRequest::getTranslationUnit()
{
return getCompileRequest()->translationUnits[m_translationUnitIndex];
}
//
// CompileRequestBase
//
CompileRequestBase::CompileRequestBase(Linkage* linkage, DiagnosticSink* sink)
: m_linkage(linkage), m_sink(sink)
{
}
Session* CompileRequestBase::getSession()
{
return getLinkage()->getSessionImpl();
}
//
// FrontEndCompileRequest
//
FrontEndCompileRequest::FrontEndCompileRequest(
Linkage* linkage,
StdWriters* writers,
DiagnosticSink* sink)
: CompileRequestBase(linkage, sink), m_writers(writers)
{
optionSet.inheritFrom(linkage->m_optionSet);
}
// Holds the hierarchy of views, the children being views that were 'initiated' (have an initiating
// SourceLoc) in the parent.
typedef Dictionary<SourceView*, List<SourceView*>> ViewInitiatingHierarchy;
// Calculate the hierarchy from the sourceManager
static void _calcViewInitiatingHierarchy(
SourceManager* sourceManager,
ViewInitiatingHierarchy& outHierarchy)
{
const List<SourceView*> emptyList;
outHierarchy.clear();
// Iterate over all managers
for (SourceManager* curManager = sourceManager; curManager;
curManager = curManager->getParent())
{
// Iterate over all views
for (SourceView* view : curManager->getSourceViews())
{
if (view->getInitiatingSourceLoc().isValid())
{
// Look up the view it came from
SourceView* parentView =
sourceManager->findSourceViewRecursively(view->getInitiatingSourceLoc());
if (parentView)
{
List<SourceView*>& children = outHierarchy.getOrAddValue(parentView, emptyList);
// It shouldn't have already been added
SLANG_ASSERT(children.indexOf(view) < 0);
children.add(view);
}
}
}
}
// Order all the children, by their raw SourceLocs. This is desirable, so that a trivial
// traversal will traverse children in the order they are initiated in the parent source. This
// assumes they increase in SourceLoc implies an later within a source file - this is true
// currently.
for (auto& [_, value] : outHierarchy)
{
value.sort(
[](SourceView* a, SourceView* b) -> bool {
return a->getInitiatingSourceLoc().getRaw() < b->getInitiatingSourceLoc().getRaw();
});
}
}
// Given a source file, find the view that is the initial SourceView use of the source. It must have
// an initiating SourceLoc that is not valid.
static SourceView* _findInitialSourceView(SourceFile* sourceFile)
{
// TODO(JS):
// This might be overkill - presumably the SourceView would belong to the same manager as it's
// SourceFile? That is not enforced by the SourceManager in any way though so we just search all
// managers, and all views.
for (SourceManager* sourceManager = sourceFile->getSourceManager(); sourceManager;
sourceManager = sourceManager->getParent())
{
for (SourceView* view : sourceManager->getSourceViews())
{
if (view->getSourceFile() == sourceFile && !view->getInitiatingSourceLoc().isValid())
{
return view;
}
}
}
return nullptr;
}
static void _outputInclude(SourceFile* sourceFile, Index depth, DiagnosticSink* sink)
{
StringBuilder buf;
for (Index i = 0; i < depth; ++i)
{
buf << " ";
}
// Output the found path for now
// TODO(JS). We could use the verbose paths flag to control what path is output -> as it may be
// useful to output the full path for example
const PathInfo& pathInfo = sourceFile->getPathInfo();
buf << "'" << pathInfo.foundPath << "'";
// TODO(JS)?
// You might want to know where this include was from.
// If I output this though there will be a problem... as the indenting won't be clearly shown.
// Perhaps I output in two sections, one the hierarchy and the other the locations of the
// includes?
sink->diagnose(SourceLoc(), Diagnostics::includeOutput, buf);
}
static void _outputIncludesRec(
SourceView* sourceView,
Index depth,
ViewInitiatingHierarchy& hierarchy,
DiagnosticSink* sink)
{
SourceFile* sourceFile = sourceView->getSourceFile();
const PathInfo& pathInfo = sourceFile->getPathInfo();
switch (pathInfo.type)
{
case PathInfo::Type::TokenPaste:
case PathInfo::Type::CommandLine:
case PathInfo::Type::TypeParse:
{
// If any of these types we don't output
return;
}
default:
break;
}
// Okay output this file at the current depth
_outputInclude(sourceFile, depth, sink);
// Now recurse to all of the children at the next depth
List<SourceView*>* children = hierarchy.tryGetValue(sourceView);
if (children)
{
for (SourceView* child : *children)
{
_outputIncludesRec(child, depth + 1, hierarchy, sink);
}
}
}
static void _outputPreprocessorTokens(const TokenList& toks, ISlangWriter* writer)
{
if (writer == nullptr)
{
return;
}
StringBuilder buf;
for (const auto& tok : toks)
{
buf << tok.getContent();
// We'll separate tokens with space for now
buf.appendChar(' ');
}
buf.appendChar('\n');
writer->write(buf.getBuffer(), buf.getLength());
}
static void _outputIncludes(
const List<SourceFile*>& sourceFiles,
SourceManager* sourceManager,
DiagnosticSink* sink)
{
// Set up the hierarchy to know how all the source views relate. This could be argued as
// overkill, but makes recursive output pretty simple
ViewInitiatingHierarchy hierarchy;
_calcViewInitiatingHierarchy(sourceManager, hierarchy);
// For all the source files
for (SourceFile* sourceFile : sourceFiles)
{
if (sourceFile->isIncludedFile())
continue;
// Find an initial view (this is the view of this file, that doesn't have an initiating loc)
SourceView* sourceView = _findInitialSourceView(sourceFile);
if (!sourceView)
{
// Okay, didn't find one, so just output the file
_outputInclude(sourceFile, 0, sink);
}
else
{
// Output from this view recursively
_outputIncludesRec(sourceView, 0, hierarchy, sink);
}
}
}
void FrontEndCompileRequest::parseTranslationUnit(TranslationUnitRequest* translationUnit)
{
SLANG_PROFILE;
if (translationUnit->isChecked)
return;
auto linkage = getLinkage();
SLANG_AST_BUILDER_RAII(linkage->getASTBuilder());
// TODO(JS): NOTE! Here we are using the searchDirectories on the linkage. This is because
// currently the API only allows the setting search paths on linkage.
//
// Here we should probably be using the searchDirectories on the FrontEndCompileRequest.
// If searchDirectories.parent pointed to the one in the Linkage would mean linkage paths
// would be checked too (after those on the FrontEndCompileRequest).
IncludeSystem includeSystem(
&linkage->getSearchDirectories(),
linkage->getFileSystemExt(),
linkage->getSourceManager());
auto combinedPreprocessorDefinitions = translationUnit->getCombinedPreprocessorDefinitions();
auto module = translationUnit->getModule();
ASTBuilder* astBuilder = module->getASTBuilder();
ModuleDecl* translationUnitSyntax = astBuilder->create<ModuleDecl>();
translationUnitSyntax->nameAndLoc.name = translationUnit->moduleName;
translationUnitSyntax->module = module;
module->setModuleDecl(translationUnitSyntax);
// When compiling a module of code that belongs to the Slang
// core module, we add a modifier to the module to act
// as a marker, so that downstream code can detect declarations
// that came from the core module (by walking up their
// chain of ancestors and looking for the marker), and treat
// them differently from user declarations.
//
// We are adding the marker here, before we even parse the
// code in the module, in case the subsequent steps would
// like to treat the core module differently. Alternatively
// we could pass down the `m_isStandardLibraryCode` flag to
// these passes.
//
if (m_isCoreModuleCode)
{
translationUnitSyntax->modifiers.first = astBuilder->create<FromCoreModuleModifier>();
}
// We use a custom handler for preprocessor callbacks, to
// ensure that relevant state that is only visible during
// preprocessoing can be communicated to later phases of
// compilation.
//
FrontEndPreprocessorHandler preprocessorHandler(module, astBuilder, getSink(), translationUnit);
for (auto sourceFile : translationUnit->getSourceFiles())
{
module->getIncludedSourceFileMap().addIfNotExists(sourceFile, nullptr);
}
for (auto sourceFile : translationUnit->getSourceFiles())
{
SourceLanguage sourceLanguage = translationUnit->sourceLanguage;
SlangLanguageVersion languageVersion =
translationUnit->compileRequest->optionSet.getLanguageVersion();
auto tokens = preprocessSource(
sourceFile,
getSink(),
&includeSystem,
combinedPreprocessorDefinitions,
getLinkage(),
sourceLanguage,
languageVersion,
&preprocessorHandler);
translationUnitSyntax->languageVersion = languageVersion;
if (sourceLanguage == SourceLanguage::Unknown)
sourceLanguage = translationUnit->sourceLanguage;
Scope* languageScope = nullptr;
switch (sourceLanguage)
{
case SourceLanguage::HLSL:
languageScope = getSession()->hlslLanguageScope;
break;
case SourceLanguage::GLSL:
languageScope = getSession()->glslLanguageScope;
break;
case SourceLanguage::Slang:
default:
languageScope = getSession()->slangLanguageScope;
break;
}
if (optionSet.getBoolOption(CompilerOptionName::OutputIncludes))
{
_outputIncludes(
translationUnit->getSourceFiles(),
getSink()->getSourceManager(),
getSink());
}
if (optionSet.getBoolOption(CompilerOptionName::PreprocessorOutput))
{
if (m_writers)
{
_outputPreprocessorTokens(
tokens,
m_writers->getWriter(SLANG_WRITER_CHANNEL_STD_OUTPUT));
}
// If we output the preprocessor output then we are done doing anything else
return;
}
parseSourceFile(
astBuilder,
translationUnit,
sourceLanguage,
tokens,
getSink(),
languageScope,
translationUnitSyntax);
// Let's try dumping
if (optionSet.getBoolOption(CompilerOptionName::DumpAst))
{
StringBuilder buf;
SourceWriter writer(linkage->getSourceManager(), LineDirectiveMode::None, nullptr);
ASTDumpUtil::dump(
translationUnit->getModuleDecl(),
ASTDumpUtil::Style::Flat,
0,
&writer);
const String& path = sourceFile->getPathInfo().foundPath;
if (path.getLength())
{
String fileName = Path::getFileNameWithoutExt(path);
fileName.append(".slang-ast");
File::writeAllText(fileName, writer.getContent());
}
}
#if 0
// Test serialization
{
ASTSerialTestUtil::testSerialize(translationUnit->getModuleDecl(), getSession()->getNamePool(), getLinkage()->getASTBuilder()->getSharedASTBuilder(), getSourceManager());
}
#endif
}
}
void FrontEndCompileRequest::checkAllTranslationUnits()
{
SLANG_PROFILE;
LoadedModuleDictionary loadedModules;
if (additionalLoadedModules)
loadedModules = *additionalLoadedModules;
// Iterate over all translation units and
// apply the semantic checking logic.
for (auto& translationUnit : translationUnits)
{
if (translationUnit->isChecked)
continue;
checkTranslationUnit(translationUnit.Ptr(), loadedModules);
// Add the checked module to list of loadedModules so that they can be
// discovered by `findOrImportModule` when processing future `import` decls.
// TODO: this does not handle the case where a translation unit to discover
// another translation unit added later to the compilation request.
// We should output an error message when we detect such a case, or support
// this scenario with a recursive style checking.
loadedModules.add(translationUnit->moduleName, translationUnit->getModule());
}
checkEntryPoints();
}
void FrontEndCompileRequest::generateIR()
{
SLANG_PROFILE;
SLANG_AST_BUILDER_RAII(getLinkage()->getASTBuilder());
// Our task in this function is to generate IR code
// for all of the declarations in the translation
// units that were loaded.
// Each translation unit is its own little world
// for code generation (we are not trying to
// replicate the GLSL linkage model), and so
// we will generate IR for each (if needed)
// in isolation.
for (auto& translationUnit : translationUnits)
{
// Skip if the module is precompiled.
if (translationUnit->getModule()->getIRModule())
continue;
// We want to only run generateIRForTranslationUnit once here. This is for two side effects:
// * it can dump ir
// * it can generate diagnostics
/// Generate IR for translation unit.
RefPtr<IRModule> irModule(
generateIRForTranslationUnit(getLinkage()->getASTBuilder(), translationUnit));
if (verifyDebugSerialization)
{
SerialContainerUtil::WriteOptions options;
options.sourceManagerToUseWhenSerializingSourceLocs = getSourceManager();
// Verify debug information
if (SLANG_FAILED(
SerialContainerUtil::verifyIRSerialize(irModule, getSession(), options)))
{
getSink()->diagnose(
irModule->getModuleInst()->sourceLoc,
Diagnostics::serialDebugVerificationFailed);
}
}
// Set the module on the translation unit
translationUnit->getModule()->setIRModule(irModule);
}
}
SlangResult FrontEndCompileRequest::executeActionsInner()
{
SLANG_PROFILE_SECTION(frontEndExecute);
SLANG_AST_BUILDER_RAII(getLinkage()->getASTBuilder());
for (TranslationUnitRequest* translationUnit : translationUnits)
{
// Make sure SourceFile representation is available for all translationUnits
SLANG_RETURN_ON_FAIL(translationUnit->requireSourceFiles());
}
// Parse everything from the input files requested
for (TranslationUnitRequest* translationUnit : translationUnits)
{
parseTranslationUnit(translationUnit);
}
if (optionSet.getBoolOption(CompilerOptionName::PreprocessorOutput))
{
// If doing pre-processor output, then we are done
return SLANG_OK;
}
if (getSink()->getErrorCount() != 0)
return SLANG_FAIL;
// Perform semantic checking on the whole collection
{
SLANG_PROFILE_SECTION(SemanticChecking);
checkAllTranslationUnits();
}
if (getSink()->getErrorCount() != 0)
return SLANG_FAIL;
// After semantic checking is performed we can try and output doc information for this
if (optionSet.getBoolOption(CompilerOptionName::Doc))
{
// TODO: implement the logic to output generated documents to target directory/zip file.
}
// Look up all the entry points that are expected,
// and use them to populate the `program` member.
//
m_globalComponentType = createUnspecializedGlobalComponentType(this);
if (getSink()->getErrorCount() != 0)
return SLANG_FAIL;
m_globalAndEntryPointsComponentType =
createUnspecializedGlobalAndEntryPointsComponentType(this, m_unspecializedEntryPoints);
if (getSink()->getErrorCount() != 0)
return SLANG_FAIL;
// We always generate IR for all the translation units.
//
// TODO: We may eventually have a mode where we skip
// IR codegen and only produce an AST (e.g., for use when
// debugging problems in the parser or semantic checking),
// but for now there are no cases where not having IR
// makes sense.
//
generateIR();
if (getSink()->getErrorCount() != 0)
return SLANG_FAIL;
// Do parameter binding generation, for each compilation target.
//
for (auto targetReq : getLinkage()->targets)
{
auto targetProgram = m_globalAndEntryPointsComponentType->getTargetProgram(targetReq);
targetProgram->getOrCreateLayout(getSink());
targetProgram->getOrCreateIRModuleForLayout(getSink());
}
if (getSink()->getErrorCount() != 0)
return SLANG_FAIL;
return SLANG_OK;
}
int FrontEndCompileRequest::addTranslationUnit(SourceLanguage language, Name* moduleName)
{
RefPtr<TranslationUnitRequest> translationUnit = new TranslationUnitRequest(this);
translationUnit->compileRequest = this;
translationUnit->sourceLanguage = SourceLanguage(language);
translationUnit->setModuleName(moduleName);
return addTranslationUnit(translationUnit);
}
int FrontEndCompileRequest::addTranslationUnit(TranslationUnitRequest* translationUnit)
{
Index result = translationUnits.getCount();
translationUnits.add(translationUnit);
return (int)result;
}
void FrontEndCompileRequest::addTranslationUnitSourceArtifact(
int translationUnitIndex,
IArtifact* sourceArtifact)
{
auto translationUnit = translationUnits[translationUnitIndex];
// Add the source file
translationUnit->addSourceArtifact(sourceArtifact);
if (!translationUnit->moduleName)
{
translationUnit->setModuleName(
getNamePool()->getName(Path::getFileNameWithoutExt(sourceArtifact->getName())));
}
if (translationUnit->module->getFilePath() == nullptr)
translationUnit->module->setPathInfo(PathInfo::makePath(sourceArtifact->getName()));
}
void FrontEndCompileRequest::addTranslationUnitSourceBlob(
int translationUnitIndex,
String const& path,
ISlangBlob* sourceBlob)
{
auto translationUnit = translationUnits[translationUnitIndex];
auto sourceDesc =
ArtifactDescUtil::makeDescForSourceLanguage(asExternal(translationUnit->sourceLanguage));
auto artifact = ArtifactUtil::createArtifact(sourceDesc, path.getBuffer());
artifact->addRepresentationUnknown(sourceBlob);
addTranslationUnitSourceArtifact(translationUnitIndex, artifact);
}
void FrontEndCompileRequest::addTranslationUnitSourceFile(
int translationUnitIndex,
String const& path)
{
// TODO: We need to consider whether a relative `path` should cause
// us to look things up using the registered search paths.
//
// This behavior wouldn't make sense for command-line invocations
// of `slangc`, but at least one API user wondered by the search
// paths were not taken into account by this function.
//
auto fileSystemExt = getLinkage()->getFileSystemExt();
auto translationUnit = getTranslationUnit(translationUnitIndex);
auto sourceDesc =
ArtifactDescUtil::makeDescForSourceLanguage(asExternal(translationUnit->sourceLanguage));
auto sourceArtifact = ArtifactUtil::createArtifact(sourceDesc, path.getBuffer());
auto extRep = new ExtFileArtifactRepresentation(path.getUnownedSlice(), fileSystemExt);
sourceArtifact->addRepresentation(extRep);
SlangResult existsRes = SLANG_OK;
// If we require caching, we demand it's loaded here.
//
// In practice this probably means repro capture is enabled. So we want to
// load the blob such that it's in the cache, even if it doesn't actually
// have to be loaded for the compilation.
if (getLinkage()->m_requireCacheFileSystem)
{
ComPtr<ISlangBlob> blob;
// If we can load the blob, then it exists
existsRes = sourceArtifact->loadBlob(ArtifactKeep::Yes, blob.writeRef());
}
else
{
existsRes = sourceArtifact->exists() ? SLANG_OK : SLANG_E_NOT_FOUND;
}
if (SLANG_FAILED(existsRes))
{
// Emit a diagnostic!
getSink()->diagnose(SourceLoc(), Diagnostics::cannotOpenFile, path);
return;
}
addTranslationUnitSourceArtifact(translationUnitIndex, sourceArtifact);
}
int FrontEndCompileRequest::addEntryPoint(
int translationUnitIndex,
String const& name,
Profile entryPointProfile)
{
auto translationUnitReq = translationUnits[translationUnitIndex];
Index result = m_entryPointReqs.getCount();
RefPtr<FrontEndEntryPointRequest> entryPointReq = new FrontEndEntryPointRequest(
this,
translationUnitIndex,
getNamePool()->getName(name),
entryPointProfile);
m_entryPointReqs.add(entryPointReq);
// translationUnitReq->entryPoints.add(entryPointReq);
return int(result);
}
int EndToEndCompileRequest::addEntryPoint(
int translationUnitIndex,
String const& name,
Profile entryPointProfile,
List<String> const& genericTypeNames)
{
getFrontEndReq()->addEntryPoint(translationUnitIndex, name, entryPointProfile);
EntryPointInfo entryPointInfo;
for (auto typeName : genericTypeNames)
entryPointInfo.specializationArgStrings.add(typeName);
Index result = m_entryPoints.getCount();
m_entryPoints.add(_Move(entryPointInfo));
return (int)result;
}
} // namespace Slang
|