module.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. auto write_inst = [binary, skip_nop, &last_scope,
  131. this](const Instruction* i) {
  132. if (!(skip_nop && i->IsNop())) {
  133. const auto& scope = i->GetDebugScope();
  134. if (scope != last_scope) {
  135. // Emit DebugScope |scope| to |binary|.
  136. auto dbg_inst = ext_inst_debuginfo_.begin();
  137. scope.ToBinary(dbg_inst->type_id(), context()->TakeNextId(),
  138. dbg_inst->GetSingleWordOperand(2), binary);
  139. last_scope = scope;
  140. }
  141. i->ToBinaryWithoutAttachedDebugInsts(binary);
  142. }
  143. };
  144. ForEachInst(write_inst, true);
  145. // We create new instructions for DebugScope. The bound must be updated.
  146. binary->data()[bound_idx] = header_.bound;
  147. }
  148. uint32_t Module::ComputeIdBound() const {
  149. uint32_t highest = 0;
  150. ForEachInst(
  151. [&highest](const Instruction* inst) {
  152. for (const auto& operand : *inst) {
  153. if (spvIsIdType(operand.type)) {
  154. highest = std::max(highest, operand.words[0]);
  155. }
  156. }
  157. },
  158. true /* scan debug line insts as well */);
  159. return highest + 1;
  160. }
  161. bool Module::HasExplicitCapability(uint32_t cap) {
  162. for (auto& ci : capabilities_) {
  163. uint32_t tcap = ci.GetSingleWordOperand(0);
  164. if (tcap == cap) {
  165. return true;
  166. }
  167. }
  168. return false;
  169. }
  170. uint32_t Module::GetExtInstImportId(const char* extstr) {
  171. for (auto& ei : ext_inst_imports_)
  172. if (!strcmp(extstr,
  173. reinterpret_cast<const char*>(&(ei.GetInOperand(0).words[0]))))
  174. return ei.result_id();
  175. return 0;
  176. }
  177. std::ostream& operator<<(std::ostream& str, const Module& module) {
  178. module.ForEachInst([&str](const Instruction* inst) {
  179. str << *inst;
  180. if (inst->opcode() != SpvOpFunctionEnd) {
  181. str << std::endl;
  182. }
  183. });
  184. return str;
  185. }
  186. } // namespace opt
  187. } // namespace spvtools