• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2017 The Khronos Group Inc.
2 // Copyright (c) 2017 Valve Corporation
3 // Copyright (c) 2017 LunarG Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #include "source/opt/mem_pass.h"
18 
19 #include <memory>
20 #include <set>
21 #include <vector>
22 
23 #include "OpenCLDebugInfo100.h"
24 #include "source/cfa.h"
25 #include "source/opt/basic_block.h"
26 #include "source/opt/dominator_analysis.h"
27 #include "source/opt/ir_context.h"
28 #include "source/opt/iterator.h"
29 
30 namespace spvtools {
31 namespace opt {
32 
33 namespace {
34 
35 const uint32_t kCopyObjectOperandInIdx = 0;
36 const uint32_t kTypePointerStorageClassInIdx = 0;
37 const uint32_t kTypePointerTypeIdInIdx = 1;
38 
39 }  // namespace
40 
IsBaseTargetType(const Instruction * typeInst) const41 bool MemPass::IsBaseTargetType(const Instruction* typeInst) const {
42   switch (typeInst->opcode()) {
43     case SpvOpTypeInt:
44     case SpvOpTypeFloat:
45     case SpvOpTypeBool:
46     case SpvOpTypeVector:
47     case SpvOpTypeMatrix:
48     case SpvOpTypeImage:
49     case SpvOpTypeSampler:
50     case SpvOpTypeSampledImage:
51     case SpvOpTypePointer:
52       return true;
53     default:
54       break;
55   }
56   return false;
57 }
58 
IsTargetType(const Instruction * typeInst) const59 bool MemPass::IsTargetType(const Instruction* typeInst) const {
60   if (IsBaseTargetType(typeInst)) return true;
61   if (typeInst->opcode() == SpvOpTypeArray) {
62     if (!IsTargetType(
63             get_def_use_mgr()->GetDef(typeInst->GetSingleWordOperand(1)))) {
64       return false;
65     }
66     return true;
67   }
68   if (typeInst->opcode() != SpvOpTypeStruct) return false;
69   // All struct members must be math type
70   return typeInst->WhileEachInId([this](const uint32_t* tid) {
71     Instruction* compTypeInst = get_def_use_mgr()->GetDef(*tid);
72     if (!IsTargetType(compTypeInst)) return false;
73     return true;
74   });
75 }
76 
IsNonPtrAccessChain(const SpvOp opcode) const77 bool MemPass::IsNonPtrAccessChain(const SpvOp opcode) const {
78   return opcode == SpvOpAccessChain || opcode == SpvOpInBoundsAccessChain;
79 }
80 
IsPtr(uint32_t ptrId)81 bool MemPass::IsPtr(uint32_t ptrId) {
82   uint32_t varId = ptrId;
83   Instruction* ptrInst = get_def_use_mgr()->GetDef(varId);
84   while (ptrInst->opcode() == SpvOpCopyObject) {
85     varId = ptrInst->GetSingleWordInOperand(kCopyObjectOperandInIdx);
86     ptrInst = get_def_use_mgr()->GetDef(varId);
87   }
88   const SpvOp op = ptrInst->opcode();
89   if (op == SpvOpVariable || IsNonPtrAccessChain(op)) return true;
90   if (op != SpvOpFunctionParameter) return false;
91   const uint32_t varTypeId = ptrInst->type_id();
92   const Instruction* varTypeInst = get_def_use_mgr()->GetDef(varTypeId);
93   return varTypeInst->opcode() == SpvOpTypePointer;
94 }
95 
GetPtr(uint32_t ptrId,uint32_t * varId)96 Instruction* MemPass::GetPtr(uint32_t ptrId, uint32_t* varId) {
97   *varId = ptrId;
98   Instruction* ptrInst = get_def_use_mgr()->GetDef(*varId);
99   Instruction* varInst;
100 
101   if (ptrInst->opcode() == SpvOpConstantNull) {
102     *varId = 0;
103     return ptrInst;
104   }
105 
106   if (ptrInst->opcode() != SpvOpVariable &&
107       ptrInst->opcode() != SpvOpFunctionParameter) {
108     varInst = ptrInst->GetBaseAddress();
109   } else {
110     varInst = ptrInst;
111   }
112   if (varInst->opcode() == SpvOpVariable) {
113     *varId = varInst->result_id();
114   } else {
115     *varId = 0;
116   }
117 
118   while (ptrInst->opcode() == SpvOpCopyObject) {
119     uint32_t temp = ptrInst->GetSingleWordInOperand(0);
120     ptrInst = get_def_use_mgr()->GetDef(temp);
121   }
122 
123   return ptrInst;
124 }
125 
GetPtr(Instruction * ip,uint32_t * varId)126 Instruction* MemPass::GetPtr(Instruction* ip, uint32_t* varId) {
127   assert(ip->opcode() == SpvOpStore || ip->opcode() == SpvOpLoad ||
128          ip->opcode() == SpvOpImageTexelPointer || ip->IsAtomicWithLoad());
129 
130   // All of these opcode place the pointer in position 0.
131   const uint32_t ptrId = ip->GetSingleWordInOperand(0);
132   return GetPtr(ptrId, varId);
133 }
134 
HasOnlyNamesAndDecorates(uint32_t id) const135 bool MemPass::HasOnlyNamesAndDecorates(uint32_t id) const {
136   return get_def_use_mgr()->WhileEachUser(id, [this](Instruction* user) {
137     SpvOp op = user->opcode();
138     if (op != SpvOpName && !IsNonTypeDecorate(op)) {
139       return false;
140     }
141     return true;
142   });
143 }
144 
KillAllInsts(BasicBlock * bp,bool killLabel)145 void MemPass::KillAllInsts(BasicBlock* bp, bool killLabel) {
146   bp->KillAllInsts(killLabel);
147 }
148 
HasLoads(uint32_t varId) const149 bool MemPass::HasLoads(uint32_t varId) const {
150   return !get_def_use_mgr()->WhileEachUser(varId, [this](Instruction* user) {
151     SpvOp op = user->opcode();
152     // TODO(): The following is slightly conservative. Could be
153     // better handling of non-store/name.
154     if (IsNonPtrAccessChain(op) || op == SpvOpCopyObject) {
155       if (HasLoads(user->result_id())) {
156         return false;
157       }
158     } else if (op != SpvOpStore && op != SpvOpName && !IsNonTypeDecorate(op)) {
159       return false;
160     }
161     return true;
162   });
163 }
164 
IsLiveVar(uint32_t varId) const165 bool MemPass::IsLiveVar(uint32_t varId) const {
166   const Instruction* varInst = get_def_use_mgr()->GetDef(varId);
167   // assume live if not a variable eg. function parameter
168   if (varInst->opcode() != SpvOpVariable) return true;
169   // non-function scope vars are live
170   const uint32_t varTypeId = varInst->type_id();
171   const Instruction* varTypeInst = get_def_use_mgr()->GetDef(varTypeId);
172   if (varTypeInst->GetSingleWordInOperand(kTypePointerStorageClassInIdx) !=
173       SpvStorageClassFunction)
174     return true;
175   // test if variable is loaded from
176   return HasLoads(varId);
177 }
178 
AddStores(uint32_t ptr_id,std::queue<Instruction * > * insts)179 void MemPass::AddStores(uint32_t ptr_id, std::queue<Instruction*>* insts) {
180   get_def_use_mgr()->ForEachUser(ptr_id, [this, insts](Instruction* user) {
181     SpvOp op = user->opcode();
182     if (IsNonPtrAccessChain(op)) {
183       AddStores(user->result_id(), insts);
184     } else if (op == SpvOpStore) {
185       insts->push(user);
186     }
187   });
188 }
189 
DCEInst(Instruction * inst,const std::function<void (Instruction *)> & call_back)190 void MemPass::DCEInst(Instruction* inst,
191                       const std::function<void(Instruction*)>& call_back) {
192   std::queue<Instruction*> deadInsts;
193   deadInsts.push(inst);
194   while (!deadInsts.empty()) {
195     Instruction* di = deadInsts.front();
196     // Don't delete labels
197     if (di->opcode() == SpvOpLabel) {
198       deadInsts.pop();
199       continue;
200     }
201     // Remember operands
202     std::set<uint32_t> ids;
203     di->ForEachInId([&ids](uint32_t* iid) { ids.insert(*iid); });
204     uint32_t varId = 0;
205     // Remember variable if dead load
206     if (di->opcode() == SpvOpLoad) (void)GetPtr(di, &varId);
207     if (call_back) {
208       call_back(di);
209     }
210     context()->KillInst(di);
211     // For all operands with no remaining uses, add their instruction
212     // to the dead instruction queue.
213     for (auto id : ids)
214       if (HasOnlyNamesAndDecorates(id)) {
215         Instruction* odi = get_def_use_mgr()->GetDef(id);
216         if (context()->IsCombinatorInstruction(odi)) deadInsts.push(odi);
217       }
218     // if a load was deleted and it was the variable's
219     // last load, add all its stores to dead queue
220     if (varId != 0 && !IsLiveVar(varId)) AddStores(varId, &deadInsts);
221     deadInsts.pop();
222   }
223 }
224 
MemPass()225 MemPass::MemPass() {}
226 
HasOnlySupportedRefs(uint32_t varId)227 bool MemPass::HasOnlySupportedRefs(uint32_t varId) {
228   return get_def_use_mgr()->WhileEachUser(varId, [this](Instruction* user) {
229     auto dbg_op = user->GetOpenCL100DebugOpcode();
230     if (dbg_op == OpenCLDebugInfo100DebugDeclare ||
231         dbg_op == OpenCLDebugInfo100DebugValue) {
232       return true;
233     }
234     SpvOp op = user->opcode();
235     if (op != SpvOpStore && op != SpvOpLoad && op != SpvOpName &&
236         !IsNonTypeDecorate(op)) {
237       return false;
238     }
239     return true;
240   });
241 }
242 
Type2Undef(uint32_t type_id)243 uint32_t MemPass::Type2Undef(uint32_t type_id) {
244   const auto uitr = type2undefs_.find(type_id);
245   if (uitr != type2undefs_.end()) return uitr->second;
246   const uint32_t undefId = TakeNextId();
247   if (undefId == 0) {
248     return 0;
249   }
250 
251   std::unique_ptr<Instruction> undef_inst(
252       new Instruction(context(), SpvOpUndef, type_id, undefId, {}));
253   get_def_use_mgr()->AnalyzeInstDefUse(&*undef_inst);
254   get_module()->AddGlobalValue(std::move(undef_inst));
255   type2undefs_[type_id] = undefId;
256   return undefId;
257 }
258 
IsTargetVar(uint32_t varId)259 bool MemPass::IsTargetVar(uint32_t varId) {
260   if (varId == 0) {
261     return false;
262   }
263 
264   if (seen_non_target_vars_.find(varId) != seen_non_target_vars_.end())
265     return false;
266   if (seen_target_vars_.find(varId) != seen_target_vars_.end()) return true;
267   const Instruction* varInst = get_def_use_mgr()->GetDef(varId);
268   if (varInst->opcode() != SpvOpVariable) return false;
269   const uint32_t varTypeId = varInst->type_id();
270   const Instruction* varTypeInst = get_def_use_mgr()->GetDef(varTypeId);
271   if (varTypeInst->GetSingleWordInOperand(kTypePointerStorageClassInIdx) !=
272       SpvStorageClassFunction) {
273     seen_non_target_vars_.insert(varId);
274     return false;
275   }
276   const uint32_t varPteTypeId =
277       varTypeInst->GetSingleWordInOperand(kTypePointerTypeIdInIdx);
278   Instruction* varPteTypeInst = get_def_use_mgr()->GetDef(varPteTypeId);
279   if (!IsTargetType(varPteTypeInst)) {
280     seen_non_target_vars_.insert(varId);
281     return false;
282   }
283   seen_target_vars_.insert(varId);
284   return true;
285 }
286 
287 // Remove all |phi| operands coming from unreachable blocks (i.e., blocks not in
288 // |reachable_blocks|).  There are two types of removal that this function can
289 // perform:
290 //
291 // 1- Any operand that comes directly from an unreachable block is completely
292 //    removed.  Since the block is unreachable, the edge between the unreachable
293 //    block and the block holding |phi| has been removed.
294 //
295 // 2- Any operand that comes via a live block and was defined at an unreachable
296 //    block gets its value replaced with an OpUndef value. Since the argument
297 //    was generated in an unreachable block, it no longer exists, so it cannot
298 //    be referenced.  However, since the value does not reach |phi| directly
299 //    from the unreachable block, the operand cannot be removed from |phi|.
300 //    Therefore, we replace the argument value with OpUndef.
301 //
302 // For example, in the switch() below, assume that we want to remove the
303 // argument with value %11 coming from block %41.
304 //
305 //          [ ... ]
306 //          %41 = OpLabel                    <--- Unreachable block
307 //          %11 = OpLoad %int %y
308 //          [ ... ]
309 //                OpSelectionMerge %16 None
310 //                OpSwitch %12 %16 10 %13 13 %14 18 %15
311 //          %13 = OpLabel
312 //                OpBranch %16
313 //          %14 = OpLabel
314 //                OpStore %outparm %int_14
315 //                OpBranch %16
316 //          %15 = OpLabel
317 //                OpStore %outparm %int_15
318 //                OpBranch %16
319 //          %16 = OpLabel
320 //          %30 = OpPhi %int %11 %41 %int_42 %13 %11 %14 %11 %15
321 //
322 // Since %41 is now an unreachable block, the first operand of |phi| needs to
323 // be removed completely.  But the operands (%11 %14) and (%11 %15) cannot be
324 // removed because %14 and %15 are reachable blocks.  Since %11 no longer exist,
325 // in those arguments, we replace all references to %11 with an OpUndef value.
326 // This results in |phi| looking like:
327 //
328 //           %50 = OpUndef %int
329 //           [ ... ]
330 //           %30 = OpPhi %int %int_42 %13 %50 %14 %50 %15
RemovePhiOperands(Instruction * phi,const std::unordered_set<BasicBlock * > & reachable_blocks)331 void MemPass::RemovePhiOperands(
332     Instruction* phi, const std::unordered_set<BasicBlock*>& reachable_blocks) {
333   std::vector<Operand> keep_operands;
334   uint32_t type_id = 0;
335   // The id of an undefined value we've generated.
336   uint32_t undef_id = 0;
337 
338   // Traverse all the operands in |phi|. Build the new operand vector by adding
339   // all the original operands from |phi| except the unwanted ones.
340   for (uint32_t i = 0; i < phi->NumOperands();) {
341     if (i < 2) {
342       // The first two arguments are always preserved.
343       keep_operands.push_back(phi->GetOperand(i));
344       ++i;
345       continue;
346     }
347 
348     // The remaining Phi arguments come in pairs. Index 'i' contains the
349     // variable id, index 'i + 1' is the originating block id.
350     assert(i % 2 == 0 && i < phi->NumOperands() - 1 &&
351            "malformed Phi arguments");
352 
353     BasicBlock* in_block = cfg()->block(phi->GetSingleWordOperand(i + 1));
354     if (reachable_blocks.find(in_block) == reachable_blocks.end()) {
355       // If the incoming block is unreachable, remove both operands as this
356       // means that the |phi| has lost an incoming edge.
357       i += 2;
358       continue;
359     }
360 
361     // In all other cases, the operand must be kept but may need to be changed.
362     uint32_t arg_id = phi->GetSingleWordOperand(i);
363     Instruction* arg_def_instr = get_def_use_mgr()->GetDef(arg_id);
364     BasicBlock* def_block = context()->get_instr_block(arg_def_instr);
365     if (def_block &&
366         reachable_blocks.find(def_block) == reachable_blocks.end()) {
367       // If the current |phi| argument was defined in an unreachable block, it
368       // means that this |phi| argument is no longer defined. Replace it with
369       // |undef_id|.
370       if (!undef_id) {
371         type_id = arg_def_instr->type_id();
372         undef_id = Type2Undef(type_id);
373       }
374       keep_operands.push_back(
375           Operand(spv_operand_type_t::SPV_OPERAND_TYPE_ID, {undef_id}));
376     } else {
377       // Otherwise, the argument comes from a reachable block or from no block
378       // at all (meaning that it was defined in the global section of the
379       // program).  In both cases, keep the argument intact.
380       keep_operands.push_back(phi->GetOperand(i));
381     }
382 
383     keep_operands.push_back(phi->GetOperand(i + 1));
384 
385     i += 2;
386   }
387 
388   context()->ForgetUses(phi);
389   phi->ReplaceOperands(keep_operands);
390   context()->AnalyzeUses(phi);
391 }
392 
RemoveBlock(Function::iterator * bi)393 void MemPass::RemoveBlock(Function::iterator* bi) {
394   auto& rm_block = **bi;
395 
396   // Remove instructions from the block.
397   rm_block.ForEachInst([&rm_block, this](Instruction* inst) {
398     // Note that we do not kill the block label instruction here. The label
399     // instruction is needed to identify the block, which is needed by the
400     // removal of phi operands.
401     if (inst != rm_block.GetLabelInst()) {
402       context()->KillInst(inst);
403     }
404   });
405 
406   // Remove the label instruction last.
407   auto label = rm_block.GetLabelInst();
408   context()->KillInst(label);
409 
410   *bi = bi->Erase();
411 }
412 
RemoveUnreachableBlocks(Function * func)413 bool MemPass::RemoveUnreachableBlocks(Function* func) {
414   bool modified = false;
415 
416   // Mark reachable all blocks reachable from the function's entry block.
417   std::unordered_set<BasicBlock*> reachable_blocks;
418   std::unordered_set<BasicBlock*> visited_blocks;
419   std::queue<BasicBlock*> worklist;
420   reachable_blocks.insert(func->entry().get());
421 
422   // Initially mark the function entry point as reachable.
423   worklist.push(func->entry().get());
424 
425   auto mark_reachable = [&reachable_blocks, &visited_blocks, &worklist,
426                          this](uint32_t label_id) {
427     auto successor = cfg()->block(label_id);
428     if (visited_blocks.count(successor) == 0) {
429       reachable_blocks.insert(successor);
430       worklist.push(successor);
431       visited_blocks.insert(successor);
432     }
433   };
434 
435   // Transitively mark all blocks reachable from the entry as reachable.
436   while (!worklist.empty()) {
437     BasicBlock* block = worklist.front();
438     worklist.pop();
439 
440     // All the successors of a live block are also live.
441     static_cast<const BasicBlock*>(block)->ForEachSuccessorLabel(
442         mark_reachable);
443 
444     // All the Merge and ContinueTarget blocks of a live block are also live.
445     block->ForMergeAndContinueLabel(mark_reachable);
446   }
447 
448   // Update operands of Phi nodes that reference unreachable blocks.
449   for (auto& block : *func) {
450     // If the block is about to be removed, don't bother updating its
451     // Phi instructions.
452     if (reachable_blocks.count(&block) == 0) {
453       continue;
454     }
455 
456     // If the block is reachable and has Phi instructions, remove all
457     // operands from its Phi instructions that reference unreachable blocks.
458     // If the block has no Phi instructions, this is a no-op.
459     block.ForEachPhiInst([&reachable_blocks, this](Instruction* phi) {
460       RemovePhiOperands(phi, reachable_blocks);
461     });
462   }
463 
464   // Erase unreachable blocks.
465   for (auto ebi = func->begin(); ebi != func->end();) {
466     if (reachable_blocks.count(&*ebi) == 0) {
467       RemoveBlock(&ebi);
468       modified = true;
469     } else {
470       ++ebi;
471     }
472   }
473 
474   return modified;
475 }
476 
CFGCleanup(Function * func)477 bool MemPass::CFGCleanup(Function* func) {
478   bool modified = false;
479   modified |= RemoveUnreachableBlocks(func);
480   return modified;
481 }
482 
CollectTargetVars(Function * func)483 void MemPass::CollectTargetVars(Function* func) {
484   seen_target_vars_.clear();
485   seen_non_target_vars_.clear();
486   type2undefs_.clear();
487 
488   // Collect target (and non-) variable sets. Remove variables with
489   // non-load/store refs from target variable set
490   for (auto& blk : *func) {
491     for (auto& inst : blk) {
492       switch (inst.opcode()) {
493         case SpvOpStore:
494         case SpvOpLoad: {
495           uint32_t varId;
496           (void)GetPtr(&inst, &varId);
497           if (!IsTargetVar(varId)) break;
498           if (HasOnlySupportedRefs(varId)) break;
499           seen_non_target_vars_.insert(varId);
500           seen_target_vars_.erase(varId);
501         } break;
502         default:
503           break;
504       }
505     }
506   }
507 }
508 
509 }  // namespace opt
510 }  // namespace spvtools
511