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