From 01efe34dbef2be952298075abd8d36cc67ac9f4e Mon Sep 17 00:00:00 2001 From: Yong He Date: Mon, 4 Mar 2024 13:14:21 -0800 Subject: Add `IGlobalSession::getSessionDescDigest`. (#3669) * Add `IGlobalSession::getSessionDescDigest`. * Fix. --- slang.h | 4 +++ source/slang/slang-compiler.h | 4 ++- source/slang/slang.cpp | 74 +++++++++++++++++++++++++++++-------------- 3 files changed, 58 insertions(+), 24 deletions(-) diff --git a/slang.h b/slang.h index 8e787ac28..b826707aa 100644 --- a/slang.h +++ b/slang.h @@ -3737,6 +3737,10 @@ namespace slang */ virtual SLANG_NO_THROW SlangResult SLANG_MCALL parseCommandLineArguments( int argc, const char* const* argv, SessionDesc* outSessionDesc, ISlangUnknown** outAuxAllocation) = 0; + + /** Computes a digest that uniquely identifies the session description. + */ + virtual SLANG_NO_THROW SlangResult SLANG_MCALL getSessionDescDigest(SessionDesc* sessionDesc, ISlangBlob** outBlob) = 0; }; #define SLANG_UUID_IGlobalSession IGlobalSession::getTypeGuid() diff --git a/source/slang/slang-compiler.h b/source/slang/slang-compiler.h index 4876026a0..0dd0012ad 100755 --- a/source/slang/slang-compiler.h +++ b/source/slang/slang-compiler.h @@ -1835,7 +1835,7 @@ namespace Slang // Updates the supplied builder with linkage-related information, which includes preprocessor // defines, the compiler version, and other compiler options. This is then merged with the hash // produced for the program to produce a key that can be used with the shader cache. - void buildHash(DigestBuilder& builder, SlangInt targetIndex); + void buildHash(DigestBuilder& builder, SlangInt targetIndex = -1); void addTarget( slang::TargetDesc const& desc); @@ -3003,6 +3003,8 @@ namespace Slang SLANG_NO_THROW SlangResult SLANG_MCALL parseCommandLineArguments( int argc, const char* const* argv, slang::SessionDesc* outSessionDesc, ISlangUnknown** outAllocation) override; + SLANG_NO_THROW SlangResult SLANG_MCALL getSessionDescDigest(slang::SessionDesc* sessionDesc, ISlangBlob** outBlob) override; + /// Get the downstream compiler for a transition IDownstreamCompiler* getDownstreamCompiler(CodeGenTarget source, CodeGenTarget target); diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp index 4b11a5216..1aa014a4b 100644 --- a/source/slang/slang.cpp +++ b/source/slang/slang.cpp @@ -878,6 +878,18 @@ SLANG_NO_THROW SlangResult SLANG_MCALL Session::parseCommandLineArguments( return SLANG_OK; } +SLANG_NO_THROW SlangResult SLANG_MCALL Session::getSessionDescDigest(slang::SessionDesc* sessionDesc, ISlangBlob** outBlob) +{ + ComPtr tempSession; + createSession(*sessionDesc, tempSession.writeRef()); + auto linkage = static_cast(tempSession.get()); + DigestBuilder digestBuilder; + linkage->buildHash(digestBuilder, -1); + auto blob = digestBuilder.finalize().toBlob(); + *outBlob = blob.detach(); + return SLANG_OK; +} + Profile getEffectiveProfile(EntryPoint* entryPoint, TargetRequest* target) { auto entryPointProfile = entryPoint->getProfile(); @@ -1488,37 +1500,53 @@ void Linkage::buildHash(DigestBuilder& builder, SlangInt targetIndex) // Add compiler options, including search path, preprocessor includes, etc. m_optionSet.buildHash(builder); - // Add the target specified by targetIndex - auto targetReq = targets[targetIndex]; - targetReq->getOptionSet().buildHash(builder); + auto addTargetDigest = [&](TargetRequest* targetReq) + { + targetReq->getOptionSet().buildHash(builder); - const PassThroughMode passThroughMode = getDownstreamCompilerRequiredForTarget(targetReq->getTarget()); - const SourceLanguage sourceLanguage = getDefaultSourceLanguageForDownstreamCompiler(passThroughMode); + const PassThroughMode passThroughMode = getDownstreamCompilerRequiredForTarget(targetReq->getTarget()); + const SourceLanguage sourceLanguage = getDefaultSourceLanguageForDownstreamCompiler(passThroughMode); - // Add prelude for the given downstream compiler. - ComPtr prelude; - getGlobalSession()->getLanguagePrelude((SlangSourceLanguage)sourceLanguage, prelude.writeRef()); - if (prelude) - { - builder.append(prelude); - } + // Add prelude for the given downstream compiler. + ComPtr prelude; + getGlobalSession()->getLanguagePrelude((SlangSourceLanguage)sourceLanguage, prelude.writeRef()); + if (prelude) + { + builder.append(prelude); + } - // TODO: Downstream compilers (specifically dxc) can currently #include additional dependencies. - // This is currently the case for NVAPI headers included in the prelude. - // These dependencies are currently not picked up by the shader cache which is a significant issue. - // This can only be fixed by running the preprocessor in the slang compiler so dxc (or any other - // downstream compiler for that matter) isn't resolving any includes implicitly. + // TODO: Downstream compilers (specifically dxc) can currently #include additional dependencies. + // This is currently the case for NVAPI headers included in the prelude. + // These dependencies are currently not picked up by the shader cache which is a significant issue. + // This can only be fixed by running the preprocessor in the slang compiler so dxc (or any other + // downstream compiler for that matter) isn't resolving any includes implicitly. - // Add the downstream compiler version (if it exists) to the hash - auto downstreamCompiler = getSessionImpl()->getOrLoadDownstreamCompiler(passThroughMode, nullptr); - if (downstreamCompiler) + // Add the downstream compiler version (if it exists) to the hash + auto downstreamCompiler = getSessionImpl()->getOrLoadDownstreamCompiler(passThroughMode, nullptr); + if (downstreamCompiler) + { + ComPtr versionString; + if (SLANG_SUCCEEDED(downstreamCompiler->getVersionString(versionString.writeRef()))) + { + builder.append(versionString); + } + } + }; + + // Add the target specified by targetIndex + if (targetIndex == -1) { - ComPtr versionString; - if (SLANG_SUCCEEDED(downstreamCompiler->getVersionString(versionString.writeRef()))) + // -1 means all targets. + for (auto targetReq : targets) { - builder.append(versionString); + addTargetDigest(targetReq); } } + else + { + auto targetReq = targets[targetIndex]; + addTargetDigest(targetReq); + } } SlangResult Linkage::addSearchPath( -- cgit v1.2.3