Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up precompileForTargets function signature #4802

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/slang.h
Original file line number Diff line number Diff line change
Expand Up @@ -5444,6 +5444,10 @@ namespace slang
SlangInt32 index) = 0;

virtual SLANG_NO_THROW DeclReflection* SLANG_MCALL getModuleReflection() = 0;

virtual SLANG_NO_THROW SlangResult SLANG_MCALL precompileForTarget(
SlangCompileTarget target,
ISlangBlob** outDiagnostics) = 0;
};

#define SLANG_UUID_IModule IModule::getTypeGuid()
Expand Down
5 changes: 4 additions & 1 deletion source/compiler-core/slang-source-loc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,10 @@ void SourceFile::setContents(ISlangBlob* blob)

char const* decodedContentBegin = (char const*)m_contentBlob->getBufferPointer();
const UInt decodedContentSize = m_contentBlob->getBufferSize();
assert(decodedContentSize <= rawContentSize);
if (decodedContentSize > rawContentSize)
{
printf("Warning: Decoded content size is larger than raw content size\n");
}
char const* decodedContentEnd = decodedContentBegin + decodedContentSize;

m_content = UnownedStringSlice(decodedContentBegin, decodedContentEnd);
Expand Down
3 changes: 3 additions & 0 deletions source/slang-record-replay/record/slang-module.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ namespace SlangRecord
virtual SLANG_NO_THROW SlangInt32 SLANG_MCALL getDependencyFileCount() override;
virtual SLANG_NO_THROW char const* SLANG_MCALL getDependencyFilePath(
SlangInt32 index) override;
virtual SLANG_NO_THROW SlangResult SLANG_MCALL precompileForTarget(
SlangCompileTarget target,
ISlangBlob** outDiagnostics) override { return SLANG_OK; };

// Interfaces for `IComponentType`
virtual SLANG_NO_THROW slang::ISession* SLANG_MCALL getSession() override;
Expand Down
89 changes: 34 additions & 55 deletions source/slang/slang-compiler-tu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,60 +8,25 @@

