validate_adjacency.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 SpvOpExtInst:
  50. // If it is a debug info instruction, we do not change the status to
  51. // allow debug info instructions before OpVariable in a function.
  52. // TODO(https://gitlab.khronos.org/spirv/SPIR-V/issues/533): We need
  53. // to discuss the location of DebugScope, DebugNoScope, DebugDeclare,
  54. // and DebugValue.
  55. if (!spvExtInstIsDebugInfo(inst.ext_inst_type())) {
  56. adjacency_status = PHI_AND_VAR_INVALID;
  57. }
  58. break;
  59. case SpvOpPhi:
  60. if (adjacency_status != PHI_VALID) {
  61. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  62. << "OpPhi must appear within a non-entry block before all "
  63. << "non-OpPhi instructions "
  64. << "(except for OpLine, which can be mixed with OpPhi).";
  65. }
  66. break;
  67. case SpvOpLine:
  68. case SpvOpNoLine:
  69. break;
  70. case SpvOpLoopMerge:
  71. adjacency_status = PHI_AND_VAR_INVALID;
  72. if (i != (instructions.size() - 1)) {
  73. switch (instructions[i + 1].opcode()) {
  74. case SpvOpBranch:
  75. case SpvOpBranchConditional:
  76. break;
  77. default:
  78. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  79. << "OpLoopMerge must immediately precede either an "
  80. << "OpBranch or OpBranchConditional instruction. "
  81. << "OpLoopMerge must be the second-to-last instruction in "
  82. << "its block.";
  83. }
  84. }
  85. break;
  86. case SpvOpSelectionMerge:
  87. adjacency_status = PHI_AND_VAR_INVALID;
  88. if (i != (instructions.size() - 1)) {
  89. switch (instructions[i + 1].opcode()) {
  90. case SpvOpBranchConditional:
  91. case SpvOpSwitch:
  92. break;
  93. default:
  94. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  95. << "OpSelectionMerge must immediately precede either an "
  96. << "OpBranchConditional or OpSwitch instruction. "
  97. << "OpSelectionMerge must be the second-to-last "
  98. << "instruction in its block.";
  99. }
  100. }
  101. break;
  102. case SpvOpVariable:
  103. if (inst.GetOperandAs<SpvStorageClass>(2) == SpvStorageClassFunction &&
  104. adjacency_status != IN_ENTRY_BLOCK) {
  105. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  106. << "All OpVariable instructions in a function must be the "
  107. "first instructions in the first block.";
  108. }
  109. break;
  110. default:
  111. adjacency_status = PHI_AND_VAR_INVALID;
  112. break;
  113. }
  114. }
  115. return SPV_SUCCESS;
  116. }
  117. } // namespace val
  118. } // namespace spvtools