From ac374852ace635a5fd116462419866730c85492b Mon Sep 17 00:00:00 2001 From: Tianqi Chen Date: Sun, 25 Oct 2020 08:44:44 -0400 Subject: [PATCH] [WASM] Update support for latest emcc, add ffi test. (#6751) --- python/tvm/contrib/emcc.py | 1 + web/Makefile | 4 ++-- web/emcc/wasm_runtime.cc | 8 ++++++++ web/tests/node/test_packed_func.js | 13 +++++++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/python/tvm/contrib/emcc.py b/python/tvm/contrib/emcc.py index 0cecc66bd1a2d..89431dc2a4f6e 100644 --- a/python/tvm/contrib/emcc.py +++ b/python/tvm/contrib/emcc.py @@ -42,6 +42,7 @@ def create_tvmjs_wasm(output, objects, options=None, cc="emcc"): cmd += ["-O3"] cmd += ["-std=c++14"] + cmd += ["--no-entry"] cmd += ["-s", "ERROR_ON_UNDEFINED_SYMBOLS=0"] cmd += ["-s", "STANDALONE_WASM=1"] cmd += ["-s", "ALLOW_MEMORY_GROWTH=1"] diff --git a/web/Makefile b/web/Makefile index eaf5a954accbf..8c4dbc20dadc6 100644 --- a/web/Makefile +++ b/web/Makefile @@ -26,8 +26,8 @@ all: dist/wasm/tvmjs_runtime.wasm dist/wasm/tvmjs_runtime.wasi.js EMCC = emcc -EMCC_CFLAGS = $(INCLUDE_FLAGS) -O3 -std=c++14 -Wno-ignored-attributes \ - -s ALLOW_MEMORY_GROWTH=1 -s STANDALONE_WASM=1 -s ERROR_ON_UNDEFINED_SYMBOLS=0 +EMCC_CFLAGS = $(INCLUDE_FLAGS) -O3 -std=c++14 -Wno-ignored-attributes --no-entry \ + -s ALLOW_MEMORY_GROWTH=1 -s STANDALONE_WASM=1 -s ERROR_ON_UNDEFINED_SYMBOLS=0 EMCC_LDFLAGS = --pre-js emcc/preload.js diff --git a/web/emcc/wasm_runtime.cc b/web/emcc/wasm_runtime.cc index a5f8c8252571c..214c1883f8742 100644 --- a/web/emcc/wasm_runtime.cc +++ b/web/emcc/wasm_runtime.cc @@ -75,5 +75,13 @@ TVM_REGISTER_GLOBAL("testing.wrap_callback").set_body([](TVMArgs args, TVMRetVal PackedFunc pf = args[0]; *ret = runtime::TypedPackedFunc([pf]() { pf(); }); }); + +// internal function used for debug and testing purposes +TVM_REGISTER_GLOBAL("testing.object_use_count").set_body([](TVMArgs args, TVMRetValue* ret) { + runtime::ObjectRef obj = args[0]; + // substract the current one because we always copy + // and get another value. + *ret = (obj.use_count() - 1); +}); } // namespace runtime } // namespace tvm diff --git a/web/tests/node/test_packed_func.js b/web/tests/node/test_packed_func.js index e18c0aecfdc0e..87b48df3d67a4 100644 --- a/web/tests/node/test_packed_func.js +++ b/web/tests/node/test_packed_func.js @@ -109,3 +109,16 @@ test("RegisterGlobal", () => { let syslib = tvm.systemLib(); syslib.dispose(); }); + +test("NDArrayCbArg", () => { + let use_count = tvm.getGlobalFunc("testing.object_use_count"); + + let fcheck = tvm.toPackedFunc(function (x) { + assert(use_count(x) == 2); + x.dispose(); + }); + let x = tvm.empty([2], "float32").copyFrom([1, 2]); + assert(use_count(x) == 1); + fcheck(x); + assert(use_count(x) == 1); +});