Skip to content

Commit

Permalink
Add IGlobalSession::getSessionDescDigest. (#3669)
Browse files Browse the repository at this point in the history
* Add `IGlobalSession::getSessionDescDigest`.

* Fix.
  • Loading branch information
csyonghe authored Mar 4, 2024
1 parent 16342f4 commit 01efe34
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 24 deletions.
4 changes: 4 additions & 0 deletions slang.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion source/slang/slang-compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<SHA1>& builder, SlangInt targetIndex);
void buildHash(DigestBuilder<SHA1>& builder, SlangInt targetIndex = -1);

void addTarget(
slang::TargetDesc const& desc);
Expand Down Expand Up @@ -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);

Expand Down
74 changes: 51 additions & 23 deletions source/slang/slang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<slang::ISession> tempSession;
createSession(*sessionDesc, tempSession.writeRef());
auto linkage = static_cast<Linkage*>(tempSession.get());
DigestBuilder<SHA1> 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();
Expand Down Expand Up @@ -1488,37 +1500,53 @@ void Linkage::buildHash(DigestBuilder<SHA1>& 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<ISlangBlob> prelude;
getGlobalSession()->getLanguagePrelude((SlangSourceLanguage)sourceLanguage, prelude.writeRef());
if (prelude)
{
builder.append(prelude);
}
// Add prelude for the given downstream compiler.
ComPtr<ISlangBlob> 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<ISlangBlob> versionString;
if (SLANG_SUCCEEDED(downstreamCompiler->getVersionString(versionString.writeRef())))
{
builder.append(versionString);
}
}
};

// Add the target specified by targetIndex
if (targetIndex == -1)
{
ComPtr<ISlangBlob> 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(
Expand Down

0 comments on commit 01efe34

Please sign in to comment.