anijain2305 commented on a change in pull request #4922: [Relay][pass] call 
graph for relay
URL: https://github.com/apache/incubator-tvm/pull/4922#discussion_r384180991
 
 

 ##########
 File path: src/relay/pass/call_graph.cc
 ##########
 @@ -0,0 +1,339 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tvm/relay/pass/call_graph.cc
+ * \brief Implementation of APIs to handle the call graph of a Relay module.
+ */
+
+#include "call_graph.h"
+
+#include <tvm/relay/expr_functor.h>
+#include <tvm/runtime/object.h>
+#include <algorithm>
+#include <memory>
+#include <sstream>
+#include <unordered_set>
+#include <vector>
+
+namespace tvm {
+namespace relay {
+
+CallGraph::CallGraph(IRModule module) {
+  auto n = make_object<CallGraphNode>();
+  n->module = std::move(module);
+  auto gvar_funcs = n->module->functions;
+  for (const auto& it : gvar_funcs) {
+    if (const auto* fn = it.second.as<FunctionNode>()) {
+      auto func = GetRef<Function>(fn);
+      // Add the global function to gradually build up the call graph.
+      n->AddToCallGraph(it.first, func);
+    }
+  }
+  data_ = std::move(n);
+}
+
+void CallGraphNode::AddToCallGraph(const GlobalVar& gv, const Function& func) {
+  CHECK(func.defined() && gv.defined());
+  // Add the current global function as an entry to the call grpah.
+  CallGraphEntry* cg_node = LookupGlobalVar(gv);
+
+  // Only GlobalVar nodes need to be handled in a function. It indicates that
+  // the global function of a callee is called by the function that is being
+  // processed. An edge will be added from the current global function, 
cg_node,
+  // to the node that contains the found callee GlobalVarNode.
+  //
+  // This is the major overhead for constructing a call graph because the
+  // post-order visitor will visit each AST node of the current function to
+  // figure out the dependencies between functions.
+  PostOrderVisit(func, [&](const Expr& expr) {
+    if (const GlobalVarNode* gvn = expr.as<GlobalVarNode>()) {
+      auto callee = GetRef<GlobalVar>(gvn);
+      cg_node->AddCalledGlobal(LookupGlobalVar(callee));
+    }
+  });
+}
+
+const CallGraphEntry* CallGraphNode::operator[](const GlobalVar& gv) const {
+  const_iterator cit = call_graph_.find(gv);
+  CHECK(cit != call_graph_.end())
 
 Review comment:
   Should we use CHECK_NE?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to