ir_loader.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 "DebugInfo.h"
  17. #include "OpenCLDebugInfo100.h"
  18. #include "source/ext_inst.h"
  19. #include "source/opt/log.h"
  20. #include "source/opt/reflect.h"
  21. #include "source/util/make_unique.h"
  22. static const uint32_t kExtInstSetIndex = 4;
  23. static const uint32_t kLexicalScopeIndex = 5;
  24. static const uint32_t kInlinedAtIndex = 6;
  25. namespace spvtools {
  26. namespace opt {
  27. IrLoader::IrLoader(const MessageConsumer& consumer, Module* m)
  28. : consumer_(consumer),
  29. module_(m),
  30. source_("<instruction>"),
  31. inst_index_(0),
  32. last_dbg_scope_(kNoDebugScope, kNoInlinedAt) {}
  33. bool IrLoader::AddInstruction(const spv_parsed_instruction_t* inst) {
  34. ++inst_index_;
  35. const auto opcode = static_cast<SpvOp>(inst->opcode);
  36. if (IsDebugLineInst(opcode)) {
  37. dbg_line_info_.push_back(
  38. Instruction(module()->context(), *inst, last_dbg_scope_));
  39. return true;
  40. }
  41. // If it is a DebugScope or DebugNoScope of debug extension, we do not
  42. // create a new instruction, but simply keep the information in
  43. // struct DebugScope.
  44. if (opcode == SpvOpExtInst && spvExtInstIsDebugInfo(inst->ext_inst_type)) {
  45. const uint32_t ext_inst_index = inst->words[kExtInstSetIndex];
  46. if (inst->ext_inst_type == SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100) {
  47. const OpenCLDebugInfo100Instructions ext_inst_key =
  48. OpenCLDebugInfo100Instructions(ext_inst_index);
  49. if (ext_inst_key == OpenCLDebugInfo100DebugScope) {
  50. uint32_t inlined_at = 0;
  51. if (inst->num_words > kInlinedAtIndex)
  52. inlined_at = inst->words[kInlinedAtIndex];
  53. last_dbg_scope_ =
  54. DebugScope(inst->words[kLexicalScopeIndex], inlined_at);
  55. module()->SetContainsDebugScope();
  56. return true;
  57. }
  58. if (ext_inst_key == OpenCLDebugInfo100DebugNoScope) {
  59. last_dbg_scope_ = DebugScope(kNoDebugScope, kNoInlinedAt);
  60. module()->SetContainsDebugScope();
  61. return true;
  62. }
  63. } else {
  64. const DebugInfoInstructions ext_inst_key =
  65. DebugInfoInstructions(ext_inst_index);
  66. if (ext_inst_key == DebugInfoDebugScope) {
  67. uint32_t inlined_at = 0;
  68. if (inst->num_words > kInlinedAtIndex)
  69. inlined_at = inst->words[kInlinedAtIndex];
  70. last_dbg_scope_ =
  71. DebugScope(inst->words[kLexicalScopeIndex], inlined_at);
  72. module()->SetContainsDebugScope();
  73. return true;
  74. }
  75. if (ext_inst_key == DebugInfoDebugNoScope) {
  76. last_dbg_scope_ = DebugScope(kNoDebugScope, kNoInlinedAt);
  77. module()->SetContainsDebugScope();
  78. return true;
  79. }
  80. }
  81. }
  82. std::unique_ptr<Instruction> spv_inst(
  83. new Instruction(module()->context(), *inst, std::move(dbg_line_info_)));
  84. dbg_line_info_.clear();
  85. const char* src = source_.c_str();
  86. spv_position_t loc = {inst_index_, 0, 0};
  87. // Handle function and basic block boundaries first, then normal
  88. // instructions.
  89. if (opcode == SpvOpFunction) {
  90. if (function_ != nullptr) {
  91. Error(consumer_, src, loc, "function inside function");
  92. return false;
  93. }
  94. function_ = MakeUnique<Function>(std::move(spv_inst));
  95. } else if (opcode == SpvOpFunctionEnd) {
  96. if (function_ == nullptr) {
  97. Error(consumer_, src, loc,
  98. "OpFunctionEnd without corresponding OpFunction");
  99. return false;
  100. }
  101. if (block_ != nullptr) {
  102. Error(consumer_, src, loc, "OpFunctionEnd inside basic block");
  103. return false;
  104. }
  105. function_->SetFunctionEnd(std::move(spv_inst));
  106. module_->AddFunction(std::move(function_));
  107. function_ = nullptr;
  108. } else if (opcode == SpvOpLabel) {
  109. if (function_ == nullptr) {
  110. Error(consumer_, src, loc, "OpLabel outside function");
  111. return false;
  112. }
  113. if (block_ != nullptr) {
  114. Error(consumer_, src, loc, "OpLabel inside basic block");
  115. return false;
  116. }
  117. block_ = MakeUnique<BasicBlock>(std::move(spv_inst));
  118. } else if (IsTerminatorInst(opcode)) {
  119. if (function_ == nullptr) {
  120. Error(consumer_, src, loc, "terminator instruction outside function");
  121. return false;
  122. }
  123. if (block_ == nullptr) {
  124. Error(consumer_, src, loc, "terminator instruction outside basic block");
  125. return false;
  126. }
  127. block_->AddInstruction(std::move(spv_inst));
  128. function_->AddBasicBlock(std::move(block_));
  129. block_ = nullptr;
  130. last_dbg_scope_ = DebugScope(kNoDebugScope, kNoInlinedAt);
  131. } else {
  132. if (function_ == nullptr) { // Outside function definition
  133. SPIRV_ASSERT(consumer_, block_ == nullptr);
  134. if (opcode == SpvOpCapability) {
  135. module_->AddCapability(std::move(spv_inst));
  136. } else if (opcode == SpvOpExtension) {
  137. module_->AddExtension(std::move(spv_inst));
  138. } else if (opcode == SpvOpExtInstImport) {
  139. module_->AddExtInstImport(std::move(spv_inst));
  140. } else if (opcode == SpvOpMemoryModel) {
  141. module_->SetMemoryModel(std::move(spv_inst));
  142. } else if (opcode == SpvOpEntryPoint) {
  143. module_->AddEntryPoint(std::move(spv_inst));
  144. } else if (opcode == SpvOpExecutionMode) {
  145. module_->AddExecutionMode(std::move(spv_inst));
  146. } else if (IsDebug1Inst(opcode)) {
  147. module_->AddDebug1Inst(std::move(spv_inst));
  148. } else if (IsDebug2Inst(opcode)) {
  149. module_->AddDebug2Inst(std::move(spv_inst));
  150. } else if (IsDebug3Inst(opcode)) {
  151. module_->AddDebug3Inst(std::move(spv_inst));
  152. } else if (IsAnnotationInst(opcode)) {
  153. module_->AddAnnotationInst(std::move(spv_inst));
  154. } else if (IsTypeInst(opcode)) {
  155. module_->AddType(std::move(spv_inst));
  156. } else if (IsConstantInst(opcode) || opcode == SpvOpVariable ||
  157. opcode == SpvOpUndef ||
  158. (opcode == SpvOpExtInst &&
  159. spvExtInstIsNonSemantic(inst->ext_inst_type))) {
  160. module_->AddGlobalValue(std::move(spv_inst));
  161. } else if (opcode == SpvOpExtInst &&
  162. spvExtInstIsDebugInfo(inst->ext_inst_type)) {
  163. module_->AddExtInstDebugInfo(std::move(spv_inst));
  164. } else {
  165. Errorf(consumer_, src, loc,
  166. "Unhandled inst type (opcode: %d) found outside function "
  167. "definition.",
  168. opcode);
  169. return false;
  170. }
  171. } else {
  172. if (opcode == SpvOpLoopMerge || opcode == SpvOpSelectionMerge)
  173. last_dbg_scope_ = DebugScope(kNoDebugScope, kNoInlinedAt);
  174. if (last_dbg_scope_.GetLexicalScope() != kNoDebugScope)
  175. spv_inst->SetDebugScope(last_dbg_scope_);
  176. if (opcode == SpvOpExtInst &&
  177. spvExtInstIsDebugInfo(inst->ext_inst_type)) {
  178. const uint32_t ext_inst_index = inst->words[kExtInstSetIndex];
  179. if (inst->ext_inst_type == SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100) {
  180. const OpenCLDebugInfo100Instructions ext_inst_key =
  181. OpenCLDebugInfo100Instructions(ext_inst_index);
  182. switch (ext_inst_key) {
  183. case OpenCLDebugInfo100DebugDeclare: {
  184. if (block_ == nullptr) // Inside function but outside blocks
  185. function_->AddDebugInstructionInHeader(std::move(spv_inst));
  186. else
  187. block_->AddInstruction(std::move(spv_inst));
  188. break;
  189. }
  190. case OpenCLDebugInfo100DebugValue: {
  191. if (block_ == nullptr) // Inside function but outside blocks
  192. function_->AddDebugInstructionInHeader(std::move(spv_inst));
  193. else
  194. block_->AddInstruction(std::move(spv_inst));
  195. break;
  196. }
  197. default: {
  198. Errorf(consumer_, src, loc,
  199. "Debug info extension instruction other than DebugScope, "
  200. "DebugNoScope, DebugDeclare, and DebugValue found inside "
  201. "function",
  202. opcode);
  203. return false;
  204. }
  205. }
  206. } else {
  207. const DebugInfoInstructions ext_inst_key =
  208. DebugInfoInstructions(ext_inst_index);
  209. switch (ext_inst_key) {
  210. case DebugInfoDebugDeclare: {
  211. if (block_ == nullptr) // Inside function but outside blocks
  212. function_->AddDebugInstructionInHeader(std::move(spv_inst));
  213. else
  214. block_->AddInstruction(std::move(spv_inst));
  215. break;
  216. }
  217. case DebugInfoDebugValue: {
  218. if (block_ == nullptr) // Inside function but outside blocks
  219. function_->AddDebugInstructionInHeader(std::move(spv_inst));
  220. else
  221. block_->AddInstruction(std::move(spv_inst));
  222. break;
  223. }
  224. default: {
  225. Errorf(consumer_, src, loc,
  226. "Debug info extension instruction other than DebugScope, "
  227. "DebugNoScope, DebugDeclare, and DebugValue found inside "
  228. "function",
  229. opcode);
  230. return false;
  231. }
  232. }
  233. }
  234. } else {
  235. if (block_ == nullptr) { // Inside function but outside blocks
  236. if (opcode != SpvOpFunctionParameter) {
  237. Errorf(consumer_, src, loc,
  238. "Non-OpFunctionParameter (opcode: %d) found inside "
  239. "function but outside basic block",
  240. opcode);
  241. return false;
  242. }
  243. function_->AddParameter(std::move(spv_inst));
  244. } else {
  245. block_->AddInstruction(std::move(spv_inst));
  246. }
  247. }
  248. }
  249. }
  250. return true;
  251. }
  252. // Resolves internal references among the module, functions, basic blocks, etc.
  253. // This function should be called after adding all instructions.
  254. void IrLoader::EndModule() {
  255. if (block_ && function_) {
  256. // We're in the middle of a basic block, but the terminator is missing.
  257. // Register the block anyway. This lets us write tests with less
  258. // boilerplate.
  259. function_->AddBasicBlock(std::move(block_));
  260. block_ = nullptr;
  261. }
  262. if (function_) {
  263. // We're in the middle of a function, but the OpFunctionEnd is missing.
  264. // Register the function anyway. This lets us write tests with less
  265. // boilerplate.
  266. module_->AddFunction(std::move(function_));
  267. function_ = nullptr;
  268. }
  269. for (auto& function : *module_) {
  270. for (auto& bb : function) bb.SetParent(&function);
  271. }
  272. // Copy any trailing Op*Line instruction into the module
  273. module_->SetTrailingDbgLineInfo(std::move(dbg_line_info_));
  274. }
  275. } // namespace opt
  276. } // namespace spvtools