You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2021/09/07 06:00:12 UTC

[GitHub] [tvm] junrushao1994 opened a new pull request #8943: [TensorIR][M2a] Compute-At

junrushao1994 opened a new pull request #8943:
URL: https://github.com/apache/tvm/pull/8943


   This PR is part of the TensorIR upstreaming effort (#7527), which adds the following schedule primitives:
   * `compute-at`
   * `reverse-compute-at`
   
   Co-authored-by: Bohan Hou <32...@users.noreply.github.com>
   Co-authored-by: Ruihang Lai <la...@qq.com>
   Co-authored-by: Hongyi Jin <32...@qq.com>
   Co-authored-by: Wuwei Lin <wu...@apache.org>
   Co-authored-by: Siyuan Feng <Hz...@sjtu.edu.cn>
   


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



[GitHub] [tvm] MasterJH5574 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
MasterJH5574 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704888308



##########
File path: src/tir/schedule/primitive/compute_at.cc
##########
@@ -0,0 +1,584 @@
+/*
+ * 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 {
+
+using support::NDIntSet;
+
+/******** Error Classes ********/
+
+/*!
+ * \brief An error raised when not all required blocks are under the given loop.
+ * \tparam is_consumer Indicates if all the required blocks are consumers or producers
+ */
+template <bool is_consumer>
+class NotAllRequiredBlocksAreVisitedError : public ScheduleError {
+ public:
+  explicit NotAllRequiredBlocksAreVisitedError(IRModule mod, int num_not_visited,
+                                               const Array<StmtSRef>& required)
+      : mod_(mod), num_not_visited_(num_not_visited) {
+    required_.reserve(required.size());
+    for (const StmtSRef& block_sref : required) {
+      const BlockNode* block = TVM_SREF_TO_BLOCK(block, block_sref);
+      required_.push_back(GetRef<Block>(block));
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Not all required blocks are under the loop scope";
+  }
+
+  String DetailRenderTemplate() const final {
+    String relation = is_consumer ? "consumer(s)" : "producer(s)";
+    std::ostringstream os;
+    os << "The primitive requires all the " << relation
+       << " of the given block to be present under the target loop. However, there are "
+       << num_not_visited_ << " " << relation << " not satisfying the constraint. List of the "
+       << relation << ":";
+    for (int i = 0, n = required_.size(); i < n; ++i) {
+      os << "{" << i << "}";
+    }
+    return os.str();
+  }
+
+  IRModule mod() const final { return mod_; }
+
+  Array<ObjectRef> LocationsOfInterest() const final {
+    return {required_.begin(), required_.end()};
+  }
+
+ private:
+  IRModule mod_;
+  int num_not_visited_;
+  Array<Block> required_;
+};
+
+/*!
+ * \brief An error raised when the given block is not in the same block scope as the given loop,
+ * or the given loop is the ancestor of the given block.
+ */
+class NotInSameScopeError : public ScheduleError {
+ public:
+  static void CheckAndBindLoopDomain(const ScheduleState& self, const StmtSRef& block_sref,
+                                     const StmtSRef& loop_sref, const StmtSRef& scope_root_sref,
+                                     arith::Analyzer* analyzer) {
+    for (const StmtSRefNode* p = loop_sref.get();; p = p->parent) {
+      if (const ForNode* loop = p->StmtAs<ForNode>()) {
+        analyzer->Bind(loop->loop_var, Range::FromMinExtent(loop->min, loop->extent));
+      } else if (p != scope_root_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      } else {
+        break;
+      }
+    }
+    for (const StmtSRefNode* p = block_sref->parent; p != scope_root_sref.get(); p = p->parent) {
+      if (p == loop_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      }
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Expected the block and loop to be under the same block scope, and loop "
+           "not to be the ancestor of block";
+  }
+  String DetailRenderTemplate() const final {
+    return "ScheduleError: Expected the block {0} and loop {1} to be under the same block scope, "
+           "and loop not to be the ancestor of block";
+  }
+  IRModule mod() const final { return mod_; }
+  Array<ObjectRef> LocationsOfInterest() const final { return {block_, loop_}; }
+
+ private:
+  explicit NotInSameScopeError(IRModule mod, const StmtSRef& block_sref, const StmtSRef& loop_sref)
+      : mod_(mod),
+        block_(GetRef<Block>(block_sref->StmtAs<BlockNode>())),
+        loop_(GetRef<For>(loop_sref->StmtAs<ForNode>())) {}
+
+  IRModule mod_;
+  Block block_;
+  For loop_;
+};
+
+/******** Helper Functions/Classes ********/
+
+/*!
+ * \brief Find a point where the block can be inserted under the loop
+ * \tparam require_all_producers_visited Requires all producer blocks to be present under the loop
+ * \tparam require_all_consumers_visited Requires all consumer blocks to be present under the loop
+ * \param self The schedule state
+ * \param subtrees The subtrees under the loop, among which the insertion points are sought
+ * \param producer_srefs The producer blocks
+ * \param consumer_srefs The consumer blocks
+ * \param block2realize A cache that maps a block to its realize
+ * \return The last position the new block can be inserted onto, and the
+ * producer-consumer-relationship is still satisfied.
+ */
+template <bool require_all_producers_visited, bool require_all_consumers_visited>
+int FindInsertionPoint(
+    const ScheduleState& self, const Array<Stmt>& subtrees, const Array<StmtSRef>& producer_srefs,
+    const Array<StmtSRef>& consumer_srefs,
+    std::unordered_map<const BlockNode*, const BlockRealizeNode*>* block2realize) {
+  ProducerConsumerSplit split =
+      ProducerConsumerSplit::Find(self, subtrees, producer_srefs, consumer_srefs, block2realize);
+  // Step 1. Check if all the producers are visited in the subtrees, if required to
+  if (require_all_producers_visited) {
+    int num_producers = producer_srefs.size();
+    if (split.n_producers_visited < num_producers) {
+      throw NotAllRequiredBlocksAreVisitedError<false>(
+          self->mod, num_producers - split.n_producers_visited, producer_srefs);
+    }
+  }
+  // Step 2. Check if all the consumers are visited in the subtrees, if required to
+  if (require_all_consumers_visited) {
+    int num_consumers = consumer_srefs.size();
+    if (split.n_consumers_visited < num_consumers) {
+      throw NotAllRequiredBlocksAreVisitedError<true>(
+          self->mod, num_consumers - split.n_consumers_visited, consumer_srefs);
+    }
+  }
+  // Step 3. Check if there is at least one index of the position can be inserted into
+  // The valid indices are: (last_producer_position, first_consumer_position]
+  ICHECK(split.last_producer_position < split.first_consumer_position);
+  // Step 4. Return the last valid insertion point
+  return split.first_consumer_position;
+}
+
+/*!
+ * \brief A helper to reconstruct the block scope where the given block is moved under the given
+ * loop, and the given block's induced loop nest is regenerated to satisfy the required region.
+ */
+class ScopeReconstructor : private StmtMutator {
+ public:
+  explicit ScopeReconstructor(Block scope_root, Block block, For loop)
+      : scope_root_(scope_root), block_(block), loop_(loop) {}
+
+  using StmtMutator::operator();
+
+  /*!
+   * \brief Create the loop nest on top of the block, induced by the given block var's domain
+   * \param insert_position The position among the subtrees where the block and its induced loop
+   * nest is inserted
+   * \param iter_doms The domain of each block var
+   * \param preserve_unit_loops Whether to generate unit loops where the loop extent is 1
+   */
+  void MakeNewLoop(int insert_position, std::vector<Range> iter_doms, bool preserve_unit_loops) {
+    int n_iters = iter_doms.size();
+    Array<Var> loop_vars;
+    Array<PrimExpr> loop_extents;
+    Array<PrimExpr> iter_values;
+    loop_vars.reserve(n_iters);
+    loop_extents.reserve(n_iters);
+    iter_values.reserve(n_iters);
+    for (int i = 0; i < n_iters; ++i) {
+      const Range& iter_dom = iter_doms[i];
+      if (preserve_unit_loops || !is_one(iter_dom->extent)) {
+        Var var("ax" + std::to_string(loop_vars.size()), DataType::Int(32));
+        loop_vars.push_back(var);
+        loop_extents.push_back(iter_dom->extent);
+        iter_values.push_back(iter_dom->min + var);
+      } else {
+        iter_values.push_back(iter_dom->min);
+      }
+    }
+    this->new_block_realize_ =
+        BlockRealize(std::move(iter_values), const_true(), std::move(block_));
+    Stmt new_subtree = this->new_block_realize_;
+    for (int i = static_cast<int>(loop_vars.size()) - 1; i >= 0; --i) {
+      const Var& loop_var = loop_vars[i];
+      const PrimExpr& loop_extent = loop_extents[i];
+      new_subtree = For(/*loop_var=*/loop_var,
+                        /*min=*/Integer(0),
+                        /*extent=*/loop_extent,
+                        /*ForKind=*/ForKind::kSerial,
+                        /*body=*/std::move(new_subtree));
+    }
+    Array<Stmt> subtrees = AsArray(loop_->body);
+    subtrees.insert(subtrees.begin() + insert_position, std::move(new_subtree));
+    ObjectPtr<ForNode> new_loop = make_object<ForNode>(*loop_.get());
+    new_loop->body = SeqStmt(std::move(subtrees));
+    this->new_loop_ = For(std::move(new_loop));
+  }
+
+ private:
+  Stmt VisitStmt_(const BlockNode* block) final {
+    if (block != scope_root_.get()) {
+      return GetRef<Block>(block);
+    }
+    if (block == rm_src_stmt_.get()) {
+      block = TVM_TYPE_AS(block, rm_tgt_stmt_, BlockNode);
+    }
+    return StmtMutator::VisitStmt_(block);
+  }
+
+  Stmt VisitStmt_(const ForNode* loop) final {
+    if (loop == rm_src_stmt_.get()) {
+      loop = TVM_TYPE_AS(loop, rm_tgt_stmt_, ForNode);
+    }
+    if (loop == loop_.get()) {
+      return new_loop_;
+    }
+    return StmtMutator::VisitStmt_(loop);
+  }
+
+ public:
+  /*! \brief The root block of the block scope */
+  Block scope_root_;
+  /*! \brief The given block to be moved */
+  Block block_;
+  /*! \brief The given loop the block and its loop nest to be put under */
+  For loop_;
+  /*! \brief The new loop to replace the original loop */
+  For new_loop_{nullptr};
+  /*! \brief The new block realize to the moved block */
+  BlockRealize new_block_realize_{nullptr};
+  /*! \brief The plan to remove the given block by replacing this loop/block in the AST */
+  Stmt rm_src_stmt_{nullptr};
+  /*! \brief The plan to remove the given block by replacing to this loop/block in the AST */
+  Stmt rm_tgt_stmt_{nullptr};
+};
+
+/*!
+ * \brief Calculate a list of accessed buffer regions under a path of loops
+ * \tparam relax_storage_scope Whether to relax beyond the path according to the storage and
+ * execution scope
+ * \param binding The block binding, used to unbind the buffer regions
+ * \param buffer_regions The buffer regions to be calculated
+ * \param relax_path_low_inclusive The lowest point in the loop path, inclusive
+ * \param relax_path_high_exclusive The highest point in the loop path, exclusive
+ * \param relaxed Where the calculation result is stored
+ */
+template <bool relax_storage_scope>
+void RelaxBufferRegions(const Map<Var, PrimExpr>& binding,
+                        const Array<BufferRegion>& buffer_regions,
+                        const StmtSRef& relax_path_low_inclusive,
+                        const StmtSRef& relax_path_high_exclusive,
+                        std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* relaxed) {
+  runtime::StorageScope global_scope{runtime::StorageRank::kGlobal, ""};
+  // We cache the variable domains
+  runtime::StorageRank previous_rank = runtime::StorageRank::kGlobal;
+  Optional<Map<Var, arith::IntSet>> var_dom = NullOpt;
+  // Enumerate every buffer region
+  for (const BufferRegion& buffer_region : buffer_regions) {
+    const Buffer& buffer = buffer_region->buffer;
+    const Array<Range>& region = buffer_region->region;
+    // Skip the buffer regions we are not interested in
+    auto it = relaxed->find(buffer.get());
+    if (it == relaxed->end()) {
+      continue;
+    }
+    std::vector<NDIntSet>& relaxed_regions = it->second;
+    // Check and update the cached `var_dom`
+    runtime::StorageScope scope =
+        relax_storage_scope ? runtime::StorageScope::Create(buffer.scope()) : global_scope;
+    runtime::StorageRank rank = scope.rank;
+    if (rank != previous_rank || !var_dom.defined()) {
+      previous_rank = rank;
+      var_dom = AsIntSet(LoopDomainOfSRefTreePath(
+          /*low_inclusive=*/relax_path_low_inclusive,
+          /*high_exclusive=*/relax_path_high_exclusive,
+          /*extra_relax_scope=*/scope));
+    }
+    // Relax the region
+    Array<arith::IntSet> relaxed = arith::EvalSet(Substitute(region, binding), var_dom.value());
+    relaxed_regions.push_back({relaxed.begin(), relaxed.end()});
+  }
+}
+
+/*!
+ * \brief Calculate the iteration domain of a provided integer set to fully cover the required
+ * domain
+ * \param provided The provided integer set to cover the required domain
+ * \param required The required domain to be covered
+ * \param iter_doms The result iteration domains to be updated
+ * \param analyzer The arithmetic analyzer
+ */
+void UpdateBlockVarDomain(const arith::IntSet& provided, const arith::IntSet& required,
+                          std::unordered_map<const VarNode*, std::vector<arith::IntSet>>* iter_doms,
+                          arith::Analyzer* analyzer) {
+  PrimExpr provided_min = analyzer->Simplify(provided.min());
+  PrimExpr provided_extent = analyzer->Simplify(provided.max() - provided_min + 1);
+  PrimExpr required_min = analyzer->Simplify(required.min());
+  PrimExpr required_extent = analyzer->Simplify(required.max() - required_min + 1);
+  PrimExpr dom_min{nullptr}, dom_extent{nullptr};
+  Var dom_var{ObjectPtr<VarNode>{nullptr}};
+  arith::PVar<Var> p_v;
+  arith::PVar<PrimExpr> p_e;
+  if ((p_v * p_e).Match(provided_min) || (p_e * p_v).Match(provided_min)) {
+    PrimExpr e = p_e.Eval();
+    dom_var = p_v.Eval();
+    dom_min = floordiv(required_min, e);
+    dom_extent = analyzer->Simplify((required_extent + e - 1) / e);
+  } else if (analyzer->CanProveEqual(provided_extent, 1) && p_v.Match(provided_min)) {
+    dom_var = p_v.Eval();
+    dom_min = required_min;
+    dom_extent = required_extent;
+  } else {
+    ICHECK(false) << "ValueError: BufferRegion pattern match failed";
+  }
+  auto it = iter_doms->find(dom_var.get());
+  if (it != iter_doms->end()) {
+    std::vector<arith::IntSet>& doms = it->second;
+    doms.push_back(arith::IntSet::FromMinExtent(dom_min, dom_extent));
+  } else {
+    ICHECK(analyzer->CanProveEqual(provided_min, required_min));
+    ICHECK(analyzer->CanProveEqual(provided_extent, required_extent));
+  }
+}
+
+/*!
+ * \brief Calculate the domain of block vars to cover the required region
+ * \param iter_vars The list of block vars to cover the required region
+ * \param provided_regions The region provided by one iteration instance of the block vars
+ * \param required_regions The region required to be covered
+ * \param analyzer The arithmetic analyzer
+ * \return A list of iteration domain corresponding to the given list of block vars
+ */
+std::vector<Range> CalculateBlockVarDomain(
+    const Array<IterVar>& iter_vars,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>> provided_regions,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>> required_regions,
+    arith::Analyzer* analyzer) {
+  int n_iters = iter_vars.size();
+  // Step 1. Construct the mapping from block var to their iteration domain (initialized to empty)
+  std::unordered_map<const VarNode*, std::vector<arith::IntSet>> iter_doms;
+  iter_doms.reserve(n_iters);
+  for (const IterVar& iter_var : iter_vars) {
+    iter_doms[iter_var->var.get()] = {};
+  }
+  // Step 2. For each buffer, update the domain according to the provided and required regions
+  for (const auto& kv : provided_regions) {
+    const BufferNode* buffer = kv.first;
+    const std::vector<NDIntSet>& many_provided_regions = kv.second;
+    // Calculate `provided_region` and `required_region`
+    auto it = required_regions.find(buffer);
+    if (it == required_regions.end() || it->second.empty()) {
+      continue;
+    }
+    NDIntSet required_region = support::NDIntSetUnion(it->second);
+    NDIntSet provided_region = support::NDIntSetUnion(many_provided_regions);
+    ICHECK_EQ(provided_region.size(), buffer->shape.size());
+    ICHECK_EQ(required_region.size(), buffer->shape.size());
+    // For each dimension, update the iteration domain
+    int ndim = buffer->shape.size();
+    for (int i = 0; i < ndim; ++i) {
+      arith::IntSet provided = provided_region[i];
+      arith::IntSet required = required_region[i];
+      required = arith::Intersect(
+          {std::move(required), arith::IntSet::FromMinExtent(Integer(0), buffer->shape[i])});
+      UpdateBlockVarDomain(provided, required, &iter_doms, analyzer);
+    }
+  }
+  // Union the iter var domains, put them in the same order of block vars, and return
+  std::vector<Range> result;
+  result.reserve(n_iters);
+  for (const IterVar& iter_var : iter_vars) {
+    const std::vector<arith::IntSet>& doms = iter_doms.at(iter_var->var.get());
+    arith::IntSet dom = arith::IntSet::FromRange(iter_var->dom);
+    if (!doms.empty()) {
+      dom = arith::Intersect({std::move(dom), arith::Union(doms)});
+    }
+    PrimExpr min = analyzer->Simplify(dom.min());
+    PrimExpr extent = analyzer->Simplify(dom.max() - min + 1);
+    result.push_back(Range::FromMinExtent(min, extent));
+  }
+  return result;
+}
+
+/*!
+ * \brief Calculate the provided region of the given block by one single of its execution instance,
+ * as well as the required buffer regions relaxed to the given loop
+ * \tparam is_compute_at Indicates if the operation is compute-at or reverse-compute-at
+ * \param block The given block that provides buffer regions
+ * \param loop_sref The given loop under which the block is going to be moved to
+ * \param block2realize Maps a block to its corresponding BlockRealize
+ * \param producer_srefs The producers of the given block
+ * \param consumer_srefs The consumers of the given block
+ * \param provided_regions The calculated regions provided by the block
+ * \param required_regions The calculated regions required by its consumers (in compute-at) or
+ * producers (in reverse-compute-at)
+ */
+template <bool is_compute_at>
+void CalculateProvidedRequiredRegions(
+    const BlockNode* block, const StmtSRef& loop_sref,
+    std::unordered_map<const BlockNode*, const BlockRealizeNode*> block2realize,
+    Array<StmtSRef> producer_srefs, Array<StmtSRef> consumer_srefs,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* provided_regions,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* required_regions) {
+  // Step 1. Calculate the region provided by a single execution instance of `block`
+  const Array<BufferRegion>& provided_buffers = is_compute_at ? block->writes : block->reads;
+  provided_regions->reserve(provided_buffers.size());
+  required_regions->reserve(provided_buffers.size());
+  for (const BufferRegion& provided_buffer_region : provided_buffers) {
+    const BufferNode* buffer = provided_buffer_region->buffer.get();
+    const Array<Range>& region = provided_buffer_region->region;
+    (*provided_regions)[buffer].push_back(support::NDIntSetFromRegion(region));
+    (*required_regions)[buffer].clear();

Review comment:
       I guess so. So why not write it as this? 
   https://github.com/apache/tvm/blob/be8bddcd15f1ee8cacb3493c97425cc1ba33d660/src/tir/schedule/primitive/compute_at.cc#L364-L366
   IMO it's much clearer than `.clear()`.




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704961036



##########
File path: src/tir/schedule/primitive/cache_read_write.cc
##########
@@ -160,6 +160,21 @@ Block MakeCacheStage(const BufferRegion& cache_region, CacheStageInfo* info,
   return block;
 }
 
+/*!
+ * \brief Recalculate the `affine_binding` flag of a specifc block
+ * \param block_sref The sref to the specific block
+ */
+bool CalculateAffineFlag(const ScheduleState& self, const StmtSRef& block_sref) {

Review comment:
       I don't think so.
   
   Note that the method `CalculateAffineFlag` here does bunch of redundant computation, e.g. finding the corresponding BlockRealize given a Block, which incurs extra overhead of re-visiting the AST; On the contrary, in compute-at, it already memorizes the mapping from Blocks to BlockRealizes, so there is no reason to introduce extra overhead.
   
   In summary, I didn't find the method specifically useful when it has non-trivial overhead. Instead, `IsAffineBinding` is the one with less burden and we should encourage its usage.




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704724549



##########
File path: src/tir/schedule/primitive/compute_at.cc
##########
@@ -0,0 +1,584 @@
+/*
+ * 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 {
+
+using support::NDIntSet;
+
+/******** Error Classes ********/
+
+/*!
+ * \brief An error raised when not all required blocks are under the given loop.
+ * \tparam is_consumer Indicates if all the required blocks are consumers or producers
+ */
+template <bool is_consumer>
+class NotAllRequiredBlocksAreVisitedError : public ScheduleError {
+ public:
+  explicit NotAllRequiredBlocksAreVisitedError(IRModule mod, int num_not_visited,
+                                               const Array<StmtSRef>& required)
+      : mod_(mod), num_not_visited_(num_not_visited) {
+    required_.reserve(required.size());
+    for (const StmtSRef& block_sref : required) {
+      const BlockNode* block = TVM_SREF_TO_BLOCK(block, block_sref);
+      required_.push_back(GetRef<Block>(block));
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Not all required blocks are under the loop scope";
+  }
+
+  String DetailRenderTemplate() const final {
+    String relation = is_consumer ? "consumer(s)" : "producer(s)";
+    std::ostringstream os;
+    os << "The primitive requires all the " << relation
+       << " of the given block to be present under the target loop. However, there are "
+       << num_not_visited_ << " " << relation << " not satisfying the constraint. List of the "
+       << relation << ":";
+    for (int i = 0, n = required_.size(); i < n; ++i) {
+      os << "{" << i << "}";
+    }
+    return os.str();
+  }
+
+  IRModule mod() const final { return mod_; }
+
+  Array<ObjectRef> LocationsOfInterest() const final {
+    return {required_.begin(), required_.end()};
+  }
+
+ private:
+  IRModule mod_;
+  int num_not_visited_;
+  Array<Block> required_;
+};
+
+/*!
+ * \brief An error raised when the given block is not in the same block scope as the given loop,
+ * or the given loop is the ancestor of the given block.
+ */
+class NotInSameScopeError : public ScheduleError {
+ public:
+  static void CheckAndBindLoopDomain(const ScheduleState& self, const StmtSRef& block_sref,
+                                     const StmtSRef& loop_sref, const StmtSRef& scope_root_sref,
+                                     arith::Analyzer* analyzer) {
+    for (const StmtSRefNode* p = loop_sref.get();; p = p->parent) {
+      if (const ForNode* loop = p->StmtAs<ForNode>()) {
+        analyzer->Bind(loop->loop_var, Range::FromMinExtent(loop->min, loop->extent));
+      } else if (p != scope_root_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      } else {
+        break;
+      }
+    }
+    for (const StmtSRefNode* p = block_sref->parent; p != scope_root_sref.get(); p = p->parent) {
+      if (p == loop_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      }
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Expected the block and loop to be under the same block scope, and loop "
+           "not to be the ancestor of block";
+  }
+  String DetailRenderTemplate() const final {
+    return "ScheduleError: Expected the block {0} and loop {1} to be under the same block scope, "
+           "and loop not to be the ancestor of block";
+  }
+  IRModule mod() const final { return mod_; }
+  Array<ObjectRef> LocationsOfInterest() const final { return {block_, loop_}; }
+
+ private:
+  explicit NotInSameScopeError(IRModule mod, const StmtSRef& block_sref, const StmtSRef& loop_sref)
+      : mod_(mod),
+        block_(GetRef<Block>(block_sref->StmtAs<BlockNode>())),
+        loop_(GetRef<For>(loop_sref->StmtAs<ForNode>())) {}
+
+  IRModule mod_;
+  Block block_;
+  For loop_;
+};
+
+/******** Helper Functions/Classes ********/
+
+/*!
+ * \brief Find a point where the block can be inserted under the loop
+ * \tparam require_all_producers_visited Requires all producer blocks to be present under the loop
+ * \tparam require_all_consumers_visited Requires all consumer blocks to be present under the loop
+ * \param self The schedule state
+ * \param subtrees The subtrees under the loop, among which the insertion points are sought
+ * \param producer_srefs The producer blocks
+ * \param consumer_srefs The consumer blocks
+ * \param block2realize A cache that maps a block to its realize
+ * \return The last position the new block can be inserted onto, and the
+ * producer-consumer-relationship is still satisfied.
+ */
+template <bool require_all_producers_visited, bool require_all_consumers_visited>
+int FindInsertionPoint(
+    const ScheduleState& self, const Array<Stmt>& subtrees, const Array<StmtSRef>& producer_srefs,
+    const Array<StmtSRef>& consumer_srefs,
+    std::unordered_map<const BlockNode*, const BlockRealizeNode*>* block2realize) {
+  ProducerConsumerSplit split =
+      ProducerConsumerSplit::Find(self, subtrees, producer_srefs, consumer_srefs, block2realize);
+  // Step 1. Check if all the producers are visited in the subtrees, if required to
+  if (require_all_producers_visited) {
+    int num_producers = producer_srefs.size();
+    if (split.n_producers_visited < num_producers) {
+      throw NotAllRequiredBlocksAreVisitedError<false>(
+          self->mod, num_producers - split.n_producers_visited, producer_srefs);
+    }
+  }
+  // Step 2. Check if all the consumers are visited in the subtrees, if required to
+  if (require_all_consumers_visited) {
+    int num_consumers = consumer_srefs.size();
+    if (split.n_consumers_visited < num_consumers) {
+      throw NotAllRequiredBlocksAreVisitedError<true>(
+          self->mod, num_consumers - split.n_consumers_visited, consumer_srefs);
+    }
+  }
+  // Step 3. Check if there is at least one index of the position can be inserted into
+  // The valid indices are: (last_producer_position, first_consumer_position]
+  ICHECK(split.last_producer_position < split.first_consumer_position);
+  // Step 4. Return the last valid insertion point
+  return split.first_consumer_position;
+}
+
+/*!
+ * \brief A helper to reconstruct the block scope where the given block is moved under the given
+ * loop, and the given block's induced loop nest is regenerated to satisfy the required region.
+ */
+class ScopeReconstructor : private StmtMutator {
+ public:
+  explicit ScopeReconstructor(Block scope_root, Block block, For loop)
+      : scope_root_(scope_root), block_(block), loop_(loop) {}
+
+  using StmtMutator::operator();
+
+  /*!
+   * \brief Create the loop nest on top of the block, induced by the given block var's domain
+   * \param insert_position The position among the subtrees where the block and its induced loop
+   * nest is inserted
+   * \param iter_doms The domain of each block var
+   * \param preserve_unit_loops Whether to generate unit loops where the loop extent is 1
+   */
+  void MakeNewLoop(int insert_position, std::vector<Range> iter_doms, bool preserve_unit_loops) {
+    int n_iters = iter_doms.size();
+    Array<Var> loop_vars;
+    Array<PrimExpr> loop_extents;
+    Array<PrimExpr> iter_values;
+    loop_vars.reserve(n_iters);
+    loop_extents.reserve(n_iters);
+    iter_values.reserve(n_iters);
+    for (int i = 0; i < n_iters; ++i) {
+      const Range& iter_dom = iter_doms[i];
+      if (preserve_unit_loops || !is_one(iter_dom->extent)) {
+        Var var("ax" + std::to_string(loop_vars.size()), DataType::Int(32));
+        loop_vars.push_back(var);
+        loop_extents.push_back(iter_dom->extent);
+        iter_values.push_back(iter_dom->min + var);
+      } else {
+        iter_values.push_back(iter_dom->min);
+      }
+    }
+    this->new_block_realize_ =
+        BlockRealize(std::move(iter_values), const_true(), std::move(block_));
+    Stmt new_subtree = this->new_block_realize_;
+    for (int i = static_cast<int>(loop_vars.size()) - 1; i >= 0; --i) {
+      const Var& loop_var = loop_vars[i];
+      const PrimExpr& loop_extent = loop_extents[i];
+      new_subtree = For(/*loop_var=*/loop_var,
+                        /*min=*/Integer(0),
+                        /*extent=*/loop_extent,
+                        /*ForKind=*/ForKind::kSerial,
+                        /*body=*/std::move(new_subtree));
+    }
+    Array<Stmt> subtrees = AsArray(loop_->body);
+    subtrees.insert(subtrees.begin() + insert_position, std::move(new_subtree));
+    ObjectPtr<ForNode> new_loop = make_object<ForNode>(*loop_.get());
+    new_loop->body = SeqStmt(std::move(subtrees));
+    this->new_loop_ = For(std::move(new_loop));
+  }
+
+ private:
+  Stmt VisitStmt_(const BlockNode* block) final {
+    if (block != scope_root_.get()) {
+      return GetRef<Block>(block);
+    }
+    if (block == rm_src_stmt_.get()) {
+      block = TVM_TYPE_AS(block, rm_tgt_stmt_, BlockNode);
+    }
+    return StmtMutator::VisitStmt_(block);
+  }
+
+  Stmt VisitStmt_(const ForNode* loop) final {
+    if (loop == rm_src_stmt_.get()) {
+      loop = TVM_TYPE_AS(loop, rm_tgt_stmt_, ForNode);
+    }
+    if (loop == loop_.get()) {
+      return new_loop_;
+    }
+    return StmtMutator::VisitStmt_(loop);
+  }
+
+ public:
+  /*! \brief The root block of the block scope */
+  Block scope_root_;
+  /*! \brief The given block to be moved */
+  Block block_;
+  /*! \brief The given loop the block and its loop nest to be put under */
+  For loop_;
+  /*! \brief The new loop to replace the original loop */
+  For new_loop_{nullptr};
+  /*! \brief The new block realize to the moved block */
+  BlockRealize new_block_realize_{nullptr};
+  /*! \brief The plan to remove the given block by replacing this loop/block in the AST */
+  Stmt rm_src_stmt_{nullptr};
+  /*! \brief The plan to remove the given block by replacing to this loop/block in the AST */
+  Stmt rm_tgt_stmt_{nullptr};
+};
+
+/*!
+ * \brief Calculate a list of accessed buffer regions under a path of loops
+ * \tparam relax_storage_scope Whether to relax beyond the path according to the storage and
+ * execution scope
+ * \param binding The block binding, used to unbind the buffer regions
+ * \param buffer_regions The buffer regions to be calculated
+ * \param relax_path_low_inclusive The lowest point in the loop path, inclusive
+ * \param relax_path_high_exclusive The highest point in the loop path, exclusive
+ * \param relaxed Where the calculation result is stored
+ */
+template <bool relax_storage_scope>
+void RelaxBufferRegions(const Map<Var, PrimExpr>& binding,
+                        const Array<BufferRegion>& buffer_regions,
+                        const StmtSRef& relax_path_low_inclusive,
+                        const StmtSRef& relax_path_high_exclusive,
+                        std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* relaxed) {
+  runtime::StorageScope global_scope{runtime::StorageRank::kGlobal, ""};
+  // We cache the variable domains
+  runtime::StorageRank previous_rank = runtime::StorageRank::kGlobal;
+  Optional<Map<Var, arith::IntSet>> var_dom = NullOpt;
+  // Enumerate every buffer region
+  for (const BufferRegion& buffer_region : buffer_regions) {
+    const Buffer& buffer = buffer_region->buffer;
+    const Array<Range>& region = buffer_region->region;
+    // Skip the buffer regions we are not interested in
+    auto it = relaxed->find(buffer.get());
+    if (it == relaxed->end()) {
+      continue;
+    }
+    std::vector<NDIntSet>& relaxed_regions = it->second;
+    // Check and update the cached `var_dom`
+    runtime::StorageScope scope =
+        relax_storage_scope ? runtime::StorageScope::Create(buffer.scope()) : global_scope;
+    runtime::StorageRank rank = scope.rank;
+    if (rank != previous_rank || !var_dom.defined()) {
+      previous_rank = rank;
+      var_dom = AsIntSet(LoopDomainOfSRefTreePath(
+          /*low_inclusive=*/relax_path_low_inclusive,
+          /*high_exclusive=*/relax_path_high_exclusive,
+          /*extra_relax_scope=*/scope));
+    }
+    // Relax the region
+    Array<arith::IntSet> relaxed = arith::EvalSet(Substitute(region, binding), var_dom.value());
+    relaxed_regions.push_back({relaxed.begin(), relaxed.end()});
+  }
+}
+
+/*!
+ * \brief Calculate the iteration domain of a provided integer set to fully cover the required
+ * domain
+ * \param provided The provided integer set to cover the required domain
+ * \param required The required domain to be covered
+ * \param iter_doms The result iteration domains to be updated
+ * \param analyzer The arithmetic analyzer
+ */
+void UpdateBlockVarDomain(const arith::IntSet& provided, const arith::IntSet& required,
+                          std::unordered_map<const VarNode*, std::vector<arith::IntSet>>* iter_doms,
+                          arith::Analyzer* analyzer) {
+  PrimExpr provided_min = analyzer->Simplify(provided.min());
+  PrimExpr provided_extent = analyzer->Simplify(provided.max() - provided_min + 1);
+  PrimExpr required_min = analyzer->Simplify(required.min());
+  PrimExpr required_extent = analyzer->Simplify(required.max() - required_min + 1);
+  PrimExpr dom_min{nullptr}, dom_extent{nullptr};
+  Var dom_var{ObjectPtr<VarNode>{nullptr}};
+  arith::PVar<Var> p_v;
+  arith::PVar<PrimExpr> p_e;
+  if ((p_v * p_e).Match(provided_min) || (p_e * p_v).Match(provided_min)) {
+    PrimExpr e = p_e.Eval();
+    dom_var = p_v.Eval();
+    dom_min = floordiv(required_min, e);
+    dom_extent = analyzer->Simplify((required_extent + e - 1) / e);
+  } else if (analyzer->CanProveEqual(provided_extent, 1) && p_v.Match(provided_min)) {
+    dom_var = p_v.Eval();
+    dom_min = required_min;
+    dom_extent = required_extent;
+  } else {
+    ICHECK(false) << "ValueError: BufferRegion pattern match failed";
+  }
+  auto it = iter_doms->find(dom_var.get());
+  if (it != iter_doms->end()) {
+    std::vector<arith::IntSet>& doms = it->second;
+    doms.push_back(arith::IntSet::FromMinExtent(dom_min, dom_extent));
+  } else {
+    ICHECK(analyzer->CanProveEqual(provided_min, required_min));
+    ICHECK(analyzer->CanProveEqual(provided_extent, required_extent));
+  }
+}
+
+/*!
+ * \brief Calculate the domain of block vars to cover the required region
+ * \param iter_vars The list of block vars to cover the required region
+ * \param provided_regions The region provided by one iteration instance of the block vars
+ * \param required_regions The region required to be covered
+ * \param analyzer The arithmetic analyzer
+ * \return A list of iteration domain corresponding to the given list of block vars
+ */
+std::vector<Range> CalculateBlockVarDomain(
+    const Array<IterVar>& iter_vars,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>> provided_regions,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>> required_regions,
+    arith::Analyzer* analyzer) {
+  int n_iters = iter_vars.size();
+  // Step 1. Construct the mapping from block var to their iteration domain (initialized to empty)
+  std::unordered_map<const VarNode*, std::vector<arith::IntSet>> iter_doms;
+  iter_doms.reserve(n_iters);
+  for (const IterVar& iter_var : iter_vars) {
+    iter_doms[iter_var->var.get()] = {};
+  }
+  // Step 2. For each buffer, update the domain according to the provided and required regions
+  for (const auto& kv : provided_regions) {
+    const BufferNode* buffer = kv.first;
+    const std::vector<NDIntSet>& many_provided_regions = kv.second;
+    // Calculate `provided_region` and `required_region`
+    auto it = required_regions.find(buffer);
+    if (it == required_regions.end() || it->second.empty()) {
+      continue;
+    }
+    NDIntSet required_region = support::NDIntSetUnion(it->second);
+    NDIntSet provided_region = support::NDIntSetUnion(many_provided_regions);
+    ICHECK_EQ(provided_region.size(), buffer->shape.size());
+    ICHECK_EQ(required_region.size(), buffer->shape.size());
+    // For each dimension, update the iteration domain
+    int ndim = buffer->shape.size();
+    for (int i = 0; i < ndim; ++i) {
+      arith::IntSet provided = provided_region[i];
+      arith::IntSet required = required_region[i];
+      required = arith::Intersect(
+          {std::move(required), arith::IntSet::FromMinExtent(Integer(0), buffer->shape[i])});
+      UpdateBlockVarDomain(provided, required, &iter_doms, analyzer);
+    }
+  }
+  // Union the iter var domains, put them in the same order of block vars, and return
+  std::vector<Range> result;
+  result.reserve(n_iters);
+  for (const IterVar& iter_var : iter_vars) {
+    const std::vector<arith::IntSet>& doms = iter_doms.at(iter_var->var.get());
+    arith::IntSet dom = arith::IntSet::FromRange(iter_var->dom);
+    if (!doms.empty()) {
+      dom = arith::Intersect({std::move(dom), arith::Union(doms)});
+    }
+    PrimExpr min = analyzer->Simplify(dom.min());
+    PrimExpr extent = analyzer->Simplify(dom.max() - min + 1);
+    result.push_back(Range::FromMinExtent(min, extent));
+  }
+  return result;
+}
+
+/*!
+ * \brief Calculate the provided region of the given block by one single of its execution instance,
+ * as well as the required buffer regions relaxed to the given loop
+ * \tparam is_compute_at Indicates if the operation is compute-at or reverse-compute-at
+ * \param block The given block that provides buffer regions
+ * \param loop_sref The given loop under which the block is going to be moved to
+ * \param block2realize Maps a block to its corresponding BlockRealize
+ * \param producer_srefs The producers of the given block
+ * \param consumer_srefs The consumers of the given block
+ * \param provided_regions The calculated regions provided by the block
+ * \param required_regions The calculated regions required by its consumers (in compute-at) or
+ * producers (in reverse-compute-at)
+ */
+template <bool is_compute_at>
+void CalculateProvidedRequiredRegions(
+    const BlockNode* block, const StmtSRef& loop_sref,
+    std::unordered_map<const BlockNode*, const BlockRealizeNode*> block2realize,
+    Array<StmtSRef> producer_srefs, Array<StmtSRef> consumer_srefs,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* provided_regions,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* required_regions) {
+  // Step 1. Calculate the region provided by a single execution instance of `block`
+  const Array<BufferRegion>& provided_buffers = is_compute_at ? block->writes : block->reads;
+  provided_regions->reserve(provided_buffers.size());
+  required_regions->reserve(provided_buffers.size());
+  for (const BufferRegion& provided_buffer_region : provided_buffers) {
+    const BufferNode* buffer = provided_buffer_region->buffer.get();
+    const Array<Range>& region = provided_buffer_region->region;
+    (*provided_regions)[buffer].push_back(support::NDIntSetFromRegion(region));
+    (*required_regions)[buffer].clear();
+  }
+  // Step 2. Calculate the region required by dependent blocks under `loop`
+  for (const StmtSRef& required_block_sref : is_compute_at ? consumer_srefs : producer_srefs) {
+    const BlockNode* required_block = TVM_SREF_TO_BLOCK(required_block, required_block_sref);
+    ICHECK(block2realize.count(required_block));
+    RelaxBufferRegions</*relax_storage_scope=*/is_compute_at>(
+        /*binding=*/GetBindings(GetRef<BlockRealize>(block2realize.at(required_block))),
+        /*buffer_regions=*/is_compute_at ? required_block->reads : required_block->writes,
+        /*relax_path_low_inclusive=*/GetRef<StmtSRef>(required_block_sref->parent),
+        /*relax_path_high_exclusive=*/loop_sref, /*relaxed=*/required_regions);
+  }
+}
+
+/******** Main Implementation ********/
+
+template <bool is_compute_at>
+void ComputeAtOrReverseComputeAtImpl(ScheduleState self, const StmtSRef& block_sref,
+                                     const StmtSRef& loop_sref, bool preserve_unit_loops) {
+  const BlockNode* block = TVM_SREF_TO_BLOCK(block, block_sref);
+  const ForNode* loop = TVM_SREF_TO_FOR(loop, loop_sref);
+  // Step 1. Bunch of checks
+  // Check condition 1) and 2): stage pipeline and subtree compact dataflow
+  StmtSRef scope_root_sref = GetScopeRoot(self, block_sref,
+                                          /*require_stage_pipeline=*/true,
+                                          /*require_subtree_compact_dataflow=*/true);
+  Block scope_root = GetRef<Block>(scope_root_sref->StmtAs<BlockNode>());
+  BlockScope scope = self->GetBlockScope(scope_root_sref);
+  Array<StmtSRef> producer_srefs = GetProducers(block_sref, scope);
+  Array<StmtSRef> consumer_srefs = GetConsumers(block_sref, scope);
+  arith::Analyzer analyzer;
+  // Check condition 3): `block` and `loop` are under the same scope,
+  // and `loop` is not the ancestor of `block`
+  NotInSameScopeError::CheckAndBindLoopDomain(self, block_sref, loop_sref, scope_root_sref,
+                                              &analyzer);
+  // Check condition 4): `block` is not an output block
+  if (is_compute_at) {
+    CheckNotOutputBlock(self, block_sref, scope_root_sref);
+  }
+  // Step 2. Plan for the removal of `block`
+  ScopeReconstructor reconstructor(scope_root, GetRef<Block>(block), GetRef<For>(loop));
+  LeafBlockRemovalPlan(self, block_sref, &reconstructor.rm_src_stmt_, &reconstructor.rm_tgt_stmt_);
+  // Step 3. Find the insertion point under `loop`
+  // Check condition 5): all the required block are under the given loop
+  std::unordered_map<const BlockNode*, const BlockRealizeNode*> block2realize;
+  block2realize.reserve(self->block_info.size());
+  int insert_position = FindInsertionPoint<!is_compute_at, is_compute_at>(
+      /*self=*/self,
+      /*subtrees=*/AsArray(loop->body),
+      /*producer_srefs=*/producer_srefs,
+      /*consumer_srefs=*/consumer_srefs, /*block2realize=*/&block2realize);
+  // Step 4. Calculate the region provided by a single execution instance of `block`,
+  // as well as the region required by dependent blocks under `loop`

Review comment:
       I intentionally use the term `provide`/`require` instead of `produce`/`consume` to prevent you from such misunderstanding.
   
   Here is the definition:
   - In compute-at, `provide` means `produce` and `require` means `consume`
   - In reverse-compute-at, `provide` means `consume` and `require` means `produce`




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



[GitHub] [tvm] MasterJH5574 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
MasterJH5574 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704552677



##########
File path: src/arith/int_set.cc
##########
@@ -607,6 +607,13 @@ inline bool ProveEqual(Analyzer* analyzer, PrimExpr lhs, PrimExpr rhs) {
   return is_zero(analyzer->Simplify(lhs - rhs));
 }
 
+IntSet IntSet::FromMinExtent(PrimExpr min, PrimExpr extent) {
+  if (is_one(extent)) {
+    return IntSet::SinglePoint(min);
+  }
+  return IntervalSet(min, extent + min - 1);

Review comment:
       I see. So looks like `min + extent - 1` won't be simplified as well. I just think that `min + extent - 1` is more natural, but anyway, it isn't a big deal.




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704961435



##########
File path: src/tir/schedule/concrete_schedule.cc
##########
@@ -439,6 +439,44 @@ BlockRV ConcreteScheduleNode::CacheWrite(const BlockRV& block_rv, int write_buff
 
 /******** Schedule: Compute location ********/
 
+void ConcreteScheduleNode::ComputeAt(const BlockRV& block_rv, const LoopRV& loop_rv,
+                                     bool preserve_unit_loops) {
+  static StmtSRef inline_mark = StmtSRef::InlineMark();
+  static StmtSRef root_mark = StmtSRef::RootMark();
+  StmtSRef loop_sref = this->GetSRef(loop_rv);
+  if (loop_sref.same_as(root_mark)) {
+    return;
+  } else if (loop_sref.same_as(inline_mark)) {
+    TVM_TIR_SCHEDULE_BEGIN();
+    tir::ComputeInline(state_, this->GetSRef(block_rv));
+    TVM_TIR_SCHEDULE_END("compute-at", this->error_render_level_);
+  } else {
+    TVM_TIR_SCHEDULE_BEGIN();
+    tir::ComputeAt(state_, this->GetSRef(block_rv), loop_sref, preserve_unit_loops);
+    TVM_TIR_SCHEDULE_END("compute-at", this->error_render_level_);
+  }
+  this->state_->DebugVerify();
+}
+
+void ConcreteScheduleNode::ReverseComputeAt(const BlockRV& block_rv, const LoopRV& loop_rv,
+                                            bool preserve_unit_loops) {
+  static StmtSRef inline_mark = StmtSRef::InlineMark();
+  static StmtSRef root_mark = StmtSRef::RootMark();
+  StmtSRef loop_sref = this->GetSRef(loop_rv);
+  if (loop_sref.same_as(root_mark)) {
+    // do nothing

Review comment:
       oh sorry it's my typo. I am going to change line 448 to "do nothing". Thanks for noticing this!




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r703889719



##########
File path: src/tir/schedule/analysis/analysis.cc
##########
@@ -174,6 +212,18 @@ int CheckCompleteBlockErrorCode(const ScheduleState& self, const StmtSRef& block
   return 0;
 }
 
+static const char* kCompleteBlockDefinition = R"(Definition of a complete block:

Review comment:
       It's TVM's naming style where we prefix constants with `k`




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



[GitHub] [tvm] MasterJH5574 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
MasterJH5574 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704558307



##########
File path: src/tir/schedule/primitive/cache_read_write.cc
##########
@@ -160,6 +160,21 @@ Block MakeCacheStage(const BufferRegion& cache_region, CacheStageInfo* info,
   return block;
 }
 
+/*!
+ * \brief Recalculate the `affine_binding` flag of a specifc block
+ * \param block_sref The sref to the specific block
+ */
+bool CalculateAffineFlag(const ScheduleState& self, const StmtSRef& block_sref) {

Review comment:
       You may also use this helper function here, in your implementation of ComputeAt https://github.com/apache/tvm/blob/5745f3d65873fdc47aa53c8db9e8fc7736007779/src/tir/schedule/primitive/compute_at.cc#L508-L511




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704729140



##########
File path: src/tir/schedule/primitive/compute_at.cc
##########
@@ -0,0 +1,584 @@
+/*
+ * 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 {
+
+using support::NDIntSet;
+
+/******** Error Classes ********/
+
+/*!
+ * \brief An error raised when not all required blocks are under the given loop.
+ * \tparam is_consumer Indicates if all the required blocks are consumers or producers
+ */
+template <bool is_consumer>
+class NotAllRequiredBlocksAreVisitedError : public ScheduleError {
+ public:
+  explicit NotAllRequiredBlocksAreVisitedError(IRModule mod, int num_not_visited,
+                                               const Array<StmtSRef>& required)
+      : mod_(mod), num_not_visited_(num_not_visited) {
+    required_.reserve(required.size());
+    for (const StmtSRef& block_sref : required) {
+      const BlockNode* block = TVM_SREF_TO_BLOCK(block, block_sref);
+      required_.push_back(GetRef<Block>(block));
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Not all required blocks are under the loop scope";
+  }
+
+  String DetailRenderTemplate() const final {
+    String relation = is_consumer ? "consumer(s)" : "producer(s)";
+    std::ostringstream os;
+    os << "The primitive requires all the " << relation
+       << " of the given block to be present under the target loop. However, there are "
+       << num_not_visited_ << " " << relation << " not satisfying the constraint. List of the "
+       << relation << ":";
+    for (int i = 0, n = required_.size(); i < n; ++i) {
+      os << "{" << i << "}";
+    }
+    return os.str();
+  }
+
+  IRModule mod() const final { return mod_; }
+
+  Array<ObjectRef> LocationsOfInterest() const final {
+    return {required_.begin(), required_.end()};
+  }
+
+ private:
+  IRModule mod_;
+  int num_not_visited_;
+  Array<Block> required_;
+};
+
+/*!
+ * \brief An error raised when the given block is not in the same block scope as the given loop,
+ * or the given loop is the ancestor of the given block.
+ */
+class NotInSameScopeError : public ScheduleError {
+ public:
+  static void CheckAndBindLoopDomain(const ScheduleState& self, const StmtSRef& block_sref,
+                                     const StmtSRef& loop_sref, const StmtSRef& scope_root_sref,
+                                     arith::Analyzer* analyzer) {
+    for (const StmtSRefNode* p = loop_sref.get();; p = p->parent) {
+      if (const ForNode* loop = p->StmtAs<ForNode>()) {
+        analyzer->Bind(loop->loop_var, Range::FromMinExtent(loop->min, loop->extent));
+      } else if (p != scope_root_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      } else {
+        break;
+      }
+    }
+    for (const StmtSRefNode* p = block_sref->parent; p != scope_root_sref.get(); p = p->parent) {
+      if (p == loop_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      }
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Expected the block and loop to be under the same block scope, and loop "
+           "not to be the ancestor of block";
+  }
+  String DetailRenderTemplate() const final {
+    return "ScheduleError: Expected the block {0} and loop {1} to be under the same block scope, "
+           "and loop not to be the ancestor of block";
+  }
+  IRModule mod() const final { return mod_; }
+  Array<ObjectRef> LocationsOfInterest() const final { return {block_, loop_}; }
+
+ private:
+  explicit NotInSameScopeError(IRModule mod, const StmtSRef& block_sref, const StmtSRef& loop_sref)
+      : mod_(mod),
+        block_(GetRef<Block>(block_sref->StmtAs<BlockNode>())),
+        loop_(GetRef<For>(loop_sref->StmtAs<ForNode>())) {}
+
+  IRModule mod_;
+  Block block_;
+  For loop_;
+};
+
+/******** Helper Functions/Classes ********/
+
+/*!
+ * \brief Find a point where the block can be inserted under the loop
+ * \tparam require_all_producers_visited Requires all producer blocks to be present under the loop
+ * \tparam require_all_consumers_visited Requires all consumer blocks to be present under the loop
+ * \param self The schedule state
+ * \param subtrees The subtrees under the loop, among which the insertion points are sought
+ * \param producer_srefs The producer blocks
+ * \param consumer_srefs The consumer blocks
+ * \param block2realize A cache that maps a block to its realize
+ * \return The last position the new block can be inserted onto, and the
+ * producer-consumer-relationship is still satisfied.
+ */
+template <bool require_all_producers_visited, bool require_all_consumers_visited>
+int FindInsertionPoint(
+    const ScheduleState& self, const Array<Stmt>& subtrees, const Array<StmtSRef>& producer_srefs,
+    const Array<StmtSRef>& consumer_srefs,
+    std::unordered_map<const BlockNode*, const BlockRealizeNode*>* block2realize) {
+  ProducerConsumerSplit split =
+      ProducerConsumerSplit::Find(self, subtrees, producer_srefs, consumer_srefs, block2realize);
+  // Step 1. Check if all the producers are visited in the subtrees, if required to
+  if (require_all_producers_visited) {
+    int num_producers = producer_srefs.size();
+    if (split.n_producers_visited < num_producers) {
+      throw NotAllRequiredBlocksAreVisitedError<false>(
+          self->mod, num_producers - split.n_producers_visited, producer_srefs);
+    }
+  }
+  // Step 2. Check if all the consumers are visited in the subtrees, if required to
+  if (require_all_consumers_visited) {
+    int num_consumers = consumer_srefs.size();
+    if (split.n_consumers_visited < num_consumers) {
+      throw NotAllRequiredBlocksAreVisitedError<true>(
+          self->mod, num_consumers - split.n_consumers_visited, consumer_srefs);
+    }
+  }
+  // Step 3. Check if there is at least one index of the position can be inserted into
+  // The valid indices are: (last_producer_position, first_consumer_position]
+  ICHECK(split.last_producer_position < split.first_consumer_position);
+  // Step 4. Return the last valid insertion point
+  return split.first_consumer_position;
+}
+
+/*!
+ * \brief A helper to reconstruct the block scope where the given block is moved under the given
+ * loop, and the given block's induced loop nest is regenerated to satisfy the required region.
+ */
+class ScopeReconstructor : private StmtMutator {
+ public:
+  explicit ScopeReconstructor(Block scope_root, Block block, For loop)
+      : scope_root_(scope_root), block_(block), loop_(loop) {}
+
+  using StmtMutator::operator();
+
+  /*!
+   * \brief Create the loop nest on top of the block, induced by the given block var's domain
+   * \param insert_position The position among the subtrees where the block and its induced loop
+   * nest is inserted
+   * \param iter_doms The domain of each block var
+   * \param preserve_unit_loops Whether to generate unit loops where the loop extent is 1
+   */
+  void MakeNewLoop(int insert_position, std::vector<Range> iter_doms, bool preserve_unit_loops) {
+    int n_iters = iter_doms.size();
+    Array<Var> loop_vars;
+    Array<PrimExpr> loop_extents;
+    Array<PrimExpr> iter_values;
+    loop_vars.reserve(n_iters);
+    loop_extents.reserve(n_iters);
+    iter_values.reserve(n_iters);
+    for (int i = 0; i < n_iters; ++i) {
+      const Range& iter_dom = iter_doms[i];
+      if (preserve_unit_loops || !is_one(iter_dom->extent)) {
+        Var var("ax" + std::to_string(loop_vars.size()), DataType::Int(32));
+        loop_vars.push_back(var);
+        loop_extents.push_back(iter_dom->extent);
+        iter_values.push_back(iter_dom->min + var);
+      } else {
+        iter_values.push_back(iter_dom->min);
+      }
+    }
+    this->new_block_realize_ =
+        BlockRealize(std::move(iter_values), const_true(), std::move(block_));
+    Stmt new_subtree = this->new_block_realize_;
+    for (int i = static_cast<int>(loop_vars.size()) - 1; i >= 0; --i) {
+      const Var& loop_var = loop_vars[i];
+      const PrimExpr& loop_extent = loop_extents[i];
+      new_subtree = For(/*loop_var=*/loop_var,
+                        /*min=*/Integer(0),
+                        /*extent=*/loop_extent,
+                        /*ForKind=*/ForKind::kSerial,
+                        /*body=*/std::move(new_subtree));
+    }
+    Array<Stmt> subtrees = AsArray(loop_->body);
+    subtrees.insert(subtrees.begin() + insert_position, std::move(new_subtree));
+    ObjectPtr<ForNode> new_loop = make_object<ForNode>(*loop_.get());
+    new_loop->body = SeqStmt(std::move(subtrees));
+    this->new_loop_ = For(std::move(new_loop));
+  }
+
+ private:
+  Stmt VisitStmt_(const BlockNode* block) final {
+    if (block != scope_root_.get()) {
+      return GetRef<Block>(block);
+    }
+    if (block == rm_src_stmt_.get()) {
+      block = TVM_TYPE_AS(block, rm_tgt_stmt_, BlockNode);
+    }
+    return StmtMutator::VisitStmt_(block);
+  }
+
+  Stmt VisitStmt_(const ForNode* loop) final {
+    if (loop == rm_src_stmt_.get()) {
+      loop = TVM_TYPE_AS(loop, rm_tgt_stmt_, ForNode);
+    }
+    if (loop == loop_.get()) {
+      return new_loop_;
+    }
+    return StmtMutator::VisitStmt_(loop);
+  }
+
+ public:
+  /*! \brief The root block of the block scope */
+  Block scope_root_;
+  /*! \brief The given block to be moved */
+  Block block_;
+  /*! \brief The given loop the block and its loop nest to be put under */
+  For loop_;
+  /*! \brief The new loop to replace the original loop */
+  For new_loop_{nullptr};
+  /*! \brief The new block realize to the moved block */
+  BlockRealize new_block_realize_{nullptr};
+  /*! \brief The plan to remove the given block by replacing this loop/block in the AST */
+  Stmt rm_src_stmt_{nullptr};
+  /*! \brief The plan to remove the given block by replacing to this loop/block in the AST */
+  Stmt rm_tgt_stmt_{nullptr};
+};
+
+/*!
+ * \brief Calculate a list of accessed buffer regions under a path of loops
+ * \tparam relax_storage_scope Whether to relax beyond the path according to the storage and
+ * execution scope
+ * \param binding The block binding, used to unbind the buffer regions
+ * \param buffer_regions The buffer regions to be calculated
+ * \param relax_path_low_inclusive The lowest point in the loop path, inclusive
+ * \param relax_path_high_exclusive The highest point in the loop path, exclusive
+ * \param relaxed Where the calculation result is stored
+ */
+template <bool relax_storage_scope>
+void RelaxBufferRegions(const Map<Var, PrimExpr>& binding,
+                        const Array<BufferRegion>& buffer_regions,
+                        const StmtSRef& relax_path_low_inclusive,
+                        const StmtSRef& relax_path_high_exclusive,
+                        std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* relaxed) {
+  runtime::StorageScope global_scope{runtime::StorageRank::kGlobal, ""};
+  // We cache the variable domains
+  runtime::StorageRank previous_rank = runtime::StorageRank::kGlobal;
+  Optional<Map<Var, arith::IntSet>> var_dom = NullOpt;
+  // Enumerate every buffer region
+  for (const BufferRegion& buffer_region : buffer_regions) {
+    const Buffer& buffer = buffer_region->buffer;
+    const Array<Range>& region = buffer_region->region;
+    // Skip the buffer regions we are not interested in
+    auto it = relaxed->find(buffer.get());
+    if (it == relaxed->end()) {
+      continue;
+    }
+    std::vector<NDIntSet>& relaxed_regions = it->second;
+    // Check and update the cached `var_dom`
+    runtime::StorageScope scope =
+        relax_storage_scope ? runtime::StorageScope::Create(buffer.scope()) : global_scope;
+    runtime::StorageRank rank = scope.rank;
+    if (rank != previous_rank || !var_dom.defined()) {
+      previous_rank = rank;
+      var_dom = AsIntSet(LoopDomainOfSRefTreePath(
+          /*low_inclusive=*/relax_path_low_inclusive,
+          /*high_exclusive=*/relax_path_high_exclusive,
+          /*extra_relax_scope=*/scope));
+    }
+    // Relax the region
+    Array<arith::IntSet> relaxed = arith::EvalSet(Substitute(region, binding), var_dom.value());
+    relaxed_regions.push_back({relaxed.begin(), relaxed.end()});
+  }
+}
+
+/*!
+ * \brief Calculate the iteration domain of a provided integer set to fully cover the required
+ * domain
+ * \param provided The provided integer set to cover the required domain
+ * \param required The required domain to be covered
+ * \param iter_doms The result iteration domains to be updated
+ * \param analyzer The arithmetic analyzer
+ */
+void UpdateBlockVarDomain(const arith::IntSet& provided, const arith::IntSet& required,
+                          std::unordered_map<const VarNode*, std::vector<arith::IntSet>>* iter_doms,
+                          arith::Analyzer* analyzer) {
+  PrimExpr provided_min = analyzer->Simplify(provided.min());
+  PrimExpr provided_extent = analyzer->Simplify(provided.max() - provided_min + 1);
+  PrimExpr required_min = analyzer->Simplify(required.min());
+  PrimExpr required_extent = analyzer->Simplify(required.max() - required_min + 1);
+  PrimExpr dom_min{nullptr}, dom_extent{nullptr};
+  Var dom_var{ObjectPtr<VarNode>{nullptr}};
+  arith::PVar<Var> p_v;
+  arith::PVar<PrimExpr> p_e;
+  if ((p_v * p_e).Match(provided_min) || (p_e * p_v).Match(provided_min)) {
+    PrimExpr e = p_e.Eval();
+    dom_var = p_v.Eval();
+    dom_min = floordiv(required_min, e);
+    dom_extent = analyzer->Simplify((required_extent + e - 1) / e);
+  } else if (analyzer->CanProveEqual(provided_extent, 1) && p_v.Match(provided_min)) {
+    dom_var = p_v.Eval();
+    dom_min = required_min;
+    dom_extent = required_extent;
+  } else {
+    ICHECK(false) << "ValueError: BufferRegion pattern match failed";
+  }
+  auto it = iter_doms->find(dom_var.get());
+  if (it != iter_doms->end()) {
+    std::vector<arith::IntSet>& doms = it->second;
+    doms.push_back(arith::IntSet::FromMinExtent(dom_min, dom_extent));
+  } else {
+    ICHECK(analyzer->CanProveEqual(provided_min, required_min));
+    ICHECK(analyzer->CanProveEqual(provided_extent, required_extent));
+  }
+}
+
+/*!
+ * \brief Calculate the domain of block vars to cover the required region
+ * \param iter_vars The list of block vars to cover the required region
+ * \param provided_regions The region provided by one iteration instance of the block vars
+ * \param required_regions The region required to be covered
+ * \param analyzer The arithmetic analyzer
+ * \return A list of iteration domain corresponding to the given list of block vars
+ */
+std::vector<Range> CalculateBlockVarDomain(
+    const Array<IterVar>& iter_vars,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>> provided_regions,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>> required_regions,
+    arith::Analyzer* analyzer) {
+  int n_iters = iter_vars.size();
+  // Step 1. Construct the mapping from block var to their iteration domain (initialized to empty)
+  std::unordered_map<const VarNode*, std::vector<arith::IntSet>> iter_doms;
+  iter_doms.reserve(n_iters);
+  for (const IterVar& iter_var : iter_vars) {
+    iter_doms[iter_var->var.get()] = {};
+  }
+  // Step 2. For each buffer, update the domain according to the provided and required regions
+  for (const auto& kv : provided_regions) {
+    const BufferNode* buffer = kv.first;
+    const std::vector<NDIntSet>& many_provided_regions = kv.second;
+    // Calculate `provided_region` and `required_region`
+    auto it = required_regions.find(buffer);
+    if (it == required_regions.end() || it->second.empty()) {
+      continue;
+    }
+    NDIntSet required_region = support::NDIntSetUnion(it->second);
+    NDIntSet provided_region = support::NDIntSetUnion(many_provided_regions);
+    ICHECK_EQ(provided_region.size(), buffer->shape.size());
+    ICHECK_EQ(required_region.size(), buffer->shape.size());
+    // For each dimension, update the iteration domain
+    int ndim = buffer->shape.size();
+    for (int i = 0; i < ndim; ++i) {
+      arith::IntSet provided = provided_region[i];
+      arith::IntSet required = required_region[i];
+      required = arith::Intersect(
+          {std::move(required), arith::IntSet::FromMinExtent(Integer(0), buffer->shape[i])});
+      UpdateBlockVarDomain(provided, required, &iter_doms, analyzer);
+    }
+  }
+  // Union the iter var domains, put them in the same order of block vars, and return
+  std::vector<Range> result;
+  result.reserve(n_iters);
+  for (const IterVar& iter_var : iter_vars) {
+    const std::vector<arith::IntSet>& doms = iter_doms.at(iter_var->var.get());
+    arith::IntSet dom = arith::IntSet::FromRange(iter_var->dom);
+    if (!doms.empty()) {
+      dom = arith::Intersect({std::move(dom), arith::Union(doms)});
+    }
+    PrimExpr min = analyzer->Simplify(dom.min());
+    PrimExpr extent = analyzer->Simplify(dom.max() - min + 1);
+    result.push_back(Range::FromMinExtent(min, extent));
+  }
+  return result;
+}
+
+/*!
+ * \brief Calculate the provided region of the given block by one single of its execution instance,
+ * as well as the required buffer regions relaxed to the given loop
+ * \tparam is_compute_at Indicates if the operation is compute-at or reverse-compute-at
+ * \param block The given block that provides buffer regions
+ * \param loop_sref The given loop under which the block is going to be moved to
+ * \param block2realize Maps a block to its corresponding BlockRealize
+ * \param producer_srefs The producers of the given block
+ * \param consumer_srefs The consumers of the given block
+ * \param provided_regions The calculated regions provided by the block
+ * \param required_regions The calculated regions required by its consumers (in compute-at) or
+ * producers (in reverse-compute-at)
+ */
+template <bool is_compute_at>
+void CalculateProvidedRequiredRegions(
+    const BlockNode* block, const StmtSRef& loop_sref,
+    std::unordered_map<const BlockNode*, const BlockRealizeNode*> block2realize,
+    Array<StmtSRef> producer_srefs, Array<StmtSRef> consumer_srefs,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* provided_regions,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* required_regions) {
+  // Step 1. Calculate the region provided by a single execution instance of `block`
+  const Array<BufferRegion>& provided_buffers = is_compute_at ? block->writes : block->reads;
+  provided_regions->reserve(provided_buffers.size());
+  required_regions->reserve(provided_buffers.size());
+  for (const BufferRegion& provided_buffer_region : provided_buffers) {
+    const BufferNode* buffer = provided_buffer_region->buffer.get();
+    const Array<Range>& region = provided_buffer_region->region;
+    (*provided_regions)[buffer].push_back(support::NDIntSetFromRegion(region));
+    (*required_regions)[buffer].clear();
+  }
+  // Step 2. Calculate the region required by dependent blocks under `loop`
+  for (const StmtSRef& required_block_sref : is_compute_at ? consumer_srefs : producer_srefs) {
+    const BlockNode* required_block = TVM_SREF_TO_BLOCK(required_block, required_block_sref);
+    ICHECK(block2realize.count(required_block));
+    RelaxBufferRegions</*relax_storage_scope=*/is_compute_at>(
+        /*binding=*/GetBindings(GetRef<BlockRealize>(block2realize.at(required_block))),
+        /*buffer_regions=*/is_compute_at ? required_block->reads : required_block->writes,
+        /*relax_path_low_inclusive=*/GetRef<StmtSRef>(required_block_sref->parent),
+        /*relax_path_high_exclusive=*/loop_sref, /*relaxed=*/required_regions);
+  }
+}
+
+/******** Main Implementation ********/
+
+template <bool is_compute_at>
+void ComputeAtOrReverseComputeAtImpl(ScheduleState self, const StmtSRef& block_sref,
+                                     const StmtSRef& loop_sref, bool preserve_unit_loops) {
+  const BlockNode* block = TVM_SREF_TO_BLOCK(block, block_sref);
+  const ForNode* loop = TVM_SREF_TO_FOR(loop, loop_sref);
+  // Step 1. Bunch of checks
+  // Check condition 1) and 2): stage pipeline and subtree compact dataflow
+  StmtSRef scope_root_sref = GetScopeRoot(self, block_sref,
+                                          /*require_stage_pipeline=*/true,
+                                          /*require_subtree_compact_dataflow=*/true);
+  Block scope_root = GetRef<Block>(scope_root_sref->StmtAs<BlockNode>());
+  BlockScope scope = self->GetBlockScope(scope_root_sref);
+  Array<StmtSRef> producer_srefs = GetProducers(block_sref, scope);
+  Array<StmtSRef> consumer_srefs = GetConsumers(block_sref, scope);
+  arith::Analyzer analyzer;
+  // Check condition 3): `block` and `loop` are under the same scope,
+  // and `loop` is not the ancestor of `block`
+  NotInSameScopeError::CheckAndBindLoopDomain(self, block_sref, loop_sref, scope_root_sref,
+                                              &analyzer);
+  // Check condition 4): `block` is not an output block
+  if (is_compute_at) {
+    CheckNotOutputBlock(self, block_sref, scope_root_sref);
+  }
+  // Step 2. Plan for the removal of `block`
+  ScopeReconstructor reconstructor(scope_root, GetRef<Block>(block), GetRef<For>(loop));
+  LeafBlockRemovalPlan(self, block_sref, &reconstructor.rm_src_stmt_, &reconstructor.rm_tgt_stmt_);
+  // Step 3. Find the insertion point under `loop`
+  // Check condition 5): all the required block are under the given loop
+  std::unordered_map<const BlockNode*, const BlockRealizeNode*> block2realize;
+  block2realize.reserve(self->block_info.size());
+  int insert_position = FindInsertionPoint<!is_compute_at, is_compute_at>(
+      /*self=*/self,
+      /*subtrees=*/AsArray(loop->body),
+      /*producer_srefs=*/producer_srefs,
+      /*consumer_srefs=*/consumer_srefs, /*block2realize=*/&block2realize);
+  // Step 4. Calculate the region provided by a single execution instance of `block`,
+  // as well as the region required by dependent blocks under `loop`

Review comment:
       probably I should make it crystal clear here in the doc




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r703887179



##########
File path: src/arith/int_set.cc
##########
@@ -607,6 +607,13 @@ inline bool ProveEqual(Analyzer* analyzer, PrimExpr lhs, PrimExpr rhs) {
   return is_zero(analyzer->Simplify(lhs - rhs));
 }
 
+IntSet IntSet::FromMinExtent(PrimExpr min, PrimExpr extent) {
+  if (is_one(extent)) {
+    return IntSet::SinglePoint(min);
+  }
+  return IntervalSet(min, extent + min - 1);

Review comment:
       I suppose there isn't any difference...I just copied the implementation a few lines above




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704131273



##########
File path: src/tir/schedule/concrete_schedule.cc
##########
@@ -439,6 +439,44 @@ BlockRV ConcreteScheduleNode::CacheWrite(const BlockRV& block_rv, int write_buff
 
 /******** Schedule: Compute location ********/
 
+void ConcreteScheduleNode::ComputeAt(const BlockRV& block_rv, const LoopRV& loop_rv,
+                                     bool preserve_unit_loops) {
+  static StmtSRef inline_mark = StmtSRef::InlineMark();
+  static StmtSRef root_mark = StmtSRef::RootMark();
+  StmtSRef loop_sref = this->GetSRef(loop_rv);

Review comment:
       We should never ever insert `InlineMark` and `RootMark` into the symbol table, if we do, it is a bug. The two marks are only used to convert a compute-at into compute-inline, and thus is more like a constant, not random variables in the symbol table




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r703888018



##########
File path: src/support/nd_int_set.h
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.
+ */
+#ifndef TVM_SUPPORT_ND_INT_SET_H_
+#define TVM_SUPPORT_ND_INT_SET_H_
+
+#include <tvm/arith/int_set.h>
+#include <tvm/ir/expr.h>
+
+#include <unordered_map>
+#include <vector>
+
+namespace tvm {
+namespace support {
+
+/*! \brief An N-dimensional integer set representing a rectangle region */
+using NDIntSet = std::vector<tvm::arith::IntSet>;

Review comment:
       Q1: Because it provides more convenience in some of our implementation (in the compact-buffer-region pass, as well as our future feature extraction)
   Q2: Yep. Thanks for pointing this out!




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704690833



##########
File path: src/tir/schedule/primitive/compute_at.cc
##########
@@ -0,0 +1,584 @@
+/*
+ * 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 {
+
+using support::NDIntSet;
+
+/******** Error Classes ********/
+
+/*!
+ * \brief An error raised when not all required blocks are under the given loop.
+ * \tparam is_consumer Indicates if all the required blocks are consumers or producers
+ */
+template <bool is_consumer>
+class NotAllRequiredBlocksAreVisitedError : public ScheduleError {
+ public:
+  explicit NotAllRequiredBlocksAreVisitedError(IRModule mod, int num_not_visited,
+                                               const Array<StmtSRef>& required)
+      : mod_(mod), num_not_visited_(num_not_visited) {
+    required_.reserve(required.size());
+    for (const StmtSRef& block_sref : required) {
+      const BlockNode* block = TVM_SREF_TO_BLOCK(block, block_sref);
+      required_.push_back(GetRef<Block>(block));
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Not all required blocks are under the loop scope";
+  }
+
+  String DetailRenderTemplate() const final {
+    String relation = is_consumer ? "consumer(s)" : "producer(s)";
+    std::ostringstream os;
+    os << "The primitive requires all the " << relation
+       << " of the given block to be present under the target loop. However, there are "
+       << num_not_visited_ << " " << relation << " not satisfying the constraint. List of the "
+       << relation << ":";
+    for (int i = 0, n = required_.size(); i < n; ++i) {
+      os << "{" << i << "}";
+    }
+    return os.str();
+  }
+
+  IRModule mod() const final { return mod_; }
+
+  Array<ObjectRef> LocationsOfInterest() const final {
+    return {required_.begin(), required_.end()};
+  }
+
+ private:
+  IRModule mod_;
+  int num_not_visited_;
+  Array<Block> required_;
+};
+
+/*!
+ * \brief An error raised when the given block is not in the same block scope as the given loop,
+ * or the given loop is the ancestor of the given block.
+ */
+class NotInSameScopeError : public ScheduleError {
+ public:
+  static void CheckAndBindLoopDomain(const ScheduleState& self, const StmtSRef& block_sref,
+                                     const StmtSRef& loop_sref, const StmtSRef& scope_root_sref,
+                                     arith::Analyzer* analyzer) {
+    for (const StmtSRefNode* p = loop_sref.get();; p = p->parent) {
+      if (const ForNode* loop = p->StmtAs<ForNode>()) {
+        analyzer->Bind(loop->loop_var, Range::FromMinExtent(loop->min, loop->extent));
+      } else if (p != scope_root_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      } else {
+        break;
+      }
+    }
+    for (const StmtSRefNode* p = block_sref->parent; p != scope_root_sref.get(); p = p->parent) {
+      if (p == loop_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      }
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Expected the block and loop to be under the same block scope, and loop "
+           "not to be the ancestor of block";
+  }
+  String DetailRenderTemplate() const final {
+    return "ScheduleError: Expected the block {0} and loop {1} to be under the same block scope, "
+           "and loop not to be the ancestor of block";
+  }
+  IRModule mod() const final { return mod_; }
+  Array<ObjectRef> LocationsOfInterest() const final { return {block_, loop_}; }
+
+ private:
+  explicit NotInSameScopeError(IRModule mod, const StmtSRef& block_sref, const StmtSRef& loop_sref)
+      : mod_(mod),
+        block_(GetRef<Block>(block_sref->StmtAs<BlockNode>())),
+        loop_(GetRef<For>(loop_sref->StmtAs<ForNode>())) {}
+
+  IRModule mod_;
+  Block block_;
+  For loop_;
+};
+
+/******** Helper Functions/Classes ********/
+
+/*!
+ * \brief Find a point where the block can be inserted under the loop
+ * \tparam require_all_producers_visited Requires all producer blocks to be present under the loop
+ * \tparam require_all_consumers_visited Requires all consumer blocks to be present under the loop
+ * \param self The schedule state
+ * \param subtrees The subtrees under the loop, among which the insertion points are sought
+ * \param producer_srefs The producer blocks
+ * \param consumer_srefs The consumer blocks
+ * \param block2realize A cache that maps a block to its realize
+ * \return The last position the new block can be inserted onto, and the
+ * producer-consumer-relationship is still satisfied.
+ */
+template <bool require_all_producers_visited, bool require_all_consumers_visited>
+int FindInsertionPoint(
+    const ScheduleState& self, const Array<Stmt>& subtrees, const Array<StmtSRef>& producer_srefs,
+    const Array<StmtSRef>& consumer_srefs,
+    std::unordered_map<const BlockNode*, const BlockRealizeNode*>* block2realize) {
+  ProducerConsumerSplit split =
+      ProducerConsumerSplit::Find(self, subtrees, producer_srefs, consumer_srefs, block2realize);
+  // Step 1. Check if all the producers are visited in the subtrees, if required to
+  if (require_all_producers_visited) {
+    int num_producers = producer_srefs.size();
+    if (split.n_producers_visited < num_producers) {
+      throw NotAllRequiredBlocksAreVisitedError<false>(
+          self->mod, num_producers - split.n_producers_visited, producer_srefs);
+    }
+  }
+  // Step 2. Check if all the consumers are visited in the subtrees, if required to
+  if (require_all_consumers_visited) {
+    int num_consumers = consumer_srefs.size();
+    if (split.n_consumers_visited < num_consumers) {
+      throw NotAllRequiredBlocksAreVisitedError<true>(
+          self->mod, num_consumers - split.n_consumers_visited, consumer_srefs);
+    }
+  }
+  // Step 3. Check if there is at least one index of the position can be inserted into
+  // The valid indices are: (last_producer_position, first_consumer_position]
+  ICHECK(split.last_producer_position < split.first_consumer_position);
+  // Step 4. Return the last valid insertion point
+  return split.first_consumer_position;
+}
+
+/*!
+ * \brief A helper to reconstruct the block scope where the given block is moved under the given
+ * loop, and the given block's induced loop nest is regenerated to satisfy the required region.
+ */
+class ScopeReconstructor : private StmtMutator {
+ public:
+  explicit ScopeReconstructor(Block scope_root, Block block, For loop)
+      : scope_root_(scope_root), block_(block), loop_(loop) {}
+
+  using StmtMutator::operator();
+
+  /*!
+   * \brief Create the loop nest on top of the block, induced by the given block var's domain
+   * \param insert_position The position among the subtrees where the block and its induced loop
+   * nest is inserted
+   * \param iter_doms The domain of each block var
+   * \param preserve_unit_loops Whether to generate unit loops where the loop extent is 1
+   */
+  void MakeNewLoop(int insert_position, std::vector<Range> iter_doms, bool preserve_unit_loops) {
+    int n_iters = iter_doms.size();
+    Array<Var> loop_vars;
+    Array<PrimExpr> loop_extents;
+    Array<PrimExpr> iter_values;
+    loop_vars.reserve(n_iters);
+    loop_extents.reserve(n_iters);
+    iter_values.reserve(n_iters);
+    for (int i = 0; i < n_iters; ++i) {
+      const Range& iter_dom = iter_doms[i];
+      if (preserve_unit_loops || !is_one(iter_dom->extent)) {
+        Var var("ax" + std::to_string(loop_vars.size()), DataType::Int(32));
+        loop_vars.push_back(var);
+        loop_extents.push_back(iter_dom->extent);
+        iter_values.push_back(iter_dom->min + var);
+      } else {
+        iter_values.push_back(iter_dom->min);
+      }
+    }
+    this->new_block_realize_ =
+        BlockRealize(std::move(iter_values), const_true(), std::move(block_));
+    Stmt new_subtree = this->new_block_realize_;
+    for (int i = static_cast<int>(loop_vars.size()) - 1; i >= 0; --i) {
+      const Var& loop_var = loop_vars[i];
+      const PrimExpr& loop_extent = loop_extents[i];
+      new_subtree = For(/*loop_var=*/loop_var,
+                        /*min=*/Integer(0),
+                        /*extent=*/loop_extent,
+                        /*ForKind=*/ForKind::kSerial,
+                        /*body=*/std::move(new_subtree));
+    }
+    Array<Stmt> subtrees = AsArray(loop_->body);
+    subtrees.insert(subtrees.begin() + insert_position, std::move(new_subtree));
+    ObjectPtr<ForNode> new_loop = make_object<ForNode>(*loop_.get());
+    new_loop->body = SeqStmt(std::move(subtrees));
+    this->new_loop_ = For(std::move(new_loop));
+  }
+
+ private:
+  Stmt VisitStmt_(const BlockNode* block) final {
+    if (block != scope_root_.get()) {
+      return GetRef<Block>(block);
+    }
+    if (block == rm_src_stmt_.get()) {
+      block = TVM_TYPE_AS(block, rm_tgt_stmt_, BlockNode);
+    }
+    return StmtMutator::VisitStmt_(block);
+  }
+
+  Stmt VisitStmt_(const ForNode* loop) final {
+    if (loop == rm_src_stmt_.get()) {
+      loop = TVM_TYPE_AS(loop, rm_tgt_stmt_, ForNode);
+    }
+    if (loop == loop_.get()) {
+      return new_loop_;
+    }
+    return StmtMutator::VisitStmt_(loop);
+  }
+
+ public:
+  /*! \brief The root block of the block scope */
+  Block scope_root_;
+  /*! \brief The given block to be moved */
+  Block block_;
+  /*! \brief The given loop the block and its loop nest to be put under */
+  For loop_;
+  /*! \brief The new loop to replace the original loop */
+  For new_loop_{nullptr};
+  /*! \brief The new block realize to the moved block */
+  BlockRealize new_block_realize_{nullptr};
+  /*! \brief The plan to remove the given block by replacing this loop/block in the AST */
+  Stmt rm_src_stmt_{nullptr};
+  /*! \brief The plan to remove the given block by replacing to this loop/block in the AST */
+  Stmt rm_tgt_stmt_{nullptr};
+};
+
+/*!
+ * \brief Calculate a list of accessed buffer regions under a path of loops
+ * \tparam relax_storage_scope Whether to relax beyond the path according to the storage and
+ * execution scope
+ * \param binding The block binding, used to unbind the buffer regions
+ * \param buffer_regions The buffer regions to be calculated
+ * \param relax_path_low_inclusive The lowest point in the loop path, inclusive
+ * \param relax_path_high_exclusive The highest point in the loop path, exclusive
+ * \param relaxed Where the calculation result is stored
+ */
+template <bool relax_storage_scope>
+void RelaxBufferRegions(const Map<Var, PrimExpr>& binding,
+                        const Array<BufferRegion>& buffer_regions,
+                        const StmtSRef& relax_path_low_inclusive,
+                        const StmtSRef& relax_path_high_exclusive,
+                        std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* relaxed) {
+  runtime::StorageScope global_scope{runtime::StorageRank::kGlobal, ""};
+  // We cache the variable domains
+  runtime::StorageRank previous_rank = runtime::StorageRank::kGlobal;
+  Optional<Map<Var, arith::IntSet>> var_dom = NullOpt;
+  // Enumerate every buffer region
+  for (const BufferRegion& buffer_region : buffer_regions) {
+    const Buffer& buffer = buffer_region->buffer;
+    const Array<Range>& region = buffer_region->region;
+    // Skip the buffer regions we are not interested in
+    auto it = relaxed->find(buffer.get());
+    if (it == relaxed->end()) {
+      continue;
+    }
+    std::vector<NDIntSet>& relaxed_regions = it->second;
+    // Check and update the cached `var_dom`
+    runtime::StorageScope scope =
+        relax_storage_scope ? runtime::StorageScope::Create(buffer.scope()) : global_scope;
+    runtime::StorageRank rank = scope.rank;
+    if (rank != previous_rank || !var_dom.defined()) {
+      previous_rank = rank;
+      var_dom = AsIntSet(LoopDomainOfSRefTreePath(
+          /*low_inclusive=*/relax_path_low_inclusive,
+          /*high_exclusive=*/relax_path_high_exclusive,
+          /*extra_relax_scope=*/scope));
+    }
+    // Relax the region
+    Array<arith::IntSet> relaxed = arith::EvalSet(Substitute(region, binding), var_dom.value());
+    relaxed_regions.push_back({relaxed.begin(), relaxed.end()});
+  }
+}
+
+/*!
+ * \brief Calculate the iteration domain of a provided integer set to fully cover the required
+ * domain
+ * \param provided The provided integer set to cover the required domain
+ * \param required The required domain to be covered
+ * \param iter_doms The result iteration domains to be updated
+ * \param analyzer The arithmetic analyzer
+ */
+void UpdateBlockVarDomain(const arith::IntSet& provided, const arith::IntSet& required,
+                          std::unordered_map<const VarNode*, std::vector<arith::IntSet>>* iter_doms,
+                          arith::Analyzer* analyzer) {
+  PrimExpr provided_min = analyzer->Simplify(provided.min());
+  PrimExpr provided_extent = analyzer->Simplify(provided.max() - provided_min + 1);
+  PrimExpr required_min = analyzer->Simplify(required.min());
+  PrimExpr required_extent = analyzer->Simplify(required.max() - required_min + 1);
+  PrimExpr dom_min{nullptr}, dom_extent{nullptr};
+  Var dom_var{ObjectPtr<VarNode>{nullptr}};
+  arith::PVar<Var> p_v;
+  arith::PVar<PrimExpr> p_e;
+  if ((p_v * p_e).Match(provided_min) || (p_e * p_v).Match(provided_min)) {
+    PrimExpr e = p_e.Eval();
+    dom_var = p_v.Eval();
+    dom_min = floordiv(required_min, e);
+    dom_extent = analyzer->Simplify((required_extent + e - 1) / e);
+  } else if (analyzer->CanProveEqual(provided_extent, 1) && p_v.Match(provided_min)) {
+    dom_var = p_v.Eval();
+    dom_min = required_min;
+    dom_extent = required_extent;
+  } else {
+    ICHECK(false) << "ValueError: BufferRegion pattern match failed";
+  }
+  auto it = iter_doms->find(dom_var.get());
+  if (it != iter_doms->end()) {
+    std::vector<arith::IntSet>& doms = it->second;
+    doms.push_back(arith::IntSet::FromMinExtent(dom_min, dom_extent));
+  } else {
+    ICHECK(analyzer->CanProveEqual(provided_min, required_min));
+    ICHECK(analyzer->CanProveEqual(provided_extent, required_extent));
+  }
+}
+
+/*!
+ * \brief Calculate the domain of block vars to cover the required region
+ * \param iter_vars The list of block vars to cover the required region
+ * \param provided_regions The region provided by one iteration instance of the block vars
+ * \param required_regions The region required to be covered
+ * \param analyzer The arithmetic analyzer
+ * \return A list of iteration domain corresponding to the given list of block vars
+ */
+std::vector<Range> CalculateBlockVarDomain(
+    const Array<IterVar>& iter_vars,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>> provided_regions,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>> required_regions,
+    arith::Analyzer* analyzer) {
+  int n_iters = iter_vars.size();
+  // Step 1. Construct the mapping from block var to their iteration domain (initialized to empty)
+  std::unordered_map<const VarNode*, std::vector<arith::IntSet>> iter_doms;
+  iter_doms.reserve(n_iters);
+  for (const IterVar& iter_var : iter_vars) {
+    iter_doms[iter_var->var.get()] = {};
+  }
+  // Step 2. For each buffer, update the domain according to the provided and required regions
+  for (const auto& kv : provided_regions) {
+    const BufferNode* buffer = kv.first;
+    const std::vector<NDIntSet>& many_provided_regions = kv.second;
+    // Calculate `provided_region` and `required_region`
+    auto it = required_regions.find(buffer);
+    if (it == required_regions.end() || it->second.empty()) {
+      continue;
+    }
+    NDIntSet required_region = support::NDIntSetUnion(it->second);
+    NDIntSet provided_region = support::NDIntSetUnion(many_provided_regions);
+    ICHECK_EQ(provided_region.size(), buffer->shape.size());
+    ICHECK_EQ(required_region.size(), buffer->shape.size());
+    // For each dimension, update the iteration domain
+    int ndim = buffer->shape.size();
+    for (int i = 0; i < ndim; ++i) {
+      arith::IntSet provided = provided_region[i];
+      arith::IntSet required = required_region[i];
+      required = arith::Intersect(
+          {std::move(required), arith::IntSet::FromMinExtent(Integer(0), buffer->shape[i])});
+      UpdateBlockVarDomain(provided, required, &iter_doms, analyzer);
+    }
+  }
+  // Union the iter var domains, put them in the same order of block vars, and return
+  std::vector<Range> result;
+  result.reserve(n_iters);
+  for (const IterVar& iter_var : iter_vars) {
+    const std::vector<arith::IntSet>& doms = iter_doms.at(iter_var->var.get());
+    arith::IntSet dom = arith::IntSet::FromRange(iter_var->dom);
+    if (!doms.empty()) {
+      dom = arith::Intersect({std::move(dom), arith::Union(doms)});
+    }
+    PrimExpr min = analyzer->Simplify(dom.min());
+    PrimExpr extent = analyzer->Simplify(dom.max() - min + 1);
+    result.push_back(Range::FromMinExtent(min, extent));
+  }
+  return result;
+}
+
+/*!
+ * \brief Calculate the provided region of the given block by one single of its execution instance,
+ * as well as the required buffer regions relaxed to the given loop
+ * \tparam is_compute_at Indicates if the operation is compute-at or reverse-compute-at
+ * \param block The given block that provides buffer regions
+ * \param loop_sref The given loop under which the block is going to be moved to
+ * \param block2realize Maps a block to its corresponding BlockRealize
+ * \param producer_srefs The producers of the given block
+ * \param consumer_srefs The consumers of the given block
+ * \param provided_regions The calculated regions provided by the block
+ * \param required_regions The calculated regions required by its consumers (in compute-at) or
+ * producers (in reverse-compute-at)
+ */
+template <bool is_compute_at>
+void CalculateProvidedRequiredRegions(
+    const BlockNode* block, const StmtSRef& loop_sref,
+    std::unordered_map<const BlockNode*, const BlockRealizeNode*> block2realize,
+    Array<StmtSRef> producer_srefs, Array<StmtSRef> consumer_srefs,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* provided_regions,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* required_regions) {
+  // Step 1. Calculate the region provided by a single execution instance of `block`
+  const Array<BufferRegion>& provided_buffers = is_compute_at ? block->writes : block->reads;
+  provided_regions->reserve(provided_buffers.size());
+  required_regions->reserve(provided_buffers.size());
+  for (const BufferRegion& provided_buffer_region : provided_buffers) {
+    const BufferNode* buffer = provided_buffer_region->buffer.get();
+    const Array<Range>& region = provided_buffer_region->region;
+    (*provided_regions)[buffer].push_back(support::NDIntSetFromRegion(region));
+    (*required_regions)[buffer].clear();

Review comment:
       just do a random operation to make sure the entry for `buffer` is there




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



[GitHub] [tvm] MasterJH5574 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
MasterJH5574 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704577677



##########
File path: src/tir/schedule/primitive/compute_at.cc
##########
@@ -0,0 +1,584 @@
+/*
+ * 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 {
+
+using support::NDIntSet;
+
+/******** Error Classes ********/
+
+/*!
+ * \brief An error raised when not all required blocks are under the given loop.
+ * \tparam is_consumer Indicates if all the required blocks are consumers or producers
+ */
+template <bool is_consumer>
+class NotAllRequiredBlocksAreVisitedError : public ScheduleError {
+ public:
+  explicit NotAllRequiredBlocksAreVisitedError(IRModule mod, int num_not_visited,
+                                               const Array<StmtSRef>& required)
+      : mod_(mod), num_not_visited_(num_not_visited) {
+    required_.reserve(required.size());
+    for (const StmtSRef& block_sref : required) {
+      const BlockNode* block = TVM_SREF_TO_BLOCK(block, block_sref);
+      required_.push_back(GetRef<Block>(block));
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Not all required blocks are under the loop scope";
+  }
+
+  String DetailRenderTemplate() const final {
+    String relation = is_consumer ? "consumer(s)" : "producer(s)";
+    std::ostringstream os;
+    os << "The primitive requires all the " << relation
+       << " of the given block to be present under the target loop. However, there are "
+       << num_not_visited_ << " " << relation << " not satisfying the constraint. List of the "
+       << relation << ":";
+    for (int i = 0, n = required_.size(); i < n; ++i) {
+      os << "{" << i << "}";
+    }
+    return os.str();
+  }
+
+  IRModule mod() const final { return mod_; }
+
+  Array<ObjectRef> LocationsOfInterest() const final {
+    return {required_.begin(), required_.end()};
+  }
+
+ private:
+  IRModule mod_;
+  int num_not_visited_;
+  Array<Block> required_;
+};
+
+/*!
+ * \brief An error raised when the given block is not in the same block scope as the given loop,
+ * or the given loop is the ancestor of the given block.
+ */
+class NotInSameScopeError : public ScheduleError {
+ public:
+  static void CheckAndBindLoopDomain(const ScheduleState& self, const StmtSRef& block_sref,
+                                     const StmtSRef& loop_sref, const StmtSRef& scope_root_sref,
+                                     arith::Analyzer* analyzer) {
+    for (const StmtSRefNode* p = loop_sref.get();; p = p->parent) {
+      if (const ForNode* loop = p->StmtAs<ForNode>()) {
+        analyzer->Bind(loop->loop_var, Range::FromMinExtent(loop->min, loop->extent));
+      } else if (p != scope_root_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      } else {
+        break;
+      }
+    }
+    for (const StmtSRefNode* p = block_sref->parent; p != scope_root_sref.get(); p = p->parent) {
+      if (p == loop_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      }
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Expected the block and loop to be under the same block scope, and loop "
+           "not to be the ancestor of block";
+  }
+  String DetailRenderTemplate() const final {
+    return "ScheduleError: Expected the block {0} and loop {1} to be under the same block scope, "
+           "and loop not to be the ancestor of block";
+  }
+  IRModule mod() const final { return mod_; }
+  Array<ObjectRef> LocationsOfInterest() const final { return {block_, loop_}; }
+
+ private:
+  explicit NotInSameScopeError(IRModule mod, const StmtSRef& block_sref, const StmtSRef& loop_sref)
+      : mod_(mod),
+        block_(GetRef<Block>(block_sref->StmtAs<BlockNode>())),
+        loop_(GetRef<For>(loop_sref->StmtAs<ForNode>())) {}
+
+  IRModule mod_;
+  Block block_;
+  For loop_;
+};
+
+/******** Helper Functions/Classes ********/
+
+/*!
+ * \brief Find a point where the block can be inserted under the loop
+ * \tparam require_all_producers_visited Requires all producer blocks to be present under the loop
+ * \tparam require_all_consumers_visited Requires all consumer blocks to be present under the loop
+ * \param self The schedule state
+ * \param subtrees The subtrees under the loop, among which the insertion points are sought
+ * \param producer_srefs The producer blocks
+ * \param consumer_srefs The consumer blocks
+ * \param block2realize A cache that maps a block to its realize
+ * \return The last position the new block can be inserted onto, and the
+ * producer-consumer-relationship is still satisfied.
+ */
+template <bool require_all_producers_visited, bool require_all_consumers_visited>
+int FindInsertionPoint(
+    const ScheduleState& self, const Array<Stmt>& subtrees, const Array<StmtSRef>& producer_srefs,
+    const Array<StmtSRef>& consumer_srefs,
+    std::unordered_map<const BlockNode*, const BlockRealizeNode*>* block2realize) {
+  ProducerConsumerSplit split =
+      ProducerConsumerSplit::Find(self, subtrees, producer_srefs, consumer_srefs, block2realize);
+  // Step 1. Check if all the producers are visited in the subtrees, if required to
+  if (require_all_producers_visited) {
+    int num_producers = producer_srefs.size();
+    if (split.n_producers_visited < num_producers) {
+      throw NotAllRequiredBlocksAreVisitedError<false>(
+          self->mod, num_producers - split.n_producers_visited, producer_srefs);
+    }
+  }
+  // Step 2. Check if all the consumers are visited in the subtrees, if required to
+  if (require_all_consumers_visited) {
+    int num_consumers = consumer_srefs.size();
+    if (split.n_consumers_visited < num_consumers) {
+      throw NotAllRequiredBlocksAreVisitedError<true>(
+          self->mod, num_consumers - split.n_consumers_visited, consumer_srefs);
+    }
+  }
+  // Step 3. Check if there is at least one index of the position can be inserted into
+  // The valid indices are: (last_producer_position, first_consumer_position]
+  ICHECK(split.last_producer_position < split.first_consumer_position);
+  // Step 4. Return the last valid insertion point
+  return split.first_consumer_position;
+}
+
+/*!
+ * \brief A helper to reconstruct the block scope where the given block is moved under the given
+ * loop, and the given block's induced loop nest is regenerated to satisfy the required region.
+ */
+class ScopeReconstructor : private StmtMutator {
+ public:
+  explicit ScopeReconstructor(Block scope_root, Block block, For loop)
+      : scope_root_(scope_root), block_(block), loop_(loop) {}
+
+  using StmtMutator::operator();

Review comment:
       What does this line mean?

##########
File path: src/tir/schedule/primitive/compute_at.cc
##########
@@ -0,0 +1,584 @@
+/*
+ * 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 {
+
+using support::NDIntSet;
+
+/******** Error Classes ********/
+
+/*!
+ * \brief An error raised when not all required blocks are under the given loop.
+ * \tparam is_consumer Indicates if all the required blocks are consumers or producers
+ */
+template <bool is_consumer>
+class NotAllRequiredBlocksAreVisitedError : public ScheduleError {
+ public:
+  explicit NotAllRequiredBlocksAreVisitedError(IRModule mod, int num_not_visited,
+                                               const Array<StmtSRef>& required)
+      : mod_(mod), num_not_visited_(num_not_visited) {
+    required_.reserve(required.size());
+    for (const StmtSRef& block_sref : required) {
+      const BlockNode* block = TVM_SREF_TO_BLOCK(block, block_sref);
+      required_.push_back(GetRef<Block>(block));
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Not all required blocks are under the loop scope";
+  }
+
+  String DetailRenderTemplate() const final {
+    String relation = is_consumer ? "consumer(s)" : "producer(s)";
+    std::ostringstream os;
+    os << "The primitive requires all the " << relation
+       << " of the given block to be present under the target loop. However, there are "
+       << num_not_visited_ << " " << relation << " not satisfying the constraint. List of the "
+       << relation << ":";
+    for (int i = 0, n = required_.size(); i < n; ++i) {
+      os << "{" << i << "}";
+    }
+    return os.str();
+  }
+
+  IRModule mod() const final { return mod_; }
+
+  Array<ObjectRef> LocationsOfInterest() const final {
+    return {required_.begin(), required_.end()};
+  }
+
+ private:
+  IRModule mod_;
+  int num_not_visited_;
+  Array<Block> required_;
+};
+
+/*!
+ * \brief An error raised when the given block is not in the same block scope as the given loop,
+ * or the given loop is the ancestor of the given block.
+ */
+class NotInSameScopeError : public ScheduleError {
+ public:
+  static void CheckAndBindLoopDomain(const ScheduleState& self, const StmtSRef& block_sref,
+                                     const StmtSRef& loop_sref, const StmtSRef& scope_root_sref,
+                                     arith::Analyzer* analyzer) {
+    for (const StmtSRefNode* p = loop_sref.get();; p = p->parent) {
+      if (const ForNode* loop = p->StmtAs<ForNode>()) {
+        analyzer->Bind(loop->loop_var, Range::FromMinExtent(loop->min, loop->extent));
+      } else if (p != scope_root_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      } else {
+        break;
+      }
+    }
+    for (const StmtSRefNode* p = block_sref->parent; p != scope_root_sref.get(); p = p->parent) {
+      if (p == loop_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      }
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Expected the block and loop to be under the same block scope, and loop "
+           "not to be the ancestor of block";
+  }
+  String DetailRenderTemplate() const final {
+    return "ScheduleError: Expected the block {0} and loop {1} to be under the same block scope, "
+           "and loop not to be the ancestor of block";
+  }
+  IRModule mod() const final { return mod_; }
+  Array<ObjectRef> LocationsOfInterest() const final { return {block_, loop_}; }
+
+ private:
+  explicit NotInSameScopeError(IRModule mod, const StmtSRef& block_sref, const StmtSRef& loop_sref)
+      : mod_(mod),
+        block_(GetRef<Block>(block_sref->StmtAs<BlockNode>())),
+        loop_(GetRef<For>(loop_sref->StmtAs<ForNode>())) {}
+
+  IRModule mod_;
+  Block block_;
+  For loop_;
+};
+
+/******** Helper Functions/Classes ********/
+
+/*!
+ * \brief Find a point where the block can be inserted under the loop
+ * \tparam require_all_producers_visited Requires all producer blocks to be present under the loop
+ * \tparam require_all_consumers_visited Requires all consumer blocks to be present under the loop
+ * \param self The schedule state
+ * \param subtrees The subtrees under the loop, among which the insertion points are sought
+ * \param producer_srefs The producer blocks
+ * \param consumer_srefs The consumer blocks
+ * \param block2realize A cache that maps a block to its realize
+ * \return The last position the new block can be inserted onto, and the
+ * producer-consumer-relationship is still satisfied.
+ */

Review comment:
       ```suggestion
    * \return The last position the new block can be inserted onto, and the
    * producer-consumer-relationship is still satisfied.
    * \throws ScheduleError If blabla
    */
   ```




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704691642



##########
File path: src/tir/schedule/primitive/compute_at.cc
##########
@@ -0,0 +1,584 @@
+/*
+ * 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 {
+
+using support::NDIntSet;
+
+/******** Error Classes ********/
+
+/*!
+ * \brief An error raised when not all required blocks are under the given loop.
+ * \tparam is_consumer Indicates if all the required blocks are consumers or producers
+ */
+template <bool is_consumer>
+class NotAllRequiredBlocksAreVisitedError : public ScheduleError {
+ public:
+  explicit NotAllRequiredBlocksAreVisitedError(IRModule mod, int num_not_visited,
+                                               const Array<StmtSRef>& required)
+      : mod_(mod), num_not_visited_(num_not_visited) {
+    required_.reserve(required.size());
+    for (const StmtSRef& block_sref : required) {
+      const BlockNode* block = TVM_SREF_TO_BLOCK(block, block_sref);
+      required_.push_back(GetRef<Block>(block));
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Not all required blocks are under the loop scope";
+  }
+
+  String DetailRenderTemplate() const final {
+    String relation = is_consumer ? "consumer(s)" : "producer(s)";
+    std::ostringstream os;
+    os << "The primitive requires all the " << relation
+       << " of the given block to be present under the target loop. However, there are "
+       << num_not_visited_ << " " << relation << " not satisfying the constraint. List of the "
+       << relation << ":";
+    for (int i = 0, n = required_.size(); i < n; ++i) {
+      os << "{" << i << "}";
+    }
+    return os.str();
+  }
+
+  IRModule mod() const final { return mod_; }
+
+  Array<ObjectRef> LocationsOfInterest() const final {
+    return {required_.begin(), required_.end()};
+  }
+
+ private:
+  IRModule mod_;
+  int num_not_visited_;
+  Array<Block> required_;
+};
+
+/*!
+ * \brief An error raised when the given block is not in the same block scope as the given loop,
+ * or the given loop is the ancestor of the given block.
+ */
+class NotInSameScopeError : public ScheduleError {
+ public:
+  static void CheckAndBindLoopDomain(const ScheduleState& self, const StmtSRef& block_sref,
+                                     const StmtSRef& loop_sref, const StmtSRef& scope_root_sref,
+                                     arith::Analyzer* analyzer) {
+    for (const StmtSRefNode* p = loop_sref.get();; p = p->parent) {
+      if (const ForNode* loop = p->StmtAs<ForNode>()) {
+        analyzer->Bind(loop->loop_var, Range::FromMinExtent(loop->min, loop->extent));
+      } else if (p != scope_root_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      } else {
+        break;
+      }
+    }
+    for (const StmtSRefNode* p = block_sref->parent; p != scope_root_sref.get(); p = p->parent) {
+      if (p == loop_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      }
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Expected the block and loop to be under the same block scope, and loop "
+           "not to be the ancestor of block";
+  }
+  String DetailRenderTemplate() const final {
+    return "ScheduleError: Expected the block {0} and loop {1} to be under the same block scope, "
+           "and loop not to be the ancestor of block";
+  }
+  IRModule mod() const final { return mod_; }
+  Array<ObjectRef> LocationsOfInterest() const final { return {block_, loop_}; }
+
+ private:
+  explicit NotInSameScopeError(IRModule mod, const StmtSRef& block_sref, const StmtSRef& loop_sref)
+      : mod_(mod),
+        block_(GetRef<Block>(block_sref->StmtAs<BlockNode>())),
+        loop_(GetRef<For>(loop_sref->StmtAs<ForNode>())) {}
+
+  IRModule mod_;
+  Block block_;
+  For loop_;
+};
+
+/******** Helper Functions/Classes ********/
+
+/*!
+ * \brief Find a point where the block can be inserted under the loop
+ * \tparam require_all_producers_visited Requires all producer blocks to be present under the loop
+ * \tparam require_all_consumers_visited Requires all consumer blocks to be present under the loop
+ * \param self The schedule state
+ * \param subtrees The subtrees under the loop, among which the insertion points are sought
+ * \param producer_srefs The producer blocks
+ * \param consumer_srefs The consumer blocks
+ * \param block2realize A cache that maps a block to its realize
+ * \return The last position the new block can be inserted onto, and the
+ * producer-consumer-relationship is still satisfied.
+ */
+template <bool require_all_producers_visited, bool require_all_consumers_visited>
+int FindInsertionPoint(
+    const ScheduleState& self, const Array<Stmt>& subtrees, const Array<StmtSRef>& producer_srefs,
+    const Array<StmtSRef>& consumer_srefs,
+    std::unordered_map<const BlockNode*, const BlockRealizeNode*>* block2realize) {
+  ProducerConsumerSplit split =
+      ProducerConsumerSplit::Find(self, subtrees, producer_srefs, consumer_srefs, block2realize);
+  // Step 1. Check if all the producers are visited in the subtrees, if required to
+  if (require_all_producers_visited) {
+    int num_producers = producer_srefs.size();
+    if (split.n_producers_visited < num_producers) {
+      throw NotAllRequiredBlocksAreVisitedError<false>(
+          self->mod, num_producers - split.n_producers_visited, producer_srefs);
+    }
+  }
+  // Step 2. Check if all the consumers are visited in the subtrees, if required to
+  if (require_all_consumers_visited) {
+    int num_consumers = consumer_srefs.size();
+    if (split.n_consumers_visited < num_consumers) {
+      throw NotAllRequiredBlocksAreVisitedError<true>(
+          self->mod, num_consumers - split.n_consumers_visited, consumer_srefs);
+    }
+  }
+  // Step 3. Check if there is at least one index of the position can be inserted into
+  // The valid indices are: (last_producer_position, first_consumer_position]
+  ICHECK(split.last_producer_position < split.first_consumer_position);
+  // Step 4. Return the last valid insertion point
+  return split.first_consumer_position;
+}
+
+/*!
+ * \brief A helper to reconstruct the block scope where the given block is moved under the given
+ * loop, and the given block's induced loop nest is regenerated to satisfy the required region.
+ */
+class ScopeReconstructor : private StmtMutator {
+ public:
+  explicit ScopeReconstructor(Block scope_root, Block block, For loop)
+      : scope_root_(scope_root), block_(block), loop_(loop) {}
+
+  using StmtMutator::operator();
+
+  /*!
+   * \brief Create the loop nest on top of the block, induced by the given block var's domain
+   * \param insert_position The position among the subtrees where the block and its induced loop
+   * nest is inserted
+   * \param iter_doms The domain of each block var
+   * \param preserve_unit_loops Whether to generate unit loops where the loop extent is 1
+   */
+  void MakeNewLoop(int insert_position, std::vector<Range> iter_doms, bool preserve_unit_loops) {
+    int n_iters = iter_doms.size();
+    Array<Var> loop_vars;
+    Array<PrimExpr> loop_extents;
+    Array<PrimExpr> iter_values;
+    loop_vars.reserve(n_iters);
+    loop_extents.reserve(n_iters);
+    iter_values.reserve(n_iters);
+    for (int i = 0; i < n_iters; ++i) {
+      const Range& iter_dom = iter_doms[i];
+      if (preserve_unit_loops || !is_one(iter_dom->extent)) {
+        Var var("ax" + std::to_string(loop_vars.size()), DataType::Int(32));
+        loop_vars.push_back(var);
+        loop_extents.push_back(iter_dom->extent);
+        iter_values.push_back(iter_dom->min + var);
+      } else {
+        iter_values.push_back(iter_dom->min);
+      }
+    }
+    this->new_block_realize_ =
+        BlockRealize(std::move(iter_values), const_true(), std::move(block_));
+    Stmt new_subtree = this->new_block_realize_;
+    for (int i = static_cast<int>(loop_vars.size()) - 1; i >= 0; --i) {
+      const Var& loop_var = loop_vars[i];
+      const PrimExpr& loop_extent = loop_extents[i];
+      new_subtree = For(/*loop_var=*/loop_var,
+                        /*min=*/Integer(0),
+                        /*extent=*/loop_extent,
+                        /*ForKind=*/ForKind::kSerial,
+                        /*body=*/std::move(new_subtree));
+    }
+    Array<Stmt> subtrees = AsArray(loop_->body);
+    subtrees.insert(subtrees.begin() + insert_position, std::move(new_subtree));
+    ObjectPtr<ForNode> new_loop = make_object<ForNode>(*loop_.get());
+    new_loop->body = SeqStmt(std::move(subtrees));
+    this->new_loop_ = For(std::move(new_loop));
+  }
+
+ private:
+  Stmt VisitStmt_(const BlockNode* block) final {
+    if (block != scope_root_.get()) {
+      return GetRef<Block>(block);
+    }
+    if (block == rm_src_stmt_.get()) {
+      block = TVM_TYPE_AS(block, rm_tgt_stmt_, BlockNode);
+    }
+    return StmtMutator::VisitStmt_(block);
+  }
+
+  Stmt VisitStmt_(const ForNode* loop) final {
+    if (loop == rm_src_stmt_.get()) {
+      loop = TVM_TYPE_AS(loop, rm_tgt_stmt_, ForNode);
+    }
+    if (loop == loop_.get()) {
+      return new_loop_;
+    }
+    return StmtMutator::VisitStmt_(loop);
+  }
+
+ public:
+  /*! \brief The root block of the block scope */
+  Block scope_root_;
+  /*! \brief The given block to be moved */
+  Block block_;
+  /*! \brief The given loop the block and its loop nest to be put under */
+  For loop_;
+  /*! \brief The new loop to replace the original loop */
+  For new_loop_{nullptr};
+  /*! \brief The new block realize to the moved block */
+  BlockRealize new_block_realize_{nullptr};
+  /*! \brief The plan to remove the given block by replacing this loop/block in the AST */
+  Stmt rm_src_stmt_{nullptr};
+  /*! \brief The plan to remove the given block by replacing to this loop/block in the AST */
+  Stmt rm_tgt_stmt_{nullptr};
+};
+
+/*!
+ * \brief Calculate a list of accessed buffer regions under a path of loops
+ * \tparam relax_storage_scope Whether to relax beyond the path according to the storage and
+ * execution scope

Review comment:
       I had a long conversation with @spectrometerHBH on this. Actually only the producer needs to be relaxed in TensorIR design




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



[GitHub] [tvm] junrushao1994 commented on pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#issuecomment-915540392


   @MasterJH5574 Would you mind taking another look? Also I didn't find the docs you mention that I didn't change, so it would be great if you can just use "suggest" on those lines you want me to rephrase - would be much easier for me.


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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r703896891



##########
File path: src/support/nd_int_set.h
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.
+ */
+#ifndef TVM_SUPPORT_ND_INT_SET_H_
+#define TVM_SUPPORT_ND_INT_SET_H_
+
+#include <tvm/arith/int_set.h>
+#include <tvm/ir/expr.h>
+
+#include <unordered_map>
+#include <vector>
+
+namespace tvm {
+namespace support {
+
+/*! \brief An N-dimensional integer set representing a rectangle region */
+using NDIntSet = std::vector<tvm::arith::IntSet>;
+
+/*!
+ * \brief Construct an N-dimensional integer set representing a region.
+ * \param region The region.
+ * \return constructed set.
+ */
+inline NDIntSet NDIntSetFromRegion(const tir::Region& region) {
+  NDIntSet result;
+  result.reserve(region.size());
+  for (const Range& range : region) {
+    result.push_back(arith::IntSet::FromRange(range));
+  }
+  return result;
+}
+
+/*!
+ * \brief Construct an N-dimensional integer set representing a shape.
+ * \param shape The shape which is an array of the length of each dimension.
+ * \return constructed set.
+ */
+inline NDIntSet NDIntSetFromShape(const Array<PrimExpr>& shape) {
+  PrimExpr zero = Integer(0);

Review comment:
       There won't be too much difference though but it does help a little bit. On the other hand, incrementing atomic ref counter in more than 1 thread could cause a performance issue on CPUs, so that's why I didn't do any further immature optimization




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704047938



##########
File path: include/tvm/tir/schedule/schedule.h
##########
@@ -305,6 +305,38 @@ class ScheduleNode : public runtime::Object {
   virtual BlockRV CacheWrite(const BlockRV& block_rv, int write_buffer_index,
                              const String& storage_scope) = 0;
   /******** Schedule: Compute location ********/
+  /*!
+   * \brief Move a producer block under the specific loop, and regenerate the loops induced by the
+   * block so that the buffer region generated by the producer block could cover those regions read
+   * by the consumers. It requires:
+   * 1) The scope block has stage-pipeline property
+   * 2) The given block's subtree of the scope block satisfies compact dataflow condition.
+   * i.e. all the blocks in the scope's subtree must be either complete block or reduction block

Review comment:
       probably it should be rephrased. The subtree of the scope block, which the given block is in, should satisfy the compact dataflow property




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



[GitHub] [tvm] MasterJH5574 edited a comment on pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
MasterJH5574 edited a comment on pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#issuecomment-915860412


   There's another issue that I want @junrushao1994 to take a look at. I've informed him privately. So we may wait for his response first.


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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704131630



##########
File path: src/tir/schedule/primitive/cache_read_write.cc
##########
@@ -160,6 +160,21 @@ Block MakeCacheStage(const BufferRegion& cache_region, CacheStageInfo* info,
   return block;
 }
 
+/*!
+ * \brief Recalculate the `affine_binding` flag of a specifc block
+ * \param block_sref The sref to the specific block
+ */
+bool CalculateAffineFlag(const ScheduleState& self, const StmtSRef& block_sref) {

Review comment:
       Let's do it in the decompose reduction PR




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704730743



##########
File path: src/tir/schedule/primitive/compute_at.cc
##########
@@ -0,0 +1,584 @@
+/*
+ * 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 {
+
+using support::NDIntSet;
+
+/******** Error Classes ********/
+
+/*!
+ * \brief An error raised when not all required blocks are under the given loop.
+ * \tparam is_consumer Indicates if all the required blocks are consumers or producers
+ */
+template <bool is_consumer>
+class NotAllRequiredBlocksAreVisitedError : public ScheduleError {
+ public:
+  explicit NotAllRequiredBlocksAreVisitedError(IRModule mod, int num_not_visited,
+                                               const Array<StmtSRef>& required)
+      : mod_(mod), num_not_visited_(num_not_visited) {
+    required_.reserve(required.size());
+    for (const StmtSRef& block_sref : required) {
+      const BlockNode* block = TVM_SREF_TO_BLOCK(block, block_sref);
+      required_.push_back(GetRef<Block>(block));
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Not all required blocks are under the loop scope";
+  }
+
+  String DetailRenderTemplate() const final {
+    String relation = is_consumer ? "consumer(s)" : "producer(s)";
+    std::ostringstream os;
+    os << "The primitive requires all the " << relation
+       << " of the given block to be present under the target loop. However, there are "
+       << num_not_visited_ << " " << relation << " not satisfying the constraint. List of the "
+       << relation << ":";
+    for (int i = 0, n = required_.size(); i < n; ++i) {
+      os << "{" << i << "}";
+    }
+    return os.str();
+  }
+
+  IRModule mod() const final { return mod_; }
+
+  Array<ObjectRef> LocationsOfInterest() const final {
+    return {required_.begin(), required_.end()};
+  }
+
+ private:
+  IRModule mod_;
+  int num_not_visited_;
+  Array<Block> required_;
+};
+
+/*!
+ * \brief An error raised when the given block is not in the same block scope as the given loop,
+ * or the given loop is the ancestor of the given block.
+ */
+class NotInSameScopeError : public ScheduleError {
+ public:
+  static void CheckAndBindLoopDomain(const ScheduleState& self, const StmtSRef& block_sref,
+                                     const StmtSRef& loop_sref, const StmtSRef& scope_root_sref,
+                                     arith::Analyzer* analyzer) {
+    for (const StmtSRefNode* p = loop_sref.get();; p = p->parent) {
+      if (const ForNode* loop = p->StmtAs<ForNode>()) {
+        analyzer->Bind(loop->loop_var, Range::FromMinExtent(loop->min, loop->extent));
+      } else if (p != scope_root_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      } else {
+        break;
+      }
+    }
+    for (const StmtSRefNode* p = block_sref->parent; p != scope_root_sref.get(); p = p->parent) {
+      if (p == loop_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      }
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Expected the block and loop to be under the same block scope, and loop "
+           "not to be the ancestor of block";
+  }
+  String DetailRenderTemplate() const final {
+    return "ScheduleError: Expected the block {0} and loop {1} to be under the same block scope, "
+           "and loop not to be the ancestor of block";
+  }
+  IRModule mod() const final { return mod_; }
+  Array<ObjectRef> LocationsOfInterest() const final { return {block_, loop_}; }
+
+ private:
+  explicit NotInSameScopeError(IRModule mod, const StmtSRef& block_sref, const StmtSRef& loop_sref)
+      : mod_(mod),
+        block_(GetRef<Block>(block_sref->StmtAs<BlockNode>())),
+        loop_(GetRef<For>(loop_sref->StmtAs<ForNode>())) {}
+
+  IRModule mod_;
+  Block block_;
+  For loop_;
+};
+
+/******** Helper Functions/Classes ********/
+
+/*!
+ * \brief Find a point where the block can be inserted under the loop
+ * \tparam require_all_producers_visited Requires all producer blocks to be present under the loop
+ * \tparam require_all_consumers_visited Requires all consumer blocks to be present under the loop
+ * \param self The schedule state
+ * \param subtrees The subtrees under the loop, among which the insertion points are sought
+ * \param producer_srefs The producer blocks
+ * \param consumer_srefs The consumer blocks
+ * \param block2realize A cache that maps a block to its realize
+ * \return The last position the new block can be inserted onto, and the
+ * producer-consumer-relationship is still satisfied.
+ */
+template <bool require_all_producers_visited, bool require_all_consumers_visited>
+int FindInsertionPoint(
+    const ScheduleState& self, const Array<Stmt>& subtrees, const Array<StmtSRef>& producer_srefs,
+    const Array<StmtSRef>& consumer_srefs,
+    std::unordered_map<const BlockNode*, const BlockRealizeNode*>* block2realize) {
+  ProducerConsumerSplit split =
+      ProducerConsumerSplit::Find(self, subtrees, producer_srefs, consumer_srefs, block2realize);
+  // Step 1. Check if all the producers are visited in the subtrees, if required to
+  if (require_all_producers_visited) {
+    int num_producers = producer_srefs.size();
+    if (split.n_producers_visited < num_producers) {
+      throw NotAllRequiredBlocksAreVisitedError<false>(
+          self->mod, num_producers - split.n_producers_visited, producer_srefs);
+    }
+  }
+  // Step 2. Check if all the consumers are visited in the subtrees, if required to
+  if (require_all_consumers_visited) {
+    int num_consumers = consumer_srefs.size();
+    if (split.n_consumers_visited < num_consumers) {
+      throw NotAllRequiredBlocksAreVisitedError<true>(
+          self->mod, num_consumers - split.n_consumers_visited, consumer_srefs);
+    }
+  }
+  // Step 3. Check if there is at least one index of the position can be inserted into
+  // The valid indices are: (last_producer_position, first_consumer_position]
+  ICHECK(split.last_producer_position < split.first_consumer_position);
+  // Step 4. Return the last valid insertion point
+  return split.first_consumer_position;
+}
+
+/*!
+ * \brief A helper to reconstruct the block scope where the given block is moved under the given
+ * loop, and the given block's induced loop nest is regenerated to satisfy the required region.
+ */
+class ScopeReconstructor : private StmtMutator {
+ public:
+  explicit ScopeReconstructor(Block scope_root, Block block, For loop)
+      : scope_root_(scope_root), block_(block), loop_(loop) {}
+
+  using StmtMutator::operator();

Review comment:
       it means making this operator public, given it's private inheritance




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



[GitHub] [tvm] MasterJH5574 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
MasterJH5574 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704401779



##########
File path: src/tir/schedule/utils.h
##########
@@ -163,6 +165,15 @@ inline Stmt RemoveFromSeqStmt(const SeqStmt& seq, const Stmt& to_remove) {
   return SeqStmt::Flatten(new_stmts);
 }
 
+inline Array<Stmt> AsArray(const Stmt& stmt) {

Review comment:
       Would be better if we can briefly document the helper, though it's simple enough.

##########
File path: src/tir/schedule/primitive/compute_at.cc
##########
@@ -0,0 +1,584 @@
+/*
+ * 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 {
+
+using support::NDIntSet;
+
+/******** Error Classes ********/
+
+/*!
+ * \brief An error raised when not all required blocks are under the given loop.
+ * \tparam is_consumer Indicates if all the required blocks are consumers or producers
+ */
+template <bool is_consumer>
+class NotAllRequiredBlocksAreVisitedError : public ScheduleError {
+ public:
+  explicit NotAllRequiredBlocksAreVisitedError(IRModule mod, int num_not_visited,
+                                               const Array<StmtSRef>& required)
+      : mod_(mod), num_not_visited_(num_not_visited) {
+    required_.reserve(required.size());
+    for (const StmtSRef& block_sref : required) {
+      const BlockNode* block = TVM_SREF_TO_BLOCK(block, block_sref);
+      required_.push_back(GetRef<Block>(block));
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Not all required blocks are under the loop scope";
+  }
+
+  String DetailRenderTemplate() const final {
+    String relation = is_consumer ? "consumer(s)" : "producer(s)";
+    std::ostringstream os;
+    os << "The primitive requires all the " << relation
+       << " of the given block to be present under the target loop. However, there are "
+       << num_not_visited_ << " " << relation << " not satisfying the constraint. List of the "
+       << relation << ":";
+    for (int i = 0, n = required_.size(); i < n; ++i) {
+      os << "{" << i << "}";
+    }
+    return os.str();
+  }
+
+  IRModule mod() const final { return mod_; }
+
+  Array<ObjectRef> LocationsOfInterest() const final {
+    return {required_.begin(), required_.end()};
+  }
+
+ private:
+  IRModule mod_;
+  int num_not_visited_;
+  Array<Block> required_;
+};
+
+/*!
+ * \brief An error raised when the given block is not in the same block scope as the given loop,
+ * or the given loop is the ancestor of the given block.
+ */
+class NotInSameScopeError : public ScheduleError {
+ public:
+  static void CheckAndBindLoopDomain(const ScheduleState& self, const StmtSRef& block_sref,
+                                     const StmtSRef& loop_sref, const StmtSRef& scope_root_sref,
+                                     arith::Analyzer* analyzer) {
+    for (const StmtSRefNode* p = loop_sref.get();; p = p->parent) {
+      if (const ForNode* loop = p->StmtAs<ForNode>()) {
+        analyzer->Bind(loop->loop_var, Range::FromMinExtent(loop->min, loop->extent));
+      } else if (p != scope_root_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      } else {
+        break;
+      }
+    }
+    for (const StmtSRefNode* p = block_sref->parent; p != scope_root_sref.get(); p = p->parent) {
+      if (p == loop_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      }
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Expected the block and loop to be under the same block scope, and loop "
+           "not to be the ancestor of block";
+  }
+  String DetailRenderTemplate() const final {
+    return "ScheduleError: Expected the block {0} and loop {1} to be under the same block scope, "
+           "and loop not to be the ancestor of block";
+  }
+  IRModule mod() const final { return mod_; }
+  Array<ObjectRef> LocationsOfInterest() const final { return {block_, loop_}; }
+
+ private:
+  explicit NotInSameScopeError(IRModule mod, const StmtSRef& block_sref, const StmtSRef& loop_sref)
+      : mod_(mod),
+        block_(GetRef<Block>(block_sref->StmtAs<BlockNode>())),
+        loop_(GetRef<For>(loop_sref->StmtAs<ForNode>())) {}
+
+  IRModule mod_;
+  Block block_;
+  For loop_;
+};
+
+/******** Helper Functions/Classes ********/
+
+/*!
+ * \brief Find a point where the block can be inserted under the loop
+ * \tparam require_all_producers_visited Requires all producer blocks to be present under the loop
+ * \tparam require_all_consumers_visited Requires all consumer blocks to be present under the loop
+ * \param self The schedule state
+ * \param subtrees The subtrees under the loop, among which the insertion points are sought
+ * \param producer_srefs The producer blocks
+ * \param consumer_srefs The consumer blocks
+ * \param block2realize A cache that maps a block to its realize
+ * \return The last position the new block can be inserted onto, and the
+ * producer-consumer-relationship is still satisfied.
+ */
+template <bool require_all_producers_visited, bool require_all_consumers_visited>
+int FindInsertionPoint(
+    const ScheduleState& self, const Array<Stmt>& subtrees, const Array<StmtSRef>& producer_srefs,
+    const Array<StmtSRef>& consumer_srefs,
+    std::unordered_map<const BlockNode*, const BlockRealizeNode*>* block2realize) {
+  ProducerConsumerSplit split =
+      ProducerConsumerSplit::Find(self, subtrees, producer_srefs, consumer_srefs, block2realize);
+  // Step 1. Check if all the producers are visited in the subtrees, if required to
+  if (require_all_producers_visited) {
+    int num_producers = producer_srefs.size();
+    if (split.n_producers_visited < num_producers) {
+      throw NotAllRequiredBlocksAreVisitedError<false>(
+          self->mod, num_producers - split.n_producers_visited, producer_srefs);
+    }
+  }
+  // Step 2. Check if all the consumers are visited in the subtrees, if required to
+  if (require_all_consumers_visited) {
+    int num_consumers = consumer_srefs.size();
+    if (split.n_consumers_visited < num_consumers) {
+      throw NotAllRequiredBlocksAreVisitedError<true>(
+          self->mod, num_consumers - split.n_consumers_visited, consumer_srefs);
+    }
+  }
+  // Step 3. Check if there is at least one index of the position can be inserted into
+  // The valid indices are: (last_producer_position, first_consumer_position]
+  ICHECK(split.last_producer_position < split.first_consumer_position);
+  // Step 4. Return the last valid insertion point
+  return split.first_consumer_position;
+}
+
+/*!
+ * \brief A helper to reconstruct the block scope where the given block is moved under the given
+ * loop, and the given block's induced loop nest is regenerated to satisfy the required region.
+ */
+class ScopeReconstructor : private StmtMutator {
+ public:
+  explicit ScopeReconstructor(Block scope_root, Block block, For loop)
+      : scope_root_(scope_root), block_(block), loop_(loop) {}
+
+  using StmtMutator::operator();
+
+  /*!
+   * \brief Create the loop nest on top of the block, induced by the given block var's domain
+   * \param insert_position The position among the subtrees where the block and its induced loop
+   * nest is inserted
+   * \param iter_doms The domain of each block var
+   * \param preserve_unit_loops Whether to generate unit loops where the loop extent is 1
+   */
+  void MakeNewLoop(int insert_position, std::vector<Range> iter_doms, bool preserve_unit_loops) {
+    int n_iters = iter_doms.size();
+    Array<Var> loop_vars;
+    Array<PrimExpr> loop_extents;
+    Array<PrimExpr> iter_values;
+    loop_vars.reserve(n_iters);
+    loop_extents.reserve(n_iters);
+    iter_values.reserve(n_iters);
+    for (int i = 0; i < n_iters; ++i) {
+      const Range& iter_dom = iter_doms[i];
+      if (preserve_unit_loops || !is_one(iter_dom->extent)) {
+        Var var("ax" + std::to_string(loop_vars.size()), DataType::Int(32));
+        loop_vars.push_back(var);
+        loop_extents.push_back(iter_dom->extent);
+        iter_values.push_back(iter_dom->min + var);
+      } else {
+        iter_values.push_back(iter_dom->min);
+      }
+    }
+    this->new_block_realize_ =
+        BlockRealize(std::move(iter_values), const_true(), std::move(block_));
+    Stmt new_subtree = this->new_block_realize_;
+    for (int i = static_cast<int>(loop_vars.size()) - 1; i >= 0; --i) {
+      const Var& loop_var = loop_vars[i];
+      const PrimExpr& loop_extent = loop_extents[i];
+      new_subtree = For(/*loop_var=*/loop_var,
+                        /*min=*/Integer(0),
+                        /*extent=*/loop_extent,
+                        /*ForKind=*/ForKind::kSerial,
+                        /*body=*/std::move(new_subtree));
+    }
+    Array<Stmt> subtrees = AsArray(loop_->body);
+    subtrees.insert(subtrees.begin() + insert_position, std::move(new_subtree));
+    ObjectPtr<ForNode> new_loop = make_object<ForNode>(*loop_.get());
+    new_loop->body = SeqStmt(std::move(subtrees));
+    this->new_loop_ = For(std::move(new_loop));
+  }
+
+ private:
+  Stmt VisitStmt_(const BlockNode* block) final {
+    if (block != scope_root_.get()) {
+      return GetRef<Block>(block);
+    }
+    if (block == rm_src_stmt_.get()) {
+      block = TVM_TYPE_AS(block, rm_tgt_stmt_, BlockNode);
+    }
+    return StmtMutator::VisitStmt_(block);
+  }
+
+  Stmt VisitStmt_(const ForNode* loop) final {
+    if (loop == rm_src_stmt_.get()) {
+      loop = TVM_TYPE_AS(loop, rm_tgt_stmt_, ForNode);
+    }
+    if (loop == loop_.get()) {
+      return new_loop_;
+    }
+    return StmtMutator::VisitStmt_(loop);
+  }
+
+ public:
+  /*! \brief The root block of the block scope */
+  Block scope_root_;
+  /*! \brief The given block to be moved */
+  Block block_;
+  /*! \brief The given loop the block and its loop nest to be put under */
+  For loop_;
+  /*! \brief The new loop to replace the original loop */
+  For new_loop_{nullptr};
+  /*! \brief The new block realize to the moved block */
+  BlockRealize new_block_realize_{nullptr};
+  /*! \brief The plan to remove the given block by replacing this loop/block in the AST */
+  Stmt rm_src_stmt_{nullptr};
+  /*! \brief The plan to remove the given block by replacing to this loop/block in the AST */
+  Stmt rm_tgt_stmt_{nullptr};
+};
+
+/*!
+ * \brief Calculate a list of accessed buffer regions under a path of loops
+ * \tparam relax_storage_scope Whether to relax beyond the path according to the storage and
+ * execution scope
+ * \param binding The block binding, used to unbind the buffer regions
+ * \param buffer_regions The buffer regions to be calculated
+ * \param relax_path_low_inclusive The lowest point in the loop path, inclusive
+ * \param relax_path_high_exclusive The highest point in the loop path, exclusive
+ * \param relaxed Where the calculation result is stored
+ */
+template <bool relax_storage_scope>
+void RelaxBufferRegions(const Map<Var, PrimExpr>& binding,
+                        const Array<BufferRegion>& buffer_regions,
+                        const StmtSRef& relax_path_low_inclusive,
+                        const StmtSRef& relax_path_high_exclusive,
+                        std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* relaxed) {
+  runtime::StorageScope global_scope{runtime::StorageRank::kGlobal, ""};
+  // We cache the variable domains
+  runtime::StorageRank previous_rank = runtime::StorageRank::kGlobal;
+  Optional<Map<Var, arith::IntSet>> var_dom = NullOpt;
+  // Enumerate every buffer region
+  for (const BufferRegion& buffer_region : buffer_regions) {
+    const Buffer& buffer = buffer_region->buffer;
+    const Array<Range>& region = buffer_region->region;
+    // Skip the buffer regions we are not interested in
+    auto it = relaxed->find(buffer.get());
+    if (it == relaxed->end()) {
+      continue;
+    }
+    std::vector<NDIntSet>& relaxed_regions = it->second;
+    // Check and update the cached `var_dom`
+    runtime::StorageScope scope =
+        relax_storage_scope ? runtime::StorageScope::Create(buffer.scope()) : global_scope;
+    runtime::StorageRank rank = scope.rank;
+    if (rank != previous_rank || !var_dom.defined()) {
+      previous_rank = rank;
+      var_dom = AsIntSet(LoopDomainOfSRefTreePath(
+          /*low_inclusive=*/relax_path_low_inclusive,
+          /*high_exclusive=*/relax_path_high_exclusive,
+          /*extra_relax_scope=*/scope));
+    }
+    // Relax the region
+    Array<arith::IntSet> relaxed = arith::EvalSet(Substitute(region, binding), var_dom.value());
+    relaxed_regions.push_back({relaxed.begin(), relaxed.end()});
+  }
+}
+
+/*!
+ * \brief Calculate the iteration domain of a provided integer set to fully cover the required
+ * domain
+ * \param provided The provided integer set to cover the required domain
+ * \param required The required domain to be covered
+ * \param iter_doms The result iteration domains to be updated
+ * \param analyzer The arithmetic analyzer
+ */
+void UpdateBlockVarDomain(const arith::IntSet& provided, const arith::IntSet& required,
+                          std::unordered_map<const VarNode*, std::vector<arith::IntSet>>* iter_doms,
+                          arith::Analyzer* analyzer) {
+  PrimExpr provided_min = analyzer->Simplify(provided.min());
+  PrimExpr provided_extent = analyzer->Simplify(provided.max() - provided_min + 1);
+  PrimExpr required_min = analyzer->Simplify(required.min());
+  PrimExpr required_extent = analyzer->Simplify(required.max() - required_min + 1);
+  PrimExpr dom_min{nullptr}, dom_extent{nullptr};
+  Var dom_var{ObjectPtr<VarNode>{nullptr}};
+  arith::PVar<Var> p_v;
+  arith::PVar<PrimExpr> p_e;
+  if ((p_v * p_e).Match(provided_min) || (p_e * p_v).Match(provided_min)) {
+    PrimExpr e = p_e.Eval();
+    dom_var = p_v.Eval();
+    dom_min = floordiv(required_min, e);
+    dom_extent = analyzer->Simplify((required_extent + e - 1) / e);
+  } else if (analyzer->CanProveEqual(provided_extent, 1) && p_v.Match(provided_min)) {
+    dom_var = p_v.Eval();
+    dom_min = required_min;
+    dom_extent = required_extent;
+  } else {
+    ICHECK(false) << "ValueError: BufferRegion pattern match failed";
+  }
+  auto it = iter_doms->find(dom_var.get());
+  if (it != iter_doms->end()) {
+    std::vector<arith::IntSet>& doms = it->second;
+    doms.push_back(arith::IntSet::FromMinExtent(dom_min, dom_extent));
+  } else {
+    ICHECK(analyzer->CanProveEqual(provided_min, required_min));
+    ICHECK(analyzer->CanProveEqual(provided_extent, required_extent));
+  }
+}
+
+/*!
+ * \brief Calculate the domain of block vars to cover the required region
+ * \param iter_vars The list of block vars to cover the required region
+ * \param provided_regions The region provided by one iteration instance of the block vars
+ * \param required_regions The region required to be covered
+ * \param analyzer The arithmetic analyzer
+ * \return A list of iteration domain corresponding to the given list of block vars
+ */
+std::vector<Range> CalculateBlockVarDomain(
+    const Array<IterVar>& iter_vars,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>> provided_regions,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>> required_regions,
+    arith::Analyzer* analyzer) {
+  int n_iters = iter_vars.size();
+  // Step 1. Construct the mapping from block var to their iteration domain (initialized to empty)
+  std::unordered_map<const VarNode*, std::vector<arith::IntSet>> iter_doms;
+  iter_doms.reserve(n_iters);
+  for (const IterVar& iter_var : iter_vars) {
+    iter_doms[iter_var->var.get()] = {};
+  }
+  // Step 2. For each buffer, update the domain according to the provided and required regions
+  for (const auto& kv : provided_regions) {
+    const BufferNode* buffer = kv.first;
+    const std::vector<NDIntSet>& many_provided_regions = kv.second;
+    // Calculate `provided_region` and `required_region`
+    auto it = required_regions.find(buffer);
+    if (it == required_regions.end() || it->second.empty()) {
+      continue;
+    }
+    NDIntSet required_region = support::NDIntSetUnion(it->second);
+    NDIntSet provided_region = support::NDIntSetUnion(many_provided_regions);
+    ICHECK_EQ(provided_region.size(), buffer->shape.size());
+    ICHECK_EQ(required_region.size(), buffer->shape.size());
+    // For each dimension, update the iteration domain
+    int ndim = buffer->shape.size();
+    for (int i = 0; i < ndim; ++i) {
+      arith::IntSet provided = provided_region[i];
+      arith::IntSet required = required_region[i];
+      required = arith::Intersect(
+          {std::move(required), arith::IntSet::FromMinExtent(Integer(0), buffer->shape[i])});
+      UpdateBlockVarDomain(provided, required, &iter_doms, analyzer);
+    }
+  }
+  // Union the iter var domains, put them in the same order of block vars, and return
+  std::vector<Range> result;
+  result.reserve(n_iters);
+  for (const IterVar& iter_var : iter_vars) {
+    const std::vector<arith::IntSet>& doms = iter_doms.at(iter_var->var.get());
+    arith::IntSet dom = arith::IntSet::FromRange(iter_var->dom);
+    if (!doms.empty()) {
+      dom = arith::Intersect({std::move(dom), arith::Union(doms)});
+    }
+    PrimExpr min = analyzer->Simplify(dom.min());
+    PrimExpr extent = analyzer->Simplify(dom.max() - min + 1);
+    result.push_back(Range::FromMinExtent(min, extent));
+  }
+  return result;
+}
+
+/*!
+ * \brief Calculate the provided region of the given block by one single of its execution instance,
+ * as well as the required buffer regions relaxed to the given loop
+ * \tparam is_compute_at Indicates if the operation is compute-at or reverse-compute-at
+ * \param block The given block that provides buffer regions
+ * \param loop_sref The given loop under which the block is going to be moved to
+ * \param block2realize Maps a block to its corresponding BlockRealize
+ * \param producer_srefs The producers of the given block
+ * \param consumer_srefs The consumers of the given block
+ * \param provided_regions The calculated regions provided by the block
+ * \param required_regions The calculated regions required by its consumers (in compute-at) or
+ * producers (in reverse-compute-at)
+ */
+template <bool is_compute_at>
+void CalculateProvidedRequiredRegions(
+    const BlockNode* block, const StmtSRef& loop_sref,
+    std::unordered_map<const BlockNode*, const BlockRealizeNode*> block2realize,
+    Array<StmtSRef> producer_srefs, Array<StmtSRef> consumer_srefs,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* provided_regions,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* required_regions) {
+  // Step 1. Calculate the region provided by a single execution instance of `block`
+  const Array<BufferRegion>& provided_buffers = is_compute_at ? block->writes : block->reads;
+  provided_regions->reserve(provided_buffers.size());
+  required_regions->reserve(provided_buffers.size());
+  for (const BufferRegion& provided_buffer_region : provided_buffers) {
+    const BufferNode* buffer = provided_buffer_region->buffer.get();
+    const Array<Range>& region = provided_buffer_region->region;
+    (*provided_regions)[buffer].push_back(support::NDIntSetFromRegion(region));
+    (*required_regions)[buffer].clear();

Review comment:
       What is `(*required_regions)[buffer].clear()` doing? `required_regions` is supposed to be empty at the beginning.

##########
File path: src/tir/schedule/primitive/compute_at.cc
##########
@@ -0,0 +1,584 @@
+/*
+ * 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 {
+
+using support::NDIntSet;
+
+/******** Error Classes ********/
+
+/*!
+ * \brief An error raised when not all required blocks are under the given loop.
+ * \tparam is_consumer Indicates if all the required blocks are consumers or producers
+ */
+template <bool is_consumer>
+class NotAllRequiredBlocksAreVisitedError : public ScheduleError {
+ public:
+  explicit NotAllRequiredBlocksAreVisitedError(IRModule mod, int num_not_visited,
+                                               const Array<StmtSRef>& required)
+      : mod_(mod), num_not_visited_(num_not_visited) {
+    required_.reserve(required.size());
+    for (const StmtSRef& block_sref : required) {
+      const BlockNode* block = TVM_SREF_TO_BLOCK(block, block_sref);
+      required_.push_back(GetRef<Block>(block));
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Not all required blocks are under the loop scope";
+  }
+
+  String DetailRenderTemplate() const final {
+    String relation = is_consumer ? "consumer(s)" : "producer(s)";
+    std::ostringstream os;
+    os << "The primitive requires all the " << relation
+       << " of the given block to be present under the target loop. However, there are "
+       << num_not_visited_ << " " << relation << " not satisfying the constraint. List of the "
+       << relation << ":";
+    for (int i = 0, n = required_.size(); i < n; ++i) {
+      os << "{" << i << "}";
+    }
+    return os.str();
+  }
+
+  IRModule mod() const final { return mod_; }
+
+  Array<ObjectRef> LocationsOfInterest() const final {
+    return {required_.begin(), required_.end()};
+  }
+
+ private:
+  IRModule mod_;
+  int num_not_visited_;
+  Array<Block> required_;
+};
+
+/*!
+ * \brief An error raised when the given block is not in the same block scope as the given loop,
+ * or the given loop is the ancestor of the given block.
+ */
+class NotInSameScopeError : public ScheduleError {
+ public:
+  static void CheckAndBindLoopDomain(const ScheduleState& self, const StmtSRef& block_sref,
+                                     const StmtSRef& loop_sref, const StmtSRef& scope_root_sref,
+                                     arith::Analyzer* analyzer) {
+    for (const StmtSRefNode* p = loop_sref.get();; p = p->parent) {
+      if (const ForNode* loop = p->StmtAs<ForNode>()) {
+        analyzer->Bind(loop->loop_var, Range::FromMinExtent(loop->min, loop->extent));
+      } else if (p != scope_root_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      } else {
+        break;
+      }
+    }
+    for (const StmtSRefNode* p = block_sref->parent; p != scope_root_sref.get(); p = p->parent) {
+      if (p == loop_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      }
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Expected the block and loop to be under the same block scope, and loop "
+           "not to be the ancestor of block";
+  }
+  String DetailRenderTemplate() const final {
+    return "ScheduleError: Expected the block {0} and loop {1} to be under the same block scope, "
+           "and loop not to be the ancestor of block";
+  }
+  IRModule mod() const final { return mod_; }
+  Array<ObjectRef> LocationsOfInterest() const final { return {block_, loop_}; }
+
+ private:
+  explicit NotInSameScopeError(IRModule mod, const StmtSRef& block_sref, const StmtSRef& loop_sref)
+      : mod_(mod),
+        block_(GetRef<Block>(block_sref->StmtAs<BlockNode>())),
+        loop_(GetRef<For>(loop_sref->StmtAs<ForNode>())) {}
+
+  IRModule mod_;
+  Block block_;
+  For loop_;
+};
+
+/******** Helper Functions/Classes ********/
+
+/*!
+ * \brief Find a point where the block can be inserted under the loop
+ * \tparam require_all_producers_visited Requires all producer blocks to be present under the loop
+ * \tparam require_all_consumers_visited Requires all consumer blocks to be present under the loop
+ * \param self The schedule state
+ * \param subtrees The subtrees under the loop, among which the insertion points are sought
+ * \param producer_srefs The producer blocks
+ * \param consumer_srefs The consumer blocks
+ * \param block2realize A cache that maps a block to its realize
+ * \return The last position the new block can be inserted onto, and the
+ * producer-consumer-relationship is still satisfied.
+ */
+template <bool require_all_producers_visited, bool require_all_consumers_visited>
+int FindInsertionPoint(
+    const ScheduleState& self, const Array<Stmt>& subtrees, const Array<StmtSRef>& producer_srefs,
+    const Array<StmtSRef>& consumer_srefs,
+    std::unordered_map<const BlockNode*, const BlockRealizeNode*>* block2realize) {
+  ProducerConsumerSplit split =
+      ProducerConsumerSplit::Find(self, subtrees, producer_srefs, consumer_srefs, block2realize);
+  // Step 1. Check if all the producers are visited in the subtrees, if required to
+  if (require_all_producers_visited) {
+    int num_producers = producer_srefs.size();
+    if (split.n_producers_visited < num_producers) {
+      throw NotAllRequiredBlocksAreVisitedError<false>(
+          self->mod, num_producers - split.n_producers_visited, producer_srefs);
+    }
+  }
+  // Step 2. Check if all the consumers are visited in the subtrees, if required to
+  if (require_all_consumers_visited) {
+    int num_consumers = consumer_srefs.size();
+    if (split.n_consumers_visited < num_consumers) {
+      throw NotAllRequiredBlocksAreVisitedError<true>(
+          self->mod, num_consumers - split.n_consumers_visited, consumer_srefs);
+    }
+  }
+  // Step 3. Check if there is at least one index of the position can be inserted into
+  // The valid indices are: (last_producer_position, first_consumer_position]
+  ICHECK(split.last_producer_position < split.first_consumer_position);
+  // Step 4. Return the last valid insertion point
+  return split.first_consumer_position;
+}
+
+/*!
+ * \brief A helper to reconstruct the block scope where the given block is moved under the given
+ * loop, and the given block's induced loop nest is regenerated to satisfy the required region.
+ */
+class ScopeReconstructor : private StmtMutator {
+ public:
+  explicit ScopeReconstructor(Block scope_root, Block block, For loop)
+      : scope_root_(scope_root), block_(block), loop_(loop) {}
+
+  using StmtMutator::operator();
+
+  /*!
+   * \brief Create the loop nest on top of the block, induced by the given block var's domain
+   * \param insert_position The position among the subtrees where the block and its induced loop
+   * nest is inserted
+   * \param iter_doms The domain of each block var
+   * \param preserve_unit_loops Whether to generate unit loops where the loop extent is 1
+   */
+  void MakeNewLoop(int insert_position, std::vector<Range> iter_doms, bool preserve_unit_loops) {
+    int n_iters = iter_doms.size();
+    Array<Var> loop_vars;
+    Array<PrimExpr> loop_extents;
+    Array<PrimExpr> iter_values;
+    loop_vars.reserve(n_iters);
+    loop_extents.reserve(n_iters);
+    iter_values.reserve(n_iters);
+    for (int i = 0; i < n_iters; ++i) {
+      const Range& iter_dom = iter_doms[i];
+      if (preserve_unit_loops || !is_one(iter_dom->extent)) {
+        Var var("ax" + std::to_string(loop_vars.size()), DataType::Int(32));
+        loop_vars.push_back(var);
+        loop_extents.push_back(iter_dom->extent);
+        iter_values.push_back(iter_dom->min + var);
+      } else {
+        iter_values.push_back(iter_dom->min);
+      }
+    }
+    this->new_block_realize_ =
+        BlockRealize(std::move(iter_values), const_true(), std::move(block_));
+    Stmt new_subtree = this->new_block_realize_;
+    for (int i = static_cast<int>(loop_vars.size()) - 1; i >= 0; --i) {
+      const Var& loop_var = loop_vars[i];
+      const PrimExpr& loop_extent = loop_extents[i];
+      new_subtree = For(/*loop_var=*/loop_var,
+                        /*min=*/Integer(0),
+                        /*extent=*/loop_extent,
+                        /*ForKind=*/ForKind::kSerial,
+                        /*body=*/std::move(new_subtree));
+    }
+    Array<Stmt> subtrees = AsArray(loop_->body);
+    subtrees.insert(subtrees.begin() + insert_position, std::move(new_subtree));
+    ObjectPtr<ForNode> new_loop = make_object<ForNode>(*loop_.get());
+    new_loop->body = SeqStmt(std::move(subtrees));
+    this->new_loop_ = For(std::move(new_loop));
+  }
+
+ private:
+  Stmt VisitStmt_(const BlockNode* block) final {
+    if (block != scope_root_.get()) {
+      return GetRef<Block>(block);
+    }
+    if (block == rm_src_stmt_.get()) {
+      block = TVM_TYPE_AS(block, rm_tgt_stmt_, BlockNode);
+    }
+    return StmtMutator::VisitStmt_(block);
+  }
+
+  Stmt VisitStmt_(const ForNode* loop) final {
+    if (loop == rm_src_stmt_.get()) {
+      loop = TVM_TYPE_AS(loop, rm_tgt_stmt_, ForNode);
+    }
+    if (loop == loop_.get()) {
+      return new_loop_;
+    }
+    return StmtMutator::VisitStmt_(loop);
+  }
+
+ public:
+  /*! \brief The root block of the block scope */
+  Block scope_root_;
+  /*! \brief The given block to be moved */
+  Block block_;
+  /*! \brief The given loop the block and its loop nest to be put under */
+  For loop_;
+  /*! \brief The new loop to replace the original loop */
+  For new_loop_{nullptr};
+  /*! \brief The new block realize to the moved block */
+  BlockRealize new_block_realize_{nullptr};
+  /*! \brief The plan to remove the given block by replacing this loop/block in the AST */
+  Stmt rm_src_stmt_{nullptr};
+  /*! \brief The plan to remove the given block by replacing to this loop/block in the AST */
+  Stmt rm_tgt_stmt_{nullptr};
+};
+
+/*!
+ * \brief Calculate a list of accessed buffer regions under a path of loops
+ * \tparam relax_storage_scope Whether to relax beyond the path according to the storage and
+ * execution scope
+ * \param binding The block binding, used to unbind the buffer regions
+ * \param buffer_regions The buffer regions to be calculated
+ * \param relax_path_low_inclusive The lowest point in the loop path, inclusive
+ * \param relax_path_high_exclusive The highest point in the loop path, exclusive
+ * \param relaxed Where the calculation result is stored
+ */
+template <bool relax_storage_scope>
+void RelaxBufferRegions(const Map<Var, PrimExpr>& binding,
+                        const Array<BufferRegion>& buffer_regions,
+                        const StmtSRef& relax_path_low_inclusive,
+                        const StmtSRef& relax_path_high_exclusive,
+                        std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* relaxed) {
+  runtime::StorageScope global_scope{runtime::StorageRank::kGlobal, ""};
+  // We cache the variable domains
+  runtime::StorageRank previous_rank = runtime::StorageRank::kGlobal;
+  Optional<Map<Var, arith::IntSet>> var_dom = NullOpt;
+  // Enumerate every buffer region
+  for (const BufferRegion& buffer_region : buffer_regions) {
+    const Buffer& buffer = buffer_region->buffer;
+    const Array<Range>& region = buffer_region->region;
+    // Skip the buffer regions we are not interested in
+    auto it = relaxed->find(buffer.get());
+    if (it == relaxed->end()) {
+      continue;
+    }
+    std::vector<NDIntSet>& relaxed_regions = it->second;
+    // Check and update the cached `var_dom`
+    runtime::StorageScope scope =
+        relax_storage_scope ? runtime::StorageScope::Create(buffer.scope()) : global_scope;
+    runtime::StorageRank rank = scope.rank;
+    if (rank != previous_rank || !var_dom.defined()) {
+      previous_rank = rank;
+      var_dom = AsIntSet(LoopDomainOfSRefTreePath(
+          /*low_inclusive=*/relax_path_low_inclusive,
+          /*high_exclusive=*/relax_path_high_exclusive,
+          /*extra_relax_scope=*/scope));
+    }
+    // Relax the region
+    Array<arith::IntSet> relaxed = arith::EvalSet(Substitute(region, binding), var_dom.value());

Review comment:
       The name `relaxed` is duplicated with the parameter name. I think we'd better change a new name. Perhaps it can be `relaxed_region`.

##########
File path: src/tir/schedule/primitive/compute_at.cc
##########
@@ -0,0 +1,584 @@
+/*
+ * 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 {
+
+using support::NDIntSet;
+
+/******** Error Classes ********/
+
+/*!
+ * \brief An error raised when not all required blocks are under the given loop.
+ * \tparam is_consumer Indicates if all the required blocks are consumers or producers
+ */
+template <bool is_consumer>
+class NotAllRequiredBlocksAreVisitedError : public ScheduleError {
+ public:
+  explicit NotAllRequiredBlocksAreVisitedError(IRModule mod, int num_not_visited,
+                                               const Array<StmtSRef>& required)
+      : mod_(mod), num_not_visited_(num_not_visited) {
+    required_.reserve(required.size());
+    for (const StmtSRef& block_sref : required) {
+      const BlockNode* block = TVM_SREF_TO_BLOCK(block, block_sref);
+      required_.push_back(GetRef<Block>(block));
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Not all required blocks are under the loop scope";
+  }
+
+  String DetailRenderTemplate() const final {
+    String relation = is_consumer ? "consumer(s)" : "producer(s)";
+    std::ostringstream os;
+    os << "The primitive requires all the " << relation
+       << " of the given block to be present under the target loop. However, there are "
+       << num_not_visited_ << " " << relation << " not satisfying the constraint. List of the "
+       << relation << ":";
+    for (int i = 0, n = required_.size(); i < n; ++i) {
+      os << "{" << i << "}";
+    }
+    return os.str();
+  }
+
+  IRModule mod() const final { return mod_; }
+
+  Array<ObjectRef> LocationsOfInterest() const final {
+    return {required_.begin(), required_.end()};
+  }
+
+ private:
+  IRModule mod_;
+  int num_not_visited_;
+  Array<Block> required_;
+};
+
+/*!
+ * \brief An error raised when the given block is not in the same block scope as the given loop,
+ * or the given loop is the ancestor of the given block.
+ */
+class NotInSameScopeError : public ScheduleError {
+ public:
+  static void CheckAndBindLoopDomain(const ScheduleState& self, const StmtSRef& block_sref,
+                                     const StmtSRef& loop_sref, const StmtSRef& scope_root_sref,
+                                     arith::Analyzer* analyzer) {
+    for (const StmtSRefNode* p = loop_sref.get();; p = p->parent) {
+      if (const ForNode* loop = p->StmtAs<ForNode>()) {
+        analyzer->Bind(loop->loop_var, Range::FromMinExtent(loop->min, loop->extent));
+      } else if (p != scope_root_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      } else {
+        break;
+      }
+    }
+    for (const StmtSRefNode* p = block_sref->parent; p != scope_root_sref.get(); p = p->parent) {
+      if (p == loop_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      }
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Expected the block and loop to be under the same block scope, and loop "
+           "not to be the ancestor of block";
+  }
+  String DetailRenderTemplate() const final {
+    return "ScheduleError: Expected the block {0} and loop {1} to be under the same block scope, "
+           "and loop not to be the ancestor of block";
+  }
+  IRModule mod() const final { return mod_; }
+  Array<ObjectRef> LocationsOfInterest() const final { return {block_, loop_}; }
+
+ private:
+  explicit NotInSameScopeError(IRModule mod, const StmtSRef& block_sref, const StmtSRef& loop_sref)
+      : mod_(mod),
+        block_(GetRef<Block>(block_sref->StmtAs<BlockNode>())),
+        loop_(GetRef<For>(loop_sref->StmtAs<ForNode>())) {}
+
+  IRModule mod_;
+  Block block_;
+  For loop_;
+};
+
+/******** Helper Functions/Classes ********/
+
+/*!
+ * \brief Find a point where the block can be inserted under the loop
+ * \tparam require_all_producers_visited Requires all producer blocks to be present under the loop
+ * \tparam require_all_consumers_visited Requires all consumer blocks to be present under the loop
+ * \param self The schedule state
+ * \param subtrees The subtrees under the loop, among which the insertion points are sought
+ * \param producer_srefs The producer blocks
+ * \param consumer_srefs The consumer blocks
+ * \param block2realize A cache that maps a block to its realize
+ * \return The last position the new block can be inserted onto, and the
+ * producer-consumer-relationship is still satisfied.
+ */
+template <bool require_all_producers_visited, bool require_all_consumers_visited>
+int FindInsertionPoint(
+    const ScheduleState& self, const Array<Stmt>& subtrees, const Array<StmtSRef>& producer_srefs,
+    const Array<StmtSRef>& consumer_srefs,
+    std::unordered_map<const BlockNode*, const BlockRealizeNode*>* block2realize) {
+  ProducerConsumerSplit split =
+      ProducerConsumerSplit::Find(self, subtrees, producer_srefs, consumer_srefs, block2realize);
+  // Step 1. Check if all the producers are visited in the subtrees, if required to
+  if (require_all_producers_visited) {
+    int num_producers = producer_srefs.size();
+    if (split.n_producers_visited < num_producers) {
+      throw NotAllRequiredBlocksAreVisitedError<false>(
+          self->mod, num_producers - split.n_producers_visited, producer_srefs);
+    }
+  }
+  // Step 2. Check if all the consumers are visited in the subtrees, if required to
+  if (require_all_consumers_visited) {
+    int num_consumers = consumer_srefs.size();
+    if (split.n_consumers_visited < num_consumers) {
+      throw NotAllRequiredBlocksAreVisitedError<true>(
+          self->mod, num_consumers - split.n_consumers_visited, consumer_srefs);
+    }
+  }
+  // Step 3. Check if there is at least one index of the position can be inserted into
+  // The valid indices are: (last_producer_position, first_consumer_position]
+  ICHECK(split.last_producer_position < split.first_consumer_position);
+  // Step 4. Return the last valid insertion point
+  return split.first_consumer_position;
+}
+
+/*!
+ * \brief A helper to reconstruct the block scope where the given block is moved under the given
+ * loop, and the given block's induced loop nest is regenerated to satisfy the required region.
+ */
+class ScopeReconstructor : private StmtMutator {
+ public:
+  explicit ScopeReconstructor(Block scope_root, Block block, For loop)
+      : scope_root_(scope_root), block_(block), loop_(loop) {}
+
+  using StmtMutator::operator();
+
+  /*!
+   * \brief Create the loop nest on top of the block, induced by the given block var's domain
+   * \param insert_position The position among the subtrees where the block and its induced loop
+   * nest is inserted
+   * \param iter_doms The domain of each block var
+   * \param preserve_unit_loops Whether to generate unit loops where the loop extent is 1
+   */
+  void MakeNewLoop(int insert_position, std::vector<Range> iter_doms, bool preserve_unit_loops) {
+    int n_iters = iter_doms.size();
+    Array<Var> loop_vars;
+    Array<PrimExpr> loop_extents;
+    Array<PrimExpr> iter_values;
+    loop_vars.reserve(n_iters);
+    loop_extents.reserve(n_iters);
+    iter_values.reserve(n_iters);
+    for (int i = 0; i < n_iters; ++i) {
+      const Range& iter_dom = iter_doms[i];
+      if (preserve_unit_loops || !is_one(iter_dom->extent)) {
+        Var var("ax" + std::to_string(loop_vars.size()), DataType::Int(32));
+        loop_vars.push_back(var);
+        loop_extents.push_back(iter_dom->extent);
+        iter_values.push_back(iter_dom->min + var);
+      } else {
+        iter_values.push_back(iter_dom->min);
+      }
+    }
+    this->new_block_realize_ =
+        BlockRealize(std::move(iter_values), const_true(), std::move(block_));
+    Stmt new_subtree = this->new_block_realize_;
+    for (int i = static_cast<int>(loop_vars.size()) - 1; i >= 0; --i) {
+      const Var& loop_var = loop_vars[i];
+      const PrimExpr& loop_extent = loop_extents[i];
+      new_subtree = For(/*loop_var=*/loop_var,
+                        /*min=*/Integer(0),
+                        /*extent=*/loop_extent,
+                        /*ForKind=*/ForKind::kSerial,
+                        /*body=*/std::move(new_subtree));
+    }
+    Array<Stmt> subtrees = AsArray(loop_->body);
+    subtrees.insert(subtrees.begin() + insert_position, std::move(new_subtree));
+    ObjectPtr<ForNode> new_loop = make_object<ForNode>(*loop_.get());
+    new_loop->body = SeqStmt(std::move(subtrees));
+    this->new_loop_ = For(std::move(new_loop));
+  }
+
+ private:
+  Stmt VisitStmt_(const BlockNode* block) final {
+    if (block != scope_root_.get()) {
+      return GetRef<Block>(block);
+    }
+    if (block == rm_src_stmt_.get()) {
+      block = TVM_TYPE_AS(block, rm_tgt_stmt_, BlockNode);
+    }
+    return StmtMutator::VisitStmt_(block);
+  }
+
+  Stmt VisitStmt_(const ForNode* loop) final {
+    if (loop == rm_src_stmt_.get()) {
+      loop = TVM_TYPE_AS(loop, rm_tgt_stmt_, ForNode);
+    }
+    if (loop == loop_.get()) {
+      return new_loop_;
+    }
+    return StmtMutator::VisitStmt_(loop);
+  }
+
+ public:
+  /*! \brief The root block of the block scope */
+  Block scope_root_;
+  /*! \brief The given block to be moved */
+  Block block_;
+  /*! \brief The given loop the block and its loop nest to be put under */
+  For loop_;
+  /*! \brief The new loop to replace the original loop */
+  For new_loop_{nullptr};
+  /*! \brief The new block realize to the moved block */
+  BlockRealize new_block_realize_{nullptr};
+  /*! \brief The plan to remove the given block by replacing this loop/block in the AST */
+  Stmt rm_src_stmt_{nullptr};
+  /*! \brief The plan to remove the given block by replacing to this loop/block in the AST */
+  Stmt rm_tgt_stmt_{nullptr};
+};
+
+/*!
+ * \brief Calculate a list of accessed buffer regions under a path of loops
+ * \tparam relax_storage_scope Whether to relax beyond the path according to the storage and
+ * execution scope
+ * \param binding The block binding, used to unbind the buffer regions
+ * \param buffer_regions The buffer regions to be calculated
+ * \param relax_path_low_inclusive The lowest point in the loop path, inclusive
+ * \param relax_path_high_exclusive The highest point in the loop path, exclusive
+ * \param relaxed Where the calculation result is stored
+ */
+template <bool relax_storage_scope>
+void RelaxBufferRegions(const Map<Var, PrimExpr>& binding,
+                        const Array<BufferRegion>& buffer_regions,
+                        const StmtSRef& relax_path_low_inclusive,
+                        const StmtSRef& relax_path_high_exclusive,
+                        std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* relaxed) {
+  runtime::StorageScope global_scope{runtime::StorageRank::kGlobal, ""};
+  // We cache the variable domains
+  runtime::StorageRank previous_rank = runtime::StorageRank::kGlobal;
+  Optional<Map<Var, arith::IntSet>> var_dom = NullOpt;
+  // Enumerate every buffer region
+  for (const BufferRegion& buffer_region : buffer_regions) {
+    const Buffer& buffer = buffer_region->buffer;
+    const Array<Range>& region = buffer_region->region;
+    // Skip the buffer regions we are not interested in
+    auto it = relaxed->find(buffer.get());
+    if (it == relaxed->end()) {
+      continue;
+    }
+    std::vector<NDIntSet>& relaxed_regions = it->second;
+    // Check and update the cached `var_dom`
+    runtime::StorageScope scope =
+        relax_storage_scope ? runtime::StorageScope::Create(buffer.scope()) : global_scope;
+    runtime::StorageRank rank = scope.rank;
+    if (rank != previous_rank || !var_dom.defined()) {
+      previous_rank = rank;
+      var_dom = AsIntSet(LoopDomainOfSRefTreePath(
+          /*low_inclusive=*/relax_path_low_inclusive,
+          /*high_exclusive=*/relax_path_high_exclusive,
+          /*extra_relax_scope=*/scope));
+    }
+    // Relax the region
+    Array<arith::IntSet> relaxed = arith::EvalSet(Substitute(region, binding), var_dom.value());
+    relaxed_regions.push_back({relaxed.begin(), relaxed.end()});
+  }
+}
+
+/*!
+ * \brief Calculate the iteration domain of a provided integer set to fully cover the required
+ * domain
+ * \param provided The provided integer set to cover the required domain
+ * \param required The required domain to be covered
+ * \param iter_doms The result iteration domains to be updated
+ * \param analyzer The arithmetic analyzer
+ */
+void UpdateBlockVarDomain(const arith::IntSet& provided, const arith::IntSet& required,
+                          std::unordered_map<const VarNode*, std::vector<arith::IntSet>>* iter_doms,
+                          arith::Analyzer* analyzer) {
+  PrimExpr provided_min = analyzer->Simplify(provided.min());
+  PrimExpr provided_extent = analyzer->Simplify(provided.max() - provided_min + 1);
+  PrimExpr required_min = analyzer->Simplify(required.min());
+  PrimExpr required_extent = analyzer->Simplify(required.max() - required_min + 1);
+  PrimExpr dom_min{nullptr}, dom_extent{nullptr};
+  Var dom_var{ObjectPtr<VarNode>{nullptr}};
+  arith::PVar<Var> p_v;
+  arith::PVar<PrimExpr> p_e;
+  if ((p_v * p_e).Match(provided_min) || (p_e * p_v).Match(provided_min)) {
+    PrimExpr e = p_e.Eval();
+    dom_var = p_v.Eval();
+    dom_min = floordiv(required_min, e);
+    dom_extent = analyzer->Simplify((required_extent + e - 1) / e);
+  } else if (analyzer->CanProveEqual(provided_extent, 1) && p_v.Match(provided_min)) {
+    dom_var = p_v.Eval();
+    dom_min = required_min;
+    dom_extent = required_extent;
+  } else {
+    ICHECK(false) << "ValueError: BufferRegion pattern match failed";
+  }
+  auto it = iter_doms->find(dom_var.get());
+  if (it != iter_doms->end()) {
+    std::vector<arith::IntSet>& doms = it->second;
+    doms.push_back(arith::IntSet::FromMinExtent(dom_min, dom_extent));
+  } else {
+    ICHECK(analyzer->CanProveEqual(provided_min, required_min));
+    ICHECK(analyzer->CanProveEqual(provided_extent, required_extent));
+  }
+}
+
+/*!
+ * \brief Calculate the domain of block vars to cover the required region
+ * \param iter_vars The list of block vars to cover the required region
+ * \param provided_regions The region provided by one iteration instance of the block vars
+ * \param required_regions The region required to be covered
+ * \param analyzer The arithmetic analyzer

Review comment:
       The `analyzer` was previously updated in `CheckAndBindLoopDomain(..)` via `analyzer->Bind(...)`, so I wonder whether it will be good to tell in the document that what kind of information `analyzer` has.

##########
File path: src/tir/schedule/primitive/compute_at.cc
##########
@@ -0,0 +1,584 @@
+/*
+ * 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 {
+
+using support::NDIntSet;
+
+/******** Error Classes ********/
+
+/*!
+ * \brief An error raised when not all required blocks are under the given loop.
+ * \tparam is_consumer Indicates if all the required blocks are consumers or producers
+ */
+template <bool is_consumer>
+class NotAllRequiredBlocksAreVisitedError : public ScheduleError {
+ public:
+  explicit NotAllRequiredBlocksAreVisitedError(IRModule mod, int num_not_visited,
+                                               const Array<StmtSRef>& required)
+      : mod_(mod), num_not_visited_(num_not_visited) {
+    required_.reserve(required.size());
+    for (const StmtSRef& block_sref : required) {
+      const BlockNode* block = TVM_SREF_TO_BLOCK(block, block_sref);
+      required_.push_back(GetRef<Block>(block));
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Not all required blocks are under the loop scope";
+  }
+
+  String DetailRenderTemplate() const final {
+    String relation = is_consumer ? "consumer(s)" : "producer(s)";
+    std::ostringstream os;
+    os << "The primitive requires all the " << relation
+       << " of the given block to be present under the target loop. However, there are "
+       << num_not_visited_ << " " << relation << " not satisfying the constraint. List of the "
+       << relation << ":";
+    for (int i = 0, n = required_.size(); i < n; ++i) {
+      os << "{" << i << "}";
+    }
+    return os.str();
+  }
+
+  IRModule mod() const final { return mod_; }
+
+  Array<ObjectRef> LocationsOfInterest() const final {
+    return {required_.begin(), required_.end()};
+  }
+
+ private:
+  IRModule mod_;
+  int num_not_visited_;
+  Array<Block> required_;
+};
+
+/*!
+ * \brief An error raised when the given block is not in the same block scope as the given loop,
+ * or the given loop is the ancestor of the given block.
+ */
+class NotInSameScopeError : public ScheduleError {
+ public:
+  static void CheckAndBindLoopDomain(const ScheduleState& self, const StmtSRef& block_sref,
+                                     const StmtSRef& loop_sref, const StmtSRef& scope_root_sref,
+                                     arith::Analyzer* analyzer) {
+    for (const StmtSRefNode* p = loop_sref.get();; p = p->parent) {
+      if (const ForNode* loop = p->StmtAs<ForNode>()) {
+        analyzer->Bind(loop->loop_var, Range::FromMinExtent(loop->min, loop->extent));
+      } else if (p != scope_root_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      } else {
+        break;
+      }
+    }
+    for (const StmtSRefNode* p = block_sref->parent; p != scope_root_sref.get(); p = p->parent) {
+      if (p == loop_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      }
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Expected the block and loop to be under the same block scope, and loop "
+           "not to be the ancestor of block";
+  }
+  String DetailRenderTemplate() const final {
+    return "ScheduleError: Expected the block {0} and loop {1} to be under the same block scope, "
+           "and loop not to be the ancestor of block";
+  }
+  IRModule mod() const final { return mod_; }
+  Array<ObjectRef> LocationsOfInterest() const final { return {block_, loop_}; }
+
+ private:
+  explicit NotInSameScopeError(IRModule mod, const StmtSRef& block_sref, const StmtSRef& loop_sref)
+      : mod_(mod),
+        block_(GetRef<Block>(block_sref->StmtAs<BlockNode>())),
+        loop_(GetRef<For>(loop_sref->StmtAs<ForNode>())) {}
+
+  IRModule mod_;
+  Block block_;
+  For loop_;
+};
+
+/******** Helper Functions/Classes ********/
+
+/*!
+ * \brief Find a point where the block can be inserted under the loop
+ * \tparam require_all_producers_visited Requires all producer blocks to be present under the loop
+ * \tparam require_all_consumers_visited Requires all consumer blocks to be present under the loop
+ * \param self The schedule state
+ * \param subtrees The subtrees under the loop, among which the insertion points are sought
+ * \param producer_srefs The producer blocks
+ * \param consumer_srefs The consumer blocks
+ * \param block2realize A cache that maps a block to its realize
+ * \return The last position the new block can be inserted onto, and the
+ * producer-consumer-relationship is still satisfied.
+ */
+template <bool require_all_producers_visited, bool require_all_consumers_visited>
+int FindInsertionPoint(
+    const ScheduleState& self, const Array<Stmt>& subtrees, const Array<StmtSRef>& producer_srefs,
+    const Array<StmtSRef>& consumer_srefs,
+    std::unordered_map<const BlockNode*, const BlockRealizeNode*>* block2realize) {
+  ProducerConsumerSplit split =
+      ProducerConsumerSplit::Find(self, subtrees, producer_srefs, consumer_srefs, block2realize);
+  // Step 1. Check if all the producers are visited in the subtrees, if required to
+  if (require_all_producers_visited) {
+    int num_producers = producer_srefs.size();
+    if (split.n_producers_visited < num_producers) {
+      throw NotAllRequiredBlocksAreVisitedError<false>(
+          self->mod, num_producers - split.n_producers_visited, producer_srefs);
+    }
+  }
+  // Step 2. Check if all the consumers are visited in the subtrees, if required to
+  if (require_all_consumers_visited) {
+    int num_consumers = consumer_srefs.size();
+    if (split.n_consumers_visited < num_consumers) {
+      throw NotAllRequiredBlocksAreVisitedError<true>(
+          self->mod, num_consumers - split.n_consumers_visited, consumer_srefs);
+    }
+  }
+  // Step 3. Check if there is at least one index of the position can be inserted into
+  // The valid indices are: (last_producer_position, first_consumer_position]
+  ICHECK(split.last_producer_position < split.first_consumer_position);
+  // Step 4. Return the last valid insertion point
+  return split.first_consumer_position;
+}
+
+/*!
+ * \brief A helper to reconstruct the block scope where the given block is moved under the given
+ * loop, and the given block's induced loop nest is regenerated to satisfy the required region.
+ */
+class ScopeReconstructor : private StmtMutator {
+ public:
+  explicit ScopeReconstructor(Block scope_root, Block block, For loop)
+      : scope_root_(scope_root), block_(block), loop_(loop) {}
+
+  using StmtMutator::operator();
+
+  /*!
+   * \brief Create the loop nest on top of the block, induced by the given block var's domain
+   * \param insert_position The position among the subtrees where the block and its induced loop
+   * nest is inserted
+   * \param iter_doms The domain of each block var
+   * \param preserve_unit_loops Whether to generate unit loops where the loop extent is 1
+   */
+  void MakeNewLoop(int insert_position, std::vector<Range> iter_doms, bool preserve_unit_loops) {
+    int n_iters = iter_doms.size();
+    Array<Var> loop_vars;
+    Array<PrimExpr> loop_extents;
+    Array<PrimExpr> iter_values;
+    loop_vars.reserve(n_iters);
+    loop_extents.reserve(n_iters);
+    iter_values.reserve(n_iters);
+    for (int i = 0; i < n_iters; ++i) {
+      const Range& iter_dom = iter_doms[i];
+      if (preserve_unit_loops || !is_one(iter_dom->extent)) {
+        Var var("ax" + std::to_string(loop_vars.size()), DataType::Int(32));
+        loop_vars.push_back(var);
+        loop_extents.push_back(iter_dom->extent);
+        iter_values.push_back(iter_dom->min + var);
+      } else {
+        iter_values.push_back(iter_dom->min);
+      }
+    }
+    this->new_block_realize_ =
+        BlockRealize(std::move(iter_values), const_true(), std::move(block_));
+    Stmt new_subtree = this->new_block_realize_;
+    for (int i = static_cast<int>(loop_vars.size()) - 1; i >= 0; --i) {
+      const Var& loop_var = loop_vars[i];
+      const PrimExpr& loop_extent = loop_extents[i];
+      new_subtree = For(/*loop_var=*/loop_var,
+                        /*min=*/Integer(0),
+                        /*extent=*/loop_extent,
+                        /*ForKind=*/ForKind::kSerial,
+                        /*body=*/std::move(new_subtree));
+    }
+    Array<Stmt> subtrees = AsArray(loop_->body);
+    subtrees.insert(subtrees.begin() + insert_position, std::move(new_subtree));
+    ObjectPtr<ForNode> new_loop = make_object<ForNode>(*loop_.get());
+    new_loop->body = SeqStmt(std::move(subtrees));
+    this->new_loop_ = For(std::move(new_loop));
+  }
+
+ private:
+  Stmt VisitStmt_(const BlockNode* block) final {
+    if (block != scope_root_.get()) {
+      return GetRef<Block>(block);
+    }
+    if (block == rm_src_stmt_.get()) {
+      block = TVM_TYPE_AS(block, rm_tgt_stmt_, BlockNode);
+    }
+    return StmtMutator::VisitStmt_(block);
+  }
+
+  Stmt VisitStmt_(const ForNode* loop) final {
+    if (loop == rm_src_stmt_.get()) {
+      loop = TVM_TYPE_AS(loop, rm_tgt_stmt_, ForNode);
+    }
+    if (loop == loop_.get()) {
+      return new_loop_;
+    }
+    return StmtMutator::VisitStmt_(loop);
+  }
+
+ public:
+  /*! \brief The root block of the block scope */
+  Block scope_root_;
+  /*! \brief The given block to be moved */
+  Block block_;
+  /*! \brief The given loop the block and its loop nest to be put under */
+  For loop_;
+  /*! \brief The new loop to replace the original loop */
+  For new_loop_{nullptr};
+  /*! \brief The new block realize to the moved block */
+  BlockRealize new_block_realize_{nullptr};
+  /*! \brief The plan to remove the given block by replacing this loop/block in the AST */
+  Stmt rm_src_stmt_{nullptr};
+  /*! \brief The plan to remove the given block by replacing to this loop/block in the AST */
+  Stmt rm_tgt_stmt_{nullptr};
+};
+
+/*!
+ * \brief Calculate a list of accessed buffer regions under a path of loops
+ * \tparam relax_storage_scope Whether to relax beyond the path according to the storage and
+ * execution scope
+ * \param binding The block binding, used to unbind the buffer regions
+ * \param buffer_regions The buffer regions to be calculated
+ * \param relax_path_low_inclusive The lowest point in the loop path, inclusive
+ * \param relax_path_high_exclusive The highest point in the loop path, exclusive
+ * \param relaxed Where the calculation result is stored
+ */
+template <bool relax_storage_scope>
+void RelaxBufferRegions(const Map<Var, PrimExpr>& binding,
+                        const Array<BufferRegion>& buffer_regions,
+                        const StmtSRef& relax_path_low_inclusive,
+                        const StmtSRef& relax_path_high_exclusive,
+                        std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* relaxed) {
+  runtime::StorageScope global_scope{runtime::StorageRank::kGlobal, ""};
+  // We cache the variable domains
+  runtime::StorageRank previous_rank = runtime::StorageRank::kGlobal;
+  Optional<Map<Var, arith::IntSet>> var_dom = NullOpt;
+  // Enumerate every buffer region
+  for (const BufferRegion& buffer_region : buffer_regions) {
+    const Buffer& buffer = buffer_region->buffer;
+    const Array<Range>& region = buffer_region->region;
+    // Skip the buffer regions we are not interested in
+    auto it = relaxed->find(buffer.get());
+    if (it == relaxed->end()) {
+      continue;
+    }
+    std::vector<NDIntSet>& relaxed_regions = it->second;
+    // Check and update the cached `var_dom`
+    runtime::StorageScope scope =
+        relax_storage_scope ? runtime::StorageScope::Create(buffer.scope()) : global_scope;
+    runtime::StorageRank rank = scope.rank;
+    if (rank != previous_rank || !var_dom.defined()) {
+      previous_rank = rank;
+      var_dom = AsIntSet(LoopDomainOfSRefTreePath(
+          /*low_inclusive=*/relax_path_low_inclusive,
+          /*high_exclusive=*/relax_path_high_exclusive,
+          /*extra_relax_scope=*/scope));
+    }
+    // Relax the region
+    Array<arith::IntSet> relaxed = arith::EvalSet(Substitute(region, binding), var_dom.value());
+    relaxed_regions.push_back({relaxed.begin(), relaxed.end()});
+  }
+}
+
+/*!
+ * \brief Calculate the iteration domain of a provided integer set to fully cover the required
+ * domain
+ * \param provided The provided integer set to cover the required domain
+ * \param required The required domain to be covered
+ * \param iter_doms The result iteration domains to be updated
+ * \param analyzer The arithmetic analyzer
+ */
+void UpdateBlockVarDomain(const arith::IntSet& provided, const arith::IntSet& required,
+                          std::unordered_map<const VarNode*, std::vector<arith::IntSet>>* iter_doms,
+                          arith::Analyzer* analyzer) {
+  PrimExpr provided_min = analyzer->Simplify(provided.min());
+  PrimExpr provided_extent = analyzer->Simplify(provided.max() - provided_min + 1);
+  PrimExpr required_min = analyzer->Simplify(required.min());
+  PrimExpr required_extent = analyzer->Simplify(required.max() - required_min + 1);
+  PrimExpr dom_min{nullptr}, dom_extent{nullptr};
+  Var dom_var{ObjectPtr<VarNode>{nullptr}};
+  arith::PVar<Var> p_v;
+  arith::PVar<PrimExpr> p_e;
+  if ((p_v * p_e).Match(provided_min) || (p_e * p_v).Match(provided_min)) {
+    PrimExpr e = p_e.Eval();
+    dom_var = p_v.Eval();
+    dom_min = floordiv(required_min, e);
+    dom_extent = analyzer->Simplify((required_extent + e - 1) / e);
+  } else if (analyzer->CanProveEqual(provided_extent, 1) && p_v.Match(provided_min)) {
+    dom_var = p_v.Eval();
+    dom_min = required_min;
+    dom_extent = required_extent;
+  } else {
+    ICHECK(false) << "ValueError: BufferRegion pattern match failed";
+  }
+  auto it = iter_doms->find(dom_var.get());
+  if (it != iter_doms->end()) {
+    std::vector<arith::IntSet>& doms = it->second;
+    doms.push_back(arith::IntSet::FromMinExtent(dom_min, dom_extent));
+  } else {
+    ICHECK(analyzer->CanProveEqual(provided_min, required_min));
+    ICHECK(analyzer->CanProveEqual(provided_extent, required_extent));
+  }
+}
+
+/*!
+ * \brief Calculate the domain of block vars to cover the required region
+ * \param iter_vars The list of block vars to cover the required region
+ * \param provided_regions The region provided by one iteration instance of the block vars
+ * \param required_regions The region required to be covered
+ * \param analyzer The arithmetic analyzer
+ * \return A list of iteration domain corresponding to the given list of block vars
+ */
+std::vector<Range> CalculateBlockVarDomain(
+    const Array<IterVar>& iter_vars,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>> provided_regions,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>> required_regions,
+    arith::Analyzer* analyzer) {
+  int n_iters = iter_vars.size();
+  // Step 1. Construct the mapping from block var to their iteration domain (initialized to empty)
+  std::unordered_map<const VarNode*, std::vector<arith::IntSet>> iter_doms;
+  iter_doms.reserve(n_iters);
+  for (const IterVar& iter_var : iter_vars) {
+    iter_doms[iter_var->var.get()] = {};
+  }
+  // Step 2. For each buffer, update the domain according to the provided and required regions
+  for (const auto& kv : provided_regions) {
+    const BufferNode* buffer = kv.first;
+    const std::vector<NDIntSet>& many_provided_regions = kv.second;
+    // Calculate `provided_region` and `required_region`
+    auto it = required_regions.find(buffer);
+    if (it == required_regions.end() || it->second.empty()) {
+      continue;
+    }
+    NDIntSet required_region = support::NDIntSetUnion(it->second);
+    NDIntSet provided_region = support::NDIntSetUnion(many_provided_regions);
+    ICHECK_EQ(provided_region.size(), buffer->shape.size());
+    ICHECK_EQ(required_region.size(), buffer->shape.size());
+    // For each dimension, update the iteration domain
+    int ndim = buffer->shape.size();
+    for (int i = 0; i < ndim; ++i) {
+      arith::IntSet provided = provided_region[i];
+      arith::IntSet required = required_region[i];
+      required = arith::Intersect(
+          {std::move(required), arith::IntSet::FromMinExtent(Integer(0), buffer->shape[i])});
+      UpdateBlockVarDomain(provided, required, &iter_doms, analyzer);
+    }
+  }
+  // Union the iter var domains, put them in the same order of block vars, and return
+  std::vector<Range> result;
+  result.reserve(n_iters);
+  for (const IterVar& iter_var : iter_vars) {
+    const std::vector<arith::IntSet>& doms = iter_doms.at(iter_var->var.get());
+    arith::IntSet dom = arith::IntSet::FromRange(iter_var->dom);
+    if (!doms.empty()) {
+      dom = arith::Intersect({std::move(dom), arith::Union(doms)});
+    }
+    PrimExpr min = analyzer->Simplify(dom.min());
+    PrimExpr extent = analyzer->Simplify(dom.max() - min + 1);
+    result.push_back(Range::FromMinExtent(min, extent));
+  }
+  return result;
+}
+
+/*!
+ * \brief Calculate the provided region of the given block by one single of its execution instance,
+ * as well as the required buffer regions relaxed to the given loop
+ * \tparam is_compute_at Indicates if the operation is compute-at or reverse-compute-at
+ * \param block The given block that provides buffer regions
+ * \param loop_sref The given loop under which the block is going to be moved to
+ * \param block2realize Maps a block to its corresponding BlockRealize
+ * \param producer_srefs The producers of the given block
+ * \param consumer_srefs The consumers of the given block
+ * \param provided_regions The calculated regions provided by the block
+ * \param required_regions The calculated regions required by its consumers (in compute-at) or
+ * producers (in reverse-compute-at)
+ */
+template <bool is_compute_at>
+void CalculateProvidedRequiredRegions(
+    const BlockNode* block, const StmtSRef& loop_sref,
+    std::unordered_map<const BlockNode*, const BlockRealizeNode*> block2realize,
+    Array<StmtSRef> producer_srefs, Array<StmtSRef> consumer_srefs,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* provided_regions,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* required_regions) {
+  // Step 1. Calculate the region provided by a single execution instance of `block`
+  const Array<BufferRegion>& provided_buffers = is_compute_at ? block->writes : block->reads;
+  provided_regions->reserve(provided_buffers.size());
+  required_regions->reserve(provided_buffers.size());
+  for (const BufferRegion& provided_buffer_region : provided_buffers) {
+    const BufferNode* buffer = provided_buffer_region->buffer.get();
+    const Array<Range>& region = provided_buffer_region->region;
+    (*provided_regions)[buffer].push_back(support::NDIntSetFromRegion(region));
+    (*required_regions)[buffer].clear();
+  }
+  // Step 2. Calculate the region required by dependent blocks under `loop`
+  for (const StmtSRef& required_block_sref : is_compute_at ? consumer_srefs : producer_srefs) {
+    const BlockNode* required_block = TVM_SREF_TO_BLOCK(required_block, required_block_sref);
+    ICHECK(block2realize.count(required_block));
+    RelaxBufferRegions</*relax_storage_scope=*/is_compute_at>(
+        /*binding=*/GetBindings(GetRef<BlockRealize>(block2realize.at(required_block))),
+        /*buffer_regions=*/is_compute_at ? required_block->reads : required_block->writes,
+        /*relax_path_low_inclusive=*/GetRef<StmtSRef>(required_block_sref->parent),
+        /*relax_path_high_exclusive=*/loop_sref, /*relaxed=*/required_regions);
+  }
+}
+
+/******** Main Implementation ********/
+
+template <bool is_compute_at>
+void ComputeAtOrReverseComputeAtImpl(ScheduleState self, const StmtSRef& block_sref,
+                                     const StmtSRef& loop_sref, bool preserve_unit_loops) {
+  const BlockNode* block = TVM_SREF_TO_BLOCK(block, block_sref);
+  const ForNode* loop = TVM_SREF_TO_FOR(loop, loop_sref);
+  // Step 1. Bunch of checks
+  // Check condition 1) and 2): stage pipeline and subtree compact dataflow
+  StmtSRef scope_root_sref = GetScopeRoot(self, block_sref,
+                                          /*require_stage_pipeline=*/true,
+                                          /*require_subtree_compact_dataflow=*/true);
+  Block scope_root = GetRef<Block>(scope_root_sref->StmtAs<BlockNode>());
+  BlockScope scope = self->GetBlockScope(scope_root_sref);
+  Array<StmtSRef> producer_srefs = GetProducers(block_sref, scope);
+  Array<StmtSRef> consumer_srefs = GetConsumers(block_sref, scope);
+  arith::Analyzer analyzer;
+  // Check condition 3): `block` and `loop` are under the same scope,
+  // and `loop` is not the ancestor of `block`
+  NotInSameScopeError::CheckAndBindLoopDomain(self, block_sref, loop_sref, scope_root_sref,
+                                              &analyzer);
+  // Check condition 4): `block` is not an output block
+  if (is_compute_at) {
+    CheckNotOutputBlock(self, block_sref, scope_root_sref);
+  }
+  // Step 2. Plan for the removal of `block`
+  ScopeReconstructor reconstructor(scope_root, GetRef<Block>(block), GetRef<For>(loop));
+  LeafBlockRemovalPlan(self, block_sref, &reconstructor.rm_src_stmt_, &reconstructor.rm_tgt_stmt_);
+  // Step 3. Find the insertion point under `loop`
+  // Check condition 5): all the required block are under the given loop
+  std::unordered_map<const BlockNode*, const BlockRealizeNode*> block2realize;
+  block2realize.reserve(self->block_info.size());
+  int insert_position = FindInsertionPoint<!is_compute_at, is_compute_at>(
+      /*self=*/self,
+      /*subtrees=*/AsArray(loop->body),
+      /*producer_srefs=*/producer_srefs,
+      /*consumer_srefs=*/consumer_srefs, /*block2realize=*/&block2realize);
+  // Step 4. Calculate the region provided by a single execution instance of `block`,
+  // as well as the region required by dependent blocks under `loop`

Review comment:
       For ComputeAt, `block` _provides_ regions and the blocks under `loop` _require_ regions. But for ReverseComputeAt, `block` _requires_ regions, and the blocks under `loop` _provide_ regions. We might need to rephrase it.

##########
File path: src/tir/schedule/analysis.h
##########
@@ -232,6 +261,55 @@ BlockRealize CheckGetSingleChildBlockRealizeOnSRefTree(const ScheduleState& self
  */
 BlockRealize GetBlockRealize(const ScheduleState& self, const StmtSRef& block_sref);
 
+/******** Producer-consumer relation ********/
+
+/*!
+ * \brief Get the producer blocks to the given block under the given scope
+ * \param block_sref The block whose producers are to be retrieved
+ * \param scope The block scope where the given block is in
+ * \return The producer blocks of the specified block
+ */
+Array<StmtSRef> GetProducers(const StmtSRef& block_sref, const BlockScope& scope);
+
+/*!
+ * \brief Get the consumer blocks to the given block under the given scope
+ * \param block_sref The block whose consumers are to be retrieved
+ * \param scope The block scope where the given block is in
+ * \return The consumer blocks of the specified block
+ */
+Array<StmtSRef> GetConsumers(const StmtSRef& block_sref, const BlockScope& scope);
+
+/*!
+ * \brief A solution to split a ordered list of subtrees into two parts,
+ * where producers are on the LHS and consumers are on the RHS.
+ * For example, subtree[0, 3) are on the LHS, and subtree[3, 6) are on the RHS.
+ */
+struct ProducerConsumerSplit {

Review comment:
       Looks like only ComputeAt/ReverseComputeAt use `ProducerConsumerSplit`. Therefore shall we move it to `compute_at.cc`?

##########
File path: src/tir/schedule/primitive/compute_at.cc
##########
@@ -0,0 +1,584 @@
+/*
+ * 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 {
+
+using support::NDIntSet;
+
+/******** Error Classes ********/
+
+/*!
+ * \brief An error raised when not all required blocks are under the given loop.
+ * \tparam is_consumer Indicates if all the required blocks are consumers or producers
+ */
+template <bool is_consumer>
+class NotAllRequiredBlocksAreVisitedError : public ScheduleError {
+ public:
+  explicit NotAllRequiredBlocksAreVisitedError(IRModule mod, int num_not_visited,
+                                               const Array<StmtSRef>& required)
+      : mod_(mod), num_not_visited_(num_not_visited) {
+    required_.reserve(required.size());
+    for (const StmtSRef& block_sref : required) {
+      const BlockNode* block = TVM_SREF_TO_BLOCK(block, block_sref);
+      required_.push_back(GetRef<Block>(block));
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Not all required blocks are under the loop scope";
+  }
+
+  String DetailRenderTemplate() const final {
+    String relation = is_consumer ? "consumer(s)" : "producer(s)";
+    std::ostringstream os;
+    os << "The primitive requires all the " << relation
+       << " of the given block to be present under the target loop. However, there are "
+       << num_not_visited_ << " " << relation << " not satisfying the constraint. List of the "
+       << relation << ":";
+    for (int i = 0, n = required_.size(); i < n; ++i) {
+      os << "{" << i << "}";
+    }
+    return os.str();
+  }
+
+  IRModule mod() const final { return mod_; }
+
+  Array<ObjectRef> LocationsOfInterest() const final {
+    return {required_.begin(), required_.end()};
+  }
+
+ private:
+  IRModule mod_;
+  int num_not_visited_;
+  Array<Block> required_;
+};
+
+/*!
+ * \brief An error raised when the given block is not in the same block scope as the given loop,
+ * or the given loop is the ancestor of the given block.
+ */
+class NotInSameScopeError : public ScheduleError {
+ public:
+  static void CheckAndBindLoopDomain(const ScheduleState& self, const StmtSRef& block_sref,
+                                     const StmtSRef& loop_sref, const StmtSRef& scope_root_sref,
+                                     arith::Analyzer* analyzer) {
+    for (const StmtSRefNode* p = loop_sref.get();; p = p->parent) {
+      if (const ForNode* loop = p->StmtAs<ForNode>()) {
+        analyzer->Bind(loop->loop_var, Range::FromMinExtent(loop->min, loop->extent));
+      } else if (p != scope_root_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      } else {
+        break;
+      }
+    }
+    for (const StmtSRefNode* p = block_sref->parent; p != scope_root_sref.get(); p = p->parent) {
+      if (p == loop_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      }
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Expected the block and loop to be under the same block scope, and loop "
+           "not to be the ancestor of block";
+  }
+  String DetailRenderTemplate() const final {
+    return "ScheduleError: Expected the block {0} and loop {1} to be under the same block scope, "
+           "and loop not to be the ancestor of block";
+  }
+  IRModule mod() const final { return mod_; }
+  Array<ObjectRef> LocationsOfInterest() const final { return {block_, loop_}; }
+
+ private:
+  explicit NotInSameScopeError(IRModule mod, const StmtSRef& block_sref, const StmtSRef& loop_sref)
+      : mod_(mod),
+        block_(GetRef<Block>(block_sref->StmtAs<BlockNode>())),
+        loop_(GetRef<For>(loop_sref->StmtAs<ForNode>())) {}
+
+  IRModule mod_;
+  Block block_;
+  For loop_;
+};
+
+/******** Helper Functions/Classes ********/
+
+/*!
+ * \brief Find a point where the block can be inserted under the loop
+ * \tparam require_all_producers_visited Requires all producer blocks to be present under the loop
+ * \tparam require_all_consumers_visited Requires all consumer blocks to be present under the loop
+ * \param self The schedule state
+ * \param subtrees The subtrees under the loop, among which the insertion points are sought
+ * \param producer_srefs The producer blocks
+ * \param consumer_srefs The consumer blocks
+ * \param block2realize A cache that maps a block to its realize
+ * \return The last position the new block can be inserted onto, and the
+ * producer-consumer-relationship is still satisfied.
+ */
+template <bool require_all_producers_visited, bool require_all_consumers_visited>
+int FindInsertionPoint(
+    const ScheduleState& self, const Array<Stmt>& subtrees, const Array<StmtSRef>& producer_srefs,
+    const Array<StmtSRef>& consumer_srefs,
+    std::unordered_map<const BlockNode*, const BlockRealizeNode*>* block2realize) {
+  ProducerConsumerSplit split =
+      ProducerConsumerSplit::Find(self, subtrees, producer_srefs, consumer_srefs, block2realize);
+  // Step 1. Check if all the producers are visited in the subtrees, if required to
+  if (require_all_producers_visited) {
+    int num_producers = producer_srefs.size();
+    if (split.n_producers_visited < num_producers) {
+      throw NotAllRequiredBlocksAreVisitedError<false>(
+          self->mod, num_producers - split.n_producers_visited, producer_srefs);
+    }
+  }
+  // Step 2. Check if all the consumers are visited in the subtrees, if required to
+  if (require_all_consumers_visited) {
+    int num_consumers = consumer_srefs.size();
+    if (split.n_consumers_visited < num_consumers) {
+      throw NotAllRequiredBlocksAreVisitedError<true>(
+          self->mod, num_consumers - split.n_consumers_visited, consumer_srefs);
+    }
+  }
+  // Step 3. Check if there is at least one index of the position can be inserted into
+  // The valid indices are: (last_producer_position, first_consumer_position]
+  ICHECK(split.last_producer_position < split.first_consumer_position);
+  // Step 4. Return the last valid insertion point
+  return split.first_consumer_position;
+}
+
+/*!
+ * \brief A helper to reconstruct the block scope where the given block is moved under the given
+ * loop, and the given block's induced loop nest is regenerated to satisfy the required region.
+ */
+class ScopeReconstructor : private StmtMutator {
+ public:
+  explicit ScopeReconstructor(Block scope_root, Block block, For loop)
+      : scope_root_(scope_root), block_(block), loop_(loop) {}
+
+  using StmtMutator::operator();
+
+  /*!
+   * \brief Create the loop nest on top of the block, induced by the given block var's domain
+   * \param insert_position The position among the subtrees where the block and its induced loop
+   * nest is inserted
+   * \param iter_doms The domain of each block var
+   * \param preserve_unit_loops Whether to generate unit loops where the loop extent is 1
+   */
+  void MakeNewLoop(int insert_position, std::vector<Range> iter_doms, bool preserve_unit_loops) {
+    int n_iters = iter_doms.size();
+    Array<Var> loop_vars;
+    Array<PrimExpr> loop_extents;
+    Array<PrimExpr> iter_values;
+    loop_vars.reserve(n_iters);
+    loop_extents.reserve(n_iters);
+    iter_values.reserve(n_iters);
+    for (int i = 0; i < n_iters; ++i) {
+      const Range& iter_dom = iter_doms[i];
+      if (preserve_unit_loops || !is_one(iter_dom->extent)) {
+        Var var("ax" + std::to_string(loop_vars.size()), DataType::Int(32));
+        loop_vars.push_back(var);
+        loop_extents.push_back(iter_dom->extent);
+        iter_values.push_back(iter_dom->min + var);
+      } else {
+        iter_values.push_back(iter_dom->min);
+      }
+    }
+    this->new_block_realize_ =
+        BlockRealize(std::move(iter_values), const_true(), std::move(block_));
+    Stmt new_subtree = this->new_block_realize_;
+    for (int i = static_cast<int>(loop_vars.size()) - 1; i >= 0; --i) {
+      const Var& loop_var = loop_vars[i];
+      const PrimExpr& loop_extent = loop_extents[i];
+      new_subtree = For(/*loop_var=*/loop_var,
+                        /*min=*/Integer(0),
+                        /*extent=*/loop_extent,
+                        /*ForKind=*/ForKind::kSerial,
+                        /*body=*/std::move(new_subtree));
+    }
+    Array<Stmt> subtrees = AsArray(loop_->body);
+    subtrees.insert(subtrees.begin() + insert_position, std::move(new_subtree));
+    ObjectPtr<ForNode> new_loop = make_object<ForNode>(*loop_.get());
+    new_loop->body = SeqStmt(std::move(subtrees));
+    this->new_loop_ = For(std::move(new_loop));
+  }
+
+ private:
+  Stmt VisitStmt_(const BlockNode* block) final {
+    if (block != scope_root_.get()) {
+      return GetRef<Block>(block);
+    }
+    if (block == rm_src_stmt_.get()) {
+      block = TVM_TYPE_AS(block, rm_tgt_stmt_, BlockNode);
+    }
+    return StmtMutator::VisitStmt_(block);
+  }
+
+  Stmt VisitStmt_(const ForNode* loop) final {
+    if (loop == rm_src_stmt_.get()) {
+      loop = TVM_TYPE_AS(loop, rm_tgt_stmt_, ForNode);
+    }
+    if (loop == loop_.get()) {
+      return new_loop_;
+    }
+    return StmtMutator::VisitStmt_(loop);
+  }
+
+ public:
+  /*! \brief The root block of the block scope */
+  Block scope_root_;
+  /*! \brief The given block to be moved */
+  Block block_;
+  /*! \brief The given loop the block and its loop nest to be put under */
+  For loop_;
+  /*! \brief The new loop to replace the original loop */
+  For new_loop_{nullptr};
+  /*! \brief The new block realize to the moved block */
+  BlockRealize new_block_realize_{nullptr};
+  /*! \brief The plan to remove the given block by replacing this loop/block in the AST */
+  Stmt rm_src_stmt_{nullptr};
+  /*! \brief The plan to remove the given block by replacing to this loop/block in the AST */
+  Stmt rm_tgt_stmt_{nullptr};
+};
+
+/*!
+ * \brief Calculate a list of accessed buffer regions under a path of loops
+ * \tparam relax_storage_scope Whether to relax beyond the path according to the storage and
+ * execution scope

Review comment:
       Why ComputeAt needs to relax according to the storage scope, while ReverseComputeAt doesn't?




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r703887290



##########
File path: src/relay/transforms/fold_scale_axis.cc
##########
@@ -243,7 +243,9 @@ class ForwardPrep : private MixedModeVisitor {
     }
   }
   // Visitor pattern override.
-  void VisitExpr_(const LetNode* op) {
+  void VisitExpr_(const TupleGetItemNode* op) final { MixedModeVisitor::VisitExpr_(op); }

Review comment:
       Fix clang-12 warnings




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r703890619



##########
File path: src/tir/schedule/primitive/cache_read_write.cc
##########
@@ -160,6 +160,21 @@ Block MakeCacheStage(const BufferRegion& cache_region, CacheStageInfo* info,
   return block;
 }
 
+/*!
+ * \brief Recalculate the `affine_binding` flag of the scope block info.
+ * \param block_sref The sref to the interested scope block.
+ */
+bool CalculateAffineFlag(const ScheduleState& self, const StmtSRef& block_sref) {
+  if (block_sref->parent == nullptr) {
+    return true;
+  }

Review comment:
       I don't actually have a good question. CC: @Hzfengsy 

##########
File path: src/tir/schedule/primitive/cache_read_write.cc
##########
@@ -160,6 +160,21 @@ Block MakeCacheStage(const BufferRegion& cache_region, CacheStageInfo* info,
   return block;
 }
 
+/*!
+ * \brief Recalculate the `affine_binding` flag of the scope block info.
+ * \param block_sref The sref to the interested scope block.
+ */
+bool CalculateAffineFlag(const ScheduleState& self, const StmtSRef& block_sref) {
+  if (block_sref->parent == nullptr) {
+    return true;
+  }

Review comment:
       I don't actually have a good answer to this question. CC: @Hzfengsy 




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



[GitHub] [tvm] junrushao1994 commented on pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#issuecomment-915778033


   @MasterJH5574 Thanks for the careful review! Without your help, I cannot find the spot I mistakes one primitive's doc for the other lol. Thanks a lot!


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



[GitHub] [tvm] MasterJH5574 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
MasterJH5574 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704568545



##########
File path: include/tvm/tir/schedule/schedule.h
##########
@@ -305,6 +305,42 @@ class ScheduleNode : public runtime::Object {
   virtual BlockRV CacheWrite(const BlockRV& block_rv, int write_buffer_index,
                              const String& storage_scope) = 0;
   /******** Schedule: Compute location ********/
+  /*!
+   * \brief Move a producer block under the specific loop, and regenerate the loops induced by the
+   * block so that the buffer region generated by the producer block could cover those regions
+   * written by the producers. It requires:
+   * 1) `block` and `loop` are under the same scope, `loop` is not the ancestor of `block`
+   * 2) The scope block has stage-pipeline property
+   * 3) The subtree of the scope block, where the given block is in, satisfies the compact dataflow
+   * condition. i.e. all the blocks in the scope block's subtree must be either complete block or
+   * reduction block
+   * 4) The block is not an output block, i.e. the buffer regions written by the block are allocated
+   * under the current scope

Review comment:
       I want to make it more clear.
   ```suggestion
      * 4) The block is not an output block with regard to the scope block, i.e. the buffers written by the block are allocated
      * under the scope block
   ```




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704740833



##########
File path: include/tvm/tir/schedule/schedule.h
##########
@@ -305,6 +305,38 @@ class ScheduleNode : public runtime::Object {
   virtual BlockRV CacheWrite(const BlockRV& block_rv, int write_buffer_index,
                              const String& storage_scope) = 0;
   /******** Schedule: Compute location ********/
+  /*!
+   * \brief Move a producer block under the specific loop, and regenerate the loops induced by the
+   * block so that the buffer region generated by the producer block could cover those regions read
+   * by the consumers. It requires:
+   * 1) The scope block has stage-pipeline property
+   * 2) The given block's subtree of the scope block satisfies compact dataflow condition.
+   * i.e. all the blocks in the scope's subtree must be either complete block or reduction block
+   * 3) `block` and `loop` are under the same scope, `loop` is not the ancestor of `block`
+   * 4) The block is not an output block,
+   * i.e. the buffer regions written by the block are allocated under the current scope
+   * 5) All the consumers of the block are under the given loop
+   * \param block_rv The block to be moved
+   * \param loop_rv The loop where the block to be moved under
+   * \param preserve_unit_loops Whether to keep the trivial loops whose extents are 1
+   */
+  virtual void ComputeAt(const BlockRV& block_rv, const LoopRV& loop_rv,
+                         bool preserve_unit_loops) = 0;
+  /*!
+   * \brief Move a consumer block under the specific loop, and regenerate the loops induced by the
+   * block so that the buffer region generated by the consumer block could cover those regions read
+   * by the consumers. It requires:

Review comment:
       Could you explicitly suggest the change?




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



[GitHub] [tvm] MasterJH5574 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
MasterJH5574 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704000075



##########
File path: include/tvm/tir/schedule/schedule.h
##########
@@ -305,6 +305,38 @@ class ScheduleNode : public runtime::Object {
   virtual BlockRV CacheWrite(const BlockRV& block_rv, int write_buffer_index,
                              const String& storage_scope) = 0;
   /******** Schedule: Compute location ********/
+  /*!
+   * \brief Move a producer block under the specific loop, and regenerate the loops induced by the
+   * block so that the buffer region generated by the producer block could cover those regions read
+   * by the consumers. It requires:
+   * 1) The scope block has stage-pipeline property
+   * 2) The given block's subtree of the scope block satisfies compact dataflow condition.
+   * i.e. all the blocks in the scope's subtree must be either complete block or reduction block

Review comment:
       If so, then condition 2 looks inconsistent:
   > The **given block**'s subtree of the scope block satisfies compact dataflow condition
   
   > i.e. all the blocks in the **scope**'s subtree must be either complete block or reduction block




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



[GitHub] [tvm] MasterJH5574 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
MasterJH5574 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704303477



##########
File path: include/tvm/tir/schedule/schedule.h
##########
@@ -305,6 +305,38 @@ class ScheduleNode : public runtime::Object {
   virtual BlockRV CacheWrite(const BlockRV& block_rv, int write_buffer_index,
                              const String& storage_scope) = 0;
   /******** Schedule: Compute location ********/
+  /*!
+   * \brief Move a producer block under the specific loop, and regenerate the loops induced by the
+   * block so that the buffer region generated by the producer block could cover those regions read
+   * by the consumers. It requires:
+   * 1) The scope block has stage-pipeline property
+   * 2) The given block's subtree of the scope block satisfies compact dataflow condition.
+   * i.e. all the blocks in the scope's subtree must be either complete block or reduction block
+   * 3) `block` and `loop` are under the same scope, `loop` is not the ancestor of `block`
+   * 4) The block is not an output block,
+   * i.e. the buffer regions written by the block are allocated under the current scope
+   * 5) All the consumers of the block are under the given loop
+   * \param block_rv The block to be moved
+   * \param loop_rv The loop where the block to be moved under
+   * \param preserve_unit_loops Whether to keep the trivial loops whose extents are 1
+   */
+  virtual void ComputeAt(const BlockRV& block_rv, const LoopRV& loop_rv,
+                         bool preserve_unit_loops) = 0;
+  /*!
+   * \brief Move a consumer block under the specific loop, and regenerate the loops induced by the
+   * block so that the buffer region generated by the consumer block could cover those regions read
+   * by the consumers. It requires:

Review comment:
       Hmmmmm I cannot find your update.. Would you like to check it again?




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704129935



##########
File path: src/tir/schedule/analysis.h
##########
@@ -128,18 +138,36 @@ void CheckReductionBlock(const ScheduleState& self, const StmtSRef& block_sref,
                          const StmtSRef& scope_root_sref);
 
 /*!
- * \brief Check whether a subtree on SRef tree has compact data flow, and throw an exception if the
- * subtree does not have compact data flow
- * \details For a given StmtSRef, We say the subtree rooted from the StmtSRef has "compact data
- * flow" property if:
- * - the scope root of the input subtree root has stage-pipeline property, and
- * - all its child blocks on SRef tree are complete blocks or reduction blocks.
+ * \brief Check if the block is a complete block or a reduction block under the scope
  * \param self The schedule state
- * \param subtree_root_sref The root of the subtree to be checked in the SRef tree
- * \throw ScheduleError If the subtree does not have compact data flow
- * \sa IsCompleteBlock, IsReductionBlock
+ * \param block_sref The sref of the block to be checked
+ * \param scope_root_sref The scope root of the block
+ * \throw ScheduleError If the block is neither a complete block nor a reduction block
+ */
+void CheckCompleteOrReductionBlock(const ScheduleState& self, const StmtSRef& block_sref,
+                                   const StmtSRef& scope_root_sref);
+
+/*!
+ * \brief Check if the block is an output block, i.e. the block writes to at least a buffer that is
+ * not allocated under the current scope
+ * \param self The schedule state
+ * \param block_sref The block to be checked
+ * \param scope_root_sref The scope root of the block
+ * \return A boolean flag indicating if the block is an output block
+ */
+bool IsOutputBlock(const ScheduleState& self, const StmtSRef& block_sref,
+                   const StmtSRef& scope_root_sref);
+
+/*!
+ * \brief Check if the block is not an output block, i.e. all the buffers the block writes to
+ * are allocated under the current scope

Review comment:
       I did updated the docs for both methods (`IsOutputBlock`, `CheckNotOutputBlock`) in the previous [commit](https://github.com/apache/tvm/pull/8943/commits/5e1be9aa8adfe1b0ecf2ed1d3a7ca6502626dd00). Note that the checking method asserts if the predicate method's result is false (i.e. not output block), I feel that the current doc is good 




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



[GitHub] [tvm] MasterJH5574 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
MasterJH5574 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704889880



##########
File path: src/tir/schedule/utils.h
##########
@@ -163,6 +165,21 @@ inline Stmt RemoveFromSeqStmt(const SeqStmt& seq, const Stmt& to_remove) {
   return SeqStmt::Flatten(new_stmts);
 }
 
+/*!
+ * \brief Convert a Stmt to an Array.
+ * \param stmt The Stmt ti be converted to

Review comment:
       Ooooops...
   ```suggestion
    * \param stmt The Stmt to be converted to
   ```




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704129935



##########
File path: src/tir/schedule/analysis.h
##########
@@ -128,18 +138,36 @@ void CheckReductionBlock(const ScheduleState& self, const StmtSRef& block_sref,
                          const StmtSRef& scope_root_sref);
 
 /*!
- * \brief Check whether a subtree on SRef tree has compact data flow, and throw an exception if the
- * subtree does not have compact data flow
- * \details For a given StmtSRef, We say the subtree rooted from the StmtSRef has "compact data
- * flow" property if:
- * - the scope root of the input subtree root has stage-pipeline property, and
- * - all its child blocks on SRef tree are complete blocks or reduction blocks.
+ * \brief Check if the block is a complete block or a reduction block under the scope
  * \param self The schedule state
- * \param subtree_root_sref The root of the subtree to be checked in the SRef tree
- * \throw ScheduleError If the subtree does not have compact data flow
- * \sa IsCompleteBlock, IsReductionBlock
+ * \param block_sref The sref of the block to be checked
+ * \param scope_root_sref The scope root of the block
+ * \throw ScheduleError If the block is neither a complete block nor a reduction block
+ */
+void CheckCompleteOrReductionBlock(const ScheduleState& self, const StmtSRef& block_sref,
+                                   const StmtSRef& scope_root_sref);
+
+/*!
+ * \brief Check if the block is an output block, i.e. the block writes to at least a buffer that is
+ * not allocated under the current scope
+ * \param self The schedule state
+ * \param block_sref The block to be checked
+ * \param scope_root_sref The scope root of the block
+ * \return A boolean flag indicating if the block is an output block
+ */
+bool IsOutputBlock(const ScheduleState& self, const StmtSRef& block_sref,
+                   const StmtSRef& scope_root_sref);
+
+/*!
+ * \brief Check if the block is not an output block, i.e. all the buffers the block writes to
+ * are allocated under the current scope

Review comment:
       I did updated the docks for both methods (`IsOutputBlock`, `CheckNotOutputBlock`) in the previous PR. Note that the checking method asserts if the predicate method's result is false (i.e. not output block), I feel that the current doc is good 




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704954091



##########
File path: include/tvm/tir/schedule/schedule.h
##########
@@ -305,6 +305,38 @@ class ScheduleNode : public runtime::Object {
   virtual BlockRV CacheWrite(const BlockRV& block_rv, int write_buffer_index,
                              const String& storage_scope) = 0;
   /******** Schedule: Compute location ********/
+  /*!
+   * \brief Move a producer block under the specific loop, and regenerate the loops induced by the
+   * block so that the buffer region generated by the producer block could cover those regions read
+   * by the consumers. It requires:
+   * 1) The scope block has stage-pipeline property
+   * 2) The given block's subtree of the scope block satisfies compact dataflow condition.
+   * i.e. all the blocks in the scope's subtree must be either complete block or reduction block
+   * 3) `block` and `loop` are under the same scope, `loop` is not the ancestor of `block`
+   * 4) The block is not an output block,
+   * i.e. the buffer regions written by the block are allocated under the current scope
+   * 5) All the consumers of the block are under the given loop
+   * \param block_rv The block to be moved
+   * \param loop_rv The loop where the block to be moved under
+   * \param preserve_unit_loops Whether to keep the trivial loops whose extents are 1
+   */
+  virtual void ComputeAt(const BlockRV& block_rv, const LoopRV& loop_rv,
+                         bool preserve_unit_loops) = 0;
+  /*!
+   * \brief Move a consumer block under the specific loop, and regenerate the loops induced by the
+   * block so that the buffer region generated by the consumer block could cover those regions read
+   * by the consumers. It requires:

Review comment:
       Oh finally I figured out. I messed up the docs between compute-at and reverse-compute-at 🤦‍♂️




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r703886209



##########
File path: include/tvm/tir/schedule/schedule.h
##########
@@ -305,6 +305,38 @@ class ScheduleNode : public runtime::Object {
   virtual BlockRV CacheWrite(const BlockRV& block_rv, int write_buffer_index,
                              const String& storage_scope) = 0;
   /******** Schedule: Compute location ********/
+  /*!
+   * \brief Move a producer block under the specific loop, and regenerate the loops induced by the
+   * block so that the buffer region generated by the producer block could cover those regions read
+   * by the consumers. It requires:
+   * 1) The scope block has stage-pipeline property
+   * 2) The given block's subtree of the scope block satisfies compact dataflow condition.
+   * i.e. all the blocks in the scope's subtree must be either complete block or reduction block
+   * 3) `block` and `loop` are under the same scope, `loop` is not the ancestor of `block`

Review comment:
       Yeah that makes sense to me




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



[GitHub] [tvm] MasterJH5574 commented on pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
MasterJH5574 commented on pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#issuecomment-915388519


   Just finished a whole review of the entire PR. Thanks @junrushao1994 for the tireless work! Please take a look at my comments. I believe that this PR can be merged very soon :-)


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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704724549



##########
File path: src/tir/schedule/primitive/compute_at.cc
##########
@@ -0,0 +1,584 @@
+/*
+ * 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 {
+
+using support::NDIntSet;
+
+/******** Error Classes ********/
+
+/*!
+ * \brief An error raised when not all required blocks are under the given loop.
+ * \tparam is_consumer Indicates if all the required blocks are consumers or producers
+ */
+template <bool is_consumer>
+class NotAllRequiredBlocksAreVisitedError : public ScheduleError {
+ public:
+  explicit NotAllRequiredBlocksAreVisitedError(IRModule mod, int num_not_visited,
+                                               const Array<StmtSRef>& required)
+      : mod_(mod), num_not_visited_(num_not_visited) {
+    required_.reserve(required.size());
+    for (const StmtSRef& block_sref : required) {
+      const BlockNode* block = TVM_SREF_TO_BLOCK(block, block_sref);
+      required_.push_back(GetRef<Block>(block));
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Not all required blocks are under the loop scope";
+  }
+
+  String DetailRenderTemplate() const final {
+    String relation = is_consumer ? "consumer(s)" : "producer(s)";
+    std::ostringstream os;
+    os << "The primitive requires all the " << relation
+       << " of the given block to be present under the target loop. However, there are "
+       << num_not_visited_ << " " << relation << " not satisfying the constraint. List of the "
+       << relation << ":";
+    for (int i = 0, n = required_.size(); i < n; ++i) {
+      os << "{" << i << "}";
+    }
+    return os.str();
+  }
+
+  IRModule mod() const final { return mod_; }
+
+  Array<ObjectRef> LocationsOfInterest() const final {
+    return {required_.begin(), required_.end()};
+  }
+
+ private:
+  IRModule mod_;
+  int num_not_visited_;
+  Array<Block> required_;
+};
+
+/*!
+ * \brief An error raised when the given block is not in the same block scope as the given loop,
+ * or the given loop is the ancestor of the given block.
+ */
+class NotInSameScopeError : public ScheduleError {
+ public:
+  static void CheckAndBindLoopDomain(const ScheduleState& self, const StmtSRef& block_sref,
+                                     const StmtSRef& loop_sref, const StmtSRef& scope_root_sref,
+                                     arith::Analyzer* analyzer) {
+    for (const StmtSRefNode* p = loop_sref.get();; p = p->parent) {
+      if (const ForNode* loop = p->StmtAs<ForNode>()) {
+        analyzer->Bind(loop->loop_var, Range::FromMinExtent(loop->min, loop->extent));
+      } else if (p != scope_root_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      } else {
+        break;
+      }
+    }
+    for (const StmtSRefNode* p = block_sref->parent; p != scope_root_sref.get(); p = p->parent) {
+      if (p == loop_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      }
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Expected the block and loop to be under the same block scope, and loop "
+           "not to be the ancestor of block";
+  }
+  String DetailRenderTemplate() const final {
+    return "ScheduleError: Expected the block {0} and loop {1} to be under the same block scope, "
+           "and loop not to be the ancestor of block";
+  }
+  IRModule mod() const final { return mod_; }
+  Array<ObjectRef> LocationsOfInterest() const final { return {block_, loop_}; }
+
+ private:
+  explicit NotInSameScopeError(IRModule mod, const StmtSRef& block_sref, const StmtSRef& loop_sref)
+      : mod_(mod),
+        block_(GetRef<Block>(block_sref->StmtAs<BlockNode>())),
+        loop_(GetRef<For>(loop_sref->StmtAs<ForNode>())) {}
+
+  IRModule mod_;
+  Block block_;
+  For loop_;
+};
+
+/******** Helper Functions/Classes ********/
+
+/*!
+ * \brief Find a point where the block can be inserted under the loop
+ * \tparam require_all_producers_visited Requires all producer blocks to be present under the loop
+ * \tparam require_all_consumers_visited Requires all consumer blocks to be present under the loop
+ * \param self The schedule state
+ * \param subtrees The subtrees under the loop, among which the insertion points are sought
+ * \param producer_srefs The producer blocks
+ * \param consumer_srefs The consumer blocks
+ * \param block2realize A cache that maps a block to its realize
+ * \return The last position the new block can be inserted onto, and the
+ * producer-consumer-relationship is still satisfied.
+ */
+template <bool require_all_producers_visited, bool require_all_consumers_visited>
+int FindInsertionPoint(
+    const ScheduleState& self, const Array<Stmt>& subtrees, const Array<StmtSRef>& producer_srefs,
+    const Array<StmtSRef>& consumer_srefs,
+    std::unordered_map<const BlockNode*, const BlockRealizeNode*>* block2realize) {
+  ProducerConsumerSplit split =
+      ProducerConsumerSplit::Find(self, subtrees, producer_srefs, consumer_srefs, block2realize);
+  // Step 1. Check if all the producers are visited in the subtrees, if required to
+  if (require_all_producers_visited) {
+    int num_producers = producer_srefs.size();
+    if (split.n_producers_visited < num_producers) {
+      throw NotAllRequiredBlocksAreVisitedError<false>(
+          self->mod, num_producers - split.n_producers_visited, producer_srefs);
+    }
+  }
+  // Step 2. Check if all the consumers are visited in the subtrees, if required to
+  if (require_all_consumers_visited) {
+    int num_consumers = consumer_srefs.size();
+    if (split.n_consumers_visited < num_consumers) {
+      throw NotAllRequiredBlocksAreVisitedError<true>(
+          self->mod, num_consumers - split.n_consumers_visited, consumer_srefs);
+    }
+  }
+  // Step 3. Check if there is at least one index of the position can be inserted into
+  // The valid indices are: (last_producer_position, first_consumer_position]
+  ICHECK(split.last_producer_position < split.first_consumer_position);
+  // Step 4. Return the last valid insertion point
+  return split.first_consumer_position;
+}
+
+/*!
+ * \brief A helper to reconstruct the block scope where the given block is moved under the given
+ * loop, and the given block's induced loop nest is regenerated to satisfy the required region.
+ */
+class ScopeReconstructor : private StmtMutator {
+ public:
+  explicit ScopeReconstructor(Block scope_root, Block block, For loop)
+      : scope_root_(scope_root), block_(block), loop_(loop) {}
+
+  using StmtMutator::operator();
+
+  /*!
+   * \brief Create the loop nest on top of the block, induced by the given block var's domain
+   * \param insert_position The position among the subtrees where the block and its induced loop
+   * nest is inserted
+   * \param iter_doms The domain of each block var
+   * \param preserve_unit_loops Whether to generate unit loops where the loop extent is 1
+   */
+  void MakeNewLoop(int insert_position, std::vector<Range> iter_doms, bool preserve_unit_loops) {
+    int n_iters = iter_doms.size();
+    Array<Var> loop_vars;
+    Array<PrimExpr> loop_extents;
+    Array<PrimExpr> iter_values;
+    loop_vars.reserve(n_iters);
+    loop_extents.reserve(n_iters);
+    iter_values.reserve(n_iters);
+    for (int i = 0; i < n_iters; ++i) {
+      const Range& iter_dom = iter_doms[i];
+      if (preserve_unit_loops || !is_one(iter_dom->extent)) {
+        Var var("ax" + std::to_string(loop_vars.size()), DataType::Int(32));
+        loop_vars.push_back(var);
+        loop_extents.push_back(iter_dom->extent);
+        iter_values.push_back(iter_dom->min + var);
+      } else {
+        iter_values.push_back(iter_dom->min);
+      }
+    }
+    this->new_block_realize_ =
+        BlockRealize(std::move(iter_values), const_true(), std::move(block_));
+    Stmt new_subtree = this->new_block_realize_;
+    for (int i = static_cast<int>(loop_vars.size()) - 1; i >= 0; --i) {
+      const Var& loop_var = loop_vars[i];
+      const PrimExpr& loop_extent = loop_extents[i];
+      new_subtree = For(/*loop_var=*/loop_var,
+                        /*min=*/Integer(0),
+                        /*extent=*/loop_extent,
+                        /*ForKind=*/ForKind::kSerial,
+                        /*body=*/std::move(new_subtree));
+    }
+    Array<Stmt> subtrees = AsArray(loop_->body);
+    subtrees.insert(subtrees.begin() + insert_position, std::move(new_subtree));
+    ObjectPtr<ForNode> new_loop = make_object<ForNode>(*loop_.get());
+    new_loop->body = SeqStmt(std::move(subtrees));
+    this->new_loop_ = For(std::move(new_loop));
+  }
+
+ private:
+  Stmt VisitStmt_(const BlockNode* block) final {
+    if (block != scope_root_.get()) {
+      return GetRef<Block>(block);
+    }
+    if (block == rm_src_stmt_.get()) {
+      block = TVM_TYPE_AS(block, rm_tgt_stmt_, BlockNode);
+    }
+    return StmtMutator::VisitStmt_(block);
+  }
+
+  Stmt VisitStmt_(const ForNode* loop) final {
+    if (loop == rm_src_stmt_.get()) {
+      loop = TVM_TYPE_AS(loop, rm_tgt_stmt_, ForNode);
+    }
+    if (loop == loop_.get()) {
+      return new_loop_;
+    }
+    return StmtMutator::VisitStmt_(loop);
+  }
+
+ public:
+  /*! \brief The root block of the block scope */
+  Block scope_root_;
+  /*! \brief The given block to be moved */
+  Block block_;
+  /*! \brief The given loop the block and its loop nest to be put under */
+  For loop_;
+  /*! \brief The new loop to replace the original loop */
+  For new_loop_{nullptr};
+  /*! \brief The new block realize to the moved block */
+  BlockRealize new_block_realize_{nullptr};
+  /*! \brief The plan to remove the given block by replacing this loop/block in the AST */
+  Stmt rm_src_stmt_{nullptr};
+  /*! \brief The plan to remove the given block by replacing to this loop/block in the AST */
+  Stmt rm_tgt_stmt_{nullptr};
+};
+
+/*!
+ * \brief Calculate a list of accessed buffer regions under a path of loops
+ * \tparam relax_storage_scope Whether to relax beyond the path according to the storage and
+ * execution scope
+ * \param binding The block binding, used to unbind the buffer regions
+ * \param buffer_regions The buffer regions to be calculated
+ * \param relax_path_low_inclusive The lowest point in the loop path, inclusive
+ * \param relax_path_high_exclusive The highest point in the loop path, exclusive
+ * \param relaxed Where the calculation result is stored
+ */
+template <bool relax_storage_scope>
+void RelaxBufferRegions(const Map<Var, PrimExpr>& binding,
+                        const Array<BufferRegion>& buffer_regions,
+                        const StmtSRef& relax_path_low_inclusive,
+                        const StmtSRef& relax_path_high_exclusive,
+                        std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* relaxed) {
+  runtime::StorageScope global_scope{runtime::StorageRank::kGlobal, ""};
+  // We cache the variable domains
+  runtime::StorageRank previous_rank = runtime::StorageRank::kGlobal;
+  Optional<Map<Var, arith::IntSet>> var_dom = NullOpt;
+  // Enumerate every buffer region
+  for (const BufferRegion& buffer_region : buffer_regions) {
+    const Buffer& buffer = buffer_region->buffer;
+    const Array<Range>& region = buffer_region->region;
+    // Skip the buffer regions we are not interested in
+    auto it = relaxed->find(buffer.get());
+    if (it == relaxed->end()) {
+      continue;
+    }
+    std::vector<NDIntSet>& relaxed_regions = it->second;
+    // Check and update the cached `var_dom`
+    runtime::StorageScope scope =
+        relax_storage_scope ? runtime::StorageScope::Create(buffer.scope()) : global_scope;
+    runtime::StorageRank rank = scope.rank;
+    if (rank != previous_rank || !var_dom.defined()) {
+      previous_rank = rank;
+      var_dom = AsIntSet(LoopDomainOfSRefTreePath(
+          /*low_inclusive=*/relax_path_low_inclusive,
+          /*high_exclusive=*/relax_path_high_exclusive,
+          /*extra_relax_scope=*/scope));
+    }
+    // Relax the region
+    Array<arith::IntSet> relaxed = arith::EvalSet(Substitute(region, binding), var_dom.value());
+    relaxed_regions.push_back({relaxed.begin(), relaxed.end()});
+  }
+}
+
+/*!
+ * \brief Calculate the iteration domain of a provided integer set to fully cover the required
+ * domain
+ * \param provided The provided integer set to cover the required domain
+ * \param required The required domain to be covered
+ * \param iter_doms The result iteration domains to be updated
+ * \param analyzer The arithmetic analyzer
+ */
+void UpdateBlockVarDomain(const arith::IntSet& provided, const arith::IntSet& required,
+                          std::unordered_map<const VarNode*, std::vector<arith::IntSet>>* iter_doms,
+                          arith::Analyzer* analyzer) {
+  PrimExpr provided_min = analyzer->Simplify(provided.min());
+  PrimExpr provided_extent = analyzer->Simplify(provided.max() - provided_min + 1);
+  PrimExpr required_min = analyzer->Simplify(required.min());
+  PrimExpr required_extent = analyzer->Simplify(required.max() - required_min + 1);
+  PrimExpr dom_min{nullptr}, dom_extent{nullptr};
+  Var dom_var{ObjectPtr<VarNode>{nullptr}};
+  arith::PVar<Var> p_v;
+  arith::PVar<PrimExpr> p_e;
+  if ((p_v * p_e).Match(provided_min) || (p_e * p_v).Match(provided_min)) {
+    PrimExpr e = p_e.Eval();
+    dom_var = p_v.Eval();
+    dom_min = floordiv(required_min, e);
+    dom_extent = analyzer->Simplify((required_extent + e - 1) / e);
+  } else if (analyzer->CanProveEqual(provided_extent, 1) && p_v.Match(provided_min)) {
+    dom_var = p_v.Eval();
+    dom_min = required_min;
+    dom_extent = required_extent;
+  } else {
+    ICHECK(false) << "ValueError: BufferRegion pattern match failed";
+  }
+  auto it = iter_doms->find(dom_var.get());
+  if (it != iter_doms->end()) {
+    std::vector<arith::IntSet>& doms = it->second;
+    doms.push_back(arith::IntSet::FromMinExtent(dom_min, dom_extent));
+  } else {
+    ICHECK(analyzer->CanProveEqual(provided_min, required_min));
+    ICHECK(analyzer->CanProveEqual(provided_extent, required_extent));
+  }
+}
+
+/*!
+ * \brief Calculate the domain of block vars to cover the required region
+ * \param iter_vars The list of block vars to cover the required region
+ * \param provided_regions The region provided by one iteration instance of the block vars
+ * \param required_regions The region required to be covered
+ * \param analyzer The arithmetic analyzer
+ * \return A list of iteration domain corresponding to the given list of block vars
+ */
+std::vector<Range> CalculateBlockVarDomain(
+    const Array<IterVar>& iter_vars,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>> provided_regions,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>> required_regions,
+    arith::Analyzer* analyzer) {
+  int n_iters = iter_vars.size();
+  // Step 1. Construct the mapping from block var to their iteration domain (initialized to empty)
+  std::unordered_map<const VarNode*, std::vector<arith::IntSet>> iter_doms;
+  iter_doms.reserve(n_iters);
+  for (const IterVar& iter_var : iter_vars) {
+    iter_doms[iter_var->var.get()] = {};
+  }
+  // Step 2. For each buffer, update the domain according to the provided and required regions
+  for (const auto& kv : provided_regions) {
+    const BufferNode* buffer = kv.first;
+    const std::vector<NDIntSet>& many_provided_regions = kv.second;
+    // Calculate `provided_region` and `required_region`
+    auto it = required_regions.find(buffer);
+    if (it == required_regions.end() || it->second.empty()) {
+      continue;
+    }
+    NDIntSet required_region = support::NDIntSetUnion(it->second);
+    NDIntSet provided_region = support::NDIntSetUnion(many_provided_regions);
+    ICHECK_EQ(provided_region.size(), buffer->shape.size());
+    ICHECK_EQ(required_region.size(), buffer->shape.size());
+    // For each dimension, update the iteration domain
+    int ndim = buffer->shape.size();
+    for (int i = 0; i < ndim; ++i) {
+      arith::IntSet provided = provided_region[i];
+      arith::IntSet required = required_region[i];
+      required = arith::Intersect(
+          {std::move(required), arith::IntSet::FromMinExtent(Integer(0), buffer->shape[i])});
+      UpdateBlockVarDomain(provided, required, &iter_doms, analyzer);
+    }
+  }
+  // Union the iter var domains, put them in the same order of block vars, and return
+  std::vector<Range> result;
+  result.reserve(n_iters);
+  for (const IterVar& iter_var : iter_vars) {
+    const std::vector<arith::IntSet>& doms = iter_doms.at(iter_var->var.get());
+    arith::IntSet dom = arith::IntSet::FromRange(iter_var->dom);
+    if (!doms.empty()) {
+      dom = arith::Intersect({std::move(dom), arith::Union(doms)});
+    }
+    PrimExpr min = analyzer->Simplify(dom.min());
+    PrimExpr extent = analyzer->Simplify(dom.max() - min + 1);
+    result.push_back(Range::FromMinExtent(min, extent));
+  }
+  return result;
+}
+
+/*!
+ * \brief Calculate the provided region of the given block by one single of its execution instance,
+ * as well as the required buffer regions relaxed to the given loop
+ * \tparam is_compute_at Indicates if the operation is compute-at or reverse-compute-at
+ * \param block The given block that provides buffer regions
+ * \param loop_sref The given loop under which the block is going to be moved to
+ * \param block2realize Maps a block to its corresponding BlockRealize
+ * \param producer_srefs The producers of the given block
+ * \param consumer_srefs The consumers of the given block
+ * \param provided_regions The calculated regions provided by the block
+ * \param required_regions The calculated regions required by its consumers (in compute-at) or
+ * producers (in reverse-compute-at)
+ */
+template <bool is_compute_at>
+void CalculateProvidedRequiredRegions(
+    const BlockNode* block, const StmtSRef& loop_sref,
+    std::unordered_map<const BlockNode*, const BlockRealizeNode*> block2realize,
+    Array<StmtSRef> producer_srefs, Array<StmtSRef> consumer_srefs,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* provided_regions,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* required_regions) {
+  // Step 1. Calculate the region provided by a single execution instance of `block`
+  const Array<BufferRegion>& provided_buffers = is_compute_at ? block->writes : block->reads;
+  provided_regions->reserve(provided_buffers.size());
+  required_regions->reserve(provided_buffers.size());
+  for (const BufferRegion& provided_buffer_region : provided_buffers) {
+    const BufferNode* buffer = provided_buffer_region->buffer.get();
+    const Array<Range>& region = provided_buffer_region->region;
+    (*provided_regions)[buffer].push_back(support::NDIntSetFromRegion(region));
+    (*required_regions)[buffer].clear();
+  }
+  // Step 2. Calculate the region required by dependent blocks under `loop`
+  for (const StmtSRef& required_block_sref : is_compute_at ? consumer_srefs : producer_srefs) {
+    const BlockNode* required_block = TVM_SREF_TO_BLOCK(required_block, required_block_sref);
+    ICHECK(block2realize.count(required_block));
+    RelaxBufferRegions</*relax_storage_scope=*/is_compute_at>(
+        /*binding=*/GetBindings(GetRef<BlockRealize>(block2realize.at(required_block))),
+        /*buffer_regions=*/is_compute_at ? required_block->reads : required_block->writes,
+        /*relax_path_low_inclusive=*/GetRef<StmtSRef>(required_block_sref->parent),
+        /*relax_path_high_exclusive=*/loop_sref, /*relaxed=*/required_regions);
+  }
+}
+
+/******** Main Implementation ********/
+
+template <bool is_compute_at>
+void ComputeAtOrReverseComputeAtImpl(ScheduleState self, const StmtSRef& block_sref,
+                                     const StmtSRef& loop_sref, bool preserve_unit_loops) {
+  const BlockNode* block = TVM_SREF_TO_BLOCK(block, block_sref);
+  const ForNode* loop = TVM_SREF_TO_FOR(loop, loop_sref);
+  // Step 1. Bunch of checks
+  // Check condition 1) and 2): stage pipeline and subtree compact dataflow
+  StmtSRef scope_root_sref = GetScopeRoot(self, block_sref,
+                                          /*require_stage_pipeline=*/true,
+                                          /*require_subtree_compact_dataflow=*/true);
+  Block scope_root = GetRef<Block>(scope_root_sref->StmtAs<BlockNode>());
+  BlockScope scope = self->GetBlockScope(scope_root_sref);
+  Array<StmtSRef> producer_srefs = GetProducers(block_sref, scope);
+  Array<StmtSRef> consumer_srefs = GetConsumers(block_sref, scope);
+  arith::Analyzer analyzer;
+  // Check condition 3): `block` and `loop` are under the same scope,
+  // and `loop` is not the ancestor of `block`
+  NotInSameScopeError::CheckAndBindLoopDomain(self, block_sref, loop_sref, scope_root_sref,
+                                              &analyzer);
+  // Check condition 4): `block` is not an output block
+  if (is_compute_at) {
+    CheckNotOutputBlock(self, block_sref, scope_root_sref);
+  }
+  // Step 2. Plan for the removal of `block`
+  ScopeReconstructor reconstructor(scope_root, GetRef<Block>(block), GetRef<For>(loop));
+  LeafBlockRemovalPlan(self, block_sref, &reconstructor.rm_src_stmt_, &reconstructor.rm_tgt_stmt_);
+  // Step 3. Find the insertion point under `loop`
+  // Check condition 5): all the required block are under the given loop
+  std::unordered_map<const BlockNode*, const BlockRealizeNode*> block2realize;
+  block2realize.reserve(self->block_info.size());
+  int insert_position = FindInsertionPoint<!is_compute_at, is_compute_at>(
+      /*self=*/self,
+      /*subtrees=*/AsArray(loop->body),
+      /*producer_srefs=*/producer_srefs,
+      /*consumer_srefs=*/consumer_srefs, /*block2realize=*/&block2realize);
+  // Step 4. Calculate the region provided by a single execution instance of `block`,
+  // as well as the region required by dependent blocks under `loop`

Review comment:
       I intentionally use the words `provide`/`require` instead of `produce`/`consume` to prevent you from such misunderstanding.
   
   Here is the definition:
   - In compute-at, `provide` means `produce` and `require` means `consume`
   - In reverse-compute-at, `provide` means `consume` and `require` means `produce`




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704132795



##########
File path: src/tir/schedule/concrete_schedule.cc
##########
@@ -439,6 +439,44 @@ BlockRV ConcreteScheduleNode::CacheWrite(const BlockRV& block_rv, int write_buff
 
 /******** Schedule: Compute location ********/
 
+void ConcreteScheduleNode::ComputeAt(const BlockRV& block_rv, const LoopRV& loop_rv,
+                                     bool preserve_unit_loops) {
+  static StmtSRef inline_mark = StmtSRef::InlineMark();
+  static StmtSRef root_mark = StmtSRef::RootMark();
+  StmtSRef loop_sref = this->GetSRef(loop_rv);
+  if (loop_sref.same_as(root_mark)) {
+    return;
+  } else if (loop_sref.same_as(inline_mark)) {
+    TVM_TIR_SCHEDULE_BEGIN();
+    tir::ComputeInline(state_, this->GetSRef(block_rv));
+    TVM_TIR_SCHEDULE_END("compute-at", this->error_render_level_);
+  } else {
+    TVM_TIR_SCHEDULE_BEGIN();
+    tir::ComputeAt(state_, this->GetSRef(block_rv), loop_sref, preserve_unit_loops);
+    TVM_TIR_SCHEDULE_END("compute-at", this->error_render_level_);
+  }
+  this->state_->DebugVerify();
+}
+
+void ConcreteScheduleNode::ReverseComputeAt(const BlockRV& block_rv, const LoopRV& loop_rv,
+                                            bool preserve_unit_loops) {
+  static StmtSRef inline_mark = StmtSRef::InlineMark();
+  static StmtSRef root_mark = StmtSRef::RootMark();
+  StmtSRef loop_sref = this->GetSRef(loop_rv);
+  if (loop_sref.same_as(root_mark)) {
+    // do nothing

Review comment:
       "compute-root" means do nothing - which is our definition in meta schedule




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r703888266



##########
File path: src/support/nd_int_set.h
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.
+ */
+#ifndef TVM_SUPPORT_ND_INT_SET_H_
+#define TVM_SUPPORT_ND_INT_SET_H_
+
+#include <tvm/arith/int_set.h>
+#include <tvm/ir/expr.h>
+
+#include <unordered_map>
+#include <vector>
+
+namespace tvm {
+namespace support {
+
+/*! \brief An N-dimensional integer set representing a rectangle region */
+using NDIntSet = std::vector<tvm::arith::IntSet>;
+
+/*!
+ * \brief Construct an N-dimensional integer set representing a region.
+ * \param region The region.
+ * \return constructed set.
+ */
+inline NDIntSet NDIntSetFromRegion(const tir::Region& region) {
+  NDIntSet result;
+  result.reserve(region.size());
+  for (const Range& range : region) {
+    result.push_back(arith::IntSet::FromRange(range));
+  }
+  return result;
+}
+
+/*!
+ * \brief Construct an N-dimensional integer set representing a shape.
+ * \param shape The shape which is an array of the length of each dimension.
+ * \return constructed set.
+ */
+inline NDIntSet NDIntSetFromShape(const Array<PrimExpr>& shape) {
+  PrimExpr zero = Integer(0);
+  NDIntSet result;
+  result.reserve(shape.size());
+  for (const PrimExpr& extent : shape) {
+    result.push_back(arith::IntSet::FromMinExtent(zero, extent));
+  }
+  return result;
+}
+
+/*!
+ * \brief Construct an N-dimensional integer set representing a point.
+ * \param indices The N-dimensional indices representing the point.
+ * \return constructed set.
+ */
+inline NDIntSet NDIntSetFromPoint(const Array<PrimExpr>& indices) {
+  NDIntSet result;
+  result.reserve(indices.size());
+  for (const PrimExpr& index : indices) {
+    result.push_back(arith::IntSet::SinglePoint(index));
+  }
+  return result;
+}
+
+/*!
+ * \brief Create a union set of two sets, possibly relaxed. The RHS set will be combined into the
+ *        LHS set.
+ * \param lhs The first N-dimensional integer set
+ * \param rhs The second N-dimensional integer set
+ */
+inline void NDIntSetUnionWith(NDIntSet* lhs, const NDIntSet& rhs) {
+  ICHECK_EQ(lhs->size(), rhs.size());
+  int ndim = rhs.size();
+  for (int i = 0; i < ndim; ++i) {
+    arith::IntSet& int_set = lhs->at(i);
+    int_set = arith::Union({int_set, rhs.at(i)});
+  }
+}
+
+/*!
+ * \brief Union a list of N-dimensional integer sets
+ * \param nd_int_sets The N-dimensional integer sets to be merged.
+ * \return The result of the union
+ */
+inline NDIntSet NDIntSetUnion(const std::vector<NDIntSet>& nd_int_sets) {
+  ICHECK(!nd_int_sets.empty());
+  int n = nd_int_sets.size();
+  if (n == 1) {
+    return nd_int_sets[0];
+  }
+  int ndim = nd_int_sets[0].size();
+  for (int i = 1; i < n; ++i) {
+    ICHECK_EQ(nd_int_sets[i].size(), ndim);
+  }
+  NDIntSet result;
+  result.reserve(ndim);
+  Array<arith::IntSet> int_sets(n, arith::IntSet{nullptr});
+  for (int dim = 0; dim < ndim; ++dim) {
+    for (int i = 0; i < n; ++i) {
+      int_sets.Set(i, nd_int_sets[i][dim]);
+    }
+    result.push_back(arith::Union(int_sets));
+  }
+  return result;
+}
+
+/*!
+ * \brief Create an empty N-dimensional integer set.

Review comment:
       `N-dimensional` here has already indicated that it is specific number of dimensions though




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704132041



##########
File path: src/tir/schedule/primitive/compute_at.cc
##########
@@ -0,0 +1,584 @@
+/*
+ * 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 {
+
+using support::NDIntSet;
+
+/******** Error Classes ********/
+
+/*!
+ * \brief An error raised when not all required blocks are under the given loop.
+ * \tparam is_consumer Indicates if all the required blocks are consumers or producers
+ */
+template <bool is_consumer>
+class NotAllRequiredBlocksAreVisitedError : public ScheduleError {
+ public:
+  explicit NotAllRequiredBlocksAreVisitedError(IRModule mod, int num_not_visited,
+                                               Array<StmtSRef> required)

Review comment:
       you are right




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r703829550



##########
File path: include/tvm/tir/schedule/schedule.h
##########
@@ -305,6 +305,38 @@ class ScheduleNode : public runtime::Object {
   virtual BlockRV CacheWrite(const BlockRV& block_rv, int write_buffer_index,
                              const String& storage_scope) = 0;
   /******** Schedule: Compute location ********/
+  /*!
+   * \brief Move a producer block under the specific loop, and regenerate the loops induced by the
+   * block so that the buffer region generated by the producer block could cover those regions read
+   * by the consumers. It requires:
+   * 1) The scope block has stage-pipeline property
+   * 2) The given block's subtree of the scope block satisfies compact dataflow condition.
+   * i.e. all the blocks in the scope's subtree must be either complete block or reduction block

Review comment:
       It isn't true. Both `scope` here means the scope block where `block` and `loop` belongs to.




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



[GitHub] [tvm] junrushao1994 merged pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 merged pull request #8943:
URL: https://github.com/apache/tvm/pull/8943


   


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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704731149



##########
File path: include/tvm/tir/schedule/schedule.h
##########
@@ -305,6 +305,42 @@ class ScheduleNode : public runtime::Object {
   virtual BlockRV CacheWrite(const BlockRV& block_rv, int write_buffer_index,
                              const String& storage_scope) = 0;
   /******** Schedule: Compute location ********/
+  /*!
+   * \brief Move a producer block under the specific loop, and regenerate the loops induced by the
+   * block so that the buffer region generated by the producer block could cover those regions
+   * written by the producers. It requires:
+   * 1) `block` and `loop` are under the same scope, `loop` is not the ancestor of `block`
+   * 2) The scope block has stage-pipeline property
+   * 3) The subtree of the scope block, where the given block is in, satisfies the compact dataflow
+   * condition. i.e. all the blocks in the scope block's subtree must be either complete block or
+   * reduction block
+   * 4) The block is not an output block, i.e. the buffer regions written by the block are allocated
+   * under the current scope

Review comment:
       Yeah this makes sense to me




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



[GitHub] [tvm] MasterJH5574 commented on pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
MasterJH5574 commented on pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#issuecomment-915860412


   There's another issue that I want @junrushao1994 to take a look. I've informed him privately. So we may wait for his response first.


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



[GitHub] [tvm] MasterJH5574 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
MasterJH5574 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704000464



##########
File path: src/arith/int_set.cc
##########
@@ -607,6 +607,13 @@ inline bool ProveEqual(Analyzer* analyzer, PrimExpr lhs, PrimExpr rhs) {
   return is_zero(analyzer->Simplify(lhs - rhs));
 }
 
+IntSet IntSet::FromMinExtent(PrimExpr min, PrimExpr extent) {
+  if (is_one(extent)) {
+    return IntSet::SinglePoint(min);
+  }
+  return IntervalSet(min, extent + min - 1);

Review comment:
       It concerns me that `min` is a variable and `extent` is an integer immediate. For example, `min = v0` and `extent = 8`. In this case, Can `extent + min - 1` be simplified to `v0 + 7`?

##########
File path: src/arith/int_set.cc
##########
@@ -607,6 +607,13 @@ inline bool ProveEqual(Analyzer* analyzer, PrimExpr lhs, PrimExpr rhs) {
   return is_zero(analyzer->Simplify(lhs - rhs));
 }
 
+IntSet IntSet::FromMinExtent(PrimExpr min, PrimExpr extent) {
+  if (is_one(extent)) {
+    return IntSet::SinglePoint(min);
+  }
+  return IntervalSet(min, extent + min - 1);

Review comment:
       It concerns me that `min` is a variable and `extent` is an integer immediate. For example, `min = v0` and `extent = 8`. In this case, can `extent + min - 1` be simplified to `v0 + 7`?




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704046625



##########
File path: src/arith/int_set.cc
##########
@@ -607,6 +607,13 @@ inline bool ProveEqual(Analyzer* analyzer, PrimExpr lhs, PrimExpr rhs) {
   return is_zero(analyzer->Simplify(lhs - rhs));
 }
 
+IntSet IntSet::FromMinExtent(PrimExpr min, PrimExpr extent) {
+  if (is_one(extent)) {
+    return IntSet::SinglePoint(min);
+  }
+  return IntervalSet(min, extent + min - 1);

Review comment:
       No. `extent + min - 1` won't be simplified because it is effectively `(8 + min) - 1 `




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r703888723



##########
File path: src/support/nd_int_set.h
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.
+ */
+#ifndef TVM_SUPPORT_ND_INT_SET_H_
+#define TVM_SUPPORT_ND_INT_SET_H_
+
+#include <tvm/arith/int_set.h>
+#include <tvm/ir/expr.h>
+
+#include <unordered_map>
+#include <vector>
+
+namespace tvm {
+namespace support {
+
+/*! \brief An N-dimensional integer set representing a rectangle region */
+using NDIntSet = std::vector<tvm::arith::IntSet>;
+
+/*!
+ * \brief Construct an N-dimensional integer set representing a region.
+ * \param region The region.
+ * \return constructed set.
+ */
+inline NDIntSet NDIntSetFromRegion(const tir::Region& region) {
+  NDIntSet result;
+  result.reserve(region.size());
+  for (const Range& range : region) {
+    result.push_back(arith::IntSet::FromRange(range));
+  }
+  return result;
+}
+
+/*!
+ * \brief Construct an N-dimensional integer set representing a shape.
+ * \param shape The shape which is an array of the length of each dimension.
+ * \return constructed set.
+ */
+inline NDIntSet NDIntSetFromShape(const Array<PrimExpr>& shape) {
+  PrimExpr zero = Integer(0);
+  NDIntSet result;
+  result.reserve(shape.size());
+  for (const PrimExpr& extent : shape) {
+    result.push_back(arith::IntSet::FromMinExtent(zero, extent));
+  }
+  return result;
+}
+
+/*!
+ * \brief Construct an N-dimensional integer set representing a point.
+ * \param indices The N-dimensional indices representing the point.
+ * \return constructed set.
+ */
+inline NDIntSet NDIntSetFromPoint(const Array<PrimExpr>& indices) {
+  NDIntSet result;
+  result.reserve(indices.size());
+  for (const PrimExpr& index : indices) {
+    result.push_back(arith::IntSet::SinglePoint(index));
+  }
+  return result;
+}
+
+/*!
+ * \brief Create a union set of two sets, possibly relaxed. The RHS set will be combined into the
+ *        LHS set.
+ * \param lhs The first N-dimensional integer set
+ * \param rhs The second N-dimensional integer set
+ */
+inline void NDIntSetUnionWith(NDIntSet* lhs, const NDIntSet& rhs) {
+  ICHECK_EQ(lhs->size(), rhs.size());
+  int ndim = rhs.size();
+  for (int i = 0; i < ndim; ++i) {
+    arith::IntSet& int_set = lhs->at(i);
+    int_set = arith::Union({int_set, rhs.at(i)});
+  }
+}
+
+/*!
+ * \brief Union a list of N-dimensional integer sets
+ * \param nd_int_sets The N-dimensional integer sets to be merged.
+ * \return The result of the union
+ */
+inline NDIntSet NDIntSetUnion(const std::vector<NDIntSet>& nd_int_sets) {
+  ICHECK(!nd_int_sets.empty());
+  int n = nd_int_sets.size();
+  if (n == 1) {
+    return nd_int_sets[0];
+  }
+  int ndim = nd_int_sets[0].size();
+  for (int i = 1; i < n; ++i) {
+    ICHECK_EQ(nd_int_sets[i].size(), ndim);
+  }
+  NDIntSet result;
+  result.reserve(ndim);
+  Array<arith::IntSet> int_sets(n, arith::IntSet{nullptr});
+  for (int dim = 0; dim < ndim; ++dim) {
+    for (int i = 0; i < n; ++i) {
+      int_sets.Set(i, nd_int_sets[i][dim]);
+    }
+    result.push_back(arith::Union(int_sets));
+  }
+  return result;
+}
+
+/*!
+ * \brief Create an empty N-dimensional integer set.
+ * \param ndim The number of dimensions.
+ * \return constructed set.
+ */
+inline NDIntSet NDIntSetEmpty(int ndim) {

Review comment:
       NDIntSetEmpty is better, which is the analogy to IntSet::Empty()




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704650630



##########
File path: src/tir/schedule/utils.h
##########
@@ -163,6 +165,15 @@ inline Stmt RemoveFromSeqStmt(const SeqStmt& seq, const Stmt& to_remove) {
   return SeqStmt::Flatten(new_stmts);
 }
 
+inline Array<Stmt> AsArray(const Stmt& stmt) {

Review comment:
       oh sorry it's my mistake. i forgot to doc this method




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



[GitHub] [tvm] junrushao1994 commented on pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#issuecomment-914948921


   @MasterJH5574 I fixed the wording issue on "compact dataflow" in the doc. Please take another look. Thanks!


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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704967570



##########
File path: src/tir/schedule/concrete_schedule.cc
##########
@@ -439,6 +439,44 @@ BlockRV ConcreteScheduleNode::CacheWrite(const BlockRV& block_rv, int write_buff
 
 /******** Schedule: Compute location ********/
 
+void ConcreteScheduleNode::ComputeAt(const BlockRV& block_rv, const LoopRV& loop_rv,
+                                     bool preserve_unit_loops) {
+  static StmtSRef inline_mark = StmtSRef::InlineMark();
+  static StmtSRef root_mark = StmtSRef::RootMark();
+  StmtSRef loop_sref = this->GetSRef(loop_rv);

Review comment:
       Here is how it works:
   - `SampleComputeLocation` returns `LoopRV`s that indicate which loops the current stage can be moved under. Besides, `SampleComputeLocation` provides options to "do nothing" and "do compute-inline" instead of doing compute-at;
   - If the primitive wants to instruct the system to "do nothing" or "do compute-inline" instead, it returns LoopRVs that are looked up as inline-mark or root-mark;
   - Therefore, on line 211 & 214 in GetSRef, we check if the loop_sref is inline-mark or root-mark.
   - In compute-at, it checks the return value of `GetSRef` to determine which primitive it should dispatch to
   
   > We should never ever insert InlineMark and RootMark into the symbol table, if we do, it is a bug
   
   Sorry I didn't make it clear. I mean they shouldn't be in the symbol table as keys, because they are not loop_rvs. I made it wrong previously in our private repo so I remembered this bug quite vividly lol.
   




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



[GitHub] [tvm] Hzfengsy commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
Hzfengsy commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r705019396



##########
File path: src/tir/schedule/primitive/cache_read_write.cc
##########
@@ -160,6 +160,21 @@ Block MakeCacheStage(const BufferRegion& cache_region, CacheStageInfo* info,
   return block;
 }
 
+/*!
+ * \brief Recalculate the `affine_binding` flag of the scope block info.
+ * \param block_sref The sref to the interested scope block.
+ */
+bool CalculateAffineFlag(const ScheduleState& self, const StmtSRef& block_sref) {
+  if (block_sref->parent == nullptr) {
+    return true;
+  }

Review comment:
       That's a good question. I think `affine flag` for opaque block makes no sense to me, no matter it's affine or not.
   Blocks with affine bindings have more schedule abilities than other blocks, while opaque blocks have no schedule abilities.




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



[GitHub] [tvm] MasterJH5574 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
MasterJH5574 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704561727



##########
File path: src/tir/schedule/concrete_schedule.cc
##########
@@ -439,6 +439,44 @@ BlockRV ConcreteScheduleNode::CacheWrite(const BlockRV& block_rv, int write_buff
 
 /******** Schedule: Compute location ********/
 
+void ConcreteScheduleNode::ComputeAt(const BlockRV& block_rv, const LoopRV& loop_rv,
+                                     bool preserve_unit_loops) {
+  static StmtSRef inline_mark = StmtSRef::InlineMark();
+  static StmtSRef root_mark = StmtSRef::RootMark();
+  StmtSRef loop_sref = this->GetSRef(loop_rv);

Review comment:
       Hmmm.. If they are never inserted into the symbol table, how possibly could we find them in symbol table here? https://github.com/apache/tvm/blob/b01ab9e81e6a9605e6d2dce5b0c81ce551c1839b/src/tir/schedule/concrete_schedule.h#L210-L216




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704727001



##########
File path: src/tir/schedule/primitive/compute_at.cc
##########
@@ -0,0 +1,584 @@
+/*
+ * 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 {
+
+using support::NDIntSet;
+
+/******** Error Classes ********/
+
+/*!
+ * \brief An error raised when not all required blocks are under the given loop.
+ * \tparam is_consumer Indicates if all the required blocks are consumers or producers
+ */
+template <bool is_consumer>
+class NotAllRequiredBlocksAreVisitedError : public ScheduleError {
+ public:
+  explicit NotAllRequiredBlocksAreVisitedError(IRModule mod, int num_not_visited,
+                                               const Array<StmtSRef>& required)
+      : mod_(mod), num_not_visited_(num_not_visited) {
+    required_.reserve(required.size());
+    for (const StmtSRef& block_sref : required) {
+      const BlockNode* block = TVM_SREF_TO_BLOCK(block, block_sref);
+      required_.push_back(GetRef<Block>(block));
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Not all required blocks are under the loop scope";
+  }
+
+  String DetailRenderTemplate() const final {
+    String relation = is_consumer ? "consumer(s)" : "producer(s)";
+    std::ostringstream os;
+    os << "The primitive requires all the " << relation
+       << " of the given block to be present under the target loop. However, there are "
+       << num_not_visited_ << " " << relation << " not satisfying the constraint. List of the "
+       << relation << ":";
+    for (int i = 0, n = required_.size(); i < n; ++i) {
+      os << "{" << i << "}";
+    }
+    return os.str();
+  }
+
+  IRModule mod() const final { return mod_; }
+
+  Array<ObjectRef> LocationsOfInterest() const final {
+    return {required_.begin(), required_.end()};
+  }
+
+ private:
+  IRModule mod_;
+  int num_not_visited_;
+  Array<Block> required_;
+};
+
+/*!
+ * \brief An error raised when the given block is not in the same block scope as the given loop,
+ * or the given loop is the ancestor of the given block.
+ */
+class NotInSameScopeError : public ScheduleError {
+ public:
+  static void CheckAndBindLoopDomain(const ScheduleState& self, const StmtSRef& block_sref,
+                                     const StmtSRef& loop_sref, const StmtSRef& scope_root_sref,
+                                     arith::Analyzer* analyzer) {
+    for (const StmtSRefNode* p = loop_sref.get();; p = p->parent) {
+      if (const ForNode* loop = p->StmtAs<ForNode>()) {
+        analyzer->Bind(loop->loop_var, Range::FromMinExtent(loop->min, loop->extent));
+      } else if (p != scope_root_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      } else {
+        break;
+      }
+    }
+    for (const StmtSRefNode* p = block_sref->parent; p != scope_root_sref.get(); p = p->parent) {
+      if (p == loop_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      }
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Expected the block and loop to be under the same block scope, and loop "
+           "not to be the ancestor of block";
+  }
+  String DetailRenderTemplate() const final {
+    return "ScheduleError: Expected the block {0} and loop {1} to be under the same block scope, "
+           "and loop not to be the ancestor of block";
+  }
+  IRModule mod() const final { return mod_; }
+  Array<ObjectRef> LocationsOfInterest() const final { return {block_, loop_}; }
+
+ private:
+  explicit NotInSameScopeError(IRModule mod, const StmtSRef& block_sref, const StmtSRef& loop_sref)
+      : mod_(mod),
+        block_(GetRef<Block>(block_sref->StmtAs<BlockNode>())),
+        loop_(GetRef<For>(loop_sref->StmtAs<ForNode>())) {}
+
+  IRModule mod_;
+  Block block_;
+  For loop_;
+};
+
+/******** Helper Functions/Classes ********/
+
+/*!
+ * \brief Find a point where the block can be inserted under the loop
+ * \tparam require_all_producers_visited Requires all producer blocks to be present under the loop
+ * \tparam require_all_consumers_visited Requires all consumer blocks to be present under the loop
+ * \param self The schedule state
+ * \param subtrees The subtrees under the loop, among which the insertion points are sought
+ * \param producer_srefs The producer blocks
+ * \param consumer_srefs The consumer blocks
+ * \param block2realize A cache that maps a block to its realize
+ * \return The last position the new block can be inserted onto, and the
+ * producer-consumer-relationship is still satisfied.
+ */
+template <bool require_all_producers_visited, bool require_all_consumers_visited>
+int FindInsertionPoint(
+    const ScheduleState& self, const Array<Stmt>& subtrees, const Array<StmtSRef>& producer_srefs,
+    const Array<StmtSRef>& consumer_srefs,
+    std::unordered_map<const BlockNode*, const BlockRealizeNode*>* block2realize) {
+  ProducerConsumerSplit split =
+      ProducerConsumerSplit::Find(self, subtrees, producer_srefs, consumer_srefs, block2realize);
+  // Step 1. Check if all the producers are visited in the subtrees, if required to
+  if (require_all_producers_visited) {
+    int num_producers = producer_srefs.size();
+    if (split.n_producers_visited < num_producers) {
+      throw NotAllRequiredBlocksAreVisitedError<false>(
+          self->mod, num_producers - split.n_producers_visited, producer_srefs);
+    }
+  }
+  // Step 2. Check if all the consumers are visited in the subtrees, if required to
+  if (require_all_consumers_visited) {
+    int num_consumers = consumer_srefs.size();
+    if (split.n_consumers_visited < num_consumers) {
+      throw NotAllRequiredBlocksAreVisitedError<true>(
+          self->mod, num_consumers - split.n_consumers_visited, consumer_srefs);
+    }
+  }
+  // Step 3. Check if there is at least one index of the position can be inserted into
+  // The valid indices are: (last_producer_position, first_consumer_position]
+  ICHECK(split.last_producer_position < split.first_consumer_position);
+  // Step 4. Return the last valid insertion point
+  return split.first_consumer_position;
+}
+
+/*!
+ * \brief A helper to reconstruct the block scope where the given block is moved under the given
+ * loop, and the given block's induced loop nest is regenerated to satisfy the required region.
+ */
+class ScopeReconstructor : private StmtMutator {
+ public:
+  explicit ScopeReconstructor(Block scope_root, Block block, For loop)
+      : scope_root_(scope_root), block_(block), loop_(loop) {}
+
+  using StmtMutator::operator();
+
+  /*!
+   * \brief Create the loop nest on top of the block, induced by the given block var's domain
+   * \param insert_position The position among the subtrees where the block and its induced loop
+   * nest is inserted
+   * \param iter_doms The domain of each block var
+   * \param preserve_unit_loops Whether to generate unit loops where the loop extent is 1
+   */
+  void MakeNewLoop(int insert_position, std::vector<Range> iter_doms, bool preserve_unit_loops) {
+    int n_iters = iter_doms.size();
+    Array<Var> loop_vars;
+    Array<PrimExpr> loop_extents;
+    Array<PrimExpr> iter_values;
+    loop_vars.reserve(n_iters);
+    loop_extents.reserve(n_iters);
+    iter_values.reserve(n_iters);
+    for (int i = 0; i < n_iters; ++i) {
+      const Range& iter_dom = iter_doms[i];
+      if (preserve_unit_loops || !is_one(iter_dom->extent)) {
+        Var var("ax" + std::to_string(loop_vars.size()), DataType::Int(32));
+        loop_vars.push_back(var);
+        loop_extents.push_back(iter_dom->extent);
+        iter_values.push_back(iter_dom->min + var);
+      } else {
+        iter_values.push_back(iter_dom->min);
+      }
+    }
+    this->new_block_realize_ =
+        BlockRealize(std::move(iter_values), const_true(), std::move(block_));
+    Stmt new_subtree = this->new_block_realize_;
+    for (int i = static_cast<int>(loop_vars.size()) - 1; i >= 0; --i) {
+      const Var& loop_var = loop_vars[i];
+      const PrimExpr& loop_extent = loop_extents[i];
+      new_subtree = For(/*loop_var=*/loop_var,
+                        /*min=*/Integer(0),
+                        /*extent=*/loop_extent,
+                        /*ForKind=*/ForKind::kSerial,
+                        /*body=*/std::move(new_subtree));
+    }
+    Array<Stmt> subtrees = AsArray(loop_->body);
+    subtrees.insert(subtrees.begin() + insert_position, std::move(new_subtree));
+    ObjectPtr<ForNode> new_loop = make_object<ForNode>(*loop_.get());
+    new_loop->body = SeqStmt(std::move(subtrees));
+    this->new_loop_ = For(std::move(new_loop));
+  }
+
+ private:
+  Stmt VisitStmt_(const BlockNode* block) final {
+    if (block != scope_root_.get()) {
+      return GetRef<Block>(block);
+    }
+    if (block == rm_src_stmt_.get()) {
+      block = TVM_TYPE_AS(block, rm_tgt_stmt_, BlockNode);
+    }
+    return StmtMutator::VisitStmt_(block);
+  }
+
+  Stmt VisitStmt_(const ForNode* loop) final {
+    if (loop == rm_src_stmt_.get()) {
+      loop = TVM_TYPE_AS(loop, rm_tgt_stmt_, ForNode);
+    }
+    if (loop == loop_.get()) {
+      return new_loop_;
+    }
+    return StmtMutator::VisitStmt_(loop);
+  }
+
+ public:
+  /*! \brief The root block of the block scope */
+  Block scope_root_;
+  /*! \brief The given block to be moved */
+  Block block_;
+  /*! \brief The given loop the block and its loop nest to be put under */
+  For loop_;
+  /*! \brief The new loop to replace the original loop */
+  For new_loop_{nullptr};
+  /*! \brief The new block realize to the moved block */
+  BlockRealize new_block_realize_{nullptr};
+  /*! \brief The plan to remove the given block by replacing this loop/block in the AST */
+  Stmt rm_src_stmt_{nullptr};
+  /*! \brief The plan to remove the given block by replacing to this loop/block in the AST */
+  Stmt rm_tgt_stmt_{nullptr};
+};
+
+/*!
+ * \brief Calculate a list of accessed buffer regions under a path of loops
+ * \tparam relax_storage_scope Whether to relax beyond the path according to the storage and
+ * execution scope
+ * \param binding The block binding, used to unbind the buffer regions
+ * \param buffer_regions The buffer regions to be calculated
+ * \param relax_path_low_inclusive The lowest point in the loop path, inclusive
+ * \param relax_path_high_exclusive The highest point in the loop path, exclusive
+ * \param relaxed Where the calculation result is stored
+ */
+template <bool relax_storage_scope>
+void RelaxBufferRegions(const Map<Var, PrimExpr>& binding,
+                        const Array<BufferRegion>& buffer_regions,
+                        const StmtSRef& relax_path_low_inclusive,
+                        const StmtSRef& relax_path_high_exclusive,
+                        std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* relaxed) {
+  runtime::StorageScope global_scope{runtime::StorageRank::kGlobal, ""};
+  // We cache the variable domains
+  runtime::StorageRank previous_rank = runtime::StorageRank::kGlobal;
+  Optional<Map<Var, arith::IntSet>> var_dom = NullOpt;
+  // Enumerate every buffer region
+  for (const BufferRegion& buffer_region : buffer_regions) {
+    const Buffer& buffer = buffer_region->buffer;
+    const Array<Range>& region = buffer_region->region;
+    // Skip the buffer regions we are not interested in
+    auto it = relaxed->find(buffer.get());
+    if (it == relaxed->end()) {
+      continue;
+    }
+    std::vector<NDIntSet>& relaxed_regions = it->second;
+    // Check and update the cached `var_dom`
+    runtime::StorageScope scope =
+        relax_storage_scope ? runtime::StorageScope::Create(buffer.scope()) : global_scope;
+    runtime::StorageRank rank = scope.rank;
+    if (rank != previous_rank || !var_dom.defined()) {
+      previous_rank = rank;
+      var_dom = AsIntSet(LoopDomainOfSRefTreePath(
+          /*low_inclusive=*/relax_path_low_inclusive,
+          /*high_exclusive=*/relax_path_high_exclusive,
+          /*extra_relax_scope=*/scope));
+    }
+    // Relax the region
+    Array<arith::IntSet> relaxed = arith::EvalSet(Substitute(region, binding), var_dom.value());
+    relaxed_regions.push_back({relaxed.begin(), relaxed.end()});
+  }
+}
+
+/*!
+ * \brief Calculate the iteration domain of a provided integer set to fully cover the required
+ * domain
+ * \param provided The provided integer set to cover the required domain
+ * \param required The required domain to be covered
+ * \param iter_doms The result iteration domains to be updated
+ * \param analyzer The arithmetic analyzer
+ */
+void UpdateBlockVarDomain(const arith::IntSet& provided, const arith::IntSet& required,
+                          std::unordered_map<const VarNode*, std::vector<arith::IntSet>>* iter_doms,
+                          arith::Analyzer* analyzer) {
+  PrimExpr provided_min = analyzer->Simplify(provided.min());
+  PrimExpr provided_extent = analyzer->Simplify(provided.max() - provided_min + 1);
+  PrimExpr required_min = analyzer->Simplify(required.min());
+  PrimExpr required_extent = analyzer->Simplify(required.max() - required_min + 1);
+  PrimExpr dom_min{nullptr}, dom_extent{nullptr};
+  Var dom_var{ObjectPtr<VarNode>{nullptr}};
+  arith::PVar<Var> p_v;
+  arith::PVar<PrimExpr> p_e;
+  if ((p_v * p_e).Match(provided_min) || (p_e * p_v).Match(provided_min)) {
+    PrimExpr e = p_e.Eval();
+    dom_var = p_v.Eval();
+    dom_min = floordiv(required_min, e);
+    dom_extent = analyzer->Simplify((required_extent + e - 1) / e);
+  } else if (analyzer->CanProveEqual(provided_extent, 1) && p_v.Match(provided_min)) {
+    dom_var = p_v.Eval();
+    dom_min = required_min;
+    dom_extent = required_extent;
+  } else {
+    ICHECK(false) << "ValueError: BufferRegion pattern match failed";
+  }
+  auto it = iter_doms->find(dom_var.get());
+  if (it != iter_doms->end()) {
+    std::vector<arith::IntSet>& doms = it->second;
+    doms.push_back(arith::IntSet::FromMinExtent(dom_min, dom_extent));
+  } else {
+    ICHECK(analyzer->CanProveEqual(provided_min, required_min));
+    ICHECK(analyzer->CanProveEqual(provided_extent, required_extent));
+  }
+}
+
+/*!
+ * \brief Calculate the domain of block vars to cover the required region
+ * \param iter_vars The list of block vars to cover the required region
+ * \param provided_regions The region provided by one iteration instance of the block vars
+ * \param required_regions The region required to be covered
+ * \param analyzer The arithmetic analyzer

Review comment:
       To be clear, this method doesn't mutate `analyzer`, and not binding analyzer won't cause any problem internally in this method - the method doesn't depend on any property of the analyzer.




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



[GitHub] [tvm] MasterJH5574 commented on pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
MasterJH5574 commented on pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#issuecomment-915368077


   I notice that boolean template parameters are widely used in this PR. Just curious that, what's the edge of boolean template parameters over normal boolean parameters?


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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r703894776



##########
File path: src/tir/schedule/analysis.h
##########
@@ -128,18 +138,36 @@ void CheckReductionBlock(const ScheduleState& self, const StmtSRef& block_sref,
                          const StmtSRef& scope_root_sref);
 
 /*!
- * \brief Check whether a subtree on SRef tree has compact data flow, and throw an exception if the
- * subtree does not have compact data flow
- * \details For a given StmtSRef, We say the subtree rooted from the StmtSRef has "compact data
- * flow" property if:
- * - the scope root of the input subtree root has stage-pipeline property, and
- * - all its child blocks on SRef tree are complete blocks or reduction blocks.
+ * \brief Check if the block is a complete block or a reduction block under the scope
  * \param self The schedule state
- * \param subtree_root_sref The root of the subtree to be checked in the SRef tree
- * \throw ScheduleError If the subtree does not have compact data flow
- * \sa IsCompleteBlock, IsReductionBlock
+ * \param block_sref The sref of the block to be checked
+ * \param scope_root_sref The scope root of the block
+ * \throw ScheduleError If the block is not a reduction block
+ */
+void CheckCompleteOrReductionBlock(const ScheduleState& self, const StmtSRef& block_sref,
+                                   const StmtSRef& scope_root_sref);
+
+/*!
+ * \brief Check if the block is an output block, i.e. the buffer regions written by the block are
+ * allocated under the current scope

Review comment:
       yeah the description should be the opposite: the block writes to at least one buffer that is not allocated under the current scope




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704690384



##########
File path: src/tir/schedule/analysis.h
##########
@@ -232,6 +261,55 @@ BlockRealize CheckGetSingleChildBlockRealizeOnSRefTree(const ScheduleState& self
  */
 BlockRealize GetBlockRealize(const ScheduleState& self, const StmtSRef& block_sref);
 
+/******** Producer-consumer relation ********/
+
+/*!
+ * \brief Get the producer blocks to the given block under the given scope
+ * \param block_sref The block whose producers are to be retrieved
+ * \param scope The block scope where the given block is in
+ * \return The producer blocks of the specified block
+ */
+Array<StmtSRef> GetProducers(const StmtSRef& block_sref, const BlockScope& scope);
+
+/*!
+ * \brief Get the consumer blocks to the given block under the given scope
+ * \param block_sref The block whose consumers are to be retrieved
+ * \param scope The block scope where the given block is in
+ * \return The consumer blocks of the specified block
+ */
+Array<StmtSRef> GetConsumers(const StmtSRef& block_sref, const BlockScope& scope);
+
+/*!
+ * \brief A solution to split a ordered list of subtrees into two parts,
+ * where producers are on the LHS and consumers are on the RHS.
+ * For example, subtree[0, 3) are on the LHS, and subtree[3, 6) are on the RHS.
+ */
+struct ProducerConsumerSplit {

Review comment:
       I intentionally did this because actually cache-read/write can be refactored into using this method.




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704136902



##########
File path: include/tvm/tir/schedule/schedule.h
##########
@@ -305,6 +305,38 @@ class ScheduleNode : public runtime::Object {
   virtual BlockRV CacheWrite(const BlockRV& block_rv, int write_buffer_index,
                              const String& storage_scope) = 0;
   /******** Schedule: Compute location ********/
+  /*!
+   * \brief Move a producer block under the specific loop, and regenerate the loops induced by the
+   * block so that the buffer region generated by the producer block could cover those regions read
+   * by the consumers. It requires:
+   * 1) The scope block has stage-pipeline property
+   * 2) The given block's subtree of the scope block satisfies compact dataflow condition.
+   * i.e. all the blocks in the scope's subtree must be either complete block or reduction block
+   * 3) `block` and `loop` are under the same scope, `loop` is not the ancestor of `block`
+   * 4) The block is not an output block,
+   * i.e. the buffer regions written by the block are allocated under the current scope
+   * 5) All the consumers of the block are under the given loop
+   * \param block_rv The block to be moved
+   * \param loop_rv The loop where the block to be moved under
+   * \param preserve_unit_loops Whether to keep the trivial loops whose extents are 1
+   */
+  virtual void ComputeAt(const BlockRV& block_rv, const LoopRV& loop_rv,
+                         bool preserve_unit_loops) = 0;
+  /*!
+   * \brief Move a consumer block under the specific loop, and regenerate the loops induced by the
+   * block so that the buffer region generated by the consumer block could cover those regions read
+   * by the consumers. It requires:

Review comment:
       I thought I addressed the comment? Would you like to double check?




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



[GitHub] [tvm] MasterJH5574 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
MasterJH5574 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704889315



##########
File path: src/tir/schedule/primitive/compute_at.cc
##########
@@ -0,0 +1,584 @@
+/*
+ * 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 {
+
+using support::NDIntSet;
+
+/******** Error Classes ********/
+
+/*!
+ * \brief An error raised when not all required blocks are under the given loop.
+ * \tparam is_consumer Indicates if all the required blocks are consumers or producers
+ */
+template <bool is_consumer>
+class NotAllRequiredBlocksAreVisitedError : public ScheduleError {
+ public:
+  explicit NotAllRequiredBlocksAreVisitedError(IRModule mod, int num_not_visited,
+                                               const Array<StmtSRef>& required)
+      : mod_(mod), num_not_visited_(num_not_visited) {
+    required_.reserve(required.size());
+    for (const StmtSRef& block_sref : required) {
+      const BlockNode* block = TVM_SREF_TO_BLOCK(block, block_sref);
+      required_.push_back(GetRef<Block>(block));
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Not all required blocks are under the loop scope";
+  }
+
+  String DetailRenderTemplate() const final {
+    String relation = is_consumer ? "consumer(s)" : "producer(s)";
+    std::ostringstream os;
+    os << "The primitive requires all the " << relation
+       << " of the given block to be present under the target loop. However, there are "
+       << num_not_visited_ << " " << relation << " not satisfying the constraint. List of the "
+       << relation << ":";
+    for (int i = 0, n = required_.size(); i < n; ++i) {
+      os << "{" << i << "}";
+    }
+    return os.str();
+  }
+
+  IRModule mod() const final { return mod_; }
+
+  Array<ObjectRef> LocationsOfInterest() const final {
+    return {required_.begin(), required_.end()};
+  }
+
+ private:
+  IRModule mod_;
+  int num_not_visited_;
+  Array<Block> required_;
+};
+
+/*!
+ * \brief An error raised when the given block is not in the same block scope as the given loop,
+ * or the given loop is the ancestor of the given block.
+ */
+class NotInSameScopeError : public ScheduleError {
+ public:
+  static void CheckAndBindLoopDomain(const ScheduleState& self, const StmtSRef& block_sref,
+                                     const StmtSRef& loop_sref, const StmtSRef& scope_root_sref,
+                                     arith::Analyzer* analyzer) {
+    for (const StmtSRefNode* p = loop_sref.get();; p = p->parent) {
+      if (const ForNode* loop = p->StmtAs<ForNode>()) {
+        analyzer->Bind(loop->loop_var, Range::FromMinExtent(loop->min, loop->extent));
+      } else if (p != scope_root_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      } else {
+        break;
+      }
+    }
+    for (const StmtSRefNode* p = block_sref->parent; p != scope_root_sref.get(); p = p->parent) {
+      if (p == loop_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      }
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Expected the block and loop to be under the same block scope, and loop "
+           "not to be the ancestor of block";
+  }
+  String DetailRenderTemplate() const final {
+    return "ScheduleError: Expected the block {0} and loop {1} to be under the same block scope, "
+           "and loop not to be the ancestor of block";
+  }
+  IRModule mod() const final { return mod_; }
+  Array<ObjectRef> LocationsOfInterest() const final { return {block_, loop_}; }
+
+ private:
+  explicit NotInSameScopeError(IRModule mod, const StmtSRef& block_sref, const StmtSRef& loop_sref)
+      : mod_(mod),
+        block_(GetRef<Block>(block_sref->StmtAs<BlockNode>())),
+        loop_(GetRef<For>(loop_sref->StmtAs<ForNode>())) {}
+
+  IRModule mod_;
+  Block block_;
+  For loop_;
+};
+
+/******** Helper Functions/Classes ********/
+
+/*!
+ * \brief Find a point where the block can be inserted under the loop
+ * \tparam require_all_producers_visited Requires all producer blocks to be present under the loop
+ * \tparam require_all_consumers_visited Requires all consumer blocks to be present under the loop
+ * \param self The schedule state
+ * \param subtrees The subtrees under the loop, among which the insertion points are sought
+ * \param producer_srefs The producer blocks
+ * \param consumer_srefs The consumer blocks
+ * \param block2realize A cache that maps a block to its realize
+ * \return The last position the new block can be inserted onto, and the
+ * producer-consumer-relationship is still satisfied.
+ */
+template <bool require_all_producers_visited, bool require_all_consumers_visited>
+int FindInsertionPoint(
+    const ScheduleState& self, const Array<Stmt>& subtrees, const Array<StmtSRef>& producer_srefs,
+    const Array<StmtSRef>& consumer_srefs,
+    std::unordered_map<const BlockNode*, const BlockRealizeNode*>* block2realize) {
+  ProducerConsumerSplit split =
+      ProducerConsumerSplit::Find(self, subtrees, producer_srefs, consumer_srefs, block2realize);
+  // Step 1. Check if all the producers are visited in the subtrees, if required to
+  if (require_all_producers_visited) {
+    int num_producers = producer_srefs.size();
+    if (split.n_producers_visited < num_producers) {
+      throw NotAllRequiredBlocksAreVisitedError<false>(
+          self->mod, num_producers - split.n_producers_visited, producer_srefs);
+    }
+  }
+  // Step 2. Check if all the consumers are visited in the subtrees, if required to
+  if (require_all_consumers_visited) {
+    int num_consumers = consumer_srefs.size();
+    if (split.n_consumers_visited < num_consumers) {
+      throw NotAllRequiredBlocksAreVisitedError<true>(
+          self->mod, num_consumers - split.n_consumers_visited, consumer_srefs);
+    }
+  }
+  // Step 3. Check if there is at least one index of the position can be inserted into
+  // The valid indices are: (last_producer_position, first_consumer_position]
+  ICHECK(split.last_producer_position < split.first_consumer_position);
+  // Step 4. Return the last valid insertion point
+  return split.first_consumer_position;
+}
+
+/*!
+ * \brief A helper to reconstruct the block scope where the given block is moved under the given
+ * loop, and the given block's induced loop nest is regenerated to satisfy the required region.
+ */
+class ScopeReconstructor : private StmtMutator {
+ public:
+  explicit ScopeReconstructor(Block scope_root, Block block, For loop)
+      : scope_root_(scope_root), block_(block), loop_(loop) {}
+
+  using StmtMutator::operator();
+
+  /*!
+   * \brief Create the loop nest on top of the block, induced by the given block var's domain
+   * \param insert_position The position among the subtrees where the block and its induced loop
+   * nest is inserted
+   * \param iter_doms The domain of each block var
+   * \param preserve_unit_loops Whether to generate unit loops where the loop extent is 1
+   */
+  void MakeNewLoop(int insert_position, std::vector<Range> iter_doms, bool preserve_unit_loops) {
+    int n_iters = iter_doms.size();
+    Array<Var> loop_vars;
+    Array<PrimExpr> loop_extents;
+    Array<PrimExpr> iter_values;
+    loop_vars.reserve(n_iters);
+    loop_extents.reserve(n_iters);
+    iter_values.reserve(n_iters);
+    for (int i = 0; i < n_iters; ++i) {
+      const Range& iter_dom = iter_doms[i];
+      if (preserve_unit_loops || !is_one(iter_dom->extent)) {
+        Var var("ax" + std::to_string(loop_vars.size()), DataType::Int(32));
+        loop_vars.push_back(var);
+        loop_extents.push_back(iter_dom->extent);
+        iter_values.push_back(iter_dom->min + var);
+      } else {
+        iter_values.push_back(iter_dom->min);
+      }
+    }
+    this->new_block_realize_ =
+        BlockRealize(std::move(iter_values), const_true(), std::move(block_));
+    Stmt new_subtree = this->new_block_realize_;
+    for (int i = static_cast<int>(loop_vars.size()) - 1; i >= 0; --i) {
+      const Var& loop_var = loop_vars[i];
+      const PrimExpr& loop_extent = loop_extents[i];
+      new_subtree = For(/*loop_var=*/loop_var,
+                        /*min=*/Integer(0),
+                        /*extent=*/loop_extent,
+                        /*ForKind=*/ForKind::kSerial,
+                        /*body=*/std::move(new_subtree));
+    }
+    Array<Stmt> subtrees = AsArray(loop_->body);
+    subtrees.insert(subtrees.begin() + insert_position, std::move(new_subtree));
+    ObjectPtr<ForNode> new_loop = make_object<ForNode>(*loop_.get());
+    new_loop->body = SeqStmt(std::move(subtrees));
+    this->new_loop_ = For(std::move(new_loop));
+  }
+
+ private:
+  Stmt VisitStmt_(const BlockNode* block) final {
+    if (block != scope_root_.get()) {
+      return GetRef<Block>(block);
+    }
+    if (block == rm_src_stmt_.get()) {
+      block = TVM_TYPE_AS(block, rm_tgt_stmt_, BlockNode);
+    }
+    return StmtMutator::VisitStmt_(block);
+  }
+
+  Stmt VisitStmt_(const ForNode* loop) final {
+    if (loop == rm_src_stmt_.get()) {
+      loop = TVM_TYPE_AS(loop, rm_tgt_stmt_, ForNode);
+    }
+    if (loop == loop_.get()) {
+      return new_loop_;
+    }
+    return StmtMutator::VisitStmt_(loop);
+  }
+
+ public:
+  /*! \brief The root block of the block scope */
+  Block scope_root_;
+  /*! \brief The given block to be moved */
+  Block block_;
+  /*! \brief The given loop the block and its loop nest to be put under */
+  For loop_;
+  /*! \brief The new loop to replace the original loop */
+  For new_loop_{nullptr};
+  /*! \brief The new block realize to the moved block */
+  BlockRealize new_block_realize_{nullptr};
+  /*! \brief The plan to remove the given block by replacing this loop/block in the AST */
+  Stmt rm_src_stmt_{nullptr};
+  /*! \brief The plan to remove the given block by replacing to this loop/block in the AST */
+  Stmt rm_tgt_stmt_{nullptr};
+};
+
+/*!
+ * \brief Calculate a list of accessed buffer regions under a path of loops
+ * \tparam relax_storage_scope Whether to relax beyond the path according to the storage and
+ * execution scope
+ * \param binding The block binding, used to unbind the buffer regions
+ * \param buffer_regions The buffer regions to be calculated
+ * \param relax_path_low_inclusive The lowest point in the loop path, inclusive
+ * \param relax_path_high_exclusive The highest point in the loop path, exclusive
+ * \param relaxed Where the calculation result is stored
+ */
+template <bool relax_storage_scope>
+void RelaxBufferRegions(const Map<Var, PrimExpr>& binding,
+                        const Array<BufferRegion>& buffer_regions,
+                        const StmtSRef& relax_path_low_inclusive,
+                        const StmtSRef& relax_path_high_exclusive,
+                        std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* relaxed) {
+  runtime::StorageScope global_scope{runtime::StorageRank::kGlobal, ""};
+  // We cache the variable domains
+  runtime::StorageRank previous_rank = runtime::StorageRank::kGlobal;
+  Optional<Map<Var, arith::IntSet>> var_dom = NullOpt;
+  // Enumerate every buffer region
+  for (const BufferRegion& buffer_region : buffer_regions) {
+    const Buffer& buffer = buffer_region->buffer;
+    const Array<Range>& region = buffer_region->region;
+    // Skip the buffer regions we are not interested in
+    auto it = relaxed->find(buffer.get());
+    if (it == relaxed->end()) {
+      continue;
+    }
+    std::vector<NDIntSet>& relaxed_regions = it->second;
+    // Check and update the cached `var_dom`
+    runtime::StorageScope scope =
+        relax_storage_scope ? runtime::StorageScope::Create(buffer.scope()) : global_scope;
+    runtime::StorageRank rank = scope.rank;
+    if (rank != previous_rank || !var_dom.defined()) {
+      previous_rank = rank;
+      var_dom = AsIntSet(LoopDomainOfSRefTreePath(
+          /*low_inclusive=*/relax_path_low_inclusive,
+          /*high_exclusive=*/relax_path_high_exclusive,
+          /*extra_relax_scope=*/scope));
+    }
+    // Relax the region
+    Array<arith::IntSet> relaxed = arith::EvalSet(Substitute(region, binding), var_dom.value());
+    relaxed_regions.push_back({relaxed.begin(), relaxed.end()});
+  }
+}
+
+/*!
+ * \brief Calculate the iteration domain of a provided integer set to fully cover the required
+ * domain
+ * \param provided The provided integer set to cover the required domain
+ * \param required The required domain to be covered
+ * \param iter_doms The result iteration domains to be updated
+ * \param analyzer The arithmetic analyzer
+ */
+void UpdateBlockVarDomain(const arith::IntSet& provided, const arith::IntSet& required,
+                          std::unordered_map<const VarNode*, std::vector<arith::IntSet>>* iter_doms,
+                          arith::Analyzer* analyzer) {
+  PrimExpr provided_min = analyzer->Simplify(provided.min());
+  PrimExpr provided_extent = analyzer->Simplify(provided.max() - provided_min + 1);
+  PrimExpr required_min = analyzer->Simplify(required.min());
+  PrimExpr required_extent = analyzer->Simplify(required.max() - required_min + 1);
+  PrimExpr dom_min{nullptr}, dom_extent{nullptr};
+  Var dom_var{ObjectPtr<VarNode>{nullptr}};
+  arith::PVar<Var> p_v;
+  arith::PVar<PrimExpr> p_e;
+  if ((p_v * p_e).Match(provided_min) || (p_e * p_v).Match(provided_min)) {
+    PrimExpr e = p_e.Eval();
+    dom_var = p_v.Eval();
+    dom_min = floordiv(required_min, e);
+    dom_extent = analyzer->Simplify((required_extent + e - 1) / e);
+  } else if (analyzer->CanProveEqual(provided_extent, 1) && p_v.Match(provided_min)) {
+    dom_var = p_v.Eval();
+    dom_min = required_min;
+    dom_extent = required_extent;
+  } else {
+    ICHECK(false) << "ValueError: BufferRegion pattern match failed";
+  }
+  auto it = iter_doms->find(dom_var.get());
+  if (it != iter_doms->end()) {
+    std::vector<arith::IntSet>& doms = it->second;
+    doms.push_back(arith::IntSet::FromMinExtent(dom_min, dom_extent));
+  } else {
+    ICHECK(analyzer->CanProveEqual(provided_min, required_min));
+    ICHECK(analyzer->CanProveEqual(provided_extent, required_extent));
+  }
+}
+
+/*!
+ * \brief Calculate the domain of block vars to cover the required region
+ * \param iter_vars The list of block vars to cover the required region
+ * \param provided_regions The region provided by one iteration instance of the block vars
+ * \param required_regions The region required to be covered
+ * \param analyzer The arithmetic analyzer
+ * \return A list of iteration domain corresponding to the given list of block vars
+ */
+std::vector<Range> CalculateBlockVarDomain(
+    const Array<IterVar>& iter_vars,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>> provided_regions,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>> required_regions,
+    arith::Analyzer* analyzer) {
+  int n_iters = iter_vars.size();
+  // Step 1. Construct the mapping from block var to their iteration domain (initialized to empty)
+  std::unordered_map<const VarNode*, std::vector<arith::IntSet>> iter_doms;
+  iter_doms.reserve(n_iters);
+  for (const IterVar& iter_var : iter_vars) {
+    iter_doms[iter_var->var.get()] = {};
+  }
+  // Step 2. For each buffer, update the domain according to the provided and required regions
+  for (const auto& kv : provided_regions) {
+    const BufferNode* buffer = kv.first;
+    const std::vector<NDIntSet>& many_provided_regions = kv.second;
+    // Calculate `provided_region` and `required_region`
+    auto it = required_regions.find(buffer);
+    if (it == required_regions.end() || it->second.empty()) {
+      continue;
+    }
+    NDIntSet required_region = support::NDIntSetUnion(it->second);
+    NDIntSet provided_region = support::NDIntSetUnion(many_provided_regions);
+    ICHECK_EQ(provided_region.size(), buffer->shape.size());
+    ICHECK_EQ(required_region.size(), buffer->shape.size());
+    // For each dimension, update the iteration domain
+    int ndim = buffer->shape.size();
+    for (int i = 0; i < ndim; ++i) {
+      arith::IntSet provided = provided_region[i];
+      arith::IntSet required = required_region[i];
+      required = arith::Intersect(
+          {std::move(required), arith::IntSet::FromMinExtent(Integer(0), buffer->shape[i])});
+      UpdateBlockVarDomain(provided, required, &iter_doms, analyzer);
+    }
+  }
+  // Union the iter var domains, put them in the same order of block vars, and return
+  std::vector<Range> result;
+  result.reserve(n_iters);
+  for (const IterVar& iter_var : iter_vars) {
+    const std::vector<arith::IntSet>& doms = iter_doms.at(iter_var->var.get());
+    arith::IntSet dom = arith::IntSet::FromRange(iter_var->dom);
+    if (!doms.empty()) {
+      dom = arith::Intersect({std::move(dom), arith::Union(doms)});
+    }
+    PrimExpr min = analyzer->Simplify(dom.min());
+    PrimExpr extent = analyzer->Simplify(dom.max() - min + 1);
+    result.push_back(Range::FromMinExtent(min, extent));
+  }
+  return result;
+}
+
+/*!
+ * \brief Calculate the provided region of the given block by one single of its execution instance,
+ * as well as the required buffer regions relaxed to the given loop
+ * \tparam is_compute_at Indicates if the operation is compute-at or reverse-compute-at
+ * \param block The given block that provides buffer regions
+ * \param loop_sref The given loop under which the block is going to be moved to
+ * \param block2realize Maps a block to its corresponding BlockRealize
+ * \param producer_srefs The producers of the given block
+ * \param consumer_srefs The consumers of the given block
+ * \param provided_regions The calculated regions provided by the block
+ * \param required_regions The calculated regions required by its consumers (in compute-at) or
+ * producers (in reverse-compute-at)
+ */
+template <bool is_compute_at>
+void CalculateProvidedRequiredRegions(
+    const BlockNode* block, const StmtSRef& loop_sref,
+    std::unordered_map<const BlockNode*, const BlockRealizeNode*> block2realize,
+    Array<StmtSRef> producer_srefs, Array<StmtSRef> consumer_srefs,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* provided_regions,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* required_regions) {
+  // Step 1. Calculate the region provided by a single execution instance of `block`
+  const Array<BufferRegion>& provided_buffers = is_compute_at ? block->writes : block->reads;
+  provided_regions->reserve(provided_buffers.size());
+  required_regions->reserve(provided_buffers.size());
+  for (const BufferRegion& provided_buffer_region : provided_buffers) {
+    const BufferNode* buffer = provided_buffer_region->buffer.get();
+    const Array<Range>& region = provided_buffer_region->region;
+    (*provided_regions)[buffer].push_back(support::NDIntSetFromRegion(region));
+    (*required_regions)[buffer].clear();
+  }
+  // Step 2. Calculate the region required by dependent blocks under `loop`
+  for (const StmtSRef& required_block_sref : is_compute_at ? consumer_srefs : producer_srefs) {
+    const BlockNode* required_block = TVM_SREF_TO_BLOCK(required_block, required_block_sref);
+    ICHECK(block2realize.count(required_block));
+    RelaxBufferRegions</*relax_storage_scope=*/is_compute_at>(
+        /*binding=*/GetBindings(GetRef<BlockRealize>(block2realize.at(required_block))),
+        /*buffer_regions=*/is_compute_at ? required_block->reads : required_block->writes,
+        /*relax_path_low_inclusive=*/GetRef<StmtSRef>(required_block_sref->parent),
+        /*relax_path_high_exclusive=*/loop_sref, /*relaxed=*/required_regions);
+  }
+}
+
+/******** Main Implementation ********/
+
+template <bool is_compute_at>
+void ComputeAtOrReverseComputeAtImpl(ScheduleState self, const StmtSRef& block_sref,
+                                     const StmtSRef& loop_sref, bool preserve_unit_loops) {
+  const BlockNode* block = TVM_SREF_TO_BLOCK(block, block_sref);
+  const ForNode* loop = TVM_SREF_TO_FOR(loop, loop_sref);
+  // Step 1. Bunch of checks
+  // Check condition 1) and 2): stage pipeline and subtree compact dataflow
+  StmtSRef scope_root_sref = GetScopeRoot(self, block_sref,
+                                          /*require_stage_pipeline=*/true,
+                                          /*require_subtree_compact_dataflow=*/true);
+  Block scope_root = GetRef<Block>(scope_root_sref->StmtAs<BlockNode>());
+  BlockScope scope = self->GetBlockScope(scope_root_sref);
+  Array<StmtSRef> producer_srefs = GetProducers(block_sref, scope);
+  Array<StmtSRef> consumer_srefs = GetConsumers(block_sref, scope);
+  arith::Analyzer analyzer;
+  // Check condition 3): `block` and `loop` are under the same scope,
+  // and `loop` is not the ancestor of `block`
+  NotInSameScopeError::CheckAndBindLoopDomain(self, block_sref, loop_sref, scope_root_sref,
+                                              &analyzer);
+  // Check condition 4): `block` is not an output block
+  if (is_compute_at) {
+    CheckNotOutputBlock(self, block_sref, scope_root_sref);
+  }
+  // Step 2. Plan for the removal of `block`
+  ScopeReconstructor reconstructor(scope_root, GetRef<Block>(block), GetRef<For>(loop));
+  LeafBlockRemovalPlan(self, block_sref, &reconstructor.rm_src_stmt_, &reconstructor.rm_tgt_stmt_);
+  // Step 3. Find the insertion point under `loop`
+  // Check condition 5): all the required block are under the given loop
+  std::unordered_map<const BlockNode*, const BlockRealizeNode*> block2realize;
+  block2realize.reserve(self->block_info.size());
+  int insert_position = FindInsertionPoint<!is_compute_at, is_compute_at>(
+      /*self=*/self,
+      /*subtrees=*/AsArray(loop->body),
+      /*producer_srefs=*/producer_srefs,
+      /*consumer_srefs=*/consumer_srefs, /*block2realize=*/&block2realize);
+  // Step 4. Calculate the region provided by a single execution instance of `block`,
+  // as well as the region required by dependent blocks under `loop`

Review comment:
       Yeah I see the new document. Thanks!




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r705561926



##########
File path: src/tir/schedule/primitive/compute_at.cc
##########
@@ -0,0 +1,584 @@
+/*
+ * 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 {
+
+using support::NDIntSet;
+
+/******** Error Classes ********/
+
+/*!
+ * \brief An error raised when not all required blocks are under the given loop.
+ * \tparam is_consumer Indicates if all the required blocks are consumers or producers
+ */
+template <bool is_consumer>
+class NotAllRequiredBlocksAreVisitedError : public ScheduleError {
+ public:
+  explicit NotAllRequiredBlocksAreVisitedError(IRModule mod, int num_not_visited,
+                                               const Array<StmtSRef>& required)
+      : mod_(mod), num_not_visited_(num_not_visited) {
+    required_.reserve(required.size());
+    for (const StmtSRef& block_sref : required) {
+      const BlockNode* block = TVM_SREF_TO_BLOCK(block, block_sref);
+      required_.push_back(GetRef<Block>(block));
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Not all required blocks are under the loop scope";
+  }
+
+  String DetailRenderTemplate() const final {
+    String relation = is_consumer ? "consumer(s)" : "producer(s)";
+    std::ostringstream os;
+    os << "The primitive requires all the " << relation
+       << " of the given block to be present under the target loop. However, there are "
+       << num_not_visited_ << " " << relation << " not satisfying the constraint. List of the "
+       << relation << ":";
+    for (int i = 0, n = required_.size(); i < n; ++i) {
+      os << "{" << i << "}";
+    }
+    return os.str();
+  }
+
+  IRModule mod() const final { return mod_; }
+
+  Array<ObjectRef> LocationsOfInterest() const final {
+    return {required_.begin(), required_.end()};
+  }
+
+ private:
+  IRModule mod_;
+  int num_not_visited_;
+  Array<Block> required_;
+};
+
+/*!
+ * \brief An error raised when the given block is not in the same block scope as the given loop,
+ * or the given loop is the ancestor of the given block.
+ */
+class NotInSameScopeError : public ScheduleError {
+ public:
+  static void CheckAndBindLoopDomain(const ScheduleState& self, const StmtSRef& block_sref,
+                                     const StmtSRef& loop_sref, const StmtSRef& scope_root_sref,
+                                     arith::Analyzer* analyzer) {
+    for (const StmtSRefNode* p = loop_sref.get();; p = p->parent) {
+      if (const ForNode* loop = p->StmtAs<ForNode>()) {
+        analyzer->Bind(loop->loop_var, Range::FromMinExtent(loop->min, loop->extent));
+      } else if (p != scope_root_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      } else {
+        break;
+      }
+    }
+    for (const StmtSRefNode* p = block_sref->parent; p != scope_root_sref.get(); p = p->parent) {
+      if (p == loop_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      }
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Expected the block and loop to be under the same block scope, and loop "
+           "not to be the ancestor of block";
+  }
+  String DetailRenderTemplate() const final {
+    return "ScheduleError: Expected the block {0} and loop {1} to be under the same block scope, "
+           "and loop not to be the ancestor of block";
+  }
+  IRModule mod() const final { return mod_; }
+  Array<ObjectRef> LocationsOfInterest() const final { return {block_, loop_}; }
+
+ private:
+  explicit NotInSameScopeError(IRModule mod, const StmtSRef& block_sref, const StmtSRef& loop_sref)
+      : mod_(mod),
+        block_(GetRef<Block>(block_sref->StmtAs<BlockNode>())),
+        loop_(GetRef<For>(loop_sref->StmtAs<ForNode>())) {}
+
+  IRModule mod_;
+  Block block_;
+  For loop_;
+};
+
+/******** Helper Functions/Classes ********/
+
+/*!
+ * \brief Find a point where the block can be inserted under the loop
+ * \tparam require_all_producers_visited Requires all producer blocks to be present under the loop
+ * \tparam require_all_consumers_visited Requires all consumer blocks to be present under the loop
+ * \param self The schedule state
+ * \param subtrees The subtrees under the loop, among which the insertion points are sought
+ * \param producer_srefs The producer blocks
+ * \param consumer_srefs The consumer blocks
+ * \param block2realize A cache that maps a block to its realize
+ * \return The last position the new block can be inserted onto, and the
+ * producer-consumer-relationship is still satisfied.
+ */
+template <bool require_all_producers_visited, bool require_all_consumers_visited>
+int FindInsertionPoint(
+    const ScheduleState& self, const Array<Stmt>& subtrees, const Array<StmtSRef>& producer_srefs,
+    const Array<StmtSRef>& consumer_srefs,
+    std::unordered_map<const BlockNode*, const BlockRealizeNode*>* block2realize) {
+  ProducerConsumerSplit split =
+      ProducerConsumerSplit::Find(self, subtrees, producer_srefs, consumer_srefs, block2realize);
+  // Step 1. Check if all the producers are visited in the subtrees, if required to
+  if (require_all_producers_visited) {
+    int num_producers = producer_srefs.size();
+    if (split.n_producers_visited < num_producers) {
+      throw NotAllRequiredBlocksAreVisitedError<false>(
+          self->mod, num_producers - split.n_producers_visited, producer_srefs);
+    }
+  }
+  // Step 2. Check if all the consumers are visited in the subtrees, if required to
+  if (require_all_consumers_visited) {
+    int num_consumers = consumer_srefs.size();
+    if (split.n_consumers_visited < num_consumers) {
+      throw NotAllRequiredBlocksAreVisitedError<true>(
+          self->mod, num_consumers - split.n_consumers_visited, consumer_srefs);
+    }
+  }
+  // Step 3. Check if there is at least one index of the position can be inserted into
+  // The valid indices are: (last_producer_position, first_consumer_position]
+  ICHECK(split.last_producer_position < split.first_consumer_position);
+  // Step 4. Return the last valid insertion point
+  return split.first_consumer_position;
+}
+
+/*!
+ * \brief A helper to reconstruct the block scope where the given block is moved under the given
+ * loop, and the given block's induced loop nest is regenerated to satisfy the required region.
+ */
+class ScopeReconstructor : private StmtMutator {
+ public:
+  explicit ScopeReconstructor(Block scope_root, Block block, For loop)
+      : scope_root_(scope_root), block_(block), loop_(loop) {}
+
+  using StmtMutator::operator();
+
+  /*!
+   * \brief Create the loop nest on top of the block, induced by the given block var's domain
+   * \param insert_position The position among the subtrees where the block and its induced loop
+   * nest is inserted
+   * \param iter_doms The domain of each block var
+   * \param preserve_unit_loops Whether to generate unit loops where the loop extent is 1
+   */
+  void MakeNewLoop(int insert_position, std::vector<Range> iter_doms, bool preserve_unit_loops) {
+    int n_iters = iter_doms.size();
+    Array<Var> loop_vars;
+    Array<PrimExpr> loop_extents;
+    Array<PrimExpr> iter_values;
+    loop_vars.reserve(n_iters);
+    loop_extents.reserve(n_iters);
+    iter_values.reserve(n_iters);
+    for (int i = 0; i < n_iters; ++i) {
+      const Range& iter_dom = iter_doms[i];
+      if (preserve_unit_loops || !is_one(iter_dom->extent)) {
+        Var var("ax" + std::to_string(loop_vars.size()), DataType::Int(32));
+        loop_vars.push_back(var);
+        loop_extents.push_back(iter_dom->extent);
+        iter_values.push_back(iter_dom->min + var);
+      } else {
+        iter_values.push_back(iter_dom->min);
+      }
+    }
+    this->new_block_realize_ =
+        BlockRealize(std::move(iter_values), const_true(), std::move(block_));
+    Stmt new_subtree = this->new_block_realize_;
+    for (int i = static_cast<int>(loop_vars.size()) - 1; i >= 0; --i) {
+      const Var& loop_var = loop_vars[i];
+      const PrimExpr& loop_extent = loop_extents[i];
+      new_subtree = For(/*loop_var=*/loop_var,
+                        /*min=*/Integer(0),
+                        /*extent=*/loop_extent,
+                        /*ForKind=*/ForKind::kSerial,
+                        /*body=*/std::move(new_subtree));
+    }
+    Array<Stmt> subtrees = AsArray(loop_->body);
+    subtrees.insert(subtrees.begin() + insert_position, std::move(new_subtree));
+    ObjectPtr<ForNode> new_loop = make_object<ForNode>(*loop_.get());
+    new_loop->body = SeqStmt(std::move(subtrees));
+    this->new_loop_ = For(std::move(new_loop));
+  }
+
+ private:
+  Stmt VisitStmt_(const BlockNode* block) final {
+    if (block != scope_root_.get()) {
+      return GetRef<Block>(block);
+    }
+    if (block == rm_src_stmt_.get()) {
+      block = TVM_TYPE_AS(block, rm_tgt_stmt_, BlockNode);
+    }
+    return StmtMutator::VisitStmt_(block);
+  }
+
+  Stmt VisitStmt_(const ForNode* loop) final {
+    if (loop == rm_src_stmt_.get()) {
+      loop = TVM_TYPE_AS(loop, rm_tgt_stmt_, ForNode);
+    }
+    if (loop == loop_.get()) {
+      return new_loop_;
+    }
+    return StmtMutator::VisitStmt_(loop);
+  }
+
+ public:
+  /*! \brief The root block of the block scope */
+  Block scope_root_;
+  /*! \brief The given block to be moved */
+  Block block_;
+  /*! \brief The given loop the block and its loop nest to be put under */
+  For loop_;
+  /*! \brief The new loop to replace the original loop */
+  For new_loop_{nullptr};
+  /*! \brief The new block realize to the moved block */
+  BlockRealize new_block_realize_{nullptr};
+  /*! \brief The plan to remove the given block by replacing this loop/block in the AST */
+  Stmt rm_src_stmt_{nullptr};
+  /*! \brief The plan to remove the given block by replacing to this loop/block in the AST */
+  Stmt rm_tgt_stmt_{nullptr};
+};
+
+/*!
+ * \brief Calculate a list of accessed buffer regions under a path of loops
+ * \tparam relax_storage_scope Whether to relax beyond the path according to the storage and
+ * execution scope
+ * \param binding The block binding, used to unbind the buffer regions
+ * \param buffer_regions The buffer regions to be calculated
+ * \param relax_path_low_inclusive The lowest point in the loop path, inclusive
+ * \param relax_path_high_exclusive The highest point in the loop path, exclusive
+ * \param relaxed Where the calculation result is stored
+ */
+template <bool relax_storage_scope>
+void RelaxBufferRegions(const Map<Var, PrimExpr>& binding,
+                        const Array<BufferRegion>& buffer_regions,
+                        const StmtSRef& relax_path_low_inclusive,
+                        const StmtSRef& relax_path_high_exclusive,
+                        std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* relaxed) {
+  runtime::StorageScope global_scope{runtime::StorageRank::kGlobal, ""};
+  // We cache the variable domains
+  runtime::StorageRank previous_rank = runtime::StorageRank::kGlobal;
+  Optional<Map<Var, arith::IntSet>> var_dom = NullOpt;
+  // Enumerate every buffer region
+  for (const BufferRegion& buffer_region : buffer_regions) {
+    const Buffer& buffer = buffer_region->buffer;
+    const Array<Range>& region = buffer_region->region;
+    // Skip the buffer regions we are not interested in
+    auto it = relaxed->find(buffer.get());
+    if (it == relaxed->end()) {
+      continue;
+    }
+    std::vector<NDIntSet>& relaxed_regions = it->second;
+    // Check and update the cached `var_dom`
+    runtime::StorageScope scope =
+        relax_storage_scope ? runtime::StorageScope::Create(buffer.scope()) : global_scope;
+    runtime::StorageRank rank = scope.rank;
+    if (rank != previous_rank || !var_dom.defined()) {
+      previous_rank = rank;
+      var_dom = AsIntSet(LoopDomainOfSRefTreePath(
+          /*low_inclusive=*/relax_path_low_inclusive,
+          /*high_exclusive=*/relax_path_high_exclusive,
+          /*extra_relax_scope=*/scope));
+    }
+    // Relax the region
+    Array<arith::IntSet> relaxed = arith::EvalSet(Substitute(region, binding), var_dom.value());
+    relaxed_regions.push_back({relaxed.begin(), relaxed.end()});
+  }
+}
+
+/*!
+ * \brief Calculate the iteration domain of a provided integer set to fully cover the required
+ * domain
+ * \param provided The provided integer set to cover the required domain
+ * \param required The required domain to be covered
+ * \param iter_doms The result iteration domains to be updated
+ * \param analyzer The arithmetic analyzer
+ */
+void UpdateBlockVarDomain(const arith::IntSet& provided, const arith::IntSet& required,
+                          std::unordered_map<const VarNode*, std::vector<arith::IntSet>>* iter_doms,
+                          arith::Analyzer* analyzer) {
+  PrimExpr provided_min = analyzer->Simplify(provided.min());
+  PrimExpr provided_extent = analyzer->Simplify(provided.max() - provided_min + 1);
+  PrimExpr required_min = analyzer->Simplify(required.min());
+  PrimExpr required_extent = analyzer->Simplify(required.max() - required_min + 1);
+  PrimExpr dom_min{nullptr}, dom_extent{nullptr};
+  Var dom_var{ObjectPtr<VarNode>{nullptr}};
+  arith::PVar<Var> p_v;
+  arith::PVar<PrimExpr> p_e;
+  if ((p_v * p_e).Match(provided_min) || (p_e * p_v).Match(provided_min)) {
+    PrimExpr e = p_e.Eval();
+    dom_var = p_v.Eval();
+    dom_min = floordiv(required_min, e);
+    dom_extent = analyzer->Simplify((required_extent + e - 1) / e);
+  } else if (analyzer->CanProveEqual(provided_extent, 1) && p_v.Match(provided_min)) {
+    dom_var = p_v.Eval();
+    dom_min = required_min;
+    dom_extent = required_extent;
+  } else {
+    ICHECK(false) << "ValueError: BufferRegion pattern match failed";
+  }
+  auto it = iter_doms->find(dom_var.get());
+  if (it != iter_doms->end()) {
+    std::vector<arith::IntSet>& doms = it->second;
+    doms.push_back(arith::IntSet::FromMinExtent(dom_min, dom_extent));
+  } else {
+    ICHECK(analyzer->CanProveEqual(provided_min, required_min));
+    ICHECK(analyzer->CanProveEqual(provided_extent, required_extent));
+  }
+}
+
+/*!
+ * \brief Calculate the domain of block vars to cover the required region
+ * \param iter_vars The list of block vars to cover the required region
+ * \param provided_regions The region provided by one iteration instance of the block vars
+ * \param required_regions The region required to be covered
+ * \param analyzer The arithmetic analyzer
+ * \return A list of iteration domain corresponding to the given list of block vars
+ */
+std::vector<Range> CalculateBlockVarDomain(
+    const Array<IterVar>& iter_vars,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>> provided_regions,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>> required_regions,
+    arith::Analyzer* analyzer) {
+  int n_iters = iter_vars.size();
+  // Step 1. Construct the mapping from block var to their iteration domain (initialized to empty)
+  std::unordered_map<const VarNode*, std::vector<arith::IntSet>> iter_doms;
+  iter_doms.reserve(n_iters);
+  for (const IterVar& iter_var : iter_vars) {
+    iter_doms[iter_var->var.get()] = {};
+  }
+  // Step 2. For each buffer, update the domain according to the provided and required regions
+  for (const auto& kv : provided_regions) {
+    const BufferNode* buffer = kv.first;
+    const std::vector<NDIntSet>& many_provided_regions = kv.second;
+    // Calculate `provided_region` and `required_region`
+    auto it = required_regions.find(buffer);
+    if (it == required_regions.end() || it->second.empty()) {
+      continue;
+    }
+    NDIntSet required_region = support::NDIntSetUnion(it->second);
+    NDIntSet provided_region = support::NDIntSetUnion(many_provided_regions);
+    ICHECK_EQ(provided_region.size(), buffer->shape.size());
+    ICHECK_EQ(required_region.size(), buffer->shape.size());
+    // For each dimension, update the iteration domain
+    int ndim = buffer->shape.size();
+    for (int i = 0; i < ndim; ++i) {
+      arith::IntSet provided = provided_region[i];
+      arith::IntSet required = required_region[i];
+      required = arith::Intersect(
+          {std::move(required), arith::IntSet::FromMinExtent(Integer(0), buffer->shape[i])});
+      UpdateBlockVarDomain(provided, required, &iter_doms, analyzer);
+    }
+  }
+  // Union the iter var domains, put them in the same order of block vars, and return
+  std::vector<Range> result;
+  result.reserve(n_iters);
+  for (const IterVar& iter_var : iter_vars) {
+    const std::vector<arith::IntSet>& doms = iter_doms.at(iter_var->var.get());
+    arith::IntSet dom = arith::IntSet::FromRange(iter_var->dom);
+    if (!doms.empty()) {
+      dom = arith::Intersect({std::move(dom), arith::Union(doms)});
+    }
+    PrimExpr min = analyzer->Simplify(dom.min());
+    PrimExpr extent = analyzer->Simplify(dom.max() - min + 1);
+    result.push_back(Range::FromMinExtent(min, extent));
+  }
+  return result;
+}
+
+/*!
+ * \brief Calculate the provided region of the given block by one single of its execution instance,
+ * as well as the required buffer regions relaxed to the given loop
+ * \tparam is_compute_at Indicates if the operation is compute-at or reverse-compute-at
+ * \param block The given block that provides buffer regions
+ * \param loop_sref The given loop under which the block is going to be moved to
+ * \param block2realize Maps a block to its corresponding BlockRealize
+ * \param producer_srefs The producers of the given block
+ * \param consumer_srefs The consumers of the given block
+ * \param provided_regions The calculated regions provided by the block
+ * \param required_regions The calculated regions required by its consumers (in compute-at) or
+ * producers (in reverse-compute-at)
+ */
+template <bool is_compute_at>
+void CalculateProvidedRequiredRegions(
+    const BlockNode* block, const StmtSRef& loop_sref,
+    std::unordered_map<const BlockNode*, const BlockRealizeNode*> block2realize,
+    Array<StmtSRef> producer_srefs, Array<StmtSRef> consumer_srefs,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* provided_regions,
+    std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* required_regions) {
+  // Step 1. Calculate the region provided by a single execution instance of `block`
+  const Array<BufferRegion>& provided_buffers = is_compute_at ? block->writes : block->reads;
+  provided_regions->reserve(provided_buffers.size());
+  required_regions->reserve(provided_buffers.size());
+  for (const BufferRegion& provided_buffer_region : provided_buffers) {
+    const BufferNode* buffer = provided_buffer_region->buffer.get();
+    const Array<Range>& region = provided_buffer_region->region;
+    (*provided_regions)[buffer].push_back(support::NDIntSetFromRegion(region));
+    (*required_regions)[buffer].clear();

Review comment:
       Note that here on line 365 we only create a vector once, but on line 434, it is possible that a buffer is inserted multiple times




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



[GitHub] [tvm] MasterJH5574 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
MasterJH5574 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704094627



##########
File path: src/tir/schedule/primitive/cache_read_write.cc
##########
@@ -160,6 +160,21 @@ Block MakeCacheStage(const BufferRegion& cache_region, CacheStageInfo* info,
   return block;
 }
 
+/*!
+ * \brief Recalculate the `affine_binding` flag of a specifc block
+ * \param block_sref The sref to the specific block
+ */
+bool CalculateAffineFlag(const ScheduleState& self, const StmtSRef& block_sref) {

Review comment:
       This recalculator is probably used by primitive DecomposeReduction, so does it make sense to move it into `schedule/analysis.h`?

##########
File path: src/tir/schedule/analysis.h
##########
@@ -128,18 +138,36 @@ void CheckReductionBlock(const ScheduleState& self, const StmtSRef& block_sref,
                          const StmtSRef& scope_root_sref);
 
 /*!
- * \brief Check whether a subtree on SRef tree has compact data flow, and throw an exception if the
- * subtree does not have compact data flow
- * \details For a given StmtSRef, We say the subtree rooted from the StmtSRef has "compact data
- * flow" property if:
- * - the scope root of the input subtree root has stage-pipeline property, and
- * - all its child blocks on SRef tree are complete blocks or reduction blocks.
+ * \brief Check if the block is a complete block or a reduction block under the scope
  * \param self The schedule state
- * \param subtree_root_sref The root of the subtree to be checked in the SRef tree
- * \throw ScheduleError If the subtree does not have compact data flow
- * \sa IsCompleteBlock, IsReductionBlock
+ * \param block_sref The sref of the block to be checked
+ * \param scope_root_sref The scope root of the block
+ * \throw ScheduleError If the block is neither a complete block nor a reduction block
+ */
+void CheckCompleteOrReductionBlock(const ScheduleState& self, const StmtSRef& block_sref,
+                                   const StmtSRef& scope_root_sref);
+
+/*!
+ * \brief Check if the block is an output block, i.e. the block writes to at least a buffer that is
+ * not allocated under the current scope
+ * \param self The schedule state
+ * \param block_sref The block to be checked
+ * \param scope_root_sref The scope root of the block
+ * \return A boolean flag indicating if the block is an output block
+ */
+bool IsOutputBlock(const ScheduleState& self, const StmtSRef& block_sref,
+                   const StmtSRef& scope_root_sref);
+
+/*!
+ * \brief Check if the block is not an output block, i.e. all the buffers the block writes to
+ * are allocated under the current scope

Review comment:
       We should also update this brief doc to make it the same as `IsOutputBlock`'s.

##########
File path: src/tir/schedule/concrete_schedule.cc
##########
@@ -439,6 +439,44 @@ BlockRV ConcreteScheduleNode::CacheWrite(const BlockRV& block_rv, int write_buff
 
 /******** Schedule: Compute location ********/
 
+void ConcreteScheduleNode::ComputeAt(const BlockRV& block_rv, const LoopRV& loop_rv,
+                                     bool preserve_unit_loops) {
+  static StmtSRef inline_mark = StmtSRef::InlineMark();
+  static StmtSRef root_mark = StmtSRef::RootMark();
+  StmtSRef loop_sref = this->GetSRef(loop_rv);

Review comment:
       Sorry that I couldn't find the place where `inline_mark` and `root_mark` are inserted into `symbol_table_`. Could you help point it out?

##########
File path: src/tir/schedule/primitive/compute_at.cc
##########
@@ -0,0 +1,584 @@
+/*
+ * 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 {
+
+using support::NDIntSet;
+
+/******** Error Classes ********/
+
+/*!
+ * \brief An error raised when not all required blocks are under the given loop.
+ * \tparam is_consumer Indicates if all the required blocks are consumers or producers
+ */
+template <bool is_consumer>
+class NotAllRequiredBlocksAreVisitedError : public ScheduleError {
+ public:
+  explicit NotAllRequiredBlocksAreVisitedError(IRModule mod, int num_not_visited,
+                                               Array<StmtSRef> required)

Review comment:
       It seems better to make the parameter a constant reference 🤔?
   ```suggestion
                                                  const Array<StmtSRef>& required)
   ```

##########
File path: src/tir/schedule/primitive/compute_at.cc
##########
@@ -0,0 +1,584 @@
+/*
+ * 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 {
+
+using support::NDIntSet;
+
+/******** Error Classes ********/
+
+/*!
+ * \brief An error raised when not all required blocks are under the given loop.
+ * \tparam is_consumer Indicates if all the required blocks are consumers or producers
+ */
+template <bool is_consumer>
+class NotAllRequiredBlocksAreVisitedError : public ScheduleError {
+ public:
+  explicit NotAllRequiredBlocksAreVisitedError(IRModule mod, int num_not_visited,
+                                               Array<StmtSRef> required)
+      : mod_(mod), num_not_visited_(num_not_visited) {
+    required_.reserve(required.size());
+    for (const StmtSRef& block_sref : required) {
+      const BlockNode* block = TVM_SREF_TO_BLOCK(block, block_sref);
+      required_.push_back(GetRef<Block>(block));
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Not all required blocks are under the loop scope";
+  }
+
+  String DetailRenderTemplate() const final {
+    String relation = is_consumer ? "consumer(s)" : "producer(s)";
+    std::ostringstream os;
+    os << "The primitive requires all the " << relation
+       << " of the given block to be present under the target loop. However, there are "
+       << num_not_visited_ << " " << relation
+       << " not satisfying the constraint. List of the producers:";

Review comment:
       ```suggestion
          << " not satisfying the constraint. List of the " << relation << ":";
   ```

##########
File path: include/tvm/tir/schedule/schedule.h
##########
@@ -305,6 +305,38 @@ class ScheduleNode : public runtime::Object {
   virtual BlockRV CacheWrite(const BlockRV& block_rv, int write_buffer_index,
                              const String& storage_scope) = 0;
   /******** Schedule: Compute location ********/
+  /*!
+   * \brief Move a producer block under the specific loop, and regenerate the loops induced by the
+   * block so that the buffer region generated by the producer block could cover those regions read
+   * by the consumers. It requires:
+   * 1) The scope block has stage-pipeline property
+   * 2) The given block's subtree of the scope block satisfies compact dataflow condition.
+   * i.e. all the blocks in the scope's subtree must be either complete block or reduction block
+   * 3) `block` and `loop` are under the same scope, `loop` is not the ancestor of `block`
+   * 4) The block is not an output block,
+   * i.e. the buffer regions written by the block are allocated under the current scope
+   * 5) All the consumers of the block are under the given loop
+   * \param block_rv The block to be moved
+   * \param loop_rv The loop where the block to be moved under
+   * \param preserve_unit_loops Whether to keep the trivial loops whose extents are 1
+   */
+  virtual void ComputeAt(const BlockRV& block_rv, const LoopRV& loop_rv,
+                         bool preserve_unit_loops) = 0;
+  /*!
+   * \brief Move a consumer block under the specific loop, and regenerate the loops induced by the
+   * block so that the buffer region generated by the consumer block could cover those regions read
+   * by the consumers. It requires:

Review comment:
       :eyes:

##########
File path: src/tir/schedule/concrete_schedule.cc
##########
@@ -439,6 +439,44 @@ BlockRV ConcreteScheduleNode::CacheWrite(const BlockRV& block_rv, int write_buff
 
 /******** Schedule: Compute location ********/
 
+void ConcreteScheduleNode::ComputeAt(const BlockRV& block_rv, const LoopRV& loop_rv,
+                                     bool preserve_unit_loops) {
+  static StmtSRef inline_mark = StmtSRef::InlineMark();
+  static StmtSRef root_mark = StmtSRef::RootMark();
+  StmtSRef loop_sref = this->GetSRef(loop_rv);
+  if (loop_sref.same_as(root_mark)) {
+    return;
+  } else if (loop_sref.same_as(inline_mark)) {
+    TVM_TIR_SCHEDULE_BEGIN();
+    tir::ComputeInline(state_, this->GetSRef(block_rv));
+    TVM_TIR_SCHEDULE_END("compute-at", this->error_render_level_);
+  } else {
+    TVM_TIR_SCHEDULE_BEGIN();
+    tir::ComputeAt(state_, this->GetSRef(block_rv), loop_sref, preserve_unit_loops);
+    TVM_TIR_SCHEDULE_END("compute-at", this->error_render_level_);
+  }
+  this->state_->DebugVerify();
+}
+
+void ConcreteScheduleNode::ReverseComputeAt(const BlockRV& block_rv, const LoopRV& loop_rv,
+                                            bool preserve_unit_loops) {
+  static StmtSRef inline_mark = StmtSRef::InlineMark();
+  static StmtSRef root_mark = StmtSRef::RootMark();
+  StmtSRef loop_sref = this->GetSRef(loop_rv);
+  if (loop_sref.same_as(root_mark)) {
+    // do nothing

Review comment:
       Why we do nothing here, and directly return at line 448?




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



[GitHub] [tvm] MasterJH5574 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
MasterJH5574 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704557259



##########
File path: src/tir/schedule/concrete_schedule.cc
##########
@@ -439,6 +439,44 @@ BlockRV ConcreteScheduleNode::CacheWrite(const BlockRV& block_rv, int write_buff
 
 /******** Schedule: Compute location ********/
 
+void ConcreteScheduleNode::ComputeAt(const BlockRV& block_rv, const LoopRV& loop_rv,
+                                     bool preserve_unit_loops) {
+  static StmtSRef inline_mark = StmtSRef::InlineMark();
+  static StmtSRef root_mark = StmtSRef::RootMark();
+  StmtSRef loop_sref = this->GetSRef(loop_rv);
+  if (loop_sref.same_as(root_mark)) {
+    return;
+  } else if (loop_sref.same_as(inline_mark)) {
+    TVM_TIR_SCHEDULE_BEGIN();
+    tir::ComputeInline(state_, this->GetSRef(block_rv));
+    TVM_TIR_SCHEDULE_END("compute-at", this->error_render_level_);
+  } else {
+    TVM_TIR_SCHEDULE_BEGIN();
+    tir::ComputeAt(state_, this->GetSRef(block_rv), loop_sref, preserve_unit_loops);
+    TVM_TIR_SCHEDULE_END("compute-at", this->error_render_level_);
+  }
+  this->state_->DebugVerify();
+}
+
+void ConcreteScheduleNode::ReverseComputeAt(const BlockRV& block_rv, const LoopRV& loop_rv,
+                                            bool preserve_unit_loops) {
+  static StmtSRef inline_mark = StmtSRef::InlineMark();
+  static StmtSRef root_mark = StmtSRef::RootMark();
+  StmtSRef loop_sref = this->GetSRef(loop_rv);
+  if (loop_sref.same_as(root_mark)) {
+    // do nothing

Review comment:
       Yes I agree that there isn't much difference. My point is just that whether we should make them consistent or not.




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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704133431



##########
File path: src/tir/schedule/concrete_schedule.cc
##########
@@ -439,6 +439,44 @@ BlockRV ConcreteScheduleNode::CacheWrite(const BlockRV& block_rv, int write_buff
 
 /******** Schedule: Compute location ********/
 
+void ConcreteScheduleNode::ComputeAt(const BlockRV& block_rv, const LoopRV& loop_rv,
+                                     bool preserve_unit_loops) {
+  static StmtSRef inline_mark = StmtSRef::InlineMark();
+  static StmtSRef root_mark = StmtSRef::RootMark();
+  StmtSRef loop_sref = this->GetSRef(loop_rv);
+  if (loop_sref.same_as(root_mark)) {
+    return;
+  } else if (loop_sref.same_as(inline_mark)) {
+    TVM_TIR_SCHEDULE_BEGIN();
+    tir::ComputeInline(state_, this->GetSRef(block_rv));
+    TVM_TIR_SCHEDULE_END("compute-at", this->error_render_level_);
+  } else {
+    TVM_TIR_SCHEDULE_BEGIN();
+    tir::ComputeAt(state_, this->GetSRef(block_rv), loop_sref, preserve_unit_loops);
+    TVM_TIR_SCHEDULE_END("compute-at", this->error_render_level_);
+  }
+  this->state_->DebugVerify();
+}
+
+void ConcreteScheduleNode::ReverseComputeAt(const BlockRV& block_rv, const LoopRV& loop_rv,
+                                            bool preserve_unit_loops) {
+  static StmtSRef inline_mark = StmtSRef::InlineMark();
+  static StmtSRef root_mark = StmtSRef::RootMark();
+  StmtSRef loop_sref = this->GetSRef(loop_rv);
+  if (loop_sref.same_as(root_mark)) {
+    // do nothing

Review comment:
       i don't think there is much difference between directly returning vs going through DebugVerify, which is effectively no-op in non debug mode




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



[GitHub] [tvm] MasterJH5574 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
MasterJH5574 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704556035



##########
File path: src/tir/schedule/analysis.h
##########
@@ -128,18 +138,36 @@ void CheckReductionBlock(const ScheduleState& self, const StmtSRef& block_sref,
                          const StmtSRef& scope_root_sref);
 
 /*!
- * \brief Check whether a subtree on SRef tree has compact data flow, and throw an exception if the
- * subtree does not have compact data flow
- * \details For a given StmtSRef, We say the subtree rooted from the StmtSRef has "compact data
- * flow" property if:
- * - the scope root of the input subtree root has stage-pipeline property, and
- * - all its child blocks on SRef tree are complete blocks or reduction blocks.
+ * \brief Check if the block is a complete block or a reduction block under the scope
  * \param self The schedule state
- * \param subtree_root_sref The root of the subtree to be checked in the SRef tree
- * \throw ScheduleError If the subtree does not have compact data flow
- * \sa IsCompleteBlock, IsReductionBlock
+ * \param block_sref The sref of the block to be checked
+ * \param scope_root_sref The scope root of the block
+ * \throw ScheduleError If the block is neither a complete block nor a reduction block
+ */
+void CheckCompleteOrReductionBlock(const ScheduleState& self, const StmtSRef& block_sref,
+                                   const StmtSRef& scope_root_sref);
+
+/*!
+ * \brief Check if the block is an output block, i.e. the block writes to at least a buffer that is
+ * not allocated under the current scope
+ * \param self The schedule state
+ * \param block_sref The block to be checked
+ * \param scope_root_sref The scope root of the block
+ * \return A boolean flag indicating if the block is an output block
+ */
+bool IsOutputBlock(const ScheduleState& self, const StmtSRef& block_sref,
+                   const StmtSRef& scope_root_sref);
+
+/*!
+ * \brief Check if the block is not an output block, i.e. all the buffers the block writes to
+ * are allocated under the current scope

Review comment:
       Yes you're right. I didn't see it clearly, sorry.




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



[GitHub] [tvm] junrushao1994 commented on pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#issuecomment-915530243


   @MasterJH5574 So the template thing here basically does compile time code generation to ensure it is zero overhead 


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



[GitHub] [tvm] junrushao1994 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r704692077



##########
File path: src/tir/schedule/primitive/compute_at.cc
##########
@@ -0,0 +1,584 @@
+/*
+ * 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 {
+
+using support::NDIntSet;
+
+/******** Error Classes ********/
+
+/*!
+ * \brief An error raised when not all required blocks are under the given loop.
+ * \tparam is_consumer Indicates if all the required blocks are consumers or producers
+ */
+template <bool is_consumer>
+class NotAllRequiredBlocksAreVisitedError : public ScheduleError {
+ public:
+  explicit NotAllRequiredBlocksAreVisitedError(IRModule mod, int num_not_visited,
+                                               const Array<StmtSRef>& required)
+      : mod_(mod), num_not_visited_(num_not_visited) {
+    required_.reserve(required.size());
+    for (const StmtSRef& block_sref : required) {
+      const BlockNode* block = TVM_SREF_TO_BLOCK(block, block_sref);
+      required_.push_back(GetRef<Block>(block));
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Not all required blocks are under the loop scope";
+  }
+
+  String DetailRenderTemplate() const final {
+    String relation = is_consumer ? "consumer(s)" : "producer(s)";
+    std::ostringstream os;
+    os << "The primitive requires all the " << relation
+       << " of the given block to be present under the target loop. However, there are "
+       << num_not_visited_ << " " << relation << " not satisfying the constraint. List of the "
+       << relation << ":";
+    for (int i = 0, n = required_.size(); i < n; ++i) {
+      os << "{" << i << "}";
+    }
+    return os.str();
+  }
+
+  IRModule mod() const final { return mod_; }
+
+  Array<ObjectRef> LocationsOfInterest() const final {
+    return {required_.begin(), required_.end()};
+  }
+
+ private:
+  IRModule mod_;
+  int num_not_visited_;
+  Array<Block> required_;
+};
+
+/*!
+ * \brief An error raised when the given block is not in the same block scope as the given loop,
+ * or the given loop is the ancestor of the given block.
+ */
+class NotInSameScopeError : public ScheduleError {
+ public:
+  static void CheckAndBindLoopDomain(const ScheduleState& self, const StmtSRef& block_sref,
+                                     const StmtSRef& loop_sref, const StmtSRef& scope_root_sref,
+                                     arith::Analyzer* analyzer) {
+    for (const StmtSRefNode* p = loop_sref.get();; p = p->parent) {
+      if (const ForNode* loop = p->StmtAs<ForNode>()) {
+        analyzer->Bind(loop->loop_var, Range::FromMinExtent(loop->min, loop->extent));
+      } else if (p != scope_root_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      } else {
+        break;
+      }
+    }
+    for (const StmtSRefNode* p = block_sref->parent; p != scope_root_sref.get(); p = p->parent) {
+      if (p == loop_sref.get()) {
+        throw NotInSameScopeError(self->mod, block_sref, loop_sref);
+      }
+    }
+  }
+
+  String FastErrorString() const final {
+    return "ScheduleError: Expected the block and loop to be under the same block scope, and loop "
+           "not to be the ancestor of block";
+  }
+  String DetailRenderTemplate() const final {
+    return "ScheduleError: Expected the block {0} and loop {1} to be under the same block scope, "
+           "and loop not to be the ancestor of block";
+  }
+  IRModule mod() const final { return mod_; }
+  Array<ObjectRef> LocationsOfInterest() const final { return {block_, loop_}; }
+
+ private:
+  explicit NotInSameScopeError(IRModule mod, const StmtSRef& block_sref, const StmtSRef& loop_sref)
+      : mod_(mod),
+        block_(GetRef<Block>(block_sref->StmtAs<BlockNode>())),
+        loop_(GetRef<For>(loop_sref->StmtAs<ForNode>())) {}
+
+  IRModule mod_;
+  Block block_;
+  For loop_;
+};
+
+/******** Helper Functions/Classes ********/
+
+/*!
+ * \brief Find a point where the block can be inserted under the loop
+ * \tparam require_all_producers_visited Requires all producer blocks to be present under the loop
+ * \tparam require_all_consumers_visited Requires all consumer blocks to be present under the loop
+ * \param self The schedule state
+ * \param subtrees The subtrees under the loop, among which the insertion points are sought
+ * \param producer_srefs The producer blocks
+ * \param consumer_srefs The consumer blocks
+ * \param block2realize A cache that maps a block to its realize
+ * \return The last position the new block can be inserted onto, and the
+ * producer-consumer-relationship is still satisfied.
+ */
+template <bool require_all_producers_visited, bool require_all_consumers_visited>
+int FindInsertionPoint(
+    const ScheduleState& self, const Array<Stmt>& subtrees, const Array<StmtSRef>& producer_srefs,
+    const Array<StmtSRef>& consumer_srefs,
+    std::unordered_map<const BlockNode*, const BlockRealizeNode*>* block2realize) {
+  ProducerConsumerSplit split =
+      ProducerConsumerSplit::Find(self, subtrees, producer_srefs, consumer_srefs, block2realize);
+  // Step 1. Check if all the producers are visited in the subtrees, if required to
+  if (require_all_producers_visited) {
+    int num_producers = producer_srefs.size();
+    if (split.n_producers_visited < num_producers) {
+      throw NotAllRequiredBlocksAreVisitedError<false>(
+          self->mod, num_producers - split.n_producers_visited, producer_srefs);
+    }
+  }
+  // Step 2. Check if all the consumers are visited in the subtrees, if required to
+  if (require_all_consumers_visited) {
+    int num_consumers = consumer_srefs.size();
+    if (split.n_consumers_visited < num_consumers) {
+      throw NotAllRequiredBlocksAreVisitedError<true>(
+          self->mod, num_consumers - split.n_consumers_visited, consumer_srefs);
+    }
+  }
+  // Step 3. Check if there is at least one index of the position can be inserted into
+  // The valid indices are: (last_producer_position, first_consumer_position]
+  ICHECK(split.last_producer_position < split.first_consumer_position);
+  // Step 4. Return the last valid insertion point
+  return split.first_consumer_position;
+}
+
+/*!
+ * \brief A helper to reconstruct the block scope where the given block is moved under the given
+ * loop, and the given block's induced loop nest is regenerated to satisfy the required region.
+ */
+class ScopeReconstructor : private StmtMutator {
+ public:
+  explicit ScopeReconstructor(Block scope_root, Block block, For loop)
+      : scope_root_(scope_root), block_(block), loop_(loop) {}
+
+  using StmtMutator::operator();
+
+  /*!
+   * \brief Create the loop nest on top of the block, induced by the given block var's domain
+   * \param insert_position The position among the subtrees where the block and its induced loop
+   * nest is inserted
+   * \param iter_doms The domain of each block var
+   * \param preserve_unit_loops Whether to generate unit loops where the loop extent is 1
+   */
+  void MakeNewLoop(int insert_position, std::vector<Range> iter_doms, bool preserve_unit_loops) {
+    int n_iters = iter_doms.size();
+    Array<Var> loop_vars;
+    Array<PrimExpr> loop_extents;
+    Array<PrimExpr> iter_values;
+    loop_vars.reserve(n_iters);
+    loop_extents.reserve(n_iters);
+    iter_values.reserve(n_iters);
+    for (int i = 0; i < n_iters; ++i) {
+      const Range& iter_dom = iter_doms[i];
+      if (preserve_unit_loops || !is_one(iter_dom->extent)) {
+        Var var("ax" + std::to_string(loop_vars.size()), DataType::Int(32));
+        loop_vars.push_back(var);
+        loop_extents.push_back(iter_dom->extent);
+        iter_values.push_back(iter_dom->min + var);
+      } else {
+        iter_values.push_back(iter_dom->min);
+      }
+    }
+    this->new_block_realize_ =
+        BlockRealize(std::move(iter_values), const_true(), std::move(block_));
+    Stmt new_subtree = this->new_block_realize_;
+    for (int i = static_cast<int>(loop_vars.size()) - 1; i >= 0; --i) {
+      const Var& loop_var = loop_vars[i];
+      const PrimExpr& loop_extent = loop_extents[i];
+      new_subtree = For(/*loop_var=*/loop_var,
+                        /*min=*/Integer(0),
+                        /*extent=*/loop_extent,
+                        /*ForKind=*/ForKind::kSerial,
+                        /*body=*/std::move(new_subtree));
+    }
+    Array<Stmt> subtrees = AsArray(loop_->body);
+    subtrees.insert(subtrees.begin() + insert_position, std::move(new_subtree));
+    ObjectPtr<ForNode> new_loop = make_object<ForNode>(*loop_.get());
+    new_loop->body = SeqStmt(std::move(subtrees));
+    this->new_loop_ = For(std::move(new_loop));
+  }
+
+ private:
+  Stmt VisitStmt_(const BlockNode* block) final {
+    if (block != scope_root_.get()) {
+      return GetRef<Block>(block);
+    }
+    if (block == rm_src_stmt_.get()) {
+      block = TVM_TYPE_AS(block, rm_tgt_stmt_, BlockNode);
+    }
+    return StmtMutator::VisitStmt_(block);
+  }
+
+  Stmt VisitStmt_(const ForNode* loop) final {
+    if (loop == rm_src_stmt_.get()) {
+      loop = TVM_TYPE_AS(loop, rm_tgt_stmt_, ForNode);
+    }
+    if (loop == loop_.get()) {
+      return new_loop_;
+    }
+    return StmtMutator::VisitStmt_(loop);
+  }
+
+ public:
+  /*! \brief The root block of the block scope */
+  Block scope_root_;
+  /*! \brief The given block to be moved */
+  Block block_;
+  /*! \brief The given loop the block and its loop nest to be put under */
+  For loop_;
+  /*! \brief The new loop to replace the original loop */
+  For new_loop_{nullptr};
+  /*! \brief The new block realize to the moved block */
+  BlockRealize new_block_realize_{nullptr};
+  /*! \brief The plan to remove the given block by replacing this loop/block in the AST */
+  Stmt rm_src_stmt_{nullptr};
+  /*! \brief The plan to remove the given block by replacing to this loop/block in the AST */
+  Stmt rm_tgt_stmt_{nullptr};
+};
+
+/*!
+ * \brief Calculate a list of accessed buffer regions under a path of loops
+ * \tparam relax_storage_scope Whether to relax beyond the path according to the storage and
+ * execution scope
+ * \param binding The block binding, used to unbind the buffer regions
+ * \param buffer_regions The buffer regions to be calculated
+ * \param relax_path_low_inclusive The lowest point in the loop path, inclusive
+ * \param relax_path_high_exclusive The highest point in the loop path, exclusive
+ * \param relaxed Where the calculation result is stored
+ */
+template <bool relax_storage_scope>
+void RelaxBufferRegions(const Map<Var, PrimExpr>& binding,
+                        const Array<BufferRegion>& buffer_regions,
+                        const StmtSRef& relax_path_low_inclusive,
+                        const StmtSRef& relax_path_high_exclusive,
+                        std::unordered_map<const BufferNode*, std::vector<NDIntSet>>* relaxed) {
+  runtime::StorageScope global_scope{runtime::StorageRank::kGlobal, ""};
+  // We cache the variable domains
+  runtime::StorageRank previous_rank = runtime::StorageRank::kGlobal;
+  Optional<Map<Var, arith::IntSet>> var_dom = NullOpt;
+  // Enumerate every buffer region
+  for (const BufferRegion& buffer_region : buffer_regions) {
+    const Buffer& buffer = buffer_region->buffer;
+    const Array<Range>& region = buffer_region->region;
+    // Skip the buffer regions we are not interested in
+    auto it = relaxed->find(buffer.get());
+    if (it == relaxed->end()) {
+      continue;
+    }
+    std::vector<NDIntSet>& relaxed_regions = it->second;
+    // Check and update the cached `var_dom`
+    runtime::StorageScope scope =
+        relax_storage_scope ? runtime::StorageScope::Create(buffer.scope()) : global_scope;
+    runtime::StorageRank rank = scope.rank;
+    if (rank != previous_rank || !var_dom.defined()) {
+      previous_rank = rank;
+      var_dom = AsIntSet(LoopDomainOfSRefTreePath(
+          /*low_inclusive=*/relax_path_low_inclusive,
+          /*high_exclusive=*/relax_path_high_exclusive,
+          /*extra_relax_scope=*/scope));
+    }
+    // Relax the region
+    Array<arith::IntSet> relaxed = arith::EvalSet(Substitute(region, binding), var_dom.value());

Review comment:
       good idea




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



[GitHub] [tvm] MasterJH5574 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
MasterJH5574 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r703530318



##########
File path: include/tvm/tir/schedule/schedule.h
##########
@@ -305,6 +305,38 @@ class ScheduleNode : public runtime::Object {
   virtual BlockRV CacheWrite(const BlockRV& block_rv, int write_buffer_index,
                              const String& storage_scope) = 0;
   /******** Schedule: Compute location ********/
+  /*!
+   * \brief Move a producer block under the specific loop, and regenerate the loops induced by the
+   * block so that the buffer region generated by the producer block could cover those regions read
+   * by the consumers. It requires:
+   * 1) The scope block has stage-pipeline property
+   * 2) The given block's subtree of the scope block satisfies compact dataflow condition.
+   * i.e. all the blocks in the scope's subtree must be either complete block or reduction block
+   * 3) `block` and `loop` are under the same scope, `loop` is not the ancestor of `block`

Review comment:
       I think moving condition 3 above condition 1 looks more natural, because in condition 3 we tell that "`block` and `loop` are under a same scope", and then in condition 1 we tell that "the scope block should have the stage-pipeline property". What do you think?

##########
File path: include/tvm/arith/int_set.h
##########
@@ -121,6 +121,13 @@ class IntSet : public ObjectRef {
    * \return The result set containing the indices in the vector.
    */
   static IntSet Vector(PrimExpr vec);
+  /*!
+   * \brief Construct a set representing a range [min, min + extent).
+   * \param min The minimum of the range range
+   * \param extent The extent of the range.
+   * \return constructed set.

Review comment:
       ```suggestion
      * \return The constructed set.
   ```

##########
File path: src/arith/int_set.cc
##########
@@ -607,6 +607,13 @@ inline bool ProveEqual(Analyzer* analyzer, PrimExpr lhs, PrimExpr rhs) {
   return is_zero(analyzer->Simplify(lhs - rhs));
 }
 
+IntSet IntSet::FromMinExtent(PrimExpr min, PrimExpr extent) {
+  if (is_one(extent)) {
+    return IntSet::SinglePoint(min);
+  }
+  return IntervalSet(min, extent + min - 1);

Review comment:
       Is there any specific reason of writing `extent + min - 1` instead of `min + extent - 1`?

##########
File path: include/tvm/tir/schedule/schedule.h
##########
@@ -305,6 +305,38 @@ class ScheduleNode : public runtime::Object {
   virtual BlockRV CacheWrite(const BlockRV& block_rv, int write_buffer_index,
                              const String& storage_scope) = 0;
   /******** Schedule: Compute location ********/
+  /*!
+   * \brief Move a producer block under the specific loop, and regenerate the loops induced by the
+   * block so that the buffer region generated by the producer block could cover those regions read
+   * by the consumers. It requires:
+   * 1) The scope block has stage-pipeline property
+   * 2) The given block's subtree of the scope block satisfies compact dataflow condition.
+   * i.e. all the blocks in the scope's subtree must be either complete block or reduction block
+   * 3) `block` and `loop` are under the same scope, `loop` is not the ancestor of `block`
+   * 4) The block is not an output block,
+   * i.e. the buffer regions written by the block are allocated under the current scope
+   * 5) All the consumers of the block are under the given loop
+   * \param block_rv The block to be moved
+   * \param loop_rv The loop where the block to be moved under
+   * \param preserve_unit_loops Whether to keep the trivial loops whose extents are 1
+   */
+  virtual void ComputeAt(const BlockRV& block_rv, const LoopRV& loop_rv,
+                         bool preserve_unit_loops) = 0;
+  /*!
+   * \brief Move a consumer block under the specific loop, and regenerate the loops induced by the
+   * block so that the buffer region generated by the consumer block could cover those regions read
+   * by the consumers. It requires:

Review comment:
       ```suggestion
      * block so that the buffer region generated by the consumer block could cover those regions written
      * by the producers. It requires:
   ```
   BTW, after applying this suggestion, line 327 will have 102 characters in total. So we should make an earlier line break.

##########
File path: python/tvm/tir/schedule/schedule.py
##########
@@ -927,6 +927,181 @@ def after_cache_write(a: ty.handle, b: ty.handle) -> None:
 
     ########## Schedule: Compute location ##########
 
+    def compute_at(
+        self,
+        block: BlockRV,
+        loop: LoopRV,
+        preserve_unit_loops: bool = False,
+    ) -> None:
+        """Compute-At. Move a producer block under the specific loop, and regenerate the loops
+        induced by the block so that the buffer region generated by the producer block could cover
+        those regions read by the consumers. It requires:
+
+        1) The scope block has stage-pipeline property
+
+        2) The given block's subtree of the scope block satisfies compact dataflow condition.
+        i.e. all the blocks in the scope's subtree must be either complete block or reduction block
+
+        3) `block` and `loop` are under the same scope, `loop` is not the ancestor of `block`
+
+        4) The block is not an output block,
+        i.e. the buffer regions written by the block are allocated under the current scope
+
+        5) All the consumers of the block are under the given loop
+
+        Parameters
+        ----------
+        block : BlockRV
+            The block to be moved
+
+        loop: LoopRV
+            The loop where the block to be moved under
+
+        preserve_unit_loops: bool
+            Whether to keep the trivial loops whose extents are 1
+
+        Examples
+        --------
+
+        Before compute-at, in TensorIR, the IR is:
+
+        .. code-block:: python
+
+            @tvm.script.tir
+            def before_compute_at(a: ty.handle, c: ty.handle) -> None:
+                A = tir.match_buffer(a, (128, 128), "float32")
+                B = tir.alloc_buffer((128, 128), "float32")
+                C = tir.match_buffer(c, (128, 128), "float32")
+                with tir.block([128, 128], "B") as [vi, vj]:
+                    B[vi, vj] = A[vi, vj] * 2.0
+                with tir.block([128, 128], "C") as [vi, vj]:
+                    C[vi, vj] = B[vi, vj] + 1.0
+
+        Create the schedule and do compute-inline:

Review comment:
       🤐
   ```suggestion
           Create the schedule and do compute-at:
   ```

##########
File path: src/relay/transforms/fold_scale_axis.cc
##########
@@ -243,7 +243,9 @@ class ForwardPrep : private MixedModeVisitor {
     }
   }
   // Visitor pattern override.
-  void VisitExpr_(const LetNode* op) {
+  void VisitExpr_(const TupleGetItemNode* op) final { MixedModeVisitor::VisitExpr_(op); }

Review comment:
       What's this :eyes:?

##########
File path: include/tvm/tir/schedule/schedule.h
##########
@@ -305,6 +305,38 @@ class ScheduleNode : public runtime::Object {
   virtual BlockRV CacheWrite(const BlockRV& block_rv, int write_buffer_index,
                              const String& storage_scope) = 0;
   /******** Schedule: Compute location ********/
+  /*!
+   * \brief Move a producer block under the specific loop, and regenerate the loops induced by the
+   * block so that the buffer region generated by the producer block could cover those regions read
+   * by the consumers. It requires:
+   * 1) The scope block has stage-pipeline property
+   * 2) The given block's subtree of the scope block satisfies compact dataflow condition.
+   * i.e. all the blocks in the scope's subtree must be either complete block or reduction block
+   * 3) `block` and `loop` are under the same scope, `loop` is not the ancestor of `block`
+   * 4) The block is not an output block,
+   * i.e. the buffer regions written by the block are allocated under the current scope
+   * 5) All the consumers of the block are under the given loop
+   * \param block_rv The block to be moved
+   * \param loop_rv The loop where the block to be moved under
+   * \param preserve_unit_loops Whether to keep the trivial loops whose extents are 1
+   */
+  virtual void ComputeAt(const BlockRV& block_rv, const LoopRV& loop_rv,
+                         bool preserve_unit_loops) = 0;
+  /*!
+   * \brief Move a consumer block under the specific loop, and regenerate the loops induced by the
+   * block so that the buffer region generated by the consumer block could cover those regions read
+   * by the consumers. It requires:
+   * 1) The scope block has stage-pipeline property
+   * 2) The given block's subtree of the scope block satisfies compact dataflow condition.
+   * i.e. all the blocks in the scope's subtree must be either complete block or reduction block
+   * 3) `block` and `loop` are under the same scope, `loop` is not the ancestor of `block`

Review comment:
       Ditto as the comments for ComputeAt.

##########
File path: include/tvm/tir/schedule/schedule.h
##########
@@ -305,6 +305,38 @@ class ScheduleNode : public runtime::Object {
   virtual BlockRV CacheWrite(const BlockRV& block_rv, int write_buffer_index,
                              const String& storage_scope) = 0;
   /******** Schedule: Compute location ********/
+  /*!
+   * \brief Move a producer block under the specific loop, and regenerate the loops induced by the
+   * block so that the buffer region generated by the producer block could cover those regions read
+   * by the consumers. It requires:
+   * 1) The scope block has stage-pipeline property
+   * 2) The given block's subtree of the scope block satisfies compact dataflow condition.
+   * i.e. all the blocks in the scope's subtree must be either complete block or reduction block

Review comment:
       I think we should differentiate the "scope" in line 312 with the "scope" in line 314. To be more specific, in line 312 the "scope" is the scope block of `block` and `loop`, while in line 314 the "scope" is exactly the input block.

##########
File path: python/tvm/tir/schedule/schedule.py
##########
@@ -927,6 +927,181 @@ def after_cache_write(a: ty.handle, b: ty.handle) -> None:
 
     ########## Schedule: Compute location ##########
 
+    def compute_at(
+        self,
+        block: BlockRV,
+        loop: LoopRV,
+        preserve_unit_loops: bool = False,
+    ) -> None:
+        """Compute-At. Move a producer block under the specific loop, and regenerate the loops
+        induced by the block so that the buffer region generated by the producer block could cover
+        those regions read by the consumers. It requires:
+
+        1) The scope block has stage-pipeline property
+
+        2) The given block's subtree of the scope block satisfies compact dataflow condition.
+        i.e. all the blocks in the scope's subtree must be either complete block or reduction block
+
+        3) `block` and `loop` are under the same scope, `loop` is not the ancestor of `block`
+
+        4) The block is not an output block,
+        i.e. the buffer regions written by the block are allocated under the current scope
+
+        5) All the consumers of the block are under the given loop
+
+        Parameters
+        ----------
+        block : BlockRV
+            The block to be moved
+
+        loop: LoopRV
+            The loop where the block to be moved under
+
+        preserve_unit_loops: bool
+            Whether to keep the trivial loops whose extents are 1
+
+        Examples
+        --------
+
+        Before compute-at, in TensorIR, the IR is:
+
+        .. code-block:: python
+
+            @tvm.script.tir
+            def before_compute_at(a: ty.handle, c: ty.handle) -> None:
+                A = tir.match_buffer(a, (128, 128), "float32")
+                B = tir.alloc_buffer((128, 128), "float32")
+                C = tir.match_buffer(c, (128, 128), "float32")
+                with tir.block([128, 128], "B") as [vi, vj]:
+                    B[vi, vj] = A[vi, vj] * 2.0
+                with tir.block([128, 128], "C") as [vi, vj]:
+                    C[vi, vj] = B[vi, vj] + 1.0
+
+        Create the schedule and do compute-inline:
+
+        .. code-block:: python
+
+            sch = tir.Schedule(before_compute_at)
+            block = sch.get_block("B")
+            loop, _ = sch.get_loops(sch.get_block("C"))
+            sch.compute_at(block, loop, preserve_unit_loops=False)
+            print(tvm.script.asscript(sch.mod["main"]))
+
+        After applying compute-at, the IR becomes:
+
+        .. code-block:: python
+
+            @tvm.script.tir
+            def after_compute_at(a: ty.handle, c: ty.handle) -> None:
+                A = tir.match_buffer(a, (128, 128), "float32")
+                B = tir.alloc_buffer((128, 128), "float32")
+                C = tir.match_buffer(c, (128, 128), "float32")
+                for i in tir.serial(0, 128):
+                    for j in tir.serial(0, 128):
+                        with tir.block([128, 128], "B") as [vi, vj]:
+                            tir.bind(vi, i)
+                            tir.bind(vj, j)
+                            B[vi, vj] = A[vi, vj] * 2.0
+                    for j in tir.serial(0, 128):
+                        with tir.block([128, 128], "C") as [vi, vj]:
+                            tir.bind(vi, i)
+                            tir.bind(vj, j)
+                            C[vi, vj] = B[vi, vj] + 1.0
+
+        """
+        _ffi_api.ScheduleComputeAt(  # type: ignore # pylint: disable=no-member
+            self,
+            block,
+            loop,
+            preserve_unit_loops,
+        )
+
+    def reverse_compute_at(
+        self,
+        block: BlockRV,
+        loop: LoopRV,
+        preserve_unit_loops: bool = False,
+    ) -> None:
+        """Reverse-Compute-At. Move a consumer block under the specific loop, and regenerate the
+        loops induced by the block so that the buffer region generated by the consumer block could
+        cover those regions read by the consumers. It requires:
+
+        1) The scope block has stage-pipeline property
+
+        2) The given block's subtree of the scope block satisfies compact dataflow condition.
+        i.e. all the blocks in the scope's subtree must be either complete block or reduction block
+
+        3) `block` and `loop` are under the same scope, `loop` is not the ancestor of `block`
+
+        4) All the producers of the block are under the given loop
+
+        Parameters
+        ----------
+        block : BlockRV
+            The block to be moved
+
+        loop: LoopRV
+            The loop where the block to be moved under
+
+        preserve_unit_loops: bool
+            Whether to keep the trivial loops whose extents are 1
+
+        Examples
+        --------
+
+        Before reverse-compute-at, in TensorIR, the IR is:
+
+        .. code-block:: python
+
+            @tvm.script.tir
+            def before_reverse_compute_at(a: ty.handle, c: ty.handle) -> None:
+                A = tir.match_buffer(a, (128, 128), "float32")
+                B = tir.alloc_buffer((128, 128), "float32")
+                C = tir.match_buffer(c, (128, 128), "float32")
+                with tir.block([128, 128], "B") as [vi, vj]:
+                    B[vi, vj] = A[vi, vj] * 2.0
+                with tir.block([128, 128], "C") as [vi, vj]:
+                    C[vi, vj] = B[vi, vj] + 1.0
+
+        Create the schedule and do compute-inline:

Review comment:
       ```suggestion
           Create the schedule and do reverse-compute-at:
   ```

##########
File path: src/support/nd_int_set.h
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.
+ */
+#ifndef TVM_SUPPORT_ND_INT_SET_H_
+#define TVM_SUPPORT_ND_INT_SET_H_
+
+#include <tvm/arith/int_set.h>
+#include <tvm/ir/expr.h>
+
+#include <unordered_map>
+#include <vector>
+
+namespace tvm {
+namespace support {
+
+/*! \brief An N-dimensional integer set representing a rectangle region */
+using NDIntSet = std::vector<tvm::arith::IntSet>;
+
+/*!
+ * \brief Construct an N-dimensional integer set representing a region.
+ * \param region The region.
+ * \return constructed set.
+ */
+inline NDIntSet NDIntSetFromRegion(const tir::Region& region) {
+  NDIntSet result;
+  result.reserve(region.size());
+  for (const Range& range : region) {
+    result.push_back(arith::IntSet::FromRange(range));
+  }
+  return result;
+}
+
+/*!
+ * \brief Construct an N-dimensional integer set representing a shape.
+ * \param shape The shape which is an array of the length of each dimension.
+ * \return constructed set.
+ */
+inline NDIntSet NDIntSetFromShape(const Array<PrimExpr>& shape) {
+  PrimExpr zero = Integer(0);
+  NDIntSet result;
+  result.reserve(shape.size());
+  for (const PrimExpr& extent : shape) {
+    result.push_back(arith::IntSet::FromMinExtent(zero, extent));
+  }
+  return result;
+}
+
+/*!
+ * \brief Construct an N-dimensional integer set representing a point.
+ * \param indices The N-dimensional indices representing the point.
+ * \return constructed set.
+ */
+inline NDIntSet NDIntSetFromPoint(const Array<PrimExpr>& indices) {
+  NDIntSet result;
+  result.reserve(indices.size());
+  for (const PrimExpr& index : indices) {
+    result.push_back(arith::IntSet::SinglePoint(index));
+  }
+  return result;
+}
+
+/*!
+ * \brief Create a union set of two sets, possibly relaxed. The RHS set will be combined into the
+ *        LHS set.
+ * \param lhs The first N-dimensional integer set
+ * \param rhs The second N-dimensional integer set
+ */
+inline void NDIntSetUnionWith(NDIntSet* lhs, const NDIntSet& rhs) {
+  ICHECK_EQ(lhs->size(), rhs.size());
+  int ndim = rhs.size();
+  for (int i = 0; i < ndim; ++i) {
+    arith::IntSet& int_set = lhs->at(i);
+    int_set = arith::Union({int_set, rhs.at(i)});
+  }
+}
+
+/*!
+ * \brief Union a list of N-dimensional integer sets
+ * \param nd_int_sets The N-dimensional integer sets to be merged.
+ * \return The result of the union
+ */
+inline NDIntSet NDIntSetUnion(const std::vector<NDIntSet>& nd_int_sets) {

Review comment:
       Again the question that why choosing `std::vector` :eyes:.

##########
File path: src/support/nd_int_set.h
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.
+ */
+#ifndef TVM_SUPPORT_ND_INT_SET_H_
+#define TVM_SUPPORT_ND_INT_SET_H_
+
+#include <tvm/arith/int_set.h>
+#include <tvm/ir/expr.h>
+
+#include <unordered_map>
+#include <vector>
+
+namespace tvm {
+namespace support {
+
+/*! \brief An N-dimensional integer set representing a rectangle region */
+using NDIntSet = std::vector<tvm::arith::IntSet>;

Review comment:
       * Q1. What's the reason that using `std::vector` instead of `Array`?
   * Q2. Is it possible to simplify the template name as below?
   ```suggestion
   using NDIntSet = std::vector<arith::IntSet>;
   ```

##########
File path: src/tir/schedule/analysis.h
##########
@@ -128,18 +138,36 @@ void CheckReductionBlock(const ScheduleState& self, const StmtSRef& block_sref,
                          const StmtSRef& scope_root_sref);
 
 /*!
- * \brief Check whether a subtree on SRef tree has compact data flow, and throw an exception if the
- * subtree does not have compact data flow
- * \details For a given StmtSRef, We say the subtree rooted from the StmtSRef has "compact data
- * flow" property if:
- * - the scope root of the input subtree root has stage-pipeline property, and
- * - all its child blocks on SRef tree are complete blocks or reduction blocks.
+ * \brief Check if the block is a complete block or a reduction block under the scope
  * \param self The schedule state
- * \param subtree_root_sref The root of the subtree to be checked in the SRef tree
- * \throw ScheduleError If the subtree does not have compact data flow
- * \sa IsCompleteBlock, IsReductionBlock
+ * \param block_sref The sref of the block to be checked
+ * \param scope_root_sref The scope root of the block
+ * \throw ScheduleError If the block is not a reduction block
+ */
+void CheckCompleteOrReductionBlock(const ScheduleState& self, const StmtSRef& block_sref,
+                                   const StmtSRef& scope_root_sref);
+
+/*!
+ * \brief Check if the block is an output block, i.e. the buffer regions written by the block are
+ * allocated under the current scope

Review comment:
       I cannot understand the description here. After reading the implementation, I know that `IsOutputBlock` returns true if the input block writes to some buffer that isn't allocated by the scope block. But my understanding seems not matching the description here.

##########
File path: src/tir/schedule/analysis.h
##########
@@ -128,18 +138,36 @@ void CheckReductionBlock(const ScheduleState& self, const StmtSRef& block_sref,
                          const StmtSRef& scope_root_sref);
 
 /*!
- * \brief Check whether a subtree on SRef tree has compact data flow, and throw an exception if the
- * subtree does not have compact data flow
- * \details For a given StmtSRef, We say the subtree rooted from the StmtSRef has "compact data
- * flow" property if:
- * - the scope root of the input subtree root has stage-pipeline property, and
- * - all its child blocks on SRef tree are complete blocks or reduction blocks.
+ * \brief Check if the block is a complete block or a reduction block under the scope
  * \param self The schedule state
- * \param subtree_root_sref The root of the subtree to be checked in the SRef tree
- * \throw ScheduleError If the subtree does not have compact data flow
- * \sa IsCompleteBlock, IsReductionBlock
+ * \param block_sref The sref of the block to be checked
+ * \param scope_root_sref The scope root of the block
+ * \throw ScheduleError If the block is not a reduction block

Review comment:
       ```suggestion
    * \throw ScheduleError If the block is neither a complete block nor a reduction block
   ```

##########
File path: src/support/nd_int_set.h
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.
+ */
+#ifndef TVM_SUPPORT_ND_INT_SET_H_
+#define TVM_SUPPORT_ND_INT_SET_H_
+
+#include <tvm/arith/int_set.h>
+#include <tvm/ir/expr.h>
+
+#include <unordered_map>
+#include <vector>
+
+namespace tvm {
+namespace support {
+
+/*! \brief An N-dimensional integer set representing a rectangle region */
+using NDIntSet = std::vector<tvm::arith::IntSet>;
+
+/*!
+ * \brief Construct an N-dimensional integer set representing a region.
+ * \param region The region.
+ * \return constructed set.
+ */
+inline NDIntSet NDIntSetFromRegion(const tir::Region& region) {
+  NDIntSet result;
+  result.reserve(region.size());
+  for (const Range& range : region) {
+    result.push_back(arith::IntSet::FromRange(range));
+  }
+  return result;
+}
+
+/*!
+ * \brief Construct an N-dimensional integer set representing a shape.
+ * \param shape The shape which is an array of the length of each dimension.
+ * \return constructed set.
+ */
+inline NDIntSet NDIntSetFromShape(const Array<PrimExpr>& shape) {
+  PrimExpr zero = Integer(0);
+  NDIntSet result;
+  result.reserve(shape.size());
+  for (const PrimExpr& extent : shape) {
+    result.push_back(arith::IntSet::FromMinExtent(zero, extent));
+  }
+  return result;
+}
+
+/*!
+ * \brief Construct an N-dimensional integer set representing a point.
+ * \param indices The N-dimensional indices representing the point.
+ * \return constructed set.
+ */
+inline NDIntSet NDIntSetFromPoint(const Array<PrimExpr>& indices) {
+  NDIntSet result;
+  result.reserve(indices.size());
+  for (const PrimExpr& index : indices) {
+    result.push_back(arith::IntSet::SinglePoint(index));
+  }
+  return result;
+}
+
+/*!
+ * \brief Create a union set of two sets, possibly relaxed. The RHS set will be combined into the
+ *        LHS set.
+ * \param lhs The first N-dimensional integer set
+ * \param rhs The second N-dimensional integer set
+ */
+inline void NDIntSetUnionWith(NDIntSet* lhs, const NDIntSet& rhs) {
+  ICHECK_EQ(lhs->size(), rhs.size());
+  int ndim = rhs.size();
+  for (int i = 0; i < ndim; ++i) {
+    arith::IntSet& int_set = lhs->at(i);
+    int_set = arith::Union({int_set, rhs.at(i)});
+  }
+}
+
+/*!
+ * \brief Union a list of N-dimensional integer sets
+ * \param nd_int_sets The N-dimensional integer sets to be merged.
+ * \return The result of the union
+ */
+inline NDIntSet NDIntSetUnion(const std::vector<NDIntSet>& nd_int_sets) {
+  ICHECK(!nd_int_sets.empty());
+  int n = nd_int_sets.size();
+  if (n == 1) {
+    return nd_int_sets[0];
+  }
+  int ndim = nd_int_sets[0].size();
+  for (int i = 1; i < n; ++i) {
+    ICHECK_EQ(nd_int_sets[i].size(), ndim);
+  }
+  NDIntSet result;
+  result.reserve(ndim);
+  Array<arith::IntSet> int_sets(n, arith::IntSet{nullptr});
+  for (int dim = 0; dim < ndim; ++dim) {
+    for (int i = 0; i < n; ++i) {
+      int_sets.Set(i, nd_int_sets[i][dim]);
+    }
+    result.push_back(arith::Union(int_sets));
+  }
+  return result;
+}
+
+/*!
+ * \brief Create an empty N-dimensional integer set.

Review comment:
       ```suggestion
    * \brief Create an empty N-dimensional integer set with specific number of dimensions.
   ```

##########
File path: src/tir/schedule/primitive/cache_read_write.cc
##########
@@ -160,6 +160,21 @@ Block MakeCacheStage(const BufferRegion& cache_region, CacheStageInfo* info,
   return block;
 }
 
+/*!
+ * \brief Recalculate the `affine_binding` flag of the scope block info.
+ * \param block_sref The sref to the interested scope block.
+ */
+bool CalculateAffineFlag(const ScheduleState& self, const StmtSRef& block_sref) {
+  if (block_sref->parent == nullptr) {
+    return true;
+  }

Review comment:
       I have a question for a long period of time: if a block has no `IterVar` (i.e., opaque block), does it have affine bindings?

##########
File path: src/tir/schedule/primitive.h
##########
@@ -160,6 +160,42 @@ TVM_DLL StmtSRef CacheRead(ScheduleState self, const StmtSRef& block_sref, int r
 TVM_DLL StmtSRef CacheWrite(ScheduleState self, const StmtSRef& block_sref, int write_buffer_index,
                             const String& storage_scope);
 /******** Schedule: Compute location ********/
+/*!
+ * \brief Move a producer block under the specific loop, and regenerate the loops induced by the
+ * block so that the buffer region generated by the producer block could cover those regions read
+ * by the consumers. It requires:
+ * 1) The scope block has stage-pipeline property
+ * 2) The given block's subtree of the scope block satisfies compact dataflow condition.
+ * i.e. all the blocks in the scope's subtree must be either complete block or reduction block
+ * 3) `block` and `loop` are under the same scope, `loop` is not the ancestor of `block`
+ * 4) The block is not an output block,
+ * i.e. the buffer regions written by the block are allocated under the current scope

Review comment:
       Ditto as the comments in schedule.h.

##########
File path: src/tir/schedule/primitive/cache_read_write.cc
##########
@@ -160,6 +160,21 @@ Block MakeCacheStage(const BufferRegion& cache_region, CacheStageInfo* info,
   return block;
 }
 
+/*!
+ * \brief Recalculate the `affine_binding` flag of the scope block info.

Review comment:
       ```suggestion
    * \brief Recalculate the `affine_binding` flag of the input block's BlockInfo.
   ```

##########
File path: src/support/nd_int_set.h
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.
+ */
+#ifndef TVM_SUPPORT_ND_INT_SET_H_
+#define TVM_SUPPORT_ND_INT_SET_H_
+
+#include <tvm/arith/int_set.h>
+#include <tvm/ir/expr.h>
+
+#include <unordered_map>
+#include <vector>
+
+namespace tvm {
+namespace support {
+
+/*! \brief An N-dimensional integer set representing a rectangle region */
+using NDIntSet = std::vector<tvm::arith::IntSet>;
+
+/*!
+ * \brief Construct an N-dimensional integer set representing a region.
+ * \param region The region.
+ * \return constructed set.
+ */
+inline NDIntSet NDIntSetFromRegion(const tir::Region& region) {
+  NDIntSet result;
+  result.reserve(region.size());
+  for (const Range& range : region) {
+    result.push_back(arith::IntSet::FromRange(range));
+  }
+  return result;
+}
+
+/*!
+ * \brief Construct an N-dimensional integer set representing a shape.
+ * \param shape The shape which is an array of the length of each dimension.
+ * \return constructed set.

Review comment:
       ```suggestion
    * \return The constructed set.
   ```

##########
File path: src/tir/schedule/analysis/analysis.cc
##########
@@ -174,6 +212,18 @@ int CheckCompleteBlockErrorCode(const ScheduleState& self, const StmtSRef& block
   return 0;
 }
 
+static const char* kCompleteBlockDefinition = R"(Definition of a complete block:

Review comment:
       What does "k" mean here 👀?

##########
File path: src/support/nd_int_set.h
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.
+ */
+#ifndef TVM_SUPPORT_ND_INT_SET_H_
+#define TVM_SUPPORT_ND_INT_SET_H_
+
+#include <tvm/arith/int_set.h>
+#include <tvm/ir/expr.h>
+
+#include <unordered_map>
+#include <vector>
+
+namespace tvm {
+namespace support {
+
+/*! \brief An N-dimensional integer set representing a rectangle region */
+using NDIntSet = std::vector<tvm::arith::IntSet>;
+
+/*!
+ * \brief Construct an N-dimensional integer set representing a region.
+ * \param region The region.
+ * \return constructed set.
+ */
+inline NDIntSet NDIntSetFromRegion(const tir::Region& region) {
+  NDIntSet result;
+  result.reserve(region.size());
+  for (const Range& range : region) {
+    result.push_back(arith::IntSet::FromRange(range));
+  }
+  return result;
+}
+
+/*!
+ * \brief Construct an N-dimensional integer set representing a shape.
+ * \param shape The shape which is an array of the length of each dimension.
+ * \return constructed set.
+ */
+inline NDIntSet NDIntSetFromShape(const Array<PrimExpr>& shape) {
+  PrimExpr zero = Integer(0);
+  NDIntSet result;
+  result.reserve(shape.size());
+  for (const PrimExpr& extent : shape) {
+    result.push_back(arith::IntSet::FromMinExtent(zero, extent));
+  }
+  return result;
+}
+
+/*!
+ * \brief Construct an N-dimensional integer set representing a point.
+ * \param indices The N-dimensional indices representing the point.
+ * \return constructed set.
+ */
+inline NDIntSet NDIntSetFromPoint(const Array<PrimExpr>& indices) {
+  NDIntSet result;
+  result.reserve(indices.size());
+  for (const PrimExpr& index : indices) {
+    result.push_back(arith::IntSet::SinglePoint(index));
+  }
+  return result;
+}
+
+/*!
+ * \brief Create a union set of two sets, possibly relaxed. The RHS set will be combined into the
+ *        LHS set.
+ * \param lhs The first N-dimensional integer set
+ * \param rhs The second N-dimensional integer set
+ */
+inline void NDIntSetUnionWith(NDIntSet* lhs, const NDIntSet& rhs) {
+  ICHECK_EQ(lhs->size(), rhs.size());
+  int ndim = rhs.size();
+  for (int i = 0; i < ndim; ++i) {
+    arith::IntSet& int_set = lhs->at(i);
+    int_set = arith::Union({int_set, rhs.at(i)});
+  }
+}
+
+/*!
+ * \brief Union a list of N-dimensional integer sets
+ * \param nd_int_sets The N-dimensional integer sets to be merged.
+ * \return The result of the union
+ */
+inline NDIntSet NDIntSetUnion(const std::vector<NDIntSet>& nd_int_sets) {
+  ICHECK(!nd_int_sets.empty());
+  int n = nd_int_sets.size();
+  if (n == 1) {
+    return nd_int_sets[0];
+  }
+  int ndim = nd_int_sets[0].size();
+  for (int i = 1; i < n; ++i) {
+    ICHECK_EQ(nd_int_sets[i].size(), ndim);
+  }
+  NDIntSet result;
+  result.reserve(ndim);
+  Array<arith::IntSet> int_sets(n, arith::IntSet{nullptr});
+  for (int dim = 0; dim < ndim; ++dim) {
+    for (int i = 0; i < n; ++i) {
+      int_sets.Set(i, nd_int_sets[i][dim]);
+    }
+    result.push_back(arith::Union(int_sets));
+  }
+  return result;
+}
+
+/*!
+ * \brief Create an empty N-dimensional integer set.
+ * \param ndim The number of dimensions.
+ * \return constructed set.

Review comment:
       ```suggestion
    * \return The constructed set.
   ```

##########
File path: src/support/nd_int_set.h
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.
+ */
+#ifndef TVM_SUPPORT_ND_INT_SET_H_
+#define TVM_SUPPORT_ND_INT_SET_H_
+
+#include <tvm/arith/int_set.h>
+#include <tvm/ir/expr.h>
+
+#include <unordered_map>
+#include <vector>
+
+namespace tvm {
+namespace support {
+
+/*! \brief An N-dimensional integer set representing a rectangle region */
+using NDIntSet = std::vector<tvm::arith::IntSet>;
+
+/*!
+ * \brief Construct an N-dimensional integer set representing a region.
+ * \param region The region.
+ * \return constructed set.
+ */
+inline NDIntSet NDIntSetFromRegion(const tir::Region& region) {
+  NDIntSet result;
+  result.reserve(region.size());
+  for (const Range& range : region) {
+    result.push_back(arith::IntSet::FromRange(range));
+  }
+  return result;
+}
+
+/*!
+ * \brief Construct an N-dimensional integer set representing a shape.
+ * \param shape The shape which is an array of the length of each dimension.
+ * \return constructed set.
+ */
+inline NDIntSet NDIntSetFromShape(const Array<PrimExpr>& shape) {
+  PrimExpr zero = Integer(0);

Review comment:
       I wonder whether extracting the definition of `Integer(0)` can reduce the number of constructions of zero?

##########
File path: src/support/nd_int_set.h
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.
+ */
+#ifndef TVM_SUPPORT_ND_INT_SET_H_
+#define TVM_SUPPORT_ND_INT_SET_H_
+
+#include <tvm/arith/int_set.h>
+#include <tvm/ir/expr.h>
+
+#include <unordered_map>
+#include <vector>
+
+namespace tvm {
+namespace support {
+
+/*! \brief An N-dimensional integer set representing a rectangle region */
+using NDIntSet = std::vector<tvm::arith::IntSet>;
+
+/*!
+ * \brief Construct an N-dimensional integer set representing a region.
+ * \param region The region.
+ * \return constructed set.
+ */
+inline NDIntSet NDIntSetFromRegion(const tir::Region& region) {
+  NDIntSet result;
+  result.reserve(region.size());
+  for (const Range& range : region) {
+    result.push_back(arith::IntSet::FromRange(range));
+  }
+  return result;
+}
+
+/*!
+ * \brief Construct an N-dimensional integer set representing a shape.
+ * \param shape The shape which is an array of the length of each dimension.
+ * \return constructed set.
+ */
+inline NDIntSet NDIntSetFromShape(const Array<PrimExpr>& shape) {
+  PrimExpr zero = Integer(0);
+  NDIntSet result;
+  result.reserve(shape.size());
+  for (const PrimExpr& extent : shape) {
+    result.push_back(arith::IntSet::FromMinExtent(zero, extent));
+  }
+  return result;
+}
+
+/*!
+ * \brief Construct an N-dimensional integer set representing a point.
+ * \param indices The N-dimensional indices representing the point.
+ * \return constructed set.
+ */
+inline NDIntSet NDIntSetFromPoint(const Array<PrimExpr>& indices) {
+  NDIntSet result;
+  result.reserve(indices.size());
+  for (const PrimExpr& index : indices) {
+    result.push_back(arith::IntSet::SinglePoint(index));
+  }
+  return result;
+}
+
+/*!
+ * \brief Create a union set of two sets, possibly relaxed. The RHS set will be combined into the
+ *        LHS set.
+ * \param lhs The first N-dimensional integer set
+ * \param rhs The second N-dimensional integer set
+ */
+inline void NDIntSetUnionWith(NDIntSet* lhs, const NDIntSet& rhs) {
+  ICHECK_EQ(lhs->size(), rhs.size());
+  int ndim = rhs.size();
+  for (int i = 0; i < ndim; ++i) {
+    arith::IntSet& int_set = lhs->at(i);
+    int_set = arith::Union({int_set, rhs.at(i)});
+  }
+}
+
+/*!
+ * \brief Union a list of N-dimensional integer sets
+ * \param nd_int_sets The N-dimensional integer sets to be merged.
+ * \return The result of the union
+ */
+inline NDIntSet NDIntSetUnion(const std::vector<NDIntSet>& nd_int_sets) {
+  ICHECK(!nd_int_sets.empty());
+  int n = nd_int_sets.size();
+  if (n == 1) {
+    return nd_int_sets[0];
+  }
+  int ndim = nd_int_sets[0].size();
+  for (int i = 1; i < n; ++i) {
+    ICHECK_EQ(nd_int_sets[i].size(), ndim);
+  }
+  NDIntSet result;
+  result.reserve(ndim);
+  Array<arith::IntSet> int_sets(n, arith::IntSet{nullptr});
+  for (int dim = 0; dim < ndim; ++dim) {
+    for (int i = 0; i < n; ++i) {
+      int_sets.Set(i, nd_int_sets[i][dim]);
+    }
+    result.push_back(arith::Union(int_sets));
+  }
+  return result;
+}
+
+/*!
+ * \brief Create an empty N-dimensional integer set.
+ * \param ndim The number of dimensions.
+ * \return constructed set.
+ */
+inline NDIntSet NDIntSetEmpty(int ndim) {

Review comment:
       `EmptyNDInsSet` or `NDIntSetEmpty`. Which name sounds better 🤔.




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



[GitHub] [tvm] MasterJH5574 commented on a change in pull request #8943: [TensorIR][M2a] Compute-At

Posted by GitBox <gi...@apache.org>.
MasterJH5574 commented on a change in pull request #8943:
URL: https://github.com/apache/tvm/pull/8943#discussion_r705040467



##########
File path: src/tir/schedule/concrete_schedule.cc
##########
@@ -439,6 +439,44 @@ BlockRV ConcreteScheduleNode::CacheWrite(const BlockRV& block_rv, int write_buff
 
 /******** Schedule: Compute location ********/
 
+void ConcreteScheduleNode::ComputeAt(const BlockRV& block_rv, const LoopRV& loop_rv,
+                                     bool preserve_unit_loops) {
+  static StmtSRef inline_mark = StmtSRef::InlineMark();
+  static StmtSRef root_mark = StmtSRef::RootMark();
+  StmtSRef loop_sref = this->GetSRef(loop_rv);

Review comment:
       Thanks for your thorough explanation! I can completely understand the workflow now 😆




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