module.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/reflect.h"
  20. namespace spvtools {
  21. namespace opt {
  22. std::vector<Instruction*> Module::GetTypes() {
  23. std::vector<Instruction*> type_insts;
  24. for (auto& inst : types_values_) {
  25. if (IsTypeInst(inst.opcode())) type_insts.push_back(&inst);
  26. }
  27. return type_insts;
  28. }
  29. std::vector<const Instruction*> Module::GetTypes() const {
  30. std::vector<const Instruction*> type_insts;
  31. for (auto& inst : types_values_) {
  32. if (IsTypeInst(inst.opcode())) type_insts.push_back(&inst);
  33. }
  34. return type_insts;
  35. }
  36. std::vector<Instruction*> Module::GetConstants() {
  37. std::vector<Instruction*> const_insts;
  38. for (auto& inst : types_values_) {
  39. if (IsConstantInst(inst.opcode())) const_insts.push_back(&inst);
  40. }
  41. return const_insts;
  42. }
  43. std::vector<const Instruction*> Module::GetConstants() const {
  44. std::vector<const Instruction*> const_insts;
  45. for (auto& inst : types_values_) {
  46. if (IsConstantInst(inst.opcode())) const_insts.push_back(&inst);
  47. }
  48. return const_insts;
  49. }
  50. uint32_t Module::GetGlobalValue(SpvOp opcode) const {
  51. for (auto& inst : types_values_) {
  52. if (inst.opcode() == opcode) return inst.result_id();
  53. }
  54. return 0;
  55. }
  56. void Module::AddGlobalValue(SpvOp opcode, uint32_t result_id,
  57. uint32_t type_id) {
  58. std::unique_ptr<Instruction> newGlobal(
  59. new Instruction(context(), opcode, type_id, result_id, {}));
  60. AddGlobalValue(std::move(newGlobal));
  61. }
  62. void Module::ForEachInst(const std::function<void(Instruction*)>& f,
  63. bool run_on_debug_line_insts) {
  64. #define DELEGATE(list) list.ForEachInst(f, run_on_debug_line_insts)
  65. DELEGATE(capabilities_);
  66. DELEGATE(extensions_);
  67. DELEGATE(ext_inst_imports_);
  68. if (memory_model_) memory_model_->ForEachInst(f, run_on_debug_line_insts);
  69. DELEGATE(entry_points_);
  70. DELEGATE(execution_modes_);
  71. DELEGATE(debugs1_);
  72. DELEGATE(debugs2_);
  73. DELEGATE(debugs3_);
  74. DELEGATE(annotations_);
  75. DELEGATE(types_values_);
  76. for (auto& i : functions_) i->ForEachInst(f, run_on_debug_line_insts);
  77. #undef DELEGATE
  78. }
  79. void Module::ForEachInst(const std::function<void(const Instruction*)>& f,
  80. bool run_on_debug_line_insts) const {
  81. #define DELEGATE(i) i.ForEachInst(f, run_on_debug_line_insts)
  82. for (auto& i : capabilities_) DELEGATE(i);
  83. for (auto& i : extensions_) DELEGATE(i);
  84. for (auto& i : ext_inst_imports_) DELEGATE(i);
  85. if (memory_model_)
  86. static_cast<const Instruction*>(memory_model_.get())
  87. ->ForEachInst(f, run_on_debug_line_insts);
  88. for (auto& i : entry_points_) DELEGATE(i);
  89. for (auto& i : execution_modes_) DELEGATE(i);
  90. for (auto& i : debugs1_) DELEGATE(i);
  91. for (auto& i : debugs2_) DELEGATE(i);
  92. for (auto& i : debugs3_) DELEGATE(i);
  93. for (auto& i : annotations_) DELEGATE(i);
  94. for (auto& i : types_values_) DELEGATE(i);
  95. for (auto& i : functions_) {
  96. static_cast<const Function*>(i.get())->ForEachInst(f,
  97. run_on_debug_line_insts);
  98. }
  99. #undef DELEGATE
  100. }
  101. void Module::ToBinary(std::vector<uint32_t>* binary, bool skip_nop) const {
  102. binary->push_back(header_.magic_number);
  103. binary->push_back(header_.version);
  104. // TODO(antiagainst): should we change the generator number?
  105. binary->push_back(header_.generator);
  106. binary->push_back(header_.bound);
  107. binary->push_back(header_.reserved);
  108. auto write_inst = [binary, skip_nop](const Instruction* i) {
  109. if (!(skip_nop && i->IsNop())) i->ToBinaryWithoutAttachedDebugInsts(binary);
  110. };
  111. ForEachInst(write_inst, true);
  112. }
  113. uint32_t Module::ComputeIdBound() const {
  114. uint32_t highest = 0;
  115. ForEachInst(
  116. [&highest](const Instruction* inst) {
  117. for (const auto& operand : *inst) {
  118. if (spvIsIdType(operand.type)) {
  119. highest = std::max(highest, operand.words[0]);
  120. }
  121. }
  122. },
  123. true /* scan debug line insts as well */);
  124. return highest + 1;
  125. }
  126. bool Module::HasExplicitCapability(uint32_t cap) {
  127. for (auto& ci : capabilities_) {
  128. uint32_t tcap = ci.GetSingleWordOperand(0);
  129. if (tcap == cap) {
  130. return true;
  131. }
  132. }
  133. return false;
  134. }
  135. uint32_t Module::GetExtInstImportId(const char* extstr) {
  136. for (auto& ei : ext_inst_imports_)
  137. if (!strcmp(extstr,
  138. reinterpret_cast<const char*>(&(ei.GetInOperand(0).words[0]))))
  139. return ei.result_id();
  140. return 0;
  141. }
  142. std::ostream& operator<<(std::ostream& str, const Module& module) {
  143. module.ForEachInst([&str](const Instruction* inst) {
  144. str << *inst;
  145. if (inst->opcode() != SpvOpFunctionEnd) {
  146. str << std::endl;
  147. }
  148. });
  149. return str;
  150. }
  151. } // namespace opt
  152. } // namespace spvtools