module.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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(spv::Op 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(spv::Op 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. if (sampled_image_address_mode_)
  81. sampled_image_address_mode_->ForEachInst(f, run_on_debug_line_insts);
  82. DELEGATE(entry_points_);
  83. DELEGATE(execution_modes_);
  84. DELEGATE(debugs1_);
  85. DELEGATE(debugs2_);
  86. DELEGATE(debugs3_);
  87. DELEGATE(ext_inst_debuginfo_);
  88. DELEGATE(annotations_);
  89. DELEGATE(types_values_);
  90. for (auto& i : functions_) {
  91. i->ForEachInst(f, run_on_debug_line_insts,
  92. /* run_on_non_semantic_insts = */ true);
  93. }
  94. #undef DELEGATE
  95. }
  96. void Module::ForEachInst(const std::function<void(const Instruction*)>& f,
  97. bool run_on_debug_line_insts) const {
  98. #define DELEGATE(i) i.ForEachInst(f, run_on_debug_line_insts)
  99. for (auto& i : capabilities_) DELEGATE(i);
  100. for (auto& i : extensions_) DELEGATE(i);
  101. for (auto& i : ext_inst_imports_) DELEGATE(i);
  102. if (memory_model_)
  103. static_cast<const Instruction*>(memory_model_.get())
  104. ->ForEachInst(f, run_on_debug_line_insts);
  105. if (sampled_image_address_mode_)
  106. static_cast<const Instruction*>(sampled_image_address_mode_.get())
  107. ->ForEachInst(f, run_on_debug_line_insts);
  108. for (auto& i : entry_points_) DELEGATE(i);
  109. for (auto& i : execution_modes_) DELEGATE(i);
  110. for (auto& i : debugs1_) DELEGATE(i);
  111. for (auto& i : debugs2_) DELEGATE(i);
  112. for (auto& i : debugs3_) DELEGATE(i);
  113. for (auto& i : annotations_) DELEGATE(i);
  114. for (auto& i : types_values_) DELEGATE(i);
  115. for (auto& i : ext_inst_debuginfo_) DELEGATE(i);
  116. for (auto& i : functions_) {
  117. static_cast<const Function*>(i.get())->ForEachInst(
  118. f, run_on_debug_line_insts,
  119. /* run_on_non_semantic_insts = */ true);
  120. }
  121. if (run_on_debug_line_insts) {
  122. for (auto& i : trailing_dbg_line_info_) DELEGATE(i);
  123. }
  124. #undef DELEGATE
  125. }
  126. void Module::ToBinary(std::vector<uint32_t>* binary, bool skip_nop) const {
  127. binary->push_back(header_.magic_number);
  128. binary->push_back(header_.version);
  129. // TODO(antiagainst): should we change the generator number?
  130. binary->push_back(header_.generator);
  131. binary->push_back(header_.bound);
  132. binary->push_back(header_.schema);
  133. size_t bound_idx = binary->size() - 2;
  134. DebugScope last_scope(kNoDebugScope, kNoInlinedAt);
  135. const Instruction* last_line_inst = nullptr;
  136. bool between_merge_and_branch = false;
  137. bool between_label_and_phi_var = false;
  138. auto write_inst = [binary, skip_nop, &last_scope, &last_line_inst,
  139. &between_merge_and_branch, &between_label_and_phi_var,
  140. this](const Instruction* i) {
  141. // Skip emitting line instructions between merge and branch instructions.
  142. auto opcode = i->opcode();
  143. if (between_merge_and_branch && i->IsLineInst()) {
  144. return;
  145. }
  146. if (last_line_inst != nullptr) {
  147. // If the current instruction is OpLine or DebugLine and it is the same
  148. // as the last line instruction that is still effective (can be applied
  149. // to the next instruction), we skip writing the current instruction.
  150. if (i->IsLine()) {
  151. uint32_t operand_index = 0;
  152. if (last_line_inst->WhileEachInOperand(
  153. [&operand_index, i](const uint32_t* word) {
  154. assert(i->NumInOperandWords() > operand_index);
  155. return *word == i->GetSingleWordInOperand(operand_index++);
  156. })) {
  157. return;
  158. }
  159. } else if (!i->IsNoLine() && i->dbg_line_insts().empty()) {
  160. // If the current instruction does not have the line information,
  161. // the last line information is not effective any more. Emit OpNoLine
  162. // or DebugNoLine to specify it.
  163. uint32_t shader_set_id = context()
  164. ->get_feature_mgr()
  165. ->GetExtInstImportId_Shader100DebugInfo();
  166. if (shader_set_id != 0) {
  167. binary->push_back((5 << 16) |
  168. static_cast<uint16_t>(spv::Op::OpExtInst));
  169. binary->push_back(context()->get_type_mgr()->GetVoidTypeId());
  170. binary->push_back(context()->TakeNextId());
  171. binary->push_back(shader_set_id);
  172. binary->push_back(NonSemanticShaderDebugInfo100DebugNoLine);
  173. } else {
  174. binary->push_back((1 << 16) |
  175. static_cast<uint16_t>(spv::Op::OpNoLine));
  176. }
  177. last_line_inst = nullptr;
  178. }
  179. }
  180. if (opcode == spv::Op::OpLabel) {
  181. between_label_and_phi_var = true;
  182. } else if (opcode != spv::Op::OpVariable && opcode != spv::Op::OpPhi &&
  183. !spvtools::opt::IsOpLineInst(opcode)) {
  184. between_label_and_phi_var = false;
  185. }
  186. if (!(skip_nop && i->IsNop())) {
  187. const auto& scope = i->GetDebugScope();
  188. if (scope != last_scope && !between_merge_and_branch) {
  189. // Can only emit nonsemantic instructions after all phi instructions
  190. // in a block so don't emit scope instructions before phi instructions
  191. // for NonSemantic.Shader.DebugInfo.100.
  192. if (!between_label_and_phi_var ||
  193. context()
  194. ->get_feature_mgr()
  195. ->GetExtInstImportId_OpenCL100DebugInfo()) {
  196. // Emit DebugScope |scope| to |binary|.
  197. auto dbg_inst = ext_inst_debuginfo_.begin();
  198. scope.ToBinary(dbg_inst->type_id(), context()->TakeNextId(),
  199. dbg_inst->GetSingleWordOperand(2), binary);
  200. }
  201. last_scope = scope;
  202. }
  203. i->ToBinaryWithoutAttachedDebugInsts(binary);
  204. }
  205. // Update the last line instruction.
  206. between_merge_and_branch = false;
  207. if (spvOpcodeIsBlockTerminator(opcode) || i->IsNoLine()) {
  208. last_line_inst = nullptr;
  209. } else if (opcode == spv::Op::OpLoopMerge ||
  210. opcode == spv::Op::OpSelectionMerge) {
  211. between_merge_and_branch = true;
  212. last_line_inst = nullptr;
  213. } else if (i->IsLine()) {
  214. last_line_inst = i;
  215. }
  216. };
  217. ForEachInst(write_inst, true);
  218. // We create new instructions for DebugScope and DebugNoLine. The bound must
  219. // be updated.
  220. binary->data()[bound_idx] = header_.bound;
  221. }
  222. uint32_t Module::ComputeIdBound() const {
  223. uint32_t highest = 0;
  224. ForEachInst(
  225. [&highest](const Instruction* inst) {
  226. for (const auto& operand : *inst) {
  227. if (spvIsIdType(operand.type)) {
  228. highest = std::max(highest, operand.words[0]);
  229. }
  230. }
  231. },
  232. true /* scan debug line insts as well */);
  233. return highest + 1;
  234. }
  235. bool Module::HasExplicitCapability(uint32_t cap) {
  236. for (auto& ci : capabilities_) {
  237. uint32_t tcap = ci.GetSingleWordOperand(0);
  238. if (tcap == cap) {
  239. return true;
  240. }
  241. }
  242. return false;
  243. }
  244. uint32_t Module::GetExtInstImportId(const char* extstr) {
  245. for (auto& ei : ext_inst_imports_)
  246. if (!ei.GetInOperand(0).AsString().compare(extstr)) return ei.result_id();
  247. return 0;
  248. }
  249. std::ostream& operator<<(std::ostream& str, const Module& module) {
  250. module.ForEachInst([&str](const Instruction* inst) {
  251. str << *inst;
  252. if (inst->opcode() != spv::Op::OpFunctionEnd) {
  253. str << std::endl;
  254. }
  255. });
  256. return str;
  257. }
  258. } // namespace opt
  259. } // namespace spvtools