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

Use disassemble API from SPIRV-Tools #6001

Merged
10 changes: 10 additions & 0 deletions source/compiler-core/slang-downstream-compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,9 @@ class IDownstreamCompiler : public ICastable
/// Validate and return the result
virtual SLANG_NO_THROW SlangResult SLANG_MCALL
validate(const uint32_t* contents, int contentsSize) = 0;
/// Disassemble and print to stdout
virtual SLANG_NO_THROW SlangResult SLANG_MCALL
disassemble(const uint32_t* contents, int contentsSize) = 0;

/// True if underlying compiler uses file system to communicate source
virtual SLANG_NO_THROW bool SLANG_MCALL isFileBased() = 0;
Expand Down Expand Up @@ -374,6 +377,13 @@ class DownstreamCompilerBase : public ComBaseObject, public IDownstreamCompiler
SLANG_UNUSED(contentsSize);
return SLANG_FAIL;
}
virtual SLANG_NO_THROW SlangResult SLANG_MCALL
disassemble(const uint32_t* contents, int contentsSize) SLANG_OVERRIDE
{
SLANG_UNUSED(contents);
SLANG_UNUSED(contentsSize);
return SLANG_FAIL;
}

DownstreamCompilerBase(const Desc& desc)
: m_desc(desc)
Expand Down
20 changes: 19 additions & 1 deletion source/compiler-core/slang-glslang-compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class GlslangDownstreamCompiler : public DownstreamCompilerBase
SLANG_OVERRIDE;
virtual SLANG_NO_THROW SlangResult SLANG_MCALL
validate(const uint32_t* contents, int contentsSize) SLANG_OVERRIDE;
virtual SLANG_NO_THROW SlangResult SLANG_MCALL
disassemble(const uint32_t* contents, int contentsSize) SLANG_OVERRIDE;

/// Must be called before use
SlangResult init(ISlangSharedLibrary* library);
Expand All @@ -63,6 +65,7 @@ class GlslangDownstreamCompiler : public DownstreamCompilerBase
glslang_CompileFunc_1_1 m_compile_1_1 = nullptr;
glslang_CompileFunc_1_2 m_compile_1_2 = nullptr;
glslang_ValidateSPIRVFunc m_validate = nullptr;
glslang_DisassembleSPIRVFunc m_disassemble = nullptr;

ComPtr<ISlangSharedLibrary> m_sharedLibrary;

Expand All @@ -75,7 +78,8 @@ SlangResult GlslangDownstreamCompiler::init(ISlangSharedLibrary* library)
m_compile_1_1 = (glslang_CompileFunc_1_1)library->findFuncByName("glslang_compile_1_1");
m_compile_1_2 = (glslang_CompileFunc_1_2)library->findFuncByName("glslang_compile_1_2");
m_validate = (glslang_ValidateSPIRVFunc)library->findFuncByName("glslang_validateSPIRV");

m_disassemble =
(glslang_DisassembleSPIRVFunc)library->findFuncByName("glslang_disassembleSPIRV");

