vinx13 commented on a change in pull request #8585:
URL: https://github.com/apache/tvm/pull/8585#discussion_r679439625



##########
File path: src/tir/transforms/lower_match_buffer.cc
##########
@@ -0,0 +1,254 @@
+/*
+ * 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 lower_match_buffer.cc
+ */
+
+#include <tvm/arith/analyzer.h>
+#include <tvm/tir/function.h>
+#include <tvm/tir/op.h>
+#include <tvm/tir/stmt_functor.h>
+#include <tvm/tir/transform.h>
+
+#include "../ir/functor_common.h"
+
+namespace tvm {
+namespace tir {
+class MatchBufferLower : public StmtExprMutator {
+ public:
+  explicit MatchBufferLower(const PrimFunc& func) {
+    for (const Var& param : func->params) {
+      // Mark input var as const variable.
+      if (!param.dtype().is_handle()) var_map_[param] = param;
+    }
+  }
+
+ private:
+  Stmt VisitStmt_(const BlockNode* op) final {
+    for (const MatchBufferRegion& match_buffer : op->match_buffers) {
+      CheckAndUpdateVarMap(match_buffer);
+    }
+
+    Stmt stmt = StmtExprMutator ::VisitStmt_(op);
+    op = stmt.as<BlockNode>();
+    ICHECK(op != nullptr);
+    Array<BufferRegion> reads = MutateArray(
+        op->reads, std::bind(&MatchBufferLower::VisitBufferRegion, this, 
std::placeholders::_1));
+    Array<BufferRegion> writes = MutateArray(
+        op->writes, std::bind(&MatchBufferLower::VisitBufferRegion, this, 
std::placeholders::_1));
+
+    if (reads.same_as(op->reads) && writes.same_as(op->writes) && 
op->match_buffers.empty()) {
+      return stmt;
+    } else {
+      auto n = CopyOnWrite(op);
+      n->match_buffers = {};
+      n->reads = std::move(reads);
+      n->writes = std::move(writes);
+      return Stmt(n);
+    }
+  }
+
+  Stmt VisitStmt_(const ForNode* op) final {
+    analyzer_.Bind(op->loop_var, Range::FromMinExtent(op->min, op->extent));
+    return StmtExprMutator::VisitStmt_(op);
+  }
+
+  PrimExpr VisitExpr_(const VarNode* op) final {
+    Var v = GetRef<Var>(op);
+    auto it = var_map_.find(v);
+    if (it != var_map_.end()) {
+      return it->second;
+    } else {
+      return std::move(v);
+    }
+  }
+
+  Stmt VisitStmt_(const BufferStoreNode* op) final {
+    Stmt stmt = StmtExprMutator::VisitStmt_(op);
+    op = stmt.as<BufferStoreNode>();
+    ICHECK(op != nullptr);
+
+    auto it = match_buffers_.find(op->buffer);
+    if (it == match_buffers_.end()) {
+      return stmt;
+    } else {
+      const Buffer& buffer = it->first;
+      const BufferRegion& source = it->second;
+
+      auto n = CopyOnWrite(op);
+      n->indices = MatchBufferRegion(buffer, 
source).ConvertIndices(op->indices);
+      n->buffer = source->buffer;
+      return Stmt(n);
+    }
+  }
+
+  PrimExpr VisitExpr_(const BufferLoadNode* op) final {
+    PrimExpr expr = StmtExprMutator::VisitExpr_(op);
+    op = expr.as<BufferLoadNode>();
+    ICHECK(op != nullptr);
+
+    auto it = match_buffers_.find(op->buffer);
+    if (it == match_buffers_.end()) {
+      return expr;
+    } else {
+      const Buffer& buffer = it->first;
+      const BufferRegion& source = it->second;
+      Array<PrimExpr> indices = MatchBufferRegion(buffer, 
source).ConvertIndices(op->indices);
+      return BufferLoad(source->buffer, indices);
+    }
+  }
+

Review comment:
       `LoadNode` and `StoreNode` are not handled here. We can add visitors to 
these nodes and prohibit matched buffer being used if we can't lowered them

##########
File path: src/tir/transforms/lower_match_buffer.cc
##########
@@ -0,0 +1,254 @@
+/*
+ * 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 lower_match_buffer.cc

Review comment:
       add a `\brief` section




-- 
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-unsubscr...@tvm.apache.org

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


Reply via email to