masahi commented on code in PR #12171:
URL: https://github.com/apache/tvm/pull/12171#discussion_r931536642


##########
src/tir/transforms/inject_software_pipeline.cc:
##########
@@ -530,20 +821,78 @@ class PipelineRewriter : public StmtExprMutator {
       Block new_block = 
Downcast<Block>(PipelineBodyRewriter(buffer_data_to_buffer_, buffer_remap_,
                                                              pipeline_loop_, 
max_stage_ != 1,
                                                              
fragment_info_)(block));
-      Map<Var, PrimExpr> subst_map;
-      if (is_unit_loop) {
-        subst_map.Set(pipeline_loop_->loop_var, skewed_loop_var);
-      } else {
-        // normalize loop range
-        PrimExpr delta = start - pipeline_loop_->min;
-        subst_map.Set(pipeline_loop_->loop_var, skewed_loop_var + delta);
+
+      PrimExpr delta = start - pipeline_loop_->min;
+      // This variable corresponds to
+      // - "producer_head" if this stage is an async producer
+      // - "consumer_head" if this stage reads from asynchronously written 
buffers.
+      PrimExpr normalized_access_index = is_unit_loop ? skewed_loop_var : 
skewed_loop_var + delta;
+
+      // Adjust the block predicate and the body according to the final loop 
bound
+      //  [pipeline_loop_->min, extent).
+      if (!is_unit_loop) {
         Var loop_iter = Downcast<Var>(new_loop_var);
-        inbound = Substitute(inbound, Map<Var, PrimExpr>{{loop_iter, loop_iter 
+ delta}});
+        inbound = Substitute(inbound, {{loop_iter, loop_iter + delta}});
+      }
+
+      new_block = Downcast<Block>(
+          Substitute(new_block, {{pipeline_loop_->loop_var, 
normalized_access_index}}));
+
+      if (pipeline_info_[block].async) {

Review Comment:
   It's possible but since this code block touches a lot of stuff defined in 
this loop, the extracted function would look rather messy like this: 
   
   ```
     void UpdateForAsync(Block block, Block new_block, int stage, size_t 
new_blocks_size,
                         PrimExpr normalized_access_index, PrimExpr inbound,
                         arith::Analyzer* ana_normalized,
                         std::map<int, AsyncStateLocal>* async_states_local,
                         std::unordered_map<const BufferNode*, int>* 
buffer_to_commit_group) {
            ...
   ```
   
   And a reader would need to go back and forth between this function 
and`EmitImpl` anyway to understand the meanings of these variables and what 
this function does.
   
   So I think making this change would rather hurt the readability. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

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

Reply via email to