namespace Slang
{
SLANG_NO_THROW SlangResult SLANG_MCALL Module::precompileForTargets(
DiagnosticSink* sink,
EndToEndCompileRequest* endToEndReq,
TargetRequest* targetReq)
{
auto module = getIRModule();
Slang::Session* session = endToEndReq->getSession();
Slang::ASTBuilder* astBuilder = session->getGlobalASTBuilder();
Slang::Linkage* builtinLinkage = session->getBuiltinLinkage();
Slang::Linkage linkage(session, astBuilder, builtinLinkage);

CapabilityName precompileRequirement = CapabilityName::Invalid;
switch (targetReq->getTarget())
SLANG_NO_THROW SlangResult SLANG_MCALL Module::precompileForTarget(
SlangCompileTarget target,
slang::IBlob** outDiagnostics)
{

if (target != SLANG_DXIL)
{
case CodeGenTarget::DXIL:
linkage.addTarget(Slang::CodeGenTarget::DXIL);
precompileRequirement = CapabilityName::dxil_lib;
break;
default:
assert(!"Unhandled target");
break;
return SLANG_FAIL;
}
SLANG_ASSERT(precompileRequirement != CapabilityName::Invalid);
CodeGenTarget targetEnum = CodeGenTarget(target);

auto module = getIRModule();
auto linkage = getLinkage();

// Ensure precompilation capability requirements are met.
auto targetCaps = targetReq->getTargetCaps();
auto precompileRequirementsCapabilitySet = CapabilitySet(precompileRequirement);
if (targetCaps.atLeastOneSetImpliedInOther(precompileRequirementsCapabilitySet) == CapabilitySet::ImpliesReturnFlags::NotImplied)
{
// If `RestrictiveCapabilityCheck` is true we will error, else we will warn.
// error ...: dxil libraries require $0, entry point compiled with $1.
// warn ...: dxil libraries require $0, entry point compiled with $1, implicitly upgrading capabilities.
maybeDiagnoseWarningOrError(
sink,
targetReq->getOptionSet(),
DiagnosticCategory::Capability,
SourceLoc(),
Diagnostics::incompatibleWithPrecompileLib,
Diagnostics::incompatibleWithPrecompileLibRestrictive,
precompileRequirementsCapabilitySet,
targetCaps);

// add precompile requirements to the cooked targetCaps
targetCaps.join(precompileRequirementsCapabilitySet);
if (targetCaps.isInvalid())
{
sink->diagnose(SourceLoc(), Diagnostics::unknownCapability, targetCaps);
return SLANG_FAIL;
}
else
{
targetReq->setTargetCaps(targetCaps);
}
}
DiagnosticSink sink(linkage->getSourceManager(), Lexer::sourceLocationLexer);
applySettingsToDiagnosticSink(&sink, &sink, linkage->m_optionSet);
applySettingsToDiagnosticSink(&sink, &sink, m_optionSet);

TargetRequest* targetReq = new TargetRequest(linkage, targetEnum);

List<RefPtr<ComponentType>> allComponentTypes;
allComponentTypes.add(this); // Add Module as a component type
Expand All @@ -72,23 +37,37 @@ namespace Slang
}

auto composite = CompositeComponentType::create(
&linkage,
linkage,
allComponentTypes);

TargetProgram tp(composite, targetReq);
tp.getOrCreateLayout(sink);
tp.getOrCreateLayout(&sink);
Slang::Index const entryPointCount = m_entryPoints.getCount();

tp.getOptionSet().add(CompilerOptionName::GenerateWholeProgram, true);

switch (targetReq->getTarget())
{
case CodeGenTarget::DXIL:
tp.getOptionSet().add(CompilerOptionName::Profile, Profile::RawEnum::DX_Lib_6_6);
break;
default:
assert(!"Unhandled target");
break;
}

CodeGenContext::EntryPointIndices entryPointIndices;

entryPointIndices.setCount(entryPointCount);
for (Index i = 0; i < entryPointCount; i++)
entryPointIndices[i] = i;
CodeGenContext::Shared sharedCodeGenContext(&tp, entryPointIndices, sink, endToEndReq);
CodeGenContext::Shared sharedCodeGenContext(&tp, entryPointIndices, &sink, nullptr);
CodeGenContext codeGenContext(&sharedCodeGenContext);

ComPtr<IArtifact> outArtifact;
SlangResult res = codeGenContext.emitTranslationUnit(outArtifact);

sink.getBlobIfNeeded(outDiagnostics);

if (res != SLANG_OK)
{
return res;
Expand Down
7 changes: 3 additions & 4 deletions source/slang/slang-compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -1482,10 +1482,9 @@ namespace Slang
SlangInt32 index) override;

/// Precompile TU to target language
virtual SLANG_NO_THROW SlangResult SLANG_MCALL precompileForTargets(
DiagnosticSink* sink,
EndToEndCompileRequest* endToEndReq,
TargetRequest* targetReq);
virtual SLANG_NO_THROW SlangResult SLANG_MCALL precompileForTarget(
SlangCompileTarget target,
slang::IBlob** outDiagnostics) override;

virtual void buildHash(DigestBuilder<SHA1>& builder) SLANG_OVERRIDE;

Expand Down
8 changes: 4 additions & 4 deletions source/slang/slang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3244,10 +3244,10 @@ SlangResult EndToEndCompileRequest::executeActionsInner()

for (auto translationUnit : frontEndReq->translationUnits)
{
translationUnit->getModule()->precompileForTargets(
getSink(),
this,
targetReq);
SlangCompileTarget target = SlangCompileTarget(targetReq->getTarget());
translationUnit->getModule()->precompileForTarget(
target,
nullptr);
}
}
}
Expand Down
36 changes: 33 additions & 3 deletions tools/gfx-unit-test/precompiled-module-2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ namespace gfx_test
static Slang::Result precompileProgram(
gfx::IDevice* device,
ISlangMutableFileSystem* fileSys,
const char* shaderModuleName)
const char* shaderModuleName,
bool precompileToTarget)
{
Slang::ComPtr<slang::ISession> slangSession;
SLANG_RETURN_ON_FAIL(device->getSlangSession(slangSession.writeRef()));
Expand All @@ -34,6 +35,20 @@ namespace gfx_test
if (!module)
return SLANG_FAIL;

if (precompileToTarget)
{
SlangCompileTarget target;
switch (device->getDeviceInfo().deviceType)
{
case gfx::DeviceType::DirectX12:
target = SLANG_DXIL;
break;
default:
return SLANG_FAIL;
}
module->precompileForTarget(target, diagnosticsBlob.writeRef());
}

// Write loaded modules to memory file system.
for (SlangInt i = 0; i < slangSession->getLoadedModuleCount(); i++)
{
Expand All @@ -50,7 +65,7 @@ namespace gfx_test
return SLANG_OK;
}

void precompiledModule2TestImpl(IDevice* device, UnitTestContext* context)
void precompiledModule2TestImplCommon(IDevice* device, UnitTestContext* context, bool precompileToTarget)
{
Slang::ComPtr<ITransientResourceHeap> transientHeap;
ITransientResourceHeap::Desc transientHeapDesc = {};
Expand All @@ -63,7 +78,7 @@ namespace gfx_test

ComPtr<IShaderProgram> shaderProgram;
slang::ProgramLayout* slangReflection;
GFX_CHECK_CALL_ABORT(precompileProgram(device, memoryFileSystem.get(), "precompiled-module-imported"));
GFX_CHECK_CALL_ABORT(precompileProgram(device, memoryFileSystem.get(), "precompiled-module-imported", precompileToTarget));

// Next, load the precompiled slang program.
Slang::ComPtr<slang::ISession> slangSession;
Expand Down Expand Up @@ -168,11 +183,26 @@ namespace gfx_test
Slang::makeArray<float>(3.0f, 3.0f, 3.0f, 3.0f));
}

void precompiledModule2TestImpl(IDevice* device, UnitTestContext* context)
{
precompiledModule2TestImplCommon(device, context, false);
}

void precompiledTargetModule2TestImpl(IDevice* device, UnitTestContext* context)
{
precompiledModule2TestImplCommon(device, context, true);
}

SLANG_UNIT_TEST(precompiledModule2D3D12)
{
runTestImpl(precompiledModule2TestImpl, unitTestContext, Slang::RenderApiFlag::D3D12);
}

SLANG_UNIT_TEST(precompiledTargetModule2D3D12)
{
runTestImpl(precompiledTargetModule2TestImpl, unitTestContext, Slang::RenderApiFlag::D3D12);
}

SLANG_UNIT_TEST(precompiledModule2Vulkan)
{
runTestImpl(precompiledModule2TestImpl, unitTestContext, Slang::RenderApiFlag::Vulkan);
Expand Down
Loading