From 6834e799b01528cb5f22e9d6fa53af0c0452e43a Mon Sep 17 00:00:00 2001 From: Eric Lunderberg Date: Fri, 2 Sep 2022 12:44:46 -0500 Subject: [PATCH 1/2] [Runtime][Bugfix] Added type-checking for Array::insert Prior to this commit, the following code would compile and run without error. This occurs because the typed `Array::insert` calls the untyped `ArrayNode::InitRange`, with no type-checking done before the call. ```c++ Var x("x"); Var y("y"); Array var_arr{x, y}; Array expr_arr{x + 1, y + 2}; // Erroneously inserts static-type PrimExpr, runtime-type Add, even // though neither PrimExpr is a type of Var. var_arr.insert(var_arr.begin(), expr_arr.begin(), expr_arr.end()); ``` After this commit, a `static_assert` in `Array::insert` and in `Array::Array(IterType,IterTYpe)` restricts the iterators, such that they must dereference to `T`, `Optional`, a subclass of `T`, or `Optional` where `U` is a subclass of `T`. The public method `ArrayNode::SetItem` exposes a similar issue. In the future, we may want to make it be private, accessed only through type-safe method in `Array::Set`. --- include/tvm/runtime/container/array.h | 5 +++++ src/te/schedule/schedule_lang.cc | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/include/tvm/runtime/container/array.h b/include/tvm/runtime/container/array.h index 11bacb18e92c..1b735e73c386 100644 --- a/include/tvm/runtime/container/array.h +++ b/include/tvm/runtime/container/array.h @@ -325,6 +325,8 @@ class Array : public ObjectRef { */ template Array(IterType first, IterType last) { + static_assert(is_valid_iterator_v, + "IterType cannot be inserted into a tvm::Array"); Assign(first, last); } @@ -481,6 +483,9 @@ class Array : public ObjectRef { */ template void insert(iterator position, IterType first, IterType last) { + static_assert(is_valid_iterator_v, + "IterType cannot be inserted into a tvm::Array"); + if (first == last) { return; } diff --git a/src/te/schedule/schedule_lang.cc b/src/te/schedule/schedule_lang.cc index 0fcd6133c4a2..e8f4f65eb651 100644 --- a/src/te/schedule/schedule_lang.cc +++ b/src/te/schedule/schedule_lang.cc @@ -200,7 +200,7 @@ Stage& Stage::env_threads(Array threads) { ICHECK_EQ(self->env_threads.size(), 0U) << "Already set env_threads"; Array& leaf_vars = self->leaf_iter_vars; Array& all_vars = self->all_iter_vars; - std::vector temp; + std::vector temp; for (IterVar iv : threads) { temp.push_back(iv); } From cc111045e9a8d1d356364f1704a4e914f021c85a Mon Sep 17 00:00:00 2001 From: Eric Lunderberg Date: Thu, 22 Sep 2022 11:16:05 -0500 Subject: [PATCH 2/2] CI bump