You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by "tqchen (via GitHub)" <gi...@apache.org> on 2026/04/27 15:20:42 UTC
[PR] [REFACTOR][RUNTIME] Macro cleanup — TVM_DLL alignment, [[maybe_unused]], logging.h legacy macros [tvm]
tqchen opened a new pull request, #19457:
URL: https://github.com/apache/tvm/pull/19457
## Summary
Three-phase macro cleanup addressing TVM's macro hygiene:
1. **Phase 1**: Restructured `TVM_DLL` and `TVM_RUNTIME_DLL` to match the cleaner pattern from tvm-ffi, adding layered Emscripten/MSVC/visibility-default fallthrough. Replaced `TVM_ATTRIBUTE_UNUSED` with C++17 `[[maybe_unused]]` (8 sites).
2. **Phase 2**: Deleted 5 legacy macros from `logging.h` (`TVM_NO_INLINE`, `TVM_ALWAYS_INLINE`, `TVM_THROW_EXCEPTION`, `TVM_LOG_STACK_TRACE`, `TVM_USE_LIBBACKTRACE`), migrating 30 use sites to tvm-ffi equivalents or direct syntax.
3. **Phase 3**: Removed `tvm::runtime::Error` and `tvm::runtime::InternalError` thin shims, routing all code through `tvm::ffi::Error`. Migrated 18+ call sites and updated include hygiene.
## Test plan
- [ ] **Build**: tvm-cpu/tvm-gpu Linux gcc visibility branch (primary) — exercises the `__attribute__((visibility("default")))` fallthrough for both DLL families and all `[[maybe_unused]]` sites. ~5 min build time.
- [ ] **Smoke tests**: `tests/python/all-platform-minimal-test/` (75/75 pass), `tests/python/tir-base/` (273/273 pass).
- [ ] **CPP tests**: `cpptest IRF.*` (8/8 pass).
- [ ] **Emscripten**: tvm-wasm exercises `__EMSCRIPTEN__` branch (all four DLL macros → `EMSCRIPTEN_KEEPALIVE`).
- [ ] **ARM**: tvm-arm Linux aarch64 visibility branch (same as cpu/gpu logically).
- [ ] **Lint**: `pre-commit run --all-files` passes (validates clang-format on rewritten base.h and migrated headers).
- [ ] **MSVC branch**: code inspection — `_MSC_VER` block preserves byte-identical dllexport/dllimport semantics vs. original.
- [ ] **Grep verification**: Zero remaining references to `TVM_ATTRIBUTE_UNUSED`, `TVM_NO_INLINE`, `TVM_ALWAYS_INLINE`, `TVM_THROW_EXCEPTION`, `TVM_LOG_STACK_TRACE`, `TVM_USE_LIBBACKTRACE`, `tvm::runtime::Error`, `tvm::runtime::InternalError`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [REFACTOR][RUNTIME] Macro cleanup — TVM_DLL alignment, [[maybe_unused]], logging.h legacy macros [tvm]
Posted by "tqchen (via GitHub)" <gi...@apache.org>.
tqchen commented on PR #19457:
URL: https://github.com/apache/tvm/pull/19457#issuecomment-4328880885
Fixed in commit 6608455eee. The android_rpc JNI wrapper was missed during Phase 3 of the macro cleanup since it's not built in the default Linux environment. I've migrated the error pattern to use `tvm::ffi::Error` with `TVMFFIBacktrace` to match the hexagon_common.cc pattern.
The change:
- Replaced `throw InternalError(file, lineno, message)` with `throw tvm::ffi::Error("InternalError", message, TVMFFIBacktrace(file.c_str(), lineno, "", 0))`
- Both `tvm::ffi::Error` and `TVMFFIBacktrace` are available via the error.cc include and logging.h includes in tvm_runtime.h
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [REFACTOR][RUNTIME] Macro cleanup — TVM_DLL alignment, [[maybe_unused]], logging.h legacy macros [tvm]
Posted by "tqchen (via GitHub)" <gi...@apache.org>.
tqchen commented on code in PR #19457:
URL: https://github.com/apache/tvm/pull/19457#discussion_r3149571987
##########
include/tvm/runtime/base.h:
##########
@@ -31,52 +31,48 @@
// TVM version
#define TVM_VERSION "0.24.dev0"
-// define extra macros for TVM DLL exprt
-#ifdef __EMSCRIPTEN__
+// TVM ships two shared libraries: libtvm_compiler and libtvm_runtime.
+// Each exposes its own DLL macro pair.
+//
+// TVM_DLL / TVM_DLL_EXPORT: symbols in libtvm_compiler.
+// - TVM_DLL is dllexport when TVM_EXPORTS is defined (compiler build),
+// dllimport otherwise (downstream consumers, runtime TUs).
+// - TVM_DLL_EXPORT is always dllexport.
+//
+// TVM_RUNTIME_DLL / TVM_RUNTIME_DLL_EXPORT: symbols in libtvm_runtime.
+// - TVM_RUNTIME_DLL is dllexport when TVM_RUNTIME_EXPORTS is defined
+// (runtime build), dllimport otherwise.
+// - TVM_RUNTIME_DLL_EXPORT is always dllexport.
+//
+// On non-MSVC platforms the import/export decision is made by the dynamic
+// loader, so all four macros expand to visibility("default"). Under
+// Emscripten they expand to EMSCRIPTEN_KEEPALIVE.
+#if !defined(TVM_DLL) && defined(__EMSCRIPTEN__)
#include <emscripten/emscripten.h>
#define TVM_DLL EMSCRIPTEN_KEEPALIVE
+#define TVM_DLL_EXPORT EMSCRIPTEN_KEEPALIVE
+#define TVM_RUNTIME_DLL EMSCRIPTEN_KEEPALIVE
+#define TVM_RUNTIME_DLL_EXPORT EMSCRIPTEN_KEEPALIVE
#endif
-
-// helper macro to suppress unused warning
-#if defined(__GNUC__)
-#define TVM_ATTRIBUTE_UNUSED __attribute__((unused))
+#if !defined(TVM_DLL) && defined(_MSC_VER)
Review Comment:
Good catch — you were right. The four macros were all gated on `!defined(TVM_DLL)`, so pre-defining only `TVM_DLL` would silently skip `TVM_RUNTIME_DLL` and its export variant. Fixed in commit `5a5f8937e7`: the guards are now two independent blocks (`TVM_DLL` family and `TVM_RUNTIME_DLL` family), each with its own Emscripten/MSVC/visibility-default fallthrough. The single `#include <emscripten/emscripten.h>` is hoisted above both blocks under `#ifdef __EMSCRIPTEN__`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [REFACTOR][RUNTIME] Macro cleanup — TVM_DLL alignment, [[maybe_unused]], logging.h legacy macros [tvm]
Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on code in PR #19457:
URL: https://github.com/apache/tvm/pull/19457#discussion_r3148405529
##########
apps/android_rpc/app/src/main/jni/tvm_runtime.h:
##########
@@ -25,7 +25,6 @@
#include <fstream>
-#define TVM_USE_LIBBACKTRACE 0
Review Comment:

While removing `TVM_USE_LIBBACKTRACE` is consistent with the cleanup, the `LogFatalImpl` function later in this file (around line 104) still uses `InternalError`, which has been removed from `logging.h` in this PR. This will cause a compilation error for the Android RPC component. Please migrate the implementation to use `tvm::ffi::Error` directly, as seen in other migrated sites like `src/runtime/hexagon/hexagon_common.cc`.
##########
include/tvm/runtime/base.h:
##########
@@ -31,52 +31,48 @@
// TVM version
#define TVM_VERSION "0.24.dev0"
-// define extra macros for TVM DLL exprt
-#ifdef __EMSCRIPTEN__
+// TVM ships two shared libraries: libtvm_compiler and libtvm_runtime.
+// Each exposes its own DLL macro pair.
+//
+// TVM_DLL / TVM_DLL_EXPORT: symbols in libtvm_compiler.
+// - TVM_DLL is dllexport when TVM_EXPORTS is defined (compiler build),
+// dllimport otherwise (downstream consumers, runtime TUs).
+// - TVM_DLL_EXPORT is always dllexport.
+//
+// TVM_RUNTIME_DLL / TVM_RUNTIME_DLL_EXPORT: symbols in libtvm_runtime.
+// - TVM_RUNTIME_DLL is dllexport when TVM_RUNTIME_EXPORTS is defined
+// (runtime build), dllimport otherwise.
+// - TVM_RUNTIME_DLL_EXPORT is always dllexport.
+//
+// On non-MSVC platforms the import/export decision is made by the dynamic
+// loader, so all four macros expand to visibility("default"). Under
+// Emscripten they expand to EMSCRIPTEN_KEEPALIVE.
+#if !defined(TVM_DLL) && defined(__EMSCRIPTEN__)
#include <emscripten/emscripten.h>
#define TVM_DLL EMSCRIPTEN_KEEPALIVE
+#define TVM_DLL_EXPORT EMSCRIPTEN_KEEPALIVE
+#define TVM_RUNTIME_DLL EMSCRIPTEN_KEEPALIVE
+#define TVM_RUNTIME_DLL_EXPORT EMSCRIPTEN_KEEPALIVE
#endif
-
-// helper macro to suppress unused warning
-#if defined(__GNUC__)
-#define TVM_ATTRIBUTE_UNUSED __attribute__((unused))
+#if !defined(TVM_DLL) && defined(_MSC_VER)
Review Comment:

