summaryrefslogtreecommitdiffstats
path: root/source/compiler-core
diff options
context:
space:
mode:
authorcheneym2 <acheney@nvidia.com>2025-03-05 16:45:03 -0500
committerGitHub <noreply@github.com>2025-03-05 13:45:03 -0800
commit0634684495f709fe3594fdcd483cfce7933e54eb (patch)
tree7a5d99705475a885b0d22169a56678a399133d12 /source/compiler-core
parent5248a0254a48382d06ecb190c9f87c0ab62ff534 (diff)
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>
Diffstat (limited to 'source/compiler-core')
-rw-r--r--source/compiler-core/slang-downstream-compiler.h13
-rw-r--r--source/compiler-core/slang-glslang-compiler.cpp41
-rw-r--r--source/compiler-core/slang-glslang-compiler.h8
3 files changed, 62 insertions, 0 deletions
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<ISlangSharedLibrary> 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