ir_loader.cpp 14 KB

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