remove_unused_interface_variables_pass.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright (c) 2021 ZHOU He
  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 "remove_unused_interface_variables_pass.h"
  15. #include "source/spirv_constant.h"
  16. namespace spvtools {
  17. namespace opt {
  18. class RemoveUnusedInterfaceVariablesContext {
  19. RemoveUnusedInterfaceVariablesPass& parent_;
  20. Instruction& entry_;
  21. std::unordered_set<uint32_t> used_variables_;
  22. std::vector<uint32_t> operands_to_add_;
  23. IRContext::ProcessFunction pfn_ =
  24. std::bind(&RemoveUnusedInterfaceVariablesContext::processFunction, this,
  25. std::placeholders::_1);
  26. bool processFunction(Function* func) {
  27. for (const auto& basic_block : *func)
  28. for (const auto& instruction : basic_block)
  29. instruction.ForEachInId([&](const uint32_t* id) {
  30. if (used_variables_.count(*id)) return;
  31. auto* var = parent_.get_def_use_mgr()->GetDef(*id);
  32. if (!var || var->opcode() != spv::Op::OpVariable) return;
  33. auto storage_class =
  34. spv::StorageClass(var->GetSingleWordInOperand(0));
  35. if (storage_class != spv::StorageClass::Function &&
  36. (parent_.get_module()->version() >=
  37. SPV_SPIRV_VERSION_WORD(1, 4) ||
  38. storage_class == spv::StorageClass::Input ||
  39. storage_class == spv::StorageClass::Output)) {
  40. used_variables_.insert(*id);
  41. operands_to_add_.push_back(*id);
  42. }
  43. });
  44. return false;
  45. }
  46. public:
  47. RemoveUnusedInterfaceVariablesContext(
  48. RemoveUnusedInterfaceVariablesPass& parent, Instruction& entry)
  49. : parent_(parent), entry_(entry) {}
  50. void CollectUsedVariables() {
  51. std::queue<uint32_t> roots;
  52. const int op_i =
  53. entry_.opcode() == spv::Op::OpConditionalEntryPointINTEL ? 2 : 1;
  54. roots.push(entry_.GetSingleWordInOperand(op_i));
  55. parent_.context()->ProcessCallTreeFromRoots(pfn_, &roots);
  56. }
  57. bool ShouldModify() {
  58. std::unordered_set<uint32_t> old_variables;
  59. for (int i = entry_.NumInOperands() - 1; i >= 3; --i) {
  60. auto variable = entry_.GetInOperand(i).words[0];
  61. if (!used_variables_.count(variable)) return true; // It is unused.
  62. if (old_variables.count(variable)) return true; // It is duplicate.
  63. old_variables.insert(variable);
  64. }
  65. if (old_variables.size() != used_variables_.size()) // Missing IDs.
  66. return true;
  67. return false;
  68. }
  69. void Modify() {
  70. const int min_num_operands =
  71. entry_.opcode() == spv::Op::OpConditionalEntryPointINTEL ? 4 : 3;
  72. for (int i = entry_.NumInOperands() - 1; i >= min_num_operands; --i)
  73. entry_.RemoveInOperand(i);
  74. for (auto id : operands_to_add_) {
  75. entry_.AddOperand(Operand(SPV_OPERAND_TYPE_ID, {id}));
  76. }
  77. }
  78. };
  79. RemoveUnusedInterfaceVariablesPass::Status
  80. RemoveUnusedInterfaceVariablesPass::Process() {
  81. bool modified = false;
  82. for (auto& entry : get_module()->entry_points()) {
  83. RemoveUnusedInterfaceVariablesContext context(*this, entry);
  84. context.CollectUsedVariables();
  85. if (context.ShouldModify()) {
  86. context.Modify();
  87. modified = true;
  88. }
  89. }
  90. return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange);
  91. }
  92. } // namespace opt
  93. } // namespace spvtools