Why NaN and Infinity imports?

17 views
Skip to first unread message

stephe...@shopify.com

unread,
Apr 26, 2019, 3:29:55 PM4/26/19
to emscripten-discuss
When compiling some code with emcc I get these imports in the result:

 (import "global" "NaN" (global $gimport$16 f64))
 (import "global" "Infinity" (global $gimport$17 f64))

Why are these needed?  Could the values for these constants not exist internal to the WASM?

Also, I searched the disassembly for NaN and it is only here in the import -- even when I use -O3 -- so maybe unused import removal is missing it for some reason?

Thanks.

Alon Zakai

unread,
Apr 26, 2019, 6:07:23 PM4/26/19
to emscripte...@googlegroups.com
This is a side effect caused by asm2wasm - in asm.js we do need to import NaN. But you're right it's unnecessary in wasm.

Once we switch away from asm2wasm to the LLVM wasm backend by default, this problem will go away.

It's odd btw that you see an import that is never used not being optimized out - I only see NaN remain when it is actually used. So you might be seeing a bug in the binaryen optimizer - if you can share a testcase I can take a look at that.


--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stephen Weber

unread,
Apr 26, 2019, 9:49:13 PM4/26/19
to emscripte...@googlegroups.com
Compiling the attached with

emcc -std=c99 -Wall --memory-init-file 0 -DEMSCRIPTEN -s
DISABLE_EXCEPTION_CATCHING=1 -s MALLOC=emmalloc -s FILESYSTEM=0 -s
EXPORTED_FUNCTIONS='["_duk_eval_raw"]' -O3 -g duktape.c

then disassembling with binaryen wasm-dis I see NaN and Infinity
imports but grepping the disassembly doesn't show any uses I can see.
> You received this message because you are subscribed to a topic in the Google Groups "emscripten-discuss" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/emscripten-discuss/0y1Ve9gz5f0/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to emscripten-disc...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
singpolyma.net
duktape.tar

Alon Zakai

unread,
Apr 27, 2019, 7:23:19 PM4/27/19
to emscripte...@googlegroups.com
Thanks for the testcase! I do see uses, though:

 (import "global" "NaN" (global $gimport$12 f64))
[..]
 (global $global$1 (mut f64) (global.get $gimport$12))
[..]
      (global.get $global$1)
etc.

So it's imported, assigned to a mutable global, and that mutable global is used.

Stephen Weber

unread,
Apr 27, 2019, 10:16:14 PM4/27/19
to emscripte...@googlegroups.com
Oh, that's my bad. I don't know what I was thinking :P Obviously
that's what is happening.

So now I just impatiently await the new backend ;)
Reply all
Reply to author
Forward
0 new messages