|
|
@ -145,6 +145,12 @@ AnfNodePtr FunctionBlock::MakeResolve(const NameSpacePtr &name_space, const Symb
|
|
|
|
void FunctionBlock::SetPhiArgument(const ParameterPtr &phi) {
|
|
|
|
void FunctionBlock::SetPhiArgument(const ParameterPtr &phi) {
|
|
|
|
std::string var = phi_nodes_[phi];
|
|
|
|
std::string var = phi_nodes_[phi];
|
|
|
|
MS_LOG(DEBUG) << "graph " << func_graph_->ToString() << " set phi " << phi->ToString() << " for var " << var;
|
|
|
|
MS_LOG(DEBUG) << "graph " << func_graph_->ToString() << " set phi " << phi->ToString() << " for var " << var;
|
|
|
|
|
|
|
|
auto removable = CollectRemovablePhi(phi);
|
|
|
|
|
|
|
|
// If the phi node is not necessary, not need to add to jumps_ of the prev blocks.
|
|
|
|
|
|
|
|
if (removable) {
|
|
|
|
|
|
|
|
MS_LOG(DEBUG) << "remove the phi when call graph " << func_graph_->ToString() << " var " << var;
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
for (auto &pred : prev_blocks_) {
|
|
|
|
for (auto &pred : prev_blocks_) {
|
|
|
|
MS_EXCEPTION_IF_NULL(pred);
|
|
|
|
MS_EXCEPTION_IF_NULL(pred);
|
|
|
|
MS_LOG(DEBUG) << "graph " << func_graph_->ToString() << " pred_blocks_ " << pred->func_graph_->ToString();
|
|
|
|
MS_LOG(DEBUG) << "graph " << func_graph_->ToString() << " pred_blocks_ " << pred->func_graph_->ToString();
|
|
|
@ -152,16 +158,6 @@ void FunctionBlock::SetPhiArgument(const ParameterPtr &phi) {
|
|
|
|
CNodePtr jump = pred->jumps_[this];
|
|
|
|
CNodePtr jump = pred->jumps_[this];
|
|
|
|
jump->add_input(arg_node);
|
|
|
|
jump->add_input(arg_node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If the phi node in the body part of a for/while loop is being removed,
|
|
|
|
|
|
|
|
// then the closure convert phase will generate a cycle in graph if the
|
|
|
|
|
|
|
|
// loop is kept after specialization. This should be investigate further.
|
|
|
|
|
|
|
|
// Just now user has to set a flag on a function to indicate the for loop
|
|
|
|
|
|
|
|
// will definitely can be unroll as the sequence in for statement is fixed
|
|
|
|
|
|
|
|
// size in compile time.
|
|
|
|
|
|
|
|
if (parser_.func_graph()->has_flag(GRAPH_FLAG_LOOP_CAN_UNROLL) ||
|
|
|
|
|
|
|
|
parser_.func_graph()->has_flag(GRAPH_FLAG_HAS_EFFECT)) {
|
|
|
|
|
|
|
|
CollectRemovablePhi(phi);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AnfNodePtr FunctionBlock::SearchReplaceNode(const std::string &var, const ParameterPtr &phi) {
|
|
|
|
AnfNodePtr FunctionBlock::SearchReplaceNode(const std::string &var, const ParameterPtr &phi) {
|
|
|
@ -207,13 +203,13 @@ AnfNodePtr FunctionBlock::SearchReplaceNode(const std::string &var, const Parame
|
|
|
|
// 2. it's costly to iterate the graph to replace the phi for each phi.
|
|
|
|
// 2. it's costly to iterate the graph to replace the phi for each phi.
|
|
|
|
// Args :
|
|
|
|
// Args :
|
|
|
|
// phi : This parameter node is functioning as a phi node.
|
|
|
|
// phi : This parameter node is functioning as a phi node.
|
|
|
|
void FunctionBlock::CollectRemovablePhi(const ParameterPtr &phi) {
|
|
|
|
bool FunctionBlock::CollectRemovablePhi(const ParameterPtr &phi) {
|
|
|
|
MS_EXCEPTION_IF_NULL(phi);
|
|
|
|
MS_EXCEPTION_IF_NULL(phi);
|
|
|
|
std::string var = phi_nodes_[phi];
|
|
|
|
std::string var = phi_nodes_[phi];
|
|
|
|
MS_LOG(DEBUG) << "check phi " << phi->ToString() << " for " << var << " in graph " << func_graph_->ToString();
|
|
|
|
MS_LOG(DEBUG) << "check phi " << phi->DebugString() << " for " << var;
|
|
|
|
if (prev_blocks_.size() == 0) {
|
|
|
|
if (prev_blocks_.size() == 0) {
|
|
|
|
MS_LOG(DEBUG) << "no phi " << phi->ToString() << " for var " << var << " in graph " << func_graph_->ToString();
|
|
|
|
MS_LOG(DEBUG) << "no phi " << phi->DebugString() << " for var " << var;
|
|
|
|
return;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
AnfNodePtr arg_node = SearchReplaceNode(var, phi);
|
|
|
|
AnfNodePtr arg_node = SearchReplaceNode(var, phi);
|
|
|
|
if (arg_node != nullptr) {
|
|
|
|
if (arg_node != nullptr) {
|
|
|
@ -235,13 +231,16 @@ void FunctionBlock::CollectRemovablePhi(const ParameterPtr &phi) {
|
|
|
|
const auto ¶m = phi_iter.second->cast<ParameterPtr>();
|
|
|
|
const auto ¶m = phi_iter.second->cast<ParameterPtr>();
|
|
|
|
if (param == phi) {
|
|
|
|
if (param == phi) {
|
|
|
|
MS_LOG(DEBUG) << "graph " << prev->func_graph_->ToString() << " var " << phi_iter.first->DebugString()
|
|
|
|
MS_LOG(DEBUG) << "graph " << prev->func_graph_->ToString() << " var " << phi_iter.first->DebugString()
|
|
|
|
<< " can be replaced from " << param->DebugString() << " with " << arg_node->DebugString();
|
|
|
|
<< " can be replaced from " << param->DebugString() << " with " << arg_node->DebugString()
|
|
|
|
|
|
|
|
<< " in graph " << arg_node->func_graph()->ToString();
|
|
|
|
prev->removable_phis_[phi_iter.first] = arg_node;
|
|
|
|
prev->removable_phis_[phi_iter.first] = arg_node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// A block should be marked matured if its predecessor blocks have been processed
|
|
|
|
// A block should be marked matured if its predecessor blocks have been processed
|
|
|
|