1 // Copyright (c) 2017 The Khronos Group Inc.
2 // Copyright (c) 2017 Valve Corporation
3 // Copyright (c) 2017 LunarG Inc.
4 // Copyright (c) 2018 Google LLC
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 //
10 // http://www.apache.org/licenses/LICENSE-2.0
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17
18 #include "source/opt/aggressive_dead_code_elim_pass.h"
19
20 #include <memory>
21 #include <stack>
22
23 #include "source/cfa.h"
24 #include "source/latest_version_glsl_std_450_header.h"
25 #include "source/opt/eliminate_dead_functions_util.h"
26 #include "source/opt/iterator.h"
27 #include "source/opt/reflect.h"
28 #include "source/spirv_constant.h"
29
30 namespace spvtools {
31 namespace opt {
32
33 namespace {
34
35 const uint32_t kTypePointerStorageClassInIdx = 0;
36 const uint32_t kEntryPointFunctionIdInIdx = 1;
37 const uint32_t kSelectionMergeMergeBlockIdInIdx = 0;
38 const uint32_t kLoopMergeMergeBlockIdInIdx = 0;
39 const uint32_t kLoopMergeContinueBlockIdInIdx = 1;
40 const uint32_t kCopyMemoryTargetAddrInIdx = 0;
41 const uint32_t kCopyMemorySourceAddrInIdx = 1;
42 const uint32_t kDebugDeclareOperandVariableIndex = 5;
43 const uint32_t kGlobalVariableVariableIndex = 12;
44
45 // Sorting functor to present annotation instructions in an easy-to-process
46 // order. The functor orders by opcode first and falls back on unique id
47 // ordering if both instructions have the same opcode.
48 //
49 // Desired priority:
50 // SpvOpGroupDecorate
51 // SpvOpGroupMemberDecorate
52 // SpvOpDecorate
53 // SpvOpMemberDecorate
54 // SpvOpDecorateId
55 // SpvOpDecorateStringGOOGLE
56 // SpvOpDecorationGroup
57 struct DecorationLess {
operator ()spvtools::opt::__anonb71c566b0111::DecorationLess58 bool operator()(const Instruction* lhs, const Instruction* rhs) const {
59 assert(lhs && rhs);
60 SpvOp lhsOp = lhs->opcode();
61 SpvOp rhsOp = rhs->opcode();
62 if (lhsOp != rhsOp) {
63 #define PRIORITY_CASE(opcode) \
64 if (lhsOp == opcode && rhsOp != opcode) return true; \
65 if (rhsOp == opcode && lhsOp != opcode) return false;
66 // OpGroupDecorate and OpGroupMember decorate are highest priority to
67 // eliminate dead targets early and simplify subsequent checks.
68 PRIORITY_CASE(SpvOpGroupDecorate)
69 PRIORITY_CASE(SpvOpGroupMemberDecorate)
70 PRIORITY_CASE(SpvOpDecorate)
71 PRIORITY_CASE(SpvOpMemberDecorate)
72 PRIORITY_CASE(SpvOpDecorateId)
73 PRIORITY_CASE(SpvOpDecorateStringGOOGLE)
74 // OpDecorationGroup is lowest priority to ensure use/def chains remain
75 // usable for instructions that target this group.
76 PRIORITY_CASE(SpvOpDecorationGroup)
77 #undef PRIORITY_CASE
78 }
79
80 // Fall back to maintain total ordering (compare unique ids).
81 return *lhs < *rhs;
82 }
83 };
84
85 } // namespace
86
IsVarOfStorage(uint32_t varId,uint32_t storageClass)87 bool AggressiveDCEPass::IsVarOfStorage(uint32_t varId, uint32_t storageClass) {
88 if (varId == 0) return false;
89 const Instruction* varInst = get_def_use_mgr()->GetDef(varId);
90 const SpvOp op = varInst->opcode();
91 if (op != SpvOpVariable) return false;
92 const uint32_t varTypeId = varInst->type_id();
93 const Instruction* varTypeInst = get_def_use_mgr()->GetDef(varTypeId);
94 if (varTypeInst->opcode() != SpvOpTypePointer) return false;
95 return varTypeInst->GetSingleWordInOperand(kTypePointerStorageClassInIdx) ==
96 storageClass;
97 }
98
IsLocalVar(uint32_t varId)99 bool AggressiveDCEPass::IsLocalVar(uint32_t varId) {
100 if (IsVarOfStorage(varId, SpvStorageClassFunction)) {
101 return true;
102 }
103 if (!private_like_local_) {
104 return false;
105 }
106
107 return IsVarOfStorage(varId, SpvStorageClassPrivate) ||
108 IsVarOfStorage(varId, SpvStorageClassWorkgroup);
109 }
110
AddStores(Function * func,uint32_t ptrId)111 void AggressiveDCEPass::AddStores(Function* func, uint32_t ptrId) {
112 get_def_use_mgr()->ForEachUser(ptrId, [this, ptrId, func](Instruction* user) {
113 // If the user is not a part of |func|, skip it.
114 BasicBlock* blk = context()->get_instr_block(user);
115 if (blk && blk->GetParent() != func) return;
116
117 switch (user->opcode()) {
118 case SpvOpAccessChain:
119 case SpvOpInBoundsAccessChain:
120 case SpvOpCopyObject:
121 this->AddStores(func, user->result_id());
122 break;
123 case SpvOpLoad:
124 break;
125 case SpvOpCopyMemory:
126 case SpvOpCopyMemorySized:
127 if (user->GetSingleWordInOperand(kCopyMemoryTargetAddrInIdx) == ptrId) {
128 AddToWorklist(user);
129 }
130 break;
131 // If default, assume it stores e.g. frexp, modf, function call
132 case SpvOpStore:
133 default:
134 AddToWorklist(user);
135 break;
136 }
137 });
138 }
139
AllExtensionsSupported() const140 bool AggressiveDCEPass::AllExtensionsSupported() const {
141 // If any extension not in allowlist, return false
142 for (auto& ei : get_module()->extensions()) {
143 const char* extName =
144 reinterpret_cast<const char*>(&ei.GetInOperand(0).words[0]);
145 if (extensions_allowlist_.find(extName) == extensions_allowlist_.end())
146 return false;
147 }
148 return true;
149 }
150
IsDead(Instruction * inst)151 bool AggressiveDCEPass::IsDead(Instruction* inst) {
152 if (IsLive(inst)) return false;
153 if ((inst->IsBranch() || inst->opcode() == SpvOpUnreachable) &&
154 !IsStructuredHeader(context()->get_instr_block(inst), nullptr, nullptr,
155 nullptr))
156 return false;
157 return true;
158 }
159
IsTargetDead(Instruction * inst)160 bool AggressiveDCEPass::IsTargetDead(Instruction* inst) {
161 const uint32_t tId = inst->GetSingleWordInOperand(0);
162 Instruction* tInst = get_def_use_mgr()->GetDef(tId);
163 if (IsAnnotationInst(tInst->opcode())) {
164 // This must be a decoration group. We go through annotations in a specific
165 // order. So if this is not used by any group or group member decorates, it
166 // is dead.
167 assert(tInst->opcode() == SpvOpDecorationGroup);
168 bool dead = true;
169 get_def_use_mgr()->ForEachUser(tInst, [&dead](Instruction* user) {
170 if (user->opcode() == SpvOpGroupDecorate ||
171 user->opcode() == SpvOpGroupMemberDecorate)
172 dead = false;
173 });
174 return dead;
175 }
176 return IsDead(tInst);
177 }
178
ProcessLoad(Function * func,uint32_t varId)179 void AggressiveDCEPass::ProcessLoad(Function* func, uint32_t varId) {
180 // Only process locals
181 if (!IsLocalVar(varId)) return;
182 // Return if already processed
183 if (live_local_vars_.find(varId) != live_local_vars_.end()) return;
184 // Mark all stores to varId as live
185 AddStores(func, varId);
186 // Cache varId as processed
187 live_local_vars_.insert(varId);
188 }
189
IsStructuredHeader(BasicBlock * bp,Instruction ** mergeInst,Instruction ** branchInst,uint32_t * mergeBlockId)190 bool AggressiveDCEPass::IsStructuredHeader(BasicBlock* bp,
191 Instruction** mergeInst,
192 Instruction** branchInst,
193 uint32_t* mergeBlockId) {
194 if (!bp) return false;
195 Instruction* mi = bp->GetMergeInst();
196 if (mi == nullptr) return false;
197 Instruction* bri = &*bp->tail();
198 if (branchInst != nullptr) *branchInst = bri;
199 if (mergeInst != nullptr) *mergeInst = mi;
200 if (mergeBlockId != nullptr) *mergeBlockId = mi->GetSingleWordInOperand(0);
201 return true;
202 }
203
ComputeBlock2HeaderMaps(std::list<BasicBlock * > & structuredOrder)204 void AggressiveDCEPass::ComputeBlock2HeaderMaps(
205 std::list<BasicBlock*>& structuredOrder) {
206 block2headerBranch_.clear();
207 header2nextHeaderBranch_.clear();
208 branch2merge_.clear();
209 structured_order_index_.clear();
210 std::stack<Instruction*> currentHeaderBranch;
211 currentHeaderBranch.push(nullptr);
212 uint32_t currentMergeBlockId = 0;
213 uint32_t index = 0;
214 for (auto bi = structuredOrder.begin(); bi != structuredOrder.end();
215 ++bi, ++index) {
216 structured_order_index_[*bi] = index;
217 // If this block is the merge block of the current control construct,
218 // we are leaving the current construct so we must update state
219 if ((*bi)->id() == currentMergeBlockId) {
220 currentHeaderBranch.pop();
221 Instruction* chb = currentHeaderBranch.top();
222 if (chb != nullptr)
223 currentMergeBlockId = branch2merge_[chb]->GetSingleWordInOperand(0);
224 }
225 Instruction* mergeInst;
226 Instruction* branchInst;
227 uint32_t mergeBlockId;
228 bool is_header =
229 IsStructuredHeader(*bi, &mergeInst, &branchInst, &mergeBlockId);
230 // Map header block to next enclosing header.
231 if (is_header) header2nextHeaderBranch_[*bi] = currentHeaderBranch.top();
232 // If this is a loop header, update state first so the block will map to
233 // itself.
234 if (is_header && mergeInst->opcode() == SpvOpLoopMerge) {
235 currentHeaderBranch.push(branchInst);
236 branch2merge_[branchInst] = mergeInst;
237 currentMergeBlockId = mergeBlockId;
238 }
239 // Map the block to the current construct.
240 block2headerBranch_[*bi] = currentHeaderBranch.top();
241 // If this is an if header, update state so following blocks map to the if.
242 if (is_header && mergeInst->opcode() == SpvOpSelectionMerge) {
243 currentHeaderBranch.push(branchInst);
244 branch2merge_[branchInst] = mergeInst;
245 currentMergeBlockId = mergeBlockId;
246 }
247 }
248 }
249
AddBranch(uint32_t labelId,BasicBlock * bp)250 void AggressiveDCEPass::AddBranch(uint32_t labelId, BasicBlock* bp) {
251 std::unique_ptr<Instruction> newBranch(
252 new Instruction(context(), SpvOpBranch, 0, 0,
253 {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {labelId}}}));
254 context()->AnalyzeDefUse(&*newBranch);
255 context()->set_instr_block(&*newBranch, bp);
256 bp->AddInstruction(std::move(newBranch));
257 }
258
AddBreaksAndContinuesToWorklist(Instruction * mergeInst)259 void AggressiveDCEPass::AddBreaksAndContinuesToWorklist(
260 Instruction* mergeInst) {
261 assert(mergeInst->opcode() == SpvOpSelectionMerge ||
262 mergeInst->opcode() == SpvOpLoopMerge);
263
264 BasicBlock* header = context()->get_instr_block(mergeInst);
265 uint32_t headerIndex = structured_order_index_[header];
266 const uint32_t mergeId = mergeInst->GetSingleWordInOperand(0);
267 BasicBlock* merge = context()->get_instr_block(mergeId);
268 uint32_t mergeIndex = structured_order_index_[merge];
269 get_def_use_mgr()->ForEachUser(
270 mergeId, [headerIndex, mergeIndex, this](Instruction* user) {
271 if (!user->IsBranch()) return;
272 BasicBlock* block = context()->get_instr_block(user);
273 uint32_t index = structured_order_index_[block];
274 if (headerIndex < index && index < mergeIndex) {
275 // This is a break from the loop.
276 AddToWorklist(user);
277 // Add branch's merge if there is one.
278 Instruction* userMerge = branch2merge_[user];
279 if (userMerge != nullptr) AddToWorklist(userMerge);
280 }
281 });
282
283 if (mergeInst->opcode() != SpvOpLoopMerge) {
284 return;
285 }
286
287 // For loops we need to find the continues as well.
288 const uint32_t contId =
289 mergeInst->GetSingleWordInOperand(kLoopMergeContinueBlockIdInIdx);
290 get_def_use_mgr()->ForEachUser(contId, [&contId, this](Instruction* user) {
291 SpvOp op = user->opcode();
292 if (op == SpvOpBranchConditional || op == SpvOpSwitch) {
293 // A conditional branch or switch can only be a continue if it does not
294 // have a merge instruction or its merge block is not the continue block.
295 Instruction* hdrMerge = branch2merge_[user];
296 if (hdrMerge != nullptr && hdrMerge->opcode() == SpvOpSelectionMerge) {
297 uint32_t hdrMergeId =
298 hdrMerge->GetSingleWordInOperand(kSelectionMergeMergeBlockIdInIdx);
299 if (hdrMergeId == contId) return;
300 // Need to mark merge instruction too
301 AddToWorklist(hdrMerge);
302 }
303 } else if (op == SpvOpBranch) {
304 // An unconditional branch can only be a continue if it is not
305 // branching to its own merge block.
306 BasicBlock* blk = context()->get_instr_block(user);
307 Instruction* hdrBranch = block2headerBranch_[blk];
308 if (hdrBranch == nullptr) return;
309 Instruction* hdrMerge = branch2merge_[hdrBranch];
310 if (hdrMerge->opcode() == SpvOpLoopMerge) return;
311 uint32_t hdrMergeId =
312 hdrMerge->GetSingleWordInOperand(kSelectionMergeMergeBlockIdInIdx);
313 if (contId == hdrMergeId) return;
314 } else {
315 return;
316 }
317 AddToWorklist(user);
318 });
319 }
320
AggressiveDCE(Function * func)321 bool AggressiveDCEPass::AggressiveDCE(Function* func) {
322 // Mark function parameters as live.
323 AddToWorklist(&func->DefInst());
324 func->ForEachParam(
325 [this](const Instruction* param) {
326 AddToWorklist(const_cast<Instruction*>(param));
327 },
328 false);
329
330 // Compute map from block to controlling conditional branch
331 std::list<BasicBlock*> structuredOrder;
332 cfg()->ComputeStructuredOrder(func, &*func->begin(), &structuredOrder);
333 ComputeBlock2HeaderMaps(structuredOrder);
334 bool modified = false;
335 // Add instructions with external side effects to worklist. Also add branches
336 // EXCEPT those immediately contained in an "if" selection construct or a loop
337 // or continue construct.
338 // TODO(greg-lunarg): Handle Frexp, Modf more optimally
339 call_in_func_ = false;
340 func_is_entry_point_ = false;
341 private_stores_.clear();
342 live_local_vars_.clear();
343 // Stacks to keep track of when we are inside an if- or loop-construct.
344 // When immediately inside an if- or loop-construct, we do not initially
345 // mark branches live. All other branches must be marked live.
346 std::stack<bool> assume_branches_live;
347 std::stack<uint32_t> currentMergeBlockId;
348 // Push sentinel values on stack for when outside of any control flow.
349 assume_branches_live.push(true);
350 currentMergeBlockId.push(0);
351 for (auto bi = structuredOrder.begin(); bi != structuredOrder.end(); ++bi) {
352 // If exiting if or loop, update stacks
353 if ((*bi)->id() == currentMergeBlockId.top()) {
354 assume_branches_live.pop();
355 currentMergeBlockId.pop();
356 }
357 for (auto ii = (*bi)->begin(); ii != (*bi)->end(); ++ii) {
358 SpvOp op = ii->opcode();
359 switch (op) {
360 case SpvOpStore: {
361 uint32_t varId;
362 (void)GetPtr(&*ii, &varId);
363 // Mark stores as live if their variable is not function scope
364 // and is not private scope. Remember private stores for possible
365 // later inclusion. We cannot call IsLocalVar at this point because
366 // private_like_local_ has not been set yet.
367 if (IsVarOfStorage(varId, SpvStorageClassPrivate) ||
368 IsVarOfStorage(varId, SpvStorageClassWorkgroup))
369 private_stores_.push_back(&*ii);
370 else if (!IsVarOfStorage(varId, SpvStorageClassFunction))
371 AddToWorklist(&*ii);
372 } break;
373 case SpvOpCopyMemory:
374 case SpvOpCopyMemorySized: {
375 uint32_t varId;
376 (void)GetPtr(ii->GetSingleWordInOperand(kCopyMemoryTargetAddrInIdx),
377 &varId);
378 if (IsVarOfStorage(varId, SpvStorageClassPrivate) ||
379 IsVarOfStorage(varId, SpvStorageClassWorkgroup))
380 private_stores_.push_back(&*ii);
381 else if (!IsVarOfStorage(varId, SpvStorageClassFunction))
382 AddToWorklist(&*ii);
383 } break;
384 case SpvOpLoopMerge: {
385 assume_branches_live.push(false);
386 currentMergeBlockId.push(
387 ii->GetSingleWordInOperand(kLoopMergeMergeBlockIdInIdx));
388 } break;
389 case SpvOpSelectionMerge: {
390 assume_branches_live.push(false);
391 currentMergeBlockId.push(
392 ii->GetSingleWordInOperand(kSelectionMergeMergeBlockIdInIdx));
393 } break;
394 case SpvOpSwitch:
395 case SpvOpBranch:
396 case SpvOpBranchConditional:
397 case SpvOpUnreachable: {
398 if (assume_branches_live.top()) {
399 AddToWorklist(&*ii);
400 }
401 } break;
402 default: {
403 // Function calls, atomics, function params, function returns, etc.
404 // TODO(greg-lunarg): function calls live only if write to non-local
405 if (!ii->IsOpcodeSafeToDelete()) {
406 AddToWorklist(&*ii);
407 }
408 // Remember function calls
409 if (op == SpvOpFunctionCall) call_in_func_ = true;
410 } break;
411 }
412 }
413 }
414 // See if current function is an entry point
415 for (auto& ei : get_module()->entry_points()) {
416 if (ei.GetSingleWordInOperand(kEntryPointFunctionIdInIdx) ==
417 func->result_id()) {
418 func_is_entry_point_ = true;
419 break;
420 }
421 }
422 // If the current function is an entry point and has no function calls,
423 // we can optimize private variables as locals
424 private_like_local_ = func_is_entry_point_ && !call_in_func_;
425 // If privates are not like local, add their stores to worklist
426 if (!private_like_local_)
427 for (auto& ps : private_stores_) AddToWorklist(ps);
428 // Perform closure on live instruction set.
429 while (!worklist_.empty()) {
430 Instruction* liveInst = worklist_.front();
431 // Add all operand instructions if not already live
432 liveInst->ForEachInId([&liveInst, this](const uint32_t* iid) {
433 Instruction* inInst = get_def_use_mgr()->GetDef(*iid);
434 // Do not add label if an operand of a branch. This is not needed
435 // as part of live code discovery and can create false live code,
436 // for example, the branch to a header of a loop.
437 if (inInst->opcode() == SpvOpLabel && liveInst->IsBranch()) return;
438 AddToWorklist(inInst);
439 });
440 if (liveInst->type_id() != 0) {
441 AddToWorklist(get_def_use_mgr()->GetDef(liveInst->type_id()));
442 }
443 // If in a structured if or loop construct, add the controlling
444 // conditional branch and its merge.
445 BasicBlock* blk = context()->get_instr_block(liveInst);
446 Instruction* branchInst = block2headerBranch_[blk];
447 if (branchInst != nullptr) {
448 AddToWorklist(branchInst);
449 Instruction* mergeInst = branch2merge_[branchInst];
450 AddToWorklist(mergeInst);
451 }
452 // If the block is a header, add the next outermost controlling
453 // conditional branch and its merge.
454 Instruction* nextBranchInst = header2nextHeaderBranch_[blk];
455 if (nextBranchInst != nullptr) {
456 AddToWorklist(nextBranchInst);
457 Instruction* mergeInst = branch2merge_[nextBranchInst];
458 AddToWorklist(mergeInst);
459 }
460 // If local load, add all variable's stores if variable not already live
461 if (liveInst->opcode() == SpvOpLoad || liveInst->IsAtomicWithLoad()) {
462 uint32_t varId;
463 (void)GetPtr(liveInst, &varId);
464 if (varId != 0) {
465 ProcessLoad(func, varId);
466 }
467 // Process memory copies like loads
468 } else if (liveInst->opcode() == SpvOpCopyMemory ||
469 liveInst->opcode() == SpvOpCopyMemorySized) {
470 uint32_t varId;
471 (void)GetPtr(liveInst->GetSingleWordInOperand(kCopyMemorySourceAddrInIdx),
472 &varId);
473 if (varId != 0) {
474 ProcessLoad(func, varId);
475 }
476 // If DebugDeclare, process as load of variable
477 } else if (liveInst->GetOpenCL100DebugOpcode() ==
478 OpenCLDebugInfo100DebugDeclare) {
479 uint32_t varId =
480 liveInst->GetSingleWordOperand(kDebugDeclareOperandVariableIndex);
481 ProcessLoad(func, varId);
482 // If DebugValue with Deref, process as load of variable
483 } else if (liveInst->GetOpenCL100DebugOpcode() ==
484 OpenCLDebugInfo100DebugValue) {
485 uint32_t varId = context()
486 ->get_debug_info_mgr()
487 ->GetVariableIdOfDebugValueUsedForDeclare(liveInst);
488 if (varId != 0) ProcessLoad(func, varId);
489 // If merge, add other branches that are part of its control structure
490 } else if (liveInst->opcode() == SpvOpLoopMerge ||
491 liveInst->opcode() == SpvOpSelectionMerge) {
492 AddBreaksAndContinuesToWorklist(liveInst);
493 // If function call, treat as if it loads from all pointer arguments
494 } else if (liveInst->opcode() == SpvOpFunctionCall) {
495 liveInst->ForEachInId([this, func](const uint32_t* iid) {
496 // Skip non-ptr args
497 if (!IsPtr(*iid)) return;
498 uint32_t varId;
499 (void)GetPtr(*iid, &varId);
500 ProcessLoad(func, varId);
501 });
502 // If function parameter, treat as if it's result id is loaded from
503 } else if (liveInst->opcode() == SpvOpFunctionParameter) {
504 ProcessLoad(func, liveInst->result_id());
505 // We treat an OpImageTexelPointer as a load of the pointer, and
506 // that value is manipulated to get the result.
507 } else if (liveInst->opcode() == SpvOpImageTexelPointer) {
508 uint32_t varId;
509 (void)GetPtr(liveInst, &varId);
510 if (varId != 0) {
511 ProcessLoad(func, varId);
512 }
513 }
514
515 // Add OpDecorateId instructions that apply to this instruction to the work
516 // list. We use the decoration manager to look through the group
517 // decorations to get to the OpDecorate* instructions themselves.
518 auto decorations =
519 get_decoration_mgr()->GetDecorationsFor(liveInst->result_id(), false);
520 for (Instruction* dec : decorations) {
521 // We only care about OpDecorateId instructions because the are the only
522 // decorations that will reference an id that will have to be kept live
523 // because of that use.
524 if (dec->opcode() != SpvOpDecorateId) {
525 continue;
526 }
527 if (dec->GetSingleWordInOperand(1) ==
528 SpvDecorationHlslCounterBufferGOOGLE) {
529 // These decorations should not force the use id to be live. It will be
530 // removed if either the target or the in operand are dead.
531 continue;
532 }
533 AddToWorklist(dec);
534 }
535
536 // Add DebugScope and DebugInlinedAt for |liveInst| to the work list.
537 if (liveInst->GetDebugScope().GetLexicalScope() != kNoDebugScope) {
538 auto* scope = get_def_use_mgr()->GetDef(
539 liveInst->GetDebugScope().GetLexicalScope());
540 AddToWorklist(scope);
541 }
542 if (liveInst->GetDebugInlinedAt() != kNoInlinedAt) {
543 auto* inlined_at =
544 get_def_use_mgr()->GetDef(liveInst->GetDebugInlinedAt());
545 AddToWorklist(inlined_at);
546 }
547 worklist_.pop();
548 }
549
550 // Kill dead instructions and remember dead blocks
551 for (auto bi = structuredOrder.begin(); bi != structuredOrder.end();) {
552 uint32_t mergeBlockId = 0;
553 (*bi)->ForEachInst([this, &modified, &mergeBlockId](Instruction* inst) {
554 if (!IsDead(inst)) return;
555 if (inst->opcode() == SpvOpLabel) return;
556 // If dead instruction is selection merge, remember merge block
557 // for new branch at end of block
558 if (inst->opcode() == SpvOpSelectionMerge ||
559 inst->opcode() == SpvOpLoopMerge)
560 mergeBlockId = inst->GetSingleWordInOperand(0);
561 to_kill_.push_back(inst);
562 modified = true;
563 });
564 // If a structured if or loop was deleted, add a branch to its merge
565 // block, and traverse to the merge block and continue processing there.
566 // We know the block still exists because the label is not deleted.
567 if (mergeBlockId != 0) {
568 AddBranch(mergeBlockId, *bi);
569 for (++bi; (*bi)->id() != mergeBlockId; ++bi) {
570 }
571
572 auto merge_terminator = (*bi)->terminator();
573 if (merge_terminator->opcode() == SpvOpUnreachable) {
574 // The merge was unreachable. This is undefined behaviour so just
575 // return (or return an undef). Then mark the new return as live.
576 auto func_ret_type_inst = get_def_use_mgr()->GetDef(func->type_id());
577 if (func_ret_type_inst->opcode() == SpvOpTypeVoid) {
578 merge_terminator->SetOpcode(SpvOpReturn);
579 } else {
580 // Find an undef for the return value and make sure it gets kept by
581 // the pass.
582 auto undef_id = Type2Undef(func->type_id());
583 auto undef = get_def_use_mgr()->GetDef(undef_id);
584 live_insts_.Set(undef->unique_id());
585 merge_terminator->SetOpcode(SpvOpReturnValue);
586 merge_terminator->SetInOperands({{SPV_OPERAND_TYPE_ID, {undef_id}}});
587 get_def_use_mgr()->AnalyzeInstUse(merge_terminator);
588 }
589 live_insts_.Set(merge_terminator->unique_id());
590 }
591 } else {
592 ++bi;
593 }
594 }
595
596 return modified;
597 }
598
InitializeModuleScopeLiveInstructions()599 void AggressiveDCEPass::InitializeModuleScopeLiveInstructions() {
600 // Keep all execution modes.
601 for (auto& exec : get_module()->execution_modes()) {
602 AddToWorklist(&exec);
603 }
604 // Keep all entry points.
605 for (auto& entry : get_module()->entry_points()) {
606 if (get_module()->version() >= SPV_SPIRV_VERSION_WORD(1, 4)) {
607 // In SPIR-V 1.4 and later, entry points must list all global variables
608 // used. DCE can still remove non-input/output variables and update the
609 // interface list. Mark the entry point as live and inputs and outputs as
610 // live, but defer decisions all other interfaces.
611 live_insts_.Set(entry.unique_id());
612 // The actual function is live always.
613 AddToWorklist(
614 get_def_use_mgr()->GetDef(entry.GetSingleWordInOperand(1u)));
615 for (uint32_t i = 3; i < entry.NumInOperands(); ++i) {
616 auto* var = get_def_use_mgr()->GetDef(entry.GetSingleWordInOperand(i));
617 auto storage_class = var->GetSingleWordInOperand(0u);
618 if (storage_class == SpvStorageClassInput ||
619 storage_class == SpvStorageClassOutput) {
620 AddToWorklist(var);
621 }
622 }
623 } else {
624 AddToWorklist(&entry);
625 }
626 }
627 for (auto& anno : get_module()->annotations()) {
628 if (anno.opcode() == SpvOpDecorate) {
629 // Keep workgroup size.
630 if (anno.GetSingleWordInOperand(1u) == SpvDecorationBuiltIn &&
631 anno.GetSingleWordInOperand(2u) == SpvBuiltInWorkgroupSize) {
632 AddToWorklist(&anno);
633 }
634
635 if (context()->preserve_bindings()) {
636 // Keep all bindings.
637 if ((anno.GetSingleWordInOperand(1u) == SpvDecorationDescriptorSet) ||
638 (anno.GetSingleWordInOperand(1u) == SpvDecorationBinding)) {
639 AddToWorklist(&anno);
640 }
641 }
642
643 if (context()->preserve_spec_constants()) {
644 // Keep all specialization constant instructions
645 if (anno.GetSingleWordInOperand(1u) == SpvDecorationSpecId) {
646 AddToWorklist(&anno);
647 }
648 }
649 }
650 }
651
652 // For each DebugInfo GlobalVariable keep all operands except the Variable.
653 // Later, if the variable is dead, we will set the operand to DebugInfoNone.
654 for (auto& dbg : get_module()->ext_inst_debuginfo()) {
655 if (dbg.GetOpenCL100DebugOpcode() != OpenCLDebugInfo100DebugGlobalVariable)
656 continue;
657 dbg.ForEachInId([this](const uint32_t* iid) {
658 Instruction* inInst = get_def_use_mgr()->GetDef(*iid);
659 if (inInst->opcode() == SpvOpVariable) return;
660 AddToWorklist(inInst);
661 });
662 }
663 }
664
ProcessImpl()665 Pass::Status AggressiveDCEPass::ProcessImpl() {
666 // Current functionality assumes shader capability
667 // TODO(greg-lunarg): Handle additional capabilities
668 if (!context()->get_feature_mgr()->HasCapability(SpvCapabilityShader))
669 return Status::SuccessWithoutChange;
670
671 // Current functionality assumes relaxed logical addressing (see
672 // instruction.h)
673 // TODO(greg-lunarg): Handle non-logical addressing
674 if (context()->get_feature_mgr()->HasCapability(SpvCapabilityAddresses))
675 return Status::SuccessWithoutChange;
676
677 // The variable pointer extension is no longer needed to use the capability,
678 // so we have to look for the capability.
679 if (context()->get_feature_mgr()->HasCapability(
680 SpvCapabilityVariablePointersStorageBuffer))
681 return Status::SuccessWithoutChange;
682
683 // If any extensions in the module are not explicitly supported,
684 // return unmodified.
685 if (!AllExtensionsSupported()) return Status::SuccessWithoutChange;
686
687 // Eliminate Dead functions.
688 bool modified = EliminateDeadFunctions();
689
690 InitializeModuleScopeLiveInstructions();
691
692 // Process all entry point functions.
693 ProcessFunction pfn = [this](Function* fp) { return AggressiveDCE(fp); };
694 modified |= context()->ProcessEntryPointCallTree(pfn);
695
696 // If the decoration manager is kept live then the context will try to keep it
697 // up to date. ADCE deals with group decorations by changing the operands in
698 // |OpGroupDecorate| instruction directly without informing the decoration
699 // manager. This can put it in an invalid state which will cause an error
700 // when the context tries to update it. To avoid this problem invalidate
701 // the decoration manager upfront.
702 //
703 // We kill it at now because it is used when processing the entry point
704 // functions.
705 context()->InvalidateAnalyses(IRContext::Analysis::kAnalysisDecorations);
706
707 // Process module-level instructions. Now that all live instructions have
708 // been marked, it is safe to remove dead global values.
709 modified |= ProcessGlobalValues();
710
711 assert((to_kill_.empty() || modified) &&
712 "A dead instruction was identified, but no change recorded.");
713
714 // Kill all dead instructions.
715 for (auto inst : to_kill_) {
716 context()->KillInst(inst);
717 }
718
719 // Cleanup all CFG including all unreachable blocks.
720 ProcessFunction cleanup = [this](Function* f) { return CFGCleanup(f); };
721 modified |= context()->ProcessEntryPointCallTree(cleanup);
722
723 return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
724 }
725
EliminateDeadFunctions()726 bool AggressiveDCEPass::EliminateDeadFunctions() {
727 // Identify live functions first. Those that are not live
728 // are dead. ADCE is disabled for non-shaders so we do not check for exported
729 // functions here.
730 std::unordered_set<const Function*> live_function_set;
731 ProcessFunction mark_live = [&live_function_set](Function* fp) {
732 live_function_set.insert(fp);
733 return false;
734 };
735 context()->ProcessEntryPointCallTree(mark_live);
736
737 bool modified = false;
738 for (auto funcIter = get_module()->begin();
739 funcIter != get_module()->end();) {
740 if (live_function_set.count(&*funcIter) == 0) {
741 modified = true;
742 funcIter =
743 eliminatedeadfunctionsutil::EliminateFunction(context(), &funcIter);
744 } else {
745 ++funcIter;
746 }
747 }
748
749 return modified;
750 }
751
ProcessGlobalValues()752 bool AggressiveDCEPass::ProcessGlobalValues() {
753 // Remove debug and annotation statements referencing dead instructions.
754 // This must be done before killing the instructions, otherwise there are
755 // dead objects in the def/use database.
756 bool modified = false;
757 Instruction* instruction = &*get_module()->debug2_begin();
758 while (instruction) {
759 if (instruction->opcode() != SpvOpName) {
760 instruction = instruction->NextNode();
761 continue;
762 }
763
764 if (IsTargetDead(instruction)) {
765 instruction = context()->KillInst(instruction);
766 modified = true;
767 } else {
768 instruction = instruction->NextNode();
769 }
770 }
771
772 // This code removes all unnecessary decorations safely (see #1174). It also
773 // does so in a more efficient manner than deleting them only as the targets
774 // are deleted.
775 std::vector<Instruction*> annotations;
776 for (auto& inst : get_module()->annotations()) annotations.push_back(&inst);
777 std::sort(annotations.begin(), annotations.end(), DecorationLess());
778 for (auto annotation : annotations) {
779 switch (annotation->opcode()) {
780 case SpvOpDecorate:
781 case SpvOpMemberDecorate:
782 case SpvOpDecorateStringGOOGLE:
783 case SpvOpMemberDecorateStringGOOGLE:
784 if (IsTargetDead(annotation)) {
785 context()->KillInst(annotation);
786 modified = true;
787 }
788 break;
789 case SpvOpDecorateId:
790 if (IsTargetDead(annotation)) {
791 context()->KillInst(annotation);
792 modified = true;
793 } else {
794 if (annotation->GetSingleWordInOperand(1) ==
795 SpvDecorationHlslCounterBufferGOOGLE) {
796 // HlslCounterBuffer will reference an id other than the target.
797 // If that id is dead, then the decoration can be removed as well.
798 uint32_t counter_buffer_id = annotation->GetSingleWordInOperand(2);
799 Instruction* counter_buffer_inst =
800 get_def_use_mgr()->GetDef(counter_buffer_id);
801 if (IsDead(counter_buffer_inst)) {
802 context()->KillInst(annotation);
803 modified = true;
804 }
805 }
806 }
807 break;
808 case SpvOpGroupDecorate: {
809 // Go through the targets of this group decorate. Remove each dead
810 // target. If all targets are dead, remove this decoration.
811 bool dead = true;
812 bool removed_operand = false;
813 for (uint32_t i = 1; i < annotation->NumOperands();) {
814 Instruction* opInst =
815 get_def_use_mgr()->GetDef(annotation->GetSingleWordOperand(i));
816 if (IsDead(opInst)) {
817 // Don't increment |i|.
818 annotation->RemoveOperand(i);
819 modified = true;
820 removed_operand = true;
821 } else {
822 i++;
823 dead = false;
824 }
825 }
826 if (dead) {
827 context()->KillInst(annotation);
828 modified = true;
829 } else if (removed_operand) {
830 context()->UpdateDefUse(annotation);
831 }
832 break;
833 }
834 case SpvOpGroupMemberDecorate: {
835 // Go through the targets of this group member decorate. Remove each
836 // dead target (and member index). If all targets are dead, remove this
837 // decoration.
838 bool dead = true;
839 bool removed_operand = false;
840 for (uint32_t i = 1; i < annotation->NumOperands();) {
841 Instruction* opInst =
842 get_def_use_mgr()->GetDef(annotation->GetSingleWordOperand(i));
843 if (IsDead(opInst)) {
844 // Don't increment |i|.
845 annotation->RemoveOperand(i + 1);
846 annotation->RemoveOperand(i);
847 modified = true;
848 removed_operand = true;
849 } else {
850 i += 2;
851 dead = false;
852 }
853 }
854 if (dead) {
855 context()->KillInst(annotation);
856 modified = true;
857 } else if (removed_operand) {
858 context()->UpdateDefUse(annotation);
859 }
860 break;
861 }
862 case SpvOpDecorationGroup:
863 // By the time we hit decoration groups we've checked everything that
864 // can target them. So if they have no uses they must be dead.
865 if (get_def_use_mgr()->NumUsers(annotation) == 0) {
866 context()->KillInst(annotation);
867 modified = true;
868 }
869 break;
870 default:
871 assert(false);
872 break;
873 }
874 }
875
876 for (auto& dbg : get_module()->ext_inst_debuginfo()) {
877 if (!IsDead(&dbg)) continue;
878 // Save GlobalVariable if its variable is live, otherwise null out variable
879 // index
880 if (dbg.GetOpenCL100DebugOpcode() ==
881 OpenCLDebugInfo100DebugGlobalVariable) {
882 auto var_id = dbg.GetSingleWordOperand(kGlobalVariableVariableIndex);
883 Instruction* var_inst = get_def_use_mgr()->GetDef(var_id);
884 if (!IsDead(var_inst)) continue;
885 context()->ForgetUses(&dbg);
886 dbg.SetOperand(
887 kGlobalVariableVariableIndex,
888 {context()->get_debug_info_mgr()->GetDebugInfoNone()->result_id()});
889 context()->AnalyzeUses(&dbg);
890 continue;
891 }
892 to_kill_.push_back(&dbg);
893 modified = true;
894 }
895
896 // Since ADCE is disabled for non-shaders, we don't check for export linkage
897 // attributes here.
898 for (auto& val : get_module()->types_values()) {
899 if (IsDead(&val)) {
900 // Save forwarded pointer if pointer is live since closure does not mark
901 // this live as it does not have a result id. This is a little too
902 // conservative since it is not known if the structure type that needed
903 // it is still live. TODO(greg-lunarg): Only save if needed.
904 if (val.opcode() == SpvOpTypeForwardPointer) {
905 uint32_t ptr_ty_id = val.GetSingleWordInOperand(0);
906 Instruction* ptr_ty_inst = get_def_use_mgr()->GetDef(ptr_ty_id);
907 if (!IsDead(ptr_ty_inst)) continue;
908 }
909 to_kill_.push_back(&val);
910 modified = true;
911 }
912 }
913
914 if (get_module()->version() >= SPV_SPIRV_VERSION_WORD(1, 4)) {
915 // Remove the dead interface variables from the entry point interface list.
916 for (auto& entry : get_module()->entry_points()) {
917 std::vector<Operand> new_operands;
918 for (uint32_t i = 0; i < entry.NumInOperands(); ++i) {
919 if (i < 3) {
920 // Execution model, function id and name are always valid.
921 new_operands.push_back(entry.GetInOperand(i));
922 } else {
923 auto* var =
924 get_def_use_mgr()->GetDef(entry.GetSingleWordInOperand(i));
925 if (!IsDead(var)) {
926 new_operands.push_back(entry.GetInOperand(i));
927 }
928 }
929 }
930 if (new_operands.size() != entry.NumInOperands()) {
931 entry.SetInOperands(std::move(new_operands));
932 get_def_use_mgr()->UpdateDefUse(&entry);
933 }
934 }
935 }
936
937 return modified;
938 }
939
940 AggressiveDCEPass::AggressiveDCEPass() = default;
941
Process()942 Pass::Status AggressiveDCEPass::Process() {
943 // Initialize extensions allowlist
944 InitExtensions();
945 return ProcessImpl();
946 }
947
InitExtensions()948 void AggressiveDCEPass::InitExtensions() {
949 extensions_allowlist_.clear();
950 extensions_allowlist_.insert({
951 "SPV_AMD_shader_explicit_vertex_parameter",
952 "SPV_AMD_shader_trinary_minmax",
953 "SPV_AMD_gcn_shader",
954 "SPV_KHR_shader_ballot",
955 "SPV_AMD_shader_ballot",
956 "SPV_AMD_gpu_shader_half_float",
957 "SPV_KHR_shader_draw_parameters",
958 "SPV_KHR_subgroup_vote",
959 "SPV_KHR_8bit_storage",
960 "SPV_KHR_16bit_storage",
961 "SPV_KHR_device_group",
962 "SPV_KHR_multiview",
963 "SPV_NVX_multiview_per_view_attributes",
964 "SPV_NV_viewport_array2",
965 "SPV_NV_stereo_view_rendering",
966 "SPV_NV_sample_mask_override_coverage",
967 "SPV_NV_geometry_shader_passthrough",
968 "SPV_AMD_texture_gather_bias_lod",
969 "SPV_KHR_storage_buffer_storage_class",
970 // SPV_KHR_variable_pointers
971 // Currently do not support extended pointer expressions
972 "SPV_AMD_gpu_shader_int16",
973 "SPV_KHR_post_depth_coverage",
974 "SPV_KHR_shader_atomic_counter_ops",
975 "SPV_EXT_shader_stencil_export",
976 "SPV_EXT_shader_viewport_index_layer",
977 "SPV_AMD_shader_image_load_store_lod",
978 "SPV_AMD_shader_fragment_mask",
979 "SPV_EXT_fragment_fully_covered",
980 "SPV_AMD_gpu_shader_half_float_fetch",
981 "SPV_GOOGLE_decorate_string",
982 "SPV_GOOGLE_hlsl_functionality1",
983 "SPV_GOOGLE_user_type",
984 "SPV_NV_shader_subgroup_partitioned",
985 "SPV_EXT_demote_to_helper_invocation",
986 "SPV_EXT_descriptor_indexing",
987 "SPV_NV_fragment_shader_barycentric",
988 "SPV_NV_compute_shader_derivatives",
989 "SPV_NV_shader_image_footprint",
990 "SPV_NV_shading_rate",
991 "SPV_NV_mesh_shader",
992 "SPV_NV_ray_tracing",
993 "SPV_KHR_ray_tracing",
994 "SPV_KHR_ray_query",
995 "SPV_EXT_fragment_invocation_density",
996 "SPV_EXT_physical_storage_buffer",
997 "SPV_KHR_terminate_invocation",
998 "SPV_KHR_shader_clock",
999 });
1000 }
1001
1002 } // namespace opt
1003 } // namespace spvtools
1004