From 0634684495f709fe3594fdcd483cfce7933e54eb Mon Sep 17 00:00:00 2001 From: cheneym2 Date: Wed, 5 Mar 2025 16:45:03 -0500 Subject: Support SPIR-V deferred linking option (#6500) The new option "SkipDownstreamLinking" will defer final downstream IR linking to the user application. This option only has an effect if there are modules that were precompiled to the target IR using precompileForTarget(). Until now, the default behavior for SPIR-V was to use deferred linking, and the default behavior for DXIL was to use immediate/internal linking in Slang. This change only affects the SPIR-V behavior such that both deferred and non-deferred linking is supported based on the new option. To support the non-deferred option, Slang will internally call into SPIRV-Tools-link to reconstitute a complete SPIR-V shader program when necessary (due to modules having been precompiled to target IR). Otherwise, if SkipDownstreamLinking is enabled, the shader returned by e.g. getTargetCode() or getEntryPointCode() may have import linkage to the SPIR-V embedded in the constituent modules. Closes #4994 Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> --- source/compiler-core/slang-downstream-compiler.h | 13 ++++++++ source/compiler-core/slang-glslang-compiler.cpp | 41 ++++++++++++++++++++++++ source/compiler-core/slang-glslang-compiler.h | 8 +++++ 3 files changed, 62 insertions(+) (limited to 'source/compiler-core') diff --git a/source/compiler-core/slang-downstream-compiler.h b/source/compiler-core/slang-downstream-compiler.h index 82aaef107..c96003cc4 100644 --- a/source/compiler-core/slang-downstream-compiler.h +++ b/source/compiler-core/slang-downstream-compiler.h @@ -343,6 +343,19 @@ public: /// True if underlying compiler uses file system to communicate source virtual SLANG_NO_THROW bool SLANG_MCALL isFileBased() = 0; + + virtual SLANG_NO_THROW int SLANG_MCALL link( + const uint32_t** modules, + const uint32_t* moduleSizes, + const uint32_t moduleCount, + IArtifact** outArtifact) + { + SLANG_UNREFERENCED_PARAMETER(modules); + SLANG_UNREFERENCED_PARAMETER(moduleSizes); + SLANG_UNREFERENCED_PARAMETER(moduleCount); + SLANG_UNREFERENCED_PARAMETER(outArtifact); + return 0; + } }; class DownstreamCompilerBase : public ComBaseObject, public IDownstreamCompiler diff --git a/source/compiler-core/slang-glslang-compiler.cpp b/source/compiler-core/slang-glslang-compiler.cpp index b619f468f..540b437c5 100644 --- a/source/compiler-core/slang-glslang-compiler.cpp +++ b/source/compiler-core/slang-glslang-compiler.cpp @@ -49,6 +49,11 @@ public: validate(const uint32_t* contents, int contentsSize) SLANG_OVERRIDE; virtual SLANG_NO_THROW SlangResult SLANG_MCALL disassemble(const uint32_t* contents, int contentsSize) SLANG_OVERRIDE; + int link( + const uint32_t** modules, + const uint32_t* moduleSizes, + const uint32_t moduleCount, + IArtifact** outArtifact) SLANG_OVERRIDE; /// Must be called before use SlangResult init(ISlangSharedLibrary* library); @@ -66,6 +71,7 @@ protected: glslang_CompileFunc_1_2 m_compile_1_2 = nullptr; glslang_ValidateSPIRVFunc m_validate = nullptr; glslang_DisassembleSPIRVFunc m_disassemble = nullptr; + glslang_LinkSPIRVFunc m_link = nullptr; ComPtr m_sharedLibrary; @@ -80,6 +86,7 @@ SlangResult GlslangDownstreamCompiler::init(ISlangSharedLibrary* library) m_validate = (glslang_ValidateSPIRVFunc)library->findFuncByName("glslang_validateSPIRV"); m_disassemble = (glslang_DisassembleSPIRVFunc)library->findFuncByName("glslang_disassembleSPIRV"); + m_link = (glslang_LinkSPIRVFunc)library->findFuncByName("glslang_linkSPIRV"); if (m_compile_1_0 == nullptr && m_compile_1_1 == nullptr && m_compile_1_2 == nullptr) { @@ -323,6 +330,32 @@ SlangResult GlslangDownstreamCompiler::disassemble(const uint32_t* contents, int return SLANG_FAIL; } +SlangResult GlslangDownstreamCompiler::link( + const uint32_t** modules, + const uint32_t* moduleSizes, + const uint32_t moduleCount, + IArtifact** outArtifact) +{ + glslang_LinkRequest request; + memset(&request, 0, sizeof(request)); + + request.modules = modules; + request.moduleSizes = moduleSizes; + request.moduleCount = moduleCount; + + if (!m_link(&request)) + { + return SLANG_FAIL; + } + + auto artifact = ArtifactUtil::createArtifactForCompileTarget(SLANG_SPIRV); + artifact->addRepresentationUnknown( + Slang::RawBlob::create(request.linkResult, request.linkResultSize * sizeof(uint32_t))); + + *outArtifact = artifact.detach(); + return SLANG_OK; +} + bool GlslangDownstreamCompiler::canConvert(const ArtifactDesc& from, const ArtifactDesc& to) { // Can only disassemble blobs that are SPIR-V @@ -467,6 +500,14 @@ SlangResult SpirvDisDownstreamCompilerUtil::locateCompilers( return locateGlslangSpirvDownstreamCompiler(path, loader, set, SLANG_PASS_THROUGH_SPIRV_DIS); } +SlangResult SpirvLinkDownstreamCompilerUtil::locateCompilers( + const String& path, + ISlangSharedLibraryLoader* loader, + DownstreamCompilerSet* set) +{ + return locateGlslangSpirvDownstreamCompiler(path, loader, set, SLANG_PASS_THROUGH_SPIRV_LINK); +} + #else // SLANG_ENABLE_GLSLANG_SUPPORT /* static */ SlangResult GlslangDownstreamCompilerUtil::locateCompilers( diff --git a/source/compiler-core/slang-glslang-compiler.h b/source/compiler-core/slang-glslang-compiler.h index 73cc61135..d56ad7114 100644 --- a/source/compiler-core/slang-glslang-compiler.h +++ b/source/compiler-core/slang-glslang-compiler.h @@ -32,6 +32,14 @@ struct SpirvDisDownstreamCompilerUtil DownstreamCompilerSet* set); }; +struct SpirvLinkDownstreamCompilerUtil +{ + static SlangResult locateCompilers( + const String& path, + ISlangSharedLibraryLoader* loader, + DownstreamCompilerSet* set); +}; + } // namespace Slang #endif -- cgit v1.2.3