diff options
author | Steven Perron <stevenperron@google.com> | 2018-02-02 11:55:05 -0500 |
---|---|---|
committer | Steven Perron <stevenperron@google.com> | 2018-02-07 23:01:47 -0500 |
commit | 06cdb96984a347af5a670dc6b8bfc6533526eb11 (patch) | |
tree | 5fe04483e0580bde051b89ea3cb8f489120a1693 /source/opt/fold.cpp | |
parent | a61e4c13562ae65663eaa5d3b7539b80d9822084 (diff) | |
download | SPIRV-Tools-06cdb96984a347af5a670dc6b8bfc6533526eb11.tar.gz SPIRV-Tools-06cdb96984a347af5a670dc6b8bfc6533526eb11.tar.bz2 SPIRV-Tools-06cdb96984a347af5a670dc6b8bfc6533526eb11.zip |
Make use of the instruction folder.
Implementation of the simplification pass.
- Create pass that calls the instruction folder on each instruction and
propagate instructions that fold to a copy. This will do copy
propagation as well.
- Did not use the propagator engine because I want to modify the instruction
as we go along.
- Change folding to not allocate new instructions, but make changes in
place. This change had a big impact on compile time.
- Add simplification pass to the legalization passes in place of
insert-extract elimination.
- Added test cases for new folding rules.
- Added tests for the simplification pass
- Added a method to the CFG to apply a function to the basic blocks in
reverse post order.
Contributes to #1164.
Diffstat (limited to 'source/opt/fold.cpp')
-rw-r--r-- | source/opt/fold.cpp | 33 |
1 files changed, 8 insertions, 25 deletions
diff --git a/source/opt/fold.cpp b/source/opt/fold.cpp index a6c93f89..04c04469 100644 --- a/source/opt/fold.cpp +++ b/source/opt/fold.cpp @@ -182,10 +182,10 @@ uint32_t OperateWords(SpvOp opcode, } } -bool FoldInstructionInternal(ir::Instruction* inst, - std::function<uint32_t(uint32_t)> id_map) { +bool FoldInstructionInternal(ir::Instruction* inst) { ir::IRContext* context = inst->context(); - ir::Instruction* folded_inst = FoldInstructionToConstant(inst, id_map); + auto identity_map = [](uint32_t id) { return id; }; + ir::Instruction* folded_inst = FoldInstructionToConstant(inst, identity_map); if (folded_inst != nullptr) { inst->SetOpcode(SpvOpCopyObject); inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {folded_inst->result_id()}}}); @@ -201,8 +201,7 @@ bool FoldInstructionInternal(ir::Instruction* inst, if (operand->type != SPV_OPERAND_TYPE_ID) { constants.push_back(nullptr); } else { - uint32_t id = id_map(operand->words[0]); - inst->SetInOperand(i, {id}); + uint32_t id = operand->words[0]; const analysis::Constant* constant = const_manger->FindDeclaredConstant(id); constants.push_back(constant); @@ -660,29 +659,13 @@ bool IsFoldableType(ir::Instruction* type_inst) { return false; } -ir::Instruction* FoldInstruction(ir::Instruction* inst, - std::function<uint32_t(uint32_t)> id_map) { - ir::IRContext* context = inst->context(); +bool FoldInstruction(ir::Instruction* inst) { bool modified = false; - std::unique_ptr<ir::Instruction> folded_inst(inst->Clone(context)); - while (FoldInstructionInternal(&*folded_inst, id_map)) { + ir::Instruction* folded_inst(inst); + while (FoldInstructionInternal(&*folded_inst)) { modified = true; } - - if (modified) { - if (folded_inst->opcode() == SpvOpCopyObject) { - analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr(); - return def_use_mgr->GetDef(folded_inst->GetSingleWordInOperand(0)); - } else { - InstructionBuilder ir_builder( - context, inst, - ir::IRContext::kAnalysisDefUse | - ir::IRContext::kAnalysisInstrToBlockMapping); - folded_inst->SetResultId(context->TakeNextId()); - return ir_builder.AddInstruction(std::move(folded_inst)); - } - } - return nullptr; + return modified; } } // namespace opt |