You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by "Lunderberg (via GitHub)" <gi...@apache.org> on 2023/02/10 15:26:23 UTC

[GitHub] [tvm] Lunderberg opened a new pull request, #13947: [TIR][Analysis] Implement IdentifyMemCpy analysis function

Lunderberg opened a new pull request, #13947:
URL: https://github.com/apache/tvm/pull/13947

   This commit adds a utility function `tir::IdentifyMemCpy`, which analyzes a `tir::For` loop and determines whether it is equivalent to a memcpy.  If it is equivalent, it will additionally return the source and destination regions of the memcpy.
   
   This utility is initially intended for use in the `LowerAsyncDMA` pass, but may be useful in other areas in the
   future.  (e.g. Identifying an entire TIR function as equivalent to memcpy would allow calls to that TIR function to be removed from end-to-end models.)


-- 
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] Lunderberg commented on a diff in pull request #13947: [TIR][Analysis] Implement IdentifyMemCpy analysis function

Posted by "Lunderberg (via GitHub)" <gi...@apache.org>.
Lunderberg commented on code in PR #13947:
URL: https://github.com/apache/tvm/pull/13947#discussion_r1124806386


##########
tests/python/unittest/test_tir_analysis_identify_memcpy.py:
##########
@@ -0,0 +1,325 @@
+# 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.
+
+import re
+
+import pytest
+
+import tvm
+import tvm.testing
+from tvm import te, topi

Review Comment:
   Thank you, and removed.



-- 
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] Lunderberg commented on a diff in pull request #13947: [TIR][Analysis] Implement IdentifyMemCpy analysis function

Posted by "Lunderberg (via GitHub)" <gi...@apache.org>.
Lunderberg commented on code in PR #13947:
URL: https://github.com/apache/tvm/pull/13947#discussion_r1124808234


