Skip to content

Commit 7a942cf

Browse files
authored
Add referenced modules as libraries when creating a session (shader-slang#6569)
In the legacy compile request based API, the referenced modules are added to the request's linkage libraries as part of compiler option parsing. In the non-legacy compilation API, the argument parsing creates a temprary compile request and so those libraries only survive as options. This change will look for such options when creating an ISession object, and again add the referenced modules to the libraries of the new linkage that's contained in the ISession object. This is done in two steps: 1. Factor out a helper to create a referenced module artifact in the same way as it's done during legacy option parsing. 2. Use the helper function to create artifacts to add to the linkage libararies, when the session is created. This helps to address issue shader-slang#4760, because it enables passing in downstream modules via options, as is required for the following tests: tests/library/library-test.slang.2 (dx12) tests/library/export-test.slang.2 (dx12)
1 parent 133d705 commit 7a942cf

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

source/slang/slang-options.cpp

+17-4
Original file line numberDiff line numberDiff line change
@@ -1748,13 +1748,17 @@ SlangResult OptionsParser::_expectInt(const CommandLineArg& initArg, Int& outInt
17481748
return SLANG_OK;
17491749
}
17501750

1751-
SlangResult OptionsParser::addReferencedModule(String path, SourceLoc loc, bool includeEntryPoint)
1751+
SlangResult createArtifactFromReferencedModule(
1752+
String path,
1753+
SourceLoc loc,
1754+
DiagnosticSink* sink,
1755+
IArtifact** outArtifact)
17521756
{
17531757
auto desc = ArtifactDescUtil::getDescFromPath(path.getUnownedSlice());
17541758

17551759
if (desc.kind == ArtifactKind::Unknown)
17561760
{
1757-
m_sink->diagnose(loc, Diagnostics::unknownLibraryKind, Path::getPathExt(path));
1761+
sink->diagnose(loc, Diagnostics::unknownLibraryKind, Path::getPathExt(path));
17581762
return SLANG_FAIL;
17591763
}
17601764

@@ -1772,7 +1776,7 @@ SlangResult OptionsParser::addReferencedModule(String path, SourceLoc loc, bool
17721776

17731777
if (!ArtifactDescUtil::isLinkable(desc))
17741778
{
1775-
m_sink->diagnose(loc, Diagnostics::kindNotLinkable, Path::getPathExt(path));
1779+
sink->diagnose(loc, Diagnostics::kindNotLinkable, Path::getPathExt(path));
17761780
return SLANG_FAIL;
17771781
}
17781782

@@ -1803,11 +1807,20 @@ SlangResult OptionsParser::addReferencedModule(String path, SourceLoc loc, bool
18031807
nullptr);
18041808
if (!fileRep->exists())
18051809
{
1806-
m_sink->diagnose(loc, Diagnostics::libraryDoesNotExist, path);
1810+
sink->diagnose(loc, Diagnostics::libraryDoesNotExist, path);
18071811
return SLANG_FAIL;
18081812
}
18091813
}
18101814
artifact->addRepresentation(fileRep);
1815+
*outArtifact = artifact.detach();
1816+
return SLANG_OK;
1817+
}
1818+
1819+
SlangResult OptionsParser::addReferencedModule(String path, SourceLoc loc, bool includeEntryPoint)
1820+
{
1821+
ComPtr<IArtifact> artifact;
1822+
SLANG_RETURN_ON_FAIL(
1823+
createArtifactFromReferencedModule(path, loc, m_sink, artifact.writeRef()));
18111824

18121825
SLANG_RETURN_ON_FAIL(_addLibraryReference(m_requestImpl, path, artifact, includeEntryPoint));
18131826
for (Index i = m_rawTranslationUnits.getCount(); i < m_requestImpl->getTranslationUnitCount();

source/slang/slang-options.h

+9
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
#ifndef SLANG_OPTIONS_H
33
#define SLANG_OPTIONS_H
44

5+
#include "../compiler-core/slang-source-loc.h"
56
#include "../core/slang-basic.h"
67

78
namespace Slang
89
{
910

1011
struct CommandOptions;
12+
class DiagnosticSink;
13+
class IArtifact;
1114

1215
UnownedStringSlice getCodeGenTargetName(SlangCompileTarget target);
1316

@@ -20,5 +23,11 @@ enum class Stage : SlangUInt32;
2023

2124
SlangSourceLanguage findSourceLanguageFromPath(const String& path, Stage& outImpliedStage);
2225

26+
SlangResult createArtifactFromReferencedModule(
27+
String path,
28+
SourceLoc loc,
29+
DiagnosticSink* sink,
30+
IArtifact** outArtifact);
31+
2332
} // namespace Slang
2433
#endif

source/slang/slang.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,29 @@ Session::createSession(slang::SessionDesc const& inDesc, slang::ISession** outSe
861861
Math::Max(linkageDebugInfoLevel, target->getOptionSet().getDebugInfoLevel());
862862
linkage->m_optionSet.set(CompilerOptionName::DebugInformation, linkageDebugInfoLevel);
863863

864+
// Add any referenced modules to the linkage
865+
for (auto& option : linkage->m_optionSet.options)
866+
{
867+
if (option.key != CompilerOptionName::ReferenceModule)
868+
continue;
869+
for (auto& path : option.value)
870+
{
871+
DiagnosticSink sink;
872+
ComPtr<IArtifact> artifact;
873+
SlangResult result = createArtifactFromReferencedModule(
874+
path.stringValue,
875+
SourceLoc{},
876+
&sink,
877+
artifact.writeRef());
878+
if (SLANG_FAILED(result))
879+
{
880+
sink.diagnose(SourceLoc{}, Diagnostics::unableToReadFile, path.stringValue);
881+
return result;
882+
}
883+
linkage->m_libModules.add(artifact);
884+
}
885+
}
886+
864887
*outSession = asExternal(linkage.detach());
865888
return SLANG_OK;
866889
}

0 commit comments

Comments
 (0)