From cfb41bb61d63d45aa47ccf9580414545630f0d97 Mon Sep 17 00:00:00 2001 From: Dietrich Geisler Date: Tue, 7 Jul 2020 17:46:02 -0400 Subject: Public Keyword for Functions (#1432) This PR introduces support for the public modifier for functions. This keyword allows labelled functions to be written to the compiled without having a link to an entry point. The goal of this change is to help support heterogeneous design of Slang by permitting C++ code to interact with CPU slang functions. Internally, this PR adds the public decoration to the IR and defines a lowering from the public modifier in the AST to this decoration. Additionally, the Keep Alive decoration is added to any public modifier being lowered, which prevents DCE from eliminating functions labelled with the public keyword. Co-authored-by: Tim Foley --- examples/heterogeneous-hello-world/shader.slang | 4 ++++ source/slang/slang-ir-inst-defs.h | 3 ++- source/slang/slang-ir-insts.h | 1 + source/slang/slang-ir-link.cpp | 16 ++++++++++++++++ source/slang/slang-lower-to-ir.cpp | 4 ++++ 5 files changed, 27 insertions(+), 1 deletion(-) diff --git a/examples/heterogeneous-hello-world/shader.slang b/examples/heterogeneous-hello-world/shader.slang index f650c3481..b36a2d2c7 100644 --- a/examples/heterogeneous-hello-world/shader.slang +++ b/examples/heterogeneous-hello-world/shader.slang @@ -14,3 +14,7 @@ void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) ioBuffer[tid] = o; } + +public int prepMain() { + return 5; +} diff --git a/source/slang/slang-ir-inst-defs.h b/source/slang/slang-ir-inst-defs.h index 85c489528..5a96fd8e8 100644 --- a/source/slang/slang-ir-inst-defs.h +++ b/source/slang/slang-ir-inst-defs.h @@ -474,7 +474,8 @@ INST(HighLevelDeclDecoration, highLevelDecl, 1, 0) INST(VulkanCallablePayloadDecoration, vulkanCallablePayload, 0, 0) INST(EarlyDepthStencilDecoration, earlyDepthStencil, 0, 0) INST(GloballyCoherentDecoration, globallyCoherent, 0, 0) - INST(PreciseDecoration, precise, 0, 0) + INST(PreciseDecoration, precise, 0, 0) + INST(PublicDecoration, public, 0, 0) INST(PatchConstantFuncDecoration, patchConstantFunc, 1, 0) INST(OutputControlPointsDecoration, outputControlPoints, 1, 0) diff --git a/source/slang/slang-ir-insts.h b/source/slang/slang-ir-insts.h index c74dfb4ea..159021412 100644 --- a/source/slang/slang-ir-insts.h +++ b/source/slang/slang-ir-insts.h @@ -220,6 +220,7 @@ IR_SIMPLE_DECORATION(ReadNoneDecoration) IR_SIMPLE_DECORATION(EarlyDepthStencilDecoration) IR_SIMPLE_DECORATION(GloballyCoherentDecoration) IR_SIMPLE_DECORATION(PreciseDecoration) +IR_SIMPLE_DECORATION(PublicDecoration) struct IROutputControlPointsDecoration : IRDecoration diff --git a/source/slang/slang-ir-link.cpp b/source/slang/slang-ir-link.cpp index 7acdac40b..3935eab07 100644 --- a/source/slang/slang-ir-link.cpp +++ b/source/slang/slang-ir-link.cpp @@ -1479,6 +1479,22 @@ LinkedIR linkIR( } } + for (IRModule* irModule : irModules) + { + for (auto inst : irModule->getGlobalInsts()) + { + auto hasPublic = inst->findDecoration(); + if (!hasPublic) + continue; + + auto cloned = cloneValue(context, inst); + if (!cloned->findDecorationImpl(kIROp_KeepAliveDecoration)) + { + context->builder->addKeepAliveDecoration(cloned); + } + } + } + // TODO: *technically* we should consider the case where // we have global variables with initializers, since // these should get run whether or not the entry point diff --git a/source/slang/slang-lower-to-ir.cpp b/source/slang/slang-lower-to-ir.cpp index d14fbfed5..1ba99dffb 100644 --- a/source/slang/slang-lower-to-ir.cpp +++ b/source/slang/slang-lower-to-ir.cpp @@ -6716,6 +6716,10 @@ struct DeclLoweringVisitor : DeclVisitor getBuilder()->addRequireCUDASMVersionDecoration(irFunc, versionMod->version); } + if (decl->findModifier()) { + getBuilder()->addSimpleDecoration(irFunc); + } + if (auto attr = decl->findModifier()) { IRIntLit* intLit = _getIntLitFromAttribute(getBuilder(), attr); -- cgit v1.2.3