ir_loader.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright (c) 2016 Google 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. #include "source/opt/ir_loader.h"
  15. #include <utility>
  16. #include "source/opt/log.h"
  17. #include "source/opt/reflect.h"
  18. #include "source/util/make_unique.h"
  19. namespace spvtools {
  20. namespace opt {
  21. IrLoader::IrLoader(const MessageConsumer& consumer, Module* m)
  22. : consumer_(consumer),
  23. module_(m),
  24. source_("<instruction>"),
  25. inst_index_(0) {}
  26. bool IrLoader::AddInstruction(const spv_parsed_instruction_t* inst) {
  27. ++inst_index_;
  28. const auto opcode = static_cast<SpvOp>(inst->opcode);
  29. if (IsDebugLineInst(opcode)) {
  30. dbg_line_info_.push_back(Instruction(module()->context(), *inst));
  31. return true;
  32. }
  33. std::unique_ptr<Instruction> spv_inst(
  34. new Instruction(module()->context(), *inst, std::move(dbg_line_info_)));
  35. dbg_line_info_.clear();
  36. const char* src = source_.c_str();
  37. spv_position_t loc = {inst_index_, 0, 0};
  38. // Handle function and basic block boundaries first, then normal
  39. // instructions.
  40. if (opcode == SpvOpFunction) {
  41. if (function_ != nullptr) {
  42. Error(consumer_, src, loc, "function inside function");
  43. return false;
  44. }
  45. function_ = MakeUnique<Function>(std::move(spv_inst));
  46. } else if (opcode == SpvOpFunctionEnd) {
  47. if (function_ == nullptr) {
  48. Error(consumer_, src, loc,
  49. "OpFunctionEnd without corresponding OpFunction");
  50. return false;
  51. }
  52. if (block_ != nullptr) {
  53. Error(consumer_, src, loc, "OpFunctionEnd inside basic block");
  54. return false;
  55. }
  56. function_->SetFunctionEnd(std::move(spv_inst));
  57. module_->AddFunction(std::move(function_));
  58. function_ = nullptr;
  59. } else if (opcode == SpvOpLabel) {
  60. if (function_ == nullptr) {
  61. Error(consumer_, src, loc, "OpLabel outside function");
  62. return false;
  63. }
  64. if (block_ != nullptr) {
  65. Error(consumer_, src, loc, "OpLabel inside basic block");
  66. return false;
  67. }
  68. block_ = MakeUnique<BasicBlock>(std::move(spv_inst));
  69. } else if (IsTerminatorInst(opcode)) {
  70. if (function_ == nullptr) {
  71. Error(consumer_, src, loc, "terminator instruction outside function");
  72. return false;
  73. }
  74. if (block_ == nullptr) {
  75. Error(consumer_, src, loc, "terminator instruction outside basic block");
  76. return false;
  77. }
  78. block_->AddInstruction(std::move(spv_inst));
  79. function_->AddBasicBlock(std::move(block_));
  80. block_ = nullptr;
  81. } else {
  82. if (function_ == nullptr) { // Outside function definition
  83. SPIRV_ASSERT(consumer_, block_ == nullptr);
  84. if (opcode == SpvOpCapability) {
  85. module_->AddCapability(std::move(spv_inst));
  86. } else if (opcode == SpvOpExtension) {
  87. module_->AddExtension(std::move(spv_inst));
  88. } else if (opcode == SpvOpExtInstImport) {
  89. module_->AddExtInstImport(std::move(spv_inst));
  90. } else if (opcode == SpvOpMemoryModel) {
  91. module_->SetMemoryModel(std::move(spv_inst));
  92. } else if (opcode == SpvOpEntryPoint) {
  93. module_->AddEntryPoint(std::move(spv_inst));
  94. } else if (opcode == SpvOpExecutionMode) {
  95. module_->AddExecutionMode(std::move(spv_inst));
  96. } else if (IsDebug1Inst(opcode)) {
  97. module_->AddDebug1Inst(std::move(spv_inst));
  98. } else if (IsDebug2Inst(opcode)) {
  99. module_->AddDebug2Inst(std::move(spv_inst));
  100. } else if (IsDebug3Inst(opcode)) {
  101. module_->AddDebug3Inst(std::move(spv_inst));
  102. } else if (IsAnnotationInst(opcode)) {
  103. module_->AddAnnotationInst(std::move(spv_inst));
  104. } else if (IsTypeInst(opcode)) {
  105. module_->AddType(std::move(spv_inst));
  106. } else if (IsConstantInst(opcode) || opcode == SpvOpVariable ||
  107. opcode == SpvOpUndef) {
  108. module_->AddGlobalValue(std::move(spv_inst));
  109. } else {
  110. SPIRV_UNIMPLEMENTED(consumer_,
  111. "unhandled inst type outside function definition");
  112. }
  113. } else {
  114. if (block_ == nullptr) { // Inside function but outside blocks
  115. if (opcode != SpvOpFunctionParameter) {
  116. Errorf(consumer_, src, loc,
  117. "Non-OpFunctionParameter (opcode: %d) found inside "
  118. "function but outside basic block",
  119. opcode);
  120. return false;
  121. }
  122. function_->AddParameter(std::move(spv_inst));
  123. } else {
  124. block_->AddInstruction(std::move(spv_inst));
  125. }
  126. }
  127. }
  128. return true;
  129. }
  130. // Resolves internal references among the module, functions, basic blocks, etc.
  131. // This function should be called after adding all instructions.
  132. void IrLoader::EndModule() {
  133. if (block_ && function_) {
  134. // We're in the middle of a basic block, but the terminator is missing.
  135. // Register the block anyway. This lets us write tests with less
  136. // boilerplate.
  137. function_->AddBasicBlock(std::move(block_));
  138. block_ = nullptr;
  139. }
  140. if (function_) {
  141. // We're in the middle of a function, but the OpFunctionEnd is missing.
  142. // Register the function anyway. This lets us write tests with less
  143. // boilerplate.
  144. module_->AddFunction(std::move(function_));
  145. function_ = nullptr;
  146. }
  147. for (auto& function : *module_) {
  148. for (auto& bb : function) bb.SetParent(&function);
  149. }
  150. }
  151. } // namespace opt
  152. } // namespace spvtools