reduce_load_size.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright (c) 2018 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/reduce_load_size.h"
  15. #include <set>
  16. #include <vector>
  17. #include "source/opt/instruction.h"
  18. #include "source/opt/ir_builder.h"
  19. #include "source/opt/ir_context.h"
  20. #include "source/util/bit_vector.h"
  21. namespace {
  22. const uint32_t kExtractCompositeIdInIdx = 0;
  23. const uint32_t kVariableStorageClassInIdx = 0;
  24. const uint32_t kLoadPointerInIdx = 0;
  25. } // namespace
  26. namespace spvtools {
  27. namespace opt {
  28. Pass::Status ReduceLoadSize::Process() {
  29. bool modified = false;
  30. for (auto& func : *get_module()) {
  31. func.ForEachInst([&modified, this](Instruction* inst) {
  32. if (inst->opcode() == SpvOpCompositeExtract) {
  33. if (ShouldReplaceExtract(inst)) {
  34. modified |= ReplaceExtract(inst);
  35. }
  36. }
  37. });
  38. }
  39. return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
  40. }
  41. bool ReduceLoadSize::ReplaceExtract(Instruction* inst) {
  42. assert(inst->opcode() == SpvOpCompositeExtract &&
  43. "Wrong opcode. Should be OpCompositeExtract.");
  44. analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
  45. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  46. analysis::ConstantManager* const_mgr = context()->get_constant_mgr();
  47. uint32_t composite_id =
  48. inst->GetSingleWordInOperand(kExtractCompositeIdInIdx);
  49. Instruction* composite_inst = def_use_mgr->GetDef(composite_id);
  50. if (composite_inst->opcode() != SpvOpLoad) {
  51. return false;
  52. }
  53. analysis::Type* composite_type = type_mgr->GetType(composite_inst->type_id());
  54. if (composite_type->kind() == analysis::Type::kVector ||
  55. composite_type->kind() == analysis::Type::kMatrix) {
  56. return false;
  57. }
  58. Instruction* var = composite_inst->GetBaseAddress();
  59. if (var == nullptr || var->opcode() != SpvOpVariable) {
  60. return false;
  61. }
  62. SpvStorageClass storage_class = static_cast<SpvStorageClass>(
  63. var->GetSingleWordInOperand(kVariableStorageClassInIdx));
  64. switch (storage_class) {
  65. case SpvStorageClassUniform:
  66. case SpvStorageClassUniformConstant:
  67. case SpvStorageClassInput:
  68. break;
  69. default:
  70. return false;
  71. }
  72. // Create a new access chain and load just after the old load.
  73. // We cannot create the new access chain load in the position of the extract
  74. // because the storage may have been written to in between.
  75. InstructionBuilder ir_builder(
  76. inst->context(), composite_inst,
  77. IRContext::kAnalysisInstrToBlockMapping | IRContext::kAnalysisDefUse);
  78. uint32_t pointer_to_result_type_id =
  79. type_mgr->FindPointerToType(inst->type_id(), storage_class);
  80. assert(pointer_to_result_type_id != 0 &&
  81. "We did not find the pointer type that we need.");
  82. analysis::Integer int_type(32, false);
  83. const analysis::Type* uint32_type = type_mgr->GetRegisteredType(&int_type);
  84. std::vector<uint32_t> ids;
  85. for (uint32_t i = 1; i < inst->NumInOperands(); ++i) {
  86. uint32_t index = inst->GetSingleWordInOperand(i);
  87. const analysis::Constant* index_const =
  88. const_mgr->GetConstant(uint32_type, {index});
  89. ids.push_back(const_mgr->GetDefiningInstruction(index_const)->result_id());
  90. }
  91. Instruction* new_access_chain = ir_builder.AddAccessChain(
  92. pointer_to_result_type_id,
  93. composite_inst->GetSingleWordInOperand(kLoadPointerInIdx), ids);
  94. Instruction* new_load =
  95. ir_builder.AddLoad(inst->type_id(), new_access_chain->result_id());
  96. context()->ReplaceAllUsesWith(inst->result_id(), new_load->result_id());
  97. context()->KillInst(inst);
  98. return true;
  99. }
  100. bool ReduceLoadSize::ShouldReplaceExtract(Instruction* inst) {
  101. analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
  102. Instruction* op_inst = def_use_mgr->GetDef(
  103. inst->GetSingleWordInOperand(kExtractCompositeIdInIdx));
  104. if (op_inst->opcode() != SpvOpLoad) {
  105. return false;
  106. }
  107. auto cached_result = should_replace_cache_.find(op_inst->result_id());
  108. if (cached_result != should_replace_cache_.end()) {
  109. return cached_result->second;
  110. }
  111. bool all_elements_used = false;
  112. std::set<uint32_t> elements_used;
  113. all_elements_used =
  114. !def_use_mgr->WhileEachUser(op_inst, [&elements_used](Instruction* use) {
  115. if (use->IsCommonDebugInstr()) return true;
  116. if (use->opcode() != SpvOpCompositeExtract ||
  117. use->NumInOperands() == 1) {
  118. return false;
  119. }
  120. elements_used.insert(use->GetSingleWordInOperand(1));
  121. return true;
  122. });
  123. bool should_replace = false;
  124. if (all_elements_used) {
  125. should_replace = false;
  126. } else if (1.0 <= replacement_threshold_) {
  127. should_replace = true;
  128. } else {
  129. analysis::ConstantManager* const_mgr = context()->get_constant_mgr();
  130. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  131. analysis::Type* load_type = type_mgr->GetType(op_inst->type_id());
  132. uint32_t total_size = 1;
  133. switch (load_type->kind()) {
  134. case analysis::Type::kArray: {
  135. const analysis::Constant* size_const =
  136. const_mgr->FindDeclaredConstant(load_type->AsArray()->LengthId());
  137. assert(size_const->AsIntConstant());
  138. total_size = size_const->GetU32();
  139. } break;
  140. case analysis::Type::kStruct:
  141. total_size = static_cast<uint32_t>(
  142. load_type->AsStruct()->element_types().size());
  143. break;
  144. default:
  145. break;
  146. }
  147. double percent_used = static_cast<double>(elements_used.size()) /
  148. static_cast<double>(total_size);
  149. should_replace = (percent_used < replacement_threshold_);
  150. }
  151. should_replace_cache_[op_inst->result_id()] = should_replace;
  152. return should_replace;
  153. }
  154. } // namespace opt
  155. } // namespace spvtools