summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2022-04-19 12:09:22 -0700
committerGitHub <noreply@github.com>2022-04-19 12:09:22 -0700
commit3d1d692f939d2e23704a90a9cd009194905de5dc (patch)
treef983e5b5c323a5aa63ceda3b5f46d5837d161312 /tools
parentd939773a9127bccbbd22903eb5b5620ad7127d37 (diff)
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 <yhe@nvidia.com> Co-authored-by: Theresa Foley <10618364+tangent-vector@users.noreply.github.com>
Diffstat (limited to 'tools')
-rw-r--r--tools/slang-unit-test/unit-test-find-type-by-name.cpp2
-rw-r--r--tools/slang-unit-test/unit-test-translation-unit-import.cpp59
2 files changed, 60 insertions, 1 deletions
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 <stdio.h>
+#include <stdlib.h>
+
+#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<int> 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<ISlangBlob> outBlob;
+ spGetEntryPointCodeBlob(request, 0, 0, outBlob.writeRef());
+ SLANG_CHECK(outBlob && outBlob->getBufferSize() != 0);
+
+ spDestroyCompileRequest(request);
+ spDestroySession(session);
+}
+