remove_unused_interface_variables_pass.cpp 3.3 KB

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