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/vector_dce.h"
16
17 #include <utility>
18
19 namespace spvtools {
20 namespace opt {
21 namespace {
22
23 const uint32_t kExtractCompositeIdInIdx = 0;
24 const uint32_t kInsertObjectIdInIdx = 0;
25 const uint32_t kInsertCompositeIdInIdx = 1;
26
27 } // namespace
28
Process()29 Pass::Status VectorDCE::Process() {
30 bool modified = false;
31 for (Function& function : *get_module()) {
32 modified |= VectorDCEFunction(&function);
33 }
34 return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange);
35 }
36
VectorDCEFunction(Function * function)37 bool VectorDCE::VectorDCEFunction(Function* function) {
38 LiveComponentMap live_components;
39 FindLiveComponents(function, &live_components);
40 return RewriteInstructions(function, live_components);
41 }
42
FindLiveComponents(Function * function,LiveComponentMap * live_components)43 void VectorDCE::FindLiveComponents(Function* function,
44 LiveComponentMap* live_components) {
45 std::vector<WorkListItem> work_list;
46
47 // Prime the work list. We will assume that any instruction that does
48 // not result in a vector is live.
49 //
50 // Extending to structures and matrices is not as straight forward because of
51 // the nesting. We cannot simply us a bit vector to keep track of which
52 // components are live because of arbitrary nesting of structs.
53 function->ForEachInst(
54 [&work_list, this, live_components](Instruction* current_inst) {
55 if (current_inst->IsOpenCL100DebugInstr()) {
56 return;
57 }
58 if (!HasVectorOrScalarResult(current_inst) ||
59 !context()->IsCombinatorInstruction(current_inst)) {
60 MarkUsesAsLive(current_inst, all_components_live_, live_components,
61 &work_list);
62 }
63 });
64
65 // Process the work list propagating liveness.
66 for (uint32_t i = 0; i < work_list.size(); i++) {
67 WorkListItem current_item = work_list[i];
68 Instruction* current_inst = current_item.instruction;
69
70 switch (current_inst->opcode()) {
71 case SpvOpCompositeExtract:
72 MarkExtractUseAsLive(current_inst, current_item.components,
73 live_components, &work_list);
74 break;
75 case SpvOpCompositeInsert:
76 MarkInsertUsesAsLive(current_item, live_components, &work_list);
77 break;
78 case SpvOpVectorShuffle:
79 MarkVectorShuffleUsesAsLive(current_item, live_components, &work_list);
80 break;
81 case SpvOpCompositeConstruct:
82 MarkCompositeContructUsesAsLive(current_item, live_components,
83 &work_list);
84 break;
85 default:
86 if (current_inst->IsScalarizable()) {
87 MarkUsesAsLive(current_inst, current_item.components, live_components,
88 &work_list);
89 } else {
90 MarkUsesAsLive(current_inst, all_components_live_, live_components,
91 &work_list);
92 }
93 break;
94 }
95 }
96 }
97
MarkExtractUseAsLive(const Instruction * current_inst,const utils::BitVector & live_elements,LiveComponentMap * live_components,std::vector<WorkListItem> * work_list)98 void VectorDCE::MarkExtractUseAsLive(const Instruction* current_inst,
99 const utils::BitVector& live_elements,
100 LiveComponentMap* live_components,
101 std::vector<WorkListItem>* work_list) {
102 analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
103 uint32_t operand_id =
104 current_inst->GetSingleWordInOperand(kExtractCompositeIdInIdx);
105 Instruction* operand_inst = def_use_mgr->GetDef(operand_id);
106
107 if (HasVectorOrScalarResult(operand_inst)) {
108 WorkListItem new_item;
109 new_item.instruction = operand_inst;
110 if (current_inst->NumInOperands() < 2) {
111 new_item.components = live_elements;
112 } else {
113 new_item.components.Set(current_inst->GetSingleWordInOperand(1));
114 }
115 AddItemToWorkListIfNeeded(new_item, live_components, work_list);
116 }
117 }
118
MarkInsertUsesAsLive(const VectorDCE::WorkListItem & current_item,LiveComponentMap * live_components,std::vector<VectorDCE::WorkListItem> * work_list)119 void VectorDCE::MarkInsertUsesAsLive(
120 const VectorDCE::WorkListItem& current_item,
121 LiveComponentMap* live_components,
122 std::vector<VectorDCE::WorkListItem>* work_list) {
123 analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
124
125 if (current_item.instruction->NumInOperands() > 2) {
126 uint32_t insert_position =
127 current_item.instruction->GetSingleWordInOperand(2);
128
129 // Add the elements of the composite object that are used.
130 uint32_t operand_id = current_item.instruction->GetSingleWordInOperand(
131 kInsertCompositeIdInIdx);
132 Instruction* operand_inst = def_use_mgr->GetDef(operand_id);
133
134 WorkListItem new_item;
135 new_item.instruction = operand_inst;
136 new_item.components = current_item.components;
137 new_item.components.Clear(insert_position);
138
139 AddItemToWorkListIfNeeded(new_item, live_components, work_list);
140
141 // Add the element being inserted if it is used.
142 if (current_item.components.Get(insert_position)) {
143 uint32_t obj_operand_id =
144 current_item.instruction->GetSingleWordInOperand(
145 kInsertObjectIdInIdx);
146 Instruction* obj_operand_inst = def_use_mgr->GetDef(obj_operand_id);
147 WorkListItem new_item_for_obj;
148 new_item_for_obj.instruction = obj_operand_inst;
149 new_item_for_obj.components.Set(0);
150 AddItemToWorkListIfNeeded(new_item_for_obj, live_components, work_list);
151 }
152 } else {
153 // If there are no indices, then this is a copy of the object being
154 // inserted.
155 uint32_t object_id =
156 current_item.instruction->GetSingleWordInOperand(kInsertObjectIdInIdx);
157 Instruction* object_inst = def_use_mgr->GetDef(object_id);
158
159 WorkListItem new_item;
160 new_item.instruction = object_inst;
161 new_item.components = current_item.components;
162 AddItemToWorkListIfNeeded(new_item, live_components, work_list);
163 }
164 }
165
MarkVectorShuffleUsesAsLive(const WorkListItem & current_item,VectorDCE::LiveComponentMap * live_components,std::vector<WorkListItem> * work_list)166 void VectorDCE::MarkVectorShuffleUsesAsLive(
167 const WorkListItem& current_item,
168 VectorDCE::LiveComponentMap* live_components,
169 std::vector<WorkListItem>* work_list) {
170 analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
171
172 WorkListItem first_operand;
173 first_operand.instruction =
174 def_use_mgr->GetDef(current_item.instruction->GetSingleWordInOperand(0));
175 WorkListItem second_operand;
176 second_operand.instruction =
177 def_use_mgr->GetDef(current_item.instruction->GetSingleWordInOperand(1));
178
179 analysis::TypeManager* type_mgr = context()->get_type_mgr();
180 analysis::Vector* first_type =
181 type_mgr->GetType(first_operand.instruction->type_id())->AsVector();
182 uint32_t size_of_first_operand = first_type->element_count();
183
184 for (uint32_t in_op = 2; in_op < current_item.instruction->NumInOperands();
185 ++in_op) {
186 uint32_t index = current_item.instruction->GetSingleWordInOperand(in_op);
187 if (current_item.components.Get(in_op - 2)) {
188 if (index < size_of_first_operand) {
189 first_operand.components.Set(index);
190 } else {
191 second_operand.components.Set(index - size_of_first_operand);
192 }
193 }
194 }
195
196 AddItemToWorkListIfNeeded(first_operand, live_components, work_list);
197 AddItemToWorkListIfNeeded(second_operand, live_components, work_list);
198 }
199
MarkCompositeContructUsesAsLive(VectorDCE::WorkListItem work_item,VectorDCE::LiveComponentMap * live_components,std::vector<VectorDCE::WorkListItem> * work_list)200 void VectorDCE::MarkCompositeContructUsesAsLive(
201 VectorDCE::WorkListItem work_item,
202 VectorDCE::LiveComponentMap* live_components,
203 std::vector<VectorDCE::WorkListItem>* work_list) {
204 analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
205 analysis::TypeManager* type_mgr = context()->get_type_mgr();
206
207 uint32_t current_component = 0;
208 Instruction* current_inst = work_item.instruction;
209 uint32_t num_in_operands = current_inst->NumInOperands();
210 for (uint32_t i = 0; i < num_in_operands; ++i) {
211 uint32_t id = current_inst->GetSingleWordInOperand(i);
212 Instruction* op_inst = def_use_mgr->GetDef(id);
213
214 if (HasScalarResult(op_inst)) {
215 WorkListItem new_work_item;
216 new_work_item.instruction = op_inst;
217 if (work_item.components.Get(current_component)) {
218 new_work_item.components.Set(0);
219 }
220 AddItemToWorkListIfNeeded(new_work_item, live_components, work_list);
221 current_component++;
222 } else {
223 assert(HasVectorResult(op_inst));
224 WorkListItem new_work_item;
225 new_work_item.instruction = op_inst;
226 uint32_t op_vector_size =
227 type_mgr->GetType(op_inst->type_id())->AsVector()->element_count();
228
229 for (uint32_t op_vector_idx = 0; op_vector_idx < op_vector_size;
230 op_vector_idx++, current_component++) {
231 if (work_item.components.Get(current_component)) {
232 new_work_item.components.Set(op_vector_idx);
233 }
234 }
235 AddItemToWorkListIfNeeded(new_work_item, live_components, work_list);
236 }
237 }
238 }
239
MarkUsesAsLive(Instruction * current_inst,const utils::BitVector & live_elements,LiveComponentMap * live_components,std::vector<VectorDCE::WorkListItem> * work_list)240 void VectorDCE::MarkUsesAsLive(
241 Instruction* current_inst, const utils::BitVector& live_elements,
242 LiveComponentMap* live_components,
243 std::vector<VectorDCE::WorkListItem>* work_list) {
244 analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
245
246 current_inst->ForEachInId([&work_list, &live_elements, this, live_components,
247 def_use_mgr](uint32_t* operand_id) {
248 Instruction* operand_inst = def_use_mgr->GetDef(*operand_id);
249
250 if (HasVectorResult(operand_inst)) {
251 WorkListItem new_item;
252 new_item.instruction = operand_inst;
253 new_item.components = live_elements;
254 AddItemToWorkListIfNeeded(new_item, live_components, work_list);
255 } else if (HasScalarResult(operand_inst)) {
256 WorkListItem new_item;
257 new_item.instruction = operand_inst;
258 new_item.components.Set(0);
259 AddItemToWorkListIfNeeded(new_item, live_components, work_list);
260 }
261 });
262 }
263
HasVectorOrScalarResult(const Instruction * inst) const264 bool VectorDCE::HasVectorOrScalarResult(const Instruction* inst) const {
265 return HasScalarResult(inst) || HasVectorResult(inst);
266 }
267
HasVectorResult(const Instruction * inst) const268 bool VectorDCE::HasVectorResult(const Instruction* inst) const {
269 analysis::TypeManager* type_mgr = context()->get_type_mgr();
270 if (inst->type_id() == 0) {
271 return false;
272 }
273
274 const analysis::Type* current_type = type_mgr->GetType(inst->type_id());
275 switch (current_type->kind()) {
276 case analysis::Type::kVector:
277 return true;
278 default:
279 return false;
280 }
281 }
282
HasScalarResult(const Instruction * inst) const283 bool VectorDCE::HasScalarResult(const Instruction* inst) const {
284 analysis::TypeManager* type_mgr = context()->get_type_mgr();
285 if (inst->type_id() == 0) {
286 return false;
287 }
288
289 const analysis::Type* current_type = type_mgr->GetType(inst->type_id());
290 switch (current_type->kind()) {
291 case analysis::Type::kBool:
292 case analysis::Type::kInteger:
293 case analysis::Type::kFloat:
294 return true;
295 default:
296 return false;
297 }
298 }
299
RewriteInstructions(Function * function,const VectorDCE::LiveComponentMap & live_components)300 bool VectorDCE::RewriteInstructions(
301 Function* function, const VectorDCE::LiveComponentMap& live_components) {
302 bool modified = false;
303
304 // Kill DebugValue in the middle of the instruction iteration will result
305 // in accessing a dangling pointer. We keep dead DebugValue instructions
306 // in |dead_dbg_value| to kill them once after the iteration.
307 std::vector<Instruction*> dead_dbg_value;
308
309 function->ForEachInst([&modified, this, live_components,
310 &dead_dbg_value](Instruction* current_inst) {
311 if (!context()->IsCombinatorInstruction(current_inst)) {
312 return;
313 }
314
315 auto live_component = live_components.find(current_inst->result_id());
316 if (live_component == live_components.end()) {
317 // If this instruction is not in live_components then it does not
318 // produce a vector, or it is never referenced and ADCE will remove
319 // it. No point in trying to differentiate.
320 return;
321 }
322
323 // If no element in the current instruction is used replace it with an
324 // OpUndef.
325 if (live_component->second.Empty()) {
326 modified = true;
327 MarkDebugValueUsesAsDead(current_inst, &dead_dbg_value);
328 uint32_t undef_id = this->Type2Undef(current_inst->type_id());
329 context()->KillNamesAndDecorates(current_inst);
330 context()->ReplaceAllUsesWith(current_inst->result_id(), undef_id);
331 context()->KillInst(current_inst);
332 return;
333 }
334
335 switch (current_inst->opcode()) {
336 case SpvOpCompositeInsert:
337 modified |= RewriteInsertInstruction(
338 current_inst, live_component->second, &dead_dbg_value);
339 break;
340 case SpvOpCompositeConstruct:
341 // TODO: The members that are not live can be replaced by an undef
342 // or constant. This will remove uses of those values, and possibly
343 // create opportunities for ADCE.
344 break;
345 default:
346 // Do nothing.
347 break;
348 }
349 });
350 for (auto* i : dead_dbg_value) context()->KillInst(i);
351 return modified;
352 }
353
RewriteInsertInstruction(Instruction * current_inst,const utils::BitVector & live_components,std::vector<Instruction * > * dead_dbg_value)354 bool VectorDCE::RewriteInsertInstruction(
355 Instruction* current_inst, const utils::BitVector& live_components,
356 std::vector<Instruction*>* dead_dbg_value) {
357 // If the value being inserted is not live, then we can skip the insert.
358
359 if (current_inst->NumInOperands() == 2) {
360 // If there are no indices, then this is the same as a copy.
361 context()->KillNamesAndDecorates(current_inst->result_id());
362 uint32_t object_id =
363 current_inst->GetSingleWordInOperand(kInsertObjectIdInIdx);
364 context()->ReplaceAllUsesWith(current_inst->result_id(), object_id);
365 return true;
366 }
367
368 uint32_t insert_index = current_inst->GetSingleWordInOperand(2);
369 if (!live_components.Get(insert_index)) {
370 MarkDebugValueUsesAsDead(current_inst, dead_dbg_value);
371 context()->KillNamesAndDecorates(current_inst->result_id());
372 uint32_t composite_id =
373 current_inst->GetSingleWordInOperand(kInsertCompositeIdInIdx);
374 context()->ReplaceAllUsesWith(current_inst->result_id(), composite_id);
375 return true;
376 }
377
378 // If the values already in the composite are not used, then replace it with
379 // an undef.
380 utils::BitVector temp = live_components;
381 temp.Clear(insert_index);
382 if (temp.Empty()) {
383 context()->ForgetUses(current_inst);
384 uint32_t undef_id = Type2Undef(current_inst->type_id());
385 current_inst->SetInOperand(kInsertCompositeIdInIdx, {undef_id});
386 context()->AnalyzeUses(current_inst);
387 return true;
388 }
389
390 return false;
391 }
392
MarkDebugValueUsesAsDead(Instruction * composite,std::vector<Instruction * > * dead_dbg_value)393 void VectorDCE::MarkDebugValueUsesAsDead(
394 Instruction* composite, std::vector<Instruction*>* dead_dbg_value) {
395 context()->get_def_use_mgr()->ForEachUser(
396 composite, [&dead_dbg_value](Instruction* use) {
397 if (use->GetOpenCL100DebugOpcode() == OpenCLDebugInfo100DebugValue)
398 dead_dbg_value->push_back(use);
399 });
400 }
401
AddItemToWorkListIfNeeded(WorkListItem work_item,VectorDCE::LiveComponentMap * live_components,std::vector<WorkListItem> * work_list)402 void VectorDCE::AddItemToWorkListIfNeeded(
403 WorkListItem work_item, VectorDCE::LiveComponentMap* live_components,
404 std::vector<WorkListItem>* work_list) {
405 Instruction* current_inst = work_item.instruction;
406 auto it = live_components->find(current_inst->result_id());
407 if (it == live_components->end()) {
408 live_components->emplace(
409 std::make_pair(current_inst->result_id(), work_item.components));
410 work_list->emplace_back(work_item);
411 } else {
412 if (it->second.Or(work_item.components)) {
413 work_list->emplace_back(work_item);
414 }
415 }
416 }
417
418 } // namespace opt
419 } // namespace spvtools
420