From 757518da3a6cce8deefece204a04db41104ac3b2 Mon Sep 17 00:00:00 2001 From: Eric Lunderberg Date: Tue, 6 Aug 2024 10:37:45 -0500 Subject: [PATCH] [Cleanup] Remove `using namespace tvm::runtime` from headers Prior to this commit, various header files had `using namespace tvm::runtime`, which imports all names from `tvm::runtime` into the current namespace. These imports can cause compilation errors depending on the order of `#include` statements. For example, the `#include ` file uses the unqualified name `Bool` to refer to `::tvm::Bool`, a subclass of `PrimExpr`. If a different header file specifies `using namespace tvm::runtime` within the `tvm::relay` namespace, then the unqualified name `Bool` ambiguously refers to either `::tvm::Bool` or `::tvm::runtime::Bool`. In MSVC, this can cause even further compilation errors. By default, MSVC does not follow the C++ standard for name resolution in templates. The standard requires that any names in a template that do not depend on template parameters be resolved when the template is declared. However, MSVC instead resolves these names when the template is instantiated. As a result, the same `using namespace tvm::runtime` may cause a compilation error if it occurs after the template's declaration, but before the template's usage. (TVM provides the `/permissive-` flag to MSVC builds specifically to disable MSVC's non-standard name resolution, so this only impacts downstream forks that disable this flag. See https://github.com/apache/tvm/pull/16343 for more details.) This commit removes `using namespace tvm::runtime`, replacing them with explicit `using tvm::runtime::SOME_SPECIFIC_SYMBOL` where necessary. This resolves both the include-order dependency for standards-compliant compilers, and the compilation errors for MSVC's default build. --- src/contrib/msc/core/ir/graph_builder.h | 3 ++- src/relay/backend/vm/compiler.h | 3 ++- src/relay/parser/parser.cc | 2 ++ src/relay/parser/token.h | 2 -- src/relay/parser/tokenizer.h | 2 -- src/runtime/contrib/cblas/gemm_common.h | 5 ++++- src/runtime/contrib/json/json_node.h | 1 - src/runtime/contrib/nnpack/nnpack_utils.h | 1 - src/runtime/contrib/verilator/verilator_runtime.h | 1 - 9 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/contrib/msc/core/ir/graph_builder.h b/src/contrib/msc/core/ir/graph_builder.h index 4b042c5617e4c..d514a793475df 100644 --- a/src/contrib/msc/core/ir/graph_builder.h +++ b/src/contrib/msc/core/ir/graph_builder.h @@ -51,7 +51,8 @@ namespace msc { using Expr = tvm::RelayExpr; using RelaxExprVisitor = tvm::relax::ExprVisitor; using RelayExprVisitor = tvm::relay::ExprVisitor; -using namespace tvm::runtime; + +using tvm::runtime::NDArray; /*! * \brief Config for building MSCGraph. diff --git a/src/relay/backend/vm/compiler.h b/src/relay/backend/vm/compiler.h index acb4d2d1d258c..d22fb3d4d5ca2 100644 --- a/src/relay/backend/vm/compiler.h +++ b/src/relay/backend/vm/compiler.h @@ -51,7 +51,8 @@ namespace tvm { namespace relay { namespace vm { -using namespace tvm::runtime; +using tvm::runtime::ModulePropertyMask; +using tvm::runtime::NDArray; using namespace tvm::runtime::vm; using namespace relay::transform; diff --git a/src/relay/parser/parser.cc b/src/relay/parser/parser.cc index b519a1778ce04..233455bf89ba7 100644 --- a/src/relay/parser/parser.cc +++ b/src/relay/parser/parser.cc @@ -48,6 +48,8 @@ namespace relay { /*! \brief The meta table maps from type key to a sequence of objects. */ using MetaTable = Map>; +using tvm::runtime::NDArray; +using tvm::runtime::String2DLDataType; using tvm::transform::CreateModulePass; using tvm::transform::PassContext; diff --git a/src/relay/parser/token.h b/src/relay/parser/token.h index 7b11e701cf6ee..13875cb09391f 100644 --- a/src/relay/parser/token.h +++ b/src/relay/parser/token.h @@ -36,8 +36,6 @@ namespace tvm { namespace relay { -using namespace runtime; - enum class TokenType { kCommentStart, kCommentEnd, diff --git a/src/relay/parser/tokenizer.h b/src/relay/parser/tokenizer.h index 04dcd3263e99c..2b7ad4e5593e7 100644 --- a/src/relay/parser/tokenizer.h +++ b/src/relay/parser/tokenizer.h @@ -41,8 +41,6 @@ namespace tvm { namespace relay { -using namespace runtime; - // trim from start (in place) static inline void ltrim(std::string& s) { // NOLINT(*) s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) { return !std::isspace(ch); })); diff --git a/src/runtime/contrib/cblas/gemm_common.h b/src/runtime/contrib/cblas/gemm_common.h index af073da9ba1ad..91341976bd029 100644 --- a/src/runtime/contrib/cblas/gemm_common.h +++ b/src/runtime/contrib/cblas/gemm_common.h @@ -34,7 +34,10 @@ namespace tvm { namespace contrib { -using namespace runtime; +using runtime::TVMArgs; +using runtime::TVMRetValue; +using runtime::TypeMatch; + inline int ColumnStride(const DLTensor* tensor) { // If the tensor itself is transposed then it will have strides // backward from what we expect. Regardless, the max of the strides diff --git a/src/runtime/contrib/json/json_node.h b/src/runtime/contrib/json/json_node.h index bafe6cfbec18b..dd16c606815ad 100644 --- a/src/runtime/contrib/json/json_node.h +++ b/src/runtime/contrib/json/json_node.h @@ -42,7 +42,6 @@ namespace tvm { namespace runtime { namespace json { -using namespace tvm::runtime; using JSONGraphAttrs = std::unordered_map; /*! diff --git a/src/runtime/contrib/nnpack/nnpack_utils.h b/src/runtime/contrib/nnpack/nnpack_utils.h index 4396ea0bcde69..ed0312dac4769 100644 --- a/src/runtime/contrib/nnpack/nnpack_utils.h +++ b/src/runtime/contrib/nnpack/nnpack_utils.h @@ -30,7 +30,6 @@ namespace tvm { namespace contrib { -using namespace runtime; struct NNPackThreadLocalEntry { pthreadpool_t threadpool{nullptr}; diff --git a/src/runtime/contrib/verilator/verilator_runtime.h b/src/runtime/contrib/verilator/verilator_runtime.h index 9ef17d7481ab7..14bf0bcdfc9b6 100644 --- a/src/runtime/contrib/verilator/verilator_runtime.h +++ b/src/runtime/contrib/verilator/verilator_runtime.h @@ -43,7 +43,6 @@ namespace tvm { namespace runtime { namespace contrib { -using namespace tvm::runtime; using namespace tvm::runtime::contrib; using namespace tvm::runtime::json;