The new structure for DLL macros is fragile because it couples the definition of `TVM_RUNTIME_DLL`, `TVM_DLL_EXPORT`, and `TVM_RUNTIME_DLL_EXPORT` to `TVM_DLL` being undefined. If `TVM_DLL` is defined externally (e.g., via compiler flags) or by an included header like `<tvm/ffi/c_api.h>`, the entire block defining `TVM_RUNTIME_DLL` will be skipped. Since `TVM_RUNTIME_DLL` is used independently in the codebase (e.g., in `logging.h`), this will lead to compilation errors. It is recommended to decouple these definitions to ensure all required macros are defined regardless of whether `TVM_DLL` is already present.
##########
include/tvm/runtime/base.h:
##########
@@ -31,52 +31,48 @@
// TVM version
#define TVM_VERSION "0.24.dev0"
-// define extra macros for TVM DLL exprt
-#ifdef __EMSCRIPTEN__
+// TVM ships two shared libraries: libtvm_compiler and libtvm_runtime.
+// Each exposes its own DLL macro pair.
+//
+// TVM_DLL / TVM_DLL_EXPORT: symbols in libtvm_compiler.
+// - TVM_DLL is dllexport when TVM_EXPORTS is defined (compiler build),
+// dllimport otherwise (downstream consumers, runtime TUs).
+// - TVM_DLL_EXPORT is always dllexport.
+//
+// TVM_RUNTIME_DLL / TVM_RUNTIME_DLL_EXPORT: symbols in libtvm_runtime.
+// - TVM_RUNTIME_DLL is dllexport when TVM_RUNTIME_EXPORTS is defined
+// (runtime build), dllimport otherwise.
+// - TVM_RUNTIME_DLL_EXPORT is always dllexport.
+//
+// On non-MSVC platforms the import/export decision is made by the dynamic
+// loader, so all four macros expand to visibility("default"). Under
+// Emscripten they expand to EMSCRIPTEN_KEEPALIVE.
+#if !defined(TVM_DLL) && defined(__EMSCRIPTEN__)
#include <emscripten/emscripten.h>
#define TVM_DLL EMSCRIPTEN_KEEPALIVE
+#define TVM_DLL_EXPORT EMSCRIPTEN_KEEPALIVE
+#define TVM_RUNTIME_DLL EMSCRIPTEN_KEEPALIVE
+#define TVM_RUNTIME_DLL_EXPORT EMSCRIPTEN_KEEPALIVE
#endif
-
-// helper macro to suppress unused warning
-#if defined(__GNUC__)
-#define TVM_ATTRIBUTE_UNUSED __attribute__((unused))
+#if !defined(TVM_DLL) && defined(_MSC_VER)
Review Comment:

Changing the platform check from `_WIN32` to `_MSC_VER` for Windows-specific DLL macros affects MinGW users. While MinGW supports visibility attributes, `__declspec(dllexport/dllimport)` is the standard on Windows and was previously used for all Windows compilers in TVM. This change forces MinGW into the fallback `visibility("default")` path, which may not be the intended behavior for Windows shared libraries. Consider using `_WIN32` to cover all Windows toolchains unless there is a specific reason to restrict this to MSVC.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [REFACTOR][RUNTIME] Macro cleanup — TVM_DLL alignment, [[maybe_unused]], logging.h legacy macros [tvm]
Posted by "tqchen (via GitHub)" <gi...@apache.org>.
tqchen merged PR #19457:
URL: https://github.com/apache/tvm/pull/19457
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org