Skip to content

Commit 49e912a

Browse files
csyongheslangbot
andauthored
Fix entrypoint auto discovery logic. (shader-slang#5885)
* Fix entrypoint auto discovery logic. * format code --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com>
1 parent 7ffc69d commit 49e912a

File tree

2 files changed

+71
-13
lines changed

2 files changed

+71
-13
lines changed

source/slang/slang.cpp

+15-13
Original file line numberDiff line numberDiff line change
@@ -5171,22 +5171,24 @@ IArtifact* ComponentType::getTargetArtifact(Int targetIndex, slang::IBlob** outD
51715171
entryPointsDiscovered = true;
51725172
}
51735173
}
5174-
// If no entry points were discovered, then we should return nullptr.
5175-
if (!entryPointsDiscovered)
5176-
{
5177-
return nullptr;
5178-
}
51795174

5180-
RefPtr<CompositeComponentType> composite = new CompositeComponentType(linkage, components);
5181-
ComPtr<IComponentType> linkedComponentType;
5182-
SLANG_RETURN_NULL_ON_FAIL(composite->link(linkedComponentType.writeRef(), outDiagnostics));
5183-
auto targetArtifact = static_cast<ComponentType*>(linkedComponentType.get())
5184-
->getTargetArtifact(targetIndex, outDiagnostics);
5185-
if (targetArtifact)
5175+
// If any entry points were discovered, then we should emit the program with entrypoints
5176+
// linked.
5177+
if (entryPointsDiscovered)
51865178
{
5187-
m_targetArtifacts[targetIndex] = targetArtifact;
5179+
RefPtr<CompositeComponentType> composite =
5180+
new CompositeComponentType(linkage, components);
5181+
ComPtr<IComponentType> linkedComponentType;
5182+
SLANG_RETURN_NULL_ON_FAIL(
5183+
composite->link(linkedComponentType.writeRef(), outDiagnostics));
5184+
auto targetArtifact = static_cast<ComponentType*>(linkedComponentType.get())
5185+
->getTargetArtifact(targetIndex, outDiagnostics);
5186+
if (targetArtifact)
5187+
{
5188+
m_targetArtifacts[targetIndex] = targetArtifact;
5189+
}
5190+
return targetArtifact;
51885191
}
5189-
return targetArtifact;
51905192
}
51915193

51925194
auto target = linkage->targets[targetIndex];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// unit-test-cuda-compile.cpp
2+
3+
#include "../../source/core/slang-io.h"
4+
#include "../../source/core/slang-process.h"
5+
#include "slang-com-ptr.h"
6+
#include "slang.h"
7+
#include "unit-test/slang-unit-test.h"
8+
9+
#include <stdio.h>
10+
#include <stdlib.h>
11+
12+
using namespace Slang;
13+
14+
// Test that the compilation API can be used to produce CUDA source.
15+
16+
SLANG_UNIT_TEST(CudaCompile)
17+
{
18+
// Source for a module that contains an undecorated entrypoint.
19+
const char* userSourceBody = R"(
20+
[CudaDeviceExport]
21+
float testExportedFunc(float3 particleRayOrigin)
22+
{
23+
return dot(particleRayOrigin,particleRayOrigin);
24+
};
25+
)";
26+
27+
auto moduleName = "moduleG" + String(Process::getId());
28+
ComPtr<slang::IGlobalSession> globalSession;
29+
SLANG_CHECK(slang_createGlobalSession(SLANG_API_VERSION, globalSession.writeRef()) == SLANG_OK);
30+
slang::TargetDesc targetDesc = {};
31+
targetDesc.format = SLANG_CUDA_SOURCE;
32+
slang::SessionDesc sessionDesc = {};
33+
sessionDesc.targetCount = 1;
34+
sessionDesc.targets = &targetDesc;
35+
ComPtr<slang::ISession> session;
36+
SLANG_CHECK(globalSession->createSession(sessionDesc, session.writeRef()) == SLANG_OK);
37+
38+
ComPtr<slang::IBlob> diagnosticBlob;
39+
auto module = session->loadModuleFromSourceString(
40+
"m",
41+
"m.slang",
42+
userSourceBody,
43+
diagnosticBlob.writeRef());
44+
SLANG_CHECK(module != nullptr);
45+
46+
ComPtr<slang::IComponentType> linkedProgram;
47+
module->link(linkedProgram.writeRef(), diagnosticBlob.writeRef());
48+
SLANG_CHECK(linkedProgram != nullptr);
49+
50+
ComPtr<slang::IBlob> code;
51+
linkedProgram->getTargetCode(0, code.writeRef(), diagnosticBlob.writeRef());
52+
SLANG_CHECK(code != nullptr);
53+
SLANG_CHECK(code->getBufferSize() != 0);
54+
String text = String((char*)code->getBufferPointer());
55+
SLANG_CHECK(text.indexOf("testExportedFunc") > 0);
56+
}

0 commit comments

Comments
 (0)