torch/csrc/jit/passes/common_subexpression_elimination.cpp
// The function implements common subexpression elimination.
// Since the nodes are visited in topological order, one pass is enough.
The function implements common subexpression elimination.
Since the nodes are visited in topological order, one pass is enough.
The function implements common subexpression elimination . Since the nodes are visited in topological order one pass is enough .
void EliminateCommonSubexpression(Block * block) {
  std::unordered_set<Node*, HashNodeCSE, EqualNodeCSE> subexprs;
  for (auto it = block->nodes().begin(); it != block->nodes().end(); ++ it) {
    auto node = *it;
    if (node->kind() == prim::PythonOp
        || node->kind() == prim::Print
        || node->blocks().size() > 0
       ) {
      continue;
    }
    auto subit = subexprs.insert(node);
    if (!subit.second) {
      auto existing = *subit.first;
      node->replaceAllUsesWith(existing);
      it.destroyCurrent();
    }
  }
}
void EliminateCommonSubexpression ( Block * block ) { std :: unordered_set < Node * , HashNodeCSE , EqualNodeCSE > subexprs ; for ( auto it = block -> nodes ( ) . begin ( ) ; it != block -> nodes ( ) . end ( ) ; ++ it ) { auto node = * it ; if ( node -> kind ( ) == prim :: PythonOp || node -> kind ( ) == prim :: Print || node -> blocks ( ) . size ( ) > 0 ) { continue ; } auto subit = subexprs . insert ( node ) ; if ( ! subit . second ) { auto existing = * subit . first ; node -> replaceAllUsesWith ( existing ) ; it . destroyCurrent ( ) ; } } }
The function implements common subexpression elimination.
The function implements common subexpression elimination .
// Do NOT have enough information to do CSE on these nodes. // Check whether the same subexpression already exists. // Subexpression exists, replace the uses of node, and destroy it. // Destroy the node.