slyubomirsky commented on code in PR #15689:
URL: https://github.com/apache/tvm/pull/15689#discussion_r1393378085


##########
src/relax/analysis/liveness.cc:
##########
@@ -0,0 +1,122 @@
+/*
+ * 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/relax/analysis/liveness.cc
+ * \brief Implementation of liveness analysis
+ */
+#include <tvm/relax/analysis.h>
+#include <tvm/relax/dataflow_analysis.h>
+#include <tvm/relax/expr.h>
+#include <tvm/relax/expr_functor.h>
+#include <tvm/runtime/object.h>
+
+namespace tvm {
+namespace relax {
+
+// just sets of vars. the bool value is unnecessary
+using Domain = Map<Var, Bool>;
+
+Domain transfer_func(const GraphBinding& binding, const ObjectRef& input) {
+  Domain in_domain = Downcast<Domain>(input);
+  Domain new_domain(in_domain);
+
+  // 1. If a var that appears in the RHS of the binding, add it (it's live)
+  // 2. Remove the bound var (it is not live prior to being bound)
+  Array<Var> vars_used;
+  Optional<Var> var_bound;
+  if (binding->kind == BindingNodeKind::kSeqBody) {
+    vars_used = AllVars(binding->seq->body);
+  } else if (binding->kind == BindingNodeKind::kIfCond) {
+    Binding b = 
binding->seq->blocks[binding->block_idx]->bindings[binding->binding_idx];
+    Expr cond = Downcast<If>(GetBoundValue(b))->cond;
+    vars_used = AllVars(cond);
+  } else if (binding->kind == BindingNodeKind::kIfMerge) {
+    // no vars are used in the merge
+    vars_used = {};
+    // define the merge var
+    var_bound = 
binding->seq->blocks[binding->block_idx]->bindings[binding->binding_idx]->var;
+  } else {
+    // the ordinary binding case
+    Binding b = 
binding->seq->blocks[binding->block_idx]->bindings[binding->binding_idx];
+    Expr bound_value = GetBoundValue(b);
+    // special case: if the RHS is a function literal, we only care about the 
free vars
+    // (those captured by the closure)
+    if (bound_value.as<FunctionNode>()) {

Review Comment:
   That's a good point, I hadn't thought of that.



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to