validate_mode_setting.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. // Copyright (c) 2018 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. //
  15. #include "source/val/validate.h"
  16. #include <algorithm>
  17. #include "source/opcode.h"
  18. #include "source/spirv_target_env.h"
  19. #include "source/val/instruction.h"
  20. #include "source/val/validation_state.h"
  21. namespace spvtools {
  22. namespace val {
  23. namespace {
  24. spv_result_t ValidateEntryPoint(ValidationState_t& _, const Instruction* inst) {
  25. const auto entry_point_id = inst->GetOperandAs<uint32_t>(1);
  26. auto entry_point = _.FindDef(entry_point_id);
  27. if (!entry_point || SpvOpFunction != entry_point->opcode()) {
  28. return _.diag(SPV_ERROR_INVALID_ID, inst)
  29. << "OpEntryPoint Entry Point <id> '" << _.getIdName(entry_point_id)
  30. << "' is not a function.";
  31. }
  32. // Only check the shader execution models
  33. const SpvExecutionModel execution_model =
  34. inst->GetOperandAs<SpvExecutionModel>(0);
  35. if (execution_model != SpvExecutionModelKernel) {
  36. const auto entry_point_type_id = entry_point->GetOperandAs<uint32_t>(3);
  37. const auto entry_point_type = _.FindDef(entry_point_type_id);
  38. if (!entry_point_type || 3 != entry_point_type->words().size()) {
  39. return _.diag(SPV_ERROR_INVALID_ID, inst)
  40. << _.VkErrorID(4633) << "OpEntryPoint Entry Point <id> '"
  41. << _.getIdName(entry_point_id)
  42. << "'s function parameter count is not zero.";
  43. }
  44. }
  45. auto return_type = _.FindDef(entry_point->type_id());
  46. if (!return_type || SpvOpTypeVoid != return_type->opcode()) {
  47. return _.diag(SPV_ERROR_INVALID_ID, inst)
  48. << _.VkErrorID(4633) << "OpEntryPoint Entry Point <id> '"
  49. << _.getIdName(entry_point_id)
  50. << "'s function return type is not void.";
  51. }
  52. const auto* execution_modes = _.GetExecutionModes(entry_point_id);
  53. if (_.HasCapability(SpvCapabilityShader)) {
  54. switch (execution_model) {
  55. case SpvExecutionModelFragment:
  56. if (execution_modes &&
  57. execution_modes->count(SpvExecutionModeOriginUpperLeft) &&
  58. execution_modes->count(SpvExecutionModeOriginLowerLeft)) {
  59. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  60. << "Fragment execution model entry points can only specify "
  61. "one of OriginUpperLeft or OriginLowerLeft execution "
  62. "modes.";
  63. }
  64. if (!execution_modes ||
  65. (!execution_modes->count(SpvExecutionModeOriginUpperLeft) &&
  66. !execution_modes->count(SpvExecutionModeOriginLowerLeft))) {
  67. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  68. << "Fragment execution model entry points require either an "
  69. "OriginUpperLeft or OriginLowerLeft execution mode.";
  70. }
  71. if (execution_modes &&
  72. 1 < std::count_if(execution_modes->begin(), execution_modes->end(),
  73. [](const SpvExecutionMode& mode) {
  74. switch (mode) {
  75. case SpvExecutionModeDepthGreater:
  76. case SpvExecutionModeDepthLess:
  77. case SpvExecutionModeDepthUnchanged:
  78. return true;
  79. default:
  80. return false;
  81. }
  82. })) {
  83. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  84. << "Fragment execution model entry points can specify at most "
  85. "one of DepthGreater, DepthLess or DepthUnchanged "
  86. "execution modes.";
  87. }
  88. if (execution_modes &&
  89. 1 < std::count_if(
  90. execution_modes->begin(), execution_modes->end(),
  91. [](const SpvExecutionMode& mode) {
  92. switch (mode) {
  93. case SpvExecutionModePixelInterlockOrderedEXT:
  94. case SpvExecutionModePixelInterlockUnorderedEXT:
  95. case SpvExecutionModeSampleInterlockOrderedEXT:
  96. case SpvExecutionModeSampleInterlockUnorderedEXT:
  97. case SpvExecutionModeShadingRateInterlockOrderedEXT:
  98. case SpvExecutionModeShadingRateInterlockUnorderedEXT:
  99. return true;
  100. default:
  101. return false;
  102. }
  103. })) {
  104. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  105. << "Fragment execution model entry points can specify at most "
  106. "one fragment shader interlock execution mode.";
  107. }
  108. break;
  109. case SpvExecutionModelTessellationControl:
  110. case SpvExecutionModelTessellationEvaluation:
  111. if (execution_modes &&
  112. 1 < std::count_if(execution_modes->begin(), execution_modes->end(),
  113. [](const SpvExecutionMode& mode) {
  114. switch (mode) {
  115. case SpvExecutionModeSpacingEqual:
  116. case SpvExecutionModeSpacingFractionalEven:
  117. case SpvExecutionModeSpacingFractionalOdd:
  118. return true;
  119. default:
  120. return false;
  121. }
  122. })) {
  123. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  124. << "Tessellation execution model entry points can specify at "
  125. "most one of SpacingEqual, SpacingFractionalOdd or "
  126. "SpacingFractionalEven execution modes.";
  127. }
  128. if (execution_modes &&
  129. 1 < std::count_if(execution_modes->begin(), execution_modes->end(),
  130. [](const SpvExecutionMode& mode) {
  131. switch (mode) {
  132. case SpvExecutionModeTriangles:
  133. case SpvExecutionModeQuads:
  134. case SpvExecutionModeIsolines:
  135. return true;
  136. default:
  137. return false;
  138. }
  139. })) {
  140. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  141. << "Tessellation execution model entry points can specify at "
  142. "most one of Triangles, Quads or Isolines execution modes.";
  143. }
  144. if (execution_modes &&
  145. 1 < std::count_if(execution_modes->begin(), execution_modes->end(),
  146. [](const SpvExecutionMode& mode) {
  147. switch (mode) {
  148. case SpvExecutionModeVertexOrderCw:
  149. case SpvExecutionModeVertexOrderCcw:
  150. return true;
  151. default:
  152. return false;
  153. }
  154. })) {
  155. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  156. << "Tessellation execution model entry points can specify at "
  157. "most one of VertexOrderCw or VertexOrderCcw execution "
  158. "modes.";
  159. }
  160. break;
  161. case SpvExecutionModelGeometry:
  162. if (!execution_modes ||
  163. 1 != std::count_if(execution_modes->begin(), execution_modes->end(),
  164. [](const SpvExecutionMode& mode) {
  165. switch (mode) {
  166. case SpvExecutionModeInputPoints:
  167. case SpvExecutionModeInputLines:
  168. case SpvExecutionModeInputLinesAdjacency:
  169. case SpvExecutionModeTriangles:
  170. case SpvExecutionModeInputTrianglesAdjacency:
  171. return true;
  172. default:
  173. return false;
  174. }
  175. })) {
  176. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  177. << "Geometry execution model entry points must specify "
  178. "exactly one of InputPoints, InputLines, "
  179. "InputLinesAdjacency, Triangles or InputTrianglesAdjacency "
  180. "execution modes.";
  181. }
  182. if (!execution_modes ||
  183. 1 != std::count_if(execution_modes->begin(), execution_modes->end(),
  184. [](const SpvExecutionMode& mode) {
  185. switch (mode) {
  186. case SpvExecutionModeOutputPoints:
  187. case SpvExecutionModeOutputLineStrip:
  188. case SpvExecutionModeOutputTriangleStrip:
  189. return true;
  190. default:
  191. return false;
  192. }
  193. })) {
  194. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  195. << "Geometry execution model entry points must specify "
  196. "exactly one of OutputPoints, OutputLineStrip or "
  197. "OutputTriangleStrip execution modes.";
  198. }
  199. break;
  200. default:
  201. break;
  202. }
  203. }
  204. if (spvIsVulkanEnv(_.context()->target_env)) {
  205. switch (execution_model) {
  206. case SpvExecutionModelGLCompute:
  207. if (!execution_modes ||
  208. !execution_modes->count(SpvExecutionModeLocalSize)) {
  209. bool ok = false;
  210. for (auto& i : _.ordered_instructions()) {
  211. if (i.opcode() == SpvOpDecorate) {
  212. if (i.operands().size() > 2) {
  213. if (i.GetOperandAs<SpvDecoration>(1) == SpvDecorationBuiltIn &&
  214. i.GetOperandAs<SpvBuiltIn>(2) == SpvBuiltInWorkgroupSize) {
  215. ok = true;
  216. break;
  217. }
  218. }
  219. }
  220. if (i.opcode() == SpvOpExecutionModeId) {
  221. const auto mode = i.GetOperandAs<SpvExecutionMode>(1);
  222. if (mode == SpvExecutionModeLocalSizeId) {
  223. ok = true;
  224. break;
  225. }
  226. }
  227. }
  228. if (!ok) {
  229. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  230. << _.VkErrorID(6426)
  231. << "In the Vulkan environment, GLCompute execution model "
  232. "entry points require either the LocalSize or "
  233. "LocalSizeId execution mode or an object decorated with "
  234. "WorkgroupSize must be specified.";
  235. }
  236. }
  237. break;
  238. default:
  239. break;
  240. }
  241. }
  242. return SPV_SUCCESS;
  243. }
  244. spv_result_t ValidateExecutionMode(ValidationState_t& _,
  245. const Instruction* inst) {
  246. const auto entry_point_id = inst->GetOperandAs<uint32_t>(0);
  247. const auto found = std::find(_.entry_points().cbegin(),
  248. _.entry_points().cend(), entry_point_id);
  249. if (found == _.entry_points().cend()) {
  250. return _.diag(SPV_ERROR_INVALID_ID, inst)
  251. << "OpExecutionMode Entry Point <id> '"
  252. << _.getIdName(entry_point_id)
  253. << "' is not the Entry Point "
  254. "operand of an OpEntryPoint.";
  255. }
  256. const auto mode = inst->GetOperandAs<SpvExecutionMode>(1);
  257. if (inst->opcode() == SpvOpExecutionModeId) {
  258. size_t operand_count = inst->operands().size();
  259. for (size_t i = 2; i < operand_count; ++i) {
  260. const auto operand_id = inst->GetOperandAs<uint32_t>(2);
  261. const auto* operand_inst = _.FindDef(operand_id);
  262. if (mode == SpvExecutionModeSubgroupsPerWorkgroupId ||
  263. mode == SpvExecutionModeLocalSizeHintId ||
  264. mode == SpvExecutionModeLocalSizeId) {
  265. if (!spvOpcodeIsConstant(operand_inst->opcode())) {
  266. return _.diag(SPV_ERROR_INVALID_ID, inst)
  267. << "For OpExecutionModeId all Extra Operand ids must be "
  268. "constant "
  269. "instructions.";
  270. }
  271. } else {
  272. return _.diag(SPV_ERROR_INVALID_ID, inst)
  273. << "OpExecutionModeId is only valid when the Mode operand is an "
  274. "execution mode that takes Extra Operands that are id "
  275. "operands.";
  276. }
  277. }
  278. } else if (mode == SpvExecutionModeSubgroupsPerWorkgroupId ||
  279. mode == SpvExecutionModeLocalSizeHintId ||
  280. mode == SpvExecutionModeLocalSizeId) {
  281. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  282. << "OpExecutionMode is only valid when the Mode operand is an "
  283. "execution mode that takes no Extra Operands, or takes Extra "
  284. "Operands that are not id operands.";
  285. }
  286. const auto* models = _.GetExecutionModels(entry_point_id);
  287. switch (mode) {
  288. case SpvExecutionModeInvocations:
  289. case SpvExecutionModeInputPoints:
  290. case SpvExecutionModeInputLines:
  291. case SpvExecutionModeInputLinesAdjacency:
  292. case SpvExecutionModeInputTrianglesAdjacency:
  293. case SpvExecutionModeOutputLineStrip:
  294. case SpvExecutionModeOutputTriangleStrip:
  295. if (!std::all_of(models->begin(), models->end(),
  296. [](const SpvExecutionModel& model) {
  297. return model == SpvExecutionModelGeometry;
  298. })) {
  299. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  300. << "Execution mode can only be used with the Geometry execution "
  301. "model.";
  302. }
  303. break;
  304. case SpvExecutionModeOutputPoints:
  305. if (!std::all_of(models->begin(), models->end(),
  306. [&_](const SpvExecutionModel& model) {
  307. switch (model) {
  308. case SpvExecutionModelGeometry:
  309. return true;
  310. case SpvExecutionModelMeshNV:
  311. return _.HasCapability(SpvCapabilityMeshShadingNV);
  312. default:
  313. return false;
  314. }
  315. })) {
  316. if (_.HasCapability(SpvCapabilityMeshShadingNV)) {
  317. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  318. << "Execution mode can only be used with the Geometry or "
  319. "MeshNV execution model.";
  320. } else {
  321. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  322. << "Execution mode can only be used with the Geometry "
  323. "execution "
  324. "model.";
  325. }
  326. }
  327. break;
  328. case SpvExecutionModeSpacingEqual:
  329. case SpvExecutionModeSpacingFractionalEven:
  330. case SpvExecutionModeSpacingFractionalOdd:
  331. case SpvExecutionModeVertexOrderCw:
  332. case SpvExecutionModeVertexOrderCcw:
  333. case SpvExecutionModePointMode:
  334. case SpvExecutionModeQuads:
  335. case SpvExecutionModeIsolines:
  336. if (!std::all_of(
  337. models->begin(), models->end(),
  338. [](const SpvExecutionModel& model) {
  339. return (model == SpvExecutionModelTessellationControl) ||
  340. (model == SpvExecutionModelTessellationEvaluation);
  341. })) {
  342. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  343. << "Execution mode can only be used with a tessellation "
  344. "execution model.";
  345. }
  346. break;
  347. case SpvExecutionModeTriangles:
  348. if (!std::all_of(models->begin(), models->end(),
  349. [](const SpvExecutionModel& model) {
  350. switch (model) {
  351. case SpvExecutionModelGeometry:
  352. case SpvExecutionModelTessellationControl:
  353. case SpvExecutionModelTessellationEvaluation:
  354. return true;
  355. default:
  356. return false;
  357. }
  358. })) {
  359. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  360. << "Execution mode can only be used with a Geometry or "
  361. "tessellation execution model.";
  362. }
  363. break;
  364. case SpvExecutionModeOutputVertices:
  365. if (!std::all_of(models->begin(), models->end(),
  366. [&_](const SpvExecutionModel& model) {
  367. switch (model) {
  368. case SpvExecutionModelGeometry:
  369. case SpvExecutionModelTessellationControl:
  370. case SpvExecutionModelTessellationEvaluation:
  371. return true;
  372. case SpvExecutionModelMeshNV:
  373. return _.HasCapability(SpvCapabilityMeshShadingNV);
  374. default:
  375. return false;
  376. }
  377. })) {
  378. if (_.HasCapability(SpvCapabilityMeshShadingNV)) {
  379. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  380. << "Execution mode can only be used with a Geometry, "
  381. "tessellation or MeshNV execution model.";
  382. } else {
  383. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  384. << "Execution mode can only be used with a Geometry or "
  385. "tessellation execution model.";
  386. }
  387. }
  388. break;
  389. case SpvExecutionModePixelCenterInteger:
  390. case SpvExecutionModeOriginUpperLeft:
  391. case SpvExecutionModeOriginLowerLeft:
  392. case SpvExecutionModeEarlyFragmentTests:
  393. case SpvExecutionModeDepthReplacing:
  394. case SpvExecutionModeDepthGreater:
  395. case SpvExecutionModeDepthLess:
  396. case SpvExecutionModeDepthUnchanged:
  397. case SpvExecutionModePixelInterlockOrderedEXT:
  398. case SpvExecutionModePixelInterlockUnorderedEXT:
  399. case SpvExecutionModeSampleInterlockOrderedEXT:
  400. case SpvExecutionModeSampleInterlockUnorderedEXT:
  401. case SpvExecutionModeShadingRateInterlockOrderedEXT:
  402. case SpvExecutionModeShadingRateInterlockUnorderedEXT:
  403. if (!std::all_of(models->begin(), models->end(),
  404. [](const SpvExecutionModel& model) {
  405. return model == SpvExecutionModelFragment;
  406. })) {
  407. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  408. << "Execution mode can only be used with the Fragment execution "
  409. "model.";
  410. }
  411. break;
  412. case SpvExecutionModeLocalSizeHint:
  413. case SpvExecutionModeVecTypeHint:
  414. case SpvExecutionModeContractionOff:
  415. case SpvExecutionModeLocalSizeHintId:
  416. if (!std::all_of(models->begin(), models->end(),
  417. [](const SpvExecutionModel& model) {
  418. return model == SpvExecutionModelKernel;
  419. })) {
  420. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  421. << "Execution mode can only be used with the Kernel execution "
  422. "model.";
  423. }
  424. break;
  425. case SpvExecutionModeLocalSize:
  426. case SpvExecutionModeLocalSizeId:
  427. if (mode == SpvExecutionModeLocalSizeId && !_.IsLocalSizeIdAllowed())
  428. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  429. << "LocalSizeId mode is not allowed by the current environment.";
  430. if (!std::all_of(models->begin(), models->end(),
  431. [&_](const SpvExecutionModel& model) {
  432. switch (model) {
  433. case SpvExecutionModelKernel:
  434. case SpvExecutionModelGLCompute:
  435. return true;
  436. case SpvExecutionModelTaskNV:
  437. case SpvExecutionModelMeshNV:
  438. return _.HasCapability(SpvCapabilityMeshShadingNV);
  439. default:
  440. return false;
  441. }
  442. })) {
  443. if (_.HasCapability(SpvCapabilityMeshShadingNV)) {
  444. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  445. << "Execution mode can only be used with a Kernel, GLCompute, "
  446. "MeshNV, or TaskNV execution model.";
  447. } else {
  448. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  449. << "Execution mode can only be used with a Kernel or "
  450. "GLCompute "
  451. "execution model.";
  452. }
  453. }
  454. default:
  455. break;
  456. }
  457. if (spvIsVulkanEnv(_.context()->target_env)) {
  458. if (mode == SpvExecutionModeOriginLowerLeft) {
  459. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  460. << _.VkErrorID(4653)
  461. << "In the Vulkan environment, the OriginLowerLeft execution mode "
  462. "must not be used.";
  463. }
  464. if (mode == SpvExecutionModePixelCenterInteger) {
  465. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  466. << _.VkErrorID(4654)
  467. << "In the Vulkan environment, the PixelCenterInteger execution "
  468. "mode must not be used.";
  469. }
  470. }
  471. return SPV_SUCCESS;
  472. }
  473. spv_result_t ValidateMemoryModel(ValidationState_t& _,
  474. const Instruction* inst) {
  475. // Already produced an error if multiple memory model instructions are
  476. // present.
  477. if (_.memory_model() != SpvMemoryModelVulkanKHR &&
  478. _.HasCapability(SpvCapabilityVulkanMemoryModelKHR)) {
  479. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  480. << "VulkanMemoryModelKHR capability must only be specified if "
  481. "the VulkanKHR memory model is used.";
  482. }
  483. if (spvIsOpenCLEnv(_.context()->target_env)) {
  484. if ((_.addressing_model() != SpvAddressingModelPhysical32) &&
  485. (_.addressing_model() != SpvAddressingModelPhysical64)) {
  486. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  487. << "Addressing model must be Physical32 or Physical64 "
  488. << "in the OpenCL environment.";
  489. }
  490. if (_.memory_model() != SpvMemoryModelOpenCL) {
  491. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  492. << "Memory model must be OpenCL in the OpenCL environment.";
  493. }
  494. }
  495. if (spvIsVulkanEnv(_.context()->target_env)) {
  496. if ((_.addressing_model() != SpvAddressingModelLogical) &&
  497. (_.addressing_model() != SpvAddressingModelPhysicalStorageBuffer64)) {
  498. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  499. << _.VkErrorID(4635)
  500. << "Addressing model must be Logical or PhysicalStorageBuffer64 "
  501. << "in the Vulkan environment.";
  502. }
  503. }
  504. return SPV_SUCCESS;
  505. }
  506. } // namespace
  507. spv_result_t ModeSettingPass(ValidationState_t& _, const Instruction* inst) {
  508. switch (inst->opcode()) {
  509. case SpvOpEntryPoint:
  510. if (auto error = ValidateEntryPoint(_, inst)) return error;
  511. break;
  512. case SpvOpExecutionMode:
  513. case SpvOpExecutionModeId:
  514. if (auto error = ValidateExecutionMode(_, inst)) return error;
  515. break;
  516. case SpvOpMemoryModel:
  517. if (auto error = ValidateMemoryModel(_, inst)) return error;
  518. break;
  519. default:
  520. break;
  521. }
  522. return SPV_SUCCESS;
  523. }
  524. } // namespace val
  525. } // namespace spvtools