validate_cfg.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. // Copyright (c) 2015-2016 The Khronos Group Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "source/val/validate.h"
  15. #include <algorithm>
  16. #include <cassert>
  17. #include <functional>
  18. #include <iostream>
  19. #include <iterator>
  20. #include <map>
  21. #include <string>
  22. #include <tuple>
  23. #include <unordered_map>
  24. #include <unordered_set>
  25. #include <utility>
  26. #include <vector>
  27. #include "source/cfa.h"
  28. #include "source/opcode.h"
  29. #include "source/spirv_target_env.h"
  30. #include "source/spirv_validator_options.h"
  31. #include "source/val/basic_block.h"
  32. #include "source/val/construct.h"
  33. #include "source/val/function.h"
  34. #include "source/val/validation_state.h"
  35. namespace spvtools {
  36. namespace val {
  37. namespace {
  38. spv_result_t ValidatePhi(ValidationState_t& _, const Instruction* inst) {
  39. auto block = inst->block();
  40. size_t num_in_ops = inst->words().size() - 3;
  41. if (num_in_ops % 2 != 0) {
  42. return _.diag(SPV_ERROR_INVALID_ID, inst)
  43. << "OpPhi does not have an equal number of incoming values and "
  44. "basic blocks.";
  45. }
  46. const Instruction* type_inst = _.FindDef(inst->type_id());
  47. assert(type_inst);
  48. const SpvOp type_opcode = type_inst->opcode();
  49. if (type_opcode == SpvOpTypePointer &&
  50. _.addressing_model() == SpvAddressingModelLogical) {
  51. if (!_.features().variable_pointers &&
  52. !_.features().variable_pointers_storage_buffer) {
  53. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  54. << "Using pointers with OpPhi requires capability "
  55. << "VariablePointers or VariablePointersStorageBuffer";
  56. }
  57. }
  58. // Create a uniqued vector of predecessor ids for comparison against
  59. // incoming values. OpBranchConditional %cond %label %label produces two
  60. // predecessors in the CFG.
  61. std::vector<uint32_t> pred_ids;
  62. std::transform(block->predecessors()->begin(), block->predecessors()->end(),
  63. std::back_inserter(pred_ids),
  64. [](const BasicBlock* b) { return b->id(); });
  65. std::sort(pred_ids.begin(), pred_ids.end());
  66. pred_ids.erase(std::unique(pred_ids.begin(), pred_ids.end()), pred_ids.end());
  67. size_t num_edges = num_in_ops / 2;
  68. if (num_edges != pred_ids.size()) {
  69. return _.diag(SPV_ERROR_INVALID_ID, inst)
  70. << "OpPhi's number of incoming blocks (" << num_edges
  71. << ") does not match block's predecessor count ("
  72. << block->predecessors()->size() << ").";
  73. }
  74. for (size_t i = 3; i < inst->words().size(); ++i) {
  75. auto inc_id = inst->word(i);
  76. if (i % 2 == 1) {
  77. // Incoming value type must match the phi result type.
  78. auto inc_type_id = _.GetTypeId(inc_id);
  79. if (inst->type_id() != inc_type_id) {
  80. return _.diag(SPV_ERROR_INVALID_ID, inst)
  81. << "OpPhi's result type <id> " << _.getIdName(inst->type_id())
  82. << " does not match incoming value <id> " << _.getIdName(inc_id)
  83. << " type <id> " << _.getIdName(inc_type_id) << ".";
  84. }
  85. } else {
  86. if (_.GetIdOpcode(inc_id) != SpvOpLabel) {
  87. return _.diag(SPV_ERROR_INVALID_ID, inst)
  88. << "OpPhi's incoming basic block <id> " << _.getIdName(inc_id)
  89. << " is not an OpLabel.";
  90. }
  91. // Incoming basic block must be an immediate predecessor of the phi's
  92. // block.
  93. if (!std::binary_search(pred_ids.begin(), pred_ids.end(), inc_id)) {
  94. return _.diag(SPV_ERROR_INVALID_ID, inst)
  95. << "OpPhi's incoming basic block <id> " << _.getIdName(inc_id)
  96. << " is not a predecessor of <id> " << _.getIdName(block->id())
  97. << ".";
  98. }
  99. }
  100. }
  101. return SPV_SUCCESS;
  102. }
  103. spv_result_t ValidateBranch(ValidationState_t& _, const Instruction* inst) {
  104. // target operands must be OpLabel
  105. const auto id = inst->GetOperandAs<uint32_t>(0);
  106. const auto target = _.FindDef(id);
  107. if (!target || SpvOpLabel != target->opcode()) {
  108. return _.diag(SPV_ERROR_INVALID_ID, inst)
  109. << "'Target Label' operands for OpBranch must be the ID "
  110. "of an OpLabel instruction";
  111. }
  112. return SPV_SUCCESS;
  113. }
  114. spv_result_t ValidateBranchConditional(ValidationState_t& _,
  115. const Instruction* inst) {
  116. // num_operands is either 3 or 5 --- if 5, the last two need to be literal
  117. // integers
  118. const auto num_operands = inst->operands().size();
  119. if (num_operands != 3 && num_operands != 5) {
  120. return _.diag(SPV_ERROR_INVALID_ID, inst)
  121. << "OpBranchConditional requires either 3 or 5 parameters";
  122. }
  123. // grab the condition operand and check that it is a bool
  124. const auto cond_id = inst->GetOperandAs<uint32_t>(0);
  125. const auto cond_op = _.FindDef(cond_id);
  126. if (!cond_op || !cond_op->type_id() ||
  127. !_.IsBoolScalarType(cond_op->type_id())) {
  128. return _.diag(SPV_ERROR_INVALID_ID, inst) << "Condition operand for "
  129. "OpBranchConditional must be "
  130. "of boolean type";
  131. }
  132. // target operands must be OpLabel
  133. // note that we don't need to check that the target labels are in the same
  134. // function,
  135. // PerformCfgChecks already checks for that
  136. const auto true_id = inst->GetOperandAs<uint32_t>(1);
  137. const auto true_target = _.FindDef(true_id);
  138. if (!true_target || SpvOpLabel != true_target->opcode()) {
  139. return _.diag(SPV_ERROR_INVALID_ID, inst)
  140. << "The 'True Label' operand for OpBranchConditional must be the "
  141. "ID of an OpLabel instruction";
  142. }
  143. const auto false_id = inst->GetOperandAs<uint32_t>(2);
  144. const auto false_target = _.FindDef(false_id);
  145. if (!false_target || SpvOpLabel != false_target->opcode()) {
  146. return _.diag(SPV_ERROR_INVALID_ID, inst)
  147. << "The 'False Label' operand for OpBranchConditional must be the "
  148. "ID of an OpLabel instruction";
  149. }
  150. return SPV_SUCCESS;
  151. }
  152. spv_result_t ValidateSwitch(ValidationState_t& _, const Instruction* inst) {
  153. const auto num_operands = inst->operands().size();
  154. // At least two operands (selector, default), any more than that are
  155. // literal/target.
  156. // target operands must be OpLabel
  157. for (size_t i = 2; i < num_operands; i += 2) {
  158. // literal, id
  159. const auto id = inst->GetOperandAs<uint32_t>(i + 1);
  160. const auto target = _.FindDef(id);
  161. if (!target || SpvOpLabel != target->opcode()) {
  162. return _.diag(SPV_ERROR_INVALID_ID, inst)
  163. << "'Target Label' operands for OpSwitch must be IDs of an "
  164. "OpLabel instruction";
  165. }
  166. }
  167. return SPV_SUCCESS;
  168. }
  169. spv_result_t ValidateReturnValue(ValidationState_t& _,
  170. const Instruction* inst) {
  171. const auto value_id = inst->GetOperandAs<uint32_t>(0);
  172. const auto value = _.FindDef(value_id);
  173. if (!value || !value->type_id()) {
  174. return _.diag(SPV_ERROR_INVALID_ID, inst)
  175. << "OpReturnValue Value <id> '" << _.getIdName(value_id)
  176. << "' does not represent a value.";
  177. }
  178. auto value_type = _.FindDef(value->type_id());
  179. if (!value_type || SpvOpTypeVoid == value_type->opcode()) {
  180. return _.diag(SPV_ERROR_INVALID_ID, inst)
  181. << "OpReturnValue value's type <id> '"
  182. << _.getIdName(value->type_id()) << "' is missing or void.";
  183. }
  184. const bool uses_variable_pointer =
  185. _.features().variable_pointers ||
  186. _.features().variable_pointers_storage_buffer;
  187. if (_.addressing_model() == SpvAddressingModelLogical &&
  188. SpvOpTypePointer == value_type->opcode() && !uses_variable_pointer &&
  189. !_.options()->relax_logical_pointer) {
  190. return _.diag(SPV_ERROR_INVALID_ID, inst)
  191. << "OpReturnValue value's type <id> '"
  192. << _.getIdName(value->type_id())
  193. << "' is a pointer, which is invalid in the Logical addressing "
  194. "model.";
  195. }
  196. const auto function = inst->function();
  197. const auto return_type = _.FindDef(function->GetResultTypeId());
  198. if (!return_type || return_type->id() != value_type->id()) {
  199. return _.diag(SPV_ERROR_INVALID_ID, inst)
  200. << "OpReturnValue Value <id> '" << _.getIdName(value_id)
  201. << "'s type does not match OpFunction's return type.";
  202. }
  203. return SPV_SUCCESS;
  204. }
  205. spv_result_t ValidateLoopMerge(ValidationState_t& _, const Instruction* inst) {
  206. const auto merge_id = inst->GetOperandAs<uint32_t>(0);
  207. const auto merge = _.FindDef(merge_id);
  208. if (!merge || merge->opcode() != SpvOpLabel) {
  209. return _.diag(SPV_ERROR_INVALID_ID, inst)
  210. << "Merge Block " << _.getIdName(merge_id) << " must be an OpLabel";
  211. }
  212. if (merge_id == inst->block()->id()) {
  213. return _.diag(SPV_ERROR_INVALID_ID, inst)
  214. << "Merge Block may not be the block containing the OpLoopMerge\n";
  215. }
  216. const auto continue_id = inst->GetOperandAs<uint32_t>(1);
  217. const auto continue_target = _.FindDef(continue_id);
  218. if (!continue_target || continue_target->opcode() != SpvOpLabel) {
  219. return _.diag(SPV_ERROR_INVALID_ID, inst)
  220. << "Continue Target " << _.getIdName(continue_id)
  221. << " must be an OpLabel";
  222. }
  223. if (merge_id == continue_id) {
  224. return _.diag(SPV_ERROR_INVALID_ID, inst)
  225. << "Merge Block and Continue Target must be different ids";
  226. }
  227. const auto loop_control = inst->GetOperandAs<uint32_t>(2);
  228. if ((loop_control >> SpvLoopControlUnrollShift) & 0x1 &&
  229. (loop_control >> SpvLoopControlDontUnrollShift) & 0x1) {
  230. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  231. << "Unroll and DontUnroll loop controls must not both be specified";
  232. }
  233. if ((loop_control >> SpvLoopControlDontUnrollShift) & 0x1 &&
  234. (loop_control >> SpvLoopControlPeelCountShift) & 0x1) {
  235. return _.diag(SPV_ERROR_INVALID_DATA, inst) << "PeelCount and DontUnroll "
  236. "loop controls must not "
  237. "both be specified";
  238. }
  239. if ((loop_control >> SpvLoopControlDontUnrollShift) & 0x1 &&
  240. (loop_control >> SpvLoopControlPartialCountShift) & 0x1) {
  241. return _.diag(SPV_ERROR_INVALID_DATA, inst) << "PartialCount and "
  242. "DontUnroll loop controls "
  243. "must not both be specified";
  244. }
  245. uint32_t operand = 3;
  246. if ((loop_control >> SpvLoopControlDependencyLengthShift) & 0x1) {
  247. ++operand;
  248. }
  249. if ((loop_control >> SpvLoopControlMinIterationsShift) & 0x1) {
  250. ++operand;
  251. }
  252. if ((loop_control >> SpvLoopControlMaxIterationsShift) & 0x1) {
  253. ++operand;
  254. }
  255. if ((loop_control >> SpvLoopControlIterationMultipleShift) & 0x1) {
  256. if (inst->operands().size() < operand ||
  257. inst->GetOperandAs<uint32_t>(operand) == 0) {
  258. return _.diag(SPV_ERROR_INVALID_DATA, inst) << "IterationMultiple loop "
  259. "control operand must be "
  260. "greater than zero";
  261. }
  262. ++operand;
  263. }
  264. if ((loop_control >> SpvLoopControlPeelCountShift) & 0x1) {
  265. ++operand;
  266. }
  267. if ((loop_control >> SpvLoopControlPartialCountShift) & 0x1) {
  268. ++operand;
  269. }
  270. // That the right number of operands is present is checked by the parser. The
  271. // above code tracks operands for expanded validation checking in the future.
  272. return SPV_SUCCESS;
  273. }
  274. } // namespace
  275. void printDominatorList(const BasicBlock& b) {
  276. std::cout << b.id() << " is dominated by: ";
  277. const BasicBlock* bb = &b;
  278. while (bb->immediate_dominator() != bb) {
  279. bb = bb->immediate_dominator();
  280. std::cout << bb->id() << " ";
  281. }
  282. }
  283. #define CFG_ASSERT(ASSERT_FUNC, TARGET) \
  284. if (spv_result_t rcode = ASSERT_FUNC(_, TARGET)) return rcode
  285. spv_result_t FirstBlockAssert(ValidationState_t& _, uint32_t target) {
  286. if (_.current_function().IsFirstBlock(target)) {
  287. return _.diag(SPV_ERROR_INVALID_CFG, _.FindDef(_.current_function().id()))
  288. << "First block " << _.getIdName(target) << " of function "
  289. << _.getIdName(_.current_function().id()) << " is targeted by block "
  290. << _.getIdName(_.current_function().current_block()->id());
  291. }
  292. return SPV_SUCCESS;
  293. }
  294. spv_result_t MergeBlockAssert(ValidationState_t& _, uint32_t merge_block) {
  295. if (_.current_function().IsBlockType(merge_block, kBlockTypeMerge)) {
  296. return _.diag(SPV_ERROR_INVALID_CFG, _.FindDef(_.current_function().id()))
  297. << "Block " << _.getIdName(merge_block)
  298. << " is already a merge block for another header";
  299. }
  300. return SPV_SUCCESS;
  301. }
  302. /// Update the continue construct's exit blocks once the backedge blocks are
  303. /// identified in the CFG.
  304. void UpdateContinueConstructExitBlocks(
  305. Function& function,
  306. const std::vector<std::pair<uint32_t, uint32_t>>& back_edges) {
  307. auto& constructs = function.constructs();
  308. // TODO(umar): Think of a faster way to do this
  309. for (auto& edge : back_edges) {
  310. uint32_t back_edge_block_id;
  311. uint32_t loop_header_block_id;
  312. std::tie(back_edge_block_id, loop_header_block_id) = edge;
  313. auto is_this_header = [=](Construct& c) {
  314. return c.type() == ConstructType::kLoop &&
  315. c.entry_block()->id() == loop_header_block_id;
  316. };
  317. for (auto construct : constructs) {
  318. if (is_this_header(construct)) {
  319. Construct* continue_construct =
  320. construct.corresponding_constructs().back();
  321. assert(continue_construct->type() == ConstructType::kContinue);
  322. BasicBlock* back_edge_block;
  323. std::tie(back_edge_block, std::ignore) =
  324. function.GetBlock(back_edge_block_id);
  325. continue_construct->set_exit(back_edge_block);
  326. }
  327. }
  328. }
  329. }
  330. std::tuple<std::string, std::string, std::string> ConstructNames(
  331. ConstructType type) {
  332. std::string construct_name, header_name, exit_name;
  333. switch (type) {
  334. case ConstructType::kSelection:
  335. construct_name = "selection";
  336. header_name = "selection header";
  337. exit_name = "merge block";
  338. break;
  339. case ConstructType::kLoop:
  340. construct_name = "loop";
  341. header_name = "loop header";
  342. exit_name = "merge block";
  343. break;
  344. case ConstructType::kContinue:
  345. construct_name = "continue";
  346. header_name = "continue target";
  347. exit_name = "back-edge block";
  348. break;
  349. case ConstructType::kCase:
  350. construct_name = "case";
  351. header_name = "case entry block";
  352. exit_name = "case exit block";
  353. break;
  354. default:
  355. assert(1 == 0 && "Not defined type");
  356. }
  357. return std::make_tuple(construct_name, header_name, exit_name);
  358. }
  359. /// Constructs an error message for construct validation errors
  360. std::string ConstructErrorString(const Construct& construct,
  361. const std::string& header_string,
  362. const std::string& exit_string,
  363. const std::string& dominate_text) {
  364. std::string construct_name, header_name, exit_name;
  365. std::tie(construct_name, header_name, exit_name) =
  366. ConstructNames(construct.type());
  367. // TODO(umar): Add header block for continue constructs to error message
  368. return "The " + construct_name + " construct with the " + header_name + " " +
  369. header_string + " " + dominate_text + " the " + exit_name + " " +
  370. exit_string;
  371. }
  372. // Finds the fall through case construct of |target_block| and records it in
  373. // |case_fall_through|. Returns SPV_ERROR_INVALID_CFG if the case construct
  374. // headed by |target_block| branches to multiple case constructs.
  375. spv_result_t FindCaseFallThrough(
  376. ValidationState_t& _, BasicBlock* target_block, uint32_t* case_fall_through,
  377. const BasicBlock* merge, const std::unordered_set<uint32_t>& case_targets,
  378. Function* function) {
  379. std::vector<BasicBlock*> stack;
  380. stack.push_back(target_block);
  381. std::unordered_set<const BasicBlock*> visited;
  382. bool target_reachable = target_block->reachable();
  383. int target_depth = function->GetBlockDepth(target_block);
  384. while (!stack.empty()) {
  385. auto block = stack.back();
  386. stack.pop_back();
  387. if (block == merge) continue;
  388. if (!visited.insert(block).second) continue;
  389. if (target_reachable && block->reachable() &&
  390. target_block->dominates(*block)) {
  391. // Still in the case construct.
  392. for (auto successor : *block->successors()) {
  393. stack.push_back(successor);
  394. }
  395. } else {
  396. // Exiting the case construct to non-merge block.
  397. if (!case_targets.count(block->id())) {
  398. int depth = function->GetBlockDepth(block);
  399. if ((depth < target_depth) ||
  400. (depth == target_depth && block->is_type(kBlockTypeContinue))) {
  401. continue;
  402. }
  403. return _.diag(SPV_ERROR_INVALID_CFG, target_block->label())
  404. << "Case construct that targets "
  405. << _.getIdName(target_block->id())
  406. << " has invalid branch to block " << _.getIdName(block->id())
  407. << " (not another case construct, corresponding merge, outer "
  408. "loop merge or outer loop continue)";
  409. }
  410. if (*case_fall_through == 0u) {
  411. if (target_block != block) {
  412. *case_fall_through = block->id();
  413. }
  414. } else if (*case_fall_through != block->id()) {
  415. // Case construct has at most one branch to another case construct.
  416. return _.diag(SPV_ERROR_INVALID_CFG, target_block->label())
  417. << "Case construct that targets "
  418. << _.getIdName(target_block->id())
  419. << " has branches to multiple other case construct targets "
  420. << _.getIdName(*case_fall_through) << " and "
  421. << _.getIdName(block->id());
  422. }
  423. }
  424. }
  425. return SPV_SUCCESS;
  426. }
  427. spv_result_t StructuredSwitchChecks(ValidationState_t& _, Function* function,
  428. const Instruction* switch_inst,
  429. const BasicBlock* header,
  430. const BasicBlock* merge) {
  431. std::unordered_set<uint32_t> case_targets;
  432. for (uint32_t i = 1; i < switch_inst->operands().size(); i += 2) {
  433. uint32_t target = switch_inst->GetOperandAs<uint32_t>(i);
  434. if (target != merge->id()) case_targets.insert(target);
  435. }
  436. // Tracks how many times each case construct is targeted by another case
  437. // construct.
  438. std::map<uint32_t, uint32_t> num_fall_through_targeted;
  439. uint32_t default_case_fall_through = 0u;
  440. uint32_t default_target = switch_inst->GetOperandAs<uint32_t>(1u);
  441. bool default_appears_multiple_times = false;
  442. for (uint32_t i = 3; i < switch_inst->operands().size(); i += 2) {
  443. if (default_target == switch_inst->GetOperandAs<uint32_t>(i)) {
  444. default_appears_multiple_times = true;
  445. break;
  446. }
  447. }
  448. std::unordered_map<uint32_t, uint32_t> seen_to_fall_through;
  449. for (uint32_t i = 1; i < switch_inst->operands().size(); i += 2) {
  450. uint32_t target = switch_inst->GetOperandAs<uint32_t>(i);
  451. if (target == merge->id()) continue;
  452. uint32_t case_fall_through = 0u;
  453. auto seen_iter = seen_to_fall_through.find(target);
  454. if (seen_iter == seen_to_fall_through.end()) {
  455. const auto target_block = function->GetBlock(target).first;
  456. // OpSwitch must dominate all its case constructs.
  457. if (header->reachable() && target_block->reachable() &&
  458. !header->dominates(*target_block)) {
  459. return _.diag(SPV_ERROR_INVALID_CFG, header->label())
  460. << "Selection header " << _.getIdName(header->id())
  461. << " does not dominate its case construct "
  462. << _.getIdName(target);
  463. }
  464. if (auto error = FindCaseFallThrough(_, target_block, &case_fall_through,
  465. merge, case_targets, function)) {
  466. return error;
  467. }
  468. // Track how many time the fall through case has been targeted.
  469. if (case_fall_through != 0u) {
  470. auto where = num_fall_through_targeted.lower_bound(case_fall_through);
  471. if (where == num_fall_through_targeted.end() ||
  472. where->first != case_fall_through) {
  473. num_fall_through_targeted.insert(
  474. where, std::make_pair(case_fall_through, 1));
  475. } else {
  476. where->second++;
  477. }
  478. }
  479. seen_to_fall_through.insert(std::make_pair(target, case_fall_through));
  480. } else {
  481. case_fall_through = seen_iter->second;
  482. }
  483. if (case_fall_through == default_target &&
  484. !default_appears_multiple_times) {
  485. case_fall_through = default_case_fall_through;
  486. }
  487. if (case_fall_through != 0u) {
  488. bool is_default = i == 1;
  489. if (is_default) {
  490. default_case_fall_through = case_fall_through;
  491. } else {
  492. // Allow code like:
  493. // case x:
  494. // case y:
  495. // ...
  496. // case z:
  497. //
  498. // Where x and y target the same block and fall through to z.
  499. uint32_t j = i;
  500. while ((j + 2 < switch_inst->operands().size()) &&
  501. target == switch_inst->GetOperandAs<uint32_t>(j + 2)) {
  502. j += 2;
  503. }
  504. // If Target T1 branches to Target T2, or if Target T1 branches to the
  505. // Default target and the Default target branches to Target T2, then T1
  506. // must immediately precede T2 in the list of OpSwitch Target operands.
  507. if ((switch_inst->operands().size() < j + 2) ||
  508. (case_fall_through != switch_inst->GetOperandAs<uint32_t>(j + 2))) {
  509. return _.diag(SPV_ERROR_INVALID_CFG, switch_inst)
  510. << "Case construct that targets " << _.getIdName(target)
  511. << " has branches to the case construct that targets "
  512. << _.getIdName(case_fall_through)
  513. << ", but does not immediately precede it in the "
  514. "OpSwitch's target list";
  515. }
  516. }
  517. }
  518. }
  519. // Each case construct must be branched to by at most one other case
  520. // construct.
  521. for (const auto& pair : num_fall_through_targeted) {
  522. if (pair.second > 1) {
  523. return _.diag(SPV_ERROR_INVALID_CFG, _.FindDef(pair.first))
  524. << "Multiple case constructs have branches to the case construct "
  525. "that targets "
  526. << _.getIdName(pair.first);
  527. }
  528. }
  529. return SPV_SUCCESS;
  530. }
  531. // Validates that all CFG divergences (i.e. conditional branch or switch) are
  532. // structured correctly. Either divergence is preceded by a merge instruction
  533. // or the divergence introduces at most one unseen label.
  534. spv_result_t ValidateStructuredSelections(
  535. ValidationState_t& _, const std::vector<const BasicBlock*>& postorder) {
  536. std::unordered_set<uint32_t> seen;
  537. for (auto iter = postorder.rbegin(); iter != postorder.rend(); ++iter) {
  538. const auto* block = *iter;
  539. const auto* terminator = block->terminator();
  540. if (!terminator) continue;
  541. const auto index = terminator - &_.ordered_instructions()[0];
  542. auto* merge = &_.ordered_instructions()[index - 1];
  543. // Marks merges and continues as seen.
  544. if (merge->opcode() == SpvOpSelectionMerge) {
  545. seen.insert(merge->GetOperandAs<uint32_t>(0));
  546. } else if (merge->opcode() == SpvOpLoopMerge) {
  547. seen.insert(merge->GetOperandAs<uint32_t>(0));
  548. seen.insert(merge->GetOperandAs<uint32_t>(1));
  549. } else {
  550. // Only track the pointer if it is a merge instruction.
  551. merge = nullptr;
  552. }
  553. // Skip unreachable blocks.
  554. if (!block->reachable()) continue;
  555. if (terminator->opcode() == SpvOpBranchConditional) {
  556. const auto true_label = terminator->GetOperandAs<uint32_t>(1);
  557. const auto false_label = terminator->GetOperandAs<uint32_t>(2);
  558. // Mark the upcoming blocks as seen now, but only error out if this block
  559. // was missing a merge instruction and both labels hadn't been seen
  560. // previously.
  561. const bool both_unseen =
  562. seen.insert(true_label).second && seen.insert(false_label).second;
  563. if (!merge && both_unseen) {
  564. return _.diag(SPV_ERROR_INVALID_CFG, terminator)
  565. << "Selection must be structured";
  566. }
  567. } else if (terminator->opcode() == SpvOpSwitch) {
  568. uint32_t count = 0;
  569. // Mark the targets as seen now, but only error out if this block was
  570. // missing a merge instruction and there were multiple unseen labels.
  571. for (uint32_t i = 1; i < terminator->operands().size(); i += 2) {
  572. const auto target = terminator->GetOperandAs<uint32_t>(i);
  573. if (seen.insert(target).second) {
  574. count++;
  575. }
  576. }
  577. if (!merge && count > 1) {
  578. return _.diag(SPV_ERROR_INVALID_CFG, terminator)
  579. << "Selection must be structured";
  580. }
  581. }
  582. }
  583. return SPV_SUCCESS;
  584. }
  585. spv_result_t StructuredControlFlowChecks(
  586. ValidationState_t& _, Function* function,
  587. const std::vector<std::pair<uint32_t, uint32_t>>& back_edges,
  588. const std::vector<const BasicBlock*>& postorder) {
  589. /// Check all backedges target only loop headers and have exactly one
  590. /// back-edge branching to it
  591. // Map a loop header to blocks with back-edges to the loop header.
  592. std::map<uint32_t, std::unordered_set<uint32_t>> loop_latch_blocks;
  593. for (auto back_edge : back_edges) {
  594. uint32_t back_edge_block;
  595. uint32_t header_block;
  596. std::tie(back_edge_block, header_block) = back_edge;
  597. if (!function->IsBlockType(header_block, kBlockTypeLoop)) {
  598. return _.diag(SPV_ERROR_INVALID_CFG, _.FindDef(back_edge_block))
  599. << "Back-edges (" << _.getIdName(back_edge_block) << " -> "
  600. << _.getIdName(header_block)
  601. << ") can only be formed between a block and a loop header.";
  602. }
  603. loop_latch_blocks[header_block].insert(back_edge_block);
  604. }
  605. // Check the loop headers have exactly one back-edge branching to it
  606. for (BasicBlock* loop_header : function->ordered_blocks()) {
  607. if (!loop_header->reachable()) continue;
  608. if (!loop_header->is_type(kBlockTypeLoop)) continue;
  609. auto loop_header_id = loop_header->id();
  610. auto num_latch_blocks = loop_latch_blocks[loop_header_id].size();
  611. if (num_latch_blocks != 1) {
  612. return _.diag(SPV_ERROR_INVALID_CFG, _.FindDef(loop_header_id))
  613. << "Loop header " << _.getIdName(loop_header_id)
  614. << " is targeted by " << num_latch_blocks
  615. << " back-edge blocks but the standard requires exactly one";
  616. }
  617. }
  618. // Check construct rules
  619. for (const Construct& construct : function->constructs()) {
  620. auto header = construct.entry_block();
  621. auto merge = construct.exit_block();
  622. if (header->reachable() && !merge) {
  623. std::string construct_name, header_name, exit_name;
  624. std::tie(construct_name, header_name, exit_name) =
  625. ConstructNames(construct.type());
  626. return _.diag(SPV_ERROR_INTERNAL, _.FindDef(header->id()))
  627. << "Construct " + construct_name + " with " + header_name + " " +
  628. _.getIdName(header->id()) + " does not have a " +
  629. exit_name + ". This may be a bug in the validator.";
  630. }
  631. // If the exit block is reachable then it's dominated by the
  632. // header.
  633. if (merge && merge->reachable()) {
  634. if (!header->dominates(*merge)) {
  635. return _.diag(SPV_ERROR_INVALID_CFG, _.FindDef(merge->id()))
  636. << ConstructErrorString(construct, _.getIdName(header->id()),
  637. _.getIdName(merge->id()),
  638. "does not dominate");
  639. }
  640. // If it's really a merge block for a selection or loop, then it must be
  641. // *strictly* dominated by the header.
  642. if (construct.ExitBlockIsMergeBlock() && (header == merge)) {
  643. return _.diag(SPV_ERROR_INVALID_CFG, _.FindDef(merge->id()))
  644. << ConstructErrorString(construct, _.getIdName(header->id()),
  645. _.getIdName(merge->id()),
  646. "does not strictly dominate");
  647. }
  648. }
  649. // Check post-dominance for continue constructs. But dominance and
  650. // post-dominance only make sense when the construct is reachable.
  651. if (header->reachable() && construct.type() == ConstructType::kContinue) {
  652. if (!merge->postdominates(*header)) {
  653. return _.diag(SPV_ERROR_INVALID_CFG, _.FindDef(merge->id()))
  654. << ConstructErrorString(construct, _.getIdName(header->id()),
  655. _.getIdName(merge->id()),
  656. "is not post dominated by");
  657. }
  658. }
  659. Construct::ConstructBlockSet construct_blocks = construct.blocks(function);
  660. for (auto block : construct_blocks) {
  661. std::string construct_name, header_name, exit_name;
  662. std::tie(construct_name, header_name, exit_name) =
  663. ConstructNames(construct.type());
  664. // Check that all exits from the construct are via structured exits.
  665. for (auto succ : *block->successors()) {
  666. if (block->reachable() && !construct_blocks.count(succ) &&
  667. !construct.IsStructuredExit(_, succ)) {
  668. return _.diag(SPV_ERROR_INVALID_CFG, _.FindDef(block->id()))
  669. << "block <ID> " << _.getIdName(block->id()) << " exits the "
  670. << construct_name << " headed by <ID> "
  671. << _.getIdName(header->id())
  672. << ", but not via a structured exit";
  673. }
  674. }
  675. if (block == header) continue;
  676. // Check that for all non-header blocks, all predecessors are within this
  677. // construct.
  678. for (auto pred : *block->predecessors()) {
  679. if (pred->reachable() && !construct_blocks.count(pred)) {
  680. return _.diag(SPV_ERROR_INVALID_CFG, _.FindDef(pred->id()))
  681. << "block <ID> " << pred->id() << " branches to the "
  682. << construct_name << " construct, but not to the "
  683. << header_name << " <ID> " << header->id();
  684. }
  685. }
  686. }
  687. // Checks rules for case constructs.
  688. if (construct.type() == ConstructType::kSelection &&
  689. header->terminator()->opcode() == SpvOpSwitch) {
  690. const auto terminator = header->terminator();
  691. if (auto error =
  692. StructuredSwitchChecks(_, function, terminator, header, merge)) {
  693. return error;
  694. }
  695. }
  696. }
  697. if (auto error = ValidateStructuredSelections(_, postorder)) {
  698. return error;
  699. }
  700. return SPV_SUCCESS;
  701. }
  702. spv_result_t PerformWebGPUCfgChecks(ValidationState_t& _, Function* function) {
  703. for (auto& block : function->ordered_blocks()) {
  704. if (block->reachable()) continue;
  705. if (block->is_type(kBlockTypeMerge)) {
  706. // 1. Find the referencing merge and confirm that it is reachable.
  707. BasicBlock* merge_header = function->GetMergeHeader(block);
  708. assert(merge_header != nullptr);
  709. if (!merge_header->reachable()) {
  710. return _.diag(SPV_ERROR_INVALID_CFG, _.FindDef(block->id()))
  711. << "For WebGPU, unreachable merge-blocks must be referenced by "
  712. "a reachable merge instruction.";
  713. }
  714. // 2. Check that the only instructions are OpLabel and OpUnreachable.
  715. auto* label_inst = block->label();
  716. auto* terminator_inst = block->terminator();
  717. assert(label_inst != nullptr);
  718. assert(terminator_inst != nullptr);
  719. if (terminator_inst->opcode() != SpvOpUnreachable) {
  720. return _.diag(SPV_ERROR_INVALID_CFG, _.FindDef(block->id()))
  721. << "For WebGPU, unreachable merge-blocks must terminate with "
  722. "OpUnreachable.";
  723. }
  724. auto label_idx = label_inst - &_.ordered_instructions()[0];
  725. auto terminator_idx = terminator_inst - &_.ordered_instructions()[0];
  726. if (label_idx + 1 != terminator_idx) {
  727. return _.diag(SPV_ERROR_INVALID_CFG, _.FindDef(block->id()))
  728. << "For WebGPU, unreachable merge-blocks must only contain an "
  729. "OpLabel and OpUnreachable instruction.";
  730. }
  731. // 3. Use label instruction to confirm there is no uses by branches.
  732. for (auto use : label_inst->uses()) {
  733. const auto* use_inst = use.first;
  734. if (spvOpcodeIsBranch(use_inst->opcode())) {
  735. return _.diag(SPV_ERROR_INVALID_CFG, _.FindDef(block->id()))
  736. << "For WebGPU, unreachable merge-blocks cannot be the target "
  737. "of a branch.";
  738. }
  739. }
  740. } else if (block->is_type(kBlockTypeContinue)) {
  741. // 1. Find referencing loop and confirm that it is reachable.
  742. std::vector<BasicBlock*> continue_headers =
  743. function->GetContinueHeaders(block);
  744. if (continue_headers.empty()) {
  745. return _.diag(SPV_ERROR_INVALID_CFG, _.FindDef(block->id()))
  746. << "For WebGPU, unreachable continue-target must be referenced "
  747. "by a loop instruction.";
  748. }
  749. std::vector<BasicBlock*> reachable_headers(continue_headers.size());
  750. auto iter =
  751. std::copy_if(continue_headers.begin(), continue_headers.end(),
  752. reachable_headers.begin(),
  753. [](BasicBlock* header) { return header->reachable(); });
  754. reachable_headers.resize(std::distance(reachable_headers.begin(), iter));
  755. if (reachable_headers.empty()) {
  756. return _.diag(SPV_ERROR_INVALID_CFG, _.FindDef(block->id()))
  757. << "For WebGPU, unreachable continue-target must be referenced "
  758. "by a reachable loop instruction.";
  759. }
  760. // 2. Check that the only instructions are OpLabel and OpBranch.
  761. auto* label_inst = block->label();
  762. auto* terminator_inst = block->terminator();
  763. assert(label_inst != nullptr);
  764. assert(terminator_inst != nullptr);
  765. if (terminator_inst->opcode() != SpvOpBranch) {
  766. return _.diag(SPV_ERROR_INVALID_CFG, _.FindDef(block->id()))
  767. << "For WebGPU, unreachable continue-target must terminate with "
  768. "OpBranch.";
  769. }
  770. auto label_idx = label_inst - &_.ordered_instructions()[0];
  771. auto terminator_idx = terminator_inst - &_.ordered_instructions()[0];
  772. if (label_idx + 1 != terminator_idx) {
  773. return _.diag(SPV_ERROR_INVALID_CFG, _.FindDef(block->id()))
  774. << "For WebGPU, unreachable continue-target must only contain "
  775. "an OpLabel and an OpBranch instruction.";
  776. }
  777. // 3. Use label instruction to confirm there is no uses by branches.
  778. for (auto use : label_inst->uses()) {
  779. const auto* use_inst = use.first;
  780. if (spvOpcodeIsBranch(use_inst->opcode())) {
  781. return _.diag(SPV_ERROR_INVALID_CFG, _.FindDef(block->id()))
  782. << "For WebGPU, unreachable continue-target cannot be the "
  783. "target of a branch.";
  784. }
  785. }
  786. // 4. Confirm that continue-target has a back edge to a reachable loop
  787. // header block.
  788. auto branch_target = terminator_inst->GetOperandAs<uint32_t>(0);
  789. for (auto* continue_header : reachable_headers) {
  790. if (branch_target != continue_header->id()) {
  791. return _.diag(SPV_ERROR_INVALID_CFG, _.FindDef(block->id()))
  792. << "For WebGPU, unreachable continue-target must only have a "
  793. "back edge to a single reachable loop instruction.";
  794. }
  795. }
  796. } else {
  797. return _.diag(SPV_ERROR_INVALID_CFG, _.FindDef(block->id()))
  798. << "For WebGPU, all blocks must be reachable, unless they are "
  799. << "degenerate cases of merge-block or continue-target.";
  800. }
  801. }
  802. return SPV_SUCCESS;
  803. }
  804. spv_result_t PerformCfgChecks(ValidationState_t& _) {
  805. for (auto& function : _.functions()) {
  806. // Check all referenced blocks are defined within a function
  807. if (function.undefined_block_count() != 0) {
  808. std::string undef_blocks("{");
  809. bool first = true;
  810. for (auto undefined_block : function.undefined_blocks()) {
  811. undef_blocks += _.getIdName(undefined_block);
  812. if (!first) {
  813. undef_blocks += " ";
  814. }
  815. first = false;
  816. }
  817. return _.diag(SPV_ERROR_INVALID_CFG, _.FindDef(function.id()))
  818. << "Block(s) " << undef_blocks << "}"
  819. << " are referenced but not defined in function "
  820. << _.getIdName(function.id());
  821. }
  822. // Set each block's immediate dominator and immediate postdominator,
  823. // and find all back-edges.
  824. //
  825. // We want to analyze all the blocks in the function, even in degenerate
  826. // control flow cases including unreachable blocks. So use the augmented
  827. // CFG to ensure we cover all the blocks.
  828. std::vector<const BasicBlock*> postorder;
  829. std::vector<const BasicBlock*> postdom_postorder;
  830. std::vector<std::pair<uint32_t, uint32_t>> back_edges;
  831. auto ignore_block = [](const BasicBlock*) {};
  832. auto ignore_edge = [](const BasicBlock*, const BasicBlock*) {};
  833. if (!function.ordered_blocks().empty()) {
  834. /// calculate dominators
  835. CFA<BasicBlock>::DepthFirstTraversal(
  836. function.first_block(), function.AugmentedCFGSuccessorsFunction(),
  837. ignore_block, [&](const BasicBlock* b) { postorder.push_back(b); },
  838. ignore_edge);
  839. auto edges = CFA<BasicBlock>::CalculateDominators(
  840. postorder, function.AugmentedCFGPredecessorsFunction());
  841. for (auto edge : edges) {
  842. if (edge.first != edge.second)
  843. edge.first->SetImmediateDominator(edge.second);
  844. }
  845. /// calculate post dominators
  846. CFA<BasicBlock>::DepthFirstTraversal(
  847. function.pseudo_exit_block(),
  848. function.AugmentedCFGPredecessorsFunction(), ignore_block,
  849. [&](const BasicBlock* b) { postdom_postorder.push_back(b); },
  850. ignore_edge);
  851. auto postdom_edges = CFA<BasicBlock>::CalculateDominators(
  852. postdom_postorder, function.AugmentedCFGSuccessorsFunction());
  853. for (auto edge : postdom_edges) {
  854. edge.first->SetImmediatePostDominator(edge.second);
  855. }
  856. /// calculate back edges.
  857. CFA<BasicBlock>::DepthFirstTraversal(
  858. function.pseudo_entry_block(),
  859. function
  860. .AugmentedCFGSuccessorsFunctionIncludingHeaderToContinueEdge(),
  861. ignore_block, ignore_block,
  862. [&](const BasicBlock* from, const BasicBlock* to) {
  863. back_edges.emplace_back(from->id(), to->id());
  864. });
  865. }
  866. UpdateContinueConstructExitBlocks(function, back_edges);
  867. auto& blocks = function.ordered_blocks();
  868. if (!blocks.empty()) {
  869. // Check if the order of blocks in the binary appear before the blocks
  870. // they dominate
  871. for (auto block = begin(blocks) + 1; block != end(blocks); ++block) {
  872. if (auto idom = (*block)->immediate_dominator()) {
  873. if (idom != function.pseudo_entry_block() &&
  874. block == std::find(begin(blocks), block, idom)) {
  875. return _.diag(SPV_ERROR_INVALID_CFG, _.FindDef(idom->id()))
  876. << "Block " << _.getIdName((*block)->id())
  877. << " appears in the binary before its dominator "
  878. << _.getIdName(idom->id());
  879. }
  880. }
  881. // For WebGPU check that all unreachable blocks are degenerate cases for
  882. // merge-block or continue-target.
  883. if (spvIsWebGPUEnv(_.context()->target_env)) {
  884. spv_result_t result = PerformWebGPUCfgChecks(_, &function);
  885. if (result != SPV_SUCCESS) return result;
  886. }
  887. }
  888. // If we have structed control flow, check that no block has a control
  889. // flow nesting depth larger than the limit.
  890. if (_.HasCapability(SpvCapabilityShader)) {
  891. const int control_flow_nesting_depth_limit =
  892. _.options()->universal_limits_.max_control_flow_nesting_depth;
  893. for (auto block = begin(blocks); block != end(blocks); ++block) {
  894. if (function.GetBlockDepth(*block) >
  895. control_flow_nesting_depth_limit) {
  896. return _.diag(SPV_ERROR_INVALID_CFG, _.FindDef((*block)->id()))
  897. << "Maximum Control Flow nesting depth exceeded.";
  898. }
  899. }
  900. }
  901. }
  902. /// Structured control flow checks are only required for shader capabilities
  903. if (_.HasCapability(SpvCapabilityShader)) {
  904. if (auto error =
  905. StructuredControlFlowChecks(_, &function, back_edges, postorder))
  906. return error;
  907. }
  908. }
  909. return SPV_SUCCESS;
  910. }
  911. spv_result_t CfgPass(ValidationState_t& _, const Instruction* inst) {
  912. SpvOp opcode = inst->opcode();
  913. switch (opcode) {
  914. case SpvOpLabel:
  915. if (auto error = _.current_function().RegisterBlock(inst->id()))
  916. return error;
  917. // TODO(github:1661) This should be done in the
  918. // ValidationState::RegisterInstruction method but because of the order of
  919. // passes the OpLabel ends up not being part of the basic block it starts.
  920. _.current_function().current_block()->set_label(inst);
  921. break;
  922. case SpvOpLoopMerge: {
  923. uint32_t merge_block = inst->GetOperandAs<uint32_t>(0);
  924. uint32_t continue_block = inst->GetOperandAs<uint32_t>(1);
  925. CFG_ASSERT(MergeBlockAssert, merge_block);
  926. if (auto error = _.current_function().RegisterLoopMerge(merge_block,
  927. continue_block))
  928. return error;
  929. } break;
  930. case SpvOpSelectionMerge: {
  931. uint32_t merge_block = inst->GetOperandAs<uint32_t>(0);
  932. CFG_ASSERT(MergeBlockAssert, merge_block);
  933. if (auto error = _.current_function().RegisterSelectionMerge(merge_block))
  934. return error;
  935. } break;
  936. case SpvOpBranch: {
  937. uint32_t target = inst->GetOperandAs<uint32_t>(0);
  938. CFG_ASSERT(FirstBlockAssert, target);
  939. _.current_function().RegisterBlockEnd({target}, opcode);
  940. } break;
  941. case SpvOpBranchConditional: {
  942. uint32_t tlabel = inst->GetOperandAs<uint32_t>(1);
  943. uint32_t flabel = inst->GetOperandAs<uint32_t>(2);
  944. CFG_ASSERT(FirstBlockAssert, tlabel);
  945. CFG_ASSERT(FirstBlockAssert, flabel);
  946. _.current_function().RegisterBlockEnd({tlabel, flabel}, opcode);
  947. } break;
  948. case SpvOpSwitch: {
  949. std::vector<uint32_t> cases;
  950. for (size_t i = 1; i < inst->operands().size(); i += 2) {
  951. uint32_t target = inst->GetOperandAs<uint32_t>(i);
  952. CFG_ASSERT(FirstBlockAssert, target);
  953. cases.push_back(target);
  954. }
  955. _.current_function().RegisterBlockEnd({cases}, opcode);
  956. } break;
  957. case SpvOpReturn: {
  958. const uint32_t return_type = _.current_function().GetResultTypeId();
  959. const Instruction* return_type_inst = _.FindDef(return_type);
  960. assert(return_type_inst);
  961. if (return_type_inst->opcode() != SpvOpTypeVoid)
  962. return _.diag(SPV_ERROR_INVALID_CFG, inst)
  963. << "OpReturn can only be called from a function with void "
  964. << "return type.";
  965. }
  966. // Fallthrough.
  967. case SpvOpKill:
  968. case SpvOpReturnValue:
  969. case SpvOpUnreachable:
  970. _.current_function().RegisterBlockEnd(std::vector<uint32_t>(), opcode);
  971. if (opcode == SpvOpKill) {
  972. _.current_function().RegisterExecutionModelLimitation(
  973. SpvExecutionModelFragment,
  974. "OpKill requires Fragment execution model");
  975. }
  976. break;
  977. default:
  978. break;
  979. }
  980. return SPV_SUCCESS;
  981. }
  982. spv_result_t ControlFlowPass(ValidationState_t& _, const Instruction* inst) {
  983. switch (inst->opcode()) {
  984. case SpvOpPhi:
  985. if (auto error = ValidatePhi(_, inst)) return error;
  986. break;
  987. case SpvOpBranch:
  988. if (auto error = ValidateBranch(_, inst)) return error;
  989. break;
  990. case SpvOpBranchConditional:
  991. if (auto error = ValidateBranchConditional(_, inst)) return error;
  992. break;
  993. case SpvOpReturnValue:
  994. if (auto error = ValidateReturnValue(_, inst)) return error;
  995. break;
  996. case SpvOpSwitch:
  997. if (auto error = ValidateSwitch(_, inst)) return error;
  998. break;
  999. case SpvOpLoopMerge:
  1000. if (auto error = ValidateLoopMerge(_, inst)) return error;
  1001. break;
  1002. default:
  1003. break;
  1004. }
  1005. return SPV_SUCCESS;
  1006. }
  1007. } // namespace val
  1008. } // namespace spvtools