You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by "jwfromm (via GitHub)" <gi...@apache.org> on 2023/03/11 20:24:36 UTC

[GitHub] [tvm] jwfromm commented on a diff in pull request #14262: [Unity][Transform] Simple Dead Code Elimination

jwfromm commented on code in PR #14262:
URL: https://github.com/apache/tvm/pull/14262#discussion_r1133143457


##########
src/relax/transform/dead_code_elimination.cc:
##########
@@ -0,0 +1,104 @@
+/*
+ * 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/transform/dead_code_elimination.cc
+ * \brief Dead code elimination pass.
+ * Currently it removes:
+ *   1. Unused local VarBindings in a DataflowBlock.
+ */
+
+#include <tvm/relax/expr.h>
+#include <tvm/relax/expr_functor.h>
+#include <tvm/relax/transform.h>
+
+namespace tvm {
+namespace relax {
+
+class DeadCodeEliminator : public ExprMutator {
+ private:
+  Expr VisitExpr_(const VarNode* op) final {
+    used_vars_.insert(GetRef<Var>(op));
+    return GetRef<Expr>(op);
+  }
+
+  Expr VisitExpr_(const DataflowVarNode* op) final {
+    used_vars_.insert(GetRef<Var>(op));
+    return GetRef<Expr>(op);
+  }
+
+  void VisitBinding_(const VarBindingNode* binding) { this->VisitExpr(binding->value); }
+
+  void VisitBinding_(const MatchCastNode* binding) {
+    this->VisitExpr(binding->value);
+    this->VisitAndCheckStructInfoFieldUnchanged(binding->struct_info);
+  }
+
+  BindingBlock VisitBindingBlock_(const DataflowBlockNode* block) final {
+    // reverse scan the data flow block to find the used vars
+    used_vars_.clear();
+    std::vector<Binding> new_bindings;
+    for (auto rit = block->bindings.rbegin(); rit != block->bindings.rend(); rit++) {

Review Comment:
   I think this bit could use more explanation since it's making a lot of assumptions about the structure and ordering of visiting bindings that aren't obvious to the reader. For example, since we've emptied `used_vars`, the check below is assuming the first visited nodes won't be `DataFlowVarNode`, but what are they instead?



-- 
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: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org