ir_loader.cpp 12 KB

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