validate_layout.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. // Source code for logical layout validation as described in section 2.4
  15. #include "source/val/validate.h"
  16. #include <cassert>
  17. #include "source/diagnostic.h"
  18. #include "source/opcode.h"
  19. #include "source/operand.h"
  20. #include "source/val/function.h"
  21. #include "source/val/instruction.h"
  22. #include "source/val/validation_state.h"
  23. namespace spvtools {
  24. namespace val {
  25. namespace {
  26. // Module scoped instructions are processed by determining if the opcode
  27. // is part of the current layout section. If it is not then the next sections is
  28. // checked.
  29. spv_result_t ModuleScopedInstructions(ValidationState_t& _,
  30. const Instruction* inst, SpvOp opcode) {
  31. while (_.IsOpcodeInCurrentLayoutSection(opcode) == false) {
  32. _.ProgressToNextLayoutSectionOrder();
  33. switch (_.current_layout_section()) {
  34. case kLayoutMemoryModel:
  35. if (opcode != SpvOpMemoryModel) {
  36. return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
  37. << spvOpcodeString(opcode)
  38. << " cannot appear before the memory model instruction";
  39. }
  40. break;
  41. case kLayoutFunctionDeclarations:
  42. // All module sections have been processed. Recursively call
  43. // ModuleLayoutPass to process the next section of the module
  44. return ModuleLayoutPass(_, inst);
  45. default:
  46. break;
  47. }
  48. }
  49. return SPV_SUCCESS;
  50. }
  51. // Function declaration validation is performed by making sure that the
  52. // FunctionParameter and FunctionEnd instructions only appear inside of
  53. // functions. It also ensures that the Function instruction does not appear
  54. // inside of another function. This stage ends when the first label is
  55. // encountered inside of a function.
  56. spv_result_t FunctionScopedInstructions(ValidationState_t& _,
  57. const Instruction* inst, SpvOp opcode) {
  58. if (_.IsOpcodeInCurrentLayoutSection(opcode)) {
  59. switch (opcode) {
  60. case SpvOpFunction: {
  61. if (_.in_function_body()) {
  62. return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
  63. << "Cannot declare a function in a function body";
  64. }
  65. auto control_mask = inst->GetOperandAs<SpvFunctionControlMask>(2);
  66. if (auto error =
  67. _.RegisterFunction(inst->id(), inst->type_id(), control_mask,
  68. inst->GetOperandAs<uint32_t>(3)))
  69. return error;
  70. if (_.current_layout_section() == kLayoutFunctionDefinitions) {
  71. if (auto error = _.current_function().RegisterSetFunctionDeclType(
  72. FunctionDecl::kFunctionDeclDefinition))
  73. return error;
  74. }
  75. } break;
  76. case SpvOpFunctionParameter:
  77. if (_.in_function_body() == false) {
  78. return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
  79. << "Function parameter instructions must be in a "
  80. "function body";
  81. }
  82. if (_.current_function().block_count() != 0) {
  83. return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
  84. << "Function parameters must only appear immediately after "
  85. "the function definition";
  86. }
  87. if (auto error = _.current_function().RegisterFunctionParameter(
  88. inst->id(), inst->type_id()))
  89. return error;
  90. break;
  91. case SpvOpFunctionEnd:
  92. if (_.in_function_body() == false) {
  93. return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
  94. << "Function end instructions must be in a function body";
  95. }
  96. if (_.in_block()) {
  97. return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
  98. << "Function end cannot be called in blocks";
  99. }
  100. if (_.current_function().block_count() == 0 &&
  101. _.current_layout_section() == kLayoutFunctionDefinitions) {
  102. return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
  103. << "Function declarations must appear before "
  104. "function definitions.";
  105. }
  106. if (_.current_layout_section() == kLayoutFunctionDeclarations) {
  107. if (auto error = _.current_function().RegisterSetFunctionDeclType(
  108. FunctionDecl::kFunctionDeclDeclaration))
  109. return error;
  110. }
  111. if (auto error = _.RegisterFunctionEnd()) return error;
  112. break;
  113. case SpvOpLine:
  114. case SpvOpNoLine:
  115. break;
  116. case SpvOpLabel:
  117. // If the label is encountered then the current function is a
  118. // definition so set the function to a declaration and update the
  119. // module section
  120. if (_.in_function_body() == false) {
  121. return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
  122. << "Label instructions must be in a function body";
  123. }
  124. if (_.in_block()) {
  125. return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
  126. << "A block must end with a branch instruction.";
  127. }
  128. if (_.current_layout_section() == kLayoutFunctionDeclarations) {
  129. _.ProgressToNextLayoutSectionOrder();
  130. if (auto error = _.current_function().RegisterSetFunctionDeclType(
  131. FunctionDecl::kFunctionDeclDefinition))
  132. return error;
  133. }
  134. break;
  135. default:
  136. if (_.current_layout_section() == kLayoutFunctionDeclarations &&
  137. _.in_function_body()) {
  138. return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
  139. << "A function must begin with a label";
  140. } else {
  141. if (_.in_block() == false) {
  142. return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
  143. << spvOpcodeString(opcode) << " must appear in a block";
  144. }
  145. }
  146. break;
  147. }
  148. } else {
  149. return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
  150. << spvOpcodeString(opcode)
  151. << " cannot appear in a function declaration";
  152. }
  153. return SPV_SUCCESS;
  154. }
  155. } // namespace
  156. // TODO(umar): Check linkage capabilities for function declarations
  157. // TODO(umar): Better error messages
  158. // NOTE: This function does not handle CFG related validation
  159. // Performs logical layout validation. See Section 2.4
  160. spv_result_t ModuleLayoutPass(ValidationState_t& _, const Instruction* inst) {
  161. const SpvOp opcode = inst->opcode();
  162. switch (_.current_layout_section()) {
  163. case kLayoutCapabilities:
  164. case kLayoutExtensions:
  165. case kLayoutExtInstImport:
  166. case kLayoutMemoryModel:
  167. case kLayoutEntryPoint:
  168. case kLayoutExecutionMode:
  169. case kLayoutDebug1:
  170. case kLayoutDebug2:
  171. case kLayoutDebug3:
  172. case kLayoutAnnotations:
  173. case kLayoutTypes:
  174. if (auto error = ModuleScopedInstructions(_, inst, opcode)) return error;
  175. break;
  176. case kLayoutFunctionDeclarations:
  177. case kLayoutFunctionDefinitions:
  178. if (auto error = FunctionScopedInstructions(_, inst, opcode)) {
  179. return error;
  180. }
  181. break;
  182. }
  183. return SPV_SUCCESS;
  184. }
  185. } // namespace val
  186. } // namespace spvtools