validate_adjacency.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright (c) 2018 LunarG 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. // Validates correctness of the intra-block preconditions of SPIR-V
  15. // instructions.
  16. #include "source/val/validate.h"
  17. #include <string>
  18. #include "source/diagnostic.h"
  19. #include "source/opcode.h"
  20. #include "source/val/instruction.h"
  21. #include "source/val/validation_state.h"
  22. namespace spvtools {
  23. namespace val {
  24. enum {
  25. // Status right after meeting OpFunction.
  26. IN_NEW_FUNCTION,
  27. // Status right after meeting the entry block.
  28. IN_ENTRY_BLOCK,
  29. // Status right after meeting non-entry blocks.
  30. PHI_VALID,
  31. // Status right after meeting non-OpVariable instructions in the entry block
  32. // or non-OpPhi instructions in non-entry blocks, except OpLine.
  33. PHI_AND_VAR_INVALID,
  34. };
  35. spv_result_t ValidateAdjacency(ValidationState_t& _) {
  36. const auto& instructions = _.ordered_instructions();
  37. int adjacency_status = PHI_AND_VAR_INVALID;
  38. for (size_t i = 0; i < instructions.size(); ++i) {
  39. const auto& inst = instructions[i];
  40. switch (inst.opcode()) {
  41. case SpvOpFunction:
  42. case SpvOpFunctionParameter:
  43. adjacency_status = IN_NEW_FUNCTION;
  44. break;
  45. case SpvOpLabel:
  46. adjacency_status =
  47. adjacency_status == IN_NEW_FUNCTION ? IN_ENTRY_BLOCK : PHI_VALID;
  48. break;
  49. case SpvOpPhi:
  50. if (adjacency_status != PHI_VALID) {
  51. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  52. << "OpPhi must appear within a non-entry block before all "
  53. << "non-OpPhi instructions "
  54. << "(except for OpLine, which can be mixed with OpPhi).";
  55. }
  56. break;
  57. case SpvOpLine:
  58. case SpvOpNoLine:
  59. break;
  60. case SpvOpLoopMerge:
  61. adjacency_status = PHI_AND_VAR_INVALID;
  62. if (i != (instructions.size() - 1)) {
  63. switch (instructions[i + 1].opcode()) {
  64. case SpvOpBranch:
  65. case SpvOpBranchConditional:
  66. break;
  67. default:
  68. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  69. << "OpLoopMerge must immediately precede either an "
  70. << "OpBranch or OpBranchConditional instruction. "
  71. << "OpLoopMerge must be the second-to-last instruction in "
  72. << "its block.";
  73. }
  74. }
  75. break;
  76. case SpvOpSelectionMerge:
  77. adjacency_status = PHI_AND_VAR_INVALID;
  78. if (i != (instructions.size() - 1)) {
  79. switch (instructions[i + 1].opcode()) {
  80. case SpvOpBranchConditional:
  81. case SpvOpSwitch:
  82. break;
  83. default:
  84. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  85. << "OpSelectionMerge must immediately precede either an "
  86. << "OpBranchConditional or OpSwitch instruction. "
  87. << "OpSelectionMerge must be the second-to-last "
  88. << "instruction in its block.";
  89. }
  90. }
  91. break;
  92. case SpvOpVariable:
  93. if (inst.GetOperandAs<SpvStorageClass>(2) == SpvStorageClassFunction &&
  94. adjacency_status != IN_ENTRY_BLOCK) {
  95. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  96. << "All OpVariable instructions in a function must be the "
  97. "first instructions in the first block.";
  98. }
  99. break;
  100. default:
  101. adjacency_status = PHI_AND_VAR_INVALID;
  102. break;
  103. }
  104. }
  105. return SPV_SUCCESS;
  106. }
  107. } // namespace val
  108. } // namespace spvtools