MindSpore解读评注(10) 对ccsrc\pipeline\jit\parse\function_block.h的部分注释

  • Post author:
  • Post category:其他



function_block 1



function_block 1


对ccsrc\pipeline\jit\parse\function_block.h的部分注释

知识浅薄,还有许多未理解之处,欢迎各位纠正、讨论。

相对路径:mindspore\ccsrc\pipeline\jit\parse\function_block.h

function_block是功能块(组件),介绍了pipeline(管道)方法中解析部分的功能块,function_block.h则先进行了函数声明,宏定义,函数原型这一部分。

h文件是C语言和【C++】语言的头文件,一般在【.h】类的头文件里面只放入函数声明,宏定义,函数原型,而具体的实现在【.cpp】文件里面。

C程序的定义文件以.c为后缀,C++程序的定义文件通常以.cpp为后缀(也有一些系统以.cc或.cxx为后缀)
#ifndef MINDSPORE_CCSRC_PIPELINE_JIT_PARSE_FUNCTION_BLOCK_H_//防止双重定义
#define MINDSPORE_CCSRC_PIPELINE_JIT_PARSE_FUNCTION_BLOCK_H_//预编译命令

//导入系统库
#include <vector>
#include <string>
#include <map>
#include <set>
#include <unordered_map>//无序图库
#include <memory>
#include <utility>定义标准模块库(STL)类型.函数和运算符

//引用自定义头文件
#include "pipeline/jit/parse/parse_base.h"
#include "utils/log_adapter.h"
#include "utils/ordered_set.h"

namespace mindspore {//创建一个名为mindspore的空间,并在其中嵌套一个名为parse解析器(作语法分析)的空间
namespace parse {

class Parser;//解析类
class NameSpace;//命名空间类
class Symbol;//符号类
class FunctionBlock;//功能块类
using FunctionBlockPtr = std::shared_ptr<FunctionBlock>;//使用功能块类这个空间

// A function block is a straight-line code sequence with no branches, every block has one one exit point
// which is return. When parsing function, loop or branch , we use function block to track the structure of
// the original source code.
/*功能块是一个没有分支的直线代码序列,每个功能块都有一个出口点
这就是回报。在解析函数、循环或分支时,我们使用函数块跟踪原始源代码。*/
class FunctionBlock : public std::enable_shared_from_this<FunctionBlock> {//从该站点启用共享
 public:
  explicit FunctionBlock(const Parser &parser);
  virtual ~FunctionBlock() {}

  FuncGraphPtr func_graph() { return func_graph_; }//获取函数图
  void WriteVariable(const std::string &var_name, const AnfNodePtr &node);//写入变量
  AnfNodePtr ReadVariable(const std::string &var_name);//读变量
  void AddPrevBlock(const FunctionBlockPtr &block);//添加块
  void SetPhiArgument(const ParameterPtr &phi);//设置φ参数
  bool CollectRemovablePhi(const ParameterPtr &phi);//判断是否可收集可移除的PHI
  // A block is matured if all its predecessors is generated
  //如果一个块的所有前辈(前面的操作)都已生成,则该块已成熟
  void Mature();
  CNodePtr ForceToBoolNode(const AnfNodePtr &cond);//强制布尔节点
  CNodePtr ForceToWhileCond(const AnfNodePtr &cond);//强制至毫秒
  void Jump(const FunctionBlockPtr &block, const AnfNodePtr &node);//跳转操作
  AnfNodePtr SearchReplaceNode(const std::string &var, const ParameterPtr &phi);//搜索替换节点
  void ConditionalJump(AnfNodePtr condNode, const FunctionBlockPtr &trueBlock, const FunctionBlockPtr &falseBlock,
                       bool unroll_loop = true);
                       //条件跳转
  // Create cnode for the assign statement like self.target = source.
  //为assign语句创建cnode,如self.target=source。
  void SetStateAssign(const AnfNodePtr &target, const AnfNodePtr &source);//设置状态分配
  void AddGlobalVar(const std::string &var_name) { (void)global_vars_.insert(var_name); }//添加全局变量
  bool IsGlobalVar(const std::string &var_name) { return global_vars_.find(var_name) != global_vars_.end(); }//判断是否是全局变量
  AnfNodePtr MakeResolveAstOp(const py::object &op);
  AnfNodePtr MakeResolveClassMember(const std::string &attr);//使解析类成为成员
  AnfNodePtr MakeResolveSymbol(const std::string &value);//生成解析符号
  AnfNodePtr MakeResolveOperation(const std::string &value);//进行解析操作
  AnfNodePtr MakeResolve(const std::shared_ptr<NameSpace> &name_space, const std::shared_ptr<Symbol> &resolve_symbol);
  const std::unordered_map<ParameterPtr, AnfNodePtr> &removable_phis() const { return removable_phis_; }
  void FindIsolatedNodes();//查找被分离的节点
  void AddIsolatedNode(const AnfNodePtr &target);//添加隔离节点
  void AttachIsolatedNodesBeforeReturn();//在返回之前附加隔离节点