##########
src/tir/analysis/identify_memcpy.cc:
##########
@@ -0,0 +1,317 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tir/analysis/identify_memcpy.cc
+ * \brief Check if a loopnest is equivalent to memcpy
+ */
+
+#include <tvm/arith/bound.h>
+#include <tvm/arith/iter_affine_map.h>
+#include <tvm/runtime/container/optional.h>
+#include <tvm/tir/analysis.h>
+#include <tvm/tir/buffer.h>
+#include <tvm/tir/stmt.h>
+
+#include <optional>
+#include <sstream>
+#include <string>
+#include <variant>
+
+#include "../../arith/ir_visitor_with_analyzer.h"
+
+namespace tvm {
+namespace tir {
+
+std::variant<MemCpyDetails, std::string> IdentifyMemCpyImpl(const For& loop,
+                                                            arith::Analyzer* analyzer) {
+  Map<Var, arith::IntSet> loop_intervals;
+  Map<Var, Range> loop_ranges;
+  PrimExpr total_loop_iterations = 1;
+
+  // Walk through the loopnest, stopping at the first loop whose body
+  // is not a loop.
+  Stmt stmt = loop;
+  while (auto* for_node = stmt.as<ForNode>()) {
+    loop_ranges.Set(for_node->loop_var, Range::FromMinExtent(for_node->min, for_node->extent));
+    loop_intervals.Set(for_node->loop_var,
+                       arith::IntSet::FromMinExtent(for_node->min, for_node->extent));
+    total_loop_iterations = total_loop_iterations * for_node->extent;
+
+    stmt = for_node->body;
+  }
+
+  BufferStore store;
+  if (auto* ptr = stmt.as<BufferStoreNode>()) {
+    store = GetRef<BufferStore>(ptr);
+  } else {
+    return static_cast<const std::stringstream&>(
+               std::stringstream()
+               << "Expected innermost loop to have BufferStore body, but instead found " << stmt)
+        .str();
+  }
+
+  BufferLoad load;
+  if (auto* ptr = store->value.as<BufferLoadNode>()) {
+    load = GetRef<BufferLoad>(ptr);
+  } else {
+    return static_cast<const std::stringstream&>(
+               std::stringstream()
+               << "Expected BufferStore's value to be BufferLoad, but instead found "
+               << store->value)
+        .str();
+  }
+
+  // Now, we have a BufferStore whose value is a BufferLoad.  Because
+  // non-flat physical indices are target-dependent, only handle cases
+  // where the buffer will be flattened to a 1-d physical buffer.
+  Array<PrimExpr> flattened_dst = store->buffer.OffsetOf(store->indices);
+  Array<PrimExpr> flattened_src = load->buffer.OffsetOf(load->indices);
+
+  if (flattened_dst.size() != 1 || flattened_src.size() != 1) {
+    return static_cast<const std::stringstream&>(
+               std::stringstream()
+               << "Expected flattened dimension of src/dest to be 1, but found"
+               << flattened_src.size() << "-d src and " << flattened_dst.size() << "-d dst")
+        .str();
+  }
+  PrimExpr src_index = flattened_src[0];
+  PrimExpr dst_index = flattened_dst[0];
+
+  // First check, do the input/output form affine subsets of their
+  // respective buffers?
+  //
+  // For example, should exclude the following, indices are not affine
+  //
+  // for i in T.serial(16):
+  //     B[i] = A[T.abs(i-8)]
+
+  auto src_iter_map = arith::DetectIterMap({src_index}, loop_ranges, Bool(true),
+                                           arith::IterMapLevel::Bijective, analyzer);
+  if (src_iter_map->errors.size()) {
+    return static_cast<const std::stringstream&>(std::stringstream()
+                                                 << "arith::DetectIterMap(src) returned "
+                                                 << src_iter_map->errors.size() << " errors: ["
+                                                 << src_iter_map->errors << "]"
+                                                 << " for src_index = " << src_index)
+        .str();
+  }
+  auto dst_iter_map = arith::DetectIterMap({dst_index}, loop_ranges, Bool(true),
+                                           arith::IterMapLevel::Bijective, analyzer);
+  if (dst_iter_map->errors.size()) {
+    return static_cast<const std::stringstream&>(std::stringstream()
+                                                 << "arith::DetectIterMap(dst) returned "
+                                                 << dst_iter_map->errors.size() << " errors: ["
+                                                 << dst_iter_map->errors << "]"
+                                                 << " for dst_index = " << dst_index)
+        .str();
+  }
+
+  // Second check, are those affine subsets contiguous?  If so, then
+  // the index expressions will visit every location between the min
+  // and the max.  This checks surjectivity over a linear region,
+  // which may not be the same as DetectIterMap's check of
+  // surjectivity over the affine subset.
+  //
+  // For example, should exclude the following, doesn't touch all
+  // output locations within the output region touched.
+  //
+  // for i in T.serial(16):
+  //     B[2*i] = A[i]
+  //
+  // Similarly, should exclude the following, doesn't touch all
+  // input locations within the input region touched.
+  //
+  // for i in T.serial(16):
+  //     B[i] = A[2*i]
+  total_loop_iterations = analyzer->Simplify(total_loop_iterations);
+  auto src_interval = analyzer->int_set(src_index, loop_intervals);
+  auto dst_interval = analyzer->int_set(dst_index, loop_intervals);
+
+  if (!src_interval.HasLowerBound() || !src_interval.HasUpperBound()) {
+    return static_cast<const std::stringstream&>(std::stringstream()
+                                                 << "Expected known bounds for src, but found "
+                                                 << src_interval << " for expression " << src_index)
+        .str();
+  }
+  if (!dst_interval.HasLowerBound() || !dst_interval.HasUpperBound()) {
+    return static_cast<const std::stringstream&>(std::stringstream()
+                                                 << "Expected known bounds for dst, but found "
+                                                 << dst_interval << " for expression " << dst_index)
+        .str();
+  }
+
+  {
+    PrimExpr must_prove = total_loop_iterations == src_interval.max() - src_interval.min() + 1;
+    PrimExpr simplified = analyzer->Simplify(must_prove);
+    if (!analyzer->CanProve(simplified)) {
+      return static_cast<const std::stringstream&>(
+                 std::stringstream()
+                 << "Mismatch between loop iterations (" << total_loop_iterations
+                 << ") and number of src indices touched (" << src_interval
+                 << ".  Equality to prove simplified to " << simplified)
+          .str();
+    }
+  }
+  {
+    PrimExpr must_prove = total_loop_iterations == dst_interval.max() - dst_interval.min() + 1;
+    PrimExpr simplified = analyzer->Simplify(must_prove);
+    if (!analyzer->CanProve(simplified)) {
+      return static_cast<const std::stringstream&>(
+                 std::stringstream()
+                 << "Mismatch between loop iterations (" << total_loop_iterations
+                 << ") and number of dst indices touched (" << dst_interval
+                 << ".  Equality to prove simplified to " << simplified)
+          .str();
+    }
+  }
+
+  // Thrid check, is there a transformation applied between the input

Review Comment:
   Thank you, and fix applied.



##########
src/tir/analysis/identify_memcpy.cc:
##########
@@ -0,0 +1,317 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tir/analysis/identify_memcpy.cc
+ * \brief Check if a loopnest is equivalent to memcpy
+ */
+
+#include <tvm/arith/bound.h>
+#include <tvm/arith/iter_affine_map.h>
+#include <tvm/runtime/container/optional.h>
+#include <tvm/tir/analysis.h>
+#include <tvm/tir/buffer.h>
+#include <tvm/tir/stmt.h>
+
+#include <optional>
+#include <sstream>
+#include <string>
+#include <variant>
+
+#include "../../arith/ir_visitor_with_analyzer.h"
+
+namespace tvm {
+namespace tir {
+
+std::variant<MemCpyDetails, std::string> IdentifyMemCpyImpl(const For& loop,
+                                                            arith::Analyzer* analyzer) {
+  Map<Var, arith::IntSet> loop_intervals;
+  Map<Var, Range> loop_ranges;
+  PrimExpr total_loop_iterations = 1;
+
+  // Walk through the loopnest, stopping at the first loop whose body

Review Comment:
   Thank you, and fix applied.



-- 
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 diff in pull request #13947: [TIR][Analysis] Implement IdentifyMemCpy analysis function

Posted by "Hzfengsy (via GitHub)" <gi...@apache.org>.
Hzfengsy commented on code in PR #13947:
URL: https://github.com/apache/tvm/pull/13947#discussion_r1121271840


##########
src/tir/analysis/identify_memcpy.cc:
##########
@@ -0,0 +1,317 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tir/analysis/identify_memcpy.cc
+ * \brief Check if a loopnest is equivalent to memcpy
+ */
+
+#include <tvm/arith/bound.h>
+#include <tvm/arith/iter_affine_map.h>
+#include <tvm/runtime/container/optional.h>
+#include <tvm/tir/analysis.h>
+#include <tvm/tir/buffer.h>
+#include <tvm/tir/stmt.h>
+
+#include <optional>
+#include <sstream>
+#include <string>
+#include <variant>
+
+#include "../../arith/ir_visitor_with_analyzer.h"
+
+namespace tvm {
+namespace tir {
+
+std::variant<MemCpyDetails, std::string> IdentifyMemCpyImpl(const For& loop,

Review Comment:
   It's a bit tricky to return the result and error message at the same time. An alternate way is to raise a specific exception and catch from outside.



-- 
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] tvm-bot commented on pull request #13947: [TIR][Analysis] Implement IdentifyMemCpy analysis function

Posted by "tvm-bot (via GitHub)" <gi...@apache.org>.
tvm-bot commented on PR #13947:
URL: https://github.com/apache/tvm/pull/13947#issuecomment-1425966307

   <!---bot-comment-->
   
   Thanks for contributing to TVM! Please refer to the contributing guidelines https://tvm.apache.org/docs/contribute/ for useful information and tips. Please request code reviews from [Reviewers](https://github.com/apache/incubator-tvm/blob/master/CONTRIBUTORS.md#reviewers) by @-ing them in a comment.
   
   <!--bot-comment-ccs-start-->
    * cc @Hzfengsy, @junrushao, @quic-sanirudh, @shingjan <sub>See [#10317](https://github.com/apache/tvm/issues/10317) for details</sub><!--bot-comment-ccs-end-->
   
   <sub>Generated by [tvm-bot](https://github.com/apache/tvm/blob/main/ci/README.md#github-actions)</sub>


-- 
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] Lunderberg commented on a diff in pull request #13947: [TIR][Analysis] Implement IdentifyMemCpy analysis function

Posted by "Lunderberg (via GitHub)" <gi...@apache.org>.
Lunderberg commented on code in PR #13947:
URL: https://github.com/apache/tvm/pull/13947#discussion_r1124809290


##########
src/tir/analysis/identify_memcpy.cc:
##########
@@ -0,0 +1,317 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tir/analysis/identify_memcpy.cc
+ * \brief Check if a loopnest is equivalent to memcpy

Review Comment:
   Thank you, and fix applied.



-- 
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 merged pull request #13947: [TIR][Analysis] Implement IdentifyMemCpy analysis function

Posted by "Hzfengsy (via GitHub)" <gi...@apache.org>.
Hzfengsy merged PR #13947:
URL: https://github.com/apache/tvm/pull/13947


-- 
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 diff in pull request #13947: [TIR][Analysis] Implement IdentifyMemCpy analysis function

Posted by "Hzfengsy (via GitHub)" <gi...@apache.org>.
Hzfengsy commented on code in PR #13947:
URL: https://github.com/apache/tvm/pull/13947#discussion_r1121270254


##########
src/tir/analysis/identify_memcpy.cc:
##########
@@ -0,0 +1,317 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tir/analysis/identify_memcpy.cc
+ * \brief Check if a loopnest is equivalent to memcpy
+ */
+
+#include <tvm/arith/bound.h>
+#include <tvm/arith/iter_affine_map.h>
+#include <tvm/runtime/container/optional.h>
+#include <tvm/tir/analysis.h>
+#include <tvm/tir/buffer.h>
+#include <tvm/tir/stmt.h>
+
+#include <optional>
+#include <sstream>
+#include <string>
+#include <variant>
+
+#include "../../arith/ir_visitor_with_analyzer.h"
+
+namespace tvm {
+namespace tir {
+
+std::variant<MemCpyDetails, std::string> IdentifyMemCpyImpl(const For& loop,
+                                                            arith::Analyzer* analyzer) {
+  Map<Var, arith::IntSet> loop_intervals;
+  Map<Var, Range> loop_ranges;
+  PrimExpr total_loop_iterations = 1;
+
+  // Walk through the loopnest, stopping at the first loop whose body
+  // is not a loop.
+  Stmt stmt = loop;
+  while (auto* for_node = stmt.as<ForNode>()) {
+    loop_ranges.Set(for_node->loop_var, Range::FromMinExtent(for_node->min, for_node->extent));
+    loop_intervals.Set(for_node->loop_var,
+                       arith::IntSet::FromMinExtent(for_node->min, for_node->extent));
+    total_loop_iterations = total_loop_iterations * for_node->extent;
+
+    stmt = for_node->body;
+  }
+
+  BufferStore store;
+  if (auto* ptr = stmt.as<BufferStoreNode>()) {
+    store = GetRef<BufferStore>(ptr);
+  } else {
+    return static_cast<const std::stringstream&>(
+               std::stringstream()
+               << "Expected innermost loop to have BufferStore body, but instead found " << stmt)
+        .str();
+  }
+
+  BufferLoad load;
+  if (auto* ptr = store->value.as<BufferLoadNode>()) {
+    load = GetRef<BufferLoad>(ptr);
+  } else {
+    return static_cast<const std::stringstream&>(
+               std::stringstream()
+               << "Expected BufferStore's value to be BufferLoad, but instead found "
+               << store->value)
+        .str();
+  }
+
+  // Now, we have a BufferStore whose value is a BufferLoad.  Because
+  // non-flat physical indices are target-dependent, only handle cases
+  // where the buffer will be flattened to a 1-d physical buffer.
+  Array<PrimExpr> flattened_dst = store->buffer.OffsetOf(store->indices);
+  Array<PrimExpr> flattened_src = load->buffer.OffsetOf(load->indices);
+
+  if (flattened_dst.size() != 1 || flattened_src.size() != 1) {
+    return static_cast<const std::stringstream&>(
+               std::stringstream()
+               << "Expected flattened dimension of src/dest to be 1, but found"
+               << flattened_src.size() << "-d src and " << flattened_dst.size() << "-d dst")
+        .str();
+  }
+  PrimExpr src_index = flattened_src[0];
+  PrimExpr dst_index = flattened_dst[0];
+
+  // First check, do the input/output form affine subsets of their
+  // respective buffers?
+  //
+  // For example, should exclude the following, indices are not affine
+  //
+  // for i in T.serial(16):
+  //     B[i] = A[T.abs(i-8)]
+
+  auto src_iter_map = arith::DetectIterMap({src_index}, loop_ranges, Bool(true),
+                                           arith::IterMapLevel::Bijective, analyzer);
+  if (src_iter_map->errors.size()) {
+    return static_cast<const std::stringstream&>(std::stringstream()
+                                                 << "arith::DetectIterMap(src) returned "
+                                                 << src_iter_map->errors.size() << " errors: ["
+                                                 << src_iter_map->errors << "]"
+                                                 << " for src_index = " << src_index)
+        .str();
+  }
+  auto dst_iter_map = arith::DetectIterMap({dst_index}, loop_ranges, Bool(true),
+                                           arith::IterMapLevel::Bijective, analyzer);
+  if (dst_iter_map->errors.size()) {
+    return static_cast<const std::stringstream&>(std::stringstream()
+                                                 << "arith::DetectIterMap(dst) returned "
+                                                 << dst_iter_map->errors.size() << " errors: ["
+                                                 << dst_iter_map->errors << "]"
+                                                 << " for dst_index = " << dst_index)
+        .str();
+  }
+
+  // Second check, are those affine subsets contiguous?  If so, then
+  // the index expressions will visit every location between the min
+  // and the max.  This checks surjectivity over a linear region,
+  // which may not be the same as DetectIterMap's check of
+  // surjectivity over the affine subset.
+  //
+  // For example, should exclude the following, doesn't touch all
+  // output locations within the output region touched.
+  //
+  // for i in T.serial(16):
+  //     B[2*i] = A[i]
+  //
+  // Similarly, should exclude the following, doesn't touch all
+  // input locations within the input region touched.
+  //
+  // for i in T.serial(16):
+  //     B[i] = A[2*i]
+  total_loop_iterations = analyzer->Simplify(total_loop_iterations);
+  auto src_interval = analyzer->int_set(src_index, loop_intervals);
+  auto dst_interval = analyzer->int_set(dst_index, loop_intervals);
+
+  if (!src_interval.HasLowerBound() || !src_interval.HasUpperBound()) {
+    return static_cast<const std::stringstream&>(std::stringstream()
+                                                 << "Expected known bounds for src, but found "
+                                                 << src_interval << " for expression " << src_index)
+        .str();
+  }
+  if (!dst_interval.HasLowerBound() || !dst_interval.HasUpperBound()) {
+    return static_cast<const std::stringstream&>(std::stringstream()
+                                                 << "Expected known bounds for dst, but found "
+                                                 << dst_interval << " for expression " << dst_index)
+        .str();
+  }
+
+  {
+    PrimExpr must_prove = total_loop_iterations == src_interval.max() - src_interval.min() + 1;
+    PrimExpr simplified = analyzer->Simplify(must_prove);
+    if (!analyzer->CanProve(simplified)) {
+      return static_cast<const std::stringstream&>(
+                 std::stringstream()
+                 << "Mismatch between loop iterations (" << total_loop_iterations
+                 << ") and number of src indices touched (" << src_interval
+                 << ".  Equality to prove simplified to " << simplified)
+          .str();
+    }
+  }
+  {
+    PrimExpr must_prove = total_loop_iterations == dst_interval.max() - dst_interval.min() + 1;
+    PrimExpr simplified = analyzer->Simplify(must_prove);
+    if (!analyzer->CanProve(simplified)) {
+      return static_cast<const std::stringstream&>(
+                 std::stringstream()
+                 << "Mismatch between loop iterations (" << total_loop_iterations
+                 << ") and number of dst indices touched (" << dst_interval
+                 << ".  Equality to prove simplified to " << simplified)
+          .str();
+    }
+  }
+
+  // Thrid check, is there a transformation applied between the input

Review Comment:
   ```suggestion
     // Third check, is there a transformation applied between the input
   ```



-- 
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] Lunderberg commented on a diff in pull request #13947: [TIR][Analysis] Implement IdentifyMemCpy analysis function

Posted by "Lunderberg (via GitHub)" <gi...@apache.org>.
Lunderberg commented on code in PR #13947:
URL: https://github.com/apache/tvm/pull/13947#discussion_r1124807438


##########
src/tir/analysis/identify_memcpy.cc:
##########
@@ -0,0 +1,317 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tir/analysis/identify_memcpy.cc
+ * \brief Check if a loopnest is equivalent to memcpy
+ */
+
+#include <tvm/arith/bound.h>
+#include <tvm/arith/iter_affine_map.h>
+#include <tvm/runtime/container/optional.h>
+#include <tvm/tir/analysis.h>
+#include <tvm/tir/buffer.h>
+#include <tvm/tir/stmt.h>
+
+#include <optional>
+#include <sstream>
+#include <string>
+#include <variant>
+
+#include "../../arith/ir_visitor_with_analyzer.h"
+
+namespace tvm {
+namespace tir {
+
+std::variant<MemCpyDetails, std::string> IdentifyMemCpyImpl(const For& loop,
+                                                            arith::Analyzer* analyzer) {
+  Map<Var, arith::IntSet> loop_intervals;
+  Map<Var, Range> loop_ranges;
+  PrimExpr total_loop_iterations = 1;
+
+  // Walk through the loopnest, stopping at the first loop whose body
+  // is not a loop.
+  Stmt stmt = loop;
+  while (auto* for_node = stmt.as<ForNode>()) {
+    loop_ranges.Set(for_node->loop_var, Range::FromMinExtent(for_node->min, for_node->extent));
+    loop_intervals.Set(for_node->loop_var,
+                       arith::IntSet::FromMinExtent(for_node->min, for_node->extent));
+    total_loop_iterations = total_loop_iterations * for_node->extent;
+
+    stmt = for_node->body;
+  }
+
+  BufferStore store;
+  if (auto* ptr = stmt.as<BufferStoreNode>()) {
+    store = GetRef<BufferStore>(ptr);
+  } else {
+    return static_cast<const std::stringstream&>(
+               std::stringstream()
+               << "Expected innermost loop to have BufferStore body, but instead found " << stmt)
+        .str();
+  }
+
+  BufferLoad load;
+  if (auto* ptr = store->value.as<BufferLoadNode>()) {
+    load = GetRef<BufferLoad>(ptr);
+  } else {
+    return static_cast<const std::stringstream&>(
+               std::stringstream()
+               << "Expected BufferStore's value to be BufferLoad, but instead found "
+               << store->value)
+        .str();
+  }
+
+  // Now, we have a BufferStore whose value is a BufferLoad.  Because
+  // non-flat physical indices are target-dependent, only handle cases
+  // where the buffer will be flattened to a 1-d physical buffer.
+  Array<PrimExpr> flattened_dst = store->buffer.OffsetOf(store->indices);
+  Array<PrimExpr> flattened_src = load->buffer.OffsetOf(load->indices);
+
+  if (flattened_dst.size() != 1 || flattened_src.size() != 1) {
+    return static_cast<const std::stringstream&>(
+               std::stringstream()
+               << "Expected flattened dimension of src/dest to be 1, but found"
+               << flattened_src.size() << "-d src and " << flattened_dst.size() << "-d dst")
+        .str();
+  }
+  PrimExpr src_index = flattened_src[0];
+  PrimExpr dst_index = flattened_dst[0];
+
+  // First check, do the input/output form affine subsets of their
+  // respective buffers?
+  //
+  // For example, should exclude the following, indices are not affine
+  //
+  // for i in T.serial(16):
+  //     B[i] = A[T.abs(i-8)]
+
+  auto src_iter_map = arith::DetectIterMap({src_index}, loop_ranges, Bool(true),
+                                           arith::IterMapLevel::Bijective, analyzer);
+  if (src_iter_map->errors.size()) {
+    return static_cast<const std::stringstream&>(std::stringstream()
+                                                 << "arith::DetectIterMap(src) returned "
+                                                 << src_iter_map->errors.size() << " errors: ["
+                                                 << src_iter_map->errors << "]"
+                                                 << " for src_index = " << src_index)
+        .str();
+  }
+  auto dst_iter_map = arith::DetectIterMap({dst_index}, loop_ranges, Bool(true),
+                                           arith::IterMapLevel::Bijective, analyzer);
+  if (dst_iter_map->errors.size()) {
+    return static_cast<const std::stringstream&>(std::stringstream()
+                                                 << "arith::DetectIterMap(dst) returned "
+                                                 << dst_iter_map->errors.size() << " errors: ["
+                                                 << dst_iter_map->errors << "]"
+                                                 << " for dst_index = " << dst_index)
+        .str();
+  }
+
+  // Second check, are those affine subsets contiguous?  If so, then
+  // the index expressions will visit every location between the min
+  // and the max.  This checks surjectivity over a linear region,
+  // which may not be the same as DetectIterMap's check of
+  // surjectivity over the affine subset.
+  //
+  // For example, should exclude the following, doesn't touch all
+  // output locations within the output region touched.
+  //
+  // for i in T.serial(16):
+  //     B[2*i] = A[i]
+  //
+  // Similarly, should exclude the following, doesn't touch all
+  // input locations within the input region touched.
+  //
+  // for i in T.serial(16):
+  //     B[i] = A[2*i]
+  total_loop_iterations = analyzer->Simplify(total_loop_iterations);
+  auto src_interval = analyzer->int_set(src_index, loop_intervals);
+  auto dst_interval = analyzer->int_set(dst_index, loop_intervals);
+
+  if (!src_interval.HasLowerBound() || !src_interval.HasUpperBound()) {
+    return static_cast<const std::stringstream&>(std::stringstream()
+                                                 << "Expected known bounds for src, but found "
+                                                 << src_interval << " for expression " << src_index)
+        .str();
+  }
+  if (!dst_interval.HasLowerBound() || !dst_interval.HasUpperBound()) {
+    return static_cast<const std::stringstream&>(std::stringstream()
+                                                 << "Expected known bounds for dst, but found "
+                                                 << dst_interval << " for expression " << dst_index)
+        .str();
+  }
+
+  {
+    PrimExpr must_prove = total_loop_iterations == src_interval.max() - src_interval.min() + 1;
+    PrimExpr simplified = analyzer->Simplify(must_prove);
+    if (!analyzer->CanProve(simplified)) {
+      return static_cast<const std::stringstream&>(
+                 std::stringstream()
+                 << "Mismatch between loop iterations (" << total_loop_iterations
+                 << ") and number of src indices touched (" << src_interval
+                 << ".  Equality to prove simplified to " << simplified)
+          .str();
+    }
+  }
+  {
+    PrimExpr must_prove = total_loop_iterations == dst_interval.max() - dst_interval.min() + 1;
+    PrimExpr simplified = analyzer->Simplify(must_prove);
+    if (!analyzer->CanProve(simplified)) {
+      return static_cast<const std::stringstream&>(
+                 std::stringstream()
+                 << "Mismatch between loop iterations (" << total_loop_iterations
+                 << ") and number of dst indices touched (" << dst_interval
+                 << ".  Equality to prove simplified to " << simplified)
+          .str();
+    }
+  }
+
+  // Thrid check, is there a transformation applied between the input
+  // and output iterators?
+  //
+  // For example, the following would pass all checks so far, but
+  // converts between row-major and column-major layouts, and could
+  // not be specified as a memcpy.
+  //
+  // for i,j in T.grid(4,4):
+  //     B[i,j] = A[j,i]
+
+  auto src_iter_sum = src_iter_map->indices[0];
+  auto dst_iter_sum = dst_iter_map->indices[0];
+
+  if (src_iter_sum->args.size() != dst_iter_sum->args.size()) {
+    return static_cast<const std::stringstream&>(
+               std::stringstream()
+               << "IterMap for src/dst unpacked to different number of IterSplitExpr: "
+               << src_iter_sum->args.size() << " for src, " << dst_iter_sum->args.size()
+               << " for dst.  "
+               << "IterMaps were detected as src = " << src_iter_sum << ", dst = " << dst_iter_sum)
+        .str();
+  }
+  std::vector<arith::IterSplitExpr> src_iter_terms(src_iter_sum->args.begin(),
+                                                   src_iter_sum->args.end());
+  std::vector<arith::IterSplitExpr> dst_iter_terms(dst_iter_sum->args.begin(),
+                                                   dst_iter_sum->args.end());
+
+  auto make_comparison_tuple = [](const arith::IterSplitExpr& expr) {
+    auto as_int_or_zero = [](auto& val) -> int64_t {
+      if (auto* as_int = val.template as<IntImmNode>()) {
+        return as_int->value;
+      } else {
+        return 0;
+      }
+    };
+    return std::tuple{
+        static_cast<bool>(expr->scale.as<IntImmNode>()),        as_int_or_zero(expr->scale),
+        static_cast<bool>(expr->extent.as<IntImmNode>()),       as_int_or_zero(expr->lower_factor),
+        static_cast<bool>(expr->lower_factor.as<IntImmNode>()), as_int_or_zero(expr->lower_factor),
+    };
+  };
+  auto sorting_function = [&make_comparison_tuple](const arith::IterSplitExpr& lhs,
+                                                   const arith::IterSplitExpr& rhs) -> bool {
+    return make_comparison_tuple(lhs) < make_comparison_tuple(rhs);
+  };
+  std::sort(src_iter_terms.begin(), src_iter_terms.end(), sorting_function);
+  std::sort(dst_iter_terms.begin(), dst_iter_terms.end(), sorting_function);
+
+  for (size_t i = 0; i < src_iter_terms.size(); i++) {
+    const arith::IterSplitExpr& src_term = src_iter_terms[i];
+    const arith::IterSplitExpr& dst_term = dst_iter_terms[i];
+
+    if (!analyzer->CanProve(
+            arith::NormalizeIterMapToExpr(src_term->source->source == dst_term->source->source))) {
+      return static_cast<const std::stringstream&>(
+                 std::stringstream()
+                 << "Term " << i << " had different source, src_term->source = " << src_term->source
+                 << ", dst_term->source = " << dst_term->source)
+          .str();
+    }
+    if (!analyzer->CanProve(src_term->lower_factor == dst_term->lower_factor)) {
+      return static_cast<const std::stringstream&>(
+                 std::stringstream()
+                 << "Term " << i << " had different lower_factor, src_term->lower_factor = "
+                 << src_term->lower_factor
+                 << ", dst_term->lower_factor = " << dst_term->lower_factor)
+          .str();
+    }
+    if (!analyzer->CanProve(src_term->extent == dst_term->extent)) {
+      return static_cast<const std::stringstream&>(
+                 std::stringstream()
+                 << "Term " << i << " had different extent, src_term->extent = " << src_term->extent
+                 << ", dst_term->extent = " << dst_term->extent)
+          .str();
+    }
+    if (!analyzer->CanProve(src_term->scale == dst_term->scale)) {
+      return static_cast<const std::stringstream&>(
+                 std::stringstream()
+                 << "Term " << i << " had different scale, src_term->scale = " << src_term->scale
+                 << ", dst_term->scale = " << dst_term->scale)
+          .str();
+    }
+  }
+
+  BufferRegion src_region(load->buffer, arith::DomainTouched(loop, load->buffer, true, true));
+  BufferRegion dst_region(store->buffer, arith::DomainTouched(loop, store->buffer, true, true));
+
+  return MemCpyDetails{src_region, dst_region};
+}
+
+std::optional<MemCpyDetails> IdentifyMemCpy(const For& loop, arith::Analyzer* analyzer) {
+  auto result = IdentifyMemCpyImpl(loop, analyzer);
+  if (auto* ptr = std::get_if<MemCpyDetails>(&result)) {
+    return *ptr;
+  } else {
+    return std::nullopt;
+  }
+}
+
+// Expose the IdentifyMemCpy functionality to Python API for purpose
+// of unit testing.

Review Comment:
   Looks good, and updated.



-- 
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] Lunderberg commented on a diff in pull request #13947: [TIR][Analysis] Implement IdentifyMemCpy analysis function

Posted by "Lunderberg (via GitHub)" <gi...@apache.org>.
Lunderberg commented on code in PR #13947:
URL: https://github.com/apache/tvm/pull/13947#discussion_r1124817646


##########
src/tir/analysis/identify_memcpy.cc:
##########
@@ -0,0 +1,317 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tir/analysis/identify_memcpy.cc
+ * \brief Check if a loopnest is equivalent to memcpy
+ */
+
+#include <tvm/arith/bound.h>
+#include <tvm/arith/iter_affine_map.h>
+#include <tvm/runtime/container/optional.h>
+#include <tvm/tir/analysis.h>
+#include <tvm/tir/buffer.h>
+#include <tvm/tir/stmt.h>
+
+#include <optional>
+#include <sstream>
+#include <string>
+#include <variant>
+
+#include "../../arith/ir_visitor_with_analyzer.h"
+
+namespace tvm {
+namespace tir {
+
+std::variant<MemCpyDetails, std::string> IdentifyMemCpyImpl(const For& loop,

Review Comment:
   Agreed, though I think the `std::variant` avoids most of the usual issues.  Rather than returning both the result and the error message at the same time, it returns either the result or the error message, but never both.  I didn't want to throw an exception, as I expect the "error" case to be much more common, which is why the public-facing API exposes a `std::optional<MemCpyDetails>` instead of the internal variant.



-- 
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 diff in pull request #13947: [TIR][Analysis] Implement IdentifyMemCpy analysis function

Posted by "Hzfengsy (via GitHub)" <gi...@apache.org>.
Hzfengsy commented on code in PR #13947:
URL: https://github.com/apache/tvm/pull/13947#discussion_r1121269052


##########
src/tir/analysis/identify_memcpy.cc:
##########
@@ -0,0 +1,317 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tir/analysis/identify_memcpy.cc
+ * \brief Check if a loopnest is equivalent to memcpy

Review Comment:
   ```suggestion
    * \brief Check if a loop nest is equivalent to memcpy
   ```



##########
src/tir/analysis/identify_memcpy.cc:
##########
@@ -0,0 +1,317 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tir/analysis/identify_memcpy.cc
+ * \brief Check if a loopnest is equivalent to memcpy
+ */
+
+#include <tvm/arith/bound.h>
+#include <tvm/arith/iter_affine_map.h>
+#include <tvm/runtime/container/optional.h>
+#include <tvm/tir/analysis.h>
+#include <tvm/tir/buffer.h>
+#include <tvm/tir/stmt.h>
+
+#include <optional>
+#include <sstream>
+#include <string>
+#include <variant>
+
+#include "../../arith/ir_visitor_with_analyzer.h"
+
+namespace tvm {
+namespace tir {
+
+std::variant<MemCpyDetails, std::string> IdentifyMemCpyImpl(const For& loop,
+                                                            arith::Analyzer* analyzer) {
+  Map<Var, arith::IntSet> loop_intervals;
+  Map<Var, Range> loop_ranges;
+  PrimExpr total_loop_iterations = 1;
+
+  // Walk through the loopnest, stopping at the first loop whose body

Review Comment:
   ```suggestion
     // Walk through the loop nest, stopping at the first loop whose body
   ```



-- 
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 diff in pull request #13947: [TIR][Analysis] Implement IdentifyMemCpy analysis function

Posted by "Hzfengsy (via GitHub)" <gi...@apache.org>.
Hzfengsy commented on code in PR #13947:
URL: https://github.com/apache/tvm/pull/13947#discussion_r1121273499


##########
tests/python/unittest/test_tir_analysis_identify_memcpy.py:
##########
@@ -0,0 +1,325 @@
+# 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.
+
+import re
+
+import pytest
+
+import tvm
+import tvm.testing
+from tvm import te, topi

Review Comment:
   Please remove this line if it's not used



##########
src/tir/analysis/identify_memcpy.cc:
##########
@@ -0,0 +1,317 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tir/analysis/identify_memcpy.cc
+ * \brief Check if a loopnest is equivalent to memcpy
+ */
+
+#include <tvm/arith/bound.h>
+#include <tvm/arith/iter_affine_map.h>
+#include <tvm/runtime/container/optional.h>
+#include <tvm/tir/analysis.h>
+#include <tvm/tir/buffer.h>
+#include <tvm/tir/stmt.h>
+
+#include <optional>
+#include <sstream>
+#include <string>
+#include <variant>
+
+#include "../../arith/ir_visitor_with_analyzer.h"
+
+namespace tvm {
+namespace tir {
+
+std::variant<MemCpyDetails, std::string> IdentifyMemCpyImpl(const For& loop,
+                                                            arith::Analyzer* analyzer) {
+  Map<Var, arith::IntSet> loop_intervals;
+  Map<Var, Range> loop_ranges;
+  PrimExpr total_loop_iterations = 1;
+
+  // Walk through the loopnest, stopping at the first loop whose body
+  // is not a loop.
+  Stmt stmt = loop;
+  while (auto* for_node = stmt.as<ForNode>()) {
+    loop_ranges.Set(for_node->loop_var, Range::FromMinExtent(for_node->min, for_node->extent));
+    loop_intervals.Set(for_node->loop_var,
+                       arith::IntSet::FromMinExtent(for_node->min, for_node->extent));
+    total_loop_iterations = total_loop_iterations * for_node->extent;
+
+    stmt = for_node->body;
+  }
+
+  BufferStore store;
+  if (auto* ptr = stmt.as<BufferStoreNode>()) {
+    store = GetRef<BufferStore>(ptr);
+  } else {
+    return static_cast<const std::stringstream&>(
+               std::stringstream()
+               << "Expected innermost loop to have BufferStore body, but instead found " << stmt)
+        .str();
+  }
+
+  BufferLoad load;
+  if (auto* ptr = store->value.as<BufferLoadNode>()) {
+    load = GetRef<BufferLoad>(ptr);
+  } else {
+    return static_cast<const std::stringstream&>(
+               std::stringstream()
+               << "Expected BufferStore's value to be BufferLoad, but instead found "
+               << store->value)
+        .str();
+  }
+
+  // Now, we have a BufferStore whose value is a BufferLoad.  Because
+  // non-flat physical indices are target-dependent, only handle cases
+  // where the buffer will be flattened to a 1-d physical buffer.
+  Array<PrimExpr> flattened_dst = store->buffer.OffsetOf(store->indices);
+  Array<PrimExpr> flattened_src = load->buffer.OffsetOf(load->indices);
+
+  if (flattened_dst.size() != 1 || flattened_src.size() != 1) {
+    return static_cast<const std::stringstream&>(
+               std::stringstream()
+               << "Expected flattened dimension of src/dest to be 1, but found"
+               << flattened_src.size() << "-d src and " << flattened_dst.size() << "-d dst")
+        .str();
+  }
+  PrimExpr src_index = flattened_src[0];
+  PrimExpr dst_index = flattened_dst[0];
+
+  // First check, do the input/output form affine subsets of their
+  // respective buffers?
+  //
+  // For example, should exclude the following, indices are not affine
+  //
+  // for i in T.serial(16):
+  //     B[i] = A[T.abs(i-8)]
+
+  auto src_iter_map = arith::DetectIterMap({src_index}, loop_ranges, Bool(true),
+                                           arith::IterMapLevel::Bijective, analyzer);
+  if (src_iter_map->errors.size()) {
+    return static_cast<const std::stringstream&>(std::stringstream()
+                                                 << "arith::DetectIterMap(src) returned "
+                                                 << src_iter_map->errors.size() << " errors: ["
+                                                 << src_iter_map->errors << "]"
+                                                 << " for src_index = " << src_index)
+        .str();
+  }
+  auto dst_iter_map = arith::DetectIterMap({dst_index}, loop_ranges, Bool(true),
+                                           arith::IterMapLevel::Bijective, analyzer);
+  if (dst_iter_map->errors.size()) {
+    return static_cast<const std::stringstream&>(std::stringstream()
+                                                 << "arith::DetectIterMap(dst) returned "
+                                                 << dst_iter_map->errors.size() << " errors: ["
+                                                 << dst_iter_map->errors << "]"
+                                                 << " for dst_index = " << dst_index)
+        .str();
+  }
+
+  // Second check, are those affine subsets contiguous?  If so, then
+  // the index expressions will visit every location between the min
+  // and the max.  This checks surjectivity over a linear region,
+  // which may not be the same as DetectIterMap's check of
+  // surjectivity over the affine subset.
+  //
+  // For example, should exclude the following, doesn't touch all
+  // output locations within the output region touched.
+  //
+  // for i in T.serial(16):
+  //     B[2*i] = A[i]
+  //
+  // Similarly, should exclude the following, doesn't touch all
+  // input locations within the input region touched.
+  //
+  // for i in T.serial(16):
+  //     B[i] = A[2*i]
+  total_loop_iterations = analyzer->Simplify(total_loop_iterations);
+  auto src_interval = analyzer->int_set(src_index, loop_intervals);
+  auto dst_interval = analyzer->int_set(dst_index, loop_intervals);
+
+  if (!src_interval.HasLowerBound() || !src_interval.HasUpperBound()) {
+    return static_cast<const std::stringstream&>(std::stringstream()
+                                                 << "Expected known bounds for src, but found "
+                                                 << src_interval << " for expression " << src_index)
+        .str();
+  }
+  if (!dst_interval.HasLowerBound() || !dst_interval.HasUpperBound()) {
+    return static_cast<const std::stringstream&>(std::stringstream()
+                                                 << "Expected known bounds for dst, but found "
+                                                 << dst_interval << " for expression " << dst_index)
+        .str();
+  }
+
+  {
+    PrimExpr must_prove = total_loop_iterations == src_interval.max() - src_interval.min() + 1;
+    PrimExpr simplified = analyzer->Simplify(must_prove);
+    if (!analyzer->CanProve(simplified)) {
+      return static_cast<const std::stringstream&>(
+                 std::stringstream()
+                 << "Mismatch between loop iterations (" << total_loop_iterations
+                 << ") and number of src indices touched (" << src_interval
+                 << ".  Equality to prove simplified to " << simplified)
+          .str();
+    }
+  }
+  {
+    PrimExpr must_prove = total_loop_iterations == dst_interval.max() - dst_interval.min() + 1;
+    PrimExpr simplified = analyzer->Simplify(must_prove);
+    if (!analyzer->CanProve(simplified)) {
+      return static_cast<const std::stringstream&>(
+                 std::stringstream()
+                 << "Mismatch between loop iterations (" << total_loop_iterations
+                 << ") and number of dst indices touched (" << dst_interval
+                 << ".  Equality to prove simplified to " << simplified)
+          .str();
+    }
+  }
+
+  // Thrid check, is there a transformation applied between the input
+  // and output iterators?
+  //
+  // For example, the following would pass all checks so far, but
+  // converts between row-major and column-major layouts, and could
+  // not be specified as a memcpy.
+  //
+  // for i,j in T.grid(4,4):
+  //     B[i,j] = A[j,i]
+
+  auto src_iter_sum = src_iter_map->indices[0];
+  auto dst_iter_sum = dst_iter_map->indices[0];
+
+  if (src_iter_sum->args.size() != dst_iter_sum->args.size()) {
+    return static_cast<const std::stringstream&>(
+               std::stringstream()
+               << "IterMap for src/dst unpacked to different number of IterSplitExpr: "
+               << src_iter_sum->args.size() << " for src, " << dst_iter_sum->args.size()
+               << " for dst.  "
+               << "IterMaps were detected as src = " << src_iter_sum << ", dst = " << dst_iter_sum)
+        .str();
+  }
+  std::vector<arith::IterSplitExpr> src_iter_terms(src_iter_sum->args.begin(),
+                                                   src_iter_sum->args.end());
+  std::vector<arith::IterSplitExpr> dst_iter_terms(dst_iter_sum->args.begin(),
+                                                   dst_iter_sum->args.end());
+
+  auto make_comparison_tuple = [](const arith::IterSplitExpr& expr) {
+    auto as_int_or_zero = [](auto& val) -> int64_t {
+      if (auto* as_int = val.template as<IntImmNode>()) {
+        return as_int->value;
+      } else {
+        return 0;
+      }
+    };
+    return std::tuple{
+        static_cast<bool>(expr->scale.as<IntImmNode>()),        as_int_or_zero(expr->scale),
+        static_cast<bool>(expr->extent.as<IntImmNode>()),       as_int_or_zero(expr->lower_factor),
+        static_cast<bool>(expr->lower_factor.as<IntImmNode>()), as_int_or_zero(expr->lower_factor),
+    };
+  };
+  auto sorting_function = [&make_comparison_tuple](const arith::IterSplitExpr& lhs,
+                                                   const arith::IterSplitExpr& rhs) -> bool {
+    return make_comparison_tuple(lhs) < make_comparison_tuple(rhs);
+  };
+  std::sort(src_iter_terms.begin(), src_iter_terms.end(), sorting_function);
+  std::sort(dst_iter_terms.begin(), dst_iter_terms.end(), sorting_function);
+
+  for (size_t i = 0; i < src_iter_terms.size(); i++) {
+    const arith::IterSplitExpr& src_term = src_iter_terms[i];
+    const arith::IterSplitExpr& dst_term = dst_iter_terms[i];
+
+    if (!analyzer->CanProve(
+            arith::NormalizeIterMapToExpr(src_term->source->source == dst_term->source->source))) {
+      return static_cast<const std::stringstream&>(
+                 std::stringstream()
+                 << "Term " << i << " had different source, src_term->source = " << src_term->source
+                 << ", dst_term->source = " << dst_term->source)
+          .str();
+    }
+    if (!analyzer->CanProve(src_term->lower_factor == dst_term->lower_factor)) {
+      return static_cast<const std::stringstream&>(
+                 std::stringstream()
+                 << "Term " << i << " had different lower_factor, src_term->lower_factor = "
+                 << src_term->lower_factor
+                 << ", dst_term->lower_factor = " << dst_term->lower_factor)
+          .str();
+    }
+    if (!analyzer->CanProve(src_term->extent == dst_term->extent)) {
+      return static_cast<const std::stringstream&>(
+                 std::stringstream()
+                 << "Term " << i << " had different extent, src_term->extent = " << src_term->extent
+                 << ", dst_term->extent = " << dst_term->extent)
+          .str();
+    }
+    if (!analyzer->CanProve(src_term->scale == dst_term->scale)) {
+      return static_cast<const std::stringstream&>(
+                 std::stringstream()
+                 << "Term " << i << " had different scale, src_term->scale = " << src_term->scale
+                 << ", dst_term->scale = " << dst_term->scale)
+          .str();
+    }
+  }
+
+  BufferRegion src_region(load->buffer, arith::DomainTouched(loop, load->buffer, true, true));
+  BufferRegion dst_region(store->buffer, arith::DomainTouched(loop, store->buffer, true, true));
+
+  return MemCpyDetails{src_region, dst_region};
+}
+
+std::optional<MemCpyDetails> IdentifyMemCpy(const For& loop, arith::Analyzer* analyzer) {
+  auto result = IdentifyMemCpyImpl(loop, analyzer);
+  if (auto* ptr = std::get_if<MemCpyDetails>(&result)) {
+    return *ptr;
+  } else {
+    return std::nullopt;
+  }
+}
+
+// Expose the IdentifyMemCpy functionality to Python API for purpose
+// of unit testing.

Review Comment:
   ```suggestion
   // Expose the IdentifyMemCpy functionality to Python API for purpose of unit testing.
   ```



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