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