The version header word has 3 byte-size components.
Bits 24-31: 0
Bits 16-23: SPIR-V major number (1)
Bits 8-15: SPIR-V minor number (0)
Bits 0-7: SPIR-V minor number (2)
The assembler will construct the word appropriately,
and the disassemble will print it in major.minor.revision form.
diff --git a/include/libspirv/libspirv.h b/include/libspirv/libspirv.h
index 74ea9b0..1f9d68a 100644
--- a/include/libspirv/libspirv.h
+++ b/include/libspirv/libspirv.h
@@ -38,6 +38,14 @@
#include <stddef.h>
#include <stdint.h>
+// Versions
+// This library is based on SPIR-V 1.0 Rev2
+// TODO(dneto): Use the values from the SPIR-V header, when it's updated for
+// SPIR-V 1.0 public release.
+#define SPV_SPIRV_VERSION_MAJOR 1
+#define SPV_SPIRV_VERSION_MINOR 0
+#define SPV_SPIRV_VERSION_REVISION 2
+
// Helpers
#define spvIsInBitfield(value, bitfield) (value == (value & bitfield))
diff --git a/source/disassemble.cpp b/source/disassemble.cpp
index c033aae..4572afa 100644
--- a/source/disassemble.cpp
+++ b/source/disassemble.cpp
@@ -135,7 +135,9 @@
const char* generator_tool =
spvGeneratorStr(SPV_GENERATOR_TOOL_PART(generator));
stream_ << "; SPIR-V\n"
- << "; Version: " << version << "\n"
+ << "; Version: " << SPV_SPIRV_VERSION_MAJOR_PART(version) << "."
+ << SPV_SPIRV_VERSION_MINOR_PART(version) << "."
+ << SPV_SPIRV_VERSION_REVISION_PART(version) << "\n"
<< "; Generator: " << generator_tool;
// For unknown tools, print the numeric tool value.
if (0 == strcmp("Unknown", generator_tool)) {
diff --git a/source/spirv_constant.h b/source/spirv_constant.h
index 5795d62..14da807 100644
--- a/source/spirv_constant.h
+++ b/source/spirv_constant.h
@@ -29,6 +29,20 @@
#include "libspirv/libspirv.h"
+// Version number macros.
+
+// Evaluates to a well-formed version header word, given valid
+// SPIR-V version major, minor, and revision numbers.
+#define SPV_SPIRV_VERSION_WORD(MAJOR, MINOR, REVISION) \
+ ((uint32_t(uint8_t(MAJOR)) << 16) | (uint32_t(uint8_t(MINOR)) << 8) | \
+ uint8_t(REVISION))
+// Returns the major version extracted from a version header word.
+#define SPV_SPIRV_VERSION_MAJOR_PART(WORD) ((uint32_t(WORD) >> 16) & 0xff)
+// Returns the minor version extracted from a version header word.
+#define SPV_SPIRV_VERSION_MINOR_PART(WORD) ((uint32_t(WORD) >> 8) & 0xff)
+// Returns the revision number extracted from a version header word.
+#define SPV_SPIRV_VERSION_REVISION_PART(WORD) (uint32_t(WORD) & 0xff)
+
// Header indices
#define SPV_INDEX_MAGIC_NUMBER 0u
diff --git a/source/text.cpp b/source/text.cpp
index 7e706a8..d33d773 100644
--- a/source/text.cpp
+++ b/source/text.cpp
@@ -667,7 +667,9 @@
if (!words) return SPV_ERROR_INVALID_BINARY;
words[SPV_INDEX_MAGIC_NUMBER] = SpvMagicNumber;
- words[SPV_INDEX_VERSION_NUMBER] = SpvVersion;
+ words[SPV_INDEX_VERSION_NUMBER] =
+ SPV_SPIRV_VERSION_WORD(SPV_SPIRV_VERSION_MAJOR, SPV_SPIRV_VERSION_MINOR,
+ SPV_SPIRV_VERSION_REVISION);
words[SPV_INDEX_GENERATOR_NUMBER] =
SPV_GENERATOR_WORD(SPV_GENERATOR_KHRONOS_ASSEMBLER, kAssemblerVersion);
words[SPV_INDEX_BOUND] = bound;
diff --git a/test/BinaryToText.cpp b/test/BinaryToText.cpp
index 7003cec..6ec1535 100644
--- a/test/BinaryToText.cpp
+++ b/test/BinaryToText.cpp
@@ -395,6 +395,25 @@
expected);
}
+// Test version string.
+TEST_F(TextToBinaryTest, VersionString) {
+ auto words = CompileSuccessfully("");
+ spv_text decoded_text = nullptr;
+ spv_diagnostic diagnostic = nullptr;
+ EXPECT_THAT(spvBinaryToText(context, words.data(), words.size(),
+ SPV_BINARY_TO_TEXT_OPTION_NONE, &decoded_text,
+ &diagnostic),
+ Eq(SPV_SUCCESS));
+ EXPECT_EQ(nullptr, diagnostic);
+
+ EXPECT_EQ(1, SPV_SPIRV_VERSION_MAJOR);
+ EXPECT_EQ(0, SPV_SPIRV_VERSION_MINOR);
+ EXPECT_EQ(2, SPV_SPIRV_VERSION_REVISION);
+ EXPECT_THAT(decoded_text->str, HasSubstr("Version: 1.0.2\n"))
+ << EncodeAndDecodeSuccessfully("");
+ spvTextDestroy(decoded_text);
+}
+
// Test generator string.
// A test case for the generator string. This allows us to
diff --git a/test/ExtInstGLSLstd450.cpp b/test/ExtInstGLSLstd450.cpp
index dc27a96..41b34e2 100644
--- a/test/ExtInstGLSLstd450.cpp
+++ b/test/ExtInstGLSLstd450.cpp
@@ -66,7 +66,7 @@
)";
const std::string spirv_header =
R"(; SPIR-V
-; Version: 100
+; Version: 1.0.2
; Generator: Khronos SPIR-V Tools Assembler; 0
; Bound: 9
; Schema: 0)";