if (m_compile_1_0 == nullptr && m_compile_1_1 == nullptr && m_compile_1_2 == nullptr)
{
Expand Down Expand Up @@ -305,6 +309,20 @@ SlangResult GlslangDownstreamCompiler::validate(const uint32_t* contents, int co
return SLANG_FAIL;
}

SlangResult GlslangDownstreamCompiler::disassemble(const uint32_t* contents, int contentsSize)
{
if (m_disassemble == nullptr)
{
return SLANG_FAIL;
}

if (m_disassemble(contents, contentsSize))
{
return SLANG_OK;
}
return SLANG_FAIL;
}

bool GlslangDownstreamCompiler::canConvert(const ArtifactDesc& from, const ArtifactDesc& to)
{
// Can only disassemble blobs that are SPIR-V
Expand Down
4 changes: 4 additions & 0 deletions source/compiler-core/slang-json-rpc-connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ SlangResult JSONRPCConnection::sendCall(

SlangResult JSONRPCConnection::waitForResult(Int timeOutInMs)
{
// Invalidate m_jsonRoot before waitForResult, because when waitForResult fail,
// we don't want to use the result from the previous read.
m_jsonRoot.reset();

SLANG_RETURN_ON_FAIL(m_connection->waitForResult(timeOutInMs));
return tryReadMessage();
}
Expand Down
31 changes: 31 additions & 0 deletions source/slang-glslang/slang-glslang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,37 @@ extern "C"
return tools.Validate(contents, contentsSize, options);
}

// Disassemble the given SPIRV-ASM instructions.
extern "C"
#ifdef _MSC_VER
_declspec(dllexport)
#else
__attribute__((__visibility__("default")))
#endif
bool glslang_disassembleSPIRV(const uint32_t* contents, int contentsSize)
{
static const auto kDefaultEnvironment = SPV_ENV_UNIVERSAL_1_5;

uint32_t options = SPV_BINARY_TO_TEXT_OPTION_NONE;
options |= SPV_BINARY_TO_TEXT_OPTION_COMMENT;
options |= SPV_BINARY_TO_TEXT_OPTION_PRINT;
options |= SPV_BINARY_TO_TEXT_OPTION_COLOR;

spv_diagnostic diagnostic = nullptr;
spv_context context = spvContextCreate(kDefaultEnvironment);
spv_result_t error =
spvBinaryToText(context, contents, contentsSize, options, nullptr, &diagnostic);
spvContextDestroy(context);
if (error)
{
spvDiagnosticPrint(diagnostic);
spvDiagnosticDestroy(diagnostic);
return false;
}

return true;
}

// Apply the SPIRV-Tools optimizer to generated SPIR-V based on the desired optimization level
// TODO: add flag for optimizing SPIR-V size as well
static void glslang_optimizeSPIRV(
Expand Down
1 change: 1 addition & 0 deletions source/slang-glslang/slang-glslang.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,6 @@ typedef int (*glslang_CompileFunc_1_0)(glslang_CompileRequest_1_0* request);
typedef int (*glslang_CompileFunc_1_1)(glslang_CompileRequest_1_1* request);
typedef int (*glslang_CompileFunc_1_2)(glslang_CompileRequest_1_2* request);
typedef bool (*glslang_ValidateSPIRVFunc)(const uint32_t* contents, int contentsSize);
typedef bool (*glslang_DisassembleSPIRVFunc)(const uint32_t* contents, int contentsSize);

#endif
7 changes: 7 additions & 0 deletions source/slang-llvm/slang-llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ class LLVMDownstreamCompiler : public ComBaseObject, public IDownstreamCompiler
SLANG_UNUSED(contentsSize);
return SLANG_FAIL;
}
virtual SLANG_NO_THROW SlangResult SLANG_MCALL
disassemble(const uint32_t* contents, int contentsSize) SLANG_OVERRIDE
{
SLANG_UNUSED(contents);
SLANG_UNUSED(contentsSize);
return SLANG_FAIL;
}

LLVMDownstreamCompiler()
: m_desc(
Expand Down
6 changes: 1 addition & 5 deletions source/slang/slang-diagnostic-defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -2562,11 +2562,7 @@ DIAGNOSTIC(
Internal,
serialDebugVerificationFailed,
"Verification of serial debug information failed.")
DIAGNOSTIC(
99999,
Internal,
spirvValidationFailed,
"Validation of generated SPIR-V failed. SPIRV generated: \n$0")
DIAGNOSTIC(99999, Internal, spirvValidationFailed, "Validation of generated SPIR-V failed.")

DIAGNOSTIC(
99999,
Expand Down
1 change: 0 additions & 1 deletion source/slang/slang-emit-spirv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "slang-ir-util.h"
#include "slang-ir.h"
#include "slang-lookup-spirv.h"
#include "slang-spirv-val.h"
#include "spirv/unified1/spirv.h"

#include <type_traits>
Expand Down
20 changes: 7 additions & 13 deletions source/slang/slang-emit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@
#include "slang-legalize-types.h"
#include "slang-lower-to-ir.h"
#include "slang-mangle.h"
#include "slang-spirv-val.h"
#include "slang-syntax.h"
#include "slang-type-layout.h"
#include "slang-visitor.h"
Expand Down Expand Up @@ -1942,18 +1941,16 @@ SlangResult emitSPIRVForEntryPointsDirectly(
ArtifactUtil::createArtifactForCompileTarget(asExternal(codeGenContext->getTargetFormat()));
artifact->addRepresentationUnknown(ListBlob::moveCreate(spirv));

#if 0
// Dump the unoptimized SPIRV after lowering from slang IR -> SPIRV
String err; String dis;
disassembleSPIRV(spirv, err, dis);
printf("%s", dis.begin());
#endif

IDownstreamCompiler* compiler = codeGenContext->getSession()->getOrLoadDownstreamCompiler(
PassThroughMode::SpirvOpt,
codeGenContext->getSink());
if (compiler)
{
#if 0
// Dump the unoptimized SPIRV after lowering from slang IR -> SPIRV
compiler->disassemble((uint32_t*)spirv.getBuffer(), int(spirv.getCount() / 4));
#endif

if (!codeGenContext->shouldSkipSPIRVValidation())
{
StringBuilder runSpirvValEnvVar;
Expand All @@ -1966,13 +1963,10 @@ SlangResult emitSPIRVForEntryPointsDirectly(
(uint32_t*)spirv.getBuffer(),
int(spirv.getCount() / 4))))
{
String err;
String dis;
disassembleSPIRV(spirv, err, dis);
compiler->disassemble((uint32_t*)spirv.getBuffer(), int(spirv.getCount() / 4));
codeGenContext->getSink()->diagnoseWithoutSourceView(
SourceLoc{},
Diagnostics::spirvValidationFailed,
dis);
Diagnostics::spirvValidationFailed);
}
}
}
Expand Down
41 changes: 0 additions & 41 deletions source/slang/slang-spirv-val.cpp

This file was deleted.

10 changes: 0 additions & 10 deletions source/slang/slang-spirv-val.h

This file was deleted.

4 changes: 2 additions & 2 deletions tests/diagnostics/illegal-func-decl.slang
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
//TEST:COMPILE: tests/diagnostics/illegal-func-decl-module.slang -o tests/diagnostics/illegal-func-decl-module.slang-module

//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK1): -r tests/diagnostics/illegal-func-decl-module.slang-module -DTEST1 -target spirv -o illegal-func-decl.spv
//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK2): -r tests/diagnostics/illegal-func-decl-module.slang-module -DTEST2 -target spirv -o illegal-func-decl.spv
//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK2): -r tests/diagnostics/illegal-func-decl-module.slang-module -DTEST2 -target spirv -o illegal-func-decl.spv -skip-spirv-validation
//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK3): -r tests/diagnostics/illegal-func-decl-module.slang-module -DTEST3 -target spirv -o illegal-func-decl.spv
//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK4): -r tests/diagnostics/illegal-func-decl-module.slang-module -DTEST4 -target spirv -o illegal-func-decl.spv
//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK4): -r tests/diagnostics/illegal-func-decl-module.slang-module -DTEST4 -target spirv -o illegal-func-decl.spv -skip-spirv-validation

#ifdef TEST1
// CHECK1: ([[# @LINE+1]]): error 45001: unresolved external symbol 'libraryFunction'.
Expand Down
4 changes: 2 additions & 2 deletions tests/language-feature/capability/capability3.slang
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//TEST:SIMPLE(filecheck=CHECK): -target spirv -emit-spirv-directly -entry main -stage compute
//TEST:SIMPLE(filecheck=CHECK_IGNORE_CAPS): -target spirv -emit-spirv-directly -entry main -stage compute -ignore-capabilities
//TEST:SIMPLE(filecheck=CHECK_IGNORE_CAPS): -target spirv -emit-spirv-directly -entry main -stage compute -ignore-capabilities -skip-spirv-validation
// CHECK_IGNORE_CAPS-NOT: error 36108

// Test that capabilities can be declared on module.
Expand Down Expand Up @@ -39,4 +39,4 @@ void main()
{
use1();
use2();
}
}
2 changes: 1 addition & 1 deletion tests/language-feature/capability/capability7.slang
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//TEST:SIMPLE(filecheck=CHECK): -target glsl -entry computeMain -stage compute -profile sm_5_0
//TEST:SIMPLE(filecheck=CHECK_IGNORE_CAPS): -target glsl -emit-spirv-directly -entry computeMain -stage compute -profile sm_5_0 -ignore-capabilities
//TEST:SIMPLE(filecheck=CHECK_IGNORE_CAPS): -target glsl -emit-spirv-directly -entry computeMain -stage compute -profile sm_5_0 -ignore-capabilities -skip-spirv-validation

// Test that we diagnose simplified capabilities
// CHECK_IGNORE_CAPS-NOT: error 36104
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

//TEST:SIMPLE(filecheck=CHECK): -target spirv -entry vsmain -profile vs_6_0
//TEST:SIMPLE(filecheck=CHECK): -target spirv -entry psmain -profile vs_6_0 -ignore-capabilities
//TEST:SIMPLE(filecheck=CHECK): -target spirv -entry vsmain -profile ps_6_0 -ignore-capabilities
//TEST:SIMPLE(filecheck=CHECK): -target spirv -entry vsmain -profile ps_6_0 -ignore-capabilities -skip-spirv-validation

// CHECK_ERROR: warning 36112
// CHECK-NOT: warning 36112
Expand Down Expand Up @@ -33,4 +33,4 @@ VSOutput vsmain(float3 PositionOS : POSITION, float3 Color : COLOR0)
float4 psmain(VSOutput input) : SV_TARGET
{
return float4(input.Color, 1);
}
}
Loading