 private:
  // Block graph
  //块图
  FuncGraphPtr func_graph_;

  // Block parser
  //块分析器
  const Parser &parser_;

  // A block is matured if all its prev_blocks is processed
  // 如果一个区块的所有上一个区块都被处理,则该区块将到期
  bool matured_;

  // Store the nest-level block.
  // Refer to comments in Parser::func_block_list_;
  //存储嵌套级别的块。
  //请参阅Parser::func\u block\u list\ux中的注释;
  std::vector<FunctionBlock *> prev_blocks_;

  // Store args and variable's node, use a bool flag to indicate if the variable is used.
  存储参数和变量的节点,使用bool标志指示是否使用了变量。
  std::map<std::string, std::pair<AnfNodePtr, bool>> vars_;

  // Map the parameter node to variable, it can be resolved if the block's predecessors are processed
  // 将参数节点映射到变量,如果处理块的前置项,则可以解析该变量
  std::map<ParameterPtr, std::string> phi_nodes_;

  // Jumps map the successor block and the function call that perform jump
  // Refer to comments in Parser::func_block_list_ that how to break the cyclic reference
  //跳转映射后续块和执行跳转的函数调用
  //请参阅Parser::func_block_list_中的注释,了解如何中断循环引用
  std::map<FunctionBlock *, CNodePtr> jumps_;

  // Keep all removable phis which will be removed in one pass.
  //保留所有将一次性删除的可移除的PHI。
  std::unordered_map<ParameterPtr, AnfNodePtr> removable_phis_;

  // Keep the map for the resolve node to the removable phi node.
  // For the case that ReadVariable returns a phi node although this phi node
  // generated in the prev block is identified as removable. The other blocks
  // should find this phi node.
  /*保留解析节点到可移动phi节点的映射。对于Read变量返回phi节点的情况,尽管此phi节点
  在prev块中生成的被标识为可移动。其他块区应该找到这个phi节点。*/
  std::unordered_map<AnfNodePtr, ParameterPtr> resolve_to_removable_phis_;

  // Hold declared global variables in function
  //在函数中保留声明的全局变量
  std::set<std::string> global_vars_;

  // Keep new made resolve symbol for the variable not found in vars_.
  //为变量保留新生成的解析符号,但在变量中找不到。
  std::unordered_map<std::string, AnfNodePtr> var_to_resolve_;

  // Isolated nodes.
  // 孤立节点。
  OrderedSet<AnfNodePtr> isolated_nodes_;//包含孤立节点的有序集
};

}  // namespace parse
}  // namespace mindspore

#endif  // MINDSPORE_CCSRC_PIPELINE_JIT_PARSE_FUNCTION_BLOCK_H_

以上即为本篇的所有内容,因学识与能力有限,如有不足之处,请多多包涵与指教。



版权声明:本文为qq_53904578原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。