val_misc_test.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // Copyright (c) 2019 Google LLC.
  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. // Validation tests for misc instructions
  15. #include <string>
  16. #include <vector>
  17. #include "gmock/gmock.h"
  18. #include "test/unit_spirv.h"
  19. #include "test/val/val_fixtures.h"
  20. namespace spvtools {
  21. namespace val {
  22. namespace {
  23. using ::testing::Eq;
  24. using ::testing::HasSubstr;
  25. using ValidateMisc = spvtest::ValidateBase<bool>;
  26. TEST_F(ValidateMisc, UndefRestrictedShort) {
  27. const std::string spirv = R"(
  28. OpCapability Shader
  29. OpCapability Linkage
  30. OpCapability StorageBuffer16BitAccess
  31. OpExtension "SPV_KHR_16bit_storage"
  32. OpMemoryModel Logical GLSL450
  33. %short = OpTypeInt 16 0
  34. %undef = OpUndef %short
  35. )";
  36. CompileSuccessfully(spirv);
  37. EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
  38. EXPECT_THAT(
  39. getDiagnosticString(),
  40. HasSubstr("Cannot create undefined values with 8- or 16-bit types"));
  41. }
  42. TEST_F(ValidateMisc, UndefRestrictedChar) {
  43. const std::string spirv = R"(
  44. OpCapability Shader
  45. OpCapability Linkage
  46. OpCapability StorageBuffer8BitAccess
  47. OpExtension "SPV_KHR_8bit_storage"
  48. OpMemoryModel Logical GLSL450
  49. %char = OpTypeInt 8 0
  50. %undef = OpUndef %char
  51. )";
  52. CompileSuccessfully(spirv);
  53. EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
  54. EXPECT_THAT(
  55. getDiagnosticString(),
  56. HasSubstr("Cannot create undefined values with 8- or 16-bit types"));
  57. }
  58. TEST_F(ValidateMisc, UndefRestrictedHalf) {
  59. const std::string spirv = R"(
  60. OpCapability Shader
  61. OpCapability Linkage
  62. OpCapability StorageBuffer16BitAccess
  63. OpExtension "SPV_KHR_16bit_storage"
  64. OpMemoryModel Logical GLSL450
  65. %half = OpTypeFloat 16
  66. %undef = OpUndef %half
  67. )";
  68. CompileSuccessfully(spirv);
  69. EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
  70. EXPECT_THAT(
  71. getDiagnosticString(),
  72. HasSubstr("Cannot create undefined values with 8- or 16-bit types"));
  73. }
  74. const std::string ShaderClockSpriv = R"(
  75. OpCapability Shader
  76. OpCapability Int64
  77. OpCapability ShaderClockKHR
  78. OpExtension "SPV_KHR_shader_clock"
  79. %1 = OpExtInstImport "GLSL.std.450"
  80. OpMemoryModel Logical GLSL450
  81. OpEntryPoint Fragment %main "main"
  82. OpExecutionMode %main OriginUpperLeft
  83. OpSource GLSL 450
  84. OpSourceExtension "GL_ARB_gpu_shader_int64"
  85. OpSourceExtension "GL_ARB_shader_clock"
  86. OpSourceExtension "GL_EXT_shader_realtime_clock"
  87. OpName %main "main"
  88. OpName %time1 "time1"
  89. %void = OpTypeVoid
  90. )";
  91. TEST_F(ValidateMisc, ShaderClockInt64) {
  92. const std::string spirv = ShaderClockSpriv + R"(
  93. %3 = OpTypeFunction %void
  94. %uint = OpTypeInt 32 0
  95. %_ptr_Function_uint = OpTypePointer Function %uint
  96. %uint_3 = OpConstant %uint 3
  97. %uint_1 = OpConstant %uint 1
  98. %main = OpFunction %void None %3
  99. %5 = OpLabel
  100. %time1 = OpVariable %_ptr_Function_uint Function
  101. %11 = OpReadClockKHR %uint %uint_3
  102. OpStore %time1 %11
  103. OpReturn
  104. OpFunctionEnd)";
  105. CompileSuccessfully(spirv);
  106. EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  107. EXPECT_THAT(getDiagnosticString(), HasSubstr("or 64bit unsigned integer"));
  108. }
  109. TEST_F(ValidateMisc, ShaderClockVec2) {
  110. const std::string spirv = ShaderClockSpriv + R"(
  111. %3 = OpTypeFunction %void
  112. %ulong = OpTypeInt 64 0
  113. %_ptr_Function_ulong = OpTypePointer Function %ulong
  114. %uint = OpTypeInt 32 0
  115. %uint_3 = OpConstant %uint 3
  116. %v2uint = OpTypeVector %ulong 2
  117. %_ptr_Function_v2uint = OpTypePointer Function %v2uint
  118. %main = OpFunction %void None %3
  119. %5 = OpLabel
  120. %time1 = OpVariable %_ptr_Function_v2uint Function
  121. %15 = OpReadClockKHR %v2uint %uint_3
  122. OpStore %time1 %15
  123. OpReturn
  124. OpFunctionEnd)";
  125. CompileSuccessfully(spirv);
  126. EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  127. EXPECT_THAT(getDiagnosticString(), HasSubstr("vector of two components"));
  128. }
  129. TEST_F(ValidateMisc, ShaderClockInvalidScopeValue) {
  130. const std::string spirv = ShaderClockSpriv + R"(
  131. %3 = OpTypeFunction %void
  132. %ulong = OpTypeInt 64 0
  133. %uint = OpTypeInt 32 0
  134. %_ptr_Function_ulong = OpTypePointer Function %ulong
  135. %uint_10 = OpConstant %uint 10
  136. %uint_1 = OpConstant %uint 1
  137. %main = OpFunction %void None %3
  138. %5 = OpLabel
  139. %time1 = OpVariable %_ptr_Function_ulong Function
  140. %11 = OpReadClockKHR %ulong %uint_10
  141. OpStore %time1 %11
  142. OpReturn
  143. OpFunctionEnd)";
  144. CompileSuccessfully(spirv);
  145. EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  146. EXPECT_THAT(getDiagnosticString(), HasSubstr("Invalid scope value"));
  147. }
  148. TEST_F(ValidateMisc, ShaderClockSubgroupScope) {
  149. const std::string spirv = ShaderClockSpriv + R"(
  150. %3 = OpTypeFunction %void
  151. %ulong = OpTypeInt 64 0
  152. %uint = OpTypeInt 32 0
  153. %_ptr_Function_ulong = OpTypePointer Function %ulong
  154. %subgroup = OpConstant %uint 3
  155. %uint_1 = OpConstant %uint 1
  156. %main = OpFunction %void None %3
  157. %5 = OpLabel
  158. %time1 = OpVariable %_ptr_Function_ulong Function
  159. %11 = OpReadClockKHR %ulong %subgroup
  160. OpStore %time1 %11
  161. OpReturn
  162. OpFunctionEnd)";
  163. CompileSuccessfully(spirv);
  164. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
  165. }
  166. TEST_F(ValidateMisc, ShaderClockDeviceScope) {
  167. const std::string spirv = ShaderClockSpriv + R"(
  168. %3 = OpTypeFunction %void
  169. %ulong = OpTypeInt 64 0
  170. %uint = OpTypeInt 32 0
  171. %_ptr_Function_ulong = OpTypePointer Function %ulong
  172. %device = OpConstant %uint 1
  173. %uint_1 = OpConstant %uint 1
  174. %main = OpFunction %void None %3
  175. %5 = OpLabel
  176. %time1 = OpVariable %_ptr_Function_ulong Function
  177. %11 = OpReadClockKHR %ulong %device
  178. OpStore %time1 %11
  179. OpReturn
  180. OpFunctionEnd)";
  181. CompileSuccessfully(spirv);
  182. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
  183. }
  184. TEST_F(ValidateMisc, ShaderClockWorkgroupScope) {
  185. const std::string spirv = ShaderClockSpriv + R"(
  186. %3 = OpTypeFunction %void
  187. %ulong = OpTypeInt 64 0
  188. %uint = OpTypeInt 32 0
  189. %_ptr_Function_ulong = OpTypePointer Function %ulong
  190. %workgroup = OpConstant %uint 2
  191. %uint_1 = OpConstant %uint 1
  192. %main = OpFunction %void None %3
  193. %5 = OpLabel
  194. %time1 = OpVariable %_ptr_Function_ulong Function
  195. %11 = OpReadClockKHR %ulong %workgroup
  196. OpStore %time1 %11
  197. OpReturn
  198. OpFunctionEnd)";
  199. CompileSuccessfully(spirv);
  200. EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  201. EXPECT_THAT(getDiagnosticString(),
  202. HasSubstr("Scope must be Subgroup or Device"));
  203. }
  204. TEST_F(ValidateMisc, VulkanShaderClockWorkgroupScope) {
  205. const std::string spirv = ShaderClockSpriv + R"(
  206. %3 = OpTypeFunction %void
  207. %ulong = OpTypeInt 64 0
  208. %uint = OpTypeInt 32 0
  209. %_ptr_Function_ulong = OpTypePointer Function %ulong
  210. %workgroup = OpConstant %uint 2
  211. %uint_1 = OpConstant %uint 1
  212. %main = OpFunction %void None %3
  213. %5 = OpLabel
  214. %time1 = OpVariable %_ptr_Function_ulong Function
  215. %11 = OpReadClockKHR %ulong %workgroup
  216. OpStore %time1 %11
  217. OpReturn
  218. OpFunctionEnd)";
  219. CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
  220. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0));
  221. EXPECT_THAT(getDiagnosticString(),
  222. AnyVUID("VUID-StandaloneSpirv-OpReadClockKHR-04652"));
  223. EXPECT_THAT(getDiagnosticString(),
  224. HasSubstr("Scope must be Subgroup or Device"));
  225. }
  226. TEST_F(ValidateMisc, UndefVoid) {
  227. const std::string spirv = R"(
  228. OpCapability Shader
  229. %1 = OpExtInstImport "GLSL.std.450"
  230. OpMemoryModel Logical GLSL450
  231. OpEntryPoint Fragment %4 "main"
  232. OpExecutionMode %4 OriginUpperLeft
  233. OpSource ESSL 320
  234. %2 = OpTypeVoid
  235. %10 = OpUndef %2
  236. %3 = OpTypeFunction %2
  237. %4 = OpFunction %2 None %3
  238. %5 = OpLabel
  239. OpReturn
  240. OpFunctionEnd
  241. )";
  242. CompileSuccessfully(spirv);
  243. EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
  244. EXPECT_THAT(getDiagnosticString(),
  245. HasSubstr("Cannot create undefined values with void type"));
  246. }
  247. } // namespace
  248. } // namespace val
  249. } // namespace spvtools