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 "gnguralnick (via GitHub)" <gi...@apache.org> on 2026/04/02 22:10:47 UTC
[PR] [Web] Fix static init order in WASM runtime to prevent GetKwargsObject crash [tvm]
gnguralnick opened a new pull request, #18968:
URL: https://github.com/apache/tvm/pull/18968
## Summary
After tvm-ffi v0.1.9 (`6b39efb`), `ObjectDef::~ObjectDef()` calls `AutoRegisterInit()` which eagerly resolves `ffi.GetKwargsObject` via `GetGlobalRequired()`. In the WASM build, all `.cc` files are `#include`'d into a single translation unit (`wasm_runtime.cc`), so `__attribute__((constructor))` functions run in deterministic source order.
Runtime files like `file_utils.cc`, `profiling.cc`, and `rpc_session.cc` (which use `ObjectDef` in their static init blocks) were included **before** `object.cc` (which registers `ffi.GetKwargsObject`). This caused a fatal `ValueError: Function ffi.GetKwargsObject not found` during WASM `_initialize`, crashing before the module could load.
**Fix:** Move all tvm-ffi core includes before the runtime files so that `ffi.GetKwargsObject` is registered before any `ObjectDef` destructor needs it.
## Error reproduced
```
ValueError: Function ffi.GetKwargsObject not found
at $_initialize
at callMain
at doRun
```
This affects all WebGPU model compilation since the tvm-ffi bump in #18938.
## Test plan
- [x] Compiled Qwen3.5-4B model to WASM with the reordered includes
- [x] Verified WASM loads and runs correctly in browser via web-llm
--
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] [Web] Fix static init order in WASM runtime to prevent GetKwargsObject crash [tvm]
Posted by "gnguralnick (via GitHub)" <gi...@apache.org>.
gnguralnick closed pull request #18968: [Web] Fix static init order in WASM runtime to prevent GetKwargsObject crash
URL: https://github.com/apache/tvm/pull/18968
--
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] [Web] Fix static init order in WASM runtime to prevent GetKwargsObject crash [tvm]
Posted by "akaashrp (via GitHub)" <gi...@apache.org>.
akaashrp commented on PR #18968:
URL: https://github.com/apache/tvm/pull/18968#issuecomment-4186466132
@gnguralnick I updated the behavior here: https://github.com/apache/tvm-ffi/pull/519. Is this still an issue?
--
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] [Web] Fix static init order in WASM runtime to prevent GetKwargsObject crash [tvm]
Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on code in PR #18968:
URL: https://github.com/apache/tvm/pull/18968#discussion_r3030615229
##########
web/emcc/wasm_runtime.cc:
##########
@@ -33,6 +33,26 @@
#include <tvm/ffi/reflection/registry.h>
#include <tvm/runtime/logging.h>
+// FFI core must come first: object.cc registers ffi.GetKwargsObject which is
+// needed by ObjectDef destructors (AutoRegisterInit) in later translation units.
+// Without this ordering, WASM static initialization fails because constructor
+// functions run in source order within a single compilation unit.
+#include "3rdparty/tvm-ffi/src/ffi/backtrace.cc"
+#include "3rdparty/tvm-ffi/src/ffi/container.cc"
+#include "3rdparty/tvm-ffi/src/ffi/dtype.cc"
+#include "3rdparty/tvm-ffi/src/ffi/error.cc"
+#include "3rdparty/tvm-ffi/src/ffi/function.cc"
+#include "3rdparty/tvm-ffi/src/ffi/object.cc"
+#include "3rdparty/tvm-ffi/src/ffi/tensor.cc"
Review Comment:

To ensure robustness against static initialization order issues within the FFI core itself, `object.cc` should be included before other FFI components that might use the object system (such as `container.cc`, `function.cc`, or `tensor.cc`). Since `object.cc` registers `ffi.GetKwargsObject`, any static initialization in the subsequent files that triggers `ObjectDef` logic will be safe if `object.cc` is included first. This aligns with the logic described in the PR summary regarding the dependency on `ffi.GetKwargsObject` during static initialization.
```suggestion
#include "3rdparty/tvm-ffi/src/ffi/backtrace.cc"
#include "3rdparty/tvm-ffi/src/ffi/error.cc"
#include "3rdparty/tvm-ffi/src/ffi/object.cc"
#include "3rdparty/tvm-ffi/src/ffi/container.cc"
#include "3rdparty/tvm-ffi/src/ffi/dtype.cc"
#include "3rdparty/tvm-ffi/src/ffi/function.cc"
#include "3rdparty/tvm-ffi/src/ffi/tensor.cc"
```
--
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] [Web] Fix static init order in WASM runtime to prevent GetKwargsObject crash [tvm]
Posted by "gnguralnick (via GitHub)" <gi...@apache.org>.
gnguralnick commented on PR #18968:
URL: https://github.com/apache/tvm/pull/18968#issuecomment-4195671264
those changes seem like they'd fix this issue, probably in a more robust manner than this reorder. thanks!
--
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] [Web] Fix static init order in WASM runtime to prevent GetKwargsObject crash [tvm]
Posted by "gnguralnick (via GitHub)" <gi...@apache.org>.
gnguralnick commented on code in PR #18968:
URL: https://github.com/apache/tvm/pull/18968#discussion_r3033705048
##########
web/emcc/wasm_runtime.cc:
##########
@@ -33,6 +33,26 @@
#include <tvm/ffi/reflection/registry.h>
#include <tvm/runtime/logging.h>
+// FFI core must come first: object.cc registers ffi.GetKwargsObject which is
+// needed by ObjectDef destructors (AutoRegisterInit) in later translation units.
+// Without this ordering, WASM static initialization fails because constructor
+// functions run in source order within a single compilation unit.
+#include "3rdparty/tvm-ffi/src/ffi/backtrace.cc"
+#include "3rdparty/tvm-ffi/src/ffi/container.cc"
+#include "3rdparty/tvm-ffi/src/ffi/dtype.cc"
+#include "3rdparty/tvm-ffi/src/ffi/error.cc"
+#include "3rdparty/tvm-ffi/src/ffi/function.cc"
+#include "3rdparty/tvm-ffi/src/ffi/object.cc"
+#include "3rdparty/tvm-ffi/src/ffi/tensor.cc"
Review Comment:
I checked the actual static init blocks in the FFI core files: container.cc, function.cc, and tensor.cc all use GlobalDef() (not ObjectDef), and dtype.cc/error.cc have no static init blocks at all. So none of them depend on ffi.GetKwargsObject during initialization, and the ordering within the FFI core group doesn't matter for correctness. The critical ordering is FFI core before runtime files (which do use ObjectDef), which is already handled.
--
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