module.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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(annotations_);
  86. DELEGATE(types_values_);
  87. for (auto& i : functions_) i->ForEachInst(f, run_on_debug_line_insts);
  88. #undef DELEGATE
  89. }
  90. void Module::ForEachInst(const std::function<void(const Instruction*)>& f,
  91. bool run_on_debug_line_insts) const {
  92. #define DELEGATE(i) i.ForEachInst(f, run_on_debug_line_insts)
  93. for (auto& i : capabilities_) DELEGATE(i);
  94. for (auto& i : extensions_) DELEGATE(i);
  95. for (auto& i : ext_inst_imports_) DELEGATE(i);
  96. if (memory_model_)
  97. static_cast<const Instruction*>(memory_model_.get())
  98. ->ForEachInst(f, run_on_debug_line_insts);
  99. for (auto& i : entry_points_) DELEGATE(i);
  100. for (auto& i : execution_modes_) DELEGATE(i);
  101. for (auto& i : debugs1_) DELEGATE(i);
  102. for (auto& i : debugs2_) DELEGATE(i);
  103. for (auto& i : debugs3_) DELEGATE(i);
  104. for (auto& i : annotations_) DELEGATE(i);
  105. for (auto& i : types_values_) DELEGATE(i);
  106. for (auto& i : functions_) {
  107. static_cast<const Function*>(i.get())->ForEachInst(f,
  108. run_on_debug_line_insts);
  109. }
  110. if (run_on_debug_line_insts) {
  111. for (auto& i : trailing_dbg_line_info_) DELEGATE(i);
  112. }
  113. #undef DELEGATE
  114. }
  115. void Module::ToBinary(std::vector<uint32_t>* binary, bool skip_nop) const {
  116. binary->push_back(header_.magic_number);
  117. binary->push_back(header_.version);
  118. // TODO(antiagainst): should we change the generator number?
  119. binary->push_back(header_.generator);
  120. binary->push_back(header_.bound);
  121. binary->push_back(header_.reserved);
  122. auto write_inst = [binary, skip_nop](const Instruction* i) {
  123. if (!(skip_nop && i->IsNop())) i->ToBinaryWithoutAttachedDebugInsts(binary);
  124. };
  125. ForEachInst(write_inst, true);
  126. }
  127. uint32_t Module::ComputeIdBound() const {
  128. uint32_t highest = 0;
  129. ForEachInst(
  130. [&highest](const Instruction* inst) {
  131. for (const auto& operand : *inst) {
  132. if (spvIsIdType(operand.type)) {
  133. highest = std::max(highest, operand.words[0]);
  134. }
  135. }
  136. },
  137. true /* scan debug line insts as well */);
  138. return highest + 1;
  139. }
  140. bool Module::HasExplicitCapability(uint32_t cap) {
  141. for (auto& ci : capabilities_) {
  142. uint32_t tcap = ci.GetSingleWordOperand(0);
  143. if (tcap == cap) {
  144. return true;
  145. }
  146. }
  147. return false;
  148. }
  149. uint32_t Module::GetExtInstImportId(const char* extstr) {
  150. for (auto& ei : ext_inst_imports_)
  151. if (!strcmp(extstr,
  152. reinterpret_cast<const char*>(&(ei.GetInOperand(0).words[0]))))
  153. return ei.result_id();
  154. return 0;
  155. }
  156. std::ostream& operator<<(std::ostream& str, const Module& module) {
  157. module.ForEachInst([&str](const Instruction* inst) {
  158. str << *inst;
  159. if (inst->opcode() != SpvOpFunctionEnd) {
  160. str << std::endl;
  161. }
  162. });
  163. return str;
  164. }
  165. } // namespace opt
  166. } // namespace spvtools