val_function_test.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  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. #include <sstream>
  15. #include <string>
  16. #include <tuple>
  17. #include "gmock/gmock.h"
  18. #include "test/test_fixture.h"
  19. #include "test/unit_spirv.h"
  20. #include "test/val/val_fixtures.h"
  21. namespace spvtools {
  22. namespace val {
  23. namespace {
  24. using ::testing::Combine;
  25. using ::testing::HasSubstr;
  26. using ::testing::Values;
  27. using ValidateFunctionCall = spvtest::ValidateBase<std::string>;
  28. std::string GenerateShader(const std::string& storage_class,
  29. const std::string& capabilities,
  30. const std::string& extensions) {
  31. std::string spirv = R"(
  32. OpCapability Shader
  33. OpCapability Linkage
  34. OpCapability AtomicStorage
  35. )" + capabilities + R"(
  36. OpExtension "SPV_KHR_storage_buffer_storage_class"
  37. )" +
  38. extensions + R"(
  39. OpMemoryModel Logical GLSL450
  40. OpName %var "var"
  41. %void = OpTypeVoid
  42. %int = OpTypeInt 32 0
  43. %ptr = OpTypePointer )" + storage_class + R"( %int
  44. %caller_ty = OpTypeFunction %void
  45. %callee_ty = OpTypeFunction %void %ptr
  46. )";
  47. if (storage_class != "Function") {
  48. spirv += "%var = OpVariable %ptr " + storage_class;
  49. }
  50. spirv += R"(
  51. %caller = OpFunction %void None %caller_ty
  52. %1 = OpLabel
  53. )";
  54. if (storage_class == "Function") {
  55. spirv += "%var = OpVariable %ptr Function";
  56. }
  57. spirv += R"(
  58. %call = OpFunctionCall %void %callee %var
  59. OpReturn
  60. OpFunctionEnd
  61. %callee = OpFunction %void None %callee_ty
  62. %param = OpFunctionParameter %ptr
  63. %2 = OpLabel
  64. OpReturn
  65. OpFunctionEnd
  66. )";
  67. return spirv;
  68. }
  69. std::string GenerateShaderParameter(const std::string& storage_class,
  70. const std::string& capabilities,
  71. const std::string& extensions) {
  72. std::string spirv = R"(
  73. OpCapability Shader
  74. OpCapability Linkage
  75. OpCapability AtomicStorage
  76. )" + capabilities + R"(
  77. OpExtension "SPV_KHR_storage_buffer_storage_class"
  78. )" +
  79. extensions + R"(
  80. OpMemoryModel Logical GLSL450
  81. OpName %p "p"
  82. %void = OpTypeVoid
  83. %int = OpTypeInt 32 0
  84. %ptr = OpTypePointer )" + storage_class + R"( %int
  85. %func_ty = OpTypeFunction %void %ptr
  86. %caller = OpFunction %void None %func_ty
  87. %p = OpFunctionParameter %ptr
  88. %1 = OpLabel
  89. %call = OpFunctionCall %void %callee %p
  90. OpReturn
  91. OpFunctionEnd
  92. %callee = OpFunction %void None %func_ty
  93. %param = OpFunctionParameter %ptr
  94. %2 = OpLabel
  95. OpReturn
  96. OpFunctionEnd
  97. )";
  98. return spirv;
  99. }
  100. std::string GenerateShaderAccessChain(const std::string& storage_class,
  101. const std::string& capabilities,
  102. const std::string& extensions) {
  103. std::string spirv = R"(
  104. OpCapability Shader
  105. OpCapability Linkage
  106. OpCapability AtomicStorage
  107. )" + capabilities + R"(
  108. OpExtension "SPV_KHR_storage_buffer_storage_class"
  109. )" +
  110. extensions + R"(
  111. OpMemoryModel Logical GLSL450
  112. OpName %var "var"
  113. OpName %gep "gep"
  114. %void = OpTypeVoid
  115. %int = OpTypeInt 32 0
  116. %int2 = OpTypeVector %int 2
  117. %int_0 = OpConstant %int 0
  118. %ptr = OpTypePointer )" + storage_class + R"( %int2
  119. %ptr2 = OpTypePointer )" +
  120. storage_class + R"( %int
  121. %caller_ty = OpTypeFunction %void
  122. %callee_ty = OpTypeFunction %void %ptr2
  123. )";
  124. if (storage_class != "Function") {
  125. spirv += "%var = OpVariable %ptr " + storage_class;
  126. }
  127. spirv += R"(
  128. %caller = OpFunction %void None %caller_ty
  129. %1 = OpLabel
  130. )";
  131. if (storage_class == "Function") {
  132. spirv += "%var = OpVariable %ptr Function";
  133. }
  134. spirv += R"(
  135. %gep = OpAccessChain %ptr2 %var %int_0
  136. %call = OpFunctionCall %void %callee %gep
  137. OpReturn
  138. OpFunctionEnd
  139. %callee = OpFunction %void None %callee_ty
  140. %param = OpFunctionParameter %ptr2
  141. %2 = OpLabel
  142. OpReturn
  143. OpFunctionEnd
  144. )";
  145. return spirv;
  146. }
  147. TEST_P(ValidateFunctionCall, VariableNoVariablePointers) {
  148. const std::string storage_class = GetParam();
  149. std::string spirv = GenerateShader(storage_class, "", "");
  150. const std::vector<std::string> valid_storage_classes = {
  151. "UniformConstant", "Function", "Private", "Workgroup", "AtomicCounter"};
  152. bool valid =
  153. std::find(valid_storage_classes.begin(), valid_storage_classes.end(),
  154. storage_class) != valid_storage_classes.end();
  155. CompileSuccessfully(spirv);
  156. if (valid) {
  157. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
  158. } else {
  159. EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
  160. if (storage_class == "StorageBuffer") {
  161. EXPECT_THAT(getDiagnosticString(),
  162. HasSubstr("StorageBuffer pointer operand 1[%var] requires a "
  163. "variable pointers capability"));
  164. } else {
  165. EXPECT_THAT(
  166. getDiagnosticString(),
  167. HasSubstr("Invalid storage class for pointer operand 1[%var]"));
  168. }
  169. }
  170. }
  171. TEST_P(ValidateFunctionCall, VariableVariablePointersStorageClass) {
  172. const std::string storage_class = GetParam();
  173. std::string spirv = GenerateShader(
  174. storage_class, "OpCapability VariablePointersStorageBuffer",
  175. "OpExtension \"SPV_KHR_variable_pointers\"");
  176. const std::vector<std::string> valid_storage_classes = {
  177. "UniformConstant", "Function", "Private",
  178. "Workgroup", "StorageBuffer", "AtomicCounter"};
  179. bool valid =
  180. std::find(valid_storage_classes.begin(), valid_storage_classes.end(),
  181. storage_class) != valid_storage_classes.end();
  182. CompileSuccessfully(spirv);
  183. if (valid) {
  184. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
  185. } else {
  186. EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
  187. EXPECT_THAT(getDiagnosticString(),
  188. HasSubstr("Invalid storage class for pointer operand 1[%var]"));
  189. }
  190. }
  191. TEST_P(ValidateFunctionCall, VariableVariablePointers) {
  192. const std::string storage_class = GetParam();
  193. std::string spirv =
  194. GenerateShader(storage_class, "OpCapability VariablePointers",
  195. "OpExtension \"SPV_KHR_variable_pointers\"");
  196. const std::vector<std::string> valid_storage_classes = {
  197. "UniformConstant", "Function", "Private",
  198. "Workgroup", "StorageBuffer", "AtomicCounter"};
  199. bool valid =
  200. std::find(valid_storage_classes.begin(), valid_storage_classes.end(),
  201. storage_class) != valid_storage_classes.end();
  202. CompileSuccessfully(spirv);
  203. if (valid) {
  204. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
  205. } else {
  206. EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
  207. EXPECT_THAT(getDiagnosticString(),
  208. HasSubstr("Invalid storage class for pointer operand 1[%var]"));
  209. }
  210. }
  211. TEST_P(ValidateFunctionCall, ParameterNoVariablePointers) {
  212. const std::string storage_class = GetParam();
  213. std::string spirv = GenerateShaderParameter(storage_class, "", "");
  214. const std::vector<std::string> valid_storage_classes = {
  215. "UniformConstant", "Function", "Private", "Workgroup", "AtomicCounter"};
  216. bool valid =
  217. std::find(valid_storage_classes.begin(), valid_storage_classes.end(),
  218. storage_class) != valid_storage_classes.end();
  219. CompileSuccessfully(spirv);
  220. if (valid) {
  221. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
  222. } else {
  223. EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
  224. if (storage_class == "StorageBuffer") {
  225. EXPECT_THAT(getDiagnosticString(),
  226. HasSubstr("StorageBuffer pointer operand 1[%p] requires a "
  227. "variable pointers capability"));
  228. } else {
  229. EXPECT_THAT(getDiagnosticString(),
  230. HasSubstr("Invalid storage class for pointer operand 1[%p]"));
  231. }
  232. }
  233. }
  234. TEST_P(ValidateFunctionCall, ParameterVariablePointersStorageBuffer) {
  235. const std::string storage_class = GetParam();
  236. std::string spirv = GenerateShaderParameter(
  237. storage_class, "OpCapability VariablePointersStorageBuffer",
  238. "OpExtension \"SPV_KHR_variable_pointers\"");
  239. const std::vector<std::string> valid_storage_classes = {
  240. "UniformConstant", "Function", "Private",
  241. "Workgroup", "StorageBuffer", "AtomicCounter"};
  242. bool valid =
  243. std::find(valid_storage_classes.begin(), valid_storage_classes.end(),
  244. storage_class) != valid_storage_classes.end();
  245. CompileSuccessfully(spirv);
  246. if (valid) {
  247. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
  248. } else {
  249. EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
  250. EXPECT_THAT(getDiagnosticString(),
  251. HasSubstr("Invalid storage class for pointer operand 1[%p]"));
  252. }
  253. }
  254. TEST_P(ValidateFunctionCall, ParameterVariablePointers) {
  255. const std::string storage_class = GetParam();
  256. std::string spirv =
  257. GenerateShaderParameter(storage_class, "OpCapability VariablePointers",
  258. "OpExtension \"SPV_KHR_variable_pointers\"");
  259. const std::vector<std::string> valid_storage_classes = {
  260. "UniformConstant", "Function", "Private",
  261. "Workgroup", "StorageBuffer", "AtomicCounter"};
  262. bool valid =
  263. std::find(valid_storage_classes.begin(), valid_storage_classes.end(),
  264. storage_class) != valid_storage_classes.end();
  265. CompileSuccessfully(spirv);
  266. if (valid) {
  267. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
  268. } else {
  269. EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
  270. EXPECT_THAT(getDiagnosticString(),
  271. HasSubstr("Invalid storage class for pointer operand 1[%p]"));
  272. }
  273. }
  274. TEST_P(ValidateFunctionCall, NonMemoryObjectDeclarationNoVariablePointers) {
  275. const std::string storage_class = GetParam();
  276. std::string spirv = GenerateShaderAccessChain(storage_class, "", "");
  277. const std::vector<std::string> valid_storage_classes = {
  278. "Function", "Private", "Workgroup", "AtomicCounter"};
  279. bool valid_sc =
  280. std::find(valid_storage_classes.begin(), valid_storage_classes.end(),
  281. storage_class) != valid_storage_classes.end();
  282. CompileSuccessfully(spirv);
  283. spv_result_t expected_result =
  284. storage_class == "UniformConstant" ? SPV_SUCCESS : SPV_ERROR_INVALID_ID;
  285. EXPECT_EQ(expected_result, ValidateInstructions());
  286. if (valid_sc) {
  287. EXPECT_THAT(
  288. getDiagnosticString(),
  289. HasSubstr(
  290. "Pointer operand 2[%gep] must be a memory object declaration"));
  291. } else {
  292. if (storage_class == "StorageBuffer") {
  293. EXPECT_THAT(getDiagnosticString(),
  294. HasSubstr("StorageBuffer pointer operand 2[%gep] requires a "
  295. "variable pointers capability"));
  296. } else if (storage_class != "UniformConstant") {
  297. EXPECT_THAT(
  298. getDiagnosticString(),
  299. HasSubstr("Invalid storage class for pointer operand 2[%gep]"));
  300. }
  301. }
  302. }
  303. TEST_P(ValidateFunctionCall,
  304. NonMemoryObjectDeclarationVariablePointersStorageBuffer) {
  305. const std::string storage_class = GetParam();
  306. std::string spirv = GenerateShaderAccessChain(
  307. storage_class, "OpCapability VariablePointersStorageBuffer",
  308. "OpExtension \"SPV_KHR_variable_pointers\"");
  309. const std::vector<std::string> valid_storage_classes = {
  310. "Function", "Private", "Workgroup", "StorageBuffer", "AtomicCounter"};
  311. bool valid_sc =
  312. std::find(valid_storage_classes.begin(), valid_storage_classes.end(),
  313. storage_class) != valid_storage_classes.end();
  314. bool validate =
  315. storage_class == "StorageBuffer" || storage_class == "UniformConstant";
  316. CompileSuccessfully(spirv);
  317. if (validate) {
  318. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
  319. } else {
  320. EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
  321. if (valid_sc) {
  322. EXPECT_THAT(
  323. getDiagnosticString(),
  324. HasSubstr(
  325. "Pointer operand 2[%gep] must be a memory object declaration"));
  326. } else {
  327. EXPECT_THAT(
  328. getDiagnosticString(),
  329. HasSubstr("Invalid storage class for pointer operand 2[%gep]"));
  330. }
  331. }
  332. }
  333. TEST_P(ValidateFunctionCall, NonMemoryObjectDeclarationVariablePointers) {
  334. const std::string storage_class = GetParam();
  335. std::string spirv =
  336. GenerateShaderAccessChain(storage_class, "OpCapability VariablePointers",
  337. "OpExtension \"SPV_KHR_variable_pointers\"");
  338. const std::vector<std::string> valid_storage_classes = {
  339. "Function", "Private", "Workgroup", "StorageBuffer", "AtomicCounter"};
  340. bool valid_sc =
  341. std::find(valid_storage_classes.begin(), valid_storage_classes.end(),
  342. storage_class) != valid_storage_classes.end();
  343. bool validate = storage_class == "StorageBuffer" ||
  344. storage_class == "Workgroup" ||
  345. storage_class == "UniformConstant";
  346. CompileSuccessfully(spirv);
  347. if (validate) {
  348. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
  349. } else {
  350. EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
  351. if (valid_sc) {
  352. EXPECT_THAT(
  353. getDiagnosticString(),
  354. HasSubstr(
  355. "Pointer operand 2[%gep] must be a memory object declaration"));
  356. } else {
  357. EXPECT_THAT(
  358. getDiagnosticString(),
  359. HasSubstr("Invalid storage class for pointer operand 2[%gep]"));
  360. }
  361. }
  362. }
  363. TEST_F(ValidateFunctionCall, LogicallyMatchingPointers) {
  364. std::string spirv =
  365. R"(
  366. OpCapability Shader
  367. OpMemoryModel Logical GLSL450
  368. OpEntryPoint GLCompute %1 "main"
  369. OpExecutionMode %1 LocalSize 1 1 1
  370. OpSource HLSL 600
  371. OpDecorate %2 DescriptorSet 0
  372. OpDecorate %2 Binding 0
  373. OpMemberDecorate %_struct_3 0 Offset 0
  374. OpDecorate %_runtimearr__struct_3 ArrayStride 4
  375. OpMemberDecorate %_struct_5 0 Offset 0
  376. OpDecorate %_struct_5 BufferBlock
  377. %int = OpTypeInt 32 1
  378. %int_0 = OpConstant %int 0
  379. %uint = OpTypeInt 32 0
  380. %uint_0 = OpConstant %uint 0
  381. %_struct_3 = OpTypeStruct %int
  382. %_runtimearr__struct_3 = OpTypeRuntimeArray %_struct_3
  383. %_struct_5 = OpTypeStruct %_runtimearr__struct_3
  384. %_ptr_Uniform__struct_5 = OpTypePointer Uniform %_struct_5
  385. %void = OpTypeVoid
  386. %14 = OpTypeFunction %void
  387. %_struct_15 = OpTypeStruct %int
  388. %_ptr_Function__struct_15 = OpTypePointer Function %_struct_15
  389. %_ptr_Uniform__struct_3 = OpTypePointer Uniform %_struct_3
  390. %18 = OpTypeFunction %void %_ptr_Function__struct_15
  391. %2 = OpVariable %_ptr_Uniform__struct_5 Uniform
  392. %1 = OpFunction %void None %14
  393. %19 = OpLabel
  394. %20 = OpAccessChain %_ptr_Uniform__struct_3 %2 %int_0 %uint_0
  395. %21 = OpFunctionCall %void %22 %20
  396. OpReturn
  397. OpFunctionEnd
  398. %22 = OpFunction %void None %18
  399. %23 = OpFunctionParameter %_ptr_Function__struct_15
  400. %24 = OpLabel
  401. OpReturn
  402. OpFunctionEnd
  403. )";
  404. CompileSuccessfully(spirv);
  405. spvValidatorOptionsSetBeforeHlslLegalization(getValidatorOptions(), true);
  406. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
  407. }
  408. TEST_F(ValidateFunctionCall, LogicallyMatchingPointersNestedStruct) {
  409. std::string spirv =
  410. R"(
  411. OpCapability Shader
  412. OpMemoryModel Logical GLSL450
  413. OpEntryPoint GLCompute %1 "main"
  414. OpExecutionMode %1 LocalSize 1 1 1
  415. OpSource HLSL 600
  416. OpDecorate %2 DescriptorSet 0
  417. OpDecorate %2 Binding 0
  418. OpMemberDecorate %_struct_3 0 Offset 0
  419. OpMemberDecorate %_struct_4 0 Offset 0
  420. OpDecorate %_runtimearr__struct_4 ArrayStride 4
  421. OpMemberDecorate %_struct_6 0 Offset 0
  422. OpDecorate %_struct_6 BufferBlock
  423. %int = OpTypeInt 32 1
  424. %int_0 = OpConstant %int 0
  425. %uint = OpTypeInt 32 0
  426. %uint_0 = OpConstant %uint 0
  427. %_struct_3 = OpTypeStruct %int
  428. %_struct_4 = OpTypeStruct %_struct_3
  429. %_runtimearr__struct_4 = OpTypeRuntimeArray %_struct_4
  430. %_struct_6 = OpTypeStruct %_runtimearr__struct_4
  431. %_ptr_Uniform__struct_6 = OpTypePointer Uniform %_struct_6
  432. %void = OpTypeVoid
  433. %13 = OpTypeFunction %void
  434. %_struct_14 = OpTypeStruct %int
  435. %_struct_15 = OpTypeStruct %_struct_14
  436. %_ptr_Function__struct_15 = OpTypePointer Function %_struct_15
  437. %_ptr_Uniform__struct_4 = OpTypePointer Uniform %_struct_4
  438. %18 = OpTypeFunction %void %_ptr_Function__struct_15
  439. %2 = OpVariable %_ptr_Uniform__struct_6 Uniform
  440. %1 = OpFunction %void None %13
  441. %19 = OpLabel
  442. %20 = OpVariable %_ptr_Function__struct_15 Function
  443. %21 = OpAccessChain %_ptr_Uniform__struct_4 %2 %int_0 %uint_0
  444. %22 = OpFunctionCall %void %23 %21
  445. OpReturn
  446. OpFunctionEnd
  447. %23 = OpFunction %void None %18
  448. %24 = OpFunctionParameter %_ptr_Function__struct_15
  449. %25 = OpLabel
  450. OpReturn
  451. OpFunctionEnd
  452. )";
  453. CompileSuccessfully(spirv);
  454. spvValidatorOptionsSetBeforeHlslLegalization(getValidatorOptions(), true);
  455. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
  456. }
  457. TEST_F(ValidateFunctionCall, LogicallyMatchingPointersNestedArray) {
  458. std::string spirv =
  459. R"(
  460. OpCapability Shader
  461. OpMemoryModel Logical GLSL450
  462. OpEntryPoint GLCompute %1 "main"
  463. OpExecutionMode %1 LocalSize 1 1 1
  464. OpSource HLSL 600
  465. OpDecorate %2 DescriptorSet 0
  466. OpDecorate %2 Binding 0
  467. OpDecorate %_arr_int_uint_10 ArrayStride 4
  468. OpMemberDecorate %_struct_4 0 Offset 0
  469. OpDecorate %_runtimearr__struct_4 ArrayStride 40
  470. OpMemberDecorate %_struct_6 0 Offset 0
  471. OpDecorate %_struct_6 BufferBlock
  472. %int = OpTypeInt 32 1
  473. %int_0 = OpConstant %int 0
  474. %uint = OpTypeInt 32 0
  475. %uint_0 = OpConstant %uint 0
  476. %uint_10 = OpConstant %uint 10
  477. %_arr_int_uint_10 = OpTypeArray %int %uint_10
  478. %_struct_4 = OpTypeStruct %_arr_int_uint_10
  479. %_runtimearr__struct_4 = OpTypeRuntimeArray %_struct_4
  480. %_struct_6 = OpTypeStruct %_runtimearr__struct_4
  481. %_ptr_Uniform__struct_6 = OpTypePointer Uniform %_struct_6
  482. %void = OpTypeVoid
  483. %14 = OpTypeFunction %void
  484. %_ptr_Uniform__struct_4 = OpTypePointer Uniform %_struct_4
  485. %_arr_int_uint_10_0 = OpTypeArray %int %uint_10
  486. %_struct_17 = OpTypeStruct %_arr_int_uint_10_0
  487. %_ptr_Function__struct_17 = OpTypePointer Function %_struct_17
  488. %19 = OpTypeFunction %void %_ptr_Function__struct_17
  489. %2 = OpVariable %_ptr_Uniform__struct_6 Uniform
  490. %1 = OpFunction %void None %14
  491. %20 = OpLabel
  492. %21 = OpAccessChain %_ptr_Uniform__struct_4 %2 %int_0 %uint_0
  493. %22 = OpFunctionCall %void %23 %21
  494. OpReturn
  495. OpFunctionEnd
  496. %23 = OpFunction %void None %19
  497. %24 = OpFunctionParameter %_ptr_Function__struct_17
  498. %25 = OpLabel
  499. OpReturn
  500. OpFunctionEnd
  501. )";
  502. CompileSuccessfully(spirv);
  503. spvValidatorOptionsSetBeforeHlslLegalization(getValidatorOptions(), true);
  504. EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
  505. }
  506. TEST_F(ValidateFunctionCall, LogicallyMismatchedPointersMissingMember) {
  507. // Validation should fail because the formal parameter type has two members,
  508. // while the actual parameter only has 1.
  509. std::string spirv =
  510. R"(
  511. OpCapability Shader
  512. OpMemoryModel Logical GLSL450
  513. OpEntryPoint GLCompute %1 "main"
  514. OpExecutionMode %1 LocalSize 1 1 1
  515. OpSource HLSL 600
  516. OpDecorate %2 DescriptorSet 0
  517. OpDecorate %2 Binding 0
  518. OpMemberDecorate %_struct_3 0 Offset 0
  519. OpDecorate %_runtimearr__struct_3 ArrayStride 4
  520. OpMemberDecorate %_struct_5 0 Offset 0
  521. OpDecorate %_struct_5 BufferBlock
  522. %int = OpTypeInt 32 1
  523. %int_0 = OpConstant %int 0
  524. %uint = OpTypeInt 32 0
  525. %uint_0 = OpConstant %uint 0
  526. %_struct_3 = OpTypeStruct %int
  527. %_runtimearr__struct_3 = OpTypeRuntimeArray %_struct_3
  528. %_struct_5 = OpTypeStruct %_runtimearr__struct_3
  529. %_ptr_Uniform__struct_5 = OpTypePointer Uniform %_struct_5
  530. %void = OpTypeVoid
  531. %14 = OpTypeFunction %void
  532. %_struct_15 = OpTypeStruct %int %int
  533. %_ptr_Function__struct_15 = OpTypePointer Function %_struct_15
  534. %_ptr_Uniform__struct_3 = OpTypePointer Uniform %_struct_3
  535. %18 = OpTypeFunction %void %_ptr_Function__struct_15
  536. %2 = OpVariable %_ptr_Uniform__struct_5 Uniform
  537. %1 = OpFunction %void None %14
  538. %19 = OpLabel
  539. %20 = OpAccessChain %_ptr_Uniform__struct_3 %2 %int_0 %uint_0
  540. %21 = OpFunctionCall %void %22 %20
  541. OpReturn
  542. OpFunctionEnd
  543. %22 = OpFunction %void None %18
  544. %23 = OpFunctionParameter %_ptr_Function__struct_15
  545. %24 = OpLabel
  546. OpReturn
  547. OpFunctionEnd
  548. )";
  549. CompileSuccessfully(spirv);
  550. spvValidatorOptionsSetBeforeHlslLegalization(getValidatorOptions(), true);
  551. EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
  552. EXPECT_THAT(getDiagnosticString(), HasSubstr("OpFunctionCall Argument <id>"));
  553. EXPECT_THAT(getDiagnosticString(),
  554. HasSubstr("type does not match Function <id>"));
  555. }
  556. TEST_F(ValidateFunctionCall, LogicallyMismatchedPointersDifferentMemberType) {
  557. // Validation should fail because the formal parameter has a member that is
  558. // a different type than the actual parameter.
  559. std::string spirv =
  560. R"(
  561. OpCapability Shader
  562. OpMemoryModel Logical GLSL450
  563. OpEntryPoint GLCompute %1 "main"
  564. OpExecutionMode %1 LocalSize 1 1 1
  565. OpSource HLSL 600
  566. OpDecorate %2 DescriptorSet 0
  567. OpDecorate %2 Binding 0
  568. OpMemberDecorate %_struct_3 0 Offset 0
  569. OpDecorate %_runtimearr__struct_3 ArrayStride 4
  570. OpMemberDecorate %_struct_5 0 Offset 0
  571. OpDecorate %_struct_5 BufferBlock
  572. %int = OpTypeInt 32 1
  573. %int_0 = OpConstant %int 0
  574. %uint = OpTypeInt 32 0
  575. %uint_0 = OpConstant %uint 0
  576. %_struct_3 = OpTypeStruct %uint
  577. %_runtimearr__struct_3 = OpTypeRuntimeArray %_struct_3
  578. %_struct_5 = OpTypeStruct %_runtimearr__struct_3
  579. %_ptr_Uniform__struct_5 = OpTypePointer Uniform %_struct_5
  580. %void = OpTypeVoid
  581. %14 = OpTypeFunction %void
  582. %_struct_15 = OpTypeStruct %int
  583. %_ptr_Function__struct_15 = OpTypePointer Function %_struct_15
  584. %_ptr_Uniform__struct_3 = OpTypePointer Uniform %_struct_3
  585. %18 = OpTypeFunction %void %_ptr_Function__struct_15
  586. %2 = OpVariable %_ptr_Uniform__struct_5 Uniform
  587. %1 = OpFunction %void None %14
  588. %19 = OpLabel
  589. %20 = OpAccessChain %_ptr_Uniform__struct_3 %2 %int_0 %uint_0
  590. %21 = OpFunctionCall %void %22 %20
  591. OpReturn
  592. OpFunctionEnd
  593. %22 = OpFunction %void None %18
  594. %23 = OpFunctionParameter %_ptr_Function__struct_15
  595. %24 = OpLabel
  596. OpReturn
  597. OpFunctionEnd
  598. )";
  599. CompileSuccessfully(spirv);
  600. spvValidatorOptionsSetBeforeHlslLegalization(getValidatorOptions(), true);
  601. EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
  602. EXPECT_THAT(getDiagnosticString(), HasSubstr("OpFunctionCall Argument <id>"));
  603. EXPECT_THAT(getDiagnosticString(),
  604. HasSubstr("type does not match Function <id>"));
  605. }
  606. TEST_F(ValidateFunctionCall,
  607. LogicallyMismatchedPointersIncompatableDecorations) {
  608. // Validation should fail because the formal parameter has an incompatible
  609. // decoration.
  610. std::string spirv =
  611. R"(
  612. OpCapability Shader
  613. OpMemoryModel Logical GLSL450
  614. OpEntryPoint GLCompute %1 "main"
  615. OpExecutionMode %1 LocalSize 1 1 1
  616. OpSource HLSL 600
  617. OpDecorate %2 DescriptorSet 0
  618. OpDecorate %2 Binding 0
  619. OpMemberDecorate %_struct_3 0 Offset 0
  620. OpDecorate %_runtimearr__struct_3 ArrayStride 4
  621. OpMemberDecorate %_struct_5 0 Offset 0
  622. OpDecorate %_struct_5 Block
  623. OpMemberDecorate %_struct_15 0 NonWritable
  624. %int = OpTypeInt 32 1
  625. %int_0 = OpConstant %int 0
  626. %uint = OpTypeInt 32 0
  627. %uint_0 = OpConstant %uint 0
  628. %_struct_3 = OpTypeStruct %int
  629. %_runtimearr__struct_3 = OpTypeRuntimeArray %_struct_3
  630. %_struct_5 = OpTypeStruct %_runtimearr__struct_3
  631. %_ptr_StorageBuffer__struct_5 = OpTypePointer StorageBuffer %_struct_5
  632. %void = OpTypeVoid
  633. %14 = OpTypeFunction %void
  634. %_struct_15 = OpTypeStruct %int
  635. %_ptr_Function__struct_15 = OpTypePointer Function %_struct_15
  636. %_ptr_StorageBuffer__struct_3 = OpTypePointer StorageBuffer %_struct_3
  637. %18 = OpTypeFunction %void %_ptr_Function__struct_15
  638. %2 = OpVariable %_ptr_StorageBuffer__struct_5 StorageBuffer
  639. %1 = OpFunction %void None %14
  640. %19 = OpLabel
  641. %20 = OpAccessChain %_ptr_StorageBuffer__struct_3 %2 %int_0 %uint_0
  642. %21 = OpFunctionCall %void %22 %20
  643. OpReturn
  644. OpFunctionEnd
  645. %22 = OpFunction %void None %18
  646. %23 = OpFunctionParameter %_ptr_Function__struct_15
  647. %24 = OpLabel
  648. OpReturn
  649. OpFunctionEnd
  650. )";
  651. CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
  652. spvValidatorOptionsSetBeforeHlslLegalization(getValidatorOptions(), true);
  653. EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
  654. EXPECT_THAT(getDiagnosticString(), HasSubstr("OpFunctionCall Argument <id>"));
  655. EXPECT_THAT(getDiagnosticString(),
  656. HasSubstr("type does not match Function <id>"));
  657. }
  658. TEST_F(ValidateFunctionCall,
  659. LogicallyMismatchedPointersIncompatableDecorations2) {
  660. // Validation should fail because the formal parameter has an incompatible
  661. // decoration.
  662. std::string spirv =
  663. R"(
  664. OpCapability Shader
  665. OpMemoryModel Logical GLSL450
  666. OpEntryPoint GLCompute %1 "main"
  667. OpExecutionMode %1 LocalSize 1 1 1
  668. OpSource HLSL 600
  669. OpDecorate %2 DescriptorSet 0
  670. OpDecorate %2 Binding 0
  671. OpMemberDecorate %_struct_3 0 Offset 0
  672. OpDecorate %_runtimearr__struct_3 ArrayStride 4
  673. OpMemberDecorate %_struct_5 0 Offset 0
  674. OpDecorate %_struct_5 BufferBlock
  675. OpDecorate %_ptr_Uniform__struct_3 ArrayStride 4
  676. OpDecorate %_ptr_Uniform__struct_3_0 ArrayStride 8
  677. %int = OpTypeInt 32 1
  678. %int_0 = OpConstant %int 0
  679. %uint = OpTypeInt 32 0
  680. %uint_0 = OpConstant %uint 0
  681. %_struct_3 = OpTypeStruct %int
  682. %_runtimearr__struct_3 = OpTypeRuntimeArray %_struct_3
  683. %_struct_5 = OpTypeStruct %_runtimearr__struct_3
  684. %_ptr_Uniform__struct_5 = OpTypePointer Uniform %_struct_5
  685. %void = OpTypeVoid
  686. %14 = OpTypeFunction %void
  687. %_ptr_Uniform__struct_3 = OpTypePointer Uniform %_struct_3
  688. %_ptr_Uniform__struct_3_0 = OpTypePointer Uniform %_struct_3
  689. %18 = OpTypeFunction %void %_ptr_Uniform__struct_3_0
  690. %2 = OpVariable %_ptr_Uniform__struct_5 Uniform
  691. %1 = OpFunction %void None %14
  692. %19 = OpLabel
  693. %20 = OpAccessChain %_ptr_Uniform__struct_3 %2 %int_0 %uint_0
  694. %21 = OpFunctionCall %void %22 %20
  695. OpReturn
  696. OpFunctionEnd
  697. %22 = OpFunction %void None %18
  698. %23 = OpFunctionParameter %_ptr_Uniform__struct_3_0
  699. %24 = OpLabel
  700. OpReturn
  701. OpFunctionEnd
  702. )";
  703. CompileSuccessfully(spirv);
  704. spvValidatorOptionsSetBeforeHlslLegalization(getValidatorOptions(), true);
  705. EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
  706. EXPECT_THAT(getDiagnosticString(), HasSubstr("OpFunctionCall Argument <id>"));
  707. EXPECT_THAT(getDiagnosticString(),
  708. HasSubstr("type does not match Function <id>"));
  709. }
  710. TEST_F(ValidateFunctionCall, LogicallyMismatchedPointersArraySize) {
  711. // Validation should fail because the formal parameter array has a different
  712. // number of element than the actual parameter.
  713. std::string spirv =
  714. R"(
  715. OpCapability Shader
  716. OpMemoryModel Logical GLSL450
  717. OpEntryPoint GLCompute %1 "main"
  718. OpExecutionMode %1 LocalSize 1 1 1
  719. OpSource HLSL 600
  720. OpDecorate %2 DescriptorSet 0
  721. OpDecorate %2 Binding 0
  722. OpDecorate %_arr_int_uint_10 ArrayStride 4
  723. OpMemberDecorate %_struct_4 0 Offset 0
  724. OpDecorate %_runtimearr__struct_4 ArrayStride 40
  725. OpMemberDecorate %_struct_6 0 Offset 0
  726. OpDecorate %_struct_6 BufferBlock
  727. %int = OpTypeInt 32 1
  728. %int_0 = OpConstant %int 0
  729. %uint = OpTypeInt 32 0
  730. %uint_0 = OpConstant %uint 0
  731. %uint_5 = OpConstant %uint 5
  732. %uint_10 = OpConstant %uint 10
  733. %_arr_int_uint_10 = OpTypeArray %int %uint_10
  734. %_struct_4 = OpTypeStruct %_arr_int_uint_10
  735. %_runtimearr__struct_4 = OpTypeRuntimeArray %_struct_4
  736. %_struct_6 = OpTypeStruct %_runtimearr__struct_4
  737. %_ptr_Uniform__struct_6 = OpTypePointer Uniform %_struct_6
  738. %void = OpTypeVoid
  739. %14 = OpTypeFunction %void
  740. %_ptr_Uniform__struct_4 = OpTypePointer Uniform %_struct_4
  741. %_arr_int_uint_5 = OpTypeArray %int %uint_5
  742. %_struct_17 = OpTypeStruct %_arr_int_uint_5
  743. %_ptr_Function__struct_17 = OpTypePointer Function %_struct_17
  744. %19 = OpTypeFunction %void %_ptr_Function__struct_17
  745. %2 = OpVariable %_ptr_Uniform__struct_6 Uniform
  746. %1 = OpFunction %void None %14
  747. %20 = OpLabel
  748. %21 = OpAccessChain %_ptr_Uniform__struct_4 %2 %int_0 %uint_0
  749. %22 = OpFunctionCall %void %23 %21
  750. OpReturn
  751. OpFunctionEnd
  752. %23 = OpFunction %void None %19
  753. %24 = OpFunctionParameter %_ptr_Function__struct_17
  754. %25 = OpLabel
  755. OpReturn
  756. OpFunctionEnd
  757. )";
  758. CompileSuccessfully(spirv);
  759. spvValidatorOptionsSetBeforeHlslLegalization(getValidatorOptions(), true);
  760. EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
  761. EXPECT_THAT(getDiagnosticString(), HasSubstr("OpFunctionCall Argument <id>"));
  762. EXPECT_THAT(getDiagnosticString(),
  763. HasSubstr("type does not match Function <id>"));
  764. }
  765. INSTANTIATE_TEST_SUITE_P(StorageClass, ValidateFunctionCall,
  766. Values("UniformConstant", "Input", "Uniform", "Output",
  767. "Workgroup", "Private", "Function",
  768. "PushConstant", "Image", "StorageBuffer",
  769. "AtomicCounter"));
  770. } // namespace
  771. } // namespace val
  772. } // namespace spvtools