summaryrefslogtreecommitdiff
path: root/source/slang-glslang
diff options
context:
space:
mode:
authorMukund Keshava <mkeshava@nvidia.com>2025-03-11 21:40:05 +0530
committerGitHub <noreply@github.com>2025-03-11 16:10:05 +0000
commitf59e0ef409844f2514435a8df8ceeff3663e5db3 (patch)
tree7f8f7514ed140a49f03923d84a549fdbfdcbcc84 /source/slang-glslang
parentff55a569a0bff44a6f8abb105c07cbc2484bc007 (diff)
IR: Add SPIR-V disassembly for embedded downstream IR dumps (#6529)
* IR: Add SPIR-V disassembly for embedded downstream IR dumps When dumping IR that contains embedded downstream SPIR-V code (via EmbeddedDownstreamIR instructions), display the disassembled SPIR-V instead of just showing "<binary blob>". This CL also does: - Adds a new interface for disassembly and get result. - Modify export-library-generics.slang test test to check for the disassembled SPIR-V Fixes #6513 * Add module-dual-target-verify test Fixes #6517 Adds a new test to verify that dxil and spirv targets are stored separately in the precompiled blob. * Fix review comments from cheneym2 * format code --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com>
Diffstat (limited to 'source/slang-glslang')
-rw-r--r--source/slang-glslang/slang-glslang.cpp42
-rw-r--r--source/slang-glslang/slang-glslang.h4
2 files changed, 39 insertions, 7 deletions
diff --git a/source/slang-glslang/slang-glslang.cpp b/source/slang-glslang/slang-glslang.cpp
index 3dc9b1bd4..8d9f64fd5 100644
--- a/source/slang-glslang/slang-glslang.cpp
+++ b/source/slang-glslang/slang-glslang.cpp
@@ -184,27 +184,30 @@ extern "C"
return tools.Validate(contents, contentsSize, options);
}
-// Disassemble the given SPIRV-ASM instructions.
+// Disassemble the given SPIRV-ASM instructions and return the result as a string.
extern "C"
#ifdef _MSC_VER
_declspec(dllexport)
#else
- __attribute__((__visibility__("default")))
+__attribute__((__visibility__("default")))
#endif
- bool glslang_disassembleSPIRV(const uint32_t* contents, int contentsSize)
+ bool glslang_disassembleSPIRVWithResult(
+ const uint32_t* contents,
+ int contentsSize,
+ char** outString)
{
static const auto kDefaultEnvironment = SPV_ENV_UNIVERSAL_1_5;
+ spv_text text;
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;
options |= SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES;
+ options |= SPV_BINARY_TO_TEXT_OPTION_INDENT;
spv_diagnostic diagnostic = nullptr;
spv_context context = spvContextCreate(kDefaultEnvironment);
spv_result_t error =
- spvBinaryToText(context, contents, contentsSize, options, nullptr, &diagnostic);
+ spvBinaryToText(context, contents, contentsSize, options, &text, &diagnostic);
spvContextDestroy(context);
if (error)
{
@@ -212,8 +215,33 @@ extern "C"
spvDiagnosticDestroy(diagnostic);
return false;
}
+ else
+ {
+ if (outString)
+ {
+ // Allocate memory for the output string and copy the result
+ size_t len = text->length + 1; // +1 for null terminator
+ *outString = new char[len];
+ memcpy(*outString, text->str, text->length);
+ (*outString)[text->length] = '\0'; // Ensure null termination
+ }
+
+ spvTextDestroy(text);
+ return true;
+ }
+}
+
- return true;
+// 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)
+{
+ return glslang_disassembleSPIRVWithResult(contents, contentsSize, nullptr);
}
// Apply the SPIRV-Tools optimizer to generated SPIR-V based on the desired optimization level
diff --git a/source/slang-glslang/slang-glslang.h b/source/slang-glslang/slang-glslang.h
index 8343b4c8e..d6e10d320 100644
--- a/source/slang-glslang/slang-glslang.h
+++ b/source/slang-glslang/slang-glslang.h
@@ -166,5 +166,9 @@ 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);
+typedef bool (*glslang_DisassembleSPIRVWithResultFunc)(
+ const uint32_t* contents,
+ int contentsSize,
+ char** outString);
typedef bool (*glslang_LinkSPIRVFunc)(glslang_LinkRequest* request);
#endif