diff options
Diffstat (limited to 'source/opt')
38 files changed, 319 insertions, 272 deletions
diff --git a/source/opt/basic_block.cpp b/source/opt/basic_block.cpp index fccd396d..e120bab9 100644 --- a/source/opt/basic_block.cpp +++ b/source/opt/basic_block.cpp @@ -30,8 +30,8 @@ const uint32_t kSelectionMergeMergeBlockIdInIdx = 0; } // namespace BasicBlock* BasicBlock::Clone(IRContext* context) const { - BasicBlock* clone = - new BasicBlock(std::unique_ptr<Instruction>(GetLabelInst().Clone(context))); + BasicBlock* clone = new BasicBlock( + std::unique_ptr<Instruction>(GetLabelInst().Clone(context))); for (const auto& inst : insts_) // Use the incoming context clone->AddInstruction(std::unique_ptr<Instruction>(inst.Clone(context))); diff --git a/source/opt/block_merge_pass.cpp b/source/opt/block_merge_pass.cpp index 9a72b147..24d19f67 100644 --- a/source/opt/block_merge_pass.cpp +++ b/source/opt/block_merge_pass.cpp @@ -24,12 +24,11 @@ namespace opt { bool BlockMergePass::HasMultipleRefs(uint32_t labId) { int rcnt = 0; - get_def_use_mgr()->ForEachUser( - labId, [&rcnt](ir::Instruction* user) { - if (user->opcode() != SpvOpName) { - ++rcnt; - } - }); + get_def_use_mgr()->ForEachUser(labId, [&rcnt](ir::Instruction* user) { + if (user->opcode() != SpvOpName) { + ++rcnt; + } + }); return rcnt > 1; } @@ -40,7 +39,7 @@ void BlockMergePass::KillInstAndName(ir::Instruction* inst) { to_kill.push_back(user); } }); - for (auto i: to_kill) { + for (auto i : to_kill) { context()->KillInst(i); } context()->KillInst(inst); diff --git a/source/opt/build_module.cpp b/source/opt/build_module.cpp index d7a4271e..7978646d 100644 --- a/source/opt/build_module.cpp +++ b/source/opt/build_module.cpp @@ -14,7 +14,7 @@ #include "build_module.h" -#include"ir_context.h" +#include "ir_context.h" #include "ir_loader.h" #include "make_unique.h" #include "table.h" @@ -42,12 +42,12 @@ spv_result_t SetSpvInst(void* builder, const spv_parsed_instruction_t* inst) { return SPV_ERROR_INVALID_BINARY; }; -} // annoymous namespace +} // namespace std::unique_ptr<ir::IRContext> BuildModule(spv_target_env env, - MessageConsumer consumer, - const uint32_t* binary, - const size_t size) { + MessageConsumer consumer, + const uint32_t* binary, + const size_t size) { auto context = spvContextCreate(env); libspirv::SetContextMessageConsumer(context, consumer); diff --git a/source/opt/build_module.h b/source/opt/build_module.h index 3ee66072..7e949363 100644 --- a/source/opt/build_module.h +++ b/source/opt/build_module.h @@ -29,8 +29,8 @@ namespace spvtools { // decoded according to the given target |env|. Returns nullptr if errors occur // and sends the errors to |consumer|. std::unique_ptr<ir::IRContext> BuildModule(spv_target_env env, - MessageConsumer consumer, - const uint32_t* binary, size_t size); + MessageConsumer consumer, + const uint32_t* binary, size_t size); // Builds an ir::Module and returns the owning ir::IRContext from the given // SPIR-V assembly |text|. The |text| will be encoded according to the given diff --git a/source/opt/cfg.cpp b/source/opt/cfg.cpp index a0b78c78..bf462361 100644 --- a/source/opt/cfg.cpp +++ b/source/opt/cfg.cpp @@ -30,8 +30,8 @@ CFG::CFG(ir::Module* module) : module_(module), pseudo_entry_block_(std::unique_ptr<ir::Instruction>( new ir::Instruction(module->context(), SpvOpLabel, 0, 0, {}))), - pseudo_exit_block_(std::unique_ptr<ir::Instruction>( - new ir::Instruction(module->context(), SpvOpLabel, 0, kInvalidId, {}))) { + pseudo_exit_block_(std::unique_ptr<ir::Instruction>(new ir::Instruction( + module->context(), SpvOpLabel, 0, kInvalidId, {}))) { for (auto& fn : *module) { for (auto& blk : fn) { uint32_t blkId = blk.id(); @@ -88,5 +88,5 @@ void CFG::ComputeStructuredSuccessors(ir::Function* func) { } } -} // namespace opt +} // namespace ir } // namespace spvtools diff --git a/source/opt/cfg.h b/source/opt/cfg.h index 6043b6ec..f8f3c6c0 100644 --- a/source/opt/cfg.h +++ b/source/opt/cfg.h @@ -104,7 +104,7 @@ class CFG { std::unordered_map<uint32_t, ir::BasicBlock*> id2block_; }; -} // namespace opt +} // namespace ir } // namespace spvtools #endif // LIBSPIRV_OPT_CFG_H_ diff --git a/source/opt/cfg_cleanup_pass.cpp b/source/opt/cfg_cleanup_pass.cpp index a74b6d35..2565225e 100644 --- a/source/opt/cfg_cleanup_pass.cpp +++ b/source/opt/cfg_cleanup_pass.cpp @@ -27,9 +27,7 @@ namespace spvtools { namespace opt { -void CFGCleanupPass::Initialize(ir::IRContext* c) { - InitializeProcessing(c); -} +void CFGCleanupPass::Initialize(ir::IRContext* c) { InitializeProcessing(c); } Pass::Status CFGCleanupPass::Process(ir::IRContext* c) { Initialize(c); diff --git a/source/opt/common_uniform_elim_pass.cpp b/source/opt/common_uniform_elim_pass.cpp index 9b8b1aca..721b9536 100644 --- a/source/opt/common_uniform_elim_pass.cpp +++ b/source/opt/common_uniform_elim_pass.cpp @@ -99,10 +99,11 @@ ir::Instruction* CommonUniformElimPass::GetPtr(ir::Instruction* ip, bool CommonUniformElimPass::IsVolatileStruct(uint32_t type_id) { assert(get_def_use_mgr()->GetDef(type_id)->opcode() == SpvOpTypeStruct); bool has_volatile_deco = false; - get_decoration_mgr()->ForEachDecoration(type_id, SpvDecorationVolatile, - [&has_volatile_deco](const ir::Instruction&) { - has_volatile_deco = true; - }); + get_decoration_mgr()->ForEachDecoration( + type_id, SpvDecorationVolatile, + [&has_volatile_deco](const ir::Instruction&) { + has_volatile_deco = true; + }); return has_volatile_deco; } @@ -256,8 +257,9 @@ void CommonUniformElimPass::GenACLoadRepl( } ++iidIdx; }); - std::unique_ptr<ir::Instruction> newExt(new ir::Instruction( - context(), SpvOpCompositeExtract, ptrPteTypeId, extResultId, ext_in_opnds)); + std::unique_ptr<ir::Instruction> newExt( + new ir::Instruction(context(), SpvOpCompositeExtract, ptrPteTypeId, + extResultId, ext_in_opnds)); get_def_use_mgr()->AnalyzeInstDefUse(&*newExt); newInsts->emplace_back(std::move(newExt)); *resultId = extResultId; @@ -553,18 +555,27 @@ void CommonUniformElimPass::InitExtensions() { extensions_whitelist_.clear(); extensions_whitelist_.insert({ "SPV_AMD_shader_explicit_vertex_parameter", - "SPV_AMD_shader_trinary_minmax", "SPV_AMD_gcn_shader", - "SPV_KHR_shader_ballot", "SPV_AMD_shader_ballot", - "SPV_AMD_gpu_shader_half_float", "SPV_KHR_shader_draw_parameters", - "SPV_KHR_subgroup_vote", "SPV_KHR_16bit_storage", "SPV_KHR_device_group", - "SPV_KHR_multiview", "SPV_NVX_multiview_per_view_attributes", - "SPV_NV_viewport_array2", "SPV_NV_stereo_view_rendering", + "SPV_AMD_shader_trinary_minmax", + "SPV_AMD_gcn_shader", + "SPV_KHR_shader_ballot", + "SPV_AMD_shader_ballot", + "SPV_AMD_gpu_shader_half_float", + "SPV_KHR_shader_draw_parameters", + "SPV_KHR_subgroup_vote", + "SPV_KHR_16bit_storage", + "SPV_KHR_device_group", + "SPV_KHR_multiview", + "SPV_NVX_multiview_per_view_attributes", + "SPV_NV_viewport_array2", + "SPV_NV_stereo_view_rendering", "SPV_NV_sample_mask_override_coverage", - "SPV_NV_geometry_shader_passthrough", "SPV_AMD_texture_gather_bias_lod", + "SPV_NV_geometry_shader_passthrough", + "SPV_AMD_texture_gather_bias_lod", "SPV_KHR_storage_buffer_storage_class", // SPV_KHR_variable_pointers // Currently do not support extended pointer expressions - "SPV_AMD_gpu_shader_int16", "SPV_KHR_post_depth_coverage", + "SPV_AMD_gpu_shader_int16", + "SPV_KHR_post_depth_coverage", "SPV_KHR_shader_atomic_counter_ops", }); } diff --git a/source/opt/dead_branch_elim_pass.cpp b/source/opt/dead_branch_elim_pass.cpp index b9c65621..90ef32d8 100644 --- a/source/opt/dead_branch_elim_pass.cpp +++ b/source/opt/dead_branch_elim_pass.cpp @@ -17,8 +17,8 @@ #include "dead_branch_elim_pass.h" #include "cfa.h" -#include "iterator.h" #include "ir_context.h" +#include "iterator.h" namespace spvtools { namespace opt { diff --git a/source/opt/dead_variable_elimination.cpp b/source/opt/dead_variable_elimination.cpp index 01ff04fd..1fec3a4f 100644 --- a/source/opt/dead_variable_elimination.cpp +++ b/source/opt/dead_variable_elimination.cpp @@ -14,8 +14,8 @@ #include "dead_variable_elimination.h" -#include "reflect.h" #include "ir_context.h" +#include "reflect.h" namespace spvtools { namespace opt { diff --git a/source/opt/decoration_manager.cpp b/source/opt/decoration_manager.cpp index 42c4e773..4b557f38 100644 --- a/source/opt/decoration_manager.cpp +++ b/source/opt/decoration_manager.cpp @@ -261,7 +261,8 @@ void DecorationManager::CloneDecorations( case SpvOpMemberDecorate: case SpvOpDecorateId: { // simply clone decoration and change |target-id| to |to| - std::unique_ptr<ir::Instruction> new_inst(inst->Clone(module_->context())); + std::unique_ptr<ir::Instruction> new_inst( + inst->Clone(module_->context())); new_inst->SetInOperand(0, {to}); id_to_decoration_insts_[to].push_back(new_inst.get()); module_->AddAnnotationInst(std::move(new_inst)); diff --git a/source/opt/decoration_manager.h b/source/opt/decoration_manager.h index a87a8f9d..fca78d39 100644 --- a/source/opt/decoration_manager.h +++ b/source/opt/decoration_manager.h @@ -71,7 +71,8 @@ class DecorationManager { // Function |f| can be used to update context information and is called // with |false|, before an instruction is going to be changed and // with |true| afterwards. - void CloneDecorations(uint32_t from, uint32_t to, std::function<void(ir::Instruction&, bool)> f); + void CloneDecorations(uint32_t from, uint32_t to, + std::function<void(ir::Instruction&, bool)> f); // Informs the decoration manager of a new decoration that it needs to track. void AddDecoration(ir::Instruction* inst); diff --git a/source/opt/def_use_manager.cpp b/source/opt/def_use_manager.cpp index 94935f03..be9dc6c0 100644 --- a/source/opt/def_use_manager.cpp +++ b/source/opt/def_use_manager.cpp @@ -95,40 +95,44 @@ bool DefUseManager::UsersNotEnd(const IdToUsersMap::const_iterator& iter, return UsersNotEnd(iter, id_to_users_.end(), inst); } -void DefUseManager::ForEachUser(const ir::Instruction* def, - const std::function<void(ir::Instruction*)>& f) const { +void DefUseManager::ForEachUser( + const ir::Instruction* def, + const std::function<void(ir::Instruction*)>& f) const { // Ensure that |def| has been registered. - assert(def && def == GetDef(def->result_id()) && "Definition is not registered."); + assert(def && def == GetDef(def->result_id()) && + "Definition is not registered."); auto end = id_to_users_.end(); for (auto iter = UsersBegin(def); UsersNotEnd(iter, end, def); ++iter) { f(iter->second); } } -void DefUseManager::ForEachUser(uint32_t id, - const std::function<void(ir::Instruction*)>& f) const { +void DefUseManager::ForEachUser( + uint32_t id, const std::function<void(ir::Instruction*)>& f) const { ForEachUser(GetDef(id), f); } -void DefUseManager::ForEachUse(const ir::Instruction* def, - const std::function<void(ir::Instruction*, uint32_t)>& f) const { +void DefUseManager::ForEachUse( + const ir::Instruction* def, + const std::function<void(ir::Instruction*, uint32_t)>& f) const { // Ensure that |def| has been registered. - assert(def && def == GetDef(def->result_id()) && "Definition is not registered."); + assert(def && def == GetDef(def->result_id()) && + "Definition is not registered."); auto end = id_to_users_.end(); for (auto iter = UsersBegin(def); UsersNotEnd(iter, end, def); ++iter) { ir::Instruction* user = iter->second; for (uint32_t idx = 0; idx != user->NumOperands(); ++idx) { const ir::Operand& op = user->GetOperand(idx); if (op.type != SPV_OPERAND_TYPE_RESULT_ID && spvIsIdType(op.type)) { - if (def->result_id() == op.words[0]) - f(user, idx); + if (def->result_id() == op.words[0]) f(user, idx); } } } } -void DefUseManager::ForEachUse(uint32_t id, - const std::function<void(ir::Instruction*, uint32_t)>& f) const { +void DefUseManager::ForEachUse( + uint32_t id, + const std::function<void(ir::Instruction*, uint32_t)>& f) const { ForEachUse(GetDef(id), f); } @@ -148,10 +152,10 @@ std::vector<ir::Instruction*> DefUseManager::GetAnnotations(uint32_t id) const { void DefUseManager::AnalyzeDefUse(ir::Module* module) { if (!module) return; // Analyze all the defs before any uses to catch forward references. - module->ForEachInst(std::bind(&DefUseManager::AnalyzeInstDef, this, - std::placeholders::_1)); - module->ForEachInst(std::bind(&DefUseManager::AnalyzeInstUse, this, - std::placeholders::_1)); + module->ForEachInst( + std::bind(&DefUseManager::AnalyzeInstDef, this, std::placeholders::_1)); + module->ForEachInst( + std::bind(&DefUseManager::AnalyzeInstUse, this, std::placeholders::_1)); } void DefUseManager::ClearInst(ir::Instruction* inst) { @@ -163,7 +167,8 @@ void DefUseManager::ClearInst(ir::Instruction* inst) { auto users_begin = UsersBegin(inst); auto end = id_to_users_.end(); auto new_end = users_begin; - for (; UsersNotEnd(new_end, end, inst); ++new_end) {} + for (; UsersNotEnd(new_end, end, inst); ++new_end) { + } id_to_users_.erase(users_begin, new_end); id_to_def_.erase(inst->result_id()); } @@ -176,7 +181,8 @@ void DefUseManager::EraseUseRecordsOfOperandIds(const ir::Instruction* inst) { auto iter = inst_to_used_ids_.find(inst); if (iter != inst_to_used_ids_.end()) { for (auto use_id : iter->second) { - id_to_users_.erase(UserEntry(GetDef(use_id), const_cast<ir::Instruction*>(inst))); + id_to_users_.erase( + UserEntry(GetDef(use_id), const_cast<ir::Instruction*>(inst))); } inst_to_used_ids_.erase(inst); } diff --git a/source/opt/def_use_manager.h b/source/opt/def_use_manager.h index 1f673058..0d814523 100644 --- a/source/opt/def_use_manager.h +++ b/source/opt/def_use_manager.h @@ -42,15 +42,11 @@ inline bool operator==(const Use& lhs, const Use& rhs) { return lhs.inst == rhs.inst && lhs.operand_index == rhs.operand_index; } -inline bool operator!=(const Use& lhs, const Use& rhs) { - return !(lhs == rhs); -} +inline bool operator!=(const Use& lhs, const Use& rhs) { return !(lhs == rhs); } inline bool operator<(const Use& lhs, const Use& rhs) { - if (lhs.inst < rhs.inst) - return true; - if (lhs.inst > rhs.inst) - return false; + if (lhs.inst < rhs.inst) return true; + if (lhs.inst > rhs.inst) return false; return lhs.operand_index < rhs.operand_index; } @@ -77,26 +73,19 @@ struct UserEntryLess { bool operator()(const UserEntry& lhs, const UserEntry& rhs) const { // If lhs.first and rhs.first are both null, fall through to checking the // second entries. - if (!lhs.first && rhs.first) - return true; - if (lhs.first && !rhs.first) - return false; + if (!lhs.first && rhs.first) return true; + if (lhs.first && !rhs.first) return false; // If neither defintion is null, then compare unique ids. if (lhs.first && rhs.first) { - if (lhs.first->unique_id() < rhs.first->unique_id()) - return true; - if (rhs.first->unique_id() < lhs.first->unique_id()) - return false; + if (lhs.first->unique_id() < rhs.first->unique_id()) return true; + if (rhs.first->unique_id() < lhs.first->unique_id()) return false; } // Return false on equality. - if (!lhs.second && !rhs.second) - return false; - if (!lhs.second) - return true; - if (!rhs.second) - return false; + if (!lhs.second && !rhs.second) return false; + if (!lhs.second) return true; + if (!rhs.second) return false; // If neither user is null then compare unique ids. return lhs.second->unique_id() < rhs.second->unique_id(); @@ -155,12 +144,12 @@ class DefUseManager { // visited separately. // // |def| (or |id|) must be registered as a definition. - void ForEachUse( - const ir::Instruction* def, - const std::function<void(ir::Instruction*, uint32_t operand_index)>& f) const; - void ForEachUse( - uint32_t id, - const std::function<void(ir::Instruction*, uint32_t operand_index)>& f) const; + void ForEachUse(const ir::Instruction* def, + const std::function<void(ir::Instruction*, + uint32_t operand_index)>& f) const; + void ForEachUse(uint32_t id, + const std::function<void(ir::Instruction*, + uint32_t operand_index)>& f) const; // Returns the annotation instrunctions which are a direct use of the given // |id|. This means when the decorations are applied through decoration @@ -184,8 +173,8 @@ class DefUseManager { // Erases the records that a given instruction uses its operand ids. void EraseUseRecordsOfOperandIds(const ir::Instruction* inst); - friend bool operator==(const DefUseManager&, const DefUseManager&); - friend bool operator!=(const DefUseManager& lhs, const DefUseManager& rhs) { + friend bool operator==(const DefUseManager&, const DefUseManager&); + friend bool operator!=(const DefUseManager& lhs, const DefUseManager& rhs) { return !(lhs == rhs); } @@ -213,11 +202,10 @@ class DefUseManager { // structures in this class. Does nothing if |module| is nullptr. void AnalyzeDefUse(ir::Module* module); - IdToDefMap id_to_def_; // Mapping from ids to their definitions - IdToUsersMap id_to_users_; // Mapping from ids to their users + IdToDefMap id_to_def_; // Mapping from ids to their definitions + IdToUsersMap id_to_users_; // Mapping from ids to their users // Mapping from instructions to the ids used in the instruction. InstToUsedIdsMap inst_to_used_ids_; - }; } // namespace analysis diff --git a/source/opt/eliminate_dead_constant_pass.cpp b/source/opt/eliminate_dead_constant_pass.cpp index 3c4e7f91..5c60f344 100644 --- a/source/opt/eliminate_dead_constant_pass.cpp +++ b/source/opt/eliminate_dead_constant_pass.cpp @@ -95,15 +95,14 @@ Pass::Status EliminateDeadConstantPass::Process(ir::IRContext* irContext) { // constants. std::unordered_set<ir::Instruction*> dead_others; for (auto* dc : dead_consts) { - irContext->get_def_use_mgr()->ForEachUser(dc, [&dead_others](ir::Instruction* user) { - SpvOp op = user->opcode(); - if (ir::IsAnnotationInst(op) || - ir::IsDebug1Inst(op) || - ir::IsDebug2Inst(op) || - ir::IsDebug3Inst(op)) { - dead_others.insert(user); - } - }); + irContext->get_def_use_mgr()->ForEachUser( + dc, [&dead_others](ir::Instruction* user) { + SpvOp op = user->opcode(); + if (ir::IsAnnotationInst(op) || ir::IsDebug1Inst(op) || + ir::IsDebug2Inst(op) || ir::IsDebug3Inst(op)) { + dead_others.insert(user); + } + }); } // Turn all dead instructions and uses of them to nop diff --git a/source/opt/eliminate_dead_functions_pass.cpp b/source/opt/eliminate_dead_functions_pass.cpp index 8c7d02f4..8f9748a6 100644 --- a/source/opt/eliminate_dead_functions_pass.cpp +++ b/source/opt/eliminate_dead_functions_pass.cpp @@ -51,10 +51,7 @@ Pass::Status EliminateDeadFunctionsPass::Process(ir::IRContext* c) { void EliminateDeadFunctionsPass::EliminateFunction(ir::Function* func) { // Remove all of the instruction in the function body func->ForEachInst( - [this](ir::Instruction* inst) { - context()->KillInst(inst); - }, - true); + [this](ir::Instruction* inst) { context()->KillInst(inst); }, true); } } // namespace opt } // namespace spvtools diff --git a/source/opt/fold_spec_constant_op_and_composite_pass.cpp b/source/opt/fold_spec_constant_op_and_composite_pass.cpp index e91d1fb1..dc33eda7 100644 --- a/source/opt/fold_spec_constant_op_and_composite_pass.cpp +++ b/source/opt/fold_spec_constant_op_and_composite_pass.cpp @@ -515,7 +515,7 @@ bool IsValidTypeForComponentWiseOperation(const analysis::Type* type) { } return false; } -} +} // namespace ir::Instruction* FoldSpecConstantOpAndCompositePass::DoComponentWiseOperation( ir::Module::inst_iterator* pos) { @@ -766,8 +766,7 @@ FoldSpecConstantOpAndCompositePass::CreateCompositeInstruction( operands.emplace_back(spv_operand_type_t::SPV_OPERAND_TYPE_ID, std::initializer_list<uint32_t>{id}); } - return MakeUnique<ir::Instruction>(context(), - SpvOp::SpvOpConstantComposite, + return MakeUnique<ir::Instruction>(context(), SpvOp::SpvOpConstantComposite, type_mgr_->GetId(cc->type()), result_id, std::move(operands)); } diff --git a/source/opt/function.cpp b/source/opt/function.cpp index dc5320f6..d70d7f85 100644 --- a/source/opt/function.cpp +++ b/source/opt/function.cpp @@ -24,7 +24,7 @@ Function* Function::Clone(IRContext* context) const { new Function(std::unique_ptr<Instruction>(DefInst().Clone(context))); clone->params_.reserve(params_.size()); ForEachParam( - [clone,context](const Instruction* inst) { + [clone, context](const Instruction* inst) { clone->AddParameter(std::unique_ptr<Instruction>(inst->Clone(context))); }, true); @@ -36,7 +36,8 @@ Function* Function::Clone(IRContext* context) const { clone->AddBasicBlock(std::move(bb)); } - clone->SetFunctionEnd(std::unique_ptr<Instruction>(function_end().Clone(context))); + clone->SetFunctionEnd( + std::unique_ptr<Instruction>(function_end().Clone(context))); return clone; } diff --git a/source/opt/inline_pass.cpp b/source/opt/inline_pass.cpp index f813f998..4b1ba58f 100644 --- a/source/opt/inline_pass.cpp +++ b/source/opt/inline_pass.cpp @@ -146,7 +146,8 @@ void InlinePass::CloneAndMapLocals( std::unique_ptr<ir::Instruction> var_inst( callee_var_itr->Clone(callee_var_itr->context())); uint32_t newId = TakeNextId(); - get_decoration_mgr()->CloneDecorations(callee_var_itr->result_id(), newId, update_def_use_mgr_); + get_decoration_mgr()->CloneDecorations(callee_var_itr->result_id(), newId, + update_def_use_mgr_); var_inst->SetResultId(newId); (*callee2caller)[callee_var_itr->result_id()] = newId; new_vars->push_back(std::move(var_inst)); @@ -175,7 +176,8 @@ uint32_t InlinePass::CreateReturnVar( {SpvStorageClassFunction}}})); new_vars->push_back(std::move(var_inst)); } - get_decoration_mgr()->CloneDecorations(calleeFn->result_id(), returnVarId, update_def_use_mgr_); + get_decoration_mgr()->CloneDecorations(calleeFn->result_id(), returnVarId, + update_def_use_mgr_); return returnVarId; } @@ -188,30 +190,30 @@ void InlinePass::CloneSameBlockOps( std::unordered_map<uint32_t, uint32_t>* postCallSB, std::unordered_map<uint32_t, ir::Instruction*>* preCallSB, std::unique_ptr<ir::BasicBlock>* block_ptr) { - (*inst)->ForEachInId( - [&postCallSB, &preCallSB, &block_ptr, this](uint32_t* iid) { - const auto mapItr = (*postCallSB).find(*iid); - if (mapItr == (*postCallSB).end()) { - const auto mapItr2 = (*preCallSB).find(*iid); - if (mapItr2 != (*preCallSB).end()) { - // Clone pre-call same-block ops, map result id. - const ir::Instruction* inInst = mapItr2->second; - std::unique_ptr<ir::Instruction> sb_inst( - inInst->Clone(inInst->context())); - CloneSameBlockOps(&sb_inst, postCallSB, preCallSB, block_ptr); - const uint32_t rid = sb_inst->result_id(); - const uint32_t nid = this->TakeNextId(); - get_decoration_mgr()->CloneDecorations(rid, nid, update_def_use_mgr_); - sb_inst->SetResultId(nid); - (*postCallSB)[rid] = nid; - *iid = nid; - (*block_ptr)->AddInstruction(std::move(sb_inst)); - } - } else { - // Reset same-block op operand. - *iid = mapItr->second; - } - }); + (*inst)->ForEachInId([&postCallSB, &preCallSB, &block_ptr, + this](uint32_t* iid) { + const auto mapItr = (*postCallSB).find(*iid); + if (mapItr == (*postCallSB).end()) { + const auto mapItr2 = (*preCallSB).find(*iid); + if (mapItr2 != (*preCallSB).end()) { + // Clone pre-call same-block ops, map result id. + const ir::Instruction* inInst = mapItr2->second; + std::unique_ptr<ir::Instruction> sb_inst( + inInst->Clone(inInst->context())); + CloneSameBlockOps(&sb_inst, postCallSB, preCallSB, block_ptr); + const uint32_t rid = sb_inst->result_id(); + const uint32_t nid = this->TakeNextId(); + get_decoration_mgr()->CloneDecorations(rid, nid, update_def_use_mgr_); + sb_inst->SetResultId(nid); + (*postCallSB)[rid] = nid; + *iid = nid; + (*block_ptr)->AddInstruction(std::move(sb_inst)); + } + } else { + // Reset same-block op operand. + *iid = mapItr->second; + } + }); } void InlinePass::GenInlineCode( @@ -649,7 +651,7 @@ void InlinePass::InitializeInline(ir::IRContext* c) { InitializeProcessing(c); // Don't bother updating the DefUseManger - update_def_use_mgr_ = [this] (ir::Instruction&, bool) {}; + update_def_use_mgr_ = [this](ir::Instruction&, bool) {}; false_id_ = 0; diff --git a/source/opt/inline_pass.h b/source/opt/inline_pass.h index 7ecf772f..0b037bc9 100644 --- a/source/opt/inline_pass.h +++ b/source/opt/inline_pass.h @@ -23,8 +23,8 @@ #include <unordered_map> #include <vector> -#include "def_use_manager.h" #include "decoration_manager.h" +#include "def_use_manager.h" #include "module.h" #include "pass.h" diff --git a/source/opt/insert_extract_elim.cpp b/source/opt/insert_extract_elim.cpp index b506d9eb..d249e421 100644 --- a/source/opt/insert_extract_elim.cpp +++ b/source/opt/insert_extract_elim.cpp @@ -43,9 +43,9 @@ bool InsertExtractElimPass::ExtInsMatch(const ir::Instruction* extInst, return true; } -bool InsertExtractElimPass::ExtInsConflict( - const ir::Instruction* extInst, const ir::Instruction* insInst, - const uint32_t extOffset) const { +bool InsertExtractElimPass::ExtInsConflict(const ir::Instruction* extInst, + const ir::Instruction* insInst, + const uint32_t extOffset) const { if (extInst->NumInOperands() - extOffset == insInst->NumInOperands() - 1) return false; uint32_t extNumIdx = extInst->NumInOperands() - 1 - extOffset; @@ -80,21 +80,18 @@ bool InsertExtractElimPass::EliminateInsertExtract(ir::Function* func) { // Match! Use inserted value as replacement replId = cinst->GetSingleWordInOperand(kInsertObjectIdInIdx); break; - } - else if (ExtInsConflict(&*ii, cinst, extOffset)) { + } else if (ExtInsConflict(&*ii, cinst, extOffset)) { // If extract has fewer indices than the insert, stop searching. // Otherwise increment offset of extract indices considered and // continue searching through the inserted value if (ii->NumInOperands() - extOffset < cinst->NumInOperands() - 1) { break; - } - else { + } else { extOffset += cinst->NumInOperands() - 2; cid = cinst->GetSingleWordInOperand(kInsertObjectIdInIdx); } - } - else { + } else { // Consider next composite in insert chain cid = cinst->GetSingleWordInOperand(kInsertCompositeIdInIdx); } diff --git a/source/opt/insert_extract_elim.h b/source/opt/insert_extract_elim.h index cb0f4f3f..ab3d8f4e 100644 --- a/source/opt/insert_extract_elim.h +++ b/source/opt/insert_extract_elim.h @@ -41,7 +41,7 @@ class InsertExtractElimPass : public Pass { private: // Return true if indices of extract |extInst| starting at |extOffset| - // match indices of insert |insInst|. + // match indices of insert |insInst|. bool ExtInsMatch(const ir::Instruction* extInst, const ir::Instruction* insInst, const uint32_t extOffset) const; diff --git a/source/opt/instruction.h b/source/opt/instruction.h index 962b66b3..a046e8dc 100644 --- a/source/opt/instruction.h +++ b/source/opt/instruction.h @@ -126,7 +126,7 @@ class Instruction : public utils::IntrusiveNodeBase<Instruction> { // It is the responsibility of the caller to make sure that the storage is // removed. It is the caller's responsibility to make sure that there is only // one instruction for each result id. - Instruction* Clone(IRContext *c) const; + Instruction* Clone(IRContext* c) const; IRContext* context() const { return context_; } diff --git a/source/opt/ir_context.cpp b/source/opt/ir_context.cpp index 2dd947e4..3afdd646 100644 --- a/source/opt/ir_context.cpp +++ b/source/opt/ir_context.cpp @@ -93,12 +93,14 @@ bool IRContext::ReplaceAllUsesWith(uint32_t before, uint32_t after) { if (before == after) return false; // Ensure that |after| has been registered as def. - assert(get_def_use_mgr()->GetDef(after) && "'after' is not a registered def."); + assert(get_def_use_mgr()->GetDef(after) && + "'after' is not a registered def."); - std::vector<std::pair<ir::Instruction*,uint32_t>> uses_to_update; - get_def_use_mgr()->ForEachUse(before, [&uses_to_update](ir::Instruction* user, uint32_t index) { - uses_to_update.emplace_back(user, index); - }); + std::vector<std::pair<ir::Instruction*, uint32_t>> uses_to_update; + get_def_use_mgr()->ForEachUse( + before, [&uses_to_update](ir::Instruction* user, uint32_t index) { + uses_to_update.emplace_back(user, index); + }); ir::Instruction* prev = nullptr; for (auto p : uses_to_update) { @@ -421,11 +423,11 @@ void IRContext::AddCombinatorsForExtension(ir::Instruction* extension) { } void IRContext::InitializeCombinators() { - for( auto& capability : module()->capabilities()) { + for (auto& capability : module()->capabilities()) { AddCombinatorsForCapability(capability.GetSingleWordInOperand(0)); } - for( auto& extension : module()->ext_inst_imports()) { + for (auto& extension : module()->ext_inst_imports()) { AddCombinatorsForExtension(&extension); } diff --git a/source/opt/ir_context.h b/source/opt/ir_context.h index 468dbc5e..12499be4 100644 --- a/source/opt/ir_context.h +++ b/source/opt/ir_context.h @@ -343,7 +343,7 @@ class IRContext { // Opcodes of shader capability core executable instructions // without side-effect. - std::unordered_map<uint32_t, std::unordered_set<uint32_t>> combinator_ops_; + std::unordered_map<uint32_t, std::unordered_set<uint32_t>> combinator_ops_; }; inline ir::IRContext::Analysis operator|(ir::IRContext::Analysis lhs, diff --git a/source/opt/local_access_chain_convert_pass.cpp b/source/opt/local_access_chain_convert_pass.cpp index db7bf953..b5729bb6 100644 --- a/source/opt/local_access_chain_convert_pass.cpp +++ b/source/opt/local_access_chain_convert_pass.cpp @@ -16,8 +16,8 @@ #include "local_access_chain_convert_pass.h" -#include "iterator.h" #include "ir_context.h" +#include "iterator.h" namespace spvtools { namespace opt { @@ -139,17 +139,18 @@ bool LocalAccessChainConvertPass::IsConstantIndexAccessChain( bool LocalAccessChainConvertPass::HasOnlySupportedRefs(uint32_t ptrId) { if (supported_ref_ptrs_.find(ptrId) != supported_ref_ptrs_.end()) return true; bool hasOnlySupportedRefs = true; - get_def_use_mgr()->ForEachUser(ptrId, [this,&hasOnlySupportedRefs](ir::Instruction* user) { - SpvOp op = user->opcode(); - if (IsNonPtrAccessChain(op) || op == SpvOpCopyObject) { - if (!HasOnlySupportedRefs(user->result_id())) { - hasOnlySupportedRefs = false; - } - } else if (op != SpvOpStore && op != SpvOpLoad && op != SpvOpName && - !IsNonTypeDecorate(op)) { - hasOnlySupportedRefs = false; - } - }); + get_def_use_mgr()->ForEachUser( + ptrId, [this, &hasOnlySupportedRefs](ir::Instruction* user) { + SpvOp op = user->opcode(); + if (IsNonPtrAccessChain(op) || op == SpvOpCopyObject) { + if (!HasOnlySupportedRefs(user->result_id())) { + hasOnlySupportedRefs = false; + } + } else if (op != SpvOpStore && op != SpvOpLoad && op != SpvOpName && + !IsNonTypeDecorate(op)) { + hasOnlySupportedRefs = false; + } + }); if (hasOnlySupportedRefs) { supported_ref_ptrs_.insert(ptrId); } @@ -174,9 +175,8 @@ void LocalAccessChainConvertPass::FindTargetVars(ir::Function* func) { } // Rule out variables with nested access chains // TODO(): Convert nested access chains - if (IsNonPtrAccessChain(op) && - ptrInst->GetSingleWordInOperand(kAccessChainPtrIdInIdx) != - varId) { + if (IsNonPtrAccessChain(op) && ptrInst->GetSingleWordInOperand( + kAccessChainPtrIdInIdx) != varId) { seen_non_target_vars_.insert(varId); seen_target_vars_.erase(varId); break; @@ -298,18 +298,27 @@ void LocalAccessChainConvertPass::InitExtensions() { extensions_whitelist_.clear(); extensions_whitelist_.insert({ "SPV_AMD_shader_explicit_vertex_parameter", - "SPV_AMD_shader_trinary_minmax", "SPV_AMD_gcn_shader", - "SPV_KHR_shader_ballot", "SPV_AMD_shader_ballot", - "SPV_AMD_gpu_shader_half_float", "SPV_KHR_shader_draw_parameters", - "SPV_KHR_subgroup_vote", "SPV_KHR_16bit_storage", "SPV_KHR_device_group", - "SPV_KHR_multiview", "SPV_NVX_multiview_per_view_attributes", - "SPV_NV_viewport_array2", "SPV_NV_stereo_view_rendering", + "SPV_AMD_shader_trinary_minmax", + "SPV_AMD_gcn_shader", + "SPV_KHR_shader_ballot", + "SPV_AMD_shader_ballot", + "SPV_AMD_gpu_shader_half_float", + "SPV_KHR_shader_draw_parameters", + "SPV_KHR_subgroup_vote", + "SPV_KHR_16bit_storage", + "SPV_KHR_device_group", + "SPV_KHR_multiview", + "SPV_NVX_multiview_per_view_attributes", + "SPV_NV_viewport_array2", + "SPV_NV_stereo_view_rendering", "SPV_NV_sample_mask_override_coverage", - "SPV_NV_geometry_shader_passthrough", "SPV_AMD_texture_gather_bias_lod", + "SPV_NV_geometry_shader_passthrough", + "SPV_AMD_texture_gather_bias_lod", "SPV_KHR_storage_buffer_storage_class", // SPV_KHR_variable_pointers // Currently do not support extended pointer expressions - "SPV_AMD_gpu_shader_int16", "SPV_KHR_post_depth_coverage", + "SPV_AMD_gpu_shader_int16", + "SPV_KHR_post_depth_coverage", "SPV_KHR_shader_atomic_counter_ops", }); } diff --git a/source/opt/local_single_block_elim_pass.cpp b/source/opt/local_single_block_elim_pass.cpp index 95e91796..3ef8e0d0 100644 --- a/source/opt/local_single_block_elim_pass.cpp +++ b/source/opt/local_single_block_elim_pass.cpp @@ -30,17 +30,18 @@ const uint32_t kStoreValIdInIdx = 1; bool LocalSingleBlockLoadStoreElimPass::HasOnlySupportedRefs(uint32_t ptrId) { if (supported_ref_ptrs_.find(ptrId) != supported_ref_ptrs_.end()) return true; bool hasOnlySupportedRefs = true; - get_def_use_mgr()->ForEachUser(ptrId, [this,&hasOnlySupportedRefs](ir::Instruction* user) { - SpvOp op = user->opcode(); - if (IsNonPtrAccessChain(op) || op == SpvOpCopyObject) { - if (!HasOnlySupportedRefs(user->result_id())) { - hasOnlySupportedRefs = false; - } - } else if (op != SpvOpStore && op != SpvOpLoad && op != SpvOpName && - !this->IsNonTypeDecorate(op)) { - hasOnlySupportedRefs = false; - } - }); + get_def_use_mgr()->ForEachUser( + ptrId, [this, &hasOnlySupportedRefs](ir::Instruction* user) { + SpvOp op = user->opcode(); + if (IsNonPtrAccessChain(op) || op == SpvOpCopyObject) { + if (!HasOnlySupportedRefs(user->result_id())) { + hasOnlySupportedRefs = false; + } + } else if (op != SpvOpStore && op != SpvOpLoad && op != SpvOpName && + !this->IsNonTypeDecorate(op)) { + hasOnlySupportedRefs = false; + } + }); if (hasOnlySupportedRefs) { supported_ref_ptrs_.insert(ptrId); } @@ -187,18 +188,27 @@ void LocalSingleBlockLoadStoreElimPass::InitExtensions() { extensions_whitelist_.clear(); extensions_whitelist_.insert({ "SPV_AMD_shader_explicit_vertex_parameter", - "SPV_AMD_shader_trinary_minmax", "SPV_AMD_gcn_shader", - "SPV_KHR_shader_ballot", "SPV_AMD_shader_ballot", - "SPV_AMD_gpu_shader_half_float", "SPV_KHR_shader_draw_parameters", - "SPV_KHR_subgroup_vote", "SPV_KHR_16bit_storage", "SPV_KHR_device_group", - "SPV_KHR_multiview", "SPV_NVX_multiview_per_view_attributes", - "SPV_NV_viewport_array2", "SPV_NV_stereo_view_rendering", + "SPV_AMD_shader_trinary_minmax", + "SPV_AMD_gcn_shader", + "SPV_KHR_shader_ballot", + "SPV_AMD_shader_ballot", + "SPV_AMD_gpu_shader_half_float", + "SPV_KHR_shader_draw_parameters", + "SPV_KHR_subgroup_vote", + "SPV_KHR_16bit_storage", + "SPV_KHR_device_group", + "SPV_KHR_multiview", + "SPV_NVX_multiview_per_view_attributes", + "SPV_NV_viewport_array2", + "SPV_NV_stereo_view_rendering", "SPV_NV_sample_mask_override_coverage", - "SPV_NV_geometry_shader_passthrough", "SPV_AMD_texture_gather_bias_lod", + "SPV_NV_geometry_shader_passthrough", + "SPV_AMD_texture_gather_bias_lod", "SPV_KHR_storage_buffer_storage_class", // SPV_KHR_variable_pointers // Currently do not support extended pointer expressions - "SPV_AMD_gpu_shader_int16", "SPV_KHR_post_depth_coverage", + "SPV_AMD_gpu_shader_int16", + "SPV_KHR_post_depth_coverage", "SPV_KHR_shader_atomic_counter_ops", }); } diff --git a/source/opt/local_single_block_elim_pass.h b/source/opt/local_single_block_elim_pass.h index 4edf116c..fa68788c 100644 --- a/source/opt/local_single_block_elim_pass.h +++ b/source/opt/local_single_block_elim_pass.h @@ -42,7 +42,7 @@ class LocalSingleBlockLoadStoreElimPass : public MemPass { ir::IRContext::Analysis GetPreservedAnalyses() override { return ir::IRContext::kAnalysisDefUse; } - + private: // Return true if all uses of |varId| are only through supported reference // operations ie. loads and store. Also cache in supported_ref_ptrs_. diff --git a/source/opt/local_single_store_elim_pass.cpp b/source/opt/local_single_store_elim_pass.cpp index b613f8a4..c7bda38f 100644 --- a/source/opt/local_single_store_elim_pass.cpp +++ b/source/opt/local_single_store_elim_pass.cpp @@ -32,17 +32,18 @@ const uint32_t kStoreValIdInIdx = 1; bool LocalSingleStoreElimPass::HasOnlySupportedRefs(uint32_t ptrId) { if (supported_ref_ptrs_.find(ptrId) != supported_ref_ptrs_.end()) return true; bool hasOnlySupportedRefs = true; - get_def_use_mgr()->ForEachUser(ptrId, [this,&hasOnlySupportedRefs](ir::Instruction* user) { - SpvOp op = user->opcode(); - if (IsNonPtrAccessChain(op) || op == SpvOpCopyObject) { - if (!HasOnlySupportedRefs(user->result_id())) { - hasOnlySupportedRefs = false; - } - } else if (op != SpvOpStore && op != SpvOpLoad && op != SpvOpName && - !IsNonTypeDecorate(op)) { - hasOnlySupportedRefs = false; - } - }); + get_def_use_mgr()->ForEachUser( + ptrId, [this, &hasOnlySupportedRefs](ir::Instruction* user) { + SpvOp op = user->opcode(); + if (IsNonPtrAccessChain(op) || op == SpvOpCopyObject) { + if (!HasOnlySupportedRefs(user->result_id())) { + hasOnlySupportedRefs = false; + } + } else if (op != SpvOpStore && op != SpvOpLoad && op != SpvOpName && + !IsNonTypeDecorate(op)) { + hasOnlySupportedRefs = false; + } + }); if (hasOnlySupportedRefs) { supported_ref_ptrs_.insert(ptrId); } @@ -283,18 +284,27 @@ void LocalSingleStoreElimPass::InitExtensions() { extensions_whitelist_.clear(); extensions_whitelist_.insert({ "SPV_AMD_shader_explicit_vertex_parameter", - "SPV_AMD_shader_trinary_minmax", "SPV_AMD_gcn_shader", - "SPV_KHR_shader_ballot", "SPV_AMD_shader_ballot", - "SPV_AMD_gpu_shader_half_float", "SPV_KHR_shader_draw_parameters", - "SPV_KHR_subgroup_vote", "SPV_KHR_16bit_storage", "SPV_KHR_device_group", - "SPV_KHR_multiview", "SPV_NVX_multiview_per_view_attributes", - "SPV_NV_viewport_array2", "SPV_NV_stereo_view_rendering", + "SPV_AMD_shader_trinary_minmax", + "SPV_AMD_gcn_shader", + "SPV_KHR_shader_ballot", + "SPV_AMD_shader_ballot", + "SPV_AMD_gpu_shader_half_float", + "SPV_KHR_shader_draw_parameters", + "SPV_KHR_subgroup_vote", + "SPV_KHR_16bit_storage", + "SPV_KHR_device_group", + "SPV_KHR_multiview", + "SPV_NVX_multiview_per_view_attributes", + "SPV_NV_viewport_array2", + "SPV_NV_stereo_view_rendering", "SPV_NV_sample_mask_override_coverage", - "SPV_NV_geometry_shader_passthrough", "SPV_AMD_texture_gather_bias_lod", + "SPV_NV_geometry_shader_passthrough", + "SPV_AMD_texture_gather_bias_lod", "SPV_KHR_storage_buffer_storage_class", // SPV_KHR_variable_pointers // Currently do not support extended pointer expressions - "SPV_AMD_gpu_shader_int16", "SPV_KHR_post_depth_coverage", + "SPV_AMD_gpu_shader_int16", + "SPV_KHR_post_depth_coverage", "SPV_KHR_shader_atomic_counter_ops", }); } diff --git a/source/opt/local_single_store_elim_pass.h b/source/opt/local_single_store_elim_pass.h index 1ecc8677..80175f08 100644 --- a/source/opt/local_single_store_elim_pass.h +++ b/source/opt/local_single_store_elim_pass.h @@ -40,7 +40,7 @@ class LocalSingleStoreElimPass : public MemPass { LocalSingleStoreElimPass(); const char* name() const override { return "eliminate-local-single-store"; } Status Process(ir::IRContext* irContext) override; - + ir::IRContext::Analysis GetPreservedAnalyses() override { return ir::IRContext::kAnalysisDefUse; } diff --git a/source/opt/local_ssa_elim_pass.cpp b/source/opt/local_ssa_elim_pass.cpp index f0aaee7d..02ee9794 100644 --- a/source/opt/local_ssa_elim_pass.cpp +++ b/source/opt/local_ssa_elim_pass.cpp @@ -96,18 +96,27 @@ void LocalMultiStoreElimPass::InitExtensions() { extensions_whitelist_.clear(); extensions_whitelist_.insert({ "SPV_AMD_shader_explicit_vertex_parameter", - "SPV_AMD_shader_trinary_minmax", "SPV_AMD_gcn_shader", - "SPV_KHR_shader_ballot", "SPV_AMD_shader_ballot", - "SPV_AMD_gpu_shader_half_float", "SPV_KHR_shader_draw_parameters", - "SPV_KHR_subgroup_vote", "SPV_KHR_16bit_storage", "SPV_KHR_device_group", - "SPV_KHR_multiview", "SPV_NVX_multiview_per_view_attributes", - "SPV_NV_viewport_array2", "SPV_NV_stereo_view_rendering", + "SPV_AMD_shader_trinary_minmax", + "SPV_AMD_gcn_shader", + "SPV_KHR_shader_ballot", + "SPV_AMD_shader_ballot", + "SPV_AMD_gpu_shader_half_float", + "SPV_KHR_shader_draw_parameters", + "SPV_KHR_subgroup_vote", + "SPV_KHR_16bit_storage", + "SPV_KHR_device_group", + "SPV_KHR_multiview", + "SPV_NVX_multiview_per_view_attributes", + "SPV_NV_viewport_array2", + "SPV_NV_stereo_view_rendering", "SPV_NV_sample_mask_override_coverage", - "SPV_NV_geometry_shader_passthrough", "SPV_AMD_texture_gather_bias_lod", + "SPV_NV_geometry_shader_passthrough", + "SPV_AMD_texture_gather_bias_lod", "SPV_KHR_storage_buffer_storage_class", // SPV_KHR_variable_pointers // Currently do not support extended pointer expressions - "SPV_AMD_gpu_shader_int16", "SPV_KHR_post_depth_coverage", + "SPV_AMD_gpu_shader_int16", + "SPV_KHR_post_depth_coverage", "SPV_KHR_shader_atomic_counter_ops", }); } diff --git a/source/opt/mem_pass.cpp b/source/opt/mem_pass.cpp index 848cbb55..8062e0f2 100644 --- a/source/opt/mem_pass.cpp +++ b/source/opt/mem_pass.cpp @@ -18,8 +18,8 @@ #include "basic_block.h" #include "cfa.h" -#include "iterator.h" #include "ir_context.h" +#include "iterator.h" namespace spvtools { namespace opt { @@ -119,24 +119,24 @@ ir::Instruction* MemPass::GetPtr(ir::Instruction* ip, uint32_t* varId) { bool MemPass::HasOnlyNamesAndDecorates(uint32_t id) const { bool hasOnlyNamesAndDecorates = true; - get_def_use_mgr()->ForEachUser(id, [this, &hasOnlyNamesAndDecorates](ir::Instruction* user) { - SpvOp op = user->opcode(); - if (op != SpvOpName && !IsNonTypeDecorate(op)) { - hasOnlyNamesAndDecorates = false; - } - }); + get_def_use_mgr()->ForEachUser( + id, [this, &hasOnlyNamesAndDecorates](ir::Instruction* user) { + SpvOp op = user->opcode(); + if (op != SpvOpName && !IsNonTypeDecorate(op)) { + hasOnlyNamesAndDecorates = false; + } + }); return hasOnlyNamesAndDecorates; } void MemPass::KillAllInsts(ir::BasicBlock* bp) { - bp->ForEachInst([this](ir::Instruction* ip) { - context()->KillInst(ip); - }); + bp->ForEachInst([this](ir::Instruction* ip) { context()->KillInst(ip); }); } bool MemPass::HasLoads(uint32_t varId) const { bool hasLoads = false; - get_def_use_mgr()->ForEachUser(varId, [this, &hasLoads](ir::Instruction* user) { + get_def_use_mgr()->ForEachUser(varId, [this, + &hasLoads](ir::Instruction* user) { SpvOp op = user->opcode(); // TODO(): The following is slightly conservative. Could be // better handling of non-store/name. @@ -173,7 +173,7 @@ bool MemPass::IsLiveStore(ir::Instruction* storeInst) { } void MemPass::AddStores(uint32_t ptr_id, std::queue<ir::Instruction*>* insts) { - get_def_use_mgr()->ForEachUser(ptr_id, [this,insts](ir::Instruction* user) { + get_def_use_mgr()->ForEachUser(ptr_id, [this, insts](ir::Instruction* user) { SpvOp op = user->opcode(); if (IsNonPtrAccessChain(op)) { AddStores(user->result_id(), insts); @@ -224,13 +224,14 @@ MemPass::MemPass() {} bool MemPass::HasOnlySupportedRefs(uint32_t varId) { if (supported_ref_vars_.find(varId) != supported_ref_vars_.end()) return true; bool hasOnlySupportedRefs = true; - get_def_use_mgr()->ForEachUser(varId, [this,&hasOnlySupportedRefs](ir::Instruction* user) { - SpvOp op = user->opcode(); - if (op != SpvOpStore && op != SpvOpLoad && op != SpvOpName && - !IsNonTypeDecorate(op)) { - hasOnlySupportedRefs = false; - } - }); + get_def_use_mgr()->ForEachUser( + varId, [this, &hasOnlySupportedRefs](ir::Instruction* user) { + SpvOp op = user->opcode(); + if (op != SpvOpStore && op != SpvOpLoad && op != SpvOpName && + !IsNonTypeDecorate(op)) { + hasOnlySupportedRefs = false; + } + }); return hasOnlySupportedRefs; } @@ -402,8 +403,8 @@ void MemPass::SSABlockInitLoopHeader( {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {predLabel}}); } const uint32_t phiId = TakeNextId(); - std::unique_ptr<ir::Instruction> newPhi( - new ir::Instruction(context(), SpvOpPhi, typeId, phiId, phi_in_operands)); + std::unique_ptr<ir::Instruction> newPhi(new ir::Instruction( + context(), SpvOpPhi, typeId, phiId, phi_in_operands)); // The only phis requiring patching are the ones we create. phis_to_patch_.insert(phiId); // Only analyze the phi define now; analyze the phi uses after the @@ -470,8 +471,8 @@ void MemPass::SSABlockInitMultiPred(ir::BasicBlock* block_ptr) { {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {predLabel}}); } const uint32_t phiId = TakeNextId(); - std::unique_ptr<ir::Instruction> newPhi( - new ir::Instruction(context(), SpvOpPhi, typeId, phiId, phi_in_operands)); + std::unique_ptr<ir::Instruction> newPhi(new ir::Instruction( + context(), SpvOpPhi, typeId, phiId, phi_in_operands)); get_def_use_mgr()->AnalyzeInstDefUse(&*newPhi); insertItr = insertItr.InsertBefore(std::move(newPhi)); ++insertItr; @@ -699,7 +700,7 @@ void MemPass::RemovePhiOperands( // In all other cases, the operand must be kept but may need to be changed. uint32_t arg_id = phi->GetSingleWordOperand(i); - ir::Instruction *arg_def_instr = get_def_use_mgr()->GetDef(arg_id); + ir::Instruction* arg_def_instr = get_def_use_mgr()->GetDef(arg_id); ir::BasicBlock* def_block = context()->get_instr_block(arg_def_instr); if (def_block && reachable_blocks.find(def_block) == reachable_blocks.end()) { @@ -794,8 +795,10 @@ bool MemPass::RemoveUnreachableBlocks(ir::Function* func) { // If the block is reachable and has Phi instructions, remove all // operands from its Phi instructions that reference unreachable blocks. // If the block has no Phi instructions, this is a no-op. - block.ForEachPhiInst([&block, &reachable_blocks, this]( - ir::Instruction* phi) { RemovePhiOperands(phi, reachable_blocks); }); + block.ForEachPhiInst( + [&block, &reachable_blocks, this](ir::Instruction* phi) { + RemovePhiOperands(phi, reachable_blocks); + }); } // Erase unreachable blocks. diff --git a/source/opt/passes.h b/source/opt/passes.h index 3a6e17e5..da5f910c 100644 --- a/source/opt/passes.h +++ b/source/opt/passes.h @@ -33,15 +33,15 @@ #include "inline_opaque_pass.h" #include "insert_extract_elim.h" #include "local_access_chain_convert_pass.h" +#include "local_redundancy_elimination.h" #include "local_single_block_elim_pass.h" #include "local_single_store_elim_pass.h" #include "local_ssa_elim_pass.h" +#include "merge_return_pass.h" #include "null_pass.h" #include "set_spec_constant_default_value_pass.h" #include "strength_reduction_pass.h" #include "strip_debug_info_pass.h" #include "unify_const_pass.h" -#include "merge_return_pass.h" -#include "local_redundancy_elimination.h" #endif // LIBSPIRV_OPT_PASSES_H_ diff --git a/source/opt/remove_duplicates_pass.cpp b/source/opt/remove_duplicates_pass.cpp index ee6104dc..168c322e 100644 --- a/source/opt/remove_duplicates_pass.cpp +++ b/source/opt/remove_duplicates_pass.cpp @@ -32,8 +32,8 @@ namespace opt { using ir::Instruction; using ir::Module; using ir::Operand; -using opt::analysis::DefUseManager; using opt::analysis::DecorationManager; +using opt::analysis::DefUseManager; Pass::Status RemoveDuplicatesPass::Process(ir::IRContext* irContext) { bool modified = RemoveDuplicateCapabilities(irContext); @@ -92,7 +92,8 @@ bool RemoveDuplicatesPass::RemoveDuplicatesExtInstImports( return modified; } -bool RemoveDuplicatesPass::RemoveDuplicateTypes(ir::IRContext* irContext) const { +bool RemoveDuplicatesPass::RemoveDuplicateTypes( + ir::IRContext* irContext) const { bool modified = false; std::vector<Instruction> visitedTypes; @@ -109,7 +110,8 @@ bool RemoveDuplicatesPass::RemoveDuplicateTypes(ir::IRContext* irContext) const // Is the current type equal to one of the types we have aready visited? SpvId idToKeep = 0u; for (auto j : visitedTypes) { - if (AreTypesEqual(*i, j, *irContext->get_def_use_mgr(), *irContext->get_decoration_mgr())) { + if (AreTypesEqual(*i, j, *irContext->get_def_use_mgr(), + *irContext->get_decoration_mgr())) { idToKeep = j.result_id(); break; } diff --git a/source/opt/set_spec_constant_default_value_pass.cpp b/source/opt/set_spec_constant_default_value_pass.cpp index a8f3e033..3ec786f7 100644 --- a/source/opt/set_spec_constant_default_value_pass.cpp +++ b/source/opt/set_spec_constant_default_value_pass.cpp @@ -32,10 +32,10 @@ namespace spvtools { namespace opt { namespace { -using spvutils::NumberType; using spvutils::EncodeNumberStatus; -using spvutils::ParseNumber; +using spvutils::NumberType; using spvutils::ParseAndEncodeNumber; +using spvutils::ParseNumber; // Given a numeric value in a null-terminated c string and the expected type of // the value, parses the string and encodes it in a vector of words. If the @@ -137,11 +137,12 @@ ir::Instruction* GetSpecIdTargetFromDecorationGroup( // the first OpGroupDecoration instruction that uses the given decoration // group. ir::Instruction* group_decorate_inst = nullptr; - def_use_mgr->ForEachUser(&decoration_group_defining_inst, [&group_decorate_inst](ir::Instruction* user) { - if (user->opcode() == SpvOp::SpvOpGroupDecorate) { - group_decorate_inst = user; - } - }); + def_use_mgr->ForEachUser(&decoration_group_defining_inst, + [&group_decorate_inst](ir::Instruction* user) { + if (user->opcode() == SpvOp::SpvOpGroupDecorate) { + group_decorate_inst = user; + } + }); if (!group_decorate_inst) return nullptr; // Scan through the target ids of the OpGroupDecorate instruction. There @@ -184,7 +185,7 @@ ir::Instruction* GetSpecIdTargetFromDecorationGroup( } return target_inst; } -}; +}; // namespace Pass::Status SetSpecConstantDefaultValuePass::Process( ir::IRContext* irContext) { diff --git a/source/opt/strength_reduction_pass.cpp b/source/opt/strength_reduction_pass.cpp index 28da788b..75118a3d 100644 --- a/source/opt/strength_reduction_pass.cpp +++ b/source/opt/strength_reduction_pass.cpp @@ -100,8 +100,8 @@ bool StrengthReductionPass::ReplaceMultiplyByPowerOf2( {shiftConstResultId}); newOperands.push_back(shiftOperand); std::unique_ptr<ir::Instruction> newInstruction( - new ir::Instruction(context(), SpvOp::SpvOpShiftLeftLogical, inst->type_id(), - newResultId, newOperands)); + new ir::Instruction(context(), SpvOp::SpvOpShiftLeftLogical, + inst->type_id(), newResultId, newOperands)); // Insert the new instruction and update the data structures. inst = inst.InsertBefore(std::move(newInstruction)); @@ -160,8 +160,9 @@ uint32_t StrengthReductionPass::GetConstantId(uint32_t val) { uint32_t resultId = TakeNextId(); ir::Operand constant(spv_operand_type_t::SPV_OPERAND_TYPE_LITERAL_INTEGER, {val}); - std::unique_ptr<ir::Instruction> newConstant(new ir::Instruction( - context(), SpvOp::SpvOpConstant, uint32_type_id_, resultId, {constant})); + std::unique_ptr<ir::Instruction> newConstant( + new ir::Instruction(context(), SpvOp::SpvOpConstant, uint32_type_id_, + resultId, {constant})); get_module()->AddGlobalValue(std::move(newConstant)); // Notify the DefUseManager about this constant. diff --git a/source/opt/unify_const_pass.cpp b/source/opt/unify_const_pass.cpp index 088e1022..ace53de0 100644 --- a/source/opt/unify_const_pass.cpp +++ b/source/opt/unify_const_pass.cpp @@ -168,5 +168,5 @@ Pass::Status UnifyConstantPass::Process(ir::IRContext* c) { return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange; } -} // opt +} // namespace opt } // namespace spvtools diff --git a/source/opt/value_number_table.cpp b/source/opt/value_number_table.cpp index 8df22971..d4dabf23 100644 --- a/source/opt/value_number_table.cpp +++ b/source/opt/value_number_table.cpp @@ -92,7 +92,8 @@ bool ComputeSameValue::operator()(const ir::Instruction& lhs, } } - return lhs.context()->get_decoration_mgr()->HaveTheSameDecorations(lhs.result_id(), rhs.result_id()); + return lhs.context()->get_decoration_mgr()->HaveTheSameDecorations( + lhs.result_id(), rhs.result_id()); } std::size_t ValueTableHash::operator()( |