From 3d1d692f939d2e23704a90a9cd009194905de5dc Mon Sep 17 00:00:00 2001 From: Yong He Date: Tue, 19 Apr 2022 12:09:22 -0700 Subject: Make translation units in the same CompileReq visible to `import`. (#2184) * Make translation unitts in the same CompileReq visible to `import`. * Fix code review comments. Co-authored-by: Yong He Co-authored-by: Theresa Foley <10618364+tangent-vector@users.noreply.github.com> --- .../unit-test-find-type-by-name.cpp | 2 +- .../unit-test-translation-unit-import.cpp | 59 ++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 tools/slang-unit-test/unit-test-translation-unit-import.cpp (limited to 'tools') diff --git a/tools/slang-unit-test/unit-test-find-type-by-name.cpp b/tools/slang-unit-test/unit-test-find-type-by-name.cpp index d12a0d91f..87be156db 100644 --- a/tools/slang-unit-test/unit-test-find-type-by-name.cpp +++ b/tools/slang-unit-test/unit-test-find-type-by-name.cpp @@ -1,4 +1,4 @@ -// unit-test-byte-encode.cpp +// unit-test-find-type-by-name.cpp #include "../../slang.h" diff --git a/tools/slang-unit-test/unit-test-translation-unit-import.cpp b/tools/slang-unit-test/unit-test-translation-unit-import.cpp new file mode 100644 index 000000000..dfa75eb9f --- /dev/null +++ b/tools/slang-unit-test/unit-test-translation-unit-import.cpp @@ -0,0 +1,59 @@ +// unit-test-translation-unit-import.cpp + +#include "../../slang.h" + +#include +#include + +#include "tools/unit-test/slang-unit-test.h" +#include "../../slang-com-ptr.h" + +using namespace Slang; + +// Test that the API supports discovering previously checked translation unit in the same +// FrontEndCompileRequest. +SLANG_UNIT_TEST(translationUnitImport) +{ + // Source for the first translation unit. + const char* generatedSource = + "int f() {" + " return 5;" + "};"; + + // Source for the second translation unit that imports the first translation unit. + // The import should succeed and `f` should be visible to this module. + const char* userSource = + R"( + import generatedUnit; + + [shader("compute")] + [numthreads(4,1,1)] + void computeMain( + uint3 sv_dispatchThreadID : SV_DispatchThreadID, + uniform RWStructuredBuffer buffer) + { + buffer[sv_dispatchThreadID.x] = f(); + } + )"; + auto session = spCreateSession(); + auto request = spCreateCompileRequest(session); + spAddCodeGenTarget(request, SLANG_HLSL); + int generatedTranslationUnitIndex = spAddTranslationUnit(request, SLANG_SOURCE_LANGUAGE_SLANG, "generatedUnit"); + spAddTranslationUnitSourceString( + request, generatedTranslationUnitIndex, "generatedFile", generatedSource); + int entryPointTranslationUnitIndex = spAddTranslationUnit(request, SLANG_SOURCE_LANGUAGE_SLANG, "userUnit"); + spAddTranslationUnitSourceString( + request, entryPointTranslationUnitIndex, "userFile", userSource); + spAddEntryPoint(request, entryPointTranslationUnitIndex, "computeMain", SLANG_STAGE_COMPUTE); + + auto compileResult = spCompile(request); + SLANG_CHECK(compileResult == SLANG_OK); + + Slang::ComPtr outBlob; + spGetEntryPointCodeBlob(request, 0, 0, outBlob.writeRef()); + SLANG_CHECK(outBlob && outBlob->getBufferSize() != 0); + + spDestroyCompileRequest(request); + spDestroySession(session); +} + -- cgit v1.2.3