summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-compiler-options.cpp
blob: 84a22535ac862c6d0ae7ab3cef11c093db3d6106 (plain)
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
#include "slang-compiler-options.h"

#include "slang-compiler.h"

namespace Slang
{
void CompilerOptionSet::load(uint32_t count, slang::CompilerOptionEntry* entries)
{
    for (uint32_t i = 0; i < count; i++)
    {
        CompilerOptionValue value;
        value.kind = entries[i].value.kind;
        value.intValue = entries[i].value.intValue0;
        value.intValue2 = entries[i].value.intValue1;
        if (value.kind == CompilerOptionValueKind::String)
        {
            value.stringValue = entries[i].value.stringValue0;
            value.stringValue2 = entries[i].value.stringValue1;
        }
        add(entries[i].name, value);

        // When we see option EmitSpirvDirectly or EmitSpirvViaGLSL, we will need to
        // translate them to EmitSpirvMethod.
        if (entries[i].name == slang::CompilerOptionName::EmitSpirvDirectly && value.intValue)
        {
            set(slang::CompilerOptionName::EmitSpirvMethod, SLANG_EMIT_SPIRV_DIRECTLY);
        }
        else if (entries[i].name == slang::CompilerOptionName::EmitSpirvViaGLSL && value.intValue)
        {
            SlangEmitSpirvMethod current =
                getEnumOption<SlangEmitSpirvMethod>(slang::CompilerOptionName::EmitSpirvMethod);
            if (current != SLANG_EMIT_SPIRV_DEFAULT)
            {
                set(CompilerOptionName::EmitSpirvMethod, SLANG_EMIT_SPIRV_VIA_GLSL);
            }
        }
    }
}

void CompilerOptionSet::writeCommandLineArgs(Session* globalSession, StringBuilder& sb)
{
    for (auto& option : options)
    {
        auto optionInfoIndex = globalSession->m_commandOptions.findOptionByUserValue(
            CommandOptions::UserValue(option.key));
        if (optionInfoIndex == -1)
            continue;
        auto optionInfo = globalSession->m_commandOptions.getOptionAt(optionInfoIndex);
        auto nameCommaIndex = optionInfo.names.indexOf(',');
        if (nameCommaIndex == -1)
            nameCommaIndex = optionInfo.names.getLength();
        auto name = optionInfo.names.head(nameCommaIndex);
        switch (option.key)
        {
        case CompilerOptionName::Capability:
            for (auto v : option.value)
            {
                sb << " " << optionInfo.names << " " << v.stringValue;
            }
            break;
        case CompilerOptionName::Include:
            for (auto v : option.value)
            {
                sb << " -I \"" << v.stringValue << "\"";
            }
            break;
        case CompilerOptionName::MacroDefine:
            for (auto v : option.value)
            {
                sb << " -D" << v.stringValue;
                if (v.stringValue2.getLength())
                    sb << "=" << v.stringValue2;
            }
            break;
        case CompilerOptionName::VulkanBindShift: // intValue0 (higher 8 bits): kind;
                                                  // intValue0(higher bits): set; intValue1:
                                                  // shift
            for (auto v : option.value)
            {
                uint8_t kind;
                int set, shift;
                v.unpackInt3(kind, set, shift);
                switch ((HLSLToVulkanLayoutOptions::Kind)(kind))
                {
                case HLSLToVulkanLayoutOptions::Kind::UnorderedAccess:
                    sb << " -fvk-u-shift";
                    break;
                case HLSLToVulkanLayoutOptions::Kind::Sampler:
                    sb << " -fvk-s-shift";
                    break;
                case HLSLToVulkanLayoutOptions::Kind::ShaderResource:
                    sb << " -fvk-t-shift";
                    break;
                case HLSLToVulkanLayoutOptions::Kind::ConstantBuffer:
                    sb << " -fvk-b-shift";
                    break;
                default:
                    continue;
                }
                sb << " " << shift << " " << set;
            }
            break;
        case CompilerOptionName::VulkanBindShiftAll: // intValue0: set; intValue1: shift
            for (auto v : option.value)
            {
                sb << " -fvk-all-shift " << v.intValue2 << " " << v.intValue;
            }
            break;
        case CompilerOptionName::VulkanBindGlobals: // intValue0: index; intValue1: set
            for (auto v : option.value)
            {
                sb << " " << name << v.intValue << " " << v.intValue2;
            }
            break;
        case CompilerOptionName::Optimization:
            for (auto v : option.value)
            {
                sb << " -O" << v.intValue;
            }
            break;
        case CompilerOptionName::DownstreamArgs:
            for (auto v : option.value)
            {
                List<UnownedStringSlice> lines;
                StringUtil::split(v.stringValue2.getUnownedSlice(), '\n', lines);
                for (auto l : lines)
                {
                    sb << " -x" << v.stringValue << " " << l.trim();
                }
            }
            break;
        case CompilerOptionName::EmitSpirvDirectly:
        case CompilerOptionName::GLSLForceScalarLayout:
        case CompilerOptionName::ForceDXLayout:
        case CompilerOptionName::MatrixLayoutRow:
        case CompilerOptionName::MatrixLayoutColumn:
        case CompilerOptionName::VulkanInvertY:
        case CompilerOptionName::VulkanUseDxPositionW:
        case CompilerOptionName::VulkanUseEntryPointName:
        case CompilerOptionName::VulkanUseGLLayout:
        case CompilerOptionName::VulkanEmitReflection:
        case CompilerOptionName::EnableEffectAnnotations:
        case CompilerOptionName::DefaultImageFormatUnknown:
        case CompilerOptionName::DisableDynamicDispatch:
        case CompilerOptionName::DisableSpecialization:
        case CompilerOptionName::DumpIntermediates:
            if (option.value.getCount() && option.value[0].intValue != 0)
                sb << " " << name;
            break;
        }
    }
}

void CompilerOptionSet::buildHash(DigestBuilder<SHA1>& builder)
{
    for (auto& kv : options)
    {
        builder.append(kv.key);
        builder.append(kv.value.getCount());
        for (auto& v : kv.value)
        {
            if (v.kind == CompilerOptionValueKind::Int)
            {
                builder.append(v.intValue);
            }
            else
            {
                builder.append(v.stringValue);
                builder.append(v.stringValue2);
            }
        }
    }
}

bool CompilerOptionSet::allowDuplicate(CompilerOptionName name)
{
    switch (name)
    {
    case CompilerOptionName::Include:
    case CompilerOptionName::MacroDefine:
    case CompilerOptionName::WarningsAsErrors:
    case CompilerOptionName::DisableWarning:
    case CompilerOptionName::DisableWarnings:
    case CompilerOptionName::EnableWarning:
    case CompilerOptionName::Capability:
    case CompilerOptionName::DownstreamArgs:
    case CompilerOptionName::VulkanBindShift:
    case CompilerOptionName::VulkanBindShiftAll:
        return true;
    }
    return false;
}
CompilerOptionValue Slang::CompilerOptionSet::getDefault(CompilerOptionName name)
{
    switch (name)
    {
    case CompilerOptionName::Optimization:
        return CompilerOptionValue::fromEnum(OptimizationLevel::Default);
    default:
        return CompilerOptionValue();
    }
}

SlangTargetFlags CompilerOptionSet::getTargetFlags()
{
    SlangTargetFlags result = 0;
    if (getBoolOption(CompilerOptionName::DumpIr))
        result |= SLANG_TARGET_FLAG_DUMP_IR;
    if (getBoolOption(CompilerOptionName::GenerateWholeProgram))
        result |= SLANG_TARGET_FLAG_GENERATE_WHOLE_PROGRAM;
    if (getBoolOption(CompilerOptionName::ParameterBlocksUseRegisterSpaces))
        result |= SLANG_TARGET_FLAG_PARAMETER_BLOCKS_USE_REGISTER_SPACES;
    if (shouldEmitSPIRVDirectly())
        result |= SLANG_TARGET_FLAG_GENERATE_SPIRV_DIRECTLY;
    return result;
}

void CompilerOptionSet::setTargetFlags(SlangTargetFlags flags)
{
    set(CompilerOptionName::DumpIr, (flags & SLANG_TARGET_FLAG_DUMP_IR) != 0);
    set(CompilerOptionName::GenerateWholeProgram,
        (flags & SLANG_TARGET_FLAG_GENERATE_WHOLE_PROGRAM) != 0);

    if ((flags & SLANG_TARGET_FLAG_GENERATE_SPIRV_DIRECTLY) != 0)
        set(CompilerOptionName::EmitSpirvMethod, SLANG_EMIT_SPIRV_DIRECTLY);
    else
    {
        // We allow to set this flag only when users are not setting the
        // the spirv emit method via CompilerOptionName.
        SlangEmitSpirvMethod current =
            getEnumOption<SlangEmitSpirvMethod>(CompilerOptionName::EmitSpirvMethod);
        if (current != SLANG_EMIT_SPIRV_DIRECTLY)
            set(CompilerOptionName::EmitSpirvMethod, SLANG_EMIT_SPIRV_VIA_GLSL);
    }

    set(CompilerOptionName::ParameterBlocksUseRegisterSpaces,
        (flags & SLANG_TARGET_FLAG_PARAMETER_BLOCKS_USE_REGISTER_SPACES) != 0);
}

void CompilerOptionSet::addTargetFlags(SlangTargetFlags flags)
{
    if ((flags & SLANG_TARGET_FLAG_DUMP_IR))
        set(CompilerOptionName::DumpIr, true);

    if ((flags & SLANG_TARGET_FLAG_GENERATE_WHOLE_PROGRAM) != 0)
        set(CompilerOptionName::GenerateWholeProgram, true);

    if ((flags & SLANG_TARGET_FLAG_GENERATE_SPIRV_DIRECTLY) != 0)
        set(CompilerOptionName::EmitSpirvMethod, SLANG_EMIT_SPIRV_DIRECTLY);

    if ((flags & SLANG_TARGET_FLAG_PARAMETER_BLOCKS_USE_REGISTER_SPACES) != 0)
        set(CompilerOptionName::ParameterBlocksUseRegisterSpaces, true);
}
MatrixLayoutMode CompilerOptionSet::getMatrixLayoutMode()
{
    if (getBoolOption(CompilerOptionName::MatrixLayoutRow))
        return kMatrixLayoutMode_RowMajor;
    if (getBoolOption(CompilerOptionName::MatrixLayoutColumn))
        return kMatrixLayoutMode_ColumnMajor;

    return (MatrixLayoutMode)kMatrixLayoutMode_RowMajor;
}

void CompilerOptionSet::setMatrixLayoutMode(MatrixLayoutMode mode)
{
    options.remove(CompilerOptionName::MatrixLayoutColumn);
    options.remove(CompilerOptionName::MatrixLayoutRow);
    if (mode == kMatrixLayoutMode_ColumnMajor)
        set(CompilerOptionName::MatrixLayoutColumn, true);
    if (mode == kMatrixLayoutMode_RowMajor)
        set(CompilerOptionName::MatrixLayoutRow, true);
}

Profile CompilerOptionSet::getProfile()
{
    if (auto profileRaw = getEnumOption<Profile::RawEnum>(CompilerOptionName::Profile))
        return Profile(profileRaw);
    return Profile();
}

void CompilerOptionSet::setProfile(Profile profile)
{
    set(CompilerOptionName::Profile, (int)profile.raw);
}

ProfileVersion CompilerOptionSet::getProfileVersion()
{
    if (auto profileRaw = getEnumOption<Profile::RawEnum>(CompilerOptionName::Profile))
        return Profile(profileRaw).getVersion();
    return ProfileVersion::Unknown;
}

void CompilerOptionSet::setProfileVersion(ProfileVersion version)
{
    Profile profile;
    if (auto profileRaw = getEnumOption<Profile::RawEnum>(CompilerOptionName::Profile))
        profile = Profile(profileRaw);
    profile.setVersion(version);
    set(CompilerOptionName::Profile, (int)profile.raw);
}

void CompilerOptionSet::addCapabilityAtom(CapabilityName cap)
{
    add(CompilerOptionName::Capability, cap);
}

List<String> CompilerOptionSet::getDownstreamArgs(String downstreamToolName)
{
    List<String> result;
    auto downstreamArgsArray = getArray(CompilerOptionName::DownstreamArgs);
    for (auto& argSet : downstreamArgsArray)
    {
        if (argSet.stringValue == downstreamToolName)
        {
            CommandLineArgs args;
            args.deserialize(argSet.stringValue2);
            for (auto arg : args.m_args)
                result.add(arg.value);
            break;
        }
    }
    return result;
}

void CompilerOptionSet::serialize(SerializedOptionsData* outData)
{
    for (auto& option : options)
    {
        for (auto val : option.value)
        {
            slang::CompilerOptionEntry entry = {};
            entry.name = option.key;
            entry.value.kind = val.kind;
            entry.value.intValue0 = val.intValue;
            entry.value.intValue1 = val.intValue2;
            outData->stringPool.add(val.stringValue);
            entry.value.stringValue0 = val.stringValue.getBuffer();
            outData->stringPool.add(val.stringValue2);
            entry.value.stringValue1 = val.stringValue.getBuffer();
            outData->entries.add(entry);
        }
    }
}

void applySettingsToDiagnosticSink(
    DiagnosticSink* targetSink,
    DiagnosticSink* outputSink,
    CompilerOptionSet& options)
{
    auto disableArray = options.getArray(CompilerOptionName::DisableWarning);
    for (auto& element : disableArray)
    {
        overrideDiagnostic(
            targetSink,
            outputSink,
            element.stringValue.getUnownedSlice(),
            Severity::Warning,
            Severity::Disable);
    }
    disableArray = options.getArray(CompilerOptionName::DisableWarnings);
    for (auto& element : disableArray)
    {
        overrideDiagnostics(
            targetSink,
            outputSink,
            element.stringValue.getUnownedSlice(),
            Severity::Warning,
            Severity::Disable);
    }
    auto enableArray = options.getArray(CompilerOptionName::EnableWarning);
    for (auto& element : enableArray)
    {
        overrideDiagnostics(
            targetSink,
            outputSink,
            element.stringValue.getUnownedSlice(),
            Severity::Warning,
            Severity::Warning);
    }
    auto warningsAsErrorsArray = options.getArray(CompilerOptionName::WarningsAsErrors);
    for (auto& element : warningsAsErrorsArray)
    {
        if (element.stringValue == "all")
            targetSink->setFlag(DiagnosticSink::Flag::TreatWarningsAsErrors);
        else
            overrideDiagnostics(
                targetSink,
                outputSink,
                element.stringValue.getUnownedSlice(),
                Severity::Warning,
                Severity::Error);
    }
}
} // namespace Slang