ir_loader.cpp 15 KB

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