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
|
#ifndef SLANG_COMPILER_OPTIONS_H
#define SLANG_COMPILER_OPTIONS_H
#include "../core/slang-basic.h"
#include "../core/slang-crypto.h"
#include "slang-generated-capability-defs.h"
#include "slang-profile.h"
#include "slang.h"
namespace Slang
{
using slang::CompilerOptionName;
using slang::CompilerOptionValueKind;
enum MatrixLayoutMode : SlangMatrixLayoutModeIntegral;
enum class LineDirectiveMode : SlangLineDirectiveModeIntegral;
enum class FloatingPointMode : SlangFloatingPointModeIntegral;
enum class FloatingPointDenormalMode : SlangFpDenormalModeIntegral;
enum class OptimizationLevel : SlangOptimizationLevelIntegral;
enum class DebugInfoLevel : SlangDebugInfoLevelIntegral;
enum class CodeGenTarget : SlangCompileTargetIntegral;
struct CompilerOptionValue
{
CompilerOptionValueKind kind = CompilerOptionValueKind::Int;
int intValue = 0;
int intValue2 = 0;
String stringValue;
String stringValue2;
template<typename T>
static CompilerOptionValue fromEnum(T val)
{
static_assert(std::is_enum<T>::value);
CompilerOptionValue value;
value.intValue = (int)val;
value.kind = CompilerOptionValueKind::Int;
return value;
}
static CompilerOptionValue fromInt(int val)
{
CompilerOptionValue value;
value.intValue = val;
value.kind = CompilerOptionValueKind::Int;
return value;
}
static CompilerOptionValue fromInt2(int val, int val2)
{
CompilerOptionValue value;
value.intValue = val;
value.intValue2 = val2;
value.kind = CompilerOptionValueKind::Int;
return value;
}
void unpackInt3(uint8_t& v0, int& v1, int& v2)
{
v0 = intValue >> 24;
v1 = intValue & 0xFFFFFF;
v2 = intValue2;
}
static CompilerOptionValue fromInt3(uint8_t v0, int v1, int v2)
{
CompilerOptionValue value;
value.intValue = (v0 << 24) + (v1 & 0xFFFFFF);
value.intValue2 = v2;
value.kind = CompilerOptionValueKind::Int;
return value;
}
static CompilerOptionValue fromString(String val)
{
CompilerOptionValue value;
value.stringValue = val;
value.kind = CompilerOptionValueKind::String;
return value;
}
};
struct SerializedOptionsData
{
List<slang::CompilerOptionEntry> entries;
List<String> stringPool;
};
class Session;
struct CompilerOptionSet
{
void load(uint32_t count, slang::CompilerOptionEntry* entries);
void buildHash(DigestBuilder<SHA1>& builder);
static bool allowDuplicate(CompilerOptionName name);
void writeCommandLineArgs(Session* globalSession, StringBuilder& sb);
OrderedDictionary<CompilerOptionName, List<CompilerOptionValue>> options;
bool hasOption(CompilerOptionName name) { return options.containsKey(name); }
void set(CompilerOptionName name, CompilerOptionValue value)
{
if (auto v = options.tryGetValue(name))
{
v->clear();
v->add(value);
return;
}
options[name] = List<CompilerOptionValue>{value};
}
void set(CompilerOptionName name, const List<CompilerOptionValue>& value)
{
if (auto v = options.tryGetValue(name))
{
v->clear();
v->addRange(value);
return;
}
options[name] = List<CompilerOptionValue>{value};
}
void add(CompilerOptionName name, CompilerOptionValue value)
{
if (auto v = options.tryGetValue(name))
{
v->add(value);
return;
}
options[name] = List<CompilerOptionValue>{value};
}
void add(
CompilerOptionName name,
const List<CompilerOptionValue>& value,
bool replaceDuplicate = true)
{
if (auto v = options.tryGetValue(name))
{
for (auto element : value)
{
Index index = v->findFirstIndex(
[&](const CompilerOptionValue& existingVal)
{
if (existingVal.kind == CompilerOptionValueKind::Int)
return existingVal.intValue == element.intValue;
else
return existingVal.stringValue == element.stringValue;
});
if (index != -1)
{
if (replaceDuplicate)
{
(*v)[index].intValue2 = element.intValue;
(*v)[index].stringValue2 = element.stringValue2;
}
}
else
{
v->add(element);
}
}
return;
}
options[name] = List<CompilerOptionValue>{value};
}
// Copy settings from other, and replace the current setting.
void overrideWith(const CompilerOptionSet& other)
{
for (auto& kv : other.options)
{
if (allowDuplicate(kv.key))
add(kv.key, kv.value, true);
else
set(kv.key, kv.value);
}
}
// Copy settings from other, but do not replace the current setting
void inheritFrom(const CompilerOptionSet& other)
{
for (auto& kv : other.options)
{
if (allowDuplicate(kv.key))
add(kv.key, kv.value, false);
else
{
if (options.containsKey(kv.key))
continue;
set(kv.key, kv.value);
}
}
}
void add(CompilerOptionName name, int intVal)
{
add(name, CompilerOptionValue::fromInt(intVal));
}
void add(CompilerOptionName name, int intVal, int intVal2)
{
add(name, CompilerOptionValue::fromInt2(intVal, intVal2));
}
void add(CompilerOptionName name, uint8_t intVal, int intVal2, int intVal3)
{
add(name, CompilerOptionValue::fromInt3(intVal, intVal2, intVal3));
}
void add(CompilerOptionName name, String stringVal)
{
add(name, CompilerOptionValue::fromString(stringVal));
}
void add(CompilerOptionName name, UnownedStringSlice stringVal)
{
add(name, CompilerOptionValue::fromString(stringVal));
}
void add(CompilerOptionName name, bool boolVal)
{
add(name, CompilerOptionValue::fromInt(boolVal ? 1 : 0));
}
template<typename EnumType>
void add(CompilerOptionName name, EnumType enumVal)
{
static_assert(std::is_enum<EnumType>::value);
add(name, (int)enumVal);
}
void set(CompilerOptionName name, int intVal)
{
set(name, CompilerOptionValue::fromInt(intVal));
}
void set(CompilerOptionName name, int intVal1, int intVal2)
{
set(name, CompilerOptionValue::fromInt2(intVal1, intVal2));
}
void set(CompilerOptionName name, uint8_t intVal1, int intVal2, int intVal3)
{
set(name, CompilerOptionValue::fromInt3(intVal1, intVal2, intVal3));
}
void set(CompilerOptionName name, String stringVal)
{
set(name, CompilerOptionValue::fromString(stringVal));
}
void set(CompilerOptionName name, bool boolVal)
{
set(name, CompilerOptionValue::fromInt(boolVal ? 1 : 0));
}
template<typename EnumType>
void set(CompilerOptionName name, EnumType enumVal)
{
static_assert(std::is_enum<EnumType>::value);
set(name, (int)enumVal);
}
static CompilerOptionValue getDefault(CompilerOptionName name);
bool getBoolOption(CompilerOptionName name)
{
if (auto result = options.tryGetValue(name))
{
SLANG_ASSERT(
result->getCount() != 0 && (*result)[0].kind == CompilerOptionValueKind::Int);
return result->getCount() != 0 && (*result)[0].intValue != 0;
}
return getDefault(name).intValue != 0;
}
int getIntOption(CompilerOptionName name)
{
if (auto result = options.tryGetValue(name))
{
SLANG_ASSERT(
result->getCount() != 0 && (*result)[0].kind == CompilerOptionValueKind::Int);
return (*result)[0].intValue;
}
return getDefault(name).intValue;
}
String getStringOption(CompilerOptionName name)
{
if (auto result = options.tryGetValue(name))
{
SLANG_ASSERT(
result->getCount() != 0 && (*result)[0].kind == CompilerOptionValueKind::String);
return (*result)[0].stringValue;
}
return getDefault(name).stringValue;
}
template<typename EnumType>
EnumType getEnumOption(CompilerOptionName name)
{
static_assert(std::is_enum<EnumType>::value);
return (EnumType)getIntOption(name);
}
ArrayView<CompilerOptionValue> getArray(CompilerOptionName name)
{
if (auto result = options.tryGetValue(name))
{
return result->getArrayView();
}
return ArrayView<CompilerOptionValue>();
}
CodeGenTarget getTarget() { return getEnumOption<CodeGenTarget>(CompilerOptionName::Target); }
SlangTargetFlags getTargetFlags();
void setTargetFlags(SlangTargetFlags flags);
void addTargetFlags(SlangTargetFlags flags);
MatrixLayoutMode getMatrixLayoutMode();
void setMatrixLayoutMode(MatrixLayoutMode mode);
ProfileVersion getProfileVersion();
Profile getProfile();
void setProfile(Profile profile);
void setProfileVersion(ProfileVersion version);
void addCapabilityAtom(CapabilityName cap);
void addPreprocessorDefine(String name, String value)
{
CompilerOptionValue v;
v.stringValue = name;
v.stringValue2 = value;
v.kind = CompilerOptionValueKind::String;
add(CompilerOptionName::MacroDefine, v);
}
void addSearchPath(String path) { add(CompilerOptionName::Include, String(path)); }
bool shouldEmitSPIRVDirectly()
{
SlangEmitSpirvMethod emitSpvMethod =
getEnumOption<SlangEmitSpirvMethod>(CompilerOptionName::EmitSpirvMethod);
return (emitSpvMethod != SlangEmitSpirvMethod::SLANG_EMIT_SPIRV_VIA_GLSL);
}
bool shouldUseScalarLayout()
{
return getBoolOption(CompilerOptionName::GLSLForceScalarLayout);
}
bool shouldUseDXLayout() { return getBoolOption(CompilerOptionName::ForceDXLayout); }
bool shouldUseCLayout() { return getBoolOption(CompilerOptionName::ForceCLayout); }
bool shouldDumpIntermediates() { return getBoolOption(CompilerOptionName::DumpIntermediates); }
bool shouldDumpIR() { return getBoolOption(CompilerOptionName::DumpIr); }
bool shouldObfuscateCode() { return getBoolOption(CompilerOptionName::Obfuscate); }
bool shouldPerformMinimumOptimizations()
{
return getBoolOption(CompilerOptionName::MinimumSlangOptimization);
}
bool shouldRunNonEssentialValidation()
{
return !getBoolOption(CompilerOptionName::DisableNonEssentialValidations);
}
bool shouldHaveSourceMap() { return !getBoolOption(CompilerOptionName::DisableSourceMap); }
bool shouldEmitSeparateDebugInfo()
{
return getBoolOption(CompilerOptionName::EmitSeparateDebug);
}
FloatingPointMode getFloatingPointMode()
{
return getEnumOption<FloatingPointMode>(CompilerOptionName::FloatingPointMode);
}
FloatingPointDenormalMode getDenormalModeFp16()
{
if (!hasOption(CompilerOptionName::DenormalModeFp16))
{
return (FloatingPointDenormalMode)SLANG_FP_DENORM_MODE_ANY;
}
return getEnumOption<FloatingPointDenormalMode>(CompilerOptionName::DenormalModeFp16);
}
FloatingPointDenormalMode getDenormalModeFp32()
{
if (!hasOption(CompilerOptionName::DenormalModeFp32))
{
return (FloatingPointDenormalMode)SLANG_FP_DENORM_MODE_ANY;
}
return getEnumOption<FloatingPointDenormalMode>(CompilerOptionName::DenormalModeFp32);
}
FloatingPointDenormalMode getDenormalModeFp64()
{
if (!hasOption(CompilerOptionName::DenormalModeFp64))
{
return (FloatingPointDenormalMode)SLANG_FP_DENORM_MODE_ANY;
}
return getEnumOption<FloatingPointDenormalMode>(CompilerOptionName::DenormalModeFp64);
}
LineDirectiveMode getLineDirectiveMode()
{
return getEnumOption<LineDirectiveMode>(CompilerOptionName::LineDirectiveMode);
}
OptimizationLevel getOptimizationLevel()
{
return getEnumOption<OptimizationLevel>(CompilerOptionName::Optimization);
}
DebugInfoLevel getDebugInfoLevel()
{
return getEnumOption<DebugInfoLevel>(CompilerOptionName::DebugInformation);
}
SlangLanguageVersion getLanguageVersion()
{
if (!hasOption(CompilerOptionName::LanguageVersion))
{
return SLANG_LANGAUGE_VERSION_DEFAULT;
}
return (SlangLanguageVersion)getIntOption(CompilerOptionName::LanguageVersion);
}
List<String> getDownstreamArgs(String downstreamToolName);
void serialize(SerializedOptionsData* outData);
};
class DiagnosticSink;
void applySettingsToDiagnosticSink(
DiagnosticSink* targetSink,
DiagnosticSink* outputSink,
CompilerOptionSet& options);
} // namespace Slang
#endif
|