From 22612d1f45dcc1e97df681fd360880a25b2a47ab Mon Sep 17 00:00:00 2001 From: Yi Xu Date: Tue, 6 Dec 2022 21:44:46 +0800 Subject: [PATCH 1/3] [Bug] Avoid overwriting global tmp with dynamic_index=True --- taichi/transforms/offload.cpp | 4 +--- tests/python/test_matrix.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/taichi/transforms/offload.cpp b/taichi/transforms/offload.cpp index 3e20aeab309f7..e74a35a3fdf95 100644 --- a/taichi/transforms/offload.cpp +++ b/taichi/transforms/offload.cpp @@ -535,9 +535,7 @@ class FixCrossOffloadReferences : public BasicStmtVisitor { auto const_zero_stmt = replacement.push_back(zero); stmt_to_offloaded_[const_zero_stmt] = offloaded; for (int i = 0; i < tensor_type->get_num_elements(); ++i) { - TypedConstant offset(i * - data_type_size(tensor_type->get_element_type())); - auto const_offset_stmt = replacement.push_back(offset); + auto const_offset_stmt = replacement.push_back(TypedConstant(i)); auto ptr_offset_stmt = replacement.push_back(ptr, const_offset_stmt); auto global_store_stmt = replacement.push_back( diff --git a/tests/python/test_matrix.py b/tests/python/test_matrix.py index 55e5b55883564..8899df33c5417 100644 --- a/tests/python/test_matrix.py +++ b/tests/python/test_matrix.py @@ -1209,3 +1209,23 @@ def foo(): r"`transpose\(\)` cannot apply to a vector. If you want something like `a @ b.transpose\(\)`, write `a.outer_product\(b\)` instead." ): foo() + + +@test_utils.test(require=ti.extension.dynamic_index, dynamic_index=True, debug=True) +def test_global_tmp_overwrite(): + # https://github.com/taichi-dev/taichi/issues/6663 + @ti.kernel + def foo(): + p = ti.Matrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) + loop = 1 + sig = ti.Vector([0, 0, 0, 0]) + assert p[0, 0] == 1 + while loop == 1: + assert p[0, 0] == 1 + loop = 0 + p[0, 0] = -1 + for i in range(1): + sig[i] = 2 + print(sig, p) + + foo() From cf4571c721324e03eb435c134bae7a68d06a8047 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 6 Dec 2022 13:47:49 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- taichi/transforms/offload.cpp | 3 ++- tests/python/test_matrix.py | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/taichi/transforms/offload.cpp b/taichi/transforms/offload.cpp index e74a35a3fdf95..68b26c0ea1fb4 100644 --- a/taichi/transforms/offload.cpp +++ b/taichi/transforms/offload.cpp @@ -535,7 +535,8 @@ class FixCrossOffloadReferences : public BasicStmtVisitor { auto const_zero_stmt = replacement.push_back(zero); stmt_to_offloaded_[const_zero_stmt] = offloaded; for (int i = 0; i < tensor_type->get_num_elements(); ++i) { - auto const_offset_stmt = replacement.push_back(TypedConstant(i)); + auto const_offset_stmt = + replacement.push_back(TypedConstant(i)); auto ptr_offset_stmt = replacement.push_back(ptr, const_offset_stmt); auto global_store_stmt = replacement.push_back( diff --git a/tests/python/test_matrix.py b/tests/python/test_matrix.py index 8899df33c5417..226e690b31b3f 100644 --- a/tests/python/test_matrix.py +++ b/tests/python/test_matrix.py @@ -1211,7 +1211,9 @@ def foo(): foo() -@test_utils.test(require=ti.extension.dynamic_index, dynamic_index=True, debug=True) +@test_utils.test(require=ti.extension.dynamic_index, + dynamic_index=True, + debug=True) def test_global_tmp_overwrite(): # https://github.com/taichi-dev/taichi/issues/6663 @ti.kernel From 5995e0f8e7a8a8cf8ca68d00ad0644e64f57bfa2 Mon Sep 17 00:00:00 2001 From: Yi Xu Date: Wed, 7 Dec 2022 10:56:14 +0800 Subject: [PATCH 3/3] Make the test work in cuda --- tests/python/test_matrix.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/python/test_matrix.py b/tests/python/test_matrix.py index 226e690b31b3f..9f7525d53395f 100644 --- a/tests/python/test_matrix.py +++ b/tests/python/test_matrix.py @@ -1217,7 +1217,7 @@ def foo(): def test_global_tmp_overwrite(): # https://github.com/taichi-dev/taichi/issues/6663 @ti.kernel - def foo(): + def foo() -> ti.i32: p = ti.Matrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) loop = 1 sig = ti.Vector([0, 0, 0, 0]) @@ -1228,6 +1228,6 @@ def foo(): p[0, 0] = -1 for i in range(1): sig[i] = 2 - print(sig, p) + return sig.sum() + p.sum() - foo() + assert foo() == 4