ir_loader.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. module()->SetContainsDebugInfo();
  38. last_line_inst_.reset();
  39. dbg_line_info_.push_back(
  40. Instruction(module()->context(), *inst, last_dbg_scope_));
  41. return true;
  42. }
  43. // If it is a DebugScope or DebugNoScope of debug extension, we do not
  44. // create a new instruction, but simply keep the information in
  45. // struct DebugScope.
  46. if (opcode == SpvOpExtInst && spvExtInstIsDebugInfo(inst->ext_inst_type)) {
  47. const uint32_t ext_inst_index = inst->words[kExtInstSetIndex];
  48. if (inst->ext_inst_type == SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100 ||
  49. inst->ext_inst_type ==
  50. SPV_EXT_INST_TYPE_NONSEMANTIC_VULKAN_DEBUGINFO_100) {
  51. const CommonDebugInfoInstructions ext_inst_key =
  52. CommonDebugInfoInstructions(ext_inst_index);
  53. if (ext_inst_key == CommonDebugInfoDebugScope) {
  54. uint32_t inlined_at = 0;
  55. if (inst->num_words > kInlinedAtIndex)
  56. inlined_at = inst->words[kInlinedAtIndex];
  57. last_dbg_scope_ =
  58. DebugScope(inst->words[kLexicalScopeIndex], inlined_at);
  59. module()->SetContainsDebugInfo();
  60. return true;
  61. }
  62. if (ext_inst_key == CommonDebugInfoDebugNoScope) {
  63. last_dbg_scope_ = DebugScope(kNoDebugScope, kNoInlinedAt);
  64. module()->SetContainsDebugInfo();
  65. return true;
  66. }
  67. } else {
  68. const DebugInfoInstructions ext_inst_key =
  69. DebugInfoInstructions(ext_inst_index);
  70. if (ext_inst_key == DebugInfoDebugScope) {
  71. uint32_t inlined_at = 0;
  72. if (inst->num_words > kInlinedAtIndex)
  73. inlined_at = inst->words[kInlinedAtIndex];
  74. last_dbg_scope_ =
  75. DebugScope(inst->words[kLexicalScopeIndex], inlined_at);
  76. module()->SetContainsDebugInfo();
  77. return true;
  78. }
  79. if (ext_inst_key == DebugInfoDebugNoScope) {
  80. last_dbg_scope_ = DebugScope(kNoDebugScope, kNoInlinedAt);
  81. module()->SetContainsDebugInfo();
  82. return true;
  83. }
  84. }
  85. }
  86. std::unique_ptr<Instruction> spv_inst(
  87. new Instruction(module()->context(), *inst, std::move(dbg_line_info_)));
  88. if (!spv_inst->dbg_line_insts().empty()) {
  89. if (extra_line_tracking_ &&
  90. (spv_inst->dbg_line_insts().back().opcode() != SpvOpNoLine)) {
  91. last_line_inst_ = std::unique_ptr<Instruction>(
  92. spv_inst->dbg_line_insts().back().Clone(module()->context()));
  93. }
  94. dbg_line_info_.clear();
  95. } else if (last_line_inst_ != nullptr) {
  96. last_line_inst_->SetDebugScope(last_dbg_scope_);
  97. spv_inst->dbg_line_insts().push_back(*last_line_inst_);
  98. }
  99. const char* src = source_.c_str();
  100. spv_position_t loc = {inst_index_, 0, 0};
  101. // Handle function and basic block boundaries first, then normal
  102. // instructions.
  103. if (opcode == SpvOpFunction) {
  104. if (function_ != nullptr) {
  105. Error(consumer_, src, loc, "function inside function");
  106. return false;
  107. }
  108. function_ = MakeUnique<Function>(std::move(spv_inst));
  109. } else if (opcode == SpvOpFunctionEnd) {
  110. if (function_ == nullptr) {
  111. Error(consumer_, src, loc,
  112. "OpFunctionEnd without corresponding OpFunction");
  113. return false;
  114. }
  115. if (block_ != nullptr) {
  116. Error(consumer_, src, loc, "OpFunctionEnd inside basic block");
  117. return false;
  118. }
  119. function_->SetFunctionEnd(std::move(spv_inst));
  120. module_->AddFunction(std::move(function_));
  121. function_ = nullptr;
  122. } else if (opcode == SpvOpLabel) {
  123. if (function_ == nullptr) {
  124. Error(consumer_, src, loc, "OpLabel outside function");
  125. return false;
  126. }
  127. if (block_ != nullptr) {
  128. Error(consumer_, src, loc, "OpLabel inside basic block");
  129. return false;
  130. }
  131. block_ = MakeUnique<BasicBlock>(std::move(spv_inst));
  132. } else if (spvOpcodeIsBlockTerminator(opcode)) {
  133. if (function_ == nullptr) {
  134. Error(consumer_, src, loc, "terminator instruction outside function");
  135. return false;
  136. }
  137. if (block_ == nullptr) {
  138. Error(consumer_, src, loc, "terminator instruction outside basic block");
  139. return false;
  140. }
  141. if (last_dbg_scope_.GetLexicalScope() != kNoDebugScope)
  142. spv_inst->SetDebugScope(last_dbg_scope_);
  143. block_->AddInstruction(std::move(spv_inst));
  144. function_->AddBasicBlock(std::move(block_));
  145. block_ = nullptr;
  146. last_dbg_scope_ = DebugScope(kNoDebugScope, kNoInlinedAt);
  147. last_line_inst_.reset();
  148. dbg_line_info_.clear();
  149. } else {
  150. if (function_ == nullptr) { // Outside function definition
  151. SPIRV_ASSERT(consumer_, block_ == nullptr);
  152. if (opcode == SpvOpCapability) {
  153. module_->AddCapability(std::move(spv_inst));
  154. } else if (opcode == SpvOpExtension) {
  155. module_->AddExtension(std::move(spv_inst));
  156. } else if (opcode == SpvOpExtInstImport) {
  157. module_->AddExtInstImport(std::move(spv_inst));
  158. } else if (opcode == SpvOpMemoryModel) {
  159. module_->SetMemoryModel(std::move(spv_inst));
  160. } else if (opcode == SpvOpEntryPoint) {
  161. module_->AddEntryPoint(std::move(spv_inst));
  162. } else if (opcode == SpvOpExecutionMode) {
  163. module_->AddExecutionMode(std::move(spv_inst));
  164. } else if (IsDebug1Inst(opcode)) {
  165. module_->AddDebug1Inst(std::move(spv_inst));
  166. } else if (IsDebug2Inst(opcode)) {
  167. module_->AddDebug2Inst(std::move(spv_inst));
  168. } else if (IsDebug3Inst(opcode)) {
  169. module_->AddDebug3Inst(std::move(spv_inst));
  170. } else if (IsAnnotationInst(opcode)) {
  171. module_->AddAnnotationInst(std::move(spv_inst));
  172. } else if (IsTypeInst(opcode)) {
  173. module_->AddType(std::move(spv_inst));
  174. } else if (IsConstantInst(opcode) || opcode == SpvOpVariable ||
  175. opcode == SpvOpUndef) {
  176. module_->AddGlobalValue(std::move(spv_inst));
  177. } else if (opcode == SpvOpExtInst &&
  178. spvExtInstIsDebugInfo(inst->ext_inst_type)) {
  179. module_->AddExtInstDebugInfo(std::move(spv_inst));
  180. } else if (opcode == SpvOpExtInst &&
  181. spvExtInstIsNonSemantic(inst->ext_inst_type)) {
  182. // If there are no functions, add the non-semantic instructions to the
  183. // global values. Otherwise append it to the list of the last function.
  184. auto func_begin = module_->begin();
  185. auto func_end = module_->end();
  186. if (func_begin == func_end) {
  187. module_->AddGlobalValue(std::move(spv_inst));
  188. } else {
  189. (--func_end)->AddNonSemanticInstruction(std::move(spv_inst));
  190. }
  191. } else {
  192. Errorf(consumer_, src, loc,
  193. "Unhandled inst type (opcode: %d) found outside function "
  194. "definition.",
  195. opcode);
  196. return false;
  197. }
  198. } else {
  199. if (opcode == SpvOpLoopMerge || opcode == SpvOpSelectionMerge)
  200. last_dbg_scope_ = DebugScope(kNoDebugScope, kNoInlinedAt);
  201. if (last_dbg_scope_.GetLexicalScope() != kNoDebugScope)
  202. spv_inst->SetDebugScope(last_dbg_scope_);
  203. if (opcode == SpvOpExtInst &&
  204. spvExtInstIsDebugInfo(inst->ext_inst_type)) {
  205. const uint32_t ext_inst_index = inst->words[kExtInstSetIndex];
  206. if (inst->ext_inst_type == SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100) {
  207. const OpenCLDebugInfo100Instructions ext_inst_key =
  208. OpenCLDebugInfo100Instructions(ext_inst_index);
  209. switch (ext_inst_key) {
  210. case OpenCLDebugInfo100DebugDeclare: {
  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 OpenCLDebugInfo100DebugValue: {
  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. } else {
  234. const DebugInfoInstructions ext_inst_key =
  235. DebugInfoInstructions(ext_inst_index);
  236. switch (ext_inst_key) {
  237. case DebugInfoDebugDeclare: {
  238. if (block_ == nullptr) // Inside function but outside blocks
  239. function_->AddDebugInstructionInHeader(std::move(spv_inst));
  240. else
  241. block_->AddInstruction(std::move(spv_inst));
  242. break;
  243. }
  244. case DebugInfoDebugValue: {
  245. if (block_ == nullptr) // Inside function but outside blocks
  246. function_->AddDebugInstructionInHeader(std::move(spv_inst));
  247. else
  248. block_->AddInstruction(std::move(spv_inst));
  249. break;
  250. }
  251. default: {
  252. Errorf(consumer_, src, loc,
  253. "Debug info extension instruction other than DebugScope, "
  254. "DebugNoScope, DebugDeclare, and DebugValue found inside "
  255. "function",
  256. opcode);
  257. return false;
  258. }
  259. }
  260. }
  261. } else {
  262. if (block_ == nullptr) { // Inside function but outside blocks
  263. if (opcode != SpvOpFunctionParameter) {
  264. Errorf(consumer_, src, loc,
  265. "Non-OpFunctionParameter (opcode: %d) found inside "
  266. "function but outside basic block",
  267. opcode);
  268. return false;
  269. }
  270. function_->AddParameter(std::move(spv_inst));
  271. } else {
  272. block_->AddInstruction(std::move(spv_inst));
  273. }
  274. }
  275. }
  276. }
  277. return true;
  278. }
  279. // Resolves internal references among the module, functions, basic blocks, etc.
  280. // This function should be called after adding all instructions.
  281. void IrLoader::EndModule() {
  282. if (block_ && function_) {
  283. // We're in the middle of a basic block, but the terminator is missing.
  284. // Register the block anyway. This lets us write tests with less
  285. // boilerplate.
  286. function_->AddBasicBlock(std::move(block_));
  287. block_ = nullptr;
  288. }
  289. if (function_) {
  290. // We're in the middle of a function, but the OpFunctionEnd is missing.
  291. // Register the function anyway. This lets us write tests with less
  292. // boilerplate.
  293. module_->AddFunction(std::move(function_));
  294. function_ = nullptr;
  295. }
  296. for (auto& function : *module_) {
  297. for (auto& bb : function) bb.SetParent(&function);
  298. }
  299. // Copy any trailing Op*Line instruction into the module
  300. module_->SetTrailingDbgLineInfo(std::move(dbg_line_info_));
  301. }
  302. } // namespace opt
  303. } // namespace spvtools