validate_mode_setting.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. // Copyright (c) 2018 Google LLC.
  2. // Modifications Copyright (C) 2024 Advanced Micro Devices, Inc. All rights
  3. // reserved.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. #include <algorithm>
  18. #include "source/opcode.h"
  19. #include "source/spirv_target_env.h"
  20. #include "source/val/instruction.h"
  21. #include "source/val/validate.h"
  22. #include "source/val/validation_state.h"
  23. namespace spvtools {
  24. namespace val {
  25. namespace {
  26. spv_result_t ValidateEntryPoint(ValidationState_t& _, const Instruction* inst) {
  27. const auto entry_point_id = inst->GetOperandAs<uint32_t>(1);
  28. auto entry_point = _.FindDef(entry_point_id);
  29. if (!entry_point || spv::Op::OpFunction != entry_point->opcode()) {
  30. return _.diag(SPV_ERROR_INVALID_ID, inst)
  31. << "OpEntryPoint Entry Point <id> " << _.getIdName(entry_point_id)
  32. << " is not a function.";
  33. }
  34. // Only check the shader execution models
  35. const spv::ExecutionModel execution_model =
  36. inst->GetOperandAs<spv::ExecutionModel>(0);
  37. if (execution_model != spv::ExecutionModel::Kernel) {
  38. const auto entry_point_type_id = entry_point->GetOperandAs<uint32_t>(3);
  39. const auto entry_point_type = _.FindDef(entry_point_type_id);
  40. if (!entry_point_type || 3 != entry_point_type->words().size()) {
  41. return _.diag(SPV_ERROR_INVALID_ID, inst)
  42. << _.VkErrorID(4633) << "OpEntryPoint Entry Point <id> "
  43. << _.getIdName(entry_point_id)
  44. << "s function parameter count is not zero.";
  45. }
  46. }
  47. auto return_type = _.FindDef(entry_point->type_id());
  48. if (!return_type || spv::Op::OpTypeVoid != return_type->opcode()) {
  49. return _.diag(SPV_ERROR_INVALID_ID, inst)
  50. << _.VkErrorID(4633) << "OpEntryPoint Entry Point <id> "
  51. << _.getIdName(entry_point_id)
  52. << "s function return type is not void.";
  53. }
  54. const auto* execution_modes = _.GetExecutionModes(entry_point_id);
  55. if (_.HasCapability(spv::Capability::Shader)) {
  56. switch (execution_model) {
  57. case spv::ExecutionModel::Fragment:
  58. if (execution_modes &&
  59. execution_modes->count(spv::ExecutionMode::OriginUpperLeft) &&
  60. execution_modes->count(spv::ExecutionMode::OriginLowerLeft)) {
  61. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  62. << "Fragment execution model entry points can only specify "
  63. "one of OriginUpperLeft or OriginLowerLeft execution "
  64. "modes.";
  65. }
  66. if (!execution_modes ||
  67. (!execution_modes->count(spv::ExecutionMode::OriginUpperLeft) &&
  68. !execution_modes->count(spv::ExecutionMode::OriginLowerLeft))) {
  69. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  70. << "Fragment execution model entry points require either an "
  71. "OriginUpperLeft or OriginLowerLeft execution mode.";
  72. }
  73. if (execution_modes &&
  74. 1 < std::count_if(execution_modes->begin(), execution_modes->end(),
  75. [](const spv::ExecutionMode& mode) {
  76. switch (mode) {
  77. case spv::ExecutionMode::DepthGreater:
  78. case spv::ExecutionMode::DepthLess:
  79. case spv::ExecutionMode::DepthUnchanged:
  80. return true;
  81. default:
  82. return false;
  83. }
  84. })) {
  85. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  86. << "Fragment execution model entry points can specify at most "
  87. "one of DepthGreater, DepthLess or DepthUnchanged "
  88. "execution modes.";
  89. }
  90. if (execution_modes &&
  91. 1 < std::count_if(
  92. execution_modes->begin(), execution_modes->end(),
  93. [](const spv::ExecutionMode& mode) {
  94. switch (mode) {
  95. case spv::ExecutionMode::PixelInterlockOrderedEXT:
  96. case spv::ExecutionMode::PixelInterlockUnorderedEXT:
  97. case spv::ExecutionMode::SampleInterlockOrderedEXT:
  98. case spv::ExecutionMode::SampleInterlockUnorderedEXT:
  99. case spv::ExecutionMode::ShadingRateInterlockOrderedEXT:
  100. case spv::ExecutionMode::
  101. ShadingRateInterlockUnorderedEXT:
  102. return true;
  103. default:
  104. return false;
  105. }
  106. })) {
  107. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  108. << "Fragment execution model entry points can specify at most "
  109. "one fragment shader interlock execution mode.";
  110. }
  111. if (execution_modes &&
  112. 1 < std::count_if(
  113. execution_modes->begin(), execution_modes->end(),
  114. [](const spv::ExecutionMode& mode) {
  115. switch (mode) {
  116. case spv::ExecutionMode::StencilRefUnchangedFrontAMD:
  117. case spv::ExecutionMode::StencilRefLessFrontAMD:
  118. case spv::ExecutionMode::StencilRefGreaterFrontAMD:
  119. return true;
  120. default:
  121. return false;
  122. }
  123. })) {
  124. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  125. << "Fragment execution model entry points can specify at most "
  126. "one of StencilRefUnchangedFrontAMD, "
  127. "StencilRefLessFrontAMD or StencilRefGreaterFrontAMD "
  128. "execution modes.";
  129. }
  130. if (execution_modes &&
  131. 1 < std::count_if(
  132. execution_modes->begin(), execution_modes->end(),
  133. [](const spv::ExecutionMode& mode) {
  134. switch (mode) {
  135. case spv::ExecutionMode::StencilRefUnchangedBackAMD:
  136. case spv::ExecutionMode::StencilRefLessBackAMD:
  137. case spv::ExecutionMode::StencilRefGreaterBackAMD:
  138. return true;
  139. default:
  140. return false;
  141. }
  142. })) {
  143. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  144. << "Fragment execution model entry points can specify at most "
  145. "one of StencilRefUnchangedBackAMD, "
  146. "StencilRefLessBackAMD or StencilRefGreaterBackAMD "
  147. "execution modes.";
  148. }
  149. break;
  150. case spv::ExecutionModel::TessellationControl:
  151. case spv::ExecutionModel::TessellationEvaluation:
  152. if (execution_modes &&
  153. 1 < std::count_if(
  154. execution_modes->begin(), execution_modes->end(),
  155. [](const spv::ExecutionMode& mode) {
  156. switch (mode) {
  157. case spv::ExecutionMode::SpacingEqual:
  158. case spv::ExecutionMode::SpacingFractionalEven:
  159. case spv::ExecutionMode::SpacingFractionalOdd:
  160. return true;
  161. default:
  162. return false;
  163. }
  164. })) {
  165. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  166. << "Tessellation execution model entry points can specify at "
  167. "most one of SpacingEqual, SpacingFractionalOdd or "
  168. "SpacingFractionalEven execution modes.";
  169. }
  170. if (execution_modes &&
  171. 1 < std::count_if(execution_modes->begin(), execution_modes->end(),
  172. [](const spv::ExecutionMode& mode) {
  173. switch (mode) {
  174. case spv::ExecutionMode::Triangles:
  175. case spv::ExecutionMode::Quads:
  176. case spv::ExecutionMode::Isolines:
  177. return true;
  178. default:
  179. return false;
  180. }
  181. })) {
  182. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  183. << "Tessellation execution model entry points can specify at "
  184. "most one of Triangles, Quads or Isolines execution modes.";
  185. }
  186. if (execution_modes &&
  187. 1 < std::count_if(execution_modes->begin(), execution_modes->end(),
  188. [](const spv::ExecutionMode& mode) {
  189. switch (mode) {
  190. case spv::ExecutionMode::VertexOrderCw:
  191. case spv::ExecutionMode::VertexOrderCcw:
  192. return true;
  193. default:
  194. return false;
  195. }
  196. })) {
  197. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  198. << "Tessellation execution model entry points can specify at "
  199. "most one of VertexOrderCw or VertexOrderCcw execution "
  200. "modes.";
  201. }
  202. break;
  203. case spv::ExecutionModel::Geometry:
  204. if (!execution_modes ||
  205. 1 != std::count_if(
  206. execution_modes->begin(), execution_modes->end(),
  207. [](const spv::ExecutionMode& mode) {
  208. switch (mode) {
  209. case spv::ExecutionMode::InputPoints:
  210. case spv::ExecutionMode::InputLines:
  211. case spv::ExecutionMode::InputLinesAdjacency:
  212. case spv::ExecutionMode::Triangles:
  213. case spv::ExecutionMode::InputTrianglesAdjacency:
  214. return true;
  215. default:
  216. return false;
  217. }
  218. })) {
  219. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  220. << "Geometry execution model entry points must specify "
  221. "exactly one of InputPoints, InputLines, "
  222. "InputLinesAdjacency, Triangles or InputTrianglesAdjacency "
  223. "execution modes.";
  224. }
  225. if (!execution_modes ||
  226. 1 != std::count_if(execution_modes->begin(), execution_modes->end(),
  227. [](const spv::ExecutionMode& mode) {
  228. switch (mode) {
  229. case spv::ExecutionMode::OutputPoints:
  230. case spv::ExecutionMode::OutputLineStrip:
  231. case spv::ExecutionMode::OutputTriangleStrip:
  232. return true;
  233. default:
  234. return false;
  235. }
  236. })) {
  237. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  238. << "Geometry execution model entry points must specify "
  239. "exactly one of OutputPoints, OutputLineStrip or "
  240. "OutputTriangleStrip execution modes.";
  241. }
  242. break;
  243. case spv::ExecutionModel::MeshEXT:
  244. if (!execution_modes ||
  245. 1 != std::count_if(execution_modes->begin(), execution_modes->end(),
  246. [](const spv::ExecutionMode& mode) {
  247. switch (mode) {
  248. case spv::ExecutionMode::OutputPoints:
  249. case spv::ExecutionMode::OutputLinesEXT:
  250. case spv::ExecutionMode::OutputTrianglesEXT:
  251. return true;
  252. default:
  253. return false;
  254. }
  255. })) {
  256. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  257. << "MeshEXT execution model entry points must specify exactly "
  258. "one of OutputPoints, OutputLinesEXT, or "
  259. "OutputTrianglesEXT Execution Modes.";
  260. } else if (2 != std::count_if(
  261. execution_modes->begin(), execution_modes->end(),
  262. [](const spv::ExecutionMode& mode) {
  263. switch (mode) {
  264. case spv::ExecutionMode::OutputPrimitivesEXT:
  265. case spv::ExecutionMode::OutputVertices:
  266. return true;
  267. default:
  268. return false;
  269. }
  270. })) {
  271. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  272. << "MeshEXT execution model entry points must specify both "
  273. "OutputPrimitivesEXT and OutputVertices Execution Modes.";
  274. }
  275. break;
  276. default:
  277. break;
  278. }
  279. }
  280. if (spvIsVulkanEnv(_.context()->target_env)) {
  281. switch (execution_model) {
  282. case spv::ExecutionModel::GLCompute:
  283. if (!execution_modes ||
  284. !execution_modes->count(spv::ExecutionMode::LocalSize)) {
  285. bool ok = false;
  286. for (auto& i : _.ordered_instructions()) {
  287. if (i.opcode() == spv::Op::OpDecorate) {
  288. if (i.operands().size() > 2) {
  289. if (i.GetOperandAs<spv::Decoration>(1) ==
  290. spv::Decoration::BuiltIn &&
  291. i.GetOperandAs<spv::BuiltIn>(2) ==
  292. spv::BuiltIn::WorkgroupSize) {
  293. ok = true;
  294. break;
  295. }
  296. }
  297. }
  298. if (i.opcode() == spv::Op::OpExecutionModeId) {
  299. const auto mode = i.GetOperandAs<spv::ExecutionMode>(1);
  300. if (mode == spv::ExecutionMode::LocalSizeId) {
  301. ok = true;
  302. break;
  303. }
  304. }
  305. }
  306. if (!ok) {
  307. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  308. << _.VkErrorID(6426)
  309. << "In the Vulkan environment, GLCompute execution model "
  310. "entry points require either the LocalSize or "
  311. "LocalSizeId execution mode or an object decorated with "
  312. "WorkgroupSize must be specified.";
  313. }
  314. }
  315. break;
  316. default:
  317. break;
  318. }
  319. }
  320. if (_.EntryPointHasLocalSizeOrId(entry_point_id)) {
  321. const Instruction* local_size_inst =
  322. _.EntryPointLocalSizeOrId(entry_point_id);
  323. if (local_size_inst) {
  324. const auto mode = local_size_inst->GetOperandAs<spv::ExecutionMode>(1);
  325. const uint32_t operand_x = local_size_inst->GetOperandAs<uint32_t>(2);
  326. const uint32_t operand_y = local_size_inst->GetOperandAs<uint32_t>(3);
  327. const uint32_t operand_z = local_size_inst->GetOperandAs<uint32_t>(4);
  328. if (mode == spv::ExecutionMode::LocalSize) {
  329. if ((operand_x * operand_y * operand_z) == 0) {
  330. return _.diag(SPV_ERROR_INVALID_DATA, local_size_inst)
  331. << "Local Size execution mode must not have a product of zero "
  332. "(X "
  333. "= "
  334. << operand_x << ", Y = " << operand_y << ", Z = " << operand_z
  335. << ").";
  336. }
  337. } else if (mode == spv::ExecutionMode::LocalSizeId) {
  338. // can only validate product if static and not spec constant
  339. // (This is done for us in EvalConstantValUint64)
  340. uint64_t x_size, y_size, z_size;
  341. bool static_x = _.EvalConstantValUint64(operand_x, &x_size);
  342. bool static_y = _.EvalConstantValUint64(operand_y, &y_size);
  343. bool static_z = _.EvalConstantValUint64(operand_z, &z_size);
  344. if (static_x && static_y && static_z &&
  345. ((x_size * y_size * z_size) == 0)) {
  346. return _.diag(SPV_ERROR_INVALID_DATA, local_size_inst)
  347. << "Local Size Id execution mode must not have a product of "
  348. "zero "
  349. "(X = "
  350. << x_size << ", Y = " << y_size << ", Z = " << z_size << ").";
  351. }
  352. }
  353. }
  354. }
  355. return SPV_SUCCESS;
  356. }
  357. spv_result_t ValidateExecutionMode(ValidationState_t& _,
  358. const Instruction* inst) {
  359. const auto entry_point_id = inst->GetOperandAs<uint32_t>(0);
  360. const auto found = std::find(_.entry_points().cbegin(),
  361. _.entry_points().cend(), entry_point_id);
  362. if (found == _.entry_points().cend()) {
  363. return _.diag(SPV_ERROR_INVALID_ID, inst)
  364. << "OpExecutionMode Entry Point <id> " << _.getIdName(entry_point_id)
  365. << " is not the Entry Point "
  366. "operand of an OpEntryPoint.";
  367. }
  368. const auto mode = inst->GetOperandAs<spv::ExecutionMode>(1);
  369. if (inst->opcode() == spv::Op::OpExecutionModeId) {
  370. bool valid_mode = false;
  371. switch (mode) {
  372. case spv::ExecutionMode::SubgroupsPerWorkgroupId:
  373. case spv::ExecutionMode::LocalSizeHintId:
  374. case spv::ExecutionMode::LocalSizeId:
  375. case spv::ExecutionMode::FPFastMathDefault:
  376. case spv::ExecutionMode::MaximumRegistersIdINTEL:
  377. case spv::ExecutionMode::IsApiEntryAMDX:
  378. case spv::ExecutionMode::MaxNodeRecursionAMDX:
  379. case spv::ExecutionMode::MaxNumWorkgroupsAMDX:
  380. case spv::ExecutionMode::ShaderIndexAMDX:
  381. case spv::ExecutionMode::SharesInputWithAMDX:
  382. case spv::ExecutionMode::StaticNumWorkgroupsAMDX:
  383. valid_mode = true;
  384. break;
  385. default:
  386. valid_mode = false;
  387. break;
  388. }
  389. if (!valid_mode) {
  390. return _.diag(SPV_ERROR_INVALID_ID, inst)
  391. << "OpExecutionModeId is only valid when the Mode operand is an "
  392. "execution mode that takes Extra Operands that are id "
  393. "operands.";
  394. }
  395. size_t operand_count = inst->operands().size();
  396. for (size_t i = 2; i < operand_count; ++i) {
  397. const auto operand_id = inst->GetOperandAs<uint32_t>(i);
  398. const auto* operand_inst = _.FindDef(operand_id);
  399. switch (mode) {
  400. case spv::ExecutionMode::SubgroupsPerWorkgroupId:
  401. case spv::ExecutionMode::LocalSizeHintId:
  402. case spv::ExecutionMode::LocalSizeId:
  403. case spv::ExecutionMode::IsApiEntryAMDX:
  404. case spv::ExecutionMode::MaxNodeRecursionAMDX:
  405. case spv::ExecutionMode::MaxNumWorkgroupsAMDX:
  406. case spv::ExecutionMode::ShaderIndexAMDX:
  407. case spv::ExecutionMode::SharesInputWithAMDX:
  408. case spv::ExecutionMode::StaticNumWorkgroupsAMDX:
  409. if (!spvOpcodeIsConstant(operand_inst->opcode())) {
  410. return _.diag(SPV_ERROR_INVALID_ID, inst)
  411. << "For OpExecutionModeId all Extra Operand ids must be "
  412. "constant instructions.";
  413. }
  414. break;
  415. case spv::ExecutionMode::FPFastMathDefault:
  416. if (i == 2) {
  417. if (!_.IsFloatScalarType(operand_id)) {
  418. return _.diag(SPV_ERROR_INVALID_ID, inst)
  419. << "The Target Type operand must be a floating-point "
  420. "scalar type";
  421. }
  422. } else {
  423. bool is_int32 = false;
  424. bool is_const = false;
  425. uint32_t value = 0;
  426. std::tie(is_int32, is_const, value) =
  427. _.EvalInt32IfConst(operand_id);
  428. if (is_int32 && is_const) {
  429. // Valid values include up to 0x00040000 (AllowTransform).
  430. uint32_t invalid_mask = 0xfff80000;
  431. if ((invalid_mask & value) != 0) {
  432. return _.diag(SPV_ERROR_INVALID_ID, inst)
  433. << "The Fast Math Default operand is an invalid bitmask "
  434. "value";
  435. }
  436. if (value &
  437. static_cast<uint32_t>(spv::FPFastMathModeMask::Fast)) {
  438. return _.diag(SPV_ERROR_INVALID_ID, inst)
  439. << "The Fast Math Default operand must not include Fast";
  440. }
  441. const auto reassoc_contract =
  442. spv::FPFastMathModeMask::AllowContract |
  443. spv::FPFastMathModeMask::AllowReassoc;
  444. if ((value & static_cast<uint32_t>(
  445. spv::FPFastMathModeMask::AllowTransform)) != 0 &&
  446. ((value & static_cast<uint32_t>(reassoc_contract)) !=
  447. static_cast<uint32_t>(reassoc_contract))) {
  448. return _.diag(SPV_ERROR_INVALID_ID, inst)
  449. << "The Fast Math Default operand must include "
  450. "AllowContract and AllowReassoc when AllowTransform "
  451. "is specified";
  452. }
  453. } else {
  454. return _.diag(SPV_ERROR_INVALID_ID, inst)
  455. << "The Fast Math Default operand must be a "
  456. "non-specialization constant";
  457. }
  458. }
  459. break;
  460. default:
  461. break;
  462. }
  463. }
  464. } else if (mode == spv::ExecutionMode::SubgroupsPerWorkgroupId ||
  465. mode == spv::ExecutionMode::LocalSizeHintId ||
  466. mode == spv::ExecutionMode::LocalSizeId ||
  467. mode == spv::ExecutionMode::FPFastMathDefault ||
  468. mode == spv::ExecutionMode::IsApiEntryAMDX ||
  469. mode == spv::ExecutionMode::MaxNodeRecursionAMDX ||
  470. mode == spv::ExecutionMode::MaxNumWorkgroupsAMDX ||
  471. mode == spv::ExecutionMode::ShaderIndexAMDX ||
  472. mode == spv::ExecutionMode::SharesInputWithAMDX ||
  473. mode == spv::ExecutionMode::StaticNumWorkgroupsAMDX) {
  474. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  475. << "OpExecutionMode is only valid when the Mode operand is an "
  476. "execution mode that takes no Extra Operands, or takes Extra "
  477. "Operands that are not id operands.";
  478. }
  479. const auto* models = _.GetExecutionModels(entry_point_id);
  480. switch (mode) {
  481. case spv::ExecutionMode::Invocations:
  482. case spv::ExecutionMode::InputPoints:
  483. case spv::ExecutionMode::InputLines:
  484. case spv::ExecutionMode::InputLinesAdjacency:
  485. case spv::ExecutionMode::InputTrianglesAdjacency:
  486. case spv::ExecutionMode::OutputLineStrip:
  487. case spv::ExecutionMode::OutputTriangleStrip:
  488. if (!std::all_of(models->begin(), models->end(),
  489. [](const spv::ExecutionModel& model) {
  490. return model == spv::ExecutionModel::Geometry;
  491. })) {
  492. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  493. << "Execution mode can only be used with the Geometry execution "
  494. "model.";
  495. }
  496. break;
  497. case spv::ExecutionMode::OutputPoints:
  498. if (!std::all_of(
  499. models->begin(), models->end(),
  500. [&_](const spv::ExecutionModel& model) {
  501. switch (model) {
  502. case spv::ExecutionModel::Geometry:
  503. return true;
  504. case spv::ExecutionModel::MeshNV:
  505. return _.HasCapability(spv::Capability::MeshShadingNV);
  506. case spv::ExecutionModel::MeshEXT:
  507. return _.HasCapability(spv::Capability::MeshShadingEXT);
  508. default:
  509. return false;
  510. }
  511. })) {
  512. if (_.HasCapability(spv::Capability::MeshShadingNV) ||
  513. _.HasCapability(spv::Capability::MeshShadingEXT)) {
  514. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  515. << "Execution mode can only be used with the Geometry "
  516. "MeshNV or MeshEXT execution model.";
  517. } else {
  518. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  519. << "Execution mode can only be used with the Geometry "
  520. "execution "
  521. "model.";
  522. }
  523. }
  524. break;
  525. case spv::ExecutionMode::SpacingEqual:
  526. case spv::ExecutionMode::SpacingFractionalEven:
  527. case spv::ExecutionMode::SpacingFractionalOdd:
  528. case spv::ExecutionMode::VertexOrderCw:
  529. case spv::ExecutionMode::VertexOrderCcw:
  530. case spv::ExecutionMode::PointMode:
  531. case spv::ExecutionMode::Quads:
  532. case spv::ExecutionMode::Isolines:
  533. if (!std::all_of(
  534. models->begin(), models->end(),
  535. [](const spv::ExecutionModel& model) {
  536. return (model == spv::ExecutionModel::TessellationControl) ||
  537. (model == spv::ExecutionModel::TessellationEvaluation);
  538. })) {
  539. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  540. << "Execution mode can only be used with a tessellation "
  541. "execution model.";
  542. }
  543. break;
  544. case spv::ExecutionMode::Triangles:
  545. if (!std::all_of(models->begin(), models->end(),
  546. [](const spv::ExecutionModel& model) {
  547. switch (model) {
  548. case spv::ExecutionModel::Geometry:
  549. case spv::ExecutionModel::TessellationControl:
  550. case spv::ExecutionModel::TessellationEvaluation:
  551. return true;
  552. default:
  553. return false;
  554. }
  555. })) {
  556. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  557. << "Execution mode can only be used with a Geometry or "
  558. "tessellation execution model.";
  559. }
  560. break;
  561. case spv::ExecutionMode::OutputVertices:
  562. if (!std::all_of(
  563. models->begin(), models->end(),
  564. [&_](const spv::ExecutionModel& model) {
  565. switch (model) {
  566. case spv::ExecutionModel::Geometry:
  567. case spv::ExecutionModel::TessellationControl:
  568. case spv::ExecutionModel::TessellationEvaluation:
  569. return true;
  570. case spv::ExecutionModel::MeshNV:
  571. return _.HasCapability(spv::Capability::MeshShadingNV);
  572. case spv::ExecutionModel::MeshEXT:
  573. return _.HasCapability(spv::Capability::MeshShadingEXT);
  574. default:
  575. return false;
  576. }
  577. })) {
  578. if (_.HasCapability(spv::Capability::MeshShadingNV) ||
  579. _.HasCapability(spv::Capability::MeshShadingEXT)) {
  580. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  581. << "Execution mode can only be used with a Geometry, "
  582. "tessellation, MeshNV or MeshEXT execution model.";
  583. } else {
  584. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  585. << "Execution mode can only be used with a Geometry or "
  586. "tessellation execution model.";
  587. }
  588. }
  589. if (spvIsVulkanEnv(_.context()->target_env)) {
  590. if (_.HasCapability(spv::Capability::MeshShadingEXT) &&
  591. inst->GetOperandAs<uint32_t>(2) == 0) {
  592. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  593. << _.VkErrorID(7330)
  594. << "In mesh shaders using the MeshEXT Execution Model the "
  595. "OutputVertices Execution Mode must be greater than 0";
  596. }
  597. }
  598. break;
  599. case spv::ExecutionMode::OutputLinesEXT:
  600. case spv::ExecutionMode::OutputTrianglesEXT:
  601. case spv::ExecutionMode::OutputPrimitivesEXT:
  602. if (!std::all_of(models->begin(), models->end(),
  603. [](const spv::ExecutionModel& model) {
  604. return (model == spv::ExecutionModel::MeshEXT ||
  605. model == spv::ExecutionModel::MeshNV);
  606. })) {
  607. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  608. << "Execution mode can only be used with the MeshEXT or MeshNV "
  609. "execution "
  610. "model.";
  611. }
  612. if (mode == spv::ExecutionMode::OutputPrimitivesEXT &&
  613. spvIsVulkanEnv(_.context()->target_env)) {
  614. if (_.HasCapability(spv::Capability::MeshShadingEXT) &&
  615. inst->GetOperandAs<uint32_t>(2) == 0) {
  616. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  617. << _.VkErrorID(7331)
  618. << "In mesh shaders using the MeshEXT Execution Model the "
  619. "OutputPrimitivesEXT Execution Mode must be greater than 0";
  620. }
  621. }
  622. break;
  623. case spv::ExecutionMode::QuadDerivativesKHR:
  624. if (!std::all_of(models->begin(), models->end(),
  625. [](const spv::ExecutionModel& model) {
  626. return (model == spv::ExecutionModel::Fragment ||
  627. model == spv::ExecutionModel::GLCompute);
  628. })) {
  629. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  630. << "Execution mode can only be used with the Fragment or "
  631. "GLCompute execution model.";
  632. }
  633. break;
  634. case spv::ExecutionMode::PixelCenterInteger:
  635. case spv::ExecutionMode::OriginUpperLeft:
  636. case spv::ExecutionMode::OriginLowerLeft:
  637. case spv::ExecutionMode::EarlyFragmentTests:
  638. case spv::ExecutionMode::DepthReplacing:
  639. case spv::ExecutionMode::DepthGreater:
  640. case spv::ExecutionMode::DepthLess:
  641. case spv::ExecutionMode::DepthUnchanged:
  642. case spv::ExecutionMode::NonCoherentColorAttachmentReadEXT:
  643. case spv::ExecutionMode::NonCoherentDepthAttachmentReadEXT:
  644. case spv::ExecutionMode::NonCoherentStencilAttachmentReadEXT:
  645. case spv::ExecutionMode::PixelInterlockOrderedEXT:
  646. case spv::ExecutionMode::PixelInterlockUnorderedEXT:
  647. case spv::ExecutionMode::SampleInterlockOrderedEXT:
  648. case spv::ExecutionMode::SampleInterlockUnorderedEXT:
  649. case spv::ExecutionMode::ShadingRateInterlockOrderedEXT:
  650. case spv::ExecutionMode::ShadingRateInterlockUnorderedEXT:
  651. case spv::ExecutionMode::EarlyAndLateFragmentTestsAMD:
  652. case spv::ExecutionMode::StencilRefUnchangedFrontAMD:
  653. case spv::ExecutionMode::StencilRefGreaterFrontAMD:
  654. case spv::ExecutionMode::StencilRefLessFrontAMD:
  655. case spv::ExecutionMode::StencilRefUnchangedBackAMD:
  656. case spv::ExecutionMode::StencilRefGreaterBackAMD:
  657. case spv::ExecutionMode::StencilRefLessBackAMD:
  658. case spv::ExecutionMode::RequireFullQuadsKHR:
  659. if (!std::all_of(models->begin(), models->end(),
  660. [](const spv::ExecutionModel& model) {
  661. return model == spv::ExecutionModel::Fragment;
  662. })) {
  663. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  664. << "Execution mode can only be used with the Fragment execution "
  665. "model.";
  666. }
  667. break;
  668. case spv::ExecutionMode::LocalSizeHint:
  669. case spv::ExecutionMode::VecTypeHint:
  670. case spv::ExecutionMode::ContractionOff:
  671. case spv::ExecutionMode::LocalSizeHintId:
  672. if (!std::all_of(models->begin(), models->end(),
  673. [](const spv::ExecutionModel& model) {
  674. return model == spv::ExecutionModel::Kernel;
  675. })) {
  676. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  677. << "Execution mode can only be used with the Kernel execution "
  678. "model.";
  679. }
  680. break;
  681. case spv::ExecutionMode::LocalSize:
  682. case spv::ExecutionMode::LocalSizeId:
  683. if (mode == spv::ExecutionMode::LocalSizeId && !_.IsLocalSizeIdAllowed())
  684. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  685. << "LocalSizeId mode is not allowed by the current environment.";
  686. if (!std::all_of(
  687. models->begin(), models->end(),
  688. [&_](const spv::ExecutionModel& model) {
  689. switch (model) {
  690. case spv::ExecutionModel::Kernel:
  691. case spv::ExecutionModel::GLCompute:
  692. return true;
  693. case spv::ExecutionModel::TaskNV:
  694. case spv::ExecutionModel::MeshNV:
  695. return _.HasCapability(spv::Capability::MeshShadingNV);
  696. case spv::ExecutionModel::TaskEXT:
  697. case spv::ExecutionModel::MeshEXT:
  698. return _.HasCapability(spv::Capability::MeshShadingEXT);
  699. default:
  700. return false;
  701. }
  702. })) {
  703. if (_.HasCapability(spv::Capability::MeshShadingNV) ||
  704. _.HasCapability(spv::Capability::MeshShadingEXT)) {
  705. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  706. << "Execution mode can only be used with a Kernel, GLCompute, "
  707. "MeshNV, MeshEXT, TaskNV or TaskEXT execution model.";
  708. } else {
  709. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  710. << "Execution mode can only be used with a Kernel or "
  711. "GLCompute "
  712. "execution model.";
  713. }
  714. }
  715. default:
  716. break;
  717. }
  718. if (mode == spv::ExecutionMode::FPFastMathDefault) {
  719. const auto* modes = _.GetExecutionModes(entry_point_id);
  720. if (modes && modes->count(spv::ExecutionMode::ContractionOff)) {
  721. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  722. << "FPFastMathDefault and ContractionOff execution modes cannot "
  723. "be applied to the same entry point";
  724. }
  725. if (modes && modes->count(spv::ExecutionMode::SignedZeroInfNanPreserve)) {
  726. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  727. << "FPFastMathDefault and SignedZeroInfNanPreserve execution "
  728. "modes cannot be applied to the same entry point";
  729. }
  730. }
  731. if (spvIsVulkanEnv(_.context()->target_env)) {
  732. if (mode == spv::ExecutionMode::OriginLowerLeft) {
  733. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  734. << _.VkErrorID(4653)
  735. << "In the Vulkan environment, the OriginLowerLeft execution mode "
  736. "must not be used.";
  737. }
  738. if (mode == spv::ExecutionMode::PixelCenterInteger) {
  739. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  740. << _.VkErrorID(4654)
  741. << "In the Vulkan environment, the PixelCenterInteger execution "
  742. "mode must not be used.";
  743. }
  744. }
  745. return SPV_SUCCESS;
  746. }
  747. spv_result_t ValidateMemoryModel(ValidationState_t& _,
  748. const Instruction* inst) {
  749. // Already produced an error if multiple memory model instructions are
  750. // present.
  751. if (_.memory_model() != spv::MemoryModel::VulkanKHR &&
  752. _.HasCapability(spv::Capability::VulkanMemoryModelKHR)) {
  753. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  754. << "VulkanMemoryModelKHR capability must only be specified if "
  755. "the VulkanKHR memory model is used.";
  756. }
  757. if (spvIsOpenCLEnv(_.context()->target_env)) {
  758. if ((_.addressing_model() != spv::AddressingModel::Physical32) &&
  759. (_.addressing_model() != spv::AddressingModel::Physical64)) {
  760. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  761. << "Addressing model must be Physical32 or Physical64 "
  762. << "in the OpenCL environment.";
  763. }
  764. if (_.memory_model() != spv::MemoryModel::OpenCL) {
  765. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  766. << "Memory model must be OpenCL in the OpenCL environment.";
  767. }
  768. }
  769. if (spvIsVulkanEnv(_.context()->target_env)) {
  770. if ((_.addressing_model() != spv::AddressingModel::Logical) &&
  771. (_.addressing_model() !=
  772. spv::AddressingModel::PhysicalStorageBuffer64)) {
  773. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  774. << _.VkErrorID(4635)
  775. << "Addressing model must be Logical or PhysicalStorageBuffer64 "
  776. << "in the Vulkan environment.";
  777. }
  778. }
  779. return SPV_SUCCESS;
  780. }
  781. bool PerEntryExecutionMode(spv::ExecutionMode mode) {
  782. switch (mode) {
  783. // These execution modes can be specified multiple times per entry point.
  784. case spv::ExecutionMode::DenormPreserve:
  785. case spv::ExecutionMode::DenormFlushToZero:
  786. case spv::ExecutionMode::SignedZeroInfNanPreserve:
  787. case spv::ExecutionMode::RoundingModeRTE:
  788. case spv::ExecutionMode::RoundingModeRTZ:
  789. case spv::ExecutionMode::FPFastMathDefault:
  790. case spv::ExecutionMode::RoundingModeRTPINTEL:
  791. case spv::ExecutionMode::RoundingModeRTNINTEL:
  792. case spv::ExecutionMode::FloatingPointModeALTINTEL:
  793. case spv::ExecutionMode::FloatingPointModeIEEEINTEL:
  794. return false;
  795. default:
  796. return true;
  797. }
  798. }
  799. } // namespace
  800. spv_result_t ValidateFloatControls2(ValidationState_t& _) {
  801. std::unordered_set<uint32_t> fp_fast_math_default_entry_points;
  802. for (auto entry_point : _.entry_points()) {
  803. const auto* exec_modes = _.GetExecutionModes(entry_point);
  804. if (exec_modes &&
  805. exec_modes->count(spv::ExecutionMode::FPFastMathDefault)) {
  806. fp_fast_math_default_entry_points.insert(entry_point);
  807. }
  808. }
  809. std::vector<std::pair<const Instruction*, spv::Decoration>> worklist;
  810. for (const auto& inst : _.ordered_instructions()) {
  811. if (inst.opcode() != spv::Op::OpDecorate) {
  812. continue;
  813. }
  814. const auto decoration = inst.GetOperandAs<spv::Decoration>(1);
  815. const auto target_id = inst.GetOperandAs<uint32_t>(0);
  816. const auto target = _.FindDef(target_id);
  817. if (decoration == spv::Decoration::NoContraction) {
  818. worklist.push_back(std::make_pair(target, decoration));
  819. } else if (decoration == spv::Decoration::FPFastMathMode) {
  820. auto mask = inst.GetOperandAs<spv::FPFastMathModeMask>(2);
  821. if ((mask & spv::FPFastMathModeMask::Fast) !=
  822. spv::FPFastMathModeMask::MaskNone) {
  823. worklist.push_back(std::make_pair(target, decoration));
  824. }
  825. }
  826. }
  827. std::unordered_set<const Instruction*> visited;
  828. while (!worklist.empty()) {
  829. const auto inst = worklist.back().first;
  830. const auto decoration = worklist.back().second;
  831. worklist.pop_back();
  832. if (!visited.insert(inst).second) {
  833. continue;
  834. }
  835. const auto function = inst->function();
  836. if (function) {
  837. const auto& entry_points = _.FunctionEntryPoints(function->id());
  838. for (auto entry_point : entry_points) {
  839. if (fp_fast_math_default_entry_points.count(entry_point)) {
  840. const std::string dec = decoration == spv::Decoration::NoContraction
  841. ? "NoContraction"
  842. : "FPFastMathMode Fast";
  843. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  844. << dec
  845. << " cannot be used by an entry point with the "
  846. "FPFastMathDefault execution mode";
  847. }
  848. }
  849. } else {
  850. for (const auto& pair : inst->uses()) {
  851. worklist.push_back(std::make_pair(pair.first, decoration));
  852. }
  853. }
  854. }
  855. return SPV_SUCCESS;
  856. }
  857. spv_result_t ModeSettingPass(ValidationState_t& _, const Instruction* inst) {
  858. switch (inst->opcode()) {
  859. case spv::Op::OpEntryPoint:
  860. if (auto error = ValidateEntryPoint(_, inst)) return error;
  861. break;
  862. case spv::Op::OpExecutionMode:
  863. case spv::Op::OpExecutionModeId:
  864. if (auto error = ValidateExecutionMode(_, inst)) return error;
  865. break;
  866. case spv::Op::OpMemoryModel:
  867. if (auto error = ValidateMemoryModel(_, inst)) return error;
  868. break;
  869. default:
  870. break;
  871. }
  872. return SPV_SUCCESS;
  873. }
  874. spv_result_t ValidateDuplicateExecutionModes(ValidationState_t& _) {
  875. using PerEntryKey = std::tuple<spv::ExecutionMode, uint32_t>;
  876. using PerOperandKey = std::tuple<spv::ExecutionMode, uint32_t, uint32_t>;
  877. std::set<PerEntryKey> seen_per_entry;
  878. std::set<PerOperandKey> seen_per_operand;
  879. const auto lookupMode = [&_](spv::ExecutionMode mode) -> std::string {
  880. spv_operand_desc desc = nullptr;
  881. if (_.grammar().lookupOperand(SPV_OPERAND_TYPE_EXECUTION_MODE,
  882. static_cast<uint32_t>(mode),
  883. &desc) == SPV_SUCCESS) {
  884. return std::string(desc->name);
  885. }
  886. return "Unknown";
  887. };
  888. for (const auto& inst : _.ordered_instructions()) {
  889. if (inst.opcode() != spv::Op::OpExecutionMode &&
  890. inst.opcode() != spv::Op::OpExecutionModeId) {
  891. continue;
  892. }
  893. const auto entry = inst.GetOperandAs<uint32_t>(0);
  894. const auto mode = inst.GetOperandAs<spv::ExecutionMode>(1);
  895. if (PerEntryExecutionMode(mode)) {
  896. if (!seen_per_entry.insert(std::make_tuple(mode, entry)).second) {
  897. return _.diag(SPV_ERROR_INVALID_ID, &inst)
  898. << lookupMode(mode)
  899. << " execution mode must not be specified multiple times per "
  900. "entry point";
  901. }
  902. } else {
  903. // Execution modes allowed multiple times all take a single operand.
  904. const auto operand = inst.GetOperandAs<uint32_t>(2);
  905. if (!seen_per_operand.insert(std::make_tuple(mode, entry, operand))
  906. .second) {
  907. return _.diag(SPV_ERROR_INVALID_ID, &inst)
  908. << lookupMode(mode)
  909. << " execution mode must not be specified multiple times for "
  910. "the same entry point and operands";
  911. }
  912. }
  913. }
  914. return SPV_SUCCESS;
  915. }
  916. } // namespace val
  917. } // namespace spvtools