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

#include "slang-artifact-info.h"

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

namespace Slang {

/* 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);
        case SLANG_HOST_HOST_CALLABLE:      return make(Kind::Callable, Payload::HostCPU, Style::Host, 0);
        default: break;
    }

    SLANG_UNEXPECTED("Unhandled type");
}

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

void* Artifact::getInterface(const Guid& uuid)
{
    if (uuid == ISlangUnknown::getTypeGuid() || uuid == IArtifact::getTypeGuid())
    {
        return static_cast<IArtifact*>(this);
    }
    return nullptr;
}

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

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

    // If we have an associated entry that represents the artifact it exists
    if (findElement(IArtifactInstance::getTypeGuid()))
    {
        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::findElement(const Guid& guid)
{
    for (auto const& element : m_elements)
    {
        ISlangUnknown* value = element.value;

        ISlangUnknown* intf = nullptr;
        if (SLANG_SUCCEEDED(value->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;
}

void Artifact::addElement(const Desc& desc, ISlangUnknown* intf) 
{ 
    SLANG_ASSERT(intf); 
    Element element{ desc, ComPtr<ISlangUnknown>(intf) };
    m_elements.add(element); 
}

void Artifact::removeElementAt(Index i)
{
    m_elements.removeAt(i);
}

void* Artifact::findElementObject(const Guid& classGuid)
{
    ComPtr<IArtifactInstance> instance;
    for (const auto& element : m_elements)
    {
        ISlangUnknown* value = element.value;

        if (SLANG_SUCCEEDED(value->queryInterface(IArtifactInstance::getTypeGuid(), (void**)instance.writeRef())) && instance)
        {
            void* classInstance = instance->queryObject(classGuid);
            if (classInstance)
            {
                return classInstance;
            }
        }
    }

    return nullptr;
}

SlangResult Artifact::requireFileLike(Keep keep)
{
    // If there is no path set and no blob we still need a name. 
    // If the artifact is a library we can assume it's a system level library, 
    // or it can be found by appropriate search paths. 
    if (m_pathType == PathType::None && 
        m_blob == nullptr && 
        (m_desc.kind == ArtifactKind::Library || 
         m_desc.kind == ArtifactKind::SharedLibrary))
    {
        if (m_name.getLength() > 0)
        {
            return SLANG_OK;
        }

        // TODO(JS): If we could serialize, we could turn some other representation into a file, and therefore 
        // a name, but currently that's not supported
        return SLANG_E_NOT_FOUND;
    }

    // Will turn into a file if necessary
    SLANG_RETURN_ON_FAIL(requireFile(keep));
    return SLANG_OK;
}

SlangResult Artifact::requireFile(Keep keep)
{
    if (m_pathType != PathType::None)
    {
        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.writeRef()));

    // If we have a name, make the generated name based on that name
    // Else just use 'slang-generated' the basis

    UnownedStringSlice nameBase;
    if (m_name.getLength() > 0)
    {
        nameBase = m_name.getUnownedSlice();
    }
    else
    {
        nameBase = UnownedStringSlice::fromLiteral("slang-generated");
    }

    // 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(nameBase, path));

    if (ArtifactInfoUtil::isCpuBinary(m_desc) && 
        (m_desc.kind == ArtifactKind::SharedLibrary ||
         m_desc.kind == ArtifactKind::Library))
    {
        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

    const UnownedStringSlice ext = ArtifactInfoUtil::getDefaultExtension(m_desc);

    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, ISlangBlob** outBlob)
{
    ComPtr<ISlangBlob> blob(m_blob);

    if (!blob)
    {
        // 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)
        {
            // Read into a blob
            ScopedAllocation alloc;
            SLANG_RETURN_ON_FAIL(File::readAllBytes(m_path, alloc));

            // Create as a blob
            blob = RawBlob::moveCreate(alloc);
        }
        else
        {
            for (const auto& element : m_elements)
            {
                ISlangUnknown* intf = element.value;

                ComPtr<IArtifactInstance> inst;
                if (SLANG_SUCCEEDED(intf->queryInterface(IArtifactInstance::getTypeGuid(), (void**)inst.writeRef())) && inst)
                {
                    SlangResult res = inst->writeToBlob(blob.writeRef());
                    if (SLANG_SUCCEEDED(res) && blob)
                    {
                        break;
                    }
                }
            }
        }

        // Wasn't able to construct

        if (!blob)
        {
            return SLANG_E_NOT_FOUND;
        }

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

    *outBlob = blob.detach();
    return SLANG_OK;
}

} // namespace Slang