ir_loader.cpp 12 KB

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