remove_unused_interface_variables_pass.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. roots.push(entry_.GetSingleWordInOperand(1));
  53. parent_.context()->ProcessCallTreeFromRoots(pfn_, &roots);
  54. }
  55. bool ShouldModify() {
  56. std::unordered_set<uint32_t> old_variables;
  57. for (int i = entry_.NumInOperands() - 1; i >= 3; --i) {
  58. auto variable = entry_.GetInOperand(i).words[0];
  59. if (!used_variables_.count(variable)) return true; // It is unused.
  60. if (old_variables.count(variable)) return true; // It is duplicate.
  61. old_variables.insert(variable);
  62. }
  63. if (old_variables.size() != used_variables_.size()) // Missing IDs.
  64. return true;
  65. return false;
  66. }
  67. void Modify() {
  68. for (int i = entry_.NumInOperands() - 1; i >= 3; --i)
  69. entry_.RemoveInOperand(i);
  70. for (auto id : operands_to_add_) {
  71. entry_.AddOperand(Operand(SPV_OPERAND_TYPE_ID, {id}));
  72. }
  73. }
  74. };
  75. RemoveUnusedInterfaceVariablesPass::Status
  76. RemoveUnusedInterfaceVariablesPass::Process() {
  77. bool modified = false;
  78. for (auto& entry : get_module()->entry_points()) {
  79. RemoveUnusedInterfaceVariablesContext context(*this, entry);
  80. context.CollectUsedVariables();
  81. if (context.ShouldModify()) {
  82. context.Modify();
  83. modified = true;
  84. }
  85. }
  86. return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange);
  87. }
  88. } // namespace opt
  89. } // namespace spvtools