Skip to content

Commit

Permalink
[Cleanup] Remove using namespace tvm::runtime from headers (#17246)
Browse files Browse the repository at this point in the history
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
<tvm/relay/attrs/transform.h>` 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
#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.
  • Loading branch information
Lunderberg authored Aug 22, 2024
1 parent ed9aa56 commit 20289e8
Show file tree
Hide file tree
Showing 9 changed files with 10 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/contrib/msc/core/ir/graph_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion src/relay/backend/vm/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 2 additions & 0 deletions src/relay/parser/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ namespace relay {
/*! \brief The meta table maps from type key to a sequence of objects. */
using MetaTable = Map<String, Array<ObjectRef>>;

using tvm::runtime::NDArray;
using tvm::runtime::String2DLDataType;
using tvm::transform::CreateModulePass;
using tvm::transform::PassContext;

Expand Down
2 changes: 0 additions & 2 deletions src/relay/parser/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
namespace tvm {
namespace relay {

using namespace runtime;

enum class TokenType {
kCommentStart,
kCommentEnd,
Expand Down
2 changes: 0 additions & 2 deletions src/relay/parser/tokenizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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); }));
Expand Down
5 changes: 4 additions & 1 deletion src/runtime/contrib/cblas/gemm_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/runtime/contrib/json/json_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ namespace tvm {
namespace runtime {
namespace json {

using namespace tvm::runtime;
using JSONGraphAttrs = std::unordered_map<std::string, dmlc::any>;

/*!
Expand Down
1 change: 0 additions & 1 deletion src/runtime/contrib/nnpack/nnpack_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

namespace tvm {
namespace contrib {
using namespace runtime;

struct NNPackThreadLocalEntry {
pthreadpool_t threadpool{nullptr};
Expand Down
1 change: 0 additions & 1 deletion src/runtime/contrib/verilator/verilator_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ namespace tvm {
namespace runtime {
namespace contrib {

using namespace tvm::runtime;
using namespace tvm::runtime::contrib;
using namespace tvm::runtime::json;

Expand Down

0 comments on commit 20289e8

Please sign in to comment.