summaryrefslogtreecommitdiff
path: root/source/compiler-core/slang-artifact.cpp
blob: 5d9f8692ccc4d07bde7bad46e3e4f64176a9c1e8 (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
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
// slang-artifact.cpp
#include "slang-artifact.h"

#include "../core/slang-type-text-util.h"
#include "../core/slang-io.h"

namespace Slang {

namespace { // anonymous
struct KindExtension
{
    ArtifactKind kind;
    UnownedStringSlice ext;
};
} // anonymous

#define SLANG_KIND_EXTENSION(kind, ext) \
    { ArtifactKind::kind, UnownedStringSlice::fromLiteral(ext) },

static const KindExtension g_cpuKindExts[] =
{
#if SLANG_WINDOWS_FAMILY
    SLANG_KIND_EXTENSION(Library, "lib")
    SLANG_KIND_EXTENSION(ObjectCode, "obj")
    SLANG_KIND_EXTENSION(Executable, "exe")
    SLANG_KIND_EXTENSION(SharedLibrary, "dll")
#else 
    SLANG_KIND_EXTENSION(Library, "a")
    SLANG_KIND_EXTENSION(ObjectCode, "o")
    SLANG_KIND_EXTENSION(Executable, "")

#if __CYGWIN__
    SLANG_KIND_EXTENSION(SharedLibrary, "dll")
#elif SLANG_APPLE_FAMILY
    SLANG_KIND_EXTENSION(SharedLibrary, "dylib")
#else
    SLANG_KIND_EXTENSION(SharedLibrary, "so")
#endif

#endif
};

/* static */ArtifactDesc ArtifactDesc::fromPath(const UnownedStringSlice& slice)
{
    auto extension = Path::getPathExt(slice);
    return fromExtension(extension);
}

/* static */ ArtifactDesc ArtifactDesc::fromExtension(const UnownedStringSlice& slice)
{
    if (slice == "slang-module" ||
        slice == "slang-lib")
    {
        return make(ArtifactKind::Library, ArtifactPayload::SlangIR, ArtifactStyle::Unknown);
    }

    for (const auto& kindExt : g_cpuKindExts)
    {
        if (slice == kindExt.ext)
        {
            // We'll assume it's for the host CPU for now..
            return make(kindExt.kind, Payload::HostCPU, Style::Unknown);
        }
    }

    const auto target = TypeTextUtil::findCompileTargetFromExtension(slice);

    return makeFromCompileTarget(target);
}

/* static */ArtifactDesc ArtifactDesc::makeFromCompileTarget(SlangCompileTarget target)
{
    switch (target)
    {
        case SLANG_TARGET_UNKNOWN:                return make(Kind::Unknown, Payload::None, Style::Unknown, 0);
        case SLANG_TARGET_NONE:                   return make(Kind::None, Payload::None, Style::Unknown, 0);
        case SLANG_GLSL_VULKAN:
        case SLANG_GLSL_VULKAN_ONE_DESC:
        case SLANG_GLSL:
        {
            // For the moment we make all just map to GLSL, but we could use flags
            // or some other mechanism to distinguish the types
            return make(Kind::Text, Payload::GLSL, Style::Kernel, 0);
        }
        case SLANG_HLSL:                    return make(Kind::Text, Payload::HLSL, Style::Kernel, 0);
        case SLANG_SPIRV:                   return make(Kind::Executable, Payload::SPIRV, Style::Kernel, 0);
        case SLANG_SPIRV_ASM:               return make(Kind::Text, Payload::SPIRVAssembly, Style::Kernel, 0);
        case SLANG_DXBC:                    return make(Kind::Executable, Payload::DXBC, Style::Kernel, 0);
        case SLANG_DXBC_ASM:                return make(Kind::Text, Payload::DXBCAssembly, Style::Kernel, 0);
        case SLANG_DXIL:                    return make(Kind::Executable, Payload::DXIL, Style::Kernel, 0);
        case SLANG_DXIL_ASM:                return make(Kind::Text, Payload::DXILAssembly, Style::Kernel, 0);
        case SLANG_C_SOURCE:                return make(Kind::Text, Payload::C, Style::Kernel, 0);
        case SLANG_CPP_SOURCE:              return make(Kind::Text, Payload::CPP, Style::Kernel, 0);
        case SLANG_HOST_CPP_SOURCE:         return make(Kind::Text, Payload::CPP, Style::Host, 0);
        case SLANG_HOST_EXECUTABLE:         return make(Kind::Executable, Payload::HostCPU, Style::Host, 0);
        case SLANG_SHADER_SHARED_LIBRARY:   return make(Kind::SharedLibrary, Payload::HostCPU, Style::Kernel, 0);
        case SLANG_SHADER_HOST_CALLABLE:    return make(Kind::Callable, Payload::HostCPU, Style::Kernel, 0);
        case SLANG_CUDA_SOURCE:             return make(Kind::Text, Payload::CUDA, Style::Kernel, 0);
        case SLANG_PTX:                     return make(Kind::Executable, Payload::PTX, Style::Kernel, 0);
        case SLANG_OBJECT_CODE:             return make(Kind::ObjectCode, Payload::HostCPU, Style::Kernel, 0);
        default: break;
    }

    SLANG_UNEXPECTED("Unhandled type");
}

/* static */bool ArtifactDesc::isPayloadGpuBinary(Payload payloadType)
{
    switch (payloadType)
    {
        case Payload::DXIL:
        case Payload::DXBC:
        case Payload::SPIRV:
        case Payload::PTX:
        {
            return true;
        }
        default: break;
    }
    return false;
}

/* static */bool ArtifactDesc::isPayloadCpuBinary(Payload payloadType)
{
    switch (payloadType)
    {
        case Payload::X86:
        case Payload::X86_64:
        case Payload::AARCH:
        case Payload::AARCH64:
        case Payload::HostCPU:
        {
            return true;
        }
        default: break;
    }
    return false;
}

/* static */bool ArtifactDesc::isPayloadGpuBinaryLinkable(Payload payload)
{
    switch (payload)
    {
        case Payload::DXBC:
        {
            // It seems as if DXBC is potentially linkable from
            // https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-appendix-keywords#export
            return true;
        }

        case Payload::DXIL:
        case Payload::PTX:
        case Payload::SPIRV:
        {
            // We can't *actually* link PTX or SPIR-V currently but it is in principal possible
            // so let's say we accept for now
            return true;
        }
        default: break;
    }
    return false;
}

bool ArtifactDesc::isBinaryLinkable() const
{
    if (isKindBinaryLinkable(kind))
    {
        return isPayloadCpuBinary(payload) || isPayloadGpuBinaryLinkable(payload) ||  payload == ArtifactPayload::SlangIR;
    }
    
    return false;
}

/* static */bool ArtifactDesc::isKindBinaryLinkable(Kind kind)
{
    switch (kind)
    {
        case Kind::Library:
        case Kind::ObjectCode:
        {
            return true;
        }
        default: break;
    }
    return false;
}

/* static*/ UnownedStringSlice ArtifactDesc::getCpuExtensionForKind(Kind kind)
{
    for (const auto& kindExt : g_cpuKindExts)
    {
        if (kind == kindExt.kind)
        {
            return kindExt.ext;
        }
    }
    return UnownedStringSlice();
}

UnownedStringSlice ArtifactDesc::getDefaultExtension()
{
    if (isPayloadCpuBinary(payload))
    {
        return getCpuExtensionForKind(kind);
    }
    else
    {
        return getDefaultExtensionForPayload(payload);
    }
}

/* static */UnownedStringSlice ArtifactDesc::getDefaultExtensionForPayload(Payload payload)
{
    switch (payload)
    {
        case Payload::None:         return UnownedStringSlice();
        case Payload::Unknown:      return UnownedStringSlice::fromLiteral("unknown");

        case Payload::DXIL:         return UnownedStringSlice::fromLiteral("dxil");
        case Payload::DXBC:         return UnownedStringSlice::fromLiteral("dxbc");
        case Payload::SPIRV:        return UnownedStringSlice::fromLiteral("spirv");

        case Payload::PTX:          return UnownedStringSlice::fromLiteral("ptx");

        case Payload::X86:
        case Payload::X86_64:
        case Payload::AARCH:
        case Payload::AARCH64:
        case Payload::HostCPU:
        {
            return UnownedStringSlice();
        }

        case Payload::SlangIR:      return UnownedStringSlice::fromLiteral("slang-ir");
        case Payload::LLVMIR:       return UnownedStringSlice::fromLiteral("llvm-ir");

        case Payload::HLSL:         return UnownedStringSlice::fromLiteral("hlsl");
        case Payload::GLSL:         return UnownedStringSlice::fromLiteral("glsl");

        case Payload::CPP:          return UnownedStringSlice::fromLiteral("cpp");
        case Payload::C:            return UnownedStringSlice::fromLiteral("c");

        case Payload::CUDA:         return UnownedStringSlice::fromLiteral("cu");

        case Payload::Slang:        return UnownedStringSlice::fromLiteral("slang");

        case Payload::Zip:          return UnownedStringSlice::fromLiteral("zip");

        default: break;
    }

    SLANG_UNEXPECTED("Unknown content type");
}

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Artifact !!!!!!!!!!!!!!!!!!!!!!!!!!! */

Artifact::~Artifact()
{
    if (m_pathType == PathType::Temporary)
    {
        File::remove(m_path);
    }

    for (const auto& entry : m_entries)
    {
        // Release the associated data
        switch (entry.type)
        {
            case Entry::Type::ObjectInstance:
            {
                entry.object->releaseReference();
                break;
            }
            case Entry::Type::InterfaceInstance:
            {
                entry.intf->release();
                break;
            }
            default: break;
        }
    }
}

Index Artifact::indexOf(Entry::Type type) const
{
    return m_entries.findFirstIndex([&](const Entry& entry) -> bool { return entry.type == type; });
}

void Artifact::add(Entry::Style style, RefObject* obj)
{
    SLANG_ASSERT(obj);

    Entry entry;
    entry.type = Entry::Type::ObjectInstance;
    entry.style = style;
    entry.object = obj;

    obj->addReference();
    
    m_entries.add(entry);
}

void Artifact::add(Entry::Style style, ISlangUnknown* intf)
{
    // Can't be nullptr
    SLANG_ASSERT(intf);

    Entry entry;
    entry.type = Entry::Type::InterfaceInstance;
    entry.style = style;

    intf->addRef();

    entry.intf = intf;
    m_entries.add(entry);
}

bool Artifact::exists() const
{
    // If we have a blob it exists
    if (m_blob)
    {
        return true;
    }

    // If we have an associated entry that represents the artifact it exists
    for (const auto& entry : m_entries)
    {
        if (entry.style == Entry::Style::Artifact)
        {
            // There is a representation that 'is' the artifact
            return true;
        }
    }

    // If we don't have a path then it can't exist
    if (m_pathType == PathType::None)
    {
        return false;
    }

    // If the file exists we assume it exists
    return File::exists(m_path);
}

ISlangUnknown* Artifact::findInterfaceInstance(const Guid& guid)
{
    for (const auto& entry : m_entries)
    {
        if (entry.type == Entry::Type::InterfaceInstance)
        {
            ISlangUnknown* intf = nullptr;
            if (SLANG_SUCCEEDED(entry.intf->queryInterface(guid, (void**)&intf)) && intf)
            {
                // NOTE! This assumes we *DONT* need to ref count to keep an interface in scope
                // (as strict COM requires so as to allow on demand interfaces).
                intf->release();

                return intf;
            }
        }
    }

    return nullptr;
}

SlangResult Artifact::requireFilePath(Keep keep, String& outFilePath)
{
    if (m_pathType != PathType::None)
    {
        outFilePath = m_path; 
        return SLANG_OK;
    }

    ComPtr<ISlangBlob> blob;

    // Get the contents as a blob. If we can't do that, then we can't write anything...
    SLANG_RETURN_ON_FAIL(loadBlob(getIntermediateKeep(keep), blob));

    const UnownedStringSlice ext = m_desc.getDefaultExtension();

    // TODO(JS): NOTE! This isn't strictly correct, as the generated filename is not guarenteed to be unique
    // if we change it with an extension (or prefix).
    // This doesn't change the previous behavior though.
    String path;
    SLANG_RETURN_ON_FAIL(File::generateTemporary(UnownedStringSlice::fromLiteral("slang-generated"), path));

    if (m_desc.isCpuBinary() && m_desc.kind == ArtifactKind::SharedLibrary)
    {
        const bool isSharedLibraryPrefixPlatform = SLANG_LINUX_FAMILY || SLANG_APPLE_FAMILY;
        if (isSharedLibraryPrefixPlatform)
        {
            StringBuilder buf;
            buf << "lib";
            buf << Path::getFileName(path);

            auto parentDir = Path::getParentDirectory(path);
            if (parentDir.getLength())
            {
                // Combine the name with path if their is a parent 
                path = Path::combine(parentDir, buf);
            }
            else
            {
                // Just use the name as is
                path = buf;
            }
        }
    }

    // If there is an extension append it
    if (ext.getLength())
    {
        path.appendChar('.');
        path.append(ext);
    }

    SLANG_RETURN_ON_FAIL(File::writeAllBytes(path, blob->getBufferPointer(), blob->getBufferSize()));

    // Okay we can now add this as temporary path too
    setPath(PathType::Temporary, path);

    return SLANG_OK;
}

SlangResult Artifact::loadBlob(Keep keep, ComPtr<ISlangBlob>& outBlob)
{
    if (m_blob)
    {
        outBlob = m_blob;
        return SLANG_OK;
    }

    // TODO(JS): 
    // Strictly speaking we could *potentially* convert some other representation into
    // a blob by serializing it, but we don't worry about any of that here
    if (m_pathType == PathType::None)
    {
        return SLANG_E_NOT_FOUND;
    }

    // Read into a blob
    ScopedAllocation alloc;
    SLANG_RETURN_ON_FAIL(File::readAllBytes(m_path, alloc));

    // Create as a blob
    RefPtr<RawBlob> blob = RawBlob::moveCreate(alloc);

    // Put in cache 
    if (canKeep(keep))
    {
        setBlob(blob);
    }

    outBlob = blob;
    return SLANG_OK;
}

} // namespace Slang