Skip to content

Commit

Permalink
Add linkage attributes to exported spirv functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cheneym2 committed Sep 3, 2024
1 parent 68e39b6 commit 6ea3a1b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 9 deletions.
6 changes: 3 additions & 3 deletions include/slang.h
Original file line number Diff line number Diff line change
Expand Up @@ -958,8 +958,8 @@ extern "C"
// precompiled modules if it is up-to-date with its source.

EmbedDXIL, // bool
EmbedSPIRV, // bool
ForceDXLayout, // bool
EmbedSPIRV, // bool
CountOf,
};

Expand Down Expand Up @@ -4932,9 +4932,9 @@ namespace slang

virtual SLANG_NO_THROW void SLANG_MCALL setEmbedDXIL(bool value) = 0;

virtual SLANG_NO_THROW void SLANG_MCALL setEmbedSPIRV(bool value) = 0;

virtual SLANG_NO_THROW void SLANG_MCALL setTargetForceDXLayout(int targetIndex, bool value) = 0;

virtual SLANG_NO_THROW void SLANG_MCALL setEmbedSPIRV(bool value) = 0;
};

#define SLANG_UUID_ICompileRequest ICompileRequest::getTypeGuid()
Expand Down
27 changes: 27 additions & 0 deletions source/slang/slang-emit-spirv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3796,6 +3796,17 @@ struct SPIRVEmitContext
}
break;
}
case kIROp_DownstreamModuleExportDecoration:
{
requireSPIRVCapability(SpvCapabilityLinkage);
auto name = decoration->getParent()->findDecoration<IRExportDecoration>()->getMangledName();
emitInst(getSection(SpvLogicalSectionID::Annotations),
decoration,
SpvOpDecorate,
dstID,
SpvDecorationLinkageAttributes, name, SpvLinkageTypeExport);
break;
}
// ...
}

Expand Down Expand Up @@ -6710,12 +6721,28 @@ SlangResult emitSPIRVFromIR(
#endif

auto shouldPreserveParams = codeGenContext->getTargetProgram()->getOptionSet().getBoolOption(CompilerOptionName::PreserveParameters);
auto generateWholeProgram = codeGenContext->getTargetProgram()->getOptionSet().getBoolOption(CompilerOptionName::GenerateWholeProgram);
for (auto inst : irModule->getGlobalInsts())
{
if (as<IRDebugSource>(inst))
{
context.ensureInst(inst);

}
if (shouldPreserveParams && as<IRGlobalParam>(inst))
{
context.ensureInst(inst);
}
if (generateWholeProgram)
{
if (auto func = as<IRFunc>(inst))
{
if (func->findDecoration<IRDownstreamModuleExportDecoration>())
{
context.ensureInst(inst);
}
}
}
}

// Emit source language info.
Expand Down
23 changes: 20 additions & 3 deletions source/slang/slang-emit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,24 @@ static void unexportNonEmbeddableDXIL(IRModule* irModule)

static void unexportNonEmbeddableSPIRV(IRModule* irModule)
{
SLANG_UNUSED(irModule);
return;
for (auto inst : irModule->getGlobalInsts())
{
if (inst->getOp() == kIROp_Func)
{
// SPIR-V does not allow exporting entry points
if (inst->findDecoration<IREntryPointDecoration>())
{
if (auto dec = inst->findDecoration<IRPublicDecoration>())
{
dec->removeAndDeallocate();
}
if (auto dec = inst->findDecoration<IRDownstreamModuleExportDecoration>())
{
dec->removeAndDeallocate();
}
}
}
}
}

Result linkAndOptimizeIR(
Expand Down Expand Up @@ -1504,7 +1520,8 @@ Result linkAndOptimizeIR(
}
else if (targetProgram->getOptionSet().getBoolOption(CompilerOptionName::EmbedSPIRV))
{
// TBD what limitation SPIRV has.
// Honor SPIR-V library restrictions for embedded precompilation, such as
// not exporting entrypoints.
unexportNonEmbeddableSPIRV(irModule);
}

Expand Down
8 changes: 5 additions & 3 deletions tests/library/precompiled-module-library-resource.slang
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// precompiled-module-library-resource.slang

// Compile this library source with -embed-dxil option. Tests that modules can be
// precompiled to dxil despite having resource parameters or return types.
// Compile this library source with -embed-dxil and -embed-spirv options.
// Tests that modules can be precompiled to dxil despite having resource
// parameters or return types.

//TEST(windows):COMPILE: tests/library/precompiled-module-library-resource.slang -o tests/library/precompiled-module-library-resource.slang-module -embed-dxil -profile lib_6_6 -incomplete-library
//TEST(windows):COMPILE: tests/library/precompiled-module-library-resource.slang -o tests/library/precompiled-module-library-resource-dxil.slang-module -embed-dxil -profile lib_6_6 -incomplete-library
//TEST:COMPILE: tests/library/precompiled-module-library-resource.slang -o tests/library/precompiled-module-library-resource-spv.slang-module -embed-spirv -incomplete-library

module "precompiled-module-library-resource";

Expand Down

0 comments on commit 6ea3a1b

Please sign in to comment.