ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
onert::compiler::pass::ConstantInsertionPass Class Reference

#include <ConstantInsertionPass.h>

Collaboration diagram for onert::compiler::pass::ConstantInsertionPass:

Public Member Functions

std::string id () final
 Returns string id for this pass. Same with class name.
 
void callback (const ir::OperationIndex &index, ir::IOperation &node) final
 Be called for all nodes of graph.
 
 LoweredOperationPass (ILoweredGraph &lowered_graph)
 
- Public Member Functions inherited from onert::compiler::pass::LoweredOperationPass
 LoweredOperationPass (ILoweredGraph &lowered_graph)
 
virtual ~LoweredOperationPass ()=default
 
- Public Member Functions inherited from onert::compiler::pass::OperationPass
virtual ~OperationPass ()=default
 
void run () final
 Run the pass.
 
 Pass (ir::Graph &graph)
 
- Public Member Functions inherited from onert::compiler::pass::Pass
 Pass (ir::Graph &graph)
 
virtual ~Pass ()=default
 
- Public Member Functions inherited from onert::compiler::pass::IPass
virtual ~IPass ()=default
 

Additional Inherited Members

- Protected Attributes inherited from onert::compiler::pass::LoweredOperationPass
ILoweredGraph_lowered_graph
 
- Protected Attributes inherited from onert::compiler::pass::Pass
ir::Graph_graph
 

Detailed Description

Definition at line 29 of file ConstantInsertionPass.h.

Member Function Documentation

◆ callback()

void onert::compiler::pass::ConstantInsertionPass::callback ( const ir::OperationIndex index,
ir::IOperation node 
)
finalvirtual

Be called for all nodes of graph.

Parameters
indexis the index of a node in graph
nodeis the node in graph

Implements onert::compiler::pass::LoweredOperationPass.

Definition at line 25 of file ConstantInsertionPass.cc.

26{
27 const auto backend = _lowered_graph.lower_info().operation.at(node_index);
28
29 for (const auto &input : node.getInputs() | ir::Remove::DUPLICATED | ir::Remove::UNDEFINED)
30 {
31 auto &object = _graph.operands().at(input);
32
33 // Skip if the operand is not constant or not shared constant
34 if (!object.isConstant() || object.getUses().size() < 2)
35 continue;
36
37 // 1st use of shared constant operand. Keep using original operand without insertion of new one
38 // Register original operand into keep_operand map for later reuse on same backend
39 if (_keep_operands_map.find(input) == _keep_operands_map.end())
40 {
41 _keep_operands_map.emplace(input, backend);
42 continue;
43 }
44
45 // Same PermuteFactor with original operand usage. Keep using original operand
46 if (_keep_operands_map.at(input) == backend)
47 continue;
48
49 // Different backend with original operand
50
51 // Check operand is already created for current input's PermuteFactor
52 // If not, create new operand and register to _replace_operands_map
53 if (_replace_operands_map.count(backend) == 0)
54 {
55 ir::Operand new_object(object);
56 new_object.clearDefUse();
57 const auto new_index = _graph.operands().emplace(new_object);
58 _replace_operands_map[backend] = new_index;
59 }
60
61 const auto replaced_input = _replace_operands_map[backend];
62
63 // Update the same inputs of a node at once because inputs of an operation have the same
64 // backend
65 node.replaceInputs(input, replaced_input);
66
67 // Update operand
68 auto &replaced_object = _graph.operands().at(replaced_input);
69 replaced_object.insertUse(node_index);
70
71 VERBOSE(ConstInsertPass) << "New operand " << replaced_input << " added(copy of " << input
72 << ") for " << backend->config()->id() << std::endl;
73 // Remove this node from uses of origin operand
74 // Constant operand has no def.
75 assert(!object.getDef().valid());
76 object.removeUse(node_index);
77
78 // Remain uses by _keep_operands_map
79 assert(object.getUses().size() != 0);
80 }
81
82 // Now this runtime does not support the node making output as constant
83 for ([[maybe_unused]] const auto &output :
84 node.getOutputs() | ir::Remove::DUPLICATED | ir::Remove::UNDEFINED)
85 {
86 assert(!_graph.operands().at(output).isConstant());
87 }
88}
const Operands & operands() const override
Definition Graph.h:110
Index emplace(Args &&...args)
Create an object with args and put it in the container with a newly assigned Index.
const Object & at(const Index &index) const
Get the object that is associated with the given index.
#define VERBOSE(name, lv)
Definition Log.h:71
bool valid(const Fildes &)
Definition Fildes.cpp:98
int32_t size[5]
Definition Slice.cpp:35
std::unordered_map< ir::OperationIndex, const backend::Backend * > operation
virtual const compiler::GraphLowerInfo & lower_info() const =0

References onert::compiler::pass::Pass::_graph, onert::compiler::pass::LoweredOperationPass::_lowered_graph, onert::util::ObjectManager< Index, Object >::at(), onert::ir::Operand::clearDefUse(), onert::ir::DUPLICATED, onert::util::ObjectManager< Index, Object >::emplace(), onert::ir::IOperation::getInputs(), onert::ir::IOperation::getOutputs(), onert::compiler::ILoweredGraph::lower_info(), onert::ir::Graph::operands(), onert::compiler::GraphLowerInfo::operation, onert::ir::IOperation::replaceInputs(), size, onert::ir::UNDEFINED, and VERBOSE.

◆ id()

std::string onert::compiler::pass::ConstantInsertionPass::id ( )
inlinefinalvirtual

Returns string id for this pass. Same with class name.

Returns
string id

Implements onert::compiler::pass::LoweredOperationPass.

Definition at line 35 of file ConstantInsertionPass.h.

35{ return "ConstantInsertionPass"; }

◆ LoweredOperationPass()

onert::compiler::pass::LoweredOperationPass::LoweredOperationPass ( ILoweredGraph lowered_graph)
inline

Definition at line 29 of file LoweredOperationPass.h.

30 : OperationPass{lowered_graph.graph()}, _lowered_graph{lowered_graph}
31 {
32 // DO NOTHING
33 }

The documentation for this class was generated from the following files: