validate_adjacency.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 <string>
  17. #include "source/val/instruction.h"
  18. #include "source/val/validate.h"
  19. #include "source/val/validation_state.h"
  20. namespace spvtools {
  21. namespace val {
  22. enum {
  23. // Status right after meeting OpFunction.
  24. IN_NEW_FUNCTION,
  25. // Status right after meeting the entry block.
  26. IN_ENTRY_BLOCK,
  27. // Status right after meeting non-entry blocks.
  28. PHI_VALID,
  29. // Status right after meeting non-OpVariable instructions in the entry block
  30. // or non-OpPhi instructions in non-entry blocks, except OpLine.
  31. PHI_AND_VAR_INVALID,
  32. };
  33. spv_result_t ValidateAdjacency(ValidationState_t& _) {
  34. const auto& instructions = _.ordered_instructions();
  35. int adjacency_status = PHI_AND_VAR_INVALID;
  36. for (size_t i = 0; i < instructions.size(); ++i) {
  37. const auto& inst = instructions[i];
  38. switch (inst.opcode()) {
  39. case spv::Op::OpFunction:
  40. case spv::Op::OpFunctionParameter:
  41. adjacency_status = IN_NEW_FUNCTION;
  42. break;
  43. case spv::Op::OpLabel:
  44. adjacency_status =
  45. adjacency_status == IN_NEW_FUNCTION ? IN_ENTRY_BLOCK : PHI_VALID;
  46. break;
  47. case spv::Op::OpExtInst:
  48. case spv::Op::OpExtInstWithForwardRefsKHR:
  49. // If it is a debug info instruction, we do not change the status to
  50. // allow debug info instructions before OpVariable in a function.
  51. // TODO(https://gitlab.khronos.org/spirv/SPIR-V/issues/533): We need
  52. // to discuss the location of DebugScope, DebugNoScope, DebugDeclare,
  53. // and DebugValue.
  54. // NOTE: This does not apply to the non-semantic vulkan debug info.
  55. if (!spvExtInstIsDebugInfo(inst.ext_inst_type()) ||
  56. inst.ext_inst_type() ==
  57. SPV_EXT_INST_TYPE_NONSEMANTIC_SHADER_DEBUGINFO_100) {
  58. adjacency_status = PHI_AND_VAR_INVALID;
  59. }
  60. break;
  61. case spv::Op::OpPhi:
  62. if (adjacency_status != PHI_VALID) {
  63. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  64. << "OpPhi must appear within a non-entry block before all "
  65. << "non-OpPhi instructions "
  66. << "(except for OpLine, which can be mixed with OpPhi).";
  67. }
  68. break;
  69. case spv::Op::OpLine:
  70. case spv::Op::OpNoLine:
  71. break;
  72. case spv::Op::OpLoopMerge:
  73. adjacency_status = PHI_AND_VAR_INVALID;
  74. if (i != (instructions.size() - 1)) {
  75. switch (instructions[i + 1].opcode()) {
  76. case spv::Op::OpBranch:
  77. case spv::Op::OpBranchConditional:
  78. break;
  79. default:
  80. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  81. << "OpLoopMerge must immediately precede either an "
  82. << "OpBranch or OpBranchConditional instruction. "
  83. << "OpLoopMerge must be the second-to-last instruction in "
  84. << "its block.";
  85. }
  86. }
  87. break;
  88. case spv::Op::OpSelectionMerge:
  89. adjacency_status = PHI_AND_VAR_INVALID;
  90. if (i != (instructions.size() - 1)) {
  91. switch (instructions[i + 1].opcode()) {
  92. case spv::Op::OpBranchConditional:
  93. case spv::Op::OpSwitch:
  94. break;
  95. default:
  96. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  97. << "OpSelectionMerge must immediately precede either an "
  98. << "OpBranchConditional or OpSwitch instruction. "
  99. << "OpSelectionMerge must be the second-to-last "
  100. << "instruction in its block.";
  101. }
  102. }
  103. break;
  104. case spv::Op::OpVariable:
  105. if (inst.GetOperandAs<spv::StorageClass>(2) ==
  106. spv::StorageClass::Function &&
  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. case spv::Op::OpUntypedVariableKHR:
  114. if (inst.GetOperandAs<spv::StorageClass>(2) ==
  115. spv::StorageClass::Function &&
  116. adjacency_status != IN_ENTRY_BLOCK) {
  117. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  118. << "All OpUntypedVariableKHR instructions in a function must "
  119. "be the first instructions in the first block.";
  120. }
  121. break;
  122. default:
  123. adjacency_status = PHI_AND_VAR_INVALID;
  124. break;
  125. }
  126. }
  127. return SPV_SUCCESS;
  128. }
  129. } // namespace val
  130. } // namespace spvtools