From 97dccb4c61645a14f541e7bceba3ba1599efb8ba Mon Sep 17 00:00:00 2001 From: Ellie Hermaszewska Date: Tue, 8 Aug 2023 13:23:06 +0800 Subject: Add spirv-dis as a downstream compiler (#3059) * Add spirv-dis as a downstream compiler * Add TODO for spirv-dis downstream compiler * Do not use SpirvDis by default * tabs to spaces * regenerate vs projects * correct test * correct calling convention --------- Co-authored-by: Yong He --- .../slang-downstream-compiler-util.cpp | 2 + source/compiler-core/slang-spirv-dis-compiler.cpp | 88 ++++++++++++++++++++++ source/compiler-core/slang-spirv-dis-compiler.h | 26 +++++++ 3 files changed, 116 insertions(+) create mode 100644 source/compiler-core/slang-spirv-dis-compiler.cpp create mode 100644 source/compiler-core/slang-spirv-dis-compiler.h (limited to 'source/compiler-core') diff --git a/source/compiler-core/slang-downstream-compiler-util.cpp b/source/compiler-core/slang-downstream-compiler-util.cpp index d00c3fecb..44d750be8 100644 --- a/source/compiler-core/slang-downstream-compiler-util.cpp +++ b/source/compiler-core/slang-downstream-compiler-util.cpp @@ -23,6 +23,7 @@ #include "slang-dxc-compiler.h" #include "slang-glslang-compiler.h" #include "slang-llvm-compiler.h" +#include "slang-spirv-dis-compiler.h" namespace Slang { @@ -327,6 +328,7 @@ DownstreamCompilerMatchVersion DownstreamCompilerUtil::getCompiledVersion() outFuncs[int(SLANG_PASS_THROUGH_FXC)] = &FXCDownstreamCompilerUtil::locateCompilers; outFuncs[int(SLANG_PASS_THROUGH_GLSLANG)] = &GlslangDownstreamCompilerUtil::locateCompilers; outFuncs[int(SLANG_PASS_THROUGH_LLVM)] = &LLVMDownstreamCompilerUtil::locateCompilers; + outFuncs[int(SLANG_PASS_THROUGH_SPIRV_DIS)] = &SPIRVDisDownstreamCompilerUtil::locateCompilers; } static String _getParentPath(const String& path) diff --git a/source/compiler-core/slang-spirv-dis-compiler.cpp b/source/compiler-core/slang-spirv-dis-compiler.cpp new file mode 100644 index 000000000..04e5c8e4a --- /dev/null +++ b/source/compiler-core/slang-spirv-dis-compiler.cpp @@ -0,0 +1,88 @@ +#include "slang-spirv-dis-compiler.h" + +#include "../core/slang-common.h" +#include "slang-artifact-representation.h" +#include "slang-artifact-util.h" + +namespace Slang +{ + +SlangResult SPIRVDisDownstreamCompilerUtil::locateCompilers( + const String&, + ISlangSharedLibraryLoader*, + DownstreamCompilerSet* set) +{ + // TODO: We could check that the compiler is actually present in PATH (or + // explicitly given) + + ComPtr com( + new SPIRVDisDownstreamCompiler(DownstreamCompilerDesc(SLANG_PASS_THROUGH_SPIRV_DIS))); + set->addCompiler(com); + + return SLANG_OK; +} + +SlangResult SLANG_MCALL SPIRVDisDownstreamCompiler::convert( + IArtifact* from, + const ArtifactDesc& to, + IArtifact** outArtifact) noexcept +{ + const auto& fromDesc = from->getDesc(); + if(to.kind != ArtifactKind::Assembly || + to.payload != ArtifactPayload::SPIRV || + to.flags != 0 || + fromDesc.kind != ArtifactKind::Executable || + fromDesc.payload != ArtifactPayload::SPIRV || + fromDesc.flags != 0) + return SLANG_E_INVALID_ARG; + + ISlangBlob* fromBlob; + SLANG_RETURN_ON_FAIL(from->loadBlob(ArtifactKeep::No, &fromBlob)); + + // Set up our process + CommandLine commandLine; + commandLine.m_executableLocation.setName("spirv-dis"); + RefPtr p; + SLANG_RETURN_ON_FAIL(Process::create(commandLine, 0, p)); + const auto in = p->getStream(StdStreamType::In); + const auto out = p->getStream(StdStreamType::Out); + const auto err = p->getStream(StdStreamType::ErrorOut); + + // Write the assembly + SLANG_RETURN_ON_FAIL(in->write(fromBlob->getBufferPointer(), fromBlob->getBufferSize())); + in->close(); + + // Wait for it to finish + if(!p->waitForTermination(1000)) + return SLANG_FAIL; + + // TODO: allow inheriting stderr in Process + List errData; + SLANG_RETURN_ON_FAIL(StreamUtil::readAll(err, 0, errData)); + fwrite(errData.getBuffer(), errData.getCount(), 1, stderr); + + const auto ret = p->getReturnValue(); + if(ret != 0) + return SLANG_FAIL; + + // Read the disassembly + List outData; + SLANG_RETURN_ON_FAIL(StreamUtil::readAll(out, 0, outData)); + + // Wobble it into an artifact + ComPtr outBlob = RawBlob::create(outData.getBuffer(), outData.getCount()); + auto artifact = ArtifactUtil::createArtifact(to); + artifact->addRepresentationUnknown(outBlob.detach()); + *outArtifact = artifact.detach(); + + return SLANG_OK; +} + +SlangResult SLANG_MCALL SPIRVDisDownstreamCompiler::compile( + const CompileOptions&, + IArtifact**) noexcept +{ + SLANG_UNIMPLEMENTED_X(__func__); +} + +} diff --git a/source/compiler-core/slang-spirv-dis-compiler.h b/source/compiler-core/slang-spirv-dis-compiler.h new file mode 100644 index 000000000..fb01627cd --- /dev/null +++ b/source/compiler-core/slang-spirv-dis-compiler.h @@ -0,0 +1,26 @@ +#pragma once + +#include "slang-downstream-compiler-util.h" + +#include "../core/slang-platform.h" + +namespace Slang +{ + +struct SPIRVDisDownstreamCompilerUtil +{ + static SlangResult locateCompilers(const String& path, ISlangSharedLibraryLoader* loader, DownstreamCompilerSet* set); +}; + +class SPIRVDisDownstreamCompiler : public DownstreamCompilerBase +{ +public: + SPIRVDisDownstreamCompiler(const Desc& desc) : DownstreamCompilerBase(desc) {} + + virtual SlangResult SLANG_MCALL convert(IArtifact* from, const ArtifactDesc& to, IArtifact** outArtifact) noexcept override; + + virtual bool SLANG_MCALL isFileBased() noexcept override { return true; } + virtual SlangResult SLANG_MCALL compile(const CompileOptions& options, IArtifact** outArtifact) noexcept override; +}; + +} -- cgit v1.2.3