void DeadVariableElimination::DeleteVariable(uint32_t result_id) { ir::Instruction* inst = get_def_use_mgr()->GetDef(result_id); assert(inst->opcode() == SpvOpVariable && "Should not be trying to delete anything other than an OpVariable."); // Look for an initializer that references another variable. We need to know // if that variable can be deleted after the reference is removed. if (inst->NumOperands() == 4) { ir::Instruction* initializer = get_def_use_mgr()->GetDef(inst->GetSingleWordOperand(3)); // TODO: Handle OpSpecConstantOP which might be defined in terms of other // variables. Will probably require a unified dead code pass that does all // instruction types. (Issue 906) if (initializer->opcode() == SpvOpVariable) { uint32_t initializer_id = initializer->result_id(); size_t& count = reference_count_[initializer_id]; if (count != kMustKeep) { --count; } if (count == 0) { DeleteVariable(initializer_id); } } } context()->KillDef(result_id); }
bool AggressiveDCEPass::IsVarOfStorage(uint32_t varId, uint32_t storageClass) { if (varId == 0) return false; const Instruction* varInst = get_def_use_mgr()->GetDef(varId); const SpvOp op = varInst->opcode(); if (op != SpvOpVariable) return false; const uint32_t varTypeId = varInst->type_id(); const Instruction* varTypeInst = get_def_use_mgr()->GetDef(varTypeId); if (varTypeInst->opcode() != SpvOpTypePointer) return false; return varTypeInst->GetSingleWordInOperand(kTypePointerStorageClassInIdx) == storageClass; }
// This optimization removes global variables that are not needed because they // are definitely not accessed. Pass::Status DeadVariableElimination::Process(ir::IRContext* c) { // The algorithm will compute the reference count for every global variable. // Anything with a reference count of 0 will then be deleted. For variables // that might have references that are not explicit in this context, we use // the // value kMustKeep as the reference count. InitializeProcessing(c); std::vector<uint32_t> ids_to_remove; // Get the reference count for all of the global OpVariable instructions. for (auto& inst : context()->types_values()) { if (inst.opcode() != SpvOp::SpvOpVariable) { continue; } size_t count = 0; uint32_t result_id = inst.result_id(); // Check the linkage. If it is exported, it could be reference somewhere // else, so we must keep the variable around. get_decoration_mgr()->ForEachDecoration( result_id, SpvDecorationLinkageAttributes, [&count](const ir::Instruction& linkage_instruction) { uint32_t last_operand = linkage_instruction.NumOperands() - 1; if (linkage_instruction.GetSingleWordOperand(last_operand) == SpvLinkageTypeExport) { count = kMustKeep; } }); if (count != kMustKeep) { // If we don't have to keep the instruction for other reasons, then look // at the uses and count the number of real references. count = 0; get_def_use_mgr()->ForEachUser( result_id, [&count](ir::Instruction* user) { if (!ir::IsAnnotationInst(user->opcode()) && user->opcode() != SpvOpName) { ++count; } }); } reference_count_[result_id] = count; if (count == 0) { ids_to_remove.push_back(result_id); } } // Remove all of the variables that have a reference count of 0. bool modified = false; if (!ids_to_remove.empty()) { modified = true; for (auto result_id : ids_to_remove) { DeleteVariable(result_id); } } return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange); }
void LocalAccessChainConvertPass::BuildAndAppendInst( SpvOp opcode, uint32_t typeId, uint32_t resultId, const std::vector<Operand>& in_opnds, std::vector<std::unique_ptr<Instruction>>* newInsts) { std::unique_ptr<Instruction> newInst( new Instruction(context(), opcode, typeId, resultId, in_opnds)); get_def_use_mgr()->AnalyzeInstDefUse(&*newInst); newInsts->emplace_back(std::move(newInst)); }
bool AggressiveDCEPass::IsTargetDead(Instruction* inst) { const uint32_t tId = inst->GetSingleWordInOperand(0); Instruction* tInst = get_def_use_mgr()->GetDef(tId); if (IsAnnotationInst(tInst->opcode())) { // This must be a decoration group. We go through annotations in a specific // order. So if this is not used by any group or group member decorates, it // is dead. assert(tInst->opcode() == SpvOpDecorationGroup); bool dead = true; get_def_use_mgr()->ForEachUser(tInst, [&dead](Instruction* user) { if (user->opcode() == SpvOpGroupDecorate || user->opcode() == SpvOpGroupMemberDecorate) dead = false; }); return dead; } return IsDead(tInst); }
bool BlockMergePass::IsMerge(uint32_t id) { return !get_def_use_mgr()->WhileEachUse(id, [](Instruction* user, uint32_t index) { SpvOp op = user->opcode(); if ((op == SpvOpLoopMerge || op == SpvOpSelectionMerge) && index == 0u) { return false; } return true; }); }
bool LocalAccessChainConvertPass::IsConstantIndexAccessChain( const Instruction* acp) const { uint32_t inIdx = 0; return acp->WhileEachInId([&inIdx, this](const uint32_t* tid) { if (inIdx > 0) { Instruction* opInst = get_def_use_mgr()->GetDef(*tid); if (opInst->opcode() != SpvOpConstant) return false; } ++inIdx; return true; }); }
void LocalAccessChainConvertPass::AppendConstantOperands( const Instruction* ptrInst, std::vector<Operand>* in_opnds) { uint32_t iidIdx = 0; ptrInst->ForEachInId([&iidIdx, &in_opnds, this](const uint32_t* iid) { if (iidIdx > 0) { const Instruction* cInst = get_def_use_mgr()->GetDef(*iid); uint32_t val = cInst->GetSingleWordInOperand(kConstantValueInIdx); in_opnds->push_back( {spv_operand_type_t::SPV_OPERAND_TYPE_LITERAL_INTEGER, {val}}); } ++iidIdx; }); }
uint32_t LocalAccessChainConvertPass::BuildAndAppendVarLoad( const Instruction* ptrInst, uint32_t* varId, uint32_t* varPteTypeId, std::vector<std::unique_ptr<Instruction>>* newInsts) { const uint32_t ldResultId = TakeNextId(); *varId = ptrInst->GetSingleWordInOperand(kAccessChainPtrIdInIdx); const Instruction* varInst = get_def_use_mgr()->GetDef(*varId); assert(varInst->opcode() == SpvOpVariable); *varPteTypeId = GetPointeeTypeId(varInst); BuildAndAppendInst(SpvOpLoad, *varPteTypeId, ldResultId, {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {*varId}}}, newInsts); return ldResultId; }
bool LocalSingleBlockLoadStoreElimPass::HasOnlySupportedRefs(uint32_t ptrId) { if (supported_ref_ptrs_.find(ptrId) != supported_ref_ptrs_.end()) return true; if (get_def_use_mgr()->WhileEachUser(ptrId, [this](Instruction* user) { SpvOp op = user->opcode(); if (IsNonPtrAccessChain(op) || op == SpvOpCopyObject) { if (!HasOnlySupportedRefs(user->result_id())) { return false; } } else if (op != SpvOpStore && op != SpvOpLoad && op != SpvOpName && !IsNonTypeDecorate(op)) { return false; } return true; })) { supported_ref_ptrs_.insert(ptrId); return true; } return false; }
void AggressiveDCEPass::AddStores(uint32_t ptrId) { get_def_use_mgr()->ForEachUser(ptrId, [this, ptrId](Instruction* user) { switch (user->opcode()) { case SpvOpAccessChain: case SpvOpInBoundsAccessChain: case SpvOpCopyObject: this->AddStores(user->result_id()); break; case SpvOpLoad: break; case SpvOpCopyMemory: case SpvOpCopyMemorySized: if (user->GetSingleWordInOperand(kCopyMemoryTargetAddrInIdx) == ptrId) { AddToWorklist(user); } break; // If default, assume it stores e.g. frexp, modf, function call case SpvOpStore: default: AddToWorklist(user); break; } }); }
bool AggressiveDCEPass::ProcessGlobalValues() { // Remove debug and annotation statements referencing dead instructions. // This must be done before killing the instructions, otherwise there are // dead objects in the def/use database. bool modified = false; Instruction* instruction = &*get_module()->debug2_begin(); while (instruction) { if (instruction->opcode() != SpvOpName) { instruction = instruction->NextNode(); continue; } if (IsTargetDead(instruction)) { instruction = context()->KillInst(instruction); modified = true; } else { instruction = instruction->NextNode(); } } // This code removes all unnecessary decorations safely (see #1174). It also // does so in a more efficient manner than deleting them only as the targets // are deleted. std::vector<Instruction*> annotations; for (auto& inst : get_module()->annotations()) annotations.push_back(&inst); std::sort(annotations.begin(), annotations.end(), DecorationLess()); for (auto annotation : annotations) { switch (annotation->opcode()) { case SpvOpDecorate: case SpvOpMemberDecorate: case SpvOpDecorateStringGOOGLE: case SpvOpMemberDecorateStringGOOGLE: if (IsTargetDead(annotation)) { context()->KillInst(annotation); modified = true; } break; case SpvOpDecorateId: if (IsTargetDead(annotation)) { context()->KillInst(annotation); modified = true; } else { if (annotation->GetSingleWordInOperand(1) == SpvDecorationHlslCounterBufferGOOGLE) { // HlslCounterBuffer will reference an id other than the target. // If that id is dead, then the decoration can be removed as well. uint32_t counter_buffer_id = annotation->GetSingleWordInOperand(2); Instruction* counter_buffer_inst = get_def_use_mgr()->GetDef(counter_buffer_id); if (IsDead(counter_buffer_inst)) { context()->KillInst(annotation); modified = true; } } } break; case SpvOpGroupDecorate: { // Go through the targets of this group decorate. Remove each dead // target. If all targets are dead, remove this decoration. bool dead = true; bool removed_operand = false; for (uint32_t i = 1; i < annotation->NumOperands();) { Instruction* opInst = get_def_use_mgr()->GetDef(annotation->GetSingleWordOperand(i)); if (IsDead(opInst)) { // Don't increment |i|. annotation->RemoveOperand(i); modified = true; removed_operand = true; } else { i++; dead = false; } } if (dead) { context()->KillInst(annotation); modified = true; } else if (removed_operand) { context()->UpdateDefUse(annotation); } break; } case SpvOpGroupMemberDecorate: { // Go through the targets of this group member decorate. Remove each // dead target (and member index). If all targets are dead, remove this // decoration. bool dead = true; bool removed_operand = false; for (uint32_t i = 1; i < annotation->NumOperands();) { Instruction* opInst = get_def_use_mgr()->GetDef(annotation->GetSingleWordOperand(i)); if (IsDead(opInst)) { // Don't increment |i|. annotation->RemoveOperand(i + 1); annotation->RemoveOperand(i); modified = true; removed_operand = true; } else { i += 2; dead = false; } } if (dead) { context()->KillInst(annotation); modified = true; } else if (removed_operand) { context()->UpdateDefUse(annotation); } break; } case SpvOpDecorationGroup: // By the time we hit decoration groups we've checked everything that // can target them. So if they have no uses they must be dead. if (get_def_use_mgr()->NumUsers(annotation) == 0) { context()->KillInst(annotation); modified = true; } break; default: assert(false); break; } } // Since ADCE is disabled for non-shaders, we don't check for export linkage // attributes here. for (auto& val : get_module()->types_values()) { if (IsDead(&val)) { to_kill_.push_back(&val); } } return modified; }
bool AggressiveDCEPass::AggressiveDCE(Function* func) { // Mark function parameters as live. AddToWorklist(&func->DefInst()); func->ForEachParam( [this](const Instruction* param) { AddToWorklist(const_cast<Instruction*>(param)); }, false); // Compute map from block to controlling conditional branch std::list<BasicBlock*> structuredOrder; cfg()->ComputeStructuredOrder(func, &*func->begin(), &structuredOrder); ComputeBlock2HeaderMaps(structuredOrder); bool modified = false; // Add instructions with external side effects to worklist. Also add branches // EXCEPT those immediately contained in an "if" selection construct or a loop // or continue construct. // TODO(greg-lunarg): Handle Frexp, Modf more optimally call_in_func_ = false; func_is_entry_point_ = false; private_stores_.clear(); // Stacks to keep track of when we are inside an if- or loop-construct. // When immediately inside an if- or loop-construct, we do not initially // mark branches live. All other branches must be marked live. std::stack<bool> assume_branches_live; std::stack<uint32_t> currentMergeBlockId; // Push sentinel values on stack for when outside of any control flow. assume_branches_live.push(true); currentMergeBlockId.push(0); for (auto bi = structuredOrder.begin(); bi != structuredOrder.end(); ++bi) { // If exiting if or loop, update stacks if ((*bi)->id() == currentMergeBlockId.top()) { assume_branches_live.pop(); currentMergeBlockId.pop(); } for (auto ii = (*bi)->begin(); ii != (*bi)->end(); ++ii) { SpvOp op = ii->opcode(); switch (op) { case SpvOpStore: { uint32_t varId; (void)GetPtr(&*ii, &varId); // Mark stores as live if their variable is not function scope // and is not private scope. Remember private stores for possible // later inclusion. We cannot call IsLocalVar at this point because // private_like_local_ has not been set yet. if (IsVarOfStorage(varId, SpvStorageClassPrivate) || IsVarOfStorage(varId, SpvStorageClassWorkgroup)) private_stores_.push_back(&*ii); else if (!IsVarOfStorage(varId, SpvStorageClassFunction)) AddToWorklist(&*ii); } break; case SpvOpCopyMemory: case SpvOpCopyMemorySized: { uint32_t varId; (void)GetPtr(ii->GetSingleWordInOperand(kCopyMemoryTargetAddrInIdx), &varId); if (IsVarOfStorage(varId, SpvStorageClassPrivate) || IsVarOfStorage(varId, SpvStorageClassWorkgroup)) private_stores_.push_back(&*ii); else if (!IsVarOfStorage(varId, SpvStorageClassFunction)) AddToWorklist(&*ii); } break; case SpvOpLoopMerge: { assume_branches_live.push(false); currentMergeBlockId.push( ii->GetSingleWordInOperand(kLoopMergeMergeBlockIdInIdx)); } break; case SpvOpSelectionMerge: { assume_branches_live.push(false); currentMergeBlockId.push( ii->GetSingleWordInOperand(kSelectionMergeMergeBlockIdInIdx)); } break; case SpvOpSwitch: case SpvOpBranch: case SpvOpBranchConditional: case SpvOpUnreachable: { if (assume_branches_live.top()) { AddToWorklist(&*ii); } } break; default: { // Function calls, atomics, function params, function returns, etc. // TODO(greg-lunarg): function calls live only if write to non-local if (!ii->IsOpcodeSafeToDelete()) { AddToWorklist(&*ii); } // Remember function calls if (op == SpvOpFunctionCall) call_in_func_ = true; } break; } } } // See if current function is an entry point for (auto& ei : get_module()->entry_points()) { if (ei.GetSingleWordInOperand(kEntryPointFunctionIdInIdx) == func->result_id()) { func_is_entry_point_ = true; break; } } // If the current function is an entry point and has no function calls, // we can optimize private variables as locals private_like_local_ = func_is_entry_point_ && !call_in_func_; // If privates are not like local, add their stores to worklist if (!private_like_local_) for (auto& ps : private_stores_) AddToWorklist(ps); // Perform closure on live instruction set. while (!worklist_.empty()) { Instruction* liveInst = worklist_.front(); // Add all operand instructions if not already live liveInst->ForEachInId([&liveInst, this](const uint32_t* iid) { Instruction* inInst = get_def_use_mgr()->GetDef(*iid); // Do not add label if an operand of a branch. This is not needed // as part of live code discovery and can create false live code, // for example, the branch to a header of a loop. if (inInst->opcode() == SpvOpLabel && liveInst->IsBranch()) return; AddToWorklist(inInst); }); if (liveInst->type_id() != 0) { AddToWorklist(get_def_use_mgr()->GetDef(liveInst->type_id())); } // If in a structured if or loop construct, add the controlling // conditional branch and its merge. BasicBlock* blk = context()->get_instr_block(liveInst); Instruction* branchInst = block2headerBranch_[blk]; if (branchInst != nullptr) { AddToWorklist(branchInst); Instruction* mergeInst = branch2merge_[branchInst]; AddToWorklist(mergeInst); } // If the block is a header, add the next outermost controlling // conditional branch and its merge. Instruction* nextBranchInst = header2nextHeaderBranch_[blk]; if (nextBranchInst != nullptr) { AddToWorklist(nextBranchInst); Instruction* mergeInst = branch2merge_[nextBranchInst]; AddToWorklist(mergeInst); } // If local load, add all variable's stores if variable not already live if (liveInst->opcode() == SpvOpLoad || liveInst->IsAtomicWithLoad()) { uint32_t varId; (void)GetPtr(liveInst, &varId); if (varId != 0) { ProcessLoad(varId); } // Process memory copies like loads } else if (liveInst->opcode() == SpvOpCopyMemory || liveInst->opcode() == SpvOpCopyMemorySized) { uint32_t varId; (void)GetPtr(liveInst->GetSingleWordInOperand(kCopyMemorySourceAddrInIdx), &varId); if (varId != 0) { ProcessLoad(varId); } // If merge, add other branches that are part of its control structure } else if (liveInst->opcode() == SpvOpLoopMerge || liveInst->opcode() == SpvOpSelectionMerge) { AddBreaksAndContinuesToWorklist(liveInst); // If function call, treat as if it loads from all pointer arguments } else if (liveInst->opcode() == SpvOpFunctionCall) { liveInst->ForEachInId([this](const uint32_t* iid) { // Skip non-ptr args if (!IsPtr(*iid)) return; uint32_t varId; (void)GetPtr(*iid, &varId); ProcessLoad(varId); }); // If function parameter, treat as if it's result id is loaded from } else if (liveInst->opcode() == SpvOpFunctionParameter) { ProcessLoad(liveInst->result_id()); // We treat an OpImageTexelPointer as a load of the pointer, and // that value is manipulated to get the result. } else if (liveInst->opcode() == SpvOpImageTexelPointer) { uint32_t varId; (void)GetPtr(liveInst, &varId); if (varId != 0) { ProcessLoad(varId); } } worklist_.pop(); } // Kill dead instructions and remember dead blocks for (auto bi = structuredOrder.begin(); bi != structuredOrder.end();) { uint32_t mergeBlockId = 0; (*bi)->ForEachInst([this, &modified, &mergeBlockId](Instruction* inst) { if (!IsDead(inst)) return; if (inst->opcode() == SpvOpLabel) return; // If dead instruction is selection merge, remember merge block // for new branch at end of block if (inst->opcode() == SpvOpSelectionMerge || inst->opcode() == SpvOpLoopMerge) mergeBlockId = inst->GetSingleWordInOperand(0); to_kill_.push_back(inst); modified = true; }); // If a structured if or loop was deleted, add a branch to its merge // block, and traverse to the merge block and continue processing there. // We know the block still exists because the label is not deleted. if (mergeBlockId != 0) { AddBranch(mergeBlockId, *bi); for (++bi; (*bi)->id() != mergeBlockId; ++bi) { } auto merge_terminator = (*bi)->terminator(); if (merge_terminator->opcode() == SpvOpUnreachable) { // The merge was unreachable. This is undefined behaviour so just // return (or return an undef). Then mark the new return as live. auto func_ret_type_inst = get_def_use_mgr()->GetDef(func->type_id()); if (func_ret_type_inst->opcode() == SpvOpTypeVoid) { merge_terminator->SetOpcode(SpvOpReturn); } else { // Find an undef for the return value and make sure it gets kept by // the pass. auto undef_id = Type2Undef(func->type_id()); auto undef = get_def_use_mgr()->GetDef(undef_id); live_insts_.Set(undef->unique_id()); merge_terminator->SetOpcode(SpvOpReturnValue); merge_terminator->SetInOperands({{SPV_OPERAND_TYPE_ID, {undef_id}}}); get_def_use_mgr()->AnalyzeInstUse(merge_terminator); } live_insts_.Set(merge_terminator->unique_id()); } } else { ++bi; } } return modified; }
void AggressiveDCEPass::AddBreaksAndContinuesToWorklist( Instruction* mergeInst) { assert(mergeInst->opcode() == SpvOpSelectionMerge || mergeInst->opcode() == SpvOpLoopMerge); BasicBlock* header = context()->get_instr_block(mergeInst); uint32_t headerIndex = structured_order_index_[header]; const uint32_t mergeId = mergeInst->GetSingleWordInOperand(0); BasicBlock* merge = context()->get_instr_block(mergeId); uint32_t mergeIndex = structured_order_index_[merge]; get_def_use_mgr()->ForEachUser( mergeId, [headerIndex, mergeIndex, this](Instruction* user) { if (!user->IsBranch()) return; BasicBlock* block = context()->get_instr_block(user); uint32_t index = structured_order_index_[block]; if (headerIndex < index && index < mergeIndex) { // This is a break from the loop. AddToWorklist(user); // Add branch's merge if there is one. Instruction* userMerge = branch2merge_[user]; if (userMerge != nullptr) AddToWorklist(userMerge); } }); if (mergeInst->opcode() != SpvOpLoopMerge) { return; } // For loops we need to find the continues as well. const uint32_t contId = mergeInst->GetSingleWordInOperand(kLoopMergeContinueBlockIdInIdx); get_def_use_mgr()->ForEachUser(contId, [&contId, this](Instruction* user) { SpvOp op = user->opcode(); if (op == SpvOpBranchConditional || op == SpvOpSwitch) { // A conditional branch or switch can only be a continue if it does not // have a merge instruction or its merge block is not the continue block. Instruction* hdrMerge = branch2merge_[user]; if (hdrMerge != nullptr && hdrMerge->opcode() == SpvOpSelectionMerge) { uint32_t hdrMergeId = hdrMerge->GetSingleWordInOperand(kSelectionMergeMergeBlockIdInIdx); if (hdrMergeId == contId) return; // Need to mark merge instruction too AddToWorklist(hdrMerge); } } else if (op == SpvOpBranch) { // An unconditional branch can only be a continue if it is not // branching to its own merge block. BasicBlock* blk = context()->get_instr_block(user); Instruction* hdrBranch = block2headerBranch_[blk]; if (hdrBranch == nullptr) return; Instruction* hdrMerge = branch2merge_[hdrBranch]; if (hdrMerge->opcode() == SpvOpLoopMerge) return; uint32_t hdrMergeId = hdrMerge->GetSingleWordInOperand(kSelectionMergeMergeBlockIdInIdx); if (contId == hdrMergeId) return; } else { return; } AddToWorklist(user); }); }
bool BlockMergePass::IsHeader(uint32_t id) { return IsHeader(context()->get_instr_block(get_def_use_mgr()->GetDef(id))); }