validate_mode_setting.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. }
  221. if (!ok) {
  222. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  223. << _.VkErrorID(4683)
  224. << "In the Vulkan environment, GLCompute execution model "
  225. "entry points require either the LocalSize execution "
  226. "mode or an object decorated with WorkgroupSize must be "
  227. "specified.";
  228. }
  229. }
  230. break;
  231. default:
  232. break;
  233. }
  234. }
  235. return SPV_SUCCESS;
  236. }
  237. spv_result_t ValidateExecutionMode(ValidationState_t& _,
  238. const Instruction* inst) {
  239. const auto entry_point_id = inst->GetOperandAs<uint32_t>(0);
  240. const auto found = std::find(_.entry_points().cbegin(),
  241. _.entry_points().cend(), entry_point_id);
  242. if (found == _.entry_points().cend()) {
  243. return _.diag(SPV_ERROR_INVALID_ID, inst)
  244. << "OpExecutionMode Entry Point <id> '"
  245. << _.getIdName(entry_point_id)
  246. << "' is not the Entry Point "
  247. "operand of an OpEntryPoint.";
  248. }
  249. const auto mode = inst->GetOperandAs<SpvExecutionMode>(1);
  250. if (inst->opcode() == SpvOpExecutionModeId) {
  251. size_t operand_count = inst->operands().size();
  252. for (size_t i = 2; i < operand_count; ++i) {
  253. const auto operand_id = inst->GetOperandAs<uint32_t>(2);
  254. const auto* operand_inst = _.FindDef(operand_id);
  255. if (mode == SpvExecutionModeSubgroupsPerWorkgroupId ||
  256. mode == SpvExecutionModeLocalSizeHintId ||
  257. mode == SpvExecutionModeLocalSizeId) {
  258. if (!spvOpcodeIsConstant(operand_inst->opcode())) {
  259. return _.diag(SPV_ERROR_INVALID_ID, inst)
  260. << "For OpExecutionModeId all Extra Operand ids must be "
  261. "constant "
  262. "instructions.";
  263. }
  264. } else {
  265. return _.diag(SPV_ERROR_INVALID_ID, inst)
  266. << "OpExecutionModeId is only valid when the Mode operand is an "
  267. "execution mode that takes Extra Operands that are id "
  268. "operands.";
  269. }
  270. }
  271. } else if (mode == SpvExecutionModeSubgroupsPerWorkgroupId ||
  272. mode == SpvExecutionModeLocalSizeHintId ||
  273. mode == SpvExecutionModeLocalSizeId) {
  274. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  275. << "OpExecutionMode is only valid when the Mode operand is an "
  276. "execution mode that takes no Extra Operands, or takes Extra "
  277. "Operands that are not id operands.";
  278. }
  279. const auto* models = _.GetExecutionModels(entry_point_id);
  280. switch (mode) {
  281. case SpvExecutionModeInvocations:
  282. case SpvExecutionModeInputPoints:
  283. case SpvExecutionModeInputLines:
  284. case SpvExecutionModeInputLinesAdjacency:
  285. case SpvExecutionModeInputTrianglesAdjacency:
  286. case SpvExecutionModeOutputLineStrip:
  287. case SpvExecutionModeOutputTriangleStrip:
  288. if (!std::all_of(models->begin(), models->end(),
  289. [](const SpvExecutionModel& model) {
  290. return model == SpvExecutionModelGeometry;
  291. })) {
  292. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  293. << "Execution mode can only be used with the Geometry execution "
  294. "model.";
  295. }
  296. break;
  297. case SpvExecutionModeOutputPoints:
  298. if (!std::all_of(models->begin(), models->end(),
  299. [&_](const SpvExecutionModel& model) {
  300. switch (model) {
  301. case SpvExecutionModelGeometry:
  302. return true;
  303. case SpvExecutionModelMeshNV:
  304. return _.HasCapability(SpvCapabilityMeshShadingNV);
  305. default:
  306. return false;
  307. }
  308. })) {
  309. if (_.HasCapability(SpvCapabilityMeshShadingNV)) {
  310. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  311. << "Execution mode can only be used with the Geometry or "
  312. "MeshNV execution model.";
  313. } else {
  314. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  315. << "Execution mode can only be used with the Geometry "
  316. "execution "
  317. "model.";
  318. }
  319. }
  320. break;
  321. case SpvExecutionModeSpacingEqual:
  322. case SpvExecutionModeSpacingFractionalEven:
  323. case SpvExecutionModeSpacingFractionalOdd:
  324. case SpvExecutionModeVertexOrderCw:
  325. case SpvExecutionModeVertexOrderCcw:
  326. case SpvExecutionModePointMode:
  327. case SpvExecutionModeQuads:
  328. case SpvExecutionModeIsolines:
  329. if (!std::all_of(
  330. models->begin(), models->end(),
  331. [](const SpvExecutionModel& model) {
  332. return (model == SpvExecutionModelTessellationControl) ||
  333. (model == SpvExecutionModelTessellationEvaluation);
  334. })) {
  335. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  336. << "Execution mode can only be used with a tessellation "
  337. "execution model.";
  338. }
  339. break;
  340. case SpvExecutionModeTriangles:
  341. if (!std::all_of(models->begin(), models->end(),
  342. [](const SpvExecutionModel& model) {
  343. switch (model) {
  344. case SpvExecutionModelGeometry:
  345. case SpvExecutionModelTessellationControl:
  346. case SpvExecutionModelTessellationEvaluation:
  347. return true;
  348. default:
  349. return false;
  350. }
  351. })) {
  352. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  353. << "Execution mode can only be used with a Geometry or "
  354. "tessellation execution model.";
  355. }
  356. break;
  357. case SpvExecutionModeOutputVertices:
  358. if (!std::all_of(models->begin(), models->end(),
  359. [&_](const SpvExecutionModel& model) {
  360. switch (model) {
  361. case SpvExecutionModelGeometry:
  362. case SpvExecutionModelTessellationControl:
  363. case SpvExecutionModelTessellationEvaluation:
  364. return true;
  365. case SpvExecutionModelMeshNV:
  366. return _.HasCapability(SpvCapabilityMeshShadingNV);
  367. default:
  368. return false;
  369. }
  370. })) {
  371. if (_.HasCapability(SpvCapabilityMeshShadingNV)) {
  372. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  373. << "Execution mode can only be used with a Geometry, "
  374. "tessellation or MeshNV execution model.";
  375. } else {
  376. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  377. << "Execution mode can only be used with a Geometry or "
  378. "tessellation execution model.";
  379. }
  380. }
  381. break;
  382. case SpvExecutionModePixelCenterInteger:
  383. case SpvExecutionModeOriginUpperLeft:
  384. case SpvExecutionModeOriginLowerLeft:
  385. case SpvExecutionModeEarlyFragmentTests:
  386. case SpvExecutionModeDepthReplacing:
  387. case SpvExecutionModeDepthGreater:
  388. case SpvExecutionModeDepthLess:
  389. case SpvExecutionModeDepthUnchanged:
  390. case SpvExecutionModePixelInterlockOrderedEXT:
  391. case SpvExecutionModePixelInterlockUnorderedEXT:
  392. case SpvExecutionModeSampleInterlockOrderedEXT:
  393. case SpvExecutionModeSampleInterlockUnorderedEXT:
  394. case SpvExecutionModeShadingRateInterlockOrderedEXT:
  395. case SpvExecutionModeShadingRateInterlockUnorderedEXT:
  396. if (!std::all_of(models->begin(), models->end(),
  397. [](const SpvExecutionModel& model) {
  398. return model == SpvExecutionModelFragment;
  399. })) {
  400. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  401. << "Execution mode can only be used with the Fragment execution "
  402. "model.";
  403. }
  404. break;
  405. case SpvExecutionModeLocalSizeHint:
  406. case SpvExecutionModeVecTypeHint:
  407. case SpvExecutionModeContractionOff:
  408. case SpvExecutionModeLocalSizeHintId:
  409. if (!std::all_of(models->begin(), models->end(),
  410. [](const SpvExecutionModel& model) {
  411. return model == SpvExecutionModelKernel;
  412. })) {
  413. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  414. << "Execution mode can only be used with the Kernel execution "
  415. "model.";
  416. }
  417. break;
  418. case SpvExecutionModeLocalSize:
  419. case SpvExecutionModeLocalSizeId:
  420. if (!std::all_of(models->begin(), models->end(),
  421. [&_](const SpvExecutionModel& model) {
  422. switch (model) {
  423. case SpvExecutionModelKernel:
  424. case SpvExecutionModelGLCompute:
  425. return true;
  426. case SpvExecutionModelTaskNV:
  427. case SpvExecutionModelMeshNV:
  428. return _.HasCapability(SpvCapabilityMeshShadingNV);
  429. default:
  430. return false;
  431. }
  432. })) {
  433. if (_.HasCapability(SpvCapabilityMeshShadingNV)) {
  434. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  435. << "Execution mode can only be used with a Kernel, GLCompute, "
  436. "MeshNV, or TaskNV execution model.";
  437. } else {
  438. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  439. << "Execution mode can only be used with a Kernel or "
  440. "GLCompute "
  441. "execution model.";
  442. }
  443. }
  444. default:
  445. break;
  446. }
  447. if (spvIsVulkanEnv(_.context()->target_env)) {
  448. if (mode == SpvExecutionModeOriginLowerLeft) {
  449. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  450. << _.VkErrorID(4653)
  451. << "In the Vulkan environment, the OriginLowerLeft execution mode "
  452. "must not be used.";
  453. }
  454. if (mode == SpvExecutionModePixelCenterInteger) {
  455. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  456. << _.VkErrorID(4654)
  457. << "In the Vulkan environment, the PixelCenterInteger execution "
  458. "mode must not be used.";
  459. }
  460. }
  461. return SPV_SUCCESS;
  462. }
  463. spv_result_t ValidateMemoryModel(ValidationState_t& _,
  464. const Instruction* inst) {
  465. // Already produced an error if multiple memory model instructions are
  466. // present.
  467. if (_.memory_model() != SpvMemoryModelVulkanKHR &&
  468. _.HasCapability(SpvCapabilityVulkanMemoryModelKHR)) {
  469. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  470. << "VulkanMemoryModelKHR capability must only be specified if "
  471. "the VulkanKHR memory model is used.";
  472. }
  473. if (spvIsOpenCLEnv(_.context()->target_env)) {
  474. if ((_.addressing_model() != SpvAddressingModelPhysical32) &&
  475. (_.addressing_model() != SpvAddressingModelPhysical64)) {
  476. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  477. << "Addressing model must be Physical32 or Physical64 "
  478. << "in the OpenCL environment.";
  479. }
  480. if (_.memory_model() != SpvMemoryModelOpenCL) {
  481. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  482. << "Memory model must be OpenCL in the OpenCL environment.";
  483. }
  484. }
  485. if (spvIsVulkanEnv(_.context()->target_env)) {
  486. if ((_.addressing_model() != SpvAddressingModelLogical) &&
  487. (_.addressing_model() != SpvAddressingModelPhysicalStorageBuffer64)) {
  488. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  489. << _.VkErrorID(4635)
  490. << "Addressing model must be Logical or PhysicalStorageBuffer64 "
  491. << "in the Vulkan environment.";
  492. }
  493. }
  494. return SPV_SUCCESS;
  495. }
  496. } // namespace
  497. spv_result_t ModeSettingPass(ValidationState_t& _, const Instruction* inst) {
  498. switch (inst->opcode()) {
  499. case SpvOpEntryPoint:
  500. if (auto error = ValidateEntryPoint(_, inst)) return error;
  501. break;
  502. case SpvOpExecutionMode:
  503. case SpvOpExecutionModeId:
  504. if (auto error = ValidateExecutionMode(_, inst)) return error;
  505. break;
  506. case SpvOpMemoryModel:
  507. if (auto error = ValidateMemoryModel(_, inst)) return error;
  508. break;
  509. default:
  510. break;
  511. }
  512. return SPV_SUCCESS;
  513. }
  514. } // namespace val
  515. } // namespace spvtools