From c39c29bf4c52a85d7c83cc8b66ae45e265f9e078 Mon Sep 17 00:00:00 2001 From: Yong He Date: Mon, 28 Apr 2025 11:42:22 -0700 Subject: Add Slang Byte Code generation and interpreter. (#6896) * Add Slang Byte Code generation and interpreter. * Fix compile issues. * format code * More compile fix. * Fix clang issue. * Fix more clang issues. * Another clang fix. * Fix clang issues. * Fix another clang issue. * Fix wasm build. * Update building.md * Fix test-server. * Fix compile error. * Fix bug. --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> --- source/slang/slang-emit.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'source/slang/slang-emit.cpp') diff --git a/source/slang/slang-emit.cpp b/source/slang/slang-emit.cpp index 9cfa7dcae..aa7387e22 100644 --- a/source/slang/slang-emit.cpp +++ b/source/slang/slang-emit.cpp @@ -15,8 +15,10 @@ #include "slang-emit-glsl.h" #include "slang-emit-hlsl.h" #include "slang-emit-metal.h" +#include "slang-emit-slang.h" #include "slang-emit-source-writer.h" #include "slang-emit-torch.h" +#include "slang-emit-vm.h" #include "slang-emit-wgsl.h" #include "slang-ir-any-value-inference.h" #include "slang-ir-autodiff.h" @@ -116,6 +118,7 @@ #include "slang-syntax.h" #include "slang-type-layout.h" #include "slang-visitor.h" +#include "slang-vm-bytecode.h" #include @@ -825,6 +828,7 @@ Result linkAndOptimizeIR( switch (target) { case CodeGenTarget::HostCPPSource: + case CodeGenTarget::HostVM: break; case CodeGenTarget::CUDASource: collectOptiXEntryPointUniformParams(irModule); @@ -859,6 +863,7 @@ Result linkAndOptimizeIR( case CodeGenTarget::HostCPPSource: case CodeGenTarget::CPPSource: case CodeGenTarget::CUDASource: + case CodeGenTarget::HostVM: break; } @@ -1154,6 +1159,15 @@ Result linkAndOptimizeIR( cleanupGenerics(targetProgram, irModule, sink); dumpIRIfEnabled(codeGenContext, irModule, "AFTER-LOWER-GENERICS"); + // Don't need to run any further target-dependent passes if we are generating code + // for host vm. + if (target == CodeGenTarget::HostVM) + { + performForceInlining(irModule); + simplifyIR(targetProgram, irModule, defaultIRSimplificationOptions, sink); + return SLANG_OK; + } + // After dynamic dispatch logic is resolved into ordinary function calls, // we can now run our stage specialization logic. if (requiredLoweringPassSet.specializeStageSwitch) @@ -2329,4 +2343,47 @@ SlangResult emitSPIRVForEntryPointsDirectly( return SLANG_OK; } +SlangResult emitHostVMCode(CodeGenContext* codeGenContext, ComPtr& outArtifact) +{ + LinkedIR linkedIR; + LinkingAndOptimizationOptions linkingAndOptimizationOptions; + SLANG_RETURN_ON_FAIL( + linkAndOptimizeIR(codeGenContext, linkingAndOptimizationOptions, linkedIR)); + + VMByteCodeBuilder byteCode; + SLANG_RETURN_ON_FAIL(emitVMByteCodeForEntryPoints(codeGenContext, linkedIR, byteCode)); + + String slangDeclaration; + SLANG_RETURN_ON_FAIL( + emitSlangDeclarationsForEntryPoints(codeGenContext, linkedIR, slangDeclaration)); + + slang::SessionDesc sessionDesc = {}; + ComPtr slangSession; + SLANG_RETURN_ON_FAIL( + codeGenContext->getSession()->createSession(sessionDesc, slangSession.writeRef())); + auto linkage = static_cast(slangSession.get()); + + ComPtr diagnostics; + auto module = slangSession->loadModuleFromSource( + "kernel", + "kernel.slang", + StringBlob::create(slangDeclaration), + diagnostics.writeRef()); + if (!module) + return SLANG_FAIL; + RefPtr newModule = new Module(linkage); + newModule->setModuleDecl(static_cast(module)->getModuleDecl()); + newModule->setIRModule(linkedIR.module); + newModule->setName("kernels"); + SLANG_RETURN_ON_FAIL(newModule->serialize(byteCode.kernelBlob.writeRef())); + + ComPtr byteCodeBlob; + SLANG_RETURN_ON_FAIL(byteCode.serialize(byteCodeBlob.writeRef())); + + outArtifact = ArtifactUtil::createArtifactForCompileTarget(SLANG_HOST_VM); + outArtifact->addRepresentationUnknown(byteCodeBlob); + + return SLANG_OK; +} + } // namespace Slang -- cgit v1.2.3