eliminate_dead_input_components_pass.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright (c) 2022 The Khronos Group Inc.
  2. // Copyright (c) 2022 LunarG Inc.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #include "source/opt/eliminate_dead_input_components_pass.h"
  16. #include <set>
  17. #include <vector>
  18. #include "source/opt/instruction.h"
  19. #include "source/opt/ir_builder.h"
  20. #include "source/opt/ir_context.h"
  21. #include "source/util/bit_vector.h"
  22. namespace {
  23. const uint32_t kAccessChainBaseInIdx = 0;
  24. const uint32_t kAccessChainIndex0InIdx = 1;
  25. const uint32_t kConstantValueInIdx = 0;
  26. const uint32_t kVariableStorageClassInIdx = 0;
  27. } // namespace
  28. namespace spvtools {
  29. namespace opt {
  30. Pass::Status EliminateDeadInputComponentsPass::Process() {
  31. // Current functionality assumes shader capability
  32. if (!context()->get_feature_mgr()->HasCapability(SpvCapabilityShader))
  33. return Status::SuccessWithoutChange;
  34. analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
  35. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  36. bool modified = false;
  37. std::vector<std::pair<Instruction*, unsigned>> arrays_to_change;
  38. for (auto& var : context()->types_values()) {
  39. if (var.opcode() != SpvOpVariable) {
  40. continue;
  41. }
  42. analysis::Type* var_type = type_mgr->GetType(var.type_id());
  43. analysis::Pointer* ptr_type = var_type->AsPointer();
  44. if (ptr_type == nullptr) {
  45. continue;
  46. }
  47. if (ptr_type->storage_class() != SpvStorageClassInput) {
  48. continue;
  49. }
  50. const analysis::Array* arr_type = ptr_type->pointee_type()->AsArray();
  51. if (arr_type == nullptr) {
  52. continue;
  53. }
  54. unsigned arr_len_id = arr_type->LengthId();
  55. Instruction* arr_len_inst = def_use_mgr->GetDef(arr_len_id);
  56. if (arr_len_inst->opcode() != SpvOpConstant) {
  57. continue;
  58. }
  59. // SPIR-V requires array size is >= 1, so this works for signed or
  60. // unsigned size
  61. unsigned original_max =
  62. arr_len_inst->GetSingleWordInOperand(kConstantValueInIdx) - 1;
  63. unsigned max_idx = FindMaxIndex(var, original_max);
  64. if (max_idx != original_max) {
  65. ChangeArrayLength(var, max_idx + 1);
  66. modified = true;
  67. }
  68. }
  69. return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
  70. }
  71. unsigned EliminateDeadInputComponentsPass::FindMaxIndex(Instruction& var,
  72. unsigned original_max) {
  73. unsigned max = 0;
  74. bool seen_non_const_ac = false;
  75. assert(var.opcode() == SpvOpVariable && "must be variable");
  76. context()->get_def_use_mgr()->WhileEachUser(
  77. var.result_id(), [&max, &seen_non_const_ac, var, this](Instruction* use) {
  78. auto use_opcode = use->opcode();
  79. if (use_opcode == SpvOpLoad || use_opcode == SpvOpCopyMemory ||
  80. use_opcode == SpvOpCopyMemorySized ||
  81. use_opcode == SpvOpCopyObject) {
  82. seen_non_const_ac = true;
  83. return false;
  84. }
  85. if (use->opcode() != SpvOpAccessChain &&
  86. use->opcode() != SpvOpInBoundsAccessChain) {
  87. return true;
  88. }
  89. // OpAccessChain with no indices currently not optimized
  90. if (use->NumInOperands() == 1) {
  91. seen_non_const_ac = true;
  92. return false;
  93. }
  94. unsigned base_id = use->GetSingleWordInOperand(kAccessChainBaseInIdx);
  95. USE_ASSERT(base_id == var.result_id() && "unexpected base");
  96. unsigned idx_id = use->GetSingleWordInOperand(kAccessChainIndex0InIdx);
  97. Instruction* idx_inst = context()->get_def_use_mgr()->GetDef(idx_id);
  98. if (idx_inst->opcode() != SpvOpConstant) {
  99. seen_non_const_ac = true;
  100. return false;
  101. }
  102. unsigned value = idx_inst->GetSingleWordInOperand(kConstantValueInIdx);
  103. if (value > max) max = value;
  104. return true;
  105. });
  106. return seen_non_const_ac ? original_max : max;
  107. }
  108. void EliminateDeadInputComponentsPass::ChangeArrayLength(Instruction& arr,
  109. unsigned length) {
  110. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  111. analysis::ConstantManager* const_mgr = context()->get_constant_mgr();
  112. analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
  113. analysis::Pointer* ptr_type = type_mgr->GetType(arr.type_id())->AsPointer();
  114. const analysis::Array* arr_ty = ptr_type->pointee_type()->AsArray();
  115. assert(arr_ty && "expecting array type");
  116. uint32_t length_id = const_mgr->GetUIntConst(length);
  117. analysis::Array new_arr_ty(arr_ty->element_type(),
  118. arr_ty->GetConstantLengthInfo(length_id, length));
  119. analysis::Type* reg_new_arr_ty = type_mgr->GetRegisteredType(&new_arr_ty);
  120. analysis::Pointer new_ptr_ty(reg_new_arr_ty, SpvStorageClassInput);
  121. analysis::Type* reg_new_ptr_ty = type_mgr->GetRegisteredType(&new_ptr_ty);
  122. uint32_t new_ptr_ty_id = type_mgr->GetTypeInstruction(reg_new_ptr_ty);
  123. arr.SetResultType(new_ptr_ty_id);
  124. def_use_mgr->AnalyzeInstUse(&arr);
  125. // Move array OpVariable instruction after its new type to preserve order
  126. USE_ASSERT(arr.GetSingleWordInOperand(kVariableStorageClassInIdx) !=
  127. SpvStorageClassFunction &&
  128. "cannot move Function variable");
  129. Instruction* new_ptr_ty_inst = def_use_mgr->GetDef(new_ptr_ty_id);
  130. arr.RemoveFromList();
  131. arr.InsertAfter(new_ptr_ty_inst);
  132. }
  133. } // namespace opt
  134. } // namespace spvtools