Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix precompiledTargetModule tests #6253

Closed
wants to merge 23 commits into from
Closed
1 change: 1 addition & 0 deletions source/slang/slang-compiler-tu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ SLANG_NO_THROW SlangResult SLANG_MCALL ComponentType::getModuleDependency(
{
return SLANG_E_INVALID_ARG;
}
getModuleDependencies()[dependencyIndex]->addRef();
*outModule = getModuleDependencies()[dependencyIndex];
return SLANG_OK;
}
Expand Down
1 change: 1 addition & 0 deletions source/slang/slang-compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1722,6 +1722,7 @@ SlangResult CodeGenContext::emitWithDownstreamForEntryPoints(ComPtr<IArtifact>&
}
}
}
// HERE
}
});

Expand Down
31 changes: 31 additions & 0 deletions source/slang/slang-emit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2049,6 +2049,37 @@ SlangResult emitSPIRVForEntryPointsDirectly(
List<uint8_t> spirv, outSpirv;
emitSPIRVFromIR(codeGenContext, irModule, irEntryPoints, spirv);

// Re-link together SPIRV
/*
// Load embedded precompiled libraries from IR into library artifacts
List<ComPtr<IArtifact>> libraries;
program->enumerateIRModules(
[&](IRModule* irModule)
{
for (auto globalInst : irModule->getModuleInst()->getChildren())
{
if (target == CodeGenTarget::SPIRVAssembly || target == CodeGenTarget::SPIRV)
{
if (auto inst = as<IREmbeddedDownstreamIR>(globalInst))
{
if (inst->getTarget() == CodeGenTarget::SPIRV)
{
auto slice = inst->getBlob()->getStringSlice();
ArtifactDesc desc =
ArtifactDescUtil::makeDescForCompileTarget(SLANG_SPIRV);
desc.kind = ArtifactKind::Library;

auto library = ArtifactUtil::createArtifact(desc);

library->addRepresentationUnknown(StringBlob::create(slice));
libraries.add(library);
}
}
}
}
});

*/
#if 0
String optErr;
if (SLANG_FAILED(optimizeSPIRV(spirv, optErr, outSpirv)))
Expand Down
3 changes: 1 addition & 2 deletions tests/expected-failure-github.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ tests/autodiff/custom-intrinsic.slang.2 syn (wgpu)
tests/bugs/buffer-swizzle-store.slang.3 syn (wgpu)
tests/compute/interface-shader-param-in-struct.slang.4 syn (wgpu)
tests/compute/interface-shader-param.slang.5 syn (wgpu)
tests/language-feature/shader-params/interface-shader-param-ordinary.slang.4 syn (wgpu)
gfx-unit-test-tool/precompiledTargetModule2Vulkan.internal
tests/language-feature/shader-params/interface-shader-param-ordinary.slang.4 syn (wgpu)
1 change: 0 additions & 1 deletion tests/expected-failure.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ tests/language-feature/saturated-cooperation/fuse.slang (vk)
tests/bugs/byte-address-buffer-interlocked-add-f32.slang (vk)
tests/ir/loop-unroll-0.slang.1 (vk)
tests/hlsl-intrinsic/texture/float-atomics.slang (vk)
gfx-unit-test-tool/precompiledTargetModule2Vulkan.internal
1 change: 1 addition & 0 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ if(SLANG_ENABLE_GFX)
$<$<BOOL:${SLANG_ENABLE_XLIB}>:X11::X11>
$<$<BOOL:${SLANG_ENABLE_CUDA}>:CUDA::cuda_driver>
$<$<BOOL:${SLANG_ENABLE_NVAPI}>:${NVAPI_LIBRARIES}>
SPIRV-Tools-link
LINK_WITH_FRAMEWORK Foundation Cocoa QuartzCore Metal
EXTRA_COMPILE_DEFINITIONS_PRIVATE
$<$<BOOL:${SLANG_ENABLE_CUDA}>:GFX_ENABLE_CUDA>
Expand Down
15 changes: 15 additions & 0 deletions tools/gfx-unit-test/gfx-test-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,12 @@ void runTestImpl(
{
SLANG_IGNORE_TEST
}
printf("File, Line: %s, %d\n", __FILE__, __LINE__);
auto device = createTestingDevice(context, api, searchPaths, shaderCache);
printf("File, Line: %s, %d\n", __FILE__, __LINE__);
if (!device)
{
printf("File, Line: %s, %d\n", __FILE__, __LINE__);
SLANG_IGNORE_TEST
}
#if SLANG_WIN32
Expand All @@ -126,21 +129,33 @@ void runTestImpl(
#endif
// Skip d3d11 tests when we don't have DXBC support as they're bound to
// fail without a backend compiler
printf("File, Line: %s, %d\n", __FILE__, __LINE__);
if (api == Slang::RenderApiFlag::D3D11 && !SLANG_ENABLE_DXBC_SUPPORT)
{
printf("File, Line: %s, %d\n", __FILE__, __LINE__);
SLANG_IGNORE_TEST
}
try
{
printf("File, Line: %s, %d\n", __FILE__, __LINE__);
fflush(stdout);
renderDocBeginFrame();
f(device, context);
printf("File, Line: %s, %d\n", __FILE__, __LINE__);
fflush(stdout);
}
catch (AbortTestException& e)
{
printf("EXCEPTION! File, Line: %s, %d\n", __FILE__, __LINE__);
fflush(stdout);
renderDocEndFrame();
throw e;
}
printf("Dun.. File, Line: %s, %d\n", __FILE__, __LINE__);
fflush(stdout);
renderDocEndFrame();
printf("File, Line: %s, %d\n", __FILE__, __LINE__);
fflush(stdout);
}

#define GFX_CHECK_CALL(x) SLANG_CHECK(!SLANG_FAILED(x))
Expand Down
88 changes: 80 additions & 8 deletions tools/gfx-unit-test/precompiled-module-2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ static Slang::Result precompileProgram(
const char* shaderModuleName,
bool precompileToTarget)
{
printf("File, Line: %s, %d\n", __FILE__, __LINE__);
fflush(stdout);
fflush(stderr);
Slang::ComPtr<slang::ISession> slangSession;
SLANG_RETURN_ON_FAIL(device->getSlangSession(slangSession.writeRef()));
slang::SessionDesc sessionDesc = {};
Expand All @@ -27,7 +30,9 @@ static Slang::Result precompileProgram(
sessionDesc.searchPaths = searchPaths.getBuffer();
auto globalSession = slangSession->getGlobalSession();
globalSession->createSession(sessionDesc, slangSession.writeRef());

printf("File, Line: %s, %d\n", __FILE__, __LINE__);
fflush(stdout);
fflush(stderr);
slang::IModule* module;
{
Slang::ComPtr<slang::IBlob> diagnosticsBlob;
Expand All @@ -36,7 +41,9 @@ static Slang::Result precompileProgram(
}
if (!module)
return SLANG_FAIL;

printf("File, Line: %s, %d\n", __FILE__, __LINE__);
fflush(stdout);
fflush(stderr);
if (precompileToTarget)
{
SlangCompileTarget target;
Expand All @@ -62,7 +69,7 @@ static Slang::Result precompileProgram(
diagnoseIfNeeded(diagnosticsBlob);
}
}

printf("File, Line: %s, %d\n", __FILE__, __LINE__);
// Write loaded modules to memory file system.
for (SlangInt i = 0; i < slangSession->getLoadedModuleCount(); i++)
{
Expand All @@ -76,6 +83,9 @@ static Slang::Result precompileProgram(
fileSys->saveFileBlob((Slang::String(name) + ".slang-module").getBuffer(), outBlob);
}
}
printf("File, Line: %s, %d\n", __FILE__, __LINE__);
fflush(stdout);
fflush(stderr);
return SLANG_OK;
}

Expand All @@ -84,12 +94,15 @@ void precompiledModule2TestImplCommon(
UnitTestContext* context,
bool precompileToTarget)
{
printf("File, Line: %s, %d\n", __FILE__, __LINE__);
Slang::ComPtr<ITransientResourceHeap> transientHeap;
ITransientResourceHeap::Desc transientHeapDesc = {};
transientHeapDesc.constantBufferSize = 4096;
GFX_CHECK_CALL_ABORT(
device->createTransientResourceHeap(transientHeapDesc, transientHeap.writeRef()));

printf("File, Line: %s, %d\n", __FILE__, __LINE__);
fflush(stdout);
fflush(stderr);
// First, load and compile the slang source.
ComPtr<ISlangMutableFileSystem> memoryFileSystem =
ComPtr<ISlangMutableFileSystem>(new Slang::MemoryFileSystem());
Expand All @@ -108,6 +121,7 @@ void precompiledModule2TestImplCommon(
slang::SessionDesc sessionDesc = {};
sessionDesc.targetCount = 1;
slang::TargetDesc targetDesc = {};
printf("File, Line: %s, %d\n", __FILE__, __LINE__);
switch (device->getDeviceInfo().deviceType)
{
case gfx::DeviceType::DirectX12:
Expand All @@ -119,11 +133,14 @@ void precompiledModule2TestImplCommon(
targetDesc.profile = device->getSlangSession()->getGlobalSession()->findProfile("GLSL_460");
break;
}
printf("File, Line: %s, %d\n", __FILE__, __LINE__);
fflush(stdout);
fflush(stderr);
sessionDesc.targets = &targetDesc;
sessionDesc.fileSystem = memoryFileSystem.get();
auto globalSession = slangSession->getGlobalSession();
globalSession->createSession(sessionDesc, slangSession.writeRef());

printf("File, Line: %s, %d\n", __FILE__, __LINE__);
const char* moduleSrc = R"(
import "precompiled-module-imported";

Expand All @@ -141,19 +158,28 @@ void precompiledModule2TestImplCommon(
}
)";
memoryFileSystem->saveFile("precompiled-module.slang", moduleSrc, strlen(moduleSrc));
printf("File, Line: %s, %d\n", __FILE__, __LINE__);
fflush(stdout);
fflush(stderr);
GFX_CHECK_CALL_ABORT(loadComputeProgram(
device,
slangSession,
shaderProgram,
"precompiled-module",
"computeMain",
slangReflection));

printf("File, Line: %s, %d\n", __FILE__, __LINE__);
ComputePipelineStateDesc pipelineDesc = {};
pipelineDesc.program = shaderProgram.get();
ComPtr<gfx::IPipelineState> pipelineState;
printf("File, Line: %s, %d\n", __FILE__, __LINE__);
fflush(stdout);
fflush(stderr);
GFX_CHECK_CALL_ABORT(
device->createComputePipelineState(pipelineDesc, pipelineState.writeRef()));
printf("File, Line: %s, %d\n", __FILE__, __LINE__);
fflush(stdout);
fflush(stderr);

const int numberCount = 4;
float initialData[] = {0.0f, 0.0f, 0.0f, 0.0f};
Expand All @@ -170,15 +196,25 @@ void precompiledModule2TestImplCommon(
bufferDesc.memoryType = MemoryType::DeviceLocal;

ComPtr<IBufferResource> numbersBuffer;
printf("File, Line: %s, %d\n", __FILE__, __LINE__);
fflush(stdout);
fflush(stderr);

GFX_CHECK_CALL_ABORT(
device->createBufferResource(bufferDesc, (void*)initialData, numbersBuffer.writeRef()));
printf("File, Line: %s, %d\n", __FILE__, __LINE__);
fflush(stdout);
fflush(stderr);

ComPtr<IResourceView> bufferView;
IResourceView::Desc viewDesc = {};
viewDesc.type = IResourceView::Type::UnorderedAccess;
viewDesc.format = Format::Unknown;
GFX_CHECK_CALL_ABORT(
device->createBufferView(numbersBuffer, nullptr, viewDesc, bufferView.writeRef()));
printf("File, Line: %s, %d\n", __FILE__, __LINE__);
fflush(stdout);
fflush(stderr);

// We have done all the set up work, now it is time to start recording a command buffer for
// GPU execution.
Expand All @@ -202,18 +238,32 @@ void precompiledModule2TestImplCommon(
queue->executeCommandBuffer(commandBuffer);
queue->waitOnHost();
}
printf("File, Line: %s, %d\n", __FILE__, __LINE__);
fflush(stdout);
fflush(stderr);

compareComputeResult(device, numbersBuffer, Slang::makeArray<float>(3.0f, 3.0f, 3.0f, 3.0f));
printf("File, Line: %s, %d\n", __FILE__, __LINE__);
fflush(stdout);
fflush(stderr);
}

void precompiledModule2TestImpl(IDevice* device, UnitTestContext* context)
{
printf("File, Line: %s, %d\n", __FILE__, __LINE__);
fflush(stdout);
precompiledModule2TestImplCommon(device, context, false);
printf("File, Line: %s, %d\n", __FILE__, __LINE__);
fflush(stdout);
}

void precompiledTargetModule2TestImpl(IDevice* device, UnitTestContext* context)
{
precompiledModule2TestImplCommon(device, context, true);
printf("File, Line: %s, %d\n", __FILE__, __LINE__);
fflush(stdout);
precompiledModule2TestImplCommon(device, context, false);
printf("File, Line: %s, %d\n", __FILE__, __LINE__);
fflush(stdout);
}

SLANG_UNIT_TEST(precompiledModule2D3D12)
Expand All @@ -223,6 +273,7 @@ SLANG_UNIT_TEST(precompiledModule2D3D12)

SLANG_UNIT_TEST(precompiledTargetModule2D3D12)
{
printf("Starting precompiledTargetModule2D3D12\n");
runTestImpl(precompiledTargetModule2TestImpl, unitTestContext, Slang::RenderApiFlag::D3D12);
}

Expand All @@ -233,7 +284,28 @@ SLANG_UNIT_TEST(precompiledModule2Vulkan)

SLANG_UNIT_TEST(precompiledTargetModule2Vulkan)
{
runTestImpl(precompiledTargetModule2TestImpl, unitTestContext, Slang::RenderApiFlag::Vulkan);
// catch all exceptions
try
{
printf("Starting precompiledTargetModule2Vulkan\n");
fflush(stdout);
runTestImpl(
precompiledTargetModule2TestImpl,
unitTestContext,
Slang::RenderApiFlag::Vulkan);
printf("Finished precompiledTargetModule2Vulkan\n");
fflush(stdout);
}
catch (const std::exception&)
{
printf("Caught exception\n");
fflush(stdout);
}
catch (...)
{
printf("Caught unknown exception\n");
fflush(stdout);
}
}

} // namespace gfx_test
6 changes: 3 additions & 3 deletions tools/gfx/d3d12/d3d12-shader-program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ using namespace Slang;

Result ShaderProgramImpl::createShaderModule(
slang::EntryPointReflection* entryPointInfo,
ComPtr<ISlangBlob> kernelCode)
List<ComPtr<ISlangBlob>> kernelCodes)
{
ShaderBinary shaderBin;
shaderBin.stage = entryPointInfo->getStage();
shaderBin.entryPointInfo = entryPointInfo;
shaderBin.code.addRange(
reinterpret_cast<const uint8_t*>(kernelCode->getBufferPointer()),
(Index)kernelCode->getBufferSize());
reinterpret_cast<const uint8_t*>(kernelCodes[0]->getBufferPointer()),
(Index)kernelCodes[0]->getBufferSize());
m_shaders.add(_Move(shaderBin));
return SLANG_OK;
}
Expand Down
2 changes: 1 addition & 1 deletion tools/gfx/d3d12/d3d12-shader-program.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ShaderProgramImpl : public ShaderProgramBase

virtual Result createShaderModule(
slang::EntryPointReflection* entryPointInfo,
ComPtr<ISlangBlob> kernelCode) override;
List<ComPtr<ISlangBlob>> kernelCodes) override;
};

} // namespace d3d12
Expand Down
8 changes: 4 additions & 4 deletions tools/gfx/metal/metal-shader-program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ ShaderProgramImpl::~ShaderProgramImpl() {}

Result ShaderProgramImpl::createShaderModule(
slang::EntryPointReflection* entryPointInfo,
ComPtr<ISlangBlob> kernelCode)
List<ComPtr<ISlangBlob>> kernelCodes)
{
Module module;
module.stage = entryPointInfo->getStage();
module.entryPointName = entryPointInfo->getNameOverride();
module.code = kernelCode;
module.code = kernelCodes[0];

dispatch_data_t data = dispatch_data_create(
kernelCode->getBufferPointer(),
kernelCode->getBufferSize(),
kernelCodes[0]->getBufferPointer(),
kernelCodes[0]->getBufferSize(),
dispatch_get_main_queue(),
NULL);
NS::Error* error;
Expand Down
2 changes: 1 addition & 1 deletion tools/gfx/metal/metal-shader-program.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ShaderProgramImpl : public ShaderProgramBase

virtual Result createShaderModule(
slang::EntryPointReflection* entryPointInfo,
ComPtr<ISlangBlob> kernelCode) override;
List<ComPtr<ISlangBlob>> kernelCodes) override;
};


Expand Down
Loading
Loading