• 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() != SpvOpVariable) return;
35           auto storage_class = var->GetSingleWordInOperand(0);
36           if (storage_class != SpvStorageClassFunction &&
37               (parent_.get_module()->version() >=
38                    SPV_SPIRV_VERSION_WORD(1, 4) ||
39                storage_class == SpvStorageClassInput ||
40                storage_class == SpvStorageClassOutput))
41             used_variables_.insert(*id);
42         });
43     return false;
44   }
45 
46  public:
RemoveUnusedInterfaceVariablesContext(RemoveUnusedInterfaceVariablesPass & parent,Instruction & entry)47   RemoveUnusedInterfaceVariablesContext(
48       RemoveUnusedInterfaceVariablesPass& parent, Instruction& entry)
49       : parent_(parent), entry_(entry) {}
50 
CollectUsedVariables()51   void CollectUsedVariables() {
52     std::queue<uint32_t> roots;
53     roots.push(entry_.GetSingleWordInOperand(1));
54     parent_.context()->ProcessCallTreeFromRoots(pfn_, &roots);
55   }
56 
ShouldModify()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 
Modify()70   void Modify() {
71     for (int i = entry_.NumInOperands() - 1; i >= 3; --i)
72       entry_.RemoveInOperand(i);
73     for (auto id : used_variables_) {
74       entry_.AddOperand(Operand(SPV_OPERAND_TYPE_ID, {id}));
75     }
76   }
77 };
78 
79 RemoveUnusedInterfaceVariablesPass::Status
Process()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