unit_spirv.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Copyright (c) 2015-2016 The Khronos Group Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef TEST_UNIT_SPIRV_H_
  15. #define TEST_UNIT_SPIRV_H_
  16. #include <stdint.h>
  17. #include <iomanip>
  18. #include <string>
  19. #include <vector>
  20. #include "gtest/gtest.h"
  21. #include "source/assembly_grammar.h"
  22. #include "source/binary.h"
  23. #include "source/diagnostic.h"
  24. #include "source/enum_set.h"
  25. #include "source/opcode.h"
  26. #include "source/spirv_endian.h"
  27. #include "source/text.h"
  28. #include "source/text_handler.h"
  29. #include "source/val/validate.h"
  30. #include "spirv-tools/libspirv.h"
  31. #ifdef __ANDROID__
  32. #include <sstream>
  33. namespace std {
  34. template <typename T>
  35. std::string to_string(const T& val) {
  36. std::ostringstream os;
  37. os << val;
  38. return os.str();
  39. }
  40. } // namespace std
  41. #endif
  42. // Determine endianness & predicate tests on it
  43. enum {
  44. I32_ENDIAN_LITTLE = 0x03020100ul,
  45. I32_ENDIAN_BIG = 0x00010203ul,
  46. };
  47. static const union {
  48. unsigned char bytes[4];
  49. uint32_t value;
  50. } o32_host_order = {{0, 1, 2, 3}};
  51. #define I32_ENDIAN_HOST (o32_host_order.value)
  52. // A namespace for utilities used in SPIR-V Tools unit tests.
  53. namespace spvtest {
  54. class WordVector;
  55. // Emits the given word vector to the given stream.
  56. // This function can be used by the gtest value printer.
  57. void PrintTo(const WordVector& words, ::std::ostream* os);
  58. // A proxy class to allow us to easily write out vectors of SPIR-V words.
  59. class WordVector {
  60. public:
  61. explicit WordVector(const std::vector<uint32_t>& val) : value_(val) {}
  62. explicit WordVector(const spv_binary_t& binary)
  63. : value_(binary.code, binary.code + binary.wordCount) {}
  64. // Returns the underlying vector.
  65. const std::vector<uint32_t>& value() const { return value_; }
  66. // Returns the string representation of this word vector.
  67. std::string str() const {
  68. std::ostringstream os;
  69. PrintTo(*this, &os);
  70. return os.str();
  71. }
  72. private:
  73. const std::vector<uint32_t> value_;
  74. };
  75. inline void PrintTo(const WordVector& words, ::std::ostream* os) {
  76. size_t count = 0;
  77. const auto saved_flags = os->flags();
  78. const auto saved_fill = os->fill();
  79. for (uint32_t value : words.value()) {
  80. *os << "0x" << std::setw(8) << std::setfill('0') << std::hex << value
  81. << " ";
  82. if (count++ % 8 == 7) {
  83. *os << std::endl;
  84. }
  85. }
  86. os->flags(saved_flags);
  87. os->fill(saved_fill);
  88. }
  89. // Returns a vector of words representing a single instruction with the
  90. // given opcode and operand words as a vector.
  91. inline std::vector<uint32_t> MakeInstruction(
  92. SpvOp opcode, const std::vector<uint32_t>& args) {
  93. std::vector<uint32_t> result{
  94. spvOpcodeMake(uint16_t(args.size() + 1), opcode)};
  95. result.insert(result.end(), args.begin(), args.end());
  96. return result;
  97. }
  98. // Returns a vector of words representing a single instruction with the
  99. // given opcode and whose operands are the concatenation of the two given
  100. // argument lists.
  101. inline std::vector<uint32_t> MakeInstruction(
  102. SpvOp opcode, std::vector<uint32_t> args,
  103. const std::vector<uint32_t>& extra_args) {
  104. args.insert(args.end(), extra_args.begin(), extra_args.end());
  105. return MakeInstruction(opcode, args);
  106. }
  107. // Returns the vector of words representing the concatenation
  108. // of all input vectors.
  109. inline std::vector<uint32_t> Concatenate(
  110. const std::vector<std::vector<uint32_t>>& instructions) {
  111. std::vector<uint32_t> result;
  112. for (const auto& instruction : instructions) {
  113. result.insert(result.end(), instruction.begin(), instruction.end());
  114. }
  115. return result;
  116. }
  117. // Encodes a string as a sequence of words, using the SPIR-V encoding.
  118. inline std::vector<uint32_t> MakeVector(std::string input) {
  119. std::vector<uint32_t> result;
  120. uint32_t word = 0;
  121. size_t num_bytes = input.size();
  122. // SPIR-V strings are null-terminated. The byte_index == num_bytes
  123. // case is used to push the terminating null byte.
  124. for (size_t byte_index = 0; byte_index <= num_bytes; byte_index++) {
  125. const auto new_byte =
  126. (byte_index < num_bytes ? uint8_t(input[byte_index]) : uint8_t(0));
  127. word |= (new_byte << (8 * (byte_index % sizeof(uint32_t))));
  128. if (3 == (byte_index % sizeof(uint32_t))) {
  129. result.push_back(word);
  130. word = 0;
  131. }
  132. }
  133. // Emit a trailing partial word.
  134. if ((num_bytes + 1) % sizeof(uint32_t)) {
  135. result.push_back(word);
  136. }
  137. return result;
  138. }
  139. // A type for easily creating spv_text_t values, with an implicit conversion to
  140. // spv_text.
  141. struct AutoText {
  142. explicit AutoText(const std::string& value)
  143. : str(value), text({str.data(), str.size()}) {}
  144. operator spv_text() { return &text; }
  145. std::string str;
  146. spv_text_t text;
  147. };
  148. // An example case for an enumerated value, optionally with operands.
  149. template <typename E>
  150. class EnumCase {
  151. public:
  152. EnumCase() = default; // Required by ::testing::Combine().
  153. EnumCase(E val, std::string enum_name, std::vector<uint32_t> ops = {})
  154. : enum_value_(val), name_(enum_name), operands_(ops) {}
  155. // Returns the enum value as a uint32_t.
  156. uint32_t value() const { return static_cast<uint32_t>(enum_value_); }
  157. // Returns the name of the enumerant.
  158. const std::string& name() const { return name_; }
  159. // Returns a reference to the operands.
  160. const std::vector<uint32_t>& operands() const { return operands_; }
  161. private:
  162. E enum_value_;
  163. std::string name_;
  164. std::vector<uint32_t> operands_;
  165. };
  166. // Returns a string with num_4_byte_chars Unicode characters,
  167. // each of which has a 4-byte UTF-8 encoding.
  168. inline std::string MakeLongUTF8String(size_t num_4_byte_chars) {
  169. // An example of a longest valid UTF-8 character.
  170. // Be explicit about the character type because Microsoft compilers can
  171. // otherwise interpret the character string as being over wide (16-bit)
  172. // characters. Ideally, we would just use a C++11 UTF-8 string literal,
  173. // but we want to support older Microsoft compilers.
  174. const std::basic_string<char> earth_africa("\xF0\x9F\x8C\x8D");
  175. EXPECT_EQ(4u, earth_africa.size());
  176. std::string result;
  177. result.reserve(num_4_byte_chars * 4);
  178. for (size_t i = 0; i < num_4_byte_chars; i++) {
  179. result += earth_africa;
  180. }
  181. EXPECT_EQ(4 * num_4_byte_chars, result.size());
  182. return result;
  183. }
  184. // Returns a vector of all valid target environment enums.
  185. inline std::vector<spv_target_env> AllTargetEnvironments() {
  186. return {
  187. SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1,
  188. SPV_ENV_OPENCL_1_2, SPV_ENV_OPENCL_EMBEDDED_1_2,
  189. SPV_ENV_OPENCL_2_0, SPV_ENV_OPENCL_EMBEDDED_2_0,
  190. SPV_ENV_OPENCL_2_1, SPV_ENV_OPENCL_EMBEDDED_2_1,
  191. SPV_ENV_OPENCL_2_2, SPV_ENV_OPENCL_EMBEDDED_2_2,
  192. SPV_ENV_VULKAN_1_0, SPV_ENV_OPENGL_4_0,
  193. SPV_ENV_OPENGL_4_1, SPV_ENV_OPENGL_4_2,
  194. SPV_ENV_OPENGL_4_3, SPV_ENV_OPENGL_4_5,
  195. SPV_ENV_UNIVERSAL_1_2, SPV_ENV_UNIVERSAL_1_3,
  196. SPV_ENV_VULKAN_1_1, SPV_ENV_WEBGPU_0,
  197. };
  198. }
  199. // Returns the capabilities in a CapabilitySet as an ordered vector.
  200. inline std::vector<SpvCapability> ElementsIn(
  201. const spvtools::CapabilitySet& capabilities) {
  202. std::vector<SpvCapability> result;
  203. capabilities.ForEach([&result](SpvCapability c) { result.push_back(c); });
  204. return result;
  205. }
  206. } // namespace spvtest
  207. #endif // TEST_UNIT_SPIRV_H_