reduce_load_size.cpp 6.0 KB

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