ir_loader.cpp 13 KB

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