Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cleanup] Remove using namespace tvm::runtime from headers #17246

Merged

Conversation

Lunderberg
Copy link
Contributor

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.

@Lunderberg Lunderberg requested a review from tqchen August 6, 2024 15:57
Lunderberg added a commit to Lunderberg/tvm that referenced this pull request Aug 6, 2024
Re-apply the change from upstream
apache#16343.  See
apache#17246 for an example of the type of
bug that can result from MSVC's default non-standard name resolution.
Copy link
Member

@tqchen tqchen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

temp block this in light of #16183 (comment)

@Lunderberg
Copy link
Contributor Author

temp block this in light of #16183 (comment)

Good point. I think this change is beneficial regardless of #16183, but the FFI changes touch so many parts of TVM that I wouldn't want to declare them 100% independent.

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
apache#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.
@Lunderberg Lunderberg force-pushed the cleanup_remove_using_namespace_tvm_runtime branch from 757518d to 4a01e78 Compare August 19, 2024 20:52
@Lunderberg
Copy link
Contributor Author

Rebased these changes onto main, and this PR should be land-able now that the performance regressions from #16183 have been resolved.

@tqchen tqchen merged commit 20289e8 into apache:main Aug 22, 2024
18 of 19 checks passed
@Lunderberg Lunderberg deleted the cleanup_remove_using_namespace_tvm_runtime branch August 22, 2024 17:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants