diff options
Diffstat (limited to 'source/opt/fold_spec_constant_op_and_composite_pass.cpp')
-rw-r--r-- | source/opt/fold_spec_constant_op_and_composite_pass.cpp | 34 |
1 files changed, 16 insertions, 18 deletions
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 8d68850a0..7a5187010 100644 --- a/source/opt/fold_spec_constant_op_and_composite_pass.cpp +++ b/source/opt/fold_spec_constant_op_and_composite_pass.cpp @@ -28,6 +28,7 @@ namespace opt { Pass::Status FoldSpecConstantOpAndCompositePass::Process() { bool modified = false; + analysis::ConstantManager* const_mgr = context()->get_constant_mgr(); // Traverse through all the constant defining instructions. For Normal // Constants whose values are determined and do not depend on OpUndef // instructions, records their values in two internal maps: id_to_const_val_ @@ -62,8 +63,8 @@ Pass::Status FoldSpecConstantOpAndCompositePass::Process() { // used in OpSpecConstant{Composite|Op} instructions. // TODO(qining): If the constant or its type has decoration, we may need // to skip it. - if (context()->get_constant_mgr()->GetType(inst) && - !context()->get_constant_mgr()->GetType(inst)->decoration_empty()) + if (const_mgr->GetType(inst) && + !const_mgr->GetType(inst)->decoration_empty()) continue; switch (SpvOp opcode = inst->opcode()) { // Records the values of Normal Constants. @@ -80,15 +81,14 @@ Pass::Status FoldSpecConstantOpAndCompositePass::Process() { // Constant will be turned in to a Normal Constant. In that case, a // Constant instance should also be created successfully and recorded // in the id_to_const_val_ and const_val_to_id_ mapps. - if (auto const_value = - context()->get_constant_mgr()->GetConstantFromInst(inst)) { + if (auto const_value = const_mgr->GetConstantFromInst(inst)) { // Need to replace the OpSpecConstantComposite instruction with a // corresponding OpConstantComposite instruction. if (opcode == SpvOp::SpvOpSpecConstantComposite) { inst->SetOpcode(SpvOp::SpvOpConstantComposite); modified = true; } - context()->get_constant_mgr()->MapConstantToInst(const_value, inst); + const_mgr->MapConstantToInst(const_value, inst); } break; } @@ -146,6 +146,7 @@ bool FoldSpecConstantOpAndCompositePass::ProcessOpSpecConstantOp( Instruction* FoldSpecConstantOpAndCompositePass::FoldWithInstructionFolder( Module::inst_iterator* inst_iter_ptr) { + analysis::ConstantManager* const_mgr = context()->get_constant_mgr(); // If one of operands to the instruction is not a // constant, then we cannot fold this spec constant. for (uint32_t i = 1; i < (*inst_iter_ptr)->NumInOperands(); i++) { @@ -155,7 +156,7 @@ Instruction* FoldSpecConstantOpAndCompositePass::FoldWithInstructionFolder( continue; } uint32_t id = operand.words[0]; - if (context()->get_constant_mgr()->FindDeclaredConstant(id) == nullptr) { + if (const_mgr->FindDeclaredConstant(id) == nullptr) { return nullptr; } } @@ -202,6 +203,7 @@ Instruction* FoldSpecConstantOpAndCompositePass::FoldWithInstructionFolder( new_const_inst->InsertAfter(insert_pos); get_def_use_mgr()->AnalyzeInstDefUse(new_const_inst); } + const_mgr->MapInst(new_const_inst); return new_const_inst; } @@ -285,8 +287,8 @@ utils::SmallVector<uint32_t, 2> EncodeIntegerAsWords(const analysis::Type& type, Instruction* FoldSpecConstantOpAndCompositePass::DoComponentWiseOperation( Module::inst_iterator* pos) { const Instruction* inst = &**pos; - const analysis::Type* result_type = - context()->get_constant_mgr()->GetType(inst); + analysis::ConstantManager* const_mgr = context()->get_constant_mgr(); + const analysis::Type* result_type = const_mgr->GetType(inst); SpvOp spec_opcode = static_cast<SpvOp>(inst->GetSingleWordInOperand(0)); // Check and collect operands. std::vector<const analysis::Constant*> operands; @@ -311,10 +313,9 @@ Instruction* FoldSpecConstantOpAndCompositePass::DoComponentWiseOperation( // Scalar operation const uint32_t result_val = context()->get_instruction_folder().FoldScalars(spec_opcode, operands); - auto result_const = context()->get_constant_mgr()->GetConstant( + auto result_const = const_mgr->GetConstant( result_type, EncodeIntegerAsWords(*result_type, result_val)); - return context()->get_constant_mgr()->BuildInstructionAndAddToModule( - result_const, pos); + return const_mgr->BuildInstructionAndAddToModule(result_const, pos); } else if (result_type->AsVector()) { // Vector operation const analysis::Type* element_type = @@ -325,11 +326,10 @@ Instruction* FoldSpecConstantOpAndCompositePass::DoComponentWiseOperation( operands); std::vector<const analysis::Constant*> result_vector_components; for (const uint32_t r : result_vec) { - if (auto rc = context()->get_constant_mgr()->GetConstant( + if (auto rc = const_mgr->GetConstant( element_type, EncodeIntegerAsWords(*element_type, r))) { result_vector_components.push_back(rc); - if (!context()->get_constant_mgr()->BuildInstructionAndAddToModule( - rc, pos)) { + if (!const_mgr->BuildInstructionAndAddToModule(rc, pos)) { assert(false && "Failed to build and insert constant declaring instruction " "for the given vector component constant"); @@ -340,10 +340,8 @@ Instruction* FoldSpecConstantOpAndCompositePass::DoComponentWiseOperation( } auto new_vec_const = MakeUnique<analysis::VectorConstant>( result_type->AsVector(), result_vector_components); - auto reg_vec_const = context()->get_constant_mgr()->RegisterConstant( - std::move(new_vec_const)); - return context()->get_constant_mgr()->BuildInstructionAndAddToModule( - reg_vec_const, pos); + auto reg_vec_const = const_mgr->RegisterConstant(std::move(new_vec_const)); + return const_mgr->BuildInstructionAndAddToModule(reg_vec_const, pos); } else { // Cannot process invalid component wise operation. The result of component // wise operation must be of integer or bool scalar or vector of |