emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.81k stars 3.31k forks source link

Closure compiler error for glClientWaitSync arguments #20515

Open jspanchu opened 1 year ago

jspanchu commented 1 year ago

When compiling with -Oz -g0 --closure=1, the compiler complains that timeout_high is not defined.

Error:

building:ERROR: /tmp/emscripten_temp_ve4g83xy/GeometryViewer.jso5.js:50:4095: ERROR - [JSC_UNDEFINED_VARIABLE] variable timeout_high is undeclared

For some reason, the generated code for that function glClientWaitSync doesn't have timeout_high in it's arguments. It thinks that parameter is unused?

/** @suppress {duplicate } */ function _glClientWaitSync(sync, flags, timeout_low) {
 sync >>>= 0;
 var timeout = convertI32PairToI53(timeout_low, timeout_high);
 return GLctx.clientWaitSync(GL.syncs[sync], flags, timeout);
}

Version of emscripten/emsdk: Please include the output emcc -v here

|-> $ emcc -v
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.46 (19607820c447a13fd8d0b7680c56148427d6e1b8)
clang version 18.0.0 (https://github.com/llvm/llvm-project 75501f53624de92aafce2f1da698b249a7293dc7)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /home/local/KHQ/jaswant.panchumarti/Documents/code/emsdk/upstream/bin
jspanchu commented 1 year ago

What's interesting is that this error doesn't occur when -g1 or higher is used.

kripken commented 1 year ago

/** @suppress {duplicate } */ function _glClientWaitSync(sync, flags, timeout_low) {

That definitely looks wrong, as there should be timeout_high right after it, like this:

$ ./emcc test/hello_world.c -sINCLUDE_FULL_LIBRARY -sUSE_WEBGL2 -O1  --closure 1 -g0 -s EXPORTED_FUNCTIONS=_main,_glClientWaitSync

$ grep glClientWaitSync a.out.js
f._glClientWaitSync = (a, b, c, d) => hc.clientWaitSync(wc[a], b, (c >>> 0) + 4294967296 * d);

There are 4 parameters there. But note that their names are minified, which yours aren't, which suggests closure wasn't actually run on yours..?

jspanchu commented 1 year ago

Yea, we expected to find a timeout_high right after. Which emcc did you use? The only difference is we used -Oz -g0 --closure 1, whereas in your command, I see -O1 -g0 --closure 1. Do you think the Oz flag caused it?

kripken commented 1 year ago

No, -Oz does not matter here:

$ ./emcc test/hello_world.c -sINCLUDE_FULL_LIBRARY -sUSE_WEBGL2 -Oz  --closure 1 -g0 -s EXPORTED_FUNCTIONS=_main,_glClientWaitSync

$ grep glClientWaitSync a.out.js
b.Wa=b.getExtension("WEBGL_multi_draw_instanced_base_vertex_base_instance");2<=a.version&&(b.ia=b.getExtension("EXT_disjoint_timer_query_webgl2"));if(2>a.version||!b.ia)b.ia=b.getExtension("EXT_disjoint_timer_query");b.Xa=b.getExtension("WEBGL_multi_draw");(b.getSupportedExtensions()||[]).forEach(c=>{c.includes("lose_context")||c.includes("debug")||b.getExtension(c)})}},bc={},fc,xc=[],S={},kc;f._glClientWaitSync=(a,b,c,d)=>ec.clientWaitSync(uc[a],b,(c>>>0)+4294967296*d);

Still 4 minified params, as expected.

The odd thing is they are not minified in yours. Perhaps some other flag you are passing in disables closure? If you can provide a full minimal testcase this should be easy to debug using EMCC_DEBUG=1, or you can do that locally yourself (inspect the temp files and see where things go wrong).

jspanchu commented 1 year ago

Thanks for the hint, the project we see this is pretty big. I'll try in a smaller example locally and get back.

jezell commented 7 months ago

This also happens attempting to compile canvaskit in release mode if you add 4GB memory limit, goes away if closure=1 is removed.

sbc100 commented 7 months ago

@jezell can you share the generated glClientWaitSync function?

When I build with /emcc test/hello_world.c -sINITIAL_MEMORY=4gb -sINCLUDE_FULL_LIBRARY -sUSE_WEBGL2 -Oz --minify 0 --closure 1 -g0 -s EXPORTED_FUNCTIONS=_main,_glClientWaitSync I get the following:

f._glClientWaitSync = function(a, b, c, d) {                                     
  return uc.clientWaitSync(nc[a >>> 0], b, (c >>> 0) + 4294967296 * d);          
};   

Do you not also get a 4 argument function?