module.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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/module.h"
  15. #include <algorithm>
  16. #include <cstring>
  17. #include <ostream>
  18. #include "source/operand.h"
  19. #include "source/opt/ir_context.h"
  20. #include "source/opt/reflect.h"
  21. namespace spvtools {
  22. namespace opt {
  23. uint32_t Module::TakeNextIdBound() {
  24. if (context()) {
  25. if (id_bound() >= context()->max_id_bound()) {
  26. return 0;
  27. }
  28. } else if (id_bound() >= kDefaultMaxIdBound) {
  29. return 0;
  30. }
  31. return header_.bound++;
  32. }
  33. std::vector<Instruction*> Module::GetTypes() {
  34. std::vector<Instruction*> type_insts;
  35. for (auto& inst : types_values_) {
  36. if (IsTypeInst(inst.opcode())) type_insts.push_back(&inst);
  37. }
  38. return type_insts;
  39. }
  40. std::vector<const Instruction*> Module::GetTypes() const {
  41. std::vector<const Instruction*> type_insts;
  42. for (auto& inst : types_values_) {
  43. if (IsTypeInst(inst.opcode())) type_insts.push_back(&inst);
  44. }
  45. return type_insts;
  46. }
  47. std::vector<Instruction*> Module::GetConstants() {
  48. std::vector<Instruction*> const_insts;
  49. for (auto& inst : types_values_) {
  50. if (IsConstantInst(inst.opcode())) const_insts.push_back(&inst);
  51. }
  52. return const_insts;
  53. }
  54. std::vector<const Instruction*> Module::GetConstants() const {
  55. std::vector<const Instruction*> const_insts;
  56. for (auto& inst : types_values_) {
  57. if (IsConstantInst(inst.opcode())) const_insts.push_back(&inst);
  58. }
  59. return const_insts;
  60. }
  61. uint32_t Module::GetGlobalValue(SpvOp opcode) const {
  62. for (auto& inst : types_values_) {
  63. if (inst.opcode() == opcode) return inst.result_id();
  64. }
  65. return 0;
  66. }
  67. void Module::AddGlobalValue(SpvOp opcode, uint32_t result_id,
  68. uint32_t type_id) {
  69. std::unique_ptr<Instruction> newGlobal(
  70. new Instruction(context(), opcode, type_id, result_id, {}));
  71. AddGlobalValue(std::move(newGlobal));
  72. }
  73. void Module::ForEachInst(const std::function<void(Instruction*)>& f,
  74. bool run_on_debug_line_insts) {
  75. #define DELEGATE(list) list.ForEachInst(f, run_on_debug_line_insts)
  76. DELEGATE(capabilities_);
  77. DELEGATE(extensions_);
  78. DELEGATE(ext_inst_imports_);
  79. if (memory_model_) memory_model_->ForEachInst(f, run_on_debug_line_insts);
  80. DELEGATE(entry_points_);
  81. DELEGATE(execution_modes_);
  82. DELEGATE(debugs1_);
  83. DELEGATE(debugs2_);
  84. DELEGATE(debugs3_);
  85. DELEGATE(ext_inst_debuginfo_);
  86. DELEGATE(annotations_);
  87. DELEGATE(types_values_);
  88. for (auto& i : functions_) {
  89. i->ForEachInst(f, run_on_debug_line_insts,
  90. /* run_on_non_semantic_insts = */ true);
  91. }
  92. #undef DELEGATE
  93. }
  94. void Module::ForEachInst(const std::function<void(const Instruction*)>& f,
  95. bool run_on_debug_line_insts) const {
  96. #define DELEGATE(i) i.ForEachInst(f, run_on_debug_line_insts)
  97. for (auto& i : capabilities_) DELEGATE(i);
  98. for (auto& i : extensions_) DELEGATE(i);
  99. for (auto& i : ext_inst_imports_) DELEGATE(i);
  100. if (memory_model_)
  101. static_cast<const Instruction*>(memory_model_.get())
  102. ->ForEachInst(f, run_on_debug_line_insts);
  103. for (auto& i : entry_points_) DELEGATE(i);
  104. for (auto& i : execution_modes_) DELEGATE(i);
  105. for (auto& i : debugs1_) DELEGATE(i);
  106. for (auto& i : debugs2_) DELEGATE(i);
  107. for (auto& i : debugs3_) DELEGATE(i);
  108. for (auto& i : annotations_) DELEGATE(i);
  109. for (auto& i : types_values_) DELEGATE(i);
  110. for (auto& i : ext_inst_debuginfo_) DELEGATE(i);
  111. for (auto& i : functions_) {
  112. static_cast<const Function*>(i.get())->ForEachInst(
  113. f, run_on_debug_line_insts,
  114. /* run_on_non_semantic_insts = */ true);
  115. }
  116. if (run_on_debug_line_insts) {
  117. for (auto& i : trailing_dbg_line_info_) DELEGATE(i);
  118. }
  119. #undef DELEGATE
  120. }
  121. void Module::ToBinary(std::vector<uint32_t>* binary, bool skip_nop) const {
  122. binary->push_back(header_.magic_number);
  123. binary->push_back(header_.version);
  124. // TODO(antiagainst): should we change the generator number?
  125. binary->push_back(header_.generator);
  126. binary->push_back(header_.bound);
  127. binary->push_back(header_.reserved);
  128. size_t bound_idx = binary->size() - 2;
  129. DebugScope last_scope(kNoDebugScope, kNoInlinedAt);
  130. const Instruction* last_line_inst = nullptr;
  131. bool between_merge_and_branch = false;
  132. auto write_inst = [binary, skip_nop, &last_scope, &last_line_inst,
  133. &between_merge_and_branch, this](const Instruction* i) {
  134. // Skip emitting line instructions between merge and branch instructions.
  135. auto opcode = i->opcode();
  136. if (between_merge_and_branch &&
  137. (opcode == SpvOpLine || opcode == SpvOpNoLine)) {
  138. return;
  139. }
  140. between_merge_and_branch = false;
  141. if (last_line_inst != nullptr) {
  142. // If the current instruction is OpLine and it is the same with
  143. // the last line instruction that is still effective (can be applied
  144. // to the next instruction), we skip writing the current instruction.
  145. if (opcode == SpvOpLine) {
  146. uint32_t operand_index = 0;
  147. if (last_line_inst->WhileEachInOperand(
  148. [&operand_index, i](const uint32_t* word) {
  149. assert(i->NumInOperandWords() > operand_index);
  150. return *word == i->GetSingleWordInOperand(operand_index++);
  151. })) {
  152. return;
  153. }
  154. } else if (opcode != SpvOpNoLine && i->dbg_line_insts().empty()) {
  155. // If the current instruction does not have the line information,
  156. // the last line information is not effective any more. Emit OpNoLine
  157. // to specify it.
  158. binary->push_back((1 << 16) | static_cast<uint16_t>(SpvOpNoLine));
  159. last_line_inst = nullptr;
  160. }
  161. }
  162. if (!(skip_nop && i->IsNop())) {
  163. const auto& scope = i->GetDebugScope();
  164. if (scope != last_scope) {
  165. // Emit DebugScope |scope| to |binary|.
  166. auto dbg_inst = ext_inst_debuginfo_.begin();
  167. scope.ToBinary(dbg_inst->type_id(), context()->TakeNextId(),
  168. dbg_inst->GetSingleWordOperand(2), binary);
  169. last_scope = scope;
  170. }
  171. i->ToBinaryWithoutAttachedDebugInsts(binary);
  172. }
  173. // Update the last line instruction.
  174. if (IsTerminatorInst(opcode) || opcode == SpvOpNoLine) {
  175. last_line_inst = nullptr;
  176. } else if (opcode == SpvOpLoopMerge || opcode == SpvOpSelectionMerge) {
  177. between_merge_and_branch = true;
  178. last_line_inst = nullptr;
  179. } else if (opcode == SpvOpLine) {
  180. last_line_inst = i;
  181. }
  182. };
  183. ForEachInst(write_inst, true);
  184. // We create new instructions for DebugScope. The bound must be updated.
  185. binary->data()[bound_idx] = header_.bound;
  186. }
  187. uint32_t Module::ComputeIdBound() const {
  188. uint32_t highest = 0;
  189. ForEachInst(
  190. [&highest](const Instruction* inst) {
  191. for (const auto& operand : *inst) {
  192. if (spvIsIdType(operand.type)) {
  193. highest = std::max(highest, operand.words[0]);
  194. }
  195. }
  196. },
  197. true /* scan debug line insts as well */);
  198. return highest + 1;
  199. }
  200. bool Module::HasExplicitCapability(uint32_t cap) {
  201. for (auto& ci : capabilities_) {
  202. uint32_t tcap = ci.GetSingleWordOperand(0);
  203. if (tcap == cap) {
  204. return true;
  205. }
  206. }
  207. return false;
  208. }
  209. uint32_t Module::GetExtInstImportId(const char* extstr) {
  210. for (auto& ei : ext_inst_imports_)
  211. if (!strcmp(extstr,
  212. reinterpret_cast<const char*>(&(ei.GetInOperand(0).words[0]))))
  213. return ei.result_id();
  214. return 0;
  215. }
  216. std::ostream& operator<<(std::ostream& str, const Module& module) {
  217. module.ForEachInst([&str](const Instruction* inst) {
  218. str << *inst;
  219. if (inst->opcode() != SpvOpFunctionEnd) {
  220. str << std::endl;
  221. }
  222. });
  223. return str;
  224. }
  225. } // namespace opt
  226. } // namespace spvtools