junrushao1994 commented on a change in pull request #8467:
URL: https://github.com/apache/tvm/pull/8467#discussion_r670049759



##########
File path: src/tir/schedule/primitive/fuse_split.cc
##########
@@ -0,0 +1,483 @@
+/*
+ * 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.
+ */
+#include "../utils.h"
+namespace tvm {
+namespace tir {
+
+/*! \brief Append a new predicate to the each children of type BlockRealize 
(not recursively) */
+class PredicateUpdater : public StmtMutator {
+ public:
+  /*!
+   * \brief Constructor
+   * \param predicate The predicate to be apppend to BlockRealizeNode
+   */
+  explicit PredicateUpdater(const PrimExpr& predicate, arith::Analyzer* ana)
+      : predicate_(predicate) {
+    if (!ana->CanProve(predicate)) {
+      add_predicate_ = true;
+    }
+  }
+
+ private:
+  // For each direct child of type BlockRealizeNode, append the predicate
+  Stmt VisitStmt_(const BlockRealizeNode* realize) final {
+    // We do not recursively do this
+    if (add_predicate_) {
+      ObjectPtr<BlockRealizeNode> n = CopyOnWrite(realize);
+      n->predicate = n->predicate && predicate_;
+      return BlockRealize(n);
+    } else {
+      return GetRef<BlockRealize>(realize);
+    }
+  }
+
+  /*! \brief The predicate to be added */
+  const PrimExpr& predicate_;
+  /*! \brief whether to add predicate */
+  bool add_predicate_;
+};
+/*! \brief Substitute vars and collect the reuse mapping of opaque blocks */
+class IRSubstituteAndCollectOpaqueBlock : public StmtExprMutator {
+ public:
+  explicit 
IRSubstituteAndCollectOpaqueBlock(std::function<Optional<PrimExpr>(const Var&)> 
vmap,
+                                             Map<Block, Block>* opaque_blocks)
+      : vmap_(vmap), opaque_blocks_(opaque_blocks) {}
+
+ private:
+  PrimExpr VisitExpr_(const VarNode* op) final {
+    Var var = GetRef<Var>(op);
+    Optional<PrimExpr> ret = vmap_(var);
+    if (ret.defined()) {
+      return ret.value();
+    } else {
+      return std::move(var);
+    }
+  }
+
+  Stmt VisitStmt_(const BlockRealizeNode* op) final {
+    Stmt res = StmtMutator::VisitStmt_(op);
+    if (op->block->iter_vars.empty()) {
+      const BlockRealizeNode* realize = res.as<BlockRealizeNode>();
+      opaque_blocks_->Set(op->block, realize->block);
+    }
+    return res;
+  }
+
+  /*! \brief The substitute function */
+  std::function<Optional<PrimExpr>(const Var&)> vmap_;
+  /*! \brief The reuse mapping */
+  Map<Block, Block>* opaque_blocks_;
+};
+
+Stmt SubstituteAndCollectOpaqueBlock(Stmt stmt, Map<Block, Block>* 
opaque_blocks,
+                                     std::function<Optional<PrimExpr>(const 
Var&)> vmap) {
+  return IRSubstituteAndCollectOpaqueBlock(vmap, 
opaque_blocks)(std::move(stmt));
+}

Review comment:
       This function seems redundant. We can do either of the following:
   
   1) remove this function. just call 
`IRSubstituteAndCollectOpaqueBlock(...)(...)` instead
   2) make it the only public member of the visitor class

