From 79dd12c21e8f5c5ce01051a280679cf6ac8ffe97 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Tue, 26 Apr 2022 12:09:32 -0400 Subject: Linking in DXC (#2190) * #include an absolute path didn't work - because paths were taken to always be relative. * Compile to a dxil library. * Added CompileProduct. * Support handling of ModuleLibrary. * CacheBehavior -> Cache * Use CompileProduct for -r references. * CompileProduct -> Artifact. * Determining an artifact type on binding. * Determine binary linkability. * Added Artifact::exists. * Added ArtifactKeep. * Small fixes. * Small improvements to Artifact. * Add zip extension. * Fix some comments. * Fix multiple adding of PublicDecoration. Make public output export for DXIL/lib. Add checking for simpleDecorations such that only added once. * Use 'whole program' to identify library build. * Move slang-artifact into compiler-core. * Split out Keep free functions. * Artifact::Keep -> ArtifactKeep. * Handle libraries as artifacts. * Add -target dxil so test infrastructure knows it needs DXC. * Linking working in DXC. * Improve handling around emit for 'export'. * Add comment around Artifact name. * Render test working with linking. Co-authored-by: Theresa Foley <10618364+tangent-vector@users.noreply.github.com> --- source/slang/slang-module-library.cpp | 95 +++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 source/slang/slang-module-library.cpp (limited to 'source/slang/slang-module-library.cpp') diff --git a/source/slang/slang-module-library.cpp b/source/slang/slang-module-library.cpp new file mode 100644 index 000000000..76bb85ebb --- /dev/null +++ b/source/slang/slang-module-library.cpp @@ -0,0 +1,95 @@ +// slang-module-library.cpp +#include "slang-module-library.h" +#include + +#include "../core/slang-blob.h" +#include "../core/slang-riff.h" + +#include "../core/slang-type-text-util.h" + +// Serialization +#include "slang-serialize-ir.h" +#include "slang-serialize-container.h" + +namespace Slang { + + +SlangResult loadModuleLibrary(const Byte* inBytes, size_t bytesCount, EndToEndCompileRequest* req, RefPtr& outLibrary) +{ + RefPtr library = new ModuleLibrary; + + // Load up the module + MemoryStreamBase memoryStream(FileAccess::Read, inBytes, bytesCount); + + RiffContainer riffContainer; + SLANG_RETURN_ON_FAIL(RiffUtil::read(&memoryStream, riffContainer)); + + auto linkage = req->getLinkage(); + + // TODO(JS): May be better to have a ITypeComponent that encapsulates a collection of modules + // For now just add to the linkage + + { + SerialContainerData containerData; + + SerialContainerUtil::ReadOptions options; + options.namePool = req->getNamePool(); + options.session = req->getSession(); + options.sharedASTBuilder = linkage->getASTBuilder()->getSharedASTBuilder(); + options.sourceManager = linkage->getSourceManager(); + options.linkage = req->getLinkage(); + options.sink = req->getSink(); + + SLANG_RETURN_ON_FAIL(SerialContainerUtil::read(&riffContainer, options, containerData)); + + for (const auto& module : containerData.modules) + { + // If the irModule is set, add it + if (module.irModule) + { + library->m_modules.add(module.irModule); + } + } + + for (const auto& entryPoint : containerData.entryPoints) + { + FrontEndCompileRequest::ExtraEntryPointInfo dst; + dst.mangledName = entryPoint.mangledName; + dst.name = entryPoint.name; + dst.profile = entryPoint.profile; + + // Add entry point + library->m_entryPoints.add(dst); + } + } + + outLibrary = library; + return SLANG_OK; +} + +SlangResult loadModuleLibrary(ArtifactKeep keep, Artifact* product, EndToEndCompileRequest* req, RefPtr& outLibrary) +{ + if (auto foundLibrary = product->findObjectInstance()) + { + outLibrary = foundLibrary; + return SLANG_OK; + } + + // Load the blob + ComPtr blob; + SLANG_RETURN_ON_FAIL(product->loadBlob(getIntermediateKeep(keep), blob)); + + // Load the module + RefPtr library; + SLANG_RETURN_ON_FAIL(loadModuleLibrary((const Byte*)blob->getBufferPointer(), blob->getBufferSize(), req, library)); + + if (canKeep(keep)) + { + product->add(Artifact::Entry::Style::Artifact, library); + } + + outLibrary = library; + return SLANG_OK; +} + +} // namespace Slang -- cgit v1.2.3