val_misc_test.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. TEST_F(ValidateMisc, SizeOfValid) {
  75. const std::string spirv = R"(
  76. OpCapability Addresses
  77. OpCapability Kernel
  78. OpMemoryModel Physical64 OpenCL
  79. OpEntryPoint Kernel %f "f"
  80. %void = OpTypeVoid
  81. %i32 = OpTypeInt 32 0
  82. %ptr = OpTypePointer CrossWorkgroup %i32
  83. %fnTy = OpTypeFunction %void
  84. %f = OpFunction %void None %fnTy
  85. %entry = OpLabel
  86. %s = OpSizeOf %i32 %ptr
  87. OpReturn
  88. OpFunctionEnd
  89. )";
  90. CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_1);
  91. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_1));
  92. }
  93. const std::string ShaderClockSpirv = R"(
  94. OpCapability Shader
  95. OpCapability Int64
  96. OpCapability ShaderClockKHR
  97. OpExtension "SPV_KHR_shader_clock"
  98. %1 = OpExtInstImport "GLSL.std.450"
  99. OpMemoryModel Logical GLSL450
  100. OpEntryPoint Fragment %main "main"
  101. OpExecutionMode %main OriginUpperLeft
  102. OpSource GLSL 450
  103. OpSourceExtension "GL_ARB_gpu_shader_int64"
  104. OpSourceExtension "GL_ARB_shader_clock"
  105. OpSourceExtension "GL_EXT_shader_realtime_clock"
  106. OpName %main "main"
  107. OpName %time1 "time1"
  108. %void = OpTypeVoid
  109. )";
  110. TEST_F(ValidateMisc, ShaderClockInt64) {
  111. const std::string spirv = ShaderClockSpirv + R"(
  112. %3 = OpTypeFunction %void
  113. %uint = OpTypeInt 32 0
  114. %_ptr_Function_uint = OpTypePointer Function %uint
  115. %uint_3 = OpConstant %uint 3
  116. %uint_1 = OpConstant %uint 1
  117. %main = OpFunction %void None %3
  118. %5 = OpLabel
  119. %time1 = OpVariable %_ptr_Function_uint Function
  120. %11 = OpReadClockKHR %uint %uint_3
  121. OpStore %time1 %11
  122. OpReturn
  123. OpFunctionEnd)";
  124. CompileSuccessfully(spirv);
  125. EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  126. EXPECT_THAT(getDiagnosticString(), HasSubstr("or 64bit unsigned integer"));
  127. }
  128. TEST_F(ValidateMisc, ShaderClockVec2) {
  129. const std::string spirv = ShaderClockSpirv + R"(
  130. %3 = OpTypeFunction %void
  131. %ulong = OpTypeInt 64 0
  132. %_ptr_Function_ulong = OpTypePointer Function %ulong
  133. %uint = OpTypeInt 32 0
  134. %uint_3 = OpConstant %uint 3
  135. %v2uint = OpTypeVector %ulong 2
  136. %_ptr_Function_v2uint = OpTypePointer Function %v2uint
  137. %main = OpFunction %void None %3
  138. %5 = OpLabel
  139. %time1 = OpVariable %_ptr_Function_v2uint Function
  140. %15 = OpReadClockKHR %v2uint %uint_3
  141. OpStore %time1 %15
  142. OpReturn
  143. OpFunctionEnd)";
  144. CompileSuccessfully(spirv);
  145. EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  146. EXPECT_THAT(getDiagnosticString(), HasSubstr("vector of two components"));
  147. }
  148. TEST_F(ValidateMisc, ShaderClockInvalidScopeValue) {
  149. const std::string spirv = ShaderClockSpirv + R"(
  150. %3 = OpTypeFunction %void
  151. %ulong = OpTypeInt 64 0
  152. %uint = OpTypeInt 32 0
  153. %_ptr_Function_ulong = OpTypePointer Function %ulong
  154. %uint_10 = OpConstant %uint 10
  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 %uint_10
  160. OpStore %time1 %11
  161. OpReturn
  162. OpFunctionEnd)";
  163. CompileSuccessfully(spirv);
  164. EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
  165. EXPECT_THAT(getDiagnosticString(), HasSubstr("Invalid scope value"));
  166. }
  167. TEST_F(ValidateMisc, ShaderClockSubgroupScope) {
  168. const std::string spirv = ShaderClockSpirv + R"(
  169. %3 = OpTypeFunction %void
  170. %ulong = OpTypeInt 64 0
  171. %uint = OpTypeInt 32 0
  172. %_ptr_Function_ulong = OpTypePointer Function %ulong
  173. %subgroup = OpConstant %uint 3
  174. %uint_1 = OpConstant %uint 1
  175. %main = OpFunction %void None %3
  176. %5 = OpLabel
  177. %time1 = OpVariable %_ptr_Function_ulong Function
  178. %11 = OpReadClockKHR %ulong %subgroup
  179. OpStore %time1 %11
  180. OpReturn
  181. OpFunctionEnd)";
  182. CompileSuccessfully(spirv);
  183. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
  184. }
  185. TEST_F(ValidateMisc, ShaderClockDeviceScope) {
  186. const std::string spirv = ShaderClockSpirv + R"(
  187. %3 = OpTypeFunction %void
  188. %ulong = OpTypeInt 64 0
  189. %uint = OpTypeInt 32 0
  190. %_ptr_Function_ulong = OpTypePointer Function %ulong
  191. %device = OpConstant %uint 1
  192. %uint_1 = OpConstant %uint 1
  193. %main = OpFunction %void None %3
  194. %5 = OpLabel
  195. %time1 = OpVariable %_ptr_Function_ulong Function
  196. %11 = OpReadClockKHR %ulong %device
  197. OpStore %time1 %11
  198. OpReturn
  199. OpFunctionEnd)";
  200. CompileSuccessfully(spirv);
  201. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
  202. }
  203. TEST_F(ValidateMisc, ShaderClockWorkgroupScope) {
  204. const std::string spirv = ShaderClockSpirv + R"(
  205. %3 = OpTypeFunction %void
  206. %ulong = OpTypeInt 64 0
  207. %uint = OpTypeInt 32 0
  208. %_ptr_Function_ulong = OpTypePointer Function %ulong
  209. %workgroup = OpConstant %uint 2
  210. %uint_1 = OpConstant %uint 1
  211. %main = OpFunction %void None %3
  212. %5 = OpLabel
  213. %time1 = OpVariable %_ptr_Function_ulong Function
  214. %11 = OpReadClockKHR %ulong %workgroup
  215. OpStore %time1 %11
  216. OpReturn
  217. OpFunctionEnd)";
  218. CompileSuccessfully(spirv);
  219. EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0));
  220. EXPECT_THAT(getDiagnosticString(),
  221. HasSubstr("Scope must be Subgroup or Device"));
  222. }
  223. TEST_F(ValidateMisc, VulkanShaderClockWorkgroupScope) {
  224. const std::string spirv = ShaderClockSpirv + R"(
  225. %3 = OpTypeFunction %void
  226. %ulong = OpTypeInt 64 0
  227. %uint = OpTypeInt 32 0
  228. %_ptr_Function_ulong = OpTypePointer Function %ulong
  229. %workgroup = OpConstant %uint 2
  230. %uint_1 = OpConstant %uint 1
  231. %main = OpFunction %void None %3
  232. %5 = OpLabel
  233. %time1 = OpVariable %_ptr_Function_ulong Function
  234. %11 = OpReadClockKHR %ulong %workgroup
  235. OpStore %time1 %11
  236. OpReturn
  237. OpFunctionEnd)";
  238. CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
  239. ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0));
  240. EXPECT_THAT(getDiagnosticString(),
  241. AnyVUID("VUID-StandaloneSpirv-OpReadClockKHR-04652"));
  242. EXPECT_THAT(getDiagnosticString(),
  243. HasSubstr("Scope must be Subgroup or Device"));
  244. }
  245. std::string GenKernelClockSpirv(const std::string& scope) {
  246. const std::string s = R"(
  247. OpCapability Kernel
  248. OpCapability Addresses
  249. OpCapability Int64
  250. OpCapability ShaderClockKHR
  251. OpExtension "SPV_KHR_shader_clock"
  252. OpMemoryModel Physical32 OpenCL
  253. OpEntryPoint Kernel %main "main"
  254. OpExecutionMode %main ContractionOff
  255. OpSource OpenCL_C 200000
  256. OpName %main "main"
  257. OpName %time1 "time1"
  258. %void = OpTypeVoid
  259. %3 = OpTypeFunction %void
  260. %ulong = OpTypeInt 64 0
  261. %uint = OpTypeInt 32 0
  262. %_ptr_Function_ulong = OpTypePointer Function %ulong
  263. %scope = OpConstant %uint )" +
  264. scope + R"(
  265. %main = OpFunction %void None %3
  266. %5 = OpLabel
  267. %time1 = OpVariable %_ptr_Function_ulong Function
  268. %11 = OpReadClockKHR %ulong %scope
  269. OpStore %time1 %11
  270. OpReturn
  271. OpFunctionEnd
  272. )";
  273. return s;
  274. }
  275. TEST_F(ValidateMisc, KernelClockScopeDevice) {
  276. CompileSuccessfully(GenKernelClockSpirv("1"), SPV_ENV_OPENCL_1_2);
  277. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_OPENCL_1_2));
  278. }
  279. TEST_F(ValidateMisc, KernelClockScopeWorkgroup) {
  280. CompileSuccessfully(GenKernelClockSpirv("2"), SPV_ENV_OPENCL_1_2);
  281. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_OPENCL_1_2));
  282. }
  283. TEST_F(ValidateMisc, KernelClockScopeSubgroup) {
  284. CompileSuccessfully(GenKernelClockSpirv("3"), SPV_ENV_OPENCL_1_2);
  285. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_OPENCL_1_2));
  286. }
  287. TEST_F(ValidateMisc, KernelClockScopeInvalid) {
  288. CompileSuccessfully(GenKernelClockSpirv("0"), SPV_ENV_OPENCL_1_2);
  289. EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_OPENCL_1_2));
  290. EXPECT_THAT(getDiagnosticString(),
  291. HasSubstr("Scope must be Subgroup, Workgroup, or Device"));
  292. }
  293. TEST_F(ValidateMisc, UndefVoid) {
  294. const std::string spirv = R"(
  295. OpCapability Shader
  296. %1 = OpExtInstImport "GLSL.std.450"
  297. OpMemoryModel Logical GLSL450
  298. OpEntryPoint Fragment %4 "main"
  299. OpExecutionMode %4 OriginUpperLeft
  300. OpSource ESSL 320
  301. %2 = OpTypeVoid
  302. %10 = OpUndef %2
  303. %3 = OpTypeFunction %2
  304. %4 = OpFunction %2 None %3
  305. %5 = OpLabel
  306. OpReturn
  307. OpFunctionEnd
  308. )";
  309. CompileSuccessfully(spirv);
  310. EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
  311. EXPECT_THAT(getDiagnosticString(),
  312. HasSubstr("Cannot create undefined values with void type"));
  313. }
  314. TEST_F(ValidateMisc, VulkanInvalidStorageClass) {
  315. const std::string spirv = R"(
  316. OpCapability Shader
  317. OpMemoryModel Logical GLSL450
  318. OpEntryPoint Vertex %func "shader"
  319. %int = OpTypeInt 32 0
  320. %ptr = OpTypePointer CrossWorkgroup %int
  321. %var = OpVariable %ptr CrossWorkgroup
  322. %void = OpTypeVoid
  323. %void_f = OpTypeFunction %void
  324. %func = OpFunction %void None %void_f
  325. %label = OpLabel
  326. OpReturn
  327. OpFunctionEnd
  328. )";
  329. CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
  330. ASSERT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions(SPV_ENV_VULKAN_1_0));
  331. EXPECT_THAT(getDiagnosticString(),
  332. AnyVUID("VUID-StandaloneSpirv-None-04643"));
  333. EXPECT_THAT(getDiagnosticString(),
  334. HasSubstr("Invalid storage class for target environment"));
  335. }
  336. TEST_F(ValidateMisc, CoopMat2WorkgroupLocalSizeIdPass) {
  337. const std::string body = R"(
  338. OpCapability Shader
  339. OpCapability Float16
  340. OpCapability Int16
  341. OpCapability CooperativeMatrixKHR
  342. OpExtension "SPV_KHR_cooperative_matrix"
  343. OpExtension "SPV_KHR_vulkan_memory_model"
  344. OpMemoryModel Logical GLSL450
  345. OpEntryPoint GLCompute %main "main"
  346. OpExecutionModeId %main LocalSizeId %u32_16 %u32_16 %u32_16
  347. %void = OpTypeVoid
  348. %func = OpTypeFunction %void
  349. %bool = OpTypeBool
  350. %f16 = OpTypeFloat 16
  351. %u32 = OpTypeInt 32 0
  352. %u32_16 = OpConstant %u32 16
  353. %use_Acc = OpConstant %u32 2
  354. %workgroup = OpConstant %u32 2
  355. %f16mat = OpTypeCooperativeMatrixKHR %f16 %workgroup %u32_16 %u32_16 %use_Acc
  356. %main = OpFunction %void None %func
  357. %main_entry = OpLabel
  358. OpReturn
  359. OpFunctionEnd)";
  360. CompileSuccessfully(body.c_str(), SPV_ENV_UNIVERSAL_1_3);
  361. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
  362. }
  363. TEST_F(ValidateMisc, CoopMat2WorkgroupLocalSizeIdConstantNotDeclaredYetFail) {
  364. const std::string body = R"(
  365. OpCapability Shader
  366. OpCapability Float16
  367. OpCapability Int16
  368. OpCapability CooperativeMatrixKHR
  369. OpExtension "SPV_KHR_cooperative_matrix"
  370. OpExtension "SPV_KHR_vulkan_memory_model"
  371. OpMemoryModel Logical GLSL450
  372. OpEntryPoint GLCompute %main "main"
  373. OpExecutionModeId %main LocalSizeId %u32_16 %u32_8 %u32_16
  374. %void = OpTypeVoid
  375. %func = OpTypeFunction %void
  376. %bool = OpTypeBool
  377. %f16 = OpTypeFloat 16
  378. %u32 = OpTypeInt 32 0
  379. %u32_16 = OpConstant %u32 16
  380. %use_Acc = OpConstant %u32 2
  381. %workgroup = OpConstant %u32 2
  382. %f16mat = OpTypeCooperativeMatrixKHR %f16 %workgroup %u32_16 %u32_16 %use_Acc
  383. %u32_8 = OpConstant %u32 8
  384. %main = OpFunction %void None %func
  385. %main_entry = OpLabel
  386. OpReturn
  387. OpFunctionEnd)";
  388. CompileSuccessfully(body.c_str(), SPV_ENV_UNIVERSAL_1_3);
  389. EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
  390. EXPECT_THAT(getDiagnosticString(),
  391. HasSubstr("OpTypeCooperativeMatrixKHR with ScopeWorkgroup used "
  392. "before LocalSizeId constant value"));
  393. }
  394. } // namespace
  395. } // namespace val
  396. } // namespace spvtools