##########
File path: src/tir/schedule/primitive/fuse_split.cc
##########
@@ -0,0 +1,483 @@
+/*
+ * 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.
+ */
+#include "../utils.h"
+namespace tvm {
+namespace tir {
+
+/*! \brief Append a new predicate to the each children of type BlockRealize 
(not recursively) */
+class PredicateUpdater : public StmtMutator {
+ public:
+  /*!
+   * \brief Constructor
+   * \param predicate The predicate to be apppend to BlockRealizeNode
+   */
+  explicit PredicateUpdater(const PrimExpr& predicate, arith::Analyzer* ana)
+      : predicate_(predicate) {
+    if (!ana->CanProve(predicate)) {
+      add_predicate_ = true;
+    }
+  }
+
+ private:
+  // For each direct child of type BlockRealizeNode, append the predicate
+  Stmt VisitStmt_(const BlockRealizeNode* realize) final {
+    // We do not recursively do this
+    if (add_predicate_) {
+      ObjectPtr<BlockRealizeNode> n = CopyOnWrite(realize);
+      n->predicate = n->predicate && predicate_;
+      return BlockRealize(n);
+    } else {
+      return GetRef<BlockRealize>(realize);
+    }
+  }
+
+  /*! \brief The predicate to be added */
+  const PrimExpr& predicate_;
+  /*! \brief whether to add predicate */
+  bool add_predicate_;
+};
+/*! \brief Substitute vars and collect the reuse mapping of opaque blocks */
+class IRSubstituteAndCollectOpaqueBlock : public StmtExprMutator {

Review comment:
       We can replace `IRSubstitute` with `SubstituteVar` which is more 
accurate description of what this class is doing
   
   ```suggestion
   class SubstituteVarAndCollectOpaqueBlock : public StmtExprMutator {
   ```

##########
File path: src/tir/schedule/primitive/fuse_split.cc
##########
@@ -0,0 +1,483 @@
+/*
+ * 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.
+ */
+#include "../utils.h"
+namespace tvm {
+namespace tir {
+
+/*! \brief Append a new predicate to the each children of type BlockRealize 
(not recursively) */
+class PredicateUpdater : public StmtMutator {
+ public:
+  /*!
+   * \brief Constructor
+   * \param predicate The predicate to be apppend to BlockRealizeNode
+   */
+  explicit PredicateUpdater(const PrimExpr& predicate, arith::Analyzer* ana)
+      : predicate_(predicate) {
+    if (!ana->CanProve(predicate)) {
+      add_predicate_ = true;
+    }
+  }
+
+ private:
+  // For each direct child of type BlockRealizeNode, append the predicate
+  Stmt VisitStmt_(const BlockRealizeNode* realize) final {
+    // We do not recursively do this
+    if (add_predicate_) {
+      ObjectPtr<BlockRealizeNode> n = CopyOnWrite(realize);
+      n->predicate = n->predicate && predicate_;
+      return BlockRealize(n);
+    } else {
+      return GetRef<BlockRealize>(realize);
+    }
+  }
+
+  /*! \brief The predicate to be added */
+  const PrimExpr& predicate_;
+  /*! \brief whether to add predicate */
+  bool add_predicate_;
+};
+/*! \brief Substitute vars and collect the reuse mapping of opaque blocks */
+class IRSubstituteAndCollectOpaqueBlock : public StmtExprMutator {
+ public:
+  explicit 
IRSubstituteAndCollectOpaqueBlock(std::function<Optional<PrimExpr>(const Var&)> 
vmap,
+                                             Map<Block, Block>* opaque_blocks)
+      : vmap_(vmap), opaque_blocks_(opaque_blocks) {}
+
+ private:
+  PrimExpr VisitExpr_(const VarNode* op) final {
+    Var var = GetRef<Var>(op);
+    Optional<PrimExpr> ret = vmap_(var);
+    if (ret.defined()) {
+      return ret.value();
+    } else {
+      return std::move(var);
+    }
+  }
+
+  Stmt VisitStmt_(const BlockRealizeNode* op) final {
+    Stmt res = StmtMutator::VisitStmt_(op);
+    if (op->block->iter_vars.empty()) {
+      const BlockRealizeNode* realize = res.as<BlockRealizeNode>();
+      opaque_blocks_->Set(op->block, realize->block);
+    }
+    return res;
+  }
+
+  /*! \brief The substitute function */
+  std::function<Optional<PrimExpr>(const Var&)> vmap_;
+  /*! \brief The reuse mapping */
+  Map<Block, Block>* opaque_blocks_;
+};
+
+Stmt SubstituteAndCollectOpaqueBlock(Stmt stmt, Map<Block, Block>* 
opaque_blocks,
+                                     std::function<Optional<PrimExpr>(const 
Var&)> vmap) {
+  return IRSubstituteAndCollectOpaqueBlock(vmap, 
opaque_blocks)(std::move(stmt));
+}
+
+/*! \brief Simplify the binding of block realize and update the opaque block 
reuse mapping*/
+class BlockRealizeRewriter : public StmtExprMutator {
+ public:
+  explicit BlockRealizeRewriter(
+      const std::unordered_map<Var, Range, ObjectPtrHash, ObjectPtrEqual>& 
loop_map,
+      Map<Block, Block>* opaque_blocks)
+      : opaque_blocks_(opaque_blocks) {
+    loop_map_.insert(loop_map.begin(), loop_map.end());
+  }
+
+ private:
+  Stmt VisitStmt_(const ForNode* op) final {
+    loop_map_[op->loop_var] = Range::FromMinExtent(op->min, op->extent);
+    Stmt res = StmtMutator::VisitStmt_(op);
+    loop_map_.erase(op->loop_var);
+    return res;
+  }
+
+  Stmt VisitStmt_(const BlockRealizeNode* op) final {
+    // skip opaque block and update mapping
+    if (op->iter_values.empty()) {
+      Stmt res = StmtMutator::VisitStmt_(op);
+      const BlockRealizeNode* realize = res.as<BlockRealizeNode>();
+      for (const std::pair<Block, Block>& entry : *opaque_blocks_) {
+        if (entry.second.same_as(op->block)) {
+          opaque_blocks_->Set(entry.first, realize->block);
+          break;
+        }
+      }
+      return res;
+    }
+    auto v = arith::IterMapSimplify(op->iter_values, loop_map_, op->predicate, 
false);
+    if (v.same_as(op->iter_values)) {
+      return GetRef<Stmt>(op);
+    } else {
+      auto n = CopyOnWrite(op);
+      n->iter_values = std::move(v);
+      return Stmt(n);
+    }
+  }
+  /*! \brief The range of loops */
+  std::unordered_map<Var, Range, ObjectPtrHash, ObjectPtrEqual> loop_map_;
+  /*! \brief The reuse mapping */
+  Map<Block, Block>* opaque_blocks_;
+};
+
+Stmt SimplifyBindings(const Stmt& stmt, const Array<StmtSRef>& loops,
+                      Map<Block, Block>* opaque_blocks) {
+  std::unordered_map<Var, Range, ObjectPtrHash, ObjectPtrEqual> loop_map;

Review comment:
       add loop_map.reserve

##########
File path: src/tir/schedule/primitive/fuse_split.cc
##########
@@ -0,0 +1,483 @@
+/*
+ * 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.
+ */
+#include "../utils.h"
+namespace tvm {
+namespace tir {
+
+/*! \brief Append a new predicate to the each children of type BlockRealize 
(not recursively) */
+class PredicateUpdater : public StmtMutator {
+ public:
+  /*!
+   * \brief Constructor
+   * \param predicate The predicate to be apppend to BlockRealizeNode
+   */
+  explicit PredicateUpdater(const PrimExpr& predicate, arith::Analyzer* ana)
+      : predicate_(predicate) {
+    if (!ana->CanProve(predicate)) {
+      add_predicate_ = true;
+    }
+  }
+
+ private:
+  // For each direct child of type BlockRealizeNode, append the predicate
+  Stmt VisitStmt_(const BlockRealizeNode* realize) final {
+    // We do not recursively do this
+    if (add_predicate_) {
+      ObjectPtr<BlockRealizeNode> n = CopyOnWrite(realize);
+      n->predicate = n->predicate && predicate_;
+      return BlockRealize(n);
+    } else {
+      return GetRef<BlockRealize>(realize);
+    }
+  }
+
+  /*! \brief The predicate to be added */
+  const PrimExpr& predicate_;
+  /*! \brief whether to add predicate */
+  bool add_predicate_;
+};
+/*! \brief Substitute vars and collect the reuse mapping of opaque blocks */
+class IRSubstituteAndCollectOpaqueBlock : public StmtExprMutator {
+ public:
+  explicit 
IRSubstituteAndCollectOpaqueBlock(std::function<Optional<PrimExpr>(const Var&)> 
vmap,
+                                             Map<Block, Block>* opaque_blocks)
+      : vmap_(vmap), opaque_blocks_(opaque_blocks) {}
+
+ private:
+  PrimExpr VisitExpr_(const VarNode* op) final {
+    Var var = GetRef<Var>(op);
+    Optional<PrimExpr> ret = vmap_(var);
+    if (ret.defined()) {
+      return ret.value();
+    } else {
+      return std::move(var);
+    }
+  }
+
+  Stmt VisitStmt_(const BlockRealizeNode* op) final {
+    Stmt res = StmtMutator::VisitStmt_(op);
+    if (op->block->iter_vars.empty()) {
+      const BlockRealizeNode* realize = res.as<BlockRealizeNode>();
+      opaque_blocks_->Set(op->block, realize->block);
+    }
+    return res;
+  }
+
+  /*! \brief The substitute function */
+  std::function<Optional<PrimExpr>(const Var&)> vmap_;
+  /*! \brief The reuse mapping */
+  Map<Block, Block>* opaque_blocks_;
+};
+
+Stmt SubstituteAndCollectOpaqueBlock(Stmt stmt, Map<Block, Block>* 
opaque_blocks,
+                                     std::function<Optional<PrimExpr>(const 
Var&)> vmap) {
+  return IRSubstituteAndCollectOpaqueBlock(vmap, 
opaque_blocks)(std::move(stmt));
+}
+
+/*! \brief Simplify the binding of block realize and update the opaque block 
reuse mapping*/
+class BlockRealizeRewriter : public StmtExprMutator {
+ public:
+  explicit BlockRealizeRewriter(
+      const std::unordered_map<Var, Range, ObjectPtrHash, ObjectPtrEqual>& 
loop_map,
+      Map<Block, Block>* opaque_blocks)
+      : opaque_blocks_(opaque_blocks) {
+    loop_map_.insert(loop_map.begin(), loop_map.end());
+  }
+
+ private:
+  Stmt VisitStmt_(const ForNode* op) final {
+    loop_map_[op->loop_var] = Range::FromMinExtent(op->min, op->extent);
+    Stmt res = StmtMutator::VisitStmt_(op);
+    loop_map_.erase(op->loop_var);
+    return res;
+  }
+
+  Stmt VisitStmt_(const BlockRealizeNode* op) final {
+    // skip opaque block and update mapping
+    if (op->iter_values.empty()) {
+      Stmt res = StmtMutator::VisitStmt_(op);
+      const BlockRealizeNode* realize = res.as<BlockRealizeNode>();
+      for (const std::pair<Block, Block>& entry : *opaque_blocks_) {
+        if (entry.second.same_as(op->block)) {
+          opaque_blocks_->Set(entry.first, realize->block);
+          break;
+        }
+      }

Review comment:
       If we wanted to mutate the `Map` frequently, we may do the following to 
actually work around frequent CoW:
   
   ```C++
   Map<X, X> some_map;
   MapNode* mutable_map = some_map.CopyOnWrite();
   mutable_map->at(k) = v;
   ```
   

##########
File path: src/tir/schedule/primitive/fuse_split.cc
##########
@@ -0,0 +1,483 @@
+/*
+ * 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.
+ */
+#include "../utils.h"
+namespace tvm {
+namespace tir {
+
+/*! \brief Append a new predicate to the each children of type BlockRealize 
(not recursively) */
+class PredicateUpdater : public StmtMutator {
+ public:
+  /*!
+   * \brief Constructor
+   * \param predicate The predicate to be apppend to BlockRealizeNode
+   */
+  explicit PredicateUpdater(const PrimExpr& predicate, arith::Analyzer* ana)
+      : predicate_(predicate) {
+    if (!ana->CanProve(predicate)) {
+      add_predicate_ = true;
+    }
+  }
+
+ private:
+  // For each direct child of type BlockRealizeNode, append the predicate
+  Stmt VisitStmt_(const BlockRealizeNode* realize) final {
+    // We do not recursively do this
+    if (add_predicate_) {
+      ObjectPtr<BlockRealizeNode> n = CopyOnWrite(realize);
+      n->predicate = n->predicate && predicate_;
+      return BlockRealize(n);
+    } else {
+      return GetRef<BlockRealize>(realize);
+    }
+  }
+
+  /*! \brief The predicate to be added */
+  const PrimExpr& predicate_;
+  /*! \brief whether to add predicate */
+  bool add_predicate_;
+};
+/*! \brief Substitute vars and collect the reuse mapping of opaque blocks */
+class IRSubstituteAndCollectOpaqueBlock : public StmtExprMutator {
+ public:
+  explicit 
IRSubstituteAndCollectOpaqueBlock(std::function<Optional<PrimExpr>(const Var&)> 
vmap,
+                                             Map<Block, Block>* opaque_blocks)
+      : vmap_(vmap), opaque_blocks_(opaque_blocks) {}
+
+ private:
+  PrimExpr VisitExpr_(const VarNode* op) final {
+    Var var = GetRef<Var>(op);
+    Optional<PrimExpr> ret = vmap_(var);
+    if (ret.defined()) {
+      return ret.value();
+    } else {
+      return std::move(var);
+    }
+  }
+
+  Stmt VisitStmt_(const BlockRealizeNode* op) final {
+    Stmt res = StmtMutator::VisitStmt_(op);
+    if (op->block->iter_vars.empty()) {
+      const BlockRealizeNode* realize = res.as<BlockRealizeNode>();
+      opaque_blocks_->Set(op->block, realize->block);
+    }
+    return res;
+  }
+
+  /*! \brief The substitute function */
+  std::function<Optional<PrimExpr>(const Var&)> vmap_;
+  /*! \brief The reuse mapping */
+  Map<Block, Block>* opaque_blocks_;
+};
+
+Stmt SubstituteAndCollectOpaqueBlock(Stmt stmt, Map<Block, Block>* 
opaque_blocks,
+                                     std::function<Optional<PrimExpr>(const 
Var&)> vmap) {
+  return IRSubstituteAndCollectOpaqueBlock(vmap, 
opaque_blocks)(std::move(stmt));
+}
+
+/*! \brief Simplify the binding of block realize and update the opaque block 
reuse mapping*/
+class BlockRealizeRewriter : public StmtExprMutator {
+ public:
+  explicit BlockRealizeRewriter(
+      const std::unordered_map<Var, Range, ObjectPtrHash, ObjectPtrEqual>& 
loop_map,
+      Map<Block, Block>* opaque_blocks)
+      : opaque_blocks_(opaque_blocks) {
+    loop_map_.insert(loop_map.begin(), loop_map.end());
+  }
+
+ private:
+  Stmt VisitStmt_(const ForNode* op) final {
+    loop_map_[op->loop_var] = Range::FromMinExtent(op->min, op->extent);
+    Stmt res = StmtMutator::VisitStmt_(op);
+    loop_map_.erase(op->loop_var);
+    return res;
+  }
+
+  Stmt VisitStmt_(const BlockRealizeNode* op) final {
+    // skip opaque block and update mapping
+    if (op->iter_values.empty()) {
+      Stmt res = StmtMutator::VisitStmt_(op);
+      const BlockRealizeNode* realize = res.as<BlockRealizeNode>();
+      for (const std::pair<Block, Block>& entry : *opaque_blocks_) {
+        if (entry.second.same_as(op->block)) {
+          opaque_blocks_->Set(entry.first, realize->block);
+          break;
+        }
+      }
+      return res;
+    }
+    auto v = arith::IterMapSimplify(op->iter_values, loop_map_, op->predicate, 
false);

Review comment:
       ```suggestion
       Array<PrimExpr> v = arith::IterMapSimplify(/*indices=*/op->iter_values, 
/*input_iters=*/loop_map_, /*input_pred=*/op->predicate, 
/*require_bijective=*/false);
   ```

##########
File path: src/tir/schedule/primitive/fuse_split.cc
##########
@@ -0,0 +1,483 @@
+/*
+ * 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.
+ */
+#include "../utils.h"
+namespace tvm {
+namespace tir {
+
+/*! \brief Append a new predicate to the each children of type BlockRealize 
(not recursively) */
+class PredicateUpdater : public StmtMutator {
+ public:
+  /*!
+   * \brief Constructor
+   * \param predicate The predicate to be apppend to BlockRealizeNode
+   */
+  explicit PredicateUpdater(const PrimExpr& predicate, arith::Analyzer* ana)
+      : predicate_(predicate) {
+    if (!ana->CanProve(predicate)) {
+      add_predicate_ = true;
+    }
+  }
+
+ private:
+  // For each direct child of type BlockRealizeNode, append the predicate
+  Stmt VisitStmt_(const BlockRealizeNode* realize) final {
+    // We do not recursively do this
+    if (add_predicate_) {
+      ObjectPtr<BlockRealizeNode> n = CopyOnWrite(realize);
+      n->predicate = n->predicate && predicate_;
+      return BlockRealize(n);
+    } else {
+      return GetRef<BlockRealize>(realize);
+    }
+  }
+
+  /*! \brief The predicate to be added */
+  const PrimExpr& predicate_;
+  /*! \brief whether to add predicate */
+  bool add_predicate_;
+};
+/*! \brief Substitute vars and collect the reuse mapping of opaque blocks */
+class IRSubstituteAndCollectOpaqueBlock : public StmtExprMutator {
+ public:
+  explicit 
IRSubstituteAndCollectOpaqueBlock(std::function<Optional<PrimExpr>(const Var&)> 
vmap,
+                                             Map<Block, Block>* opaque_blocks)
+      : vmap_(vmap), opaque_blocks_(opaque_blocks) {}
+
+ private:
+  PrimExpr VisitExpr_(const VarNode* op) final {
+    Var var = GetRef<Var>(op);
+    Optional<PrimExpr> ret = vmap_(var);
+    if (ret.defined()) {
+      return ret.value();
+    } else {
+      return std::move(var);
+    }

Review comment:
       nit: we can always move Optional into if
   
   ```suggestion
       if (Optional<PrimExpr> ret = vmap_(var)) {
         return ret.value();
       } else {
         return std::move(var);
       }
   ```

##########
File path: src/tir/schedule/primitive/fuse_split.cc
##########
@@ -0,0 +1,483 @@
+/*
+ * 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.
+ */
+#include "../utils.h"
+namespace tvm {
+namespace tir {
+
+/*! \brief Append a new predicate to the each children of type BlockRealize 
(not recursively) */
+class PredicateUpdater : public StmtMutator {
+ public:
+  /*!
+   * \brief Constructor
+   * \param predicate The predicate to be apppend to BlockRealizeNode
+   */
+  explicit PredicateUpdater(const PrimExpr& predicate, arith::Analyzer* ana)
+      : predicate_(predicate) {
+    if (!ana->CanProve(predicate)) {
+      add_predicate_ = true;
+    }
+  }
+
+ private:
+  // For each direct child of type BlockRealizeNode, append the predicate
+  Stmt VisitStmt_(const BlockRealizeNode* realize) final {
+    // We do not recursively do this
+    if (add_predicate_) {
+      ObjectPtr<BlockRealizeNode> n = CopyOnWrite(realize);
+      n->predicate = n->predicate && predicate_;
+      return BlockRealize(n);
+    } else {
+      return GetRef<BlockRealize>(realize);
+    }
+  }
+
+  /*! \brief The predicate to be added */
+  const PrimExpr& predicate_;
+  /*! \brief whether to add predicate */
+  bool add_predicate_;
+};
+/*! \brief Substitute vars and collect the reuse mapping of opaque blocks */
+class IRSubstituteAndCollectOpaqueBlock : public StmtExprMutator {
+ public:
+  explicit 
IRSubstituteAndCollectOpaqueBlock(std::function<Optional<PrimExpr>(const Var&)> 
vmap,
+                                             Map<Block, Block>* opaque_blocks)
+      : vmap_(vmap), opaque_blocks_(opaque_blocks) {}
+
+ private:
+  PrimExpr VisitExpr_(const VarNode* op) final {
+    Var var = GetRef<Var>(op);
+    Optional<PrimExpr> ret = vmap_(var);
+    if (ret.defined()) {
+      return ret.value();
+    } else {
+      return std::move(var);
+    }
+  }
+
+  Stmt VisitStmt_(const BlockRealizeNode* op) final {
+    Stmt res = StmtMutator::VisitStmt_(op);
+    if (op->block->iter_vars.empty()) {
+      const BlockRealizeNode* realize = res.as<BlockRealizeNode>();
+      opaque_blocks_->Set(op->block, realize->block);
+    }
+    return res;
+  }
+
+  /*! \brief The substitute function */
+  std::function<Optional<PrimExpr>(const Var&)> vmap_;
+  /*! \brief The reuse mapping */
+  Map<Block, Block>* opaque_blocks_;
+};
+
+Stmt SubstituteAndCollectOpaqueBlock(Stmt stmt, Map<Block, Block>* 
opaque_blocks,
+                                     std::function<Optional<PrimExpr>(const 
Var&)> vmap) {
+  return IRSubstituteAndCollectOpaqueBlock(vmap, 
opaque_blocks)(std::move(stmt));
+}
+
+/*! \brief Simplify the binding of block realize and update the opaque block 
reuse mapping*/
+class BlockRealizeRewriter : public StmtExprMutator {
+ public:
+  explicit BlockRealizeRewriter(
+      const std::unordered_map<Var, Range, ObjectPtrHash, ObjectPtrEqual>& 
loop_map,
+      Map<Block, Block>* opaque_blocks)
+      : opaque_blocks_(opaque_blocks) {
+    loop_map_.insert(loop_map.begin(), loop_map.end());
+  }
+
+ private:
+  Stmt VisitStmt_(const ForNode* op) final {
+    loop_map_[op->loop_var] = Range::FromMinExtent(op->min, op->extent);
+    Stmt res = StmtMutator::VisitStmt_(op);
+    loop_map_.erase(op->loop_var);
+    return res;
+  }
+
+  Stmt VisitStmt_(const BlockRealizeNode* op) final {
+    // skip opaque block and update mapping
+    if (op->iter_values.empty()) {
+      Stmt res = StmtMutator::VisitStmt_(op);
+      const BlockRealizeNode* realize = res.as<BlockRealizeNode>();
+      for (const std::pair<Block, Block>& entry : *opaque_blocks_) {
+        if (entry.second.same_as(op->block)) {
+          opaque_blocks_->Set(entry.first, realize->block);
+          break;
+        }
+      }
+      return res;
+    }
+    auto v = arith::IterMapSimplify(op->iter_values, loop_map_, op->predicate, 
false);
+    if (v.same_as(op->iter_values)) {
+      return GetRef<Stmt>(op);
+    } else {
+      auto n = CopyOnWrite(op);
+      n->iter_values = std::move(v);
+      return Stmt(n);
+    }
+  }
+  /*! \brief The range of loops */
+  std::unordered_map<Var, Range, ObjectPtrHash, ObjectPtrEqual> loop_map_;
+  /*! \brief The reuse mapping */
+  Map<Block, Block>* opaque_blocks_;
+};
+
+Stmt SimplifyBindings(const Stmt& stmt, const Array<StmtSRef>& loops,
+                      Map<Block, Block>* opaque_blocks) {
+  std::unordered_map<Var, Range, ObjectPtrHash, ObjectPtrEqual> loop_map;
+  for (const StmtSRef& sref : loops) {
+    const auto* loop = sref->StmtAs<ForNode>();

Review comment:
       Use the macro to enforce an ICHECK
   
   ```suggestion
       const ForNode* loop = TVM_SREF_TO_FOR(loop, sref);
   ```

##########
File path: src/tir/schedule/primitive/fuse_split.cc
##########
@@ -0,0 +1,483 @@
+/*
+ * 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.
+ */
+#include "../utils.h"
+namespace tvm {
+namespace tir {
+
+/*! \brief Append a new predicate to the each children of type BlockRealize 
(not recursively) */
+class PredicateUpdater : public StmtMutator {
+ public:
+  /*!
+   * \brief Constructor
+   * \param predicate The predicate to be apppend to BlockRealizeNode
+   */
+  explicit PredicateUpdater(const PrimExpr& predicate, arith::Analyzer* ana)
+      : predicate_(predicate) {
+    if (!ana->CanProve(predicate)) {
+      add_predicate_ = true;
+    }
+  }
+
+ private:
+  // For each direct child of type BlockRealizeNode, append the predicate
+  Stmt VisitStmt_(const BlockRealizeNode* realize) final {
+    // We do not recursively do this
+    if (add_predicate_) {
+      ObjectPtr<BlockRealizeNode> n = CopyOnWrite(realize);
+      n->predicate = n->predicate && predicate_;
+      return BlockRealize(n);
+    } else {
+      return GetRef<BlockRealize>(realize);
+    }
+  }
+
+  /*! \brief The predicate to be added */
+  const PrimExpr& predicate_;
+  /*! \brief whether to add predicate */
+  bool add_predicate_;
+};
+/*! \brief Substitute vars and collect the reuse mapping of opaque blocks */
+class IRSubstituteAndCollectOpaqueBlock : public StmtExprMutator {
+ public:
+  explicit 
IRSubstituteAndCollectOpaqueBlock(std::function<Optional<PrimExpr>(const Var&)> 
vmap,
+                                             Map<Block, Block>* opaque_blocks)
+      : vmap_(vmap), opaque_blocks_(opaque_blocks) {}
+
+ private:
+  PrimExpr VisitExpr_(const VarNode* op) final {
+    Var var = GetRef<Var>(op);
+    Optional<PrimExpr> ret = vmap_(var);
+    if (ret.defined()) {
+      return ret.value();
+    } else {
+      return std::move(var);
+    }
+  }
+
+  Stmt VisitStmt_(const BlockRealizeNode* op) final {
+    Stmt res = StmtMutator::VisitStmt_(op);
+    if (op->block->iter_vars.empty()) {
+      const BlockRealizeNode* realize = res.as<BlockRealizeNode>();
+      opaque_blocks_->Set(op->block, realize->block);
+    }
+    return res;
+  }
+
+  /*! \brief The substitute function */
+  std::function<Optional<PrimExpr>(const Var&)> vmap_;
+  /*! \brief The reuse mapping */
+  Map<Block, Block>* opaque_blocks_;
+};
+
+Stmt SubstituteAndCollectOpaqueBlock(Stmt stmt, Map<Block, Block>* 
opaque_blocks,
+                                     std::function<Optional<PrimExpr>(const 
Var&)> vmap) {
+  return IRSubstituteAndCollectOpaqueBlock(vmap, 
opaque_blocks)(std::move(stmt));
+}
+
+/*! \brief Simplify the binding of block realize and update the opaque block 
reuse mapping*/
+class BlockRealizeRewriter : public StmtExprMutator {

Review comment:
       Consider more specific name, e.g. `IterMapSimplifyBlockBinding`

##########
File path: src/tir/schedule/primitive/fuse_split.cc
##########
@@ -0,0 +1,483 @@
+/*
+ * 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.
+ */
+#include "../utils.h"
+namespace tvm {
+namespace tir {
+
+/*! \brief Append a new predicate to the each children of type BlockRealize 
(not recursively) */
+class PredicateUpdater : public StmtMutator {
+ public:
+  /*!
+   * \brief Constructor
+   * \param predicate The predicate to be apppend to BlockRealizeNode
+   */
+  explicit PredicateUpdater(const PrimExpr& predicate, arith::Analyzer* ana)
+      : predicate_(predicate) {
+    if (!ana->CanProve(predicate)) {
+      add_predicate_ = true;
+    }
+  }
+
+ private:
+  // For each direct child of type BlockRealizeNode, append the predicate
+  Stmt VisitStmt_(const BlockRealizeNode* realize) final {
+    // We do not recursively do this
+    if (add_predicate_) {
+      ObjectPtr<BlockRealizeNode> n = CopyOnWrite(realize);
+      n->predicate = n->predicate && predicate_;
+      return BlockRealize(n);
+    } else {
+      return GetRef<BlockRealize>(realize);
+    }
+  }
+
+  /*! \brief The predicate to be added */
+  const PrimExpr& predicate_;
+  /*! \brief whether to add predicate */
+  bool add_predicate_;
+};
+/*! \brief Substitute vars and collect the reuse mapping of opaque blocks */
+class IRSubstituteAndCollectOpaqueBlock : public StmtExprMutator {
+ public:
+  explicit 
IRSubstituteAndCollectOpaqueBlock(std::function<Optional<PrimExpr>(const Var&)> 
vmap,
+                                             Map<Block, Block>* opaque_blocks)
+      : vmap_(vmap), opaque_blocks_(opaque_blocks) {}
+
+ private:
+  PrimExpr VisitExpr_(const VarNode* op) final {
+    Var var = GetRef<Var>(op);
+    Optional<PrimExpr> ret = vmap_(var);
+    if (ret.defined()) {
+      return ret.value();
+    } else {
+      return std::move(var);
+    }
+  }
+
+  Stmt VisitStmt_(const BlockRealizeNode* op) final {
+    Stmt res = StmtMutator::VisitStmt_(op);
+    if (op->block->iter_vars.empty()) {
+      const BlockRealizeNode* realize = res.as<BlockRealizeNode>();
+      opaque_blocks_->Set(op->block, realize->block);
+    }
+    return res;
+  }
+
+  /*! \brief The substitute function */
+  std::function<Optional<PrimExpr>(const Var&)> vmap_;
+  /*! \brief The reuse mapping */
+  Map<Block, Block>* opaque_blocks_;
+};
+
+Stmt SubstituteAndCollectOpaqueBlock(Stmt stmt, Map<Block, Block>* 
opaque_blocks,
+                                     std::function<Optional<PrimExpr>(const 
Var&)> vmap) {
+  return IRSubstituteAndCollectOpaqueBlock(vmap, 
opaque_blocks)(std::move(stmt));
+}
+
+/*! \brief Simplify the binding of block realize and update the opaque block 
reuse mapping*/
+class BlockRealizeRewriter : public StmtExprMutator {
+ public:
+  explicit BlockRealizeRewriter(
+      const std::unordered_map<Var, Range, ObjectPtrHash, ObjectPtrEqual>& 
loop_map,
+      Map<Block, Block>* opaque_blocks)
+      : opaque_blocks_(opaque_blocks) {
+    loop_map_.insert(loop_map.begin(), loop_map.end());
+  }
+
+ private:
+  Stmt VisitStmt_(const ForNode* op) final {
+    loop_map_[op->loop_var] = Range::FromMinExtent(op->min, op->extent);
+    Stmt res = StmtMutator::VisitStmt_(op);
+    loop_map_.erase(op->loop_var);
+    return res;
+  }
+
+  Stmt VisitStmt_(const BlockRealizeNode* op) final {
+    // skip opaque block and update mapping
+    if (op->iter_values.empty()) {
+      Stmt res = StmtMutator::VisitStmt_(op);
+      const BlockRealizeNode* realize = res.as<BlockRealizeNode>();
+      for (const std::pair<Block, Block>& entry : *opaque_blocks_) {
+        if (entry.second.same_as(op->block)) {
+          opaque_blocks_->Set(entry.first, realize->block);
+          break;
+        }
+      }
+      return res;
+    }
+    auto v = arith::IterMapSimplify(op->iter_values, loop_map_, op->predicate, 
false);
+    if (v.same_as(op->iter_values)) {
+      return GetRef<Stmt>(op);
+    } else {
+      auto n = CopyOnWrite(op);
+      n->iter_values = std::move(v);
+      return Stmt(n);
+    }
+  }
+  /*! \brief The range of loops */
+  std::unordered_map<Var, Range, ObjectPtrHash, ObjectPtrEqual> loop_map_;
+  /*! \brief The reuse mapping */
+  Map<Block, Block>* opaque_blocks_;
+};
+
+Stmt SimplifyBindings(const Stmt& stmt, const Array<StmtSRef>& loops,

Review comment:
       Move this as a static method of the previous class to consolidate the 
logic

##########
File path: src/tir/schedule/primitive/fuse_split.cc
##########
@@ -0,0 +1,483 @@
+/*
+ * 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.
+ */
+#include "../utils.h"
+namespace tvm {
+namespace tir {
+
+/*! \brief Append a new predicate to the each children of type BlockRealize 
(not recursively) */
+class PredicateUpdater : public StmtMutator {
+ public:
+  /*!
+   * \brief Constructor
+   * \param predicate The predicate to be apppend to BlockRealizeNode
+   */
+  explicit PredicateUpdater(const PrimExpr& predicate, arith::Analyzer* ana)
+      : predicate_(predicate) {
+    if (!ana->CanProve(predicate)) {
+      add_predicate_ = true;
+    }
+  }
+
+ private:
+  // For each direct child of type BlockRealizeNode, append the predicate
+  Stmt VisitStmt_(const BlockRealizeNode* realize) final {
+    // We do not recursively do this
+    if (add_predicate_) {
+      ObjectPtr<BlockRealizeNode> n = CopyOnWrite(realize);
+      n->predicate = n->predicate && predicate_;
+      return BlockRealize(n);
+    } else {
+      return GetRef<BlockRealize>(realize);
+    }
+  }
+
+  /*! \brief The predicate to be added */
+  const PrimExpr& predicate_;
+  /*! \brief whether to add predicate */
+  bool add_predicate_;
+};
+/*! \brief Substitute vars and collect the reuse mapping of opaque blocks */
+class IRSubstituteAndCollectOpaqueBlock : public StmtExprMutator {
+ public:
+  explicit 
IRSubstituteAndCollectOpaqueBlock(std::function<Optional<PrimExpr>(const Var&)> 
vmap,
+                                             Map<Block, Block>* opaque_blocks)
+      : vmap_(vmap), opaque_blocks_(opaque_blocks) {}
+
+ private:
+  PrimExpr VisitExpr_(const VarNode* op) final {
+    Var var = GetRef<Var>(op);
+    Optional<PrimExpr> ret = vmap_(var);
+    if (ret.defined()) {
+      return ret.value();
+    } else {
+      return std::move(var);
+    }
+  }
+
+  Stmt VisitStmt_(const BlockRealizeNode* op) final {
+    Stmt res = StmtMutator::VisitStmt_(op);
+    if (op->block->iter_vars.empty()) {
+      const BlockRealizeNode* realize = res.as<BlockRealizeNode>();
+      opaque_blocks_->Set(op->block, realize->block);
+    }
+    return res;
+  }
+
+  /*! \brief The substitute function */
+  std::function<Optional<PrimExpr>(const Var&)> vmap_;
+  /*! \brief The reuse mapping */
+  Map<Block, Block>* opaque_blocks_;
+};
+
+Stmt SubstituteAndCollectOpaqueBlock(Stmt stmt, Map<Block, Block>* 
opaque_blocks,
+                                     std::function<Optional<PrimExpr>(const 
Var&)> vmap) {
+  return IRSubstituteAndCollectOpaqueBlock(vmap, 
opaque_blocks)(std::move(stmt));
+}
+
+/*! \brief Simplify the binding of block realize and update the opaque block 
reuse mapping*/
+class BlockRealizeRewriter : public StmtExprMutator {
+ public:
+  explicit BlockRealizeRewriter(
+      const std::unordered_map<Var, Range, ObjectPtrHash, ObjectPtrEqual>& 
loop_map,
+      Map<Block, Block>* opaque_blocks)
+      : opaque_blocks_(opaque_blocks) {
+    loop_map_.insert(loop_map.begin(), loop_map.end());
+  }
+
+ private:
+  Stmt VisitStmt_(const ForNode* op) final {
+    loop_map_[op->loop_var] = Range::FromMinExtent(op->min, op->extent);
+    Stmt res = StmtMutator::VisitStmt_(op);
+    loop_map_.erase(op->loop_var);
+    return res;
+  }
+
+  Stmt VisitStmt_(const BlockRealizeNode* op) final {
+    // skip opaque block and update mapping
+    if (op->iter_values.empty()) {
+      Stmt res = StmtMutator::VisitStmt_(op);
+      const BlockRealizeNode* realize = res.as<BlockRealizeNode>();
+      for (const std::pair<Block, Block>& entry : *opaque_blocks_) {
+        if (entry.second.same_as(op->block)) {
+          opaque_blocks_->Set(entry.first, realize->block);
+          break;
+        }
+      }
+      return res;
+    }
+    auto v = arith::IterMapSimplify(op->iter_values, loop_map_, op->predicate, 
false);
+    if (v.same_as(op->iter_values)) {
+      return GetRef<Stmt>(op);
+    } else {
+      auto n = CopyOnWrite(op);

Review comment:
       ```suggestion
         ObjectPtr<BlockRealizeNode> n = CopyOnWrite(op);
   ```




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