decompose_initialized_variables_pass.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright (c) 2019 Google LLC.
  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/decompose_initialized_variables_pass.h"
  15. #include "source/opt/ir_context.h"
  16. namespace spvtools {
  17. namespace opt {
  18. using inst_iterator = InstructionList::iterator;
  19. namespace {
  20. bool HasInitializer(Instruction* inst) {
  21. if (inst->opcode() != SpvOpVariable) return false;
  22. if (inst->NumOperands() < 4) return false;
  23. return true;
  24. }
  25. } // namespace
  26. Pass::Status DecomposeInitializedVariablesPass::Process() {
  27. auto* module = context()->module();
  28. std::unordered_set<Instruction*> changed;
  29. std::vector<std::tuple<uint32_t, uint32_t>> global_stores;
  30. for (auto iter = module->types_values_begin();
  31. iter != module->types_values_end(); ++iter) {
  32. Instruction* inst = &(*iter);
  33. if (!HasInitializer(inst)) continue;
  34. auto var_id = inst->result_id();
  35. auto val_id = inst->GetOperand(3).words[0];
  36. global_stores.push_back(std::make_tuple(var_id, val_id));
  37. iter->RemoveOperand(3);
  38. changed.insert(&*iter);
  39. }
  40. std::unordered_set<uint32_t> entry_ids;
  41. for (auto entry = module->entry_points().begin();
  42. entry != module->entry_points().end(); ++entry) {
  43. entry_ids.insert(entry->GetSingleWordInOperand(1));
  44. }
  45. for (auto func = module->begin(); func != module->end(); ++func) {
  46. std::vector<Instruction*> function_stores;
  47. auto first_block = func->entry().get();
  48. inst_iterator insert_point = first_block->begin();
  49. for (auto iter = first_block->begin();
  50. iter != first_block->end() && iter->opcode() == SpvOpVariable;
  51. ++iter) {
  52. // For valid SPIRV-V, there is guaranteed to be at least one instruction
  53. // after the OpVariable instructions.
  54. insert_point = (*iter).NextNode();
  55. Instruction* inst = &(*iter);
  56. if (!HasInitializer(inst)) continue;
  57. auto var_id = inst->result_id();
  58. auto val_id = inst->GetOperand(3).words[0];
  59. Instruction* store_inst = new Instruction(
  60. context(), SpvOpStore, 0, 0,
  61. {{SPV_OPERAND_TYPE_ID, {var_id}}, {SPV_OPERAND_TYPE_ID, {val_id}}});
  62. function_stores.push_back(store_inst);
  63. iter->RemoveOperand(3);
  64. changed.insert(&*iter);
  65. }
  66. if (entry_ids.find(func->result_id()) != entry_ids.end()) {
  67. for (auto store_ids : global_stores) {
  68. uint32_t var_id;
  69. uint32_t val_id;
  70. std::tie(var_id, val_id) = store_ids;
  71. auto* store_inst = new Instruction(
  72. context(), SpvOpStore, 0, 0,
  73. {{SPV_OPERAND_TYPE_ID, {var_id}}, {SPV_OPERAND_TYPE_ID, {val_id}}});
  74. context()->set_instr_block(store_inst, &*first_block);
  75. first_block->AddInstruction(std::unique_ptr<Instruction>(store_inst));
  76. store_inst->InsertBefore(&*insert_point);
  77. changed.insert(store_inst);
  78. }
  79. }
  80. for (auto store = function_stores.begin(); store != function_stores.end();
  81. ++store) {
  82. context()->set_instr_block(*store, first_block);
  83. (*store)->InsertBefore(&*insert_point);
  84. changed.insert(*store);
  85. }
  86. }
  87. auto* def_use_mgr = get_def_use_mgr();
  88. for (auto* inst : changed) def_use_mgr->UpdateDefUse(inst);
  89. return !changed.empty() ? Pass::Status::SuccessWithChange
  90. : Pass::Status::SuccessWithoutChange;
  91. }
  92. } // namespace opt
  93. } // namespace spvtools