remove_unused_interface_variables_pass.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. IRContext::ProcessFunction pfn_ =
  23. std::bind(&RemoveUnusedInterfaceVariablesContext::processFunction, this,
  24. std::placeholders::_1);
  25. bool processFunction(Function* func) {
  26. for (const auto& basic_block : *func)
  27. for (const auto& instruction : basic_block)
  28. instruction.ForEachInId([&](const uint32_t* id) {
  29. if (used_variables_.count(*id)) return;
  30. auto* var = parent_.get_def_use_mgr()->GetDef(*id);
  31. if (!var || var->opcode() != SpvOpVariable) return;
  32. auto storage_class = var->GetSingleWordInOperand(0);
  33. if (storage_class != SpvStorageClassFunction &&
  34. (parent_.get_module()->version() >=
  35. SPV_SPIRV_VERSION_WORD(1, 4) ||
  36. storage_class == SpvStorageClassInput ||
  37. storage_class == SpvStorageClassOutput))
  38. used_variables_.insert(*id);
  39. });
  40. return false;
  41. }
  42. public:
  43. RemoveUnusedInterfaceVariablesContext(
  44. RemoveUnusedInterfaceVariablesPass& parent, Instruction& entry)
  45. : parent_(parent), entry_(entry) {}
  46. void CollectUsedVariables() {
  47. std::queue<uint32_t> roots;
  48. roots.push(entry_.GetSingleWordInOperand(1));
  49. parent_.context()->ProcessCallTreeFromRoots(pfn_, &roots);
  50. }
  51. bool ShouldModify() {
  52. std::unordered_set<uint32_t> old_variables;
  53. for (int i = entry_.NumInOperands() - 1; i >= 3; --i) {
  54. auto variable = entry_.GetInOperand(i).words[0];
  55. if (!used_variables_.count(variable)) return true; // It is unused.
  56. if (old_variables.count(variable)) return true; // It is duplicate.
  57. old_variables.insert(variable);
  58. }
  59. if (old_variables.size() != used_variables_.size()) // Missing IDs.
  60. return true;
  61. return false;
  62. }
  63. void Modify() {
  64. for (int i = entry_.NumInOperands() - 1; i >= 3; --i)
  65. entry_.RemoveInOperand(i);
  66. for (auto id : used_variables_) {
  67. entry_.AddOperand(Operand(SPV_OPERAND_TYPE_ID, {id}));
  68. }
  69. }
  70. };
  71. RemoveUnusedInterfaceVariablesPass::Status
  72. RemoveUnusedInterfaceVariablesPass::Process() {
  73. bool modified = false;
  74. for (auto& entry : get_module()->entry_points()) {
  75. RemoveUnusedInterfaceVariablesContext context(*this, entry);
  76. context.CollectUsedVariables();
  77. if (context.ShouldModify()) {
  78. context.Modify();
  79. modified = true;
  80. }
  81. }
  82. return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange);
  83. }
  84. } // namespace opt
  85. } // namespace spvtools