/src/spirv-tools/source/opt/simplification_pass.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2018 Google LLC |
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 "source/opt/simplification_pass.h" |
16 | | |
17 | | #include <set> |
18 | | #include <unordered_set> |
19 | | #include <vector> |
20 | | |
21 | | #include "source/opt/fold.h" |
22 | | |
23 | | namespace spvtools { |
24 | | namespace opt { |
25 | | |
26 | 23.6k | Pass::Status SimplificationPass::Process() { |
27 | 23.6k | bool modified = false; |
28 | | |
29 | 23.6k | for (Function& function : *get_module()) { |
30 | 21.8k | modified |= SimplifyFunction(&function); |
31 | 21.8k | } |
32 | 23.6k | return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange); |
33 | 23.6k | } |
34 | | |
35 | | void SimplificationPass::AddNewOperands( |
36 | | Instruction* folded_inst, std::unordered_set<Instruction*>* inst_seen, |
37 | 1.24M | std::vector<Instruction*>* work_list) { |
38 | 1.24M | analysis::DefUseManager* def_use_mgr = get_def_use_mgr(); |
39 | 1.24M | folded_inst->ForEachInId( |
40 | 1.25M | [&inst_seen, &def_use_mgr, &work_list](uint32_t* iid) { |
41 | 1.25M | Instruction* iid_inst = def_use_mgr->GetDef(*iid); |
42 | 1.25M | if (!inst_seen->insert(iid_inst).second) return; |
43 | 216k | work_list->push_back(iid_inst); |
44 | 216k | }); |
45 | 1.24M | } |
46 | | |
47 | 21.8k | bool SimplificationPass::SimplifyFunction(Function* function) { |
48 | 21.8k | if (function->IsDeclaration()) { |
49 | 0 | return false; |
50 | 0 | } |
51 | | |
52 | 21.8k | bool modified = false; |
53 | | // Phase 1: Traverse all instructions in dominance order. |
54 | | // The second phase will only be on the instructions whose inputs have changed |
55 | | // after being processed during phase 1. Since OpPhi instructions are the |
56 | | // only instructions whose inputs do not necessarily dominate the use, we keep |
57 | | // track of the OpPhi instructions already seen, and add them to the work list |
58 | | // for phase 2 when needed. |
59 | 21.8k | std::vector<Instruction*> work_list; |
60 | 21.8k | std::unordered_set<Instruction*> process_phis; |
61 | 21.8k | std::unordered_set<Instruction*> inst_to_kill; |
62 | 21.8k | std::unordered_set<Instruction*> in_work_list; |
63 | 21.8k | std::unordered_set<Instruction*> inst_seen; |
64 | 21.8k | const InstructionFolder& folder = context()->get_instruction_folder(); |
65 | | |
66 | 21.8k | cfg()->ForEachBlockInReversePostOrder( |
67 | 21.8k | function->entry().get(), |
68 | 21.8k | [&modified, &process_phis, &work_list, &in_work_list, &inst_to_kill, |
69 | 1.35M | &folder, &inst_seen, this](BasicBlock* bb) { |
70 | 7.29M | for (Instruction* inst = &*bb->begin(); inst; inst = inst->NextNode()) { |
71 | 5.94M | inst_seen.insert(inst); |
72 | 5.94M | if (inst->opcode() == spv::Op::OpPhi) { |
73 | 188k | process_phis.insert(inst); |
74 | 188k | } |
75 | | |
76 | 5.94M | bool is_foldable_copy = |
77 | 5.94M | inst->opcode() == spv::Op::OpCopyObject && |
78 | 5.94M | context()->get_decoration_mgr()->HaveSubsetOfDecorations( |
79 | 158 | inst->result_id(), inst->GetSingleWordInOperand(0)); |
80 | | |
81 | 5.94M | if (is_foldable_copy || folder.FoldInstruction(inst)) { |
82 | 1.23M | modified = true; |
83 | 1.23M | context()->AnalyzeUses(inst); |
84 | 1.23M | get_def_use_mgr()->ForEachUser(inst, [&work_list, &process_phis, |
85 | 1.23M | &in_work_list]( |
86 | 1.59M | Instruction* use) { |
87 | 1.59M | if (process_phis.count(use) && in_work_list.insert(use).second) { |
88 | 2.26k | work_list.push_back(use); |
89 | 2.26k | } |
90 | 1.59M | }); |
91 | | |
92 | 1.23M | AddNewOperands(inst, &inst_seen, &work_list); |
93 | | |
94 | 1.23M | if (inst->opcode() == spv::Op::OpCopyObject) { |
95 | 1.21M | context()->ReplaceAllUsesWithPredicate( |
96 | 1.21M | inst->result_id(), inst->GetSingleWordInOperand(0), |
97 | 1.59M | [](Instruction* user) { |
98 | 1.59M | const auto opcode = user->opcode(); |
99 | 1.59M | if (!spvOpcodeIsDebug(opcode) && |
100 | 1.59M | !spvOpcodeIsDecoration(opcode)) { |
101 | 1.58M | return true; |
102 | 1.58M | } |
103 | 1.88k | return false; |
104 | 1.59M | }); |
105 | 1.21M | inst_to_kill.insert(inst); |
106 | 1.21M | in_work_list.insert(inst); |
107 | 1.21M | } else if (inst->opcode() == spv::Op::OpNop) { |
108 | 6.11k | inst_to_kill.insert(inst); |
109 | 6.11k | in_work_list.insert(inst); |
110 | 6.11k | } |
111 | 1.23M | } |
112 | 5.94M | } |
113 | 1.35M | }); |
114 | | |
115 | | // Phase 2: process the instructions in the work list until all of the work is |
116 | | // done. This time we add all users to the work list because phase 1 |
117 | | // has already finished. |
118 | 246k | for (size_t i = 0; i < work_list.size(); ++i) { |
119 | 224k | Instruction* inst = work_list[i]; |
120 | 224k | in_work_list.erase(inst); |
121 | 224k | inst_seen.insert(inst); |
122 | | |
123 | 224k | bool is_foldable_copy = |
124 | 224k | inst->opcode() == spv::Op::OpCopyObject && |
125 | 224k | context()->get_decoration_mgr()->HaveSubsetOfDecorations( |
126 | 0 | inst->result_id(), inst->GetSingleWordInOperand(0)); |
127 | | |
128 | 224k | if (is_foldable_copy || folder.FoldInstruction(inst)) { |
129 | 8.59k | modified = true; |
130 | 8.59k | context()->AnalyzeUses(inst); |
131 | 8.59k | get_def_use_mgr()->ForEachUser( |
132 | 10.3k | inst, [&work_list, &in_work_list](Instruction* use) { |
133 | 10.3k | if (!use->IsDecoration() && use->opcode() != spv::Op::OpName && |
134 | 10.3k | in_work_list.insert(use).second) { |
135 | 6.66k | work_list.push_back(use); |
136 | 6.66k | } |
137 | 10.3k | }); |
138 | | |
139 | 8.59k | AddNewOperands(inst, &inst_seen, &work_list); |
140 | | |
141 | 8.59k | if (inst->opcode() == spv::Op::OpCopyObject) { |
142 | 8.01k | context()->ReplaceAllUsesWithPredicate( |
143 | 8.01k | inst->result_id(), inst->GetSingleWordInOperand(0), |
144 | 9.83k | [](Instruction* user) { |
145 | 9.83k | const auto opcode = user->opcode(); |
146 | 9.83k | if (!spvOpcodeIsDebug(opcode) && !spvOpcodeIsDecoration(opcode)) { |
147 | 9.54k | return true; |
148 | 9.54k | } |
149 | 290 | return false; |
150 | 9.83k | }); |
151 | 8.01k | inst_to_kill.insert(inst); |
152 | 8.01k | in_work_list.insert(inst); |
153 | 8.01k | } else if (inst->opcode() == spv::Op::OpNop) { |
154 | 20 | inst_to_kill.insert(inst); |
155 | 20 | in_work_list.insert(inst); |
156 | 20 | } |
157 | 8.59k | } |
158 | 224k | } |
159 | | |
160 | | // Phase 3: Kill instructions we know are no longer needed. |
161 | 1.22M | for (Instruction* inst : inst_to_kill) { |
162 | 1.22M | context()->KillInst(inst); |
163 | 1.22M | } |
164 | | |
165 | 21.8k | return modified; |
166 | 21.8k | } |
167 | | |
168 | | } // namespace opt |
169 | | } // namespace spvtools |