validate_adjacency.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. // NOTE: This does not apply to the non-semantic vulkan debug info.
  56. if (!spvExtInstIsDebugInfo(inst.ext_inst_type()) ||
  57. inst.ext_inst_type() ==
  58. SPV_EXT_INST_TYPE_NONSEMANTIC_SHADER_DEBUGINFO_100) {
  59. adjacency_status = PHI_AND_VAR_INVALID;
  60. }
  61. break;
  62. case SpvOpPhi:
  63. if (adjacency_status != PHI_VALID) {
  64. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  65. << "OpPhi must appear within a non-entry block before all "
  66. << "non-OpPhi instructions "
  67. << "(except for OpLine, which can be mixed with OpPhi).";
  68. }
  69. break;
  70. case SpvOpLine:
  71. case SpvOpNoLine:
  72. break;
  73. case SpvOpLoopMerge:
  74. adjacency_status = PHI_AND_VAR_INVALID;
  75. if (i != (instructions.size() - 1)) {
  76. switch (instructions[i + 1].opcode()) {
  77. case SpvOpBranch:
  78. case SpvOpBranchConditional:
  79. break;
  80. default:
  81. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  82. << "OpLoopMerge must immediately precede either an "
  83. << "OpBranch or OpBranchConditional instruction. "
  84. << "OpLoopMerge must be the second-to-last instruction in "
  85. << "its block.";
  86. }
  87. }
  88. break;
  89. case SpvOpSelectionMerge:
  90. adjacency_status = PHI_AND_VAR_INVALID;
  91. if (i != (instructions.size() - 1)) {
  92. switch (instructions[i + 1].opcode()) {
  93. case SpvOpBranchConditional:
  94. case SpvOpSwitch:
  95. break;
  96. default:
  97. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  98. << "OpSelectionMerge must immediately precede either an "
  99. << "OpBranchConditional or OpSwitch instruction. "
  100. << "OpSelectionMerge must be the second-to-last "
  101. << "instruction in its block.";
  102. }
  103. }
  104. break;
  105. case SpvOpVariable:
  106. if (inst.GetOperandAs<SpvStorageClass>(2) == SpvStorageClassFunction &&
  107. adjacency_status != IN_ENTRY_BLOCK) {
  108. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  109. << "All OpVariable instructions in a function must be the "
  110. "first instructions in the first block.";
  111. }
  112. break;
  113. default:
  114. adjacency_status = PHI_AND_VAR_INVALID;
  115. break;
  116. }
  117. }
  118. return SPV_SUCCESS;
  119. }
  120. } // namespace val
  121. } // namespace spvtools