ir_loader.cpp 11 KB

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