validate_mode_setting.cpp 23 KB

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