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
|
// slang-module.h
#pragma once
//
// This file provides the `Module` class, which is
// central to many parts of the Slang compiler codebase.
//
#include "../core/slang-string-util.h"
#include "slang-ast-builder.h"
#include "slang-entry-point.h"
#include "slang-linkable.h"
namespace Slang
{
/// A module of code that has been compiled through the front-end
///
/// A module comprises all the code from one translation unit (which
/// may span multiple Slang source files), and provides access
/// to both the AST and IR representations of that code.
///
/// This class serves multiple important roles in the Slang compiler:
///
/// * this class implements the `slang::IModule` interface from
/// the public Slang API.
///
/// * this class is the primary output of front-end compilation,
/// and its data is what gets stored/loaded using the `.slang-module`
/// file format.
///
/// * The checked AST in a `Module` provides all of the information
/// that the front-end uses when checking code that `import`s
/// that module (e.g., the names and signatures of functions defined
/// in the module).
///
/// * The checked AST is also used to service queries through the
/// Slang reflection API.
///
/// * the `Module` class is a subclass of `ComponentType` and thus
/// is a unit of linkable code. One or more modules (and other
/// linkable objects) can be combined to form a linked program.
///
/// * The Slang IR in a `Module` provides all of the information that
/// the back-end uses when generating code for a program/binary
/// that links this module (or any of its entry points).
///
class Module : public ComponentType, public slang::IModule
{
typedef ComponentType Super;
public:
SLANG_REF_OBJECT_IUNKNOWN_ALL
ISlangUnknown* getInterface(const Guid& guid);
// Forward `IComponentType` methods
SLANG_NO_THROW slang::ISession* SLANG_MCALL getSession() SLANG_OVERRIDE
{
return Super::getSession();
}
SLANG_NO_THROW slang::ProgramLayout* SLANG_MCALL
getLayout(SlangInt targetIndex, slang::IBlob** outDiagnostics) SLANG_OVERRIDE
{
return Super::getLayout(targetIndex, outDiagnostics);
}
SLANG_NO_THROW SlangResult SLANG_MCALL getEntryPointCode(
SlangInt entryPointIndex,
SlangInt targetIndex,
slang::IBlob** outCode,
slang::IBlob** outDiagnostics) SLANG_OVERRIDE
{
return Super::getEntryPointCode(entryPointIndex, targetIndex, outCode, outDiagnostics);
}
SLANG_NO_THROW SlangResult SLANG_MCALL getTargetCode(
SlangInt targetIndex,
slang::IBlob** outCode,
slang::IBlob** outDiagnostics) SLANG_OVERRIDE
{
return Super::getTargetCode(targetIndex, outCode, outDiagnostics);
}
SLANG_NO_THROW SlangResult SLANG_MCALL getResultAsFileSystem(
SlangInt entryPointIndex,
SlangInt targetIndex,
ISlangMutableFileSystem** outFileSystem) SLANG_OVERRIDE
{
return Super::getResultAsFileSystem(entryPointIndex, targetIndex, outFileSystem);
}
SLANG_NO_THROW SlangResult SLANG_MCALL specialize(
slang::SpecializationArg const* specializationArgs,
SlangInt specializationArgCount,
slang::IComponentType** outSpecializedComponentType,
ISlangBlob** outDiagnostics) SLANG_OVERRIDE
{
return Super::specialize(
specializationArgs,
specializationArgCount,
outSpecializedComponentType,
outDiagnostics);
}
SLANG_NO_THROW SlangResult SLANG_MCALL
renameEntryPoint(const char* newName, slang::IComponentType** outEntryPoint) SLANG_OVERRIDE
{
return Super::renameEntryPoint(newName, outEntryPoint);
}
SLANG_NO_THROW SlangResult SLANG_MCALL
link(slang::IComponentType** outLinkedComponentType, ISlangBlob** outDiagnostics) SLANG_OVERRIDE
{
return Super::link(outLinkedComponentType, outDiagnostics);
}
SLANG_NO_THROW SlangResult SLANG_MCALL getEntryPointHostCallable(
int entryPointIndex,
int targetIndex,
ISlangSharedLibrary** outSharedLibrary,
slang::IBlob** outDiagnostics) SLANG_OVERRIDE
{
return Super::getEntryPointHostCallable(
entryPointIndex,
targetIndex,
outSharedLibrary,
outDiagnostics);
}
SLANG_NO_THROW SlangResult SLANG_MCALL
findEntryPointByName(char const* name, slang::IEntryPoint** outEntryPoint) SLANG_OVERRIDE
{
if (outEntryPoint == nullptr)
{
return SLANG_E_INVALID_ARG;
}
SLANG_AST_BUILDER_RAII(m_astBuilder);
ComPtr<slang::IEntryPoint> entryPoint(findEntryPointByName(UnownedStringSlice(name)));
if ((!entryPoint))
return SLANG_FAIL;
*outEntryPoint = entryPoint.detach();
return SLANG_OK;
}
virtual SLANG_NO_THROW SlangResult SLANG_MCALL findAndCheckEntryPoint(
char const* name,
SlangStage stage,
slang::IEntryPoint** outEntryPoint,
ISlangBlob** outDiagnostics) override
{
if (outEntryPoint == nullptr)
{
return SLANG_E_INVALID_ARG;
}
ComPtr<slang::IEntryPoint> entryPoint(
findAndCheckEntryPoint(UnownedStringSlice(name), stage, outDiagnostics));
if ((!entryPoint))
return SLANG_FAIL;
*outEntryPoint = entryPoint.detach();
return SLANG_OK;
}
virtual SLANG_NO_THROW SlangInt32 SLANG_MCALL getDefinedEntryPointCount() override
{
return (SlangInt32)m_entryPoints.getCount();
}
virtual SLANG_NO_THROW SlangResult SLANG_MCALL
getDefinedEntryPoint(SlangInt32 index, slang::IEntryPoint** outEntryPoint) override
{
if (index < 0 || index >= m_entryPoints.getCount())
return SLANG_E_INVALID_ARG;
if (outEntryPoint == nullptr)
{
return SLANG_E_INVALID_ARG;
}
ComPtr<slang::IEntryPoint> entryPoint(m_entryPoints[index].Ptr());
*outEntryPoint = entryPoint.detach();
return SLANG_OK;
}
virtual SLANG_NO_THROW SlangResult SLANG_MCALL linkWithOptions(
slang::IComponentType** outLinkedComponentType,
uint32_t count,
slang::CompilerOptionEntry* entries,
ISlangBlob** outDiagnostics) override
{
return Super::linkWithOptions(outLinkedComponentType, count, entries, outDiagnostics);
}
//
SLANG_NO_THROW void SLANG_MCALL getEntryPointHash(
SlangInt entryPointIndex,
SlangInt targetIndex,
slang::IBlob** outHash) SLANG_OVERRIDE
{
return Super::getEntryPointHash(entryPointIndex, targetIndex, outHash);
}
SLANG_NO_THROW SlangResult SLANG_MCALL getEntryPointMetadata(
SlangInt entryPointIndex,
SlangInt targetIndex,
slang::IMetadata** outMetadata,
slang::IBlob** outDiagnostics) SLANG_OVERRIDE
{
return Super::getEntryPointMetadata(
entryPointIndex,
targetIndex,
outMetadata,
outDiagnostics);
}
SLANG_NO_THROW SlangResult SLANG_MCALL getTargetMetadata(
SlangInt targetIndex,
slang::IMetadata** outMetadata,
slang::IBlob** outDiagnostics) SLANG_OVERRIDE
{
return Super::getTargetMetadata(targetIndex, outMetadata, outDiagnostics);
}
SLANG_NO_THROW SlangResult SLANG_MCALL getEntryPointCompileResult(
SlangInt entryPointIndex,
SlangInt targetIndex,
slang::ICompileResult** outCompileResult,
slang::IBlob** outDiagnostics) SLANG_OVERRIDE
{
return Super::getEntryPointCompileResult(
entryPointIndex,
targetIndex,
outCompileResult,
outDiagnostics);
}
SLANG_NO_THROW SlangResult SLANG_MCALL getTargetCompileResult(
SlangInt targetIndex,
slang::ICompileResult** outCompileResult,
slang::IBlob** outDiagnostics) SLANG_OVERRIDE
{
return Super::getTargetCompileResult(targetIndex, outCompileResult, outDiagnostics);
}
/// Get a serialized representation of the checked module.
virtual SLANG_NO_THROW SlangResult SLANG_MCALL
serialize(ISlangBlob** outSerializedBlob) override;
/// Write the serialized representation of this module to a file.
virtual SLANG_NO_THROW SlangResult SLANG_MCALL writeToFile(char const* fileName) override;
/// Get the name of the module.
virtual SLANG_NO_THROW const char* SLANG_MCALL getName() override;
/// Get the path of the module.
virtual SLANG_NO_THROW const char* SLANG_MCALL getFilePath() override;
/// Get the unique identity of the module.
virtual SLANG_NO_THROW const char* SLANG_MCALL getUniqueIdentity() override;
/// Get the number of dependency files that this module depends on.
/// This includes both the explicit source files, as well as any
/// additional files that were transitively referenced (e.g., via
/// a `#include` directive).
virtual SLANG_NO_THROW SlangInt32 SLANG_MCALL getDependencyFileCount() override;
/// Get the path to a file this module depends on.
virtual SLANG_NO_THROW char const* SLANG_MCALL getDependencyFilePath(SlangInt32 index) override;
// IModulePrecompileService_Experimental
/// Precompile TU to target language
virtual SLANG_NO_THROW SlangResult SLANG_MCALL
precompileForTarget(SlangCompileTarget target, slang::IBlob** outDiagnostics) override;
virtual SLANG_NO_THROW SlangResult SLANG_MCALL getPrecompiledTargetCode(
SlangCompileTarget target,
slang::IBlob** outCode,
slang::IBlob** outDiagnostics = nullptr) override;
virtual SLANG_NO_THROW SlangInt SLANG_MCALL getModuleDependencyCount() SLANG_OVERRIDE;
virtual SLANG_NO_THROW SlangResult SLANG_MCALL getModuleDependency(
SlangInt dependencyIndex,
slang::IModule** outModule,
slang::IBlob** outDiagnostics = nullptr) SLANG_OVERRIDE;
virtual void buildHash(DigestBuilder<SHA1>& builder) SLANG_OVERRIDE;
virtual SLANG_NO_THROW slang::DeclReflection* SLANG_MCALL getModuleReflection() SLANG_OVERRIDE;
void setDigest(SHA1::Digest const& digest) { m_digest = digest; }
SHA1::Digest computeDigest();
/// Create a module (initially empty).
Module(Linkage* linkage, ASTBuilder* astBuilder = nullptr);
/// Get the AST for the module (if it has been parsed)
ModuleDecl* getModuleDecl() { return m_moduleDecl; }
/// The the IR for the module (if it has been generated)
IRModule* getIRModule() { return m_irModule; }
/// Get the list of other modules this module depends on
List<Module*> const& getModuleDependencyList()
{
return m_moduleDependencyList.getModuleList();
}
/// Get the list of files this module depends on
List<SourceFile*> const& getFileDependencyList() { return m_fileDependencyList.getFileList(); }
/// Register a module that this module depends on
void addModuleDependency(Module* module);
/// Register a source file that this module depends on
void addFileDependency(SourceFile* sourceFile);
void clearFileDependency() { m_fileDependencyList.clear(); }
/// Set the AST for this module.
///
/// This should only be called once, during creation of the module.
///
void setModuleDecl(ModuleDecl* moduleDecl); // { m_moduleDecl = moduleDecl; }
void setName(String name);
void setName(Name* name) { m_name = name; }
Name* getNameObj() { return m_name; }
void setPathInfo(PathInfo pathInfo) { m_pathInfo = pathInfo; }
/// Set the IR for this module.
///
/// This should only be called once, during creation of the module.
///
void setIRModule(IRModule* irModule) { m_irModule = irModule; }
Index getEntryPointCount() SLANG_OVERRIDE { return 0; }
RefPtr<EntryPoint> getEntryPoint(Index index) SLANG_OVERRIDE
{
SLANG_UNUSED(index);
return nullptr;
}
String getEntryPointMangledName(Index index) SLANG_OVERRIDE
{
SLANG_UNUSED(index);
return String();
}
String getEntryPointNameOverride(Index index) SLANG_OVERRIDE
{
SLANG_UNUSED(index);
return String();
}
Index getShaderParamCount() SLANG_OVERRIDE { return m_shaderParams.getCount(); }
ShaderParamInfo getShaderParam(Index index) SLANG_OVERRIDE { return m_shaderParams[index]; }
SLANG_NO_THROW Index SLANG_MCALL getSpecializationParamCount() SLANG_OVERRIDE
{
return m_specializationParams.getCount();
}
SpecializationParam const& getSpecializationParam(Index index) SLANG_OVERRIDE
{
return m_specializationParams[index];
}
Index getRequirementCount() SLANG_OVERRIDE;
RefPtr<ComponentType> getRequirement(Index index) SLANG_OVERRIDE;
List<Module*> const& getModuleDependencies() SLANG_OVERRIDE
{
return m_moduleDependencyList.getModuleList();
}
List<SourceFile*> const& getFileDependencies() SLANG_OVERRIDE
{
return m_fileDependencyList.getFileList();
}
/// Given a mangled name finds the exported NodeBase associated with this module.
/// If not found returns nullptr.
Decl* findExportedDeclByMangledName(const UnownedStringSlice& mangledName);
/// Ensure that the any accelerator(s) used for `findExportedDeclByMangledName`
/// have already been built.
///
void ensureExportLookupAcceleratorBuilt();
Count getExportedDeclCount();
Decl* getExportedDecl(Index index);
UnownedStringSlice getExportedDeclMangledName(Index index);
/// Get the ASTBuilder
ASTBuilder* getASTBuilder() { return m_astBuilder; }
/// Collect information on the shader parameters of the module.
///
/// This method should only be called once, after the core
/// structured of the module (its AST and IR) have been created,
/// and before any of the `ComponentType` APIs are used.
///
/// TODO: We might eventually consider a non-stateful approach
/// to constructing a `Module`.
///
void _collectShaderParams();
void _discoverEntryPoints(DiagnosticSink* sink, const List<RefPtr<TargetRequest>>& targets);
void _discoverEntryPointsImpl(
ContainerDecl* containerDecl,
DiagnosticSink* sink,
const List<RefPtr<TargetRequest>>& targets);
class ModuleSpecializationInfo : public SpecializationInfo
{
public:
struct GenericArgInfo
{
Decl* paramDecl = nullptr;
Val* argVal = nullptr;
};
List<GenericArgInfo> genericArgs;
List<ExpandedSpecializationArg> existentialArgs;
};
RefPtr<EntryPoint> findEntryPointByName(UnownedStringSlice const& name);
RefPtr<EntryPoint> findAndCheckEntryPoint(
UnownedStringSlice const& name,
SlangStage stage,
ISlangBlob** outDiagnostics);
List<RefPtr<EntryPoint>>& getEntryPoints() { return m_entryPoints; }
void _addEntryPoint(EntryPoint* entryPoint);
void _processFindDeclsExportSymbolsRec(Decl* decl);
// Gets the files that has been included into the module.
Dictionary<SourceFile*, FileDecl*>& getIncludedSourceFileMap()
{
return m_mapSourceFileToFileDecl;
}
protected:
void acceptVisitor(ComponentTypeVisitor* visitor, SpecializationInfo* specializationInfo)
SLANG_OVERRIDE;
RefPtr<SpecializationInfo> _validateSpecializationArgsImpl(
SpecializationArg const* args,
Index argCount,
Index& outConsumedArgCount,
DiagnosticSink* sink) SLANG_OVERRIDE;
private:
Name* m_name = nullptr;
PathInfo m_pathInfo;
// The AST for the module
ModuleDecl* m_moduleDecl = nullptr;
// The IR for the module
RefPtr<IRModule> m_irModule = nullptr;
List<ShaderParamInfo> m_shaderParams;
SpecializationParams m_specializationParams;
List<Module*> m_requirements;
// A digest that uniquely identifies the contents of the module.
SHA1::Digest m_digest;
// List of modules this module depends on
ModuleDependencyList m_moduleDependencyList;
// List of source files this module depends on
FileDependencyList m_fileDependencyList;
// Entry points that were defined in this module
//
// Note: the entry point defined in the module are *not*
// part of the memory image/layout of the module when
// it is considered as an IComponentType. This can be
// a bit confusing, but if all the entry points in the
// module were automatically linked into the component
// type, we'd need a way to access just the global
// scope of the module without the entry points, in
// case we wanted to link a single entry point against
// the global scope. The `Module` type provides exactly
// that "module without its entry points" unit of
// granularity for linking.
//
// This list only exists for lookup purposes, so that
// the user can find an existing entry-point function
// that was defined as part of the module.
//
List<RefPtr<EntryPoint>> m_entryPoints;
// The builder that owns all of the AST nodes from parsing the source of
// this module.
RefPtr<ASTBuilder> m_astBuilder;
// Holds map of exported mangled names to symbols. m_mangledExportPool maps names to indices,
// and m_mangledExportSymbols holds the NodeBase* values for each index.
StringSlicePool m_mangledExportPool;
List<Decl*> m_mangledExportSymbols;
// Source files that have been pulled into the module with `__include`.
Dictionary<SourceFile*, FileDecl*> m_mapSourceFileToFileDecl;
public:
SLANG_NO_THROW SlangResult SLANG_MCALL disassemble(slang::IBlob** outDisassembledBlob) override
{
if (!outDisassembledBlob)
return SLANG_E_INVALID_ARG;
String disassembly;
this->getIRModule()->getModuleInst()->dump(disassembly);
auto blob = StringUtil::createStringBlob(disassembly);
*outDisassembledBlob = blob.detach();
return SLANG_OK;
}
};
} // namespace Slang
|