Skip to content

Commit 0243356

Browse files
authored
tools: Accept hex representation as binary input (KhronosGroup#5870)
Sometimes when debugging or logging, SPIR-V may be dumped as a stream of hex values. There are tools to convert such a stream to binary (such as [1]) but they create an inconvenient extra step when for example the disassembly of that hex stream is needed. [1]: https://www.khronos.org/spir/visualizer/hexdump.html In this change, the binary reader used by the tools is enhanced to detect when the binary is actually a hex stream, and parse that instead. The following formats are accepted, detected based on how the SPIR-V magic number is output: === Words If the first token of the hex stream is one of 0x07230203, 0x7230203, x07230203, or x7230203, the hex stream is expected to consist of 32-bit hex words prefixed with 0x or x. For example: 0x7230203, 0x10400, 0x180001, 0x79, 0x0 is parsed as: 0x07230203 0x00010400 0x00180001 0x00000079 0x00000000 Note that `,` is optional in the stream, but the hex values are expected to be delimited by either `,` or whitespace. === Bytes With Prefix If the first token of the hex stream is one of 0x07, 0x7, x07, x7, 0x03, 0x3, x03, or x3, the hex stream is expected to consist of 8-bit hex bytes prefixed with 0x or x. If the first token has a value of 7, the stream is big-endian. Otherwise it's little-endian. For example: 0x3, 0x2, 0x23, 0x7, 0x0, 0x4, 0x1, 0x0, 0x1, 0x0, 0x18, 0x0, 0x79, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 is parsed as: 0x07230203 0x00010400 0x00180001 0x00000079 0x00000000 Similar to "Words", `,` is optional in the stream, but the hex values are expected to be delimited by either `,` or whitespace. === Bytes Without Prefix If the first two characters of the hex stream is 07, or 03, the hex stream is expected to consist of 8-bit hex bytes of 2 characters each. If the first token is 07, the stream is big-endian. Otherwise it's little-endian. Unlike the other modes, delimiter is optional (which automatically handles 32-bit word streams), but no 0-padding is done. For example, all of the following: 03, 02, 23, 07, 00, 04, 01, 00, 01, 00, 18, 00, 79, 00, 00, 00, 00, 00, 00, 00 03 02 23 07 00 04 01 00 01 00 18 00 79 00 00 00 00 00 00 00 03022307 00040100 01001800 79000000 00000000 07,23,02,03,00,01,04,00,00,18,00,01,00,00,00,79,00,00,00,00 07230203, 00010400, 00180001, 00000079, 00000000 are parsed as: 0x07230203 0x00010400 0x00180001 0x00000079 0x00000000
1 parent d426fc5 commit 0243356

File tree

10 files changed

+775
-15
lines changed

10 files changed

+775
-15
lines changed

BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ cc_library(
447447
"tools_util",
448448
":spirv_tools_internal",
449449
":test_lib",
450+
":tools_io",
450451
"@googletest//:gtest",
451452
"@googletest//:gtest_main",
452453
],

BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,7 @@ if (build_with_chromium && spvtools_build_executables) {
13771377
"test/fix_word_test.cpp",
13781378
"test/generator_magic_number_test.cpp",
13791379
"test/hex_float_test.cpp",
1380+
"test/hex_to_text_test.cpp",
13801381
"test/immediate_int_test.cpp",
13811382
"test/libspirv_macros_test.cpp",
13821383
"test/name_mapper_test.cpp",
@@ -1424,6 +1425,7 @@ if (build_with_chromium && spvtools_build_executables) {
14241425
":spvtools_language_header_cldebuginfo100",
14251426
":spvtools_language_header_debuginfo",
14261427
":spvtools_language_header_vkdebuginfo100",
1428+
":spvtools_tools_io",
14271429
":spvtools_val",
14281430
"//testing/gmock",
14291431
"//testing/gtest",

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ further notice.
8080
* Assembler only does basic syntax checking. No cross validation of
8181
IDs or types is performed, except to check literal arguments to
8282
`OpConstant`, `OpSpecConstant`, and `OpSwitch`.
83+
* Where tools expect binary input, a hex stream may be provided instead. See
84+
`spirv-dis --help`.
8385

8486
See [`docs/syntax.md`](docs/syntax.md) for the assembly language syntax.
8587

test/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ endfunction()
8888
set(TEST_SOURCES
8989
test_fixture.h
9090
unit_spirv.h
91+
${spirv-tools_SOURCE_DIR}/tools/io.h
9192

9293
assembly_context_test.cpp
9394
assembly_format_test.cpp
@@ -110,6 +111,7 @@ set(TEST_SOURCES
110111
fix_word_test.cpp
111112
generator_magic_number_test.cpp
112113
hex_float_test.cpp
114+
hex_to_text_test.cpp
113115
immediate_int_test.cpp
114116
libspirv_macros_test.cpp
115117
named_id_test.cpp
@@ -154,6 +156,7 @@ set(TEST_SOURCES
154156
to_string_test.cpp
155157

156158
unit_spirv.cpp
159+
${spirv-tools_SOURCE_DIR}/tools/io.cpp
157160
)
158161

159162
spvtools_pch(TEST_SOURCES pch_test)

test/diff/diff_test.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include "source/opt/ir_context.h"
2121
#include "source/spirv_constant.h"
2222
#include "spirv-tools/libspirv.hpp"
23-
#include "tools/io.h"
2423
#include "tools/util/cli_consumer.h"
2524

2625
#include <fstream>

test/diff/diff_test_utils.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "source/opt/ir_context.h"
1919

2020
#include "spirv-tools/libspirv.hpp"
21-
#include "tools/io.h"
2221
#include "tools/util/cli_consumer.h"
2322

2423
#include "gtest/gtest.h"

0 commit comments

Comments
 (0)