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> --- tools/slang-unit-test/unit-test-slang-vm.cpp | 102 +++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 tools/slang-unit-test/unit-test-slang-vm.cpp (limited to 'tools/slang-unit-test') diff --git a/tools/slang-unit-test/unit-test-slang-vm.cpp b/tools/slang-unit-test/unit-test-slang-vm.cpp new file mode 100644 index 000000000..0f5e0f9f3 --- /dev/null +++ b/tools/slang-unit-test/unit-test-slang-vm.cpp @@ -0,0 +1,102 @@ +// unit-test-slang-vm.cpp + +#include "core/slang-memory-file-system.h" +#include "slang-com-ptr.h" +#include "slang.h" +#include "unit-test/slang-unit-test.h" + +#include +#include + +using namespace Slang; + +SLANG_UNIT_TEST(slangVM) +{ + const char* testSource = R"( + int one() { return 1; } + int sum(int x) + { + int result = 0; + for (int i = 0; i <= x; i++) + { + result += i; + } + return result + one(); + } + [shader("dispatch")] + int dispatchMain(uniform int2 v, out int c) + { + int a = v.x; + int b = v.y; + int tmp = 0; + if (a > 0) + tmp = a + b; + else + tmp = b - a; + tmp += sum(b); + c = tmp; + return 100; + } + )"; + + // Create Slang session and compile code. + ComPtr code; + String disasmText; + { + ComPtr globalSession; + SLANG_CHECK( + slang_createGlobalSession(SLANG_API_VERSION, globalSession.writeRef()) == SLANG_OK); + slang::TargetDesc targetDesc = {}; + targetDesc.format = SLANG_HOST_VM; + slang::SessionDesc sessionDesc = {}; + sessionDesc.targetCount = 1; + sessionDesc.targets = &targetDesc; + sessionDesc.compilerOptionEntryCount = 0; + + ComPtr session; + SLANG_CHECK(globalSession->createSession(sessionDesc, session.writeRef()) == SLANG_OK); + + ComPtr diagnosticBlob; + auto module = session->loadModuleFromSourceString( + "test", + "test.slang", + testSource, + diagnosticBlob.writeRef()); + SLANG_CHECK(module != nullptr); + + ComPtr linkedProgram; + module->link(linkedProgram.writeRef()); + + + linkedProgram->getTargetCode(0, code.writeRef(), diagnosticBlob.writeRef()); + + SLANG_CHECK(code->getBufferSize() > 0); + + ComPtr disasmBlob; + SLANG_CHECK(slang_disassembleByteCode(code, disasmBlob.writeRef()) == SLANG_OK); + disasmText = (const char*)disasmBlob->getBufferPointer(); + SLANG_CHECK(disasmText.indexOf("ret") != -1); + } + + // Create a byte code runner and interpret the code. + ComPtr runner; + slang::ByteCodeRunnerDesc runnerDesc = {}; + SLANG_CHECK(slang_createByteCodeRunner(&runnerDesc, runner.writeRef()) == SLANG_OK); + SLANG_CHECK(runner->loadModule(code) == SLANG_OK); + SLANG_CHECK(runner->selectFunctionByIndex(0) == SLANG_OK); + struct Params + { + int a; + int b; + int* result; + }; + int result = 0; + Params params = {1, 2, &result}; + SLANG_CHECK(runner->execute(¶ms, sizeof(params)) == SLANG_OK); + SLANG_CHECK(result == 7); + + size_t returnValSize = 0; + int* returnVal = (int*)runner->getReturnValue(&returnValSize); + SLANG_CHECK(returnValSize == sizeof(int)); + SLANG_CHECK(*returnVal == 100); +} -- cgit v1.2.3