From 1b543c6778bbd80dd94137bcc13b8cdcd546bb26 Mon Sep 17 00:00:00 2001 From: David Neto Date: Tue, 24 Nov 2020 15:17:46 +0000 Subject: [PATCH] spirv-reader: Add a disassembler helper This helped me debug a problem. Change-Id: I665aaa482c6f20d24966f0990bfe4e15cdd82915 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/33800 Commit-Queue: David Neto Commit-Queue: Ben Clayton Reviewed-by: Ben Clayton Auto-Submit: David Neto --- src/reader/spirv/spirv_tools_helpers_test.cc | 19 +++++++++++++++++-- src/reader/spirv/spirv_tools_helpers_test.h | 6 ++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/reader/spirv/spirv_tools_helpers_test.cc b/src/reader/spirv/spirv_tools_helpers_test.cc index 81dd5e7232..4849a7a3a7 100644 --- a/src/reader/spirv/spirv_tools_helpers_test.cc +++ b/src/reader/spirv/spirv_tools_helpers_test.cc @@ -26,8 +26,6 @@ namespace reader { namespace spirv { namespace test { -/// @returns the SPIR-V module assembled from the given text. Numeric IDs -/// are preserved. std::vector Assemble(const std::string& spirv_assembly) { // TODO(dneto): Use ScopedTrace? @@ -69,6 +67,23 @@ std::string AssembleFailure(const std::string& spirv_assembly) { return errors.str(); } +std::string Disassemble(const std::vector& spirv_module) { + spvtools::SpirvTools tools(SPV_ENV_UNIVERSAL_1_0); + std::stringstream errors; + tools.SetMessageConsumer([&errors](spv_message_level_t, const char*, + const spv_position_t& position, + const char* message) { + errors << "disassmbly error:" << position.line << ":" << position.column + << ": " << message; + }); + + std::string result; + const auto success = tools.Disassemble(spirv_module, &result); + EXPECT_TRUE(success) << errors.str(); + + return result; +} + } // namespace test } // namespace spirv } // namespace reader diff --git a/src/reader/spirv/spirv_tools_helpers_test.h b/src/reader/spirv/spirv_tools_helpers_test.h index ba4ad6863f..654d85a936 100644 --- a/src/reader/spirv/spirv_tools_helpers_test.h +++ b/src/reader/spirv/spirv_tools_helpers_test.h @@ -24,14 +24,20 @@ namespace reader { namespace spirv { namespace test { +/// @param spirv_assembly SPIR-V assembly text /// @returns the SPIR-V module assembled from the given text. Numeric IDs /// are preserved. std::vector Assemble(const std::string& spirv_assembly); /// Attempts to assemble given SPIR-V assembly text. Expect it to fail. +/// @param spirv_assembly the SPIR-V assembly /// @returns the failure message. std::string AssembleFailure(const std::string& spirv_assembly); +/// @param spirv_module a SPIR-V binary module +/// @returns the disassembled module +std::string Disassemble(const std::vector& spirv_module); + } // namespace test } // namespace spirv } // namespace reader