emscripten
emscripten copied to clipboard
cwrap() does not work correctly for -O2
main.cpp:
#include <iostream>
#include <emscripten.h>
int _Test(const char *argument)
{
std::cout << "_Test: " << argument << std::endl;
return 0;
}
int main(int argc, char *argv[])
{
// Make a dummy call to make sure the function does not
// get removed by emcc.
_Test("");
emscripten_run_script(
"var func = cwrap('_Z5_TestPKc', 'number', ['string']);"
"func('Hello!');");
return 0;
}
First do a non-optimized build and run:
[Work] [none] 03:22 21560M y:\emscripten>python emcc main.cpp
clang++: warning: argument unused during compilation: '-nostdinc++'
emcc: warning: using libcxx turns on CORRECT_* options
emcc: warning: using libcxxabi, this may need CORRECT_* options
Warning: .ll contains floating-point values with more than 64 bits. Faking values for them. If they are used, this will almost certainly break horribly!
[Work] [none] 03:22 21556M y:\emscripten>node a.out.js
_Test:
_Test: Hello!
Then try with -O2 (with closure disabled):
[Work] [none] 03:25 21561M y:\emscripten>python emcc -O2 --closure 0 main.cpp
clang++: warning: argument unused during compilation: '-nostdinc++'
emcc: warning: using libcxx turns on CORRECT_* options
emcc: warning: using libcxxabi, this may need CORRECT_* options
Warning: .ll contains floating-point values with more than 64 bits. Faking values for them. If they are used, this will almost certainly break horribly!
[Work] [none] 03:26 21560M y:\emscripten>node a.out.js
_Test:
_Test:
It also seems to be a problem that cwrap gets removed by closure:
[Work] [none] 03:23 21556M y:\emscripten>python emcc -O2 -s "EXPORTED_FUNCTIONS=['_main', '__Z5_TestPKc']" main.cpp
clang++: warning: argument unused during compilation: '-nostdinc++'
emcc: warning: using libcxx turns on CORRECT_* options
emcc: warning: using libcxxabi, this may need CORRECT_* options
Warning: .ll contains floating-point values with more than 64 bits. Faking values for them. If they are used, this will almost certainly break horribly!
[Work] [none] 03:24 21561M y:\emscripten>node a.out.js
_Test:
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
ReferenceError: cwrap is not defined
at eval at rd (Y:\emscripten\a.out.js:1:161895)
at rd (Y:\emscripten\a.out.js:1:161890)
at qd (Y:\emscripten\a.out.js:1:6912)
at Object.pd (Y:\emscripten\a.out.js:1:177439)
at $p (Y:\emscripten\a.out.js:1:261169)
at Object.<anonymous> (Y:\emscripten\a.out.js:1:261368)
at Module._compile (module.js:441:26)
at Object..js (module.js:459:10)
at Module.load (module.js:348:31)
at Function._load (module.js:308:12)
I guess that calling cwrap() from inline JS could avoid it being removed by closure, and then add cwrap to EXPORTED_FUNCTIONS so that its name remains unchanged.
- The incorrect output happens in -O1 too. What seems to happen is that LLVM optimizes that function for the single use it has here. Mark the function with
__attribute__((used))prevents such optimizations and it will work ok. - cwrap is minified by closure, but we export it on Module.
Module.cwrapwill work ok.
Sweet! I'll give this a try when I'm back in the office.
1 and 2 both worked great, thank you!