Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined scalbnf after switching to new emsdk 3.1.42 #19781

Closed
Honya2000 opened this issue Jul 5, 2023 · 37 comments · Fixed by #20215
Closed

Undefined scalbnf after switching to new emsdk 3.1.42 #19781

Honya2000 opened this issue Jul 5, 2023 · 37 comments · Fixed by #20215

Comments

@Honya2000
Copy link

Honya2000 commented Jul 5, 2023

Hello,
Today I switched to new emsdk 3.1.42 and unfortunatelly my project failed to compile now.
It throws error during linking:
wasm-ld: error: C:\Projects\emsdk\upstream\emscripten\cache\sysroot\lib\wasm32-emscripten\lto\libc.a(scalbnf.o): undefined symbol: scalbnf

The problem is i don't use any scalbnf in my project explicitly. But I'm using SDL2. And seems like SDL2 is using this function:

float
SDL_scalbnf(float x, int n)
{
#if defined(HAVE_SCALBNF)
return scalbnf(x, n);
#else
return (float)SDL_scalbn((double)x, n);
#endif
}

I cannot fix it in SDL2, because it is part of emsdk.

So what should i do now?

P.S. Previously i used emsdk 3.1.40 and everything was fine.

P.P.S in 3.1.41 and 3.1.42 release notes - no information about those changes. It just mentiones new LLVM version.

@Honya2000 Honya2000 changed the title Undefined sclbnf after switching to new emsdk 3.1.42 Undefined scalbnf after switching to new emsdk 3.1.42 Jul 5, 2023
@sbc100
Copy link
Collaborator

sbc100 commented Jul 5, 2023

My guess is that this actually broke in 3.1.41, and that the change that did it was the update of libc++/lib++abi in #19515.

Can you confirm that 3.1.41 is also broken for you?

@sbc100
Copy link
Collaborator

sbc100 commented Jul 5, 2023

Also, can you confirm if this is an LTO-specific issue? i.e. does it also happen when you build without -flto?

@Honya2000
Copy link
Author

Actually checked out to 3.1.41 and this fixed the issue.
Will try without lto…

@sbc100
Copy link
Collaborator

sbc100 commented Jul 5, 2023

I'm trying to repro this locally and not having much luck. I've added some testing here: #19782

@sbc100
Copy link
Collaborator

sbc100 commented Jul 5, 2023

Seems likely to be related to #16836 (comment).. although the error message you are seeing here is very confusing since the scalbnf.o object file contains just one symbol definition which is scalbnf.. so I can't see how this error would ever be possible.

@sbc100
Copy link
Collaborator

sbc100 commented Jul 5, 2023

See also #19774

@Honya2000
Copy link
Author

Without lto the project compiled fine with emsdk 3.1.42
And though non-lto wasm file is less in size that the lto version - i still need lto to optimize the code beweeen obj files / libs.

With -flto i have this observations:

emsdk 3.1.41 generates libc.a of size: 6050348 bytes.
emsdk 3.1.42 compiles libc.a of size 6042782 bytes.

As i mentioned earlier 3.1.41 links my project fine.
3.1.42 - fails.

I'm using this options to compile:
set(CMAKE_CXX_FLAGS "-flto -frtti -DEMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES=1 -DNDEBUG -s ALLOW_MEMORY_GROWTH=1 -s USE_SDL=2 --bind")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --bind -flto -s WASM=1 -O3 -s EXPORT_NAME=Module -s ALLOW_MEMORY_GROWTH=1 -fexceptions -frtti -s USE_SDL=2 -s MIN_WEBGL_VERSION=2 -s MAX_WEBGL_VERSION=2 -s USE_WEBGL2=1 -s AUTO_NATIVE_LIBRARIES=1 -s AUTO_JS_LIBRARIES=1 -s REVERSE_DEPS=all -s ENVIRONMENT=web")

My project is quite huge, so i cannot share it. Also i don't have much time for more deep investigations and creating small test cases.
Probably later, if the issue will remain in future emsdk versions...

For now I have to stick to 3.1.41.

@sbc100
Copy link
Collaborator

sbc100 commented Jul 6, 2023

Since you have a repro case, if you get time to look at this again, a useful task would be to try to try down which change in the 3.1.42 -> 3.1.42 range caused this issue. There are instructions for how to bisect on emsdk versions here: https://emscripten.org/docs/contributing/developers_guide.html#bisecting

mgerhardy added a commit to vengi-voxel/vengi that referenced this issue Jul 13, 2023
@Honya2000
Copy link
Author

Honya2000 commented Aug 17, 2023

Tried to use sdk 3.1.44, the same error:
undefined symbol: scalbnf.

I even tried workaround in my code:

// Workaround for emsdk: 3.1.44
float __cdecl scalbnf(float x, int n)
{
    union { float f; uint32_t i; } u;
    float_t y = x;

    if (n > 127)
    {
        y *= 0x1p127f;
        n -= 127;
        if (n > 127)
        {
            y *= 0x1p127f;
            n -= 127;
            if (n > 127)
                n = 127;
        }
    }
    else
    if (n < -126)
    {
        y *= 0x1p-126f * 0x1p24f;
        n += 126 - 24;
        if (n < -126)
        {
            y *= 0x1p-126f * 0x1p24f;
            n += 126 - 24;
            if (n < -126)
                n = -126;
        }
    }
    u.i = (uint32_t)(0x7f + n) << 23;
    x = y * u.f;
    return x;
}

And now it reports the same error in my code.
Looks like it treats scalbnf symbol as reserved and doesn't allow to declare the function with this name.

Probably there is some define with this name exists. But i didn't find it.

@Honya2000
Copy link
Author

tried to replace all scalbnf symbols to _scalbnf inside headers and sources of emsdk/upstream.
This didn't fix the issue:

wasm-ld: error: C:\Projects\emsdk\upstream\emscripten\cache\sysroot\lib\wasm32-emscripten\lto\libc.a(scalbnf.o): undefined symbol: _scalbnf

@Honya2000
Copy link
Author

i give up.
Will have to rollback to 3.1.41 again.

@sbc100
Copy link
Collaborator

sbc100 commented Aug 17, 2023

BTW you can get a lot of the benefits of LTO by building all your code with -flto but not linking with -flto. Passing -flto at link time causes LTO versions of the standard libraries to be included, but its likely that is not essential. Can you try removing -flto just from your link flags?

@Honya2000
Copy link
Author

I'm using this cmake setup for the project:
set(CMAKE_CXX_FLAGS "-flto -frtti -ffast-math -fexceptions -DNDEBUG -O3 -s USE_SDL=2")
set(CMAKE_EXE_LINKER_FLAGS "--bind -s WASM=1 -O3 -s EXPORT_NAME=Module -s ALLOW_MEMORY_GROWTH=1 -fexceptions -frtti -s USE_SDL=2 -s MIN_WEBGL_VERSION=2 -s MAX_WEBGL_VERSION=2 -s USE_WEBGL2=1 -s ENVIRONMENT=web")

And during linking phase get this error:

wasm-ld: error: C:\Projects\emsdk\upstream\emscripten\cache\sysroot\lib\wasm32-emscripten\lto\libc.a(scalbnf.o): undefined symbol: scalbnf
em++: error: 'C:/Projects/emsdk/upstream/bin\wasm-ld.exe -o Release\bin\tmrwRooms-nt.wasm TmrwApplications/src/roomsExplorer/CMakeFiles/tmrwRooms.dir/Platform/Wasm/wasmApp.cpp.o TmrwApplications/src/roomsExplorer/CMakeFiles/tmrwRooms.dir/Platform/Wasm/wasmBindings.cpp.o TmrwApplications/src/roomsExplorer/CMakeFiles/tmrwRooms.dir/Platform/Wasm/wasmBindingsEnums.cpp.o TmrwApplications/src/roomsExplorer/CMakeFiles/tmrwRooms.dir/Platform/Wasm/wasmBindingsOptions.cpp.o TmrwApplications/src/roomsExplorer/CMakeFiles/tmrwRooms.dir/engineApi.cpp.o TmrwApplications/src/roomsExplorer/CMakeFiles/tmrwRooms.dir/Common/Basic/ThreadWindows.cpp.o TmrwApplications/src/roomsExplorer/CMakeFiles/tmrwRooms.dir/Common/Basic/File.cpp.o TmrwApplications/src/roomsExplorer/CMakeFiles/tmrwRooms.dir/Common/Room/RoomFramework.cpp.o TmrwApplications/src/roomsExplorer/CMakeFiles/tmrwRooms.dir////Tmrw/src/TmrwPlugins/SceneImporter/importStaticPlugin.cpp.o TmrwApplications/src/roomsExplorer/CMakeFiles/tmrwRooms.dir////Tmrw/src/TmrwPlugins/ImageImporter/importStaticPlugin.cpp.o TmrwApplications/src/roomsExplorer/CMakeFiles/tmrwRooms.dir////TmrwPlugins/src/TmrwPlugins/ObjImporter/importStaticPlugin.cpp.o TmrwApplications/src/roomsExplorer/CMakeFiles/tmrwRooms.dir////TmrwPlugins/src/TmrwPlugins/GltfImporter/importStaticPlugin.cpp.o TmrwApplications/src/roomsExplorer/CMakeFiles/tmrwRooms.dir////TmrwPlugins/src/TmrwPlugins/HdreImporter/importStaticPlugin.cpp.o TmrwApplications/src/roomsExplorer/CMakeFiles/tmrwRooms.dir////TmrwPlugins/src/TmrwPlugins/DdsImporter/importStaticPlugin.cpp.o Release/lib/libTmrwSdl2Application.a Release/lib/libTmrwGL.a Release/lib/libTmrw.a Release/lib/libTmrwMeshTools.a Release/lib/libTmrwPrimitives.a Release/lib/libTmrwRenderPipeline.a Release/lib/libTmrwEngine.a Release/lib/libTmrwShaders.a Release/lib/libTmrwData.a Release/lib/plugins/importers/libAnySceneImporter.a Release/lib/plugins/importers/libImageImporter.a Release/lib/plugins/importers/libObjImporter.a Release/lib/plugins/importers/libGltfImporter.a Release/lib/plugins/importers/libHdreImporter.a Release/lib/plugins/importers/libDdsImporter.a Release/lib/libTmrwPrimitives.a Release/lib/libTmrwRenderPipeline.a third_party/smol-v/tmrw/libsmol-v.a third_party/basisu/tmrw/libbasis_transcoder.a third_party/basisu/tmrw/libzstd.a third_party/kNet/libkNet.a third_party/Bullet3/src/BulletCollision/libBulletCollision.a third_party/Bullet3/src/BulletDynamics/libBulletDynamics.a third_party/Bullet3/src/LinearMath/libLinearMath.a third_party/miniz/libminiz.a Release/lib/libTmrwMeshTools.a Release/lib/libTmrwGL.a Release/lib/plugins/importers/libAnyImageImporter.a Release/lib/libTmrwData.a Release/lib/libTmrw.a Release/lib/libTmuPluginManager.a Release/lib/libTmuUtility.a -LC:\Projects\emsdk\upstream\emscripten\cache\sysroot\lib\wasm32-emscripten\lto C:\Projects\emsdk\upstream\emscripten\cache\sysroot\lib\wasm32-emscripten\lto\libSDL2.a --whole-archive -lembind-rtti -lGL-webgl2 --no-whole-archive -lal -lhtml5 -lstubs -lnoexit -lc -ldlmalloc -lcompiler_rt -lc++ -lc++abi -lsockets -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-cxx-exceptions -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr C:\Users\Andrey\AppData\Local\Temp\tmpkymoq05qlibemscripten_js_symbols.so --strip-debug --export-if-defined=main --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__start_em_lib_deps --export-if-defined=__stop_em_lib_deps --export-if-defined=__start_em_js --export-if-defined=__stop_em_js --export-if-defined=__main_argc_argv --export=stackSave --export=stackRestore --export=stackAlloc --export=__errno_location --export=__get_temp_ret --export=__set_temp_ret --export=malloc --export=free --export=__cxa_is_pointer_type --export=__cxa_can_catch --export=__cxa_increment_exception_refcount --export=__cxa_decrement_exception_refcount --export=setThrew --export=__cxa_free_exception --export=__wasm_call_ctors --export-table -z stack-size=65536 --initial-memory=16777216 --no-entry --max-memory=2147483648 --global-base=1024' failed (returned 1)
ninja: build stopped: subcommand failed.

Not sure why it still uses lto libc.a. But sometimes ago i tried to compile my project without LTO at all. And it still reported the same error.

Weird thing is that linker seems like expect referencing of scalbnf in the place where it is actually implemented.
As i mentioned earlier - i tried to implement this function in my user code. And in this case linker failed in my code with the same missing symbol scalbnf. How it can be missed if it's implemented there?

@sbc100
Copy link
Collaborator

sbc100 commented Aug 17, 2023

If the LTO version of libc is being used then -flto must be being passed to the link command.

ninja should be showing you the failing em++ command.. can you include that.. or check that it has -flto (which I assume it must).

@Honya2000
Copy link
Author

Honya2000 commented Aug 17, 2023

this is issued em++ command:

FAILED: Release/bin/tmrwRooms-nt.js
cmd.exe /C "cd . && C:\Projects\emsdk\upstream\emscripten\em++.bat -flto -frtti -ffast-math -fexceptions -DNDEBUG -O3 -s USE_SDL=2 -Wno-unknown-pragmas -Wno-unused-function -Wno-deprecated-declarations -fvisibility=hidden -fcolor-diagnostics -O3 -DNDEBUG -fomit-frame-pointer -ffunction-sections -fdata-sections --bind -s WASM=1 -O3 -s EXPORT_NAME=Module -s ALLOW_MEMORY_GROWTH=1 -fexceptions -frtti -s USE_SDL=2 -s MIN_WEBGL_VERSION=2 -s MAX_WEBGL_VERSION=2 -s USE_WEBGL2=1 -s ENVIRONMENT=web -s EXPORTED_RUNTIME_METHODS=['ccall','cwrap','onLoadURL','AsciiToString'] --preload-file C:/Projects/TMRW/RoomGit/tmrwNative/assets/wasm/assets@assets --js-library C:/Projects/TMRW/RoomGit/tmrwNative/assets/wasm/nativeBridge.js -s USE_WEBGL2=1 TmrwApplications/src/roomsExplorer/CMakeFiles/tmrwRooms.dir/Platform/Wasm/wasmApp.cpp.o TmrwApplications/src/roomsExplorer/CMakeFiles/tmrwRooms.dir/Platform/Wasm/wasmBindings.cpp.o TmrwApplications/src/roomsExplorer/CMakeFiles/tmrwRooms.dir/Platform/Wasm/wasmBindingsEnums.cpp.o TmrwApplications/src/roomsExplorer/CMakeFiles/tmrwRooms.dir/Platform/Wasm/wasmBindingsOptions.cpp.o TmrwApplications/src/roomsExplorer/CMakeFiles/tmrwRooms.dir/engineApi.cpp.o TmrwApplications/src/roomsExplorer/CMakeFiles/tmrwRooms.dir/Common/Basic/ThreadWindows.cpp.o TmrwApplications/src/roomsExplorer/CMakeFiles/tmrwRooms.dir/Common/Basic/File.cpp.o TmrwApplications/src/roomsExplorer/CMakeFiles/tmrwRooms.dir/Common/Room/RoomFramework.cpp.o TmrwApplications/src/roomsExplorer/CMakeFiles/tmrwRooms.dir////Tmrw/src/TmrwPlugins/SceneImporter/importStaticPlugin.cpp.o TmrwApplications/src/roomsExplorer/CMakeFiles/tmrwRooms.dir////Tmrw/src/TmrwPlugins/ImageImporter/importStaticPlugin.cpp.o TmrwApplications/src/roomsExplorer/CMakeFiles/tmrwRooms.dir////TmrwPlugins/src/TmrwPlugins/ObjImporter/importStaticPlugin.cpp.o TmrwApplications/src/roomsExplorer/CMakeFiles/tmrwRooms.dir////TmrwPlugins/src/TmrwPlugins/GltfImporter/importStaticPlugin.cpp.o TmrwApplications/src/roomsExplorer/CMakeFiles/tmrwRooms.dir////TmrwPlugins/src/TmrwPlugins/HdreImporter/importStaticPlugin.cpp.o TmrwApplications/src/roomsExplorer/CMakeFiles/tmrwRooms.dir////TmrwPlugins/src/TmrwPlugins/DdsImporter/importStaticPlugin.cpp.o -o Release\bin\tmrwRooms-nt.js Release/lib/libTmrwSdl2Application.a Release/lib/libTmrwGL.a Release/lib/libTmrw.a Release/lib/libTmrwMeshTools.a Release/lib/libTmrwPrimitives.a Release/lib/libTmrwRenderPipeline.a Release/lib/libTmrwEngine.a Release/lib/libTmrwShaders.a Release/lib/libTmrwData.a Release/lib/plugins/importers/libSceneImporter.a Release/lib/plugins/importers/libImageImporter.a Release/lib/plugins/importers/libObjImporter.a Release/lib/plugins/importers/libGltfImporter.a Release/lib/plugins/importers/libHdreImporter.a Release/lib/plugins/importers/libDdsImporter.a Release/lib/libTmrwPrimitives.a Release/lib/libTmrwRenderPipeline.a third_party/smol-v/tmrw/libsmol-v.a third_party/basisu/tmrw/libbasis_transcoder.a third_party/basisu/tmrw/libzstd.a third_party/kNet/libkNet.a -lpthread third_party/Bullet3/src/BulletCollision/libBulletCollision.a third_party/Bullet3/src/BulletDynamics/libBulletDynamics.a third_party/Bullet3/src/LinearMath/libLinearMath.a third_party/miniz/libminiz.a Release/lib/libTmrwMeshTools.a Release/lib/libTmrwGL.a -lGL Release/lib/plugins/importers/libImageImporter.a Release/lib/libTmrwData.a Release/lib/libTmrw.a Release/lib/libTmuPluginManager.a Release/lib/libTmuUtility.a && cmd.exe /C "cd /D C:\Projects\TMRW\RoomGit\tmrwNative\out\cmake-wasm-release-nt\TmrwApplications\src\roomsExplorer && C:\Projects\TMRW\RoomGit\tmrwNative\Tools\bin\repl -V -w C:/Projects/TMRW/RoomGit/tmrwNative/out/cmake-wasm-release-nt/Release/bin/tmrwRooms-nt.js GL GLEM && cd /D C:\Projects\TMRW\RoomGit\tmrwNative\out\cmake-wasm-release-nt\TmrwApplications\src\roomsExplorer && C:\Projects\TMRW\RoomGit\tmrwNative\Tools\bin\repl -V -w C:/Projects/TMRW/RoomGit/tmrwNative/out/cmake-wasm-release-nt/Release/bin/tmrwRooms-nt.js tmrwRooms-nt.data tmrwRooms.data""
cache:INFO: generating system asset: symbol_lists/fef6336472d1860a96d78b695c4171d4dbcd242b.json... (this will be cached in "C:\Projects\emsdk\upstream\emscripten\cache\symbol_lists\fef6336472d1860a96d78b695c4171d4dbcd242b.json" for subsequent builds)
cache:INFO: - ok

@Honya2000
Copy link
Author

Will check later why lto still used during linking, but 100% this will not change anything. Because removing LTO in cmake didn't fix the issue one month ago.

@sbc100
Copy link
Collaborator

sbc100 commented Aug 17, 2023

Yup there is the -flto setting right at the start. I guess cmake is adding CMAKE_CXX_FLAGS to the link command in addition to CMAKE_EXE_LINKER_FLAGS.

@sbc100
Copy link
Collaborator

sbc100 commented Aug 17, 2023

Back in #19781 (comment) you said removing LTO did fix the issue? Are you sure it doesn't fix it now?

@Honya2000
Copy link
Author

I said LTO didn't fix the issue one month ago.

@Honya2000
Copy link
Author

Will try to replace llvm binaries from 3.1.41 (but tomorrow). Because really looks like linker bug...

@Honya2000
Copy link
Author

Sorry, you right! disabling LTO for entire project in cmake - fixed the issue one month ago.
I completely forgot this. Will check again,

@Honya2000
Copy link
Author

Yes, i confirm, without lto - the project is compilable!
Now need to find the way how to pass lto to compiler but not to linker via cmake somehow...

@Honya2000
Copy link
Author

Regarding my experiments with the binaries.

In emsdk 3.1.44 today i replaced upstream/bin folder from emsdk 3.1.41.
And now my project is compilable with LTO.
So as I suspected there is bug in LLVM / Clang tools.
But not sure compiler or linker.

Anyway will have to rollback to 3.1.41...

@unix-based-system
Copy link

I can confirm that my project also breaks after switching to version > 3.1.41.

Hope there will be a fix soon.

@mekhontsev
Copy link

My project is also breaks with Emscripten > 3.1.41.

mosra added a commit to mosra/magnum-plugins that referenced this issue Sep 3, 2023
These plugins both use ldexp(), which delegates to scalbnf(). See
emscripten-core/emscripten#19781 for the
upstream bugreport.
mosra added a commit to mosra/magnum-plugins that referenced this issue Sep 3, 2023
These plugins both use ldexp(), which delegates to scalbnf(). See
emscripten-core/emscripten#19781 for the
upstream bugreport.
@mosra
Copy link
Contributor

mosra commented Sep 3, 2023

In case it helps with reproducing or fixing the issue, in Magnum I'm hitting this problem with any code that uses ldexp(), which is for example the stb_image or stb_vorbis libraries.

The workaround described in #16836, i.e. passing -Wl,-u,scalbnf to the linker, resolves the issue.

@akien-mga
Copy link

akien-mga commented Sep 5, 2023

Another public test project: We face the same issue in Godot, described in godotengine/godot#80010.

Edit: I've already worked it around with -Wl,-u,scalbnf in godotengine/godot#81340, so to reproduce the issue in the current Godot repo you need to check out a commit prior to that PR (e.g. 4.1.1-stable).
Alternatively, revert PR 81340 locally.

To reproduce the issue, with Emscripten setup:

git clone https://github.com/godotengine/godot
cd godot
git checkout 4.1.1-stable
pip install scons
scons p=web target=template_release production=yes verbose=yes

The production=yes flag toggled LTO. It can also be toggled with lto=full.

It might take a while, especially the linking step as LLVM LTO doesn't use multiple threads, but eventually it fails with:

wasm-ld: error: /home/akien/Projects/godot/emscripten/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/lto/libc-mt-debug.a(scalbnf.o): attempt to add bitcode file after LTO.
em++: error: '/home/akien/Projects/godot/emscripten/emsdk/upstream/bin/wasm-ld -o bin/godot.web.template_release.wasm32.wasm [[ bunch of .o and .a files ]] modules/msdfgen/libmsdfgen_builtin.web.template_release.wasm32.a -L/home/akien/Projects/godot/emscripten/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/lto /home/akien/Projects/godot/emscripten/emsdk/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten/lto/crtbegin.o -lGL-mt-webgl2-ofb -lal -lhtml5 -lbulkmemory -lstubs-debug -lc-mt-debug -ldlmalloc-mt -lcompiler_rt-mt -lc++-mt-noexcept -lc++abi-debug-mt-noexcept -lsockets-mt -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr /tmp/tmpowj6vflhlibemscripten_js_symbols.so --import-memory --shared-memory --strip-debug --export-if-defined=main --export-if-defined=_emscripten_thread_init --export-if-defined=_emscripten_thread_exit --export-if-defined=_emscripten_thread_crashed --export-if-defined=_emscripten_tls_init --export-if-defined=pthread_self --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__start_em_lib_deps --export-if-defined=__stop_em_lib_deps --export-if-defined=__start_em_js --export-if-defined=__stop_em_js --export-if-defined=__main_argc_argv --export-if-defined=fflush --export=emscripten_stack_get_end --export=emscripten_stack_get_free --export=emscripten_stack_get_base --export=emscripten_stack_get_current --export=emscripten_stack_init --export=stackSave --export=stackRestore --export=stackAlloc --export=__errno_location --export=_emscripten_thread_free_data --export=emscripten_main_runtime_thread_id --export=emscripten_main_thread_process_queued_calls --export=_emscripten_run_on_main_thread_js --export=emscripten_stack_set_limits --export=__get_temp_ret --export=__set_temp_ret --export=__funcs_on_exit --export=__wasm_call_ctors --export=_emscripten_thread_init --export=_emscripten_thread_exit --export-table -z stack-size=65536 --initial-memory=33554432 --max-memory=2147483648 --no-entry --stack-first' failed (returned 1)
scons: *** [bin/godot.web.template_release.wasm32.js] Error 1

Full build log with Emscripten 3.1.45:
godot_build_web_lto_emscripten_3.1.45.log.zip

@kripken
Copy link
Member

kripken commented Sep 7, 2023

It sounds like this is a rather serious issue as it affects many projects, including Godot, Magnum, etc.

To make sure I understand, it sounds like this is definitely an LTO issue as everyone here is using LTO?

@sbc100 any idea what could be causing this? If not, would bisection help?

Speaking of bisection, I tried to reproduce with the STR from the last comment, but @akien-mga I don't see an error on 3.1.42:

$ emcc -v
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.42 (6ede0b8fc1c979bb206148804bfb48b472ccc3da)
clang version 17.0.0 (https://github.com/llvm/llvm-project f3b64887de61020c09404bfee97b2fadd30df10a)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /home/azakai/Dev/sdk/upstream/bin

$ nice scons p=web target=template_release production=yes verbose=yes
...lots of work, very long LTO phase...
[Initial build] Install file: "bin/godot.web.template_release.wasm32.wasm" as "bin/.web_zip/godot.wasm"
Install file: "bin/godot.web.template_release.wasm32.worker.js" as "bin/.web_zip/godot.worker.js"
Creating 'bin/godot.web.template_release.wasm32.wrapped.js'
[Initial build] Install file: "bin/godot.web.template_release.wasm32.wrapped.js" as "bin/.web_zip/godot.js"
[Initial build] Archiving bin/.web_zip/godot.js bin/.web_zip/godot.wasm bin/.web_zip/godot.worker.js bin/.web_zip/godot.audio.worklet.js bin/.web_zip/godot.html bin/.web_zip/godot.service.worker.js bin/.web_zip/godot.offline.html as bin/godot.web.template_release.wasm32.zip
[Initial build] scons: done building targets.
[Time elapsed: 00:34:20.772]

Same with 3.1.45:

[100%] Install file: "bin/godot.web.template_release.wasm32.worker.js" as "bin/.web_zip/godot.worker.js"
Creating 'bin/godot.web.template_release.wasm32.wrapped.js'
[100%] Install file: "bin/godot.web.template_release.wasm32.wasm" as "bin/.web_zip/godot.wasm"
[100%] Install file: "bin/godot.web.template_release.wasm32.wrapped.js" as "bin/.web_zip/godot.js"
[100%] Archiving bin/.web_zip/godot.js bin/.web_zip/godot.wasm bin/.web_zip/godot.worker.js bin/.web_zip/godot.audio.worklet.js bin/.web_zip/godot.html bin/.web_zip/godot.service.worker.js bin/.web_zip/godot.offline.html as bin/godot.web.template_release.wasm32.zip
[100%] scons: done building targets.
[Time elapsed: 00:29:02.422]

$ emcc -v
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.45 (ef3e4e3b044de98e1811546e0bc605c65d3412f4)
clang version 18.0.0 (https://github.com/llvm/llvm-project d1e685df45dc5944b43d2547d0138cd4a3ee4efe)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /home/azakai/Dev/sdk/upstream/bin

(I did scons -c to clean up between builds and even emcc --clear-cache to ensure a totally new build from scratch each time.)

@sbc100
Copy link
Collaborator

sbc100 commented Sep 7, 2023

I'm also struggling to reproduce this. I tried the code produced by @Honya2000 in #19781 (comment) but I can't get it to fail.

Does anyone have a simple repro case?

@akien-mga
Copy link

Speaking of bisection, I tried to reproduce with the STR from the last comment, but @akien-mga I don't see an error on 3.1.42:

That's because I already merged a workaround for this issue: godotengine/godot#81340
I should have pinned a specific commit in my steps to reproduce, I'll amend them.

@mosra
Copy link
Contributor

mosra commented Sep 8, 2023

Simple repro case is below. Fails for me with 3.1.44, removing -flto makes it pass:

// em++ ldexp.cpp -o ldexp.js -flto

#include <cmath>

int main() {
    return std::ldexp(3.0f, 3) >= 24.0f;
}

sbc100 added a commit that referenced this issue Sep 8, 2023
Continuation of #15497

Fixes: #19781
@kripken
Copy link
Member

kripken commented Sep 8, 2023

Thanks @mosra !

I bisected with that to this:

$ git bisect bad
dcada612a092d69dac9d5c8a1930198b78d98216 is the first bad commit
commit dcada612a092d69dac9d5c8a1930198b78d98216
Author: chromium-autoroll <[email protected]>
Date:   Wed Jun 7 17:52:37 2023 +0000

    Roll llvm-project from e387f49d133d to 5f6f8a8e255d (127 revisions)
    
    https://chromium.googlesource.com/external/github.com/llvm/llvm-project.git/+log/e387f49d133d..5f6f8a8e255d
    
    2023-06-07 [email protected] [WebAssembly] Add ldexp{,f,l} libcall signatures
    2023-06-07 [email protected] [InstrProf] Skip Balanced Partitioning tests on ARM
    2023-06-07 [email protected] Improve WebAssembly vector bitmask, mask reduction, and extending
    2023-06-07 [email protected] [lldb][NFCI] Change type of Broadcaster's name
    2023-06-07 [email protected] [MLIR][Tosa] Fix fp canonicalization for `clamp`
    2023-06-07 [email protected] [mlir][quant] Move comments to TableGen statements
    2023-06-07 [email protected] [MLIR][python bindings] TypeCasters for Attributes
    2023-06-07 [email protected] [AMDGPU] Regenerate some spill checks
    2023-06-07 [email protected] Fix parameter name in Sema::addInitCapture to ByRef.
    2023-06-07 [email protected] [LLVM] Removes CMake work-arounds.
    2023-06-07 [email protected] [gn] port 66a562d22e7 more (libc++ _LIBCPP_HAS_NO_FILESYSTEM)
    2023-06-07 [email protected] Add support for the NO_COLOR environment variable
    2023-06-07 [email protected] [RISCV] Use inheritance to reduce repeated code in RISCVInstrInfoV.td. NFC
    2023-06-07 [email protected] [MLIR] Removes CMake work-arounds.
    2023-06-07 [email protected] [compiler-rt] Removes CMake work-arounds.
    2023-06-07 [email protected] [gn] port 66a562d22e7 (libc++ _LIBCPP_HAS_NO_FILESYSTEM)
    2023-06-07 [email protected] Revert "[flang][hlfir] allow recursive intrinsic lowering"
    2023-06-07 [email protected] [TableGen] Emit separate computeRequiredFeatures() function
    2023-06-07 [email protected] [Option] Support special argument "--"
    2023-06-07 [email protected] [libc++] Roll up fstream support into filesystem support
    2023-06-07 [email protected] [ASAN] Fix validation size for dirent on FreeBSD
    2023-06-07 [email protected] [clang][DeclPrinter] Fix AST print of out-of-line record definitions
    2023-06-07 [email protected] [DebugMetadata] Simplify handling subprogram's retainedNodes field. NFCI (1/7)
    2023-06-07 [email protected] [flang][hlfir] allow recursive intrinsic lowering
    2023-06-07 [email protected] [flang][hlfir] relax the strictness of intrinsic verifiers
    2023-06-07 [email protected] [gn] port 1794532bb94
    2023-06-07 [email protected] [gn] port d19a3834dce5
    2023-06-07 [email protected] [LV] Add option to tune the cost model, NFC
    2023-06-07 [email protected] Reland "D144999 [MC][MachO]Only emits compact-unwind format for "canonical" personality symbols. For the rest, use DWARFs."
    2023-06-07 [email protected] [Clang] Convert some tests to opaque pointers (NFC)
    2023-06-07 [email protected] [clang][CodeGen] Fix GPU-specific attributes being dropped by bitcode linking
    2023-06-07 [email protected] [TableGen] Add !setdagarg and !setdagname
    2023-06-07 [email protected] [X86][FP16] Do not generate VBROADCAST for fp16
    2023-06-07 [email protected] PreISelIntrinsicLowering: Identify load.relative with intrinsic id
    2023-06-07 [email protected] AMDGPU: Add MF independent version of getImplicitParameterOffset
    2023-06-07 [email protected] AMDGPU: Use available subtarget member
    2023-06-07 [email protected] [Clang][OpenMP] Fix -Wcovered-switch-default in CGOpenMPRuntime.cpp (NFC)
    2023-06-07 [email protected] [AArch64][SVE] Add a commutative VSelectCommPredOrPassthruPatFrags
    2023-06-07 [email protected] [Clang][OpenMP][IRBuilder] Move registerTargetGlobalVariable & getAddrOfDeclareTargetVar into the OMPIRBuilder
    2023-06-07 [email protected] [compiler-rt] Do not redefine builtins in MinGW configs either
    2023-06-07 [email protected] [flang][hlfir] Add hlfir.dot_product intrinsic
    2023-06-07 [email protected] [libc] Silence warning about returning from noreturn function
    2023-06-07 [email protected] [sanitizer_common] Use interception macros for s390 __tls_get_addr declarations
    2023-06-07 [email protected] [clang] Show error if defaulted comparions operator function is volatile or has ref-qualifier &&.
    2023-06-07 [email protected] [AMDGPU][NFC] Add a getRegBitWidth() helper for TargetRegisterClass operands.
    2023-06-07 [email protected] [clangd] Add semantic token for labels
    2023-06-07 [email protected] [CGP] Add test to show the missed case in remove llvm.assume
    2023-06-07 [email protected] [mlir][transform] Add builder to ApplyPatternsOp
    2023-06-07 [email protected] [AMDGPU] Turn off pass to rewrite partially used virtual superregisters after RenameIndependentSubregs pass with registers of minimal size.
    2023-06-07 [email protected] [X86] Add test case for Issue #63108
    2023-06-07 [email protected] [include-cleaner] Report all specializations if the primary template is introduced by a using-decl.
    2023-06-07 [email protected] [mlir][vector][transform] Add ApplyCastAwayVectorLeadingOneDimPatternsOp
    2023-06-07 [email protected] [clang] Fix assertion while parsing an invalid for loop
    2023-06-07 [email protected] Revert "[compiler-rt] Allow 3 simultaneous interceptors on Linux"
    2023-06-07 [email protected] [AsmPrinter][AMDGPU] Generate uwtable entries in .eh_frame
    2023-06-07 [email protected] [NFC][RFC][TableGen] Split GlobalISelEmitter.cpp
    2023-06-07 [email protected] [CMake] Add option to link LLVM/subproject executables against LLVM libc
    2023-06-07 [email protected] Fix "[compiler-rt] Allow 3 simultaneous interceptors on Linux"
    2023-06-07 [email protected] [compiler-rt] Allow 3 simultaneous interceptors on Linux
    2023-06-07 [email protected] [mlir][linalg] Add option to pad Linalg ops to a specified multiple
    2023-06-07 [email protected] [mlir][linalg][NFC] Simplify padOperandToSmallestStaticBoundingBox
    2023-06-07 [email protected] [LoopIdiom] Freeze BitPos if !isGuaranteedNotToBeUndefOrPoison
    2023-06-07 [email protected] [CodeGen] Disable default copy assignment operator for struct VectorInfo
    2023-06-07 [email protected] [CodeGen] Disable default copy ctor and copy assignment operator for class Array
    2023-06-07 [email protected] [CodeGen] Disable default copy ctor and copy assignment operator for VLIWPacketizerList
    2023-06-07 [email protected] [CodeGen] Disable default copy ctor and copy assignment operator for class Scoreboard
    2023-06-07 [email protected] [CodeGen] Disable default copy ctor and copy assignment operator for InterferenceCache
    2023-06-07 [email protected] [CodeGen] Disable default copy ctor and copy assignment operator for class Circuits
    2023-06-07 [email protected] [CodeGen] Disable default copy ctor and copy assignment operator for VLIWResourceModel
    2023-06-07 [email protected] [RISCV] Fix sched class for MC layer version of vmsgt.vx.
    2023-06-07 [email protected] Set isRequired true for CFGViewer/CFGPrinter passes
    2023-06-07 [email protected] [RISCV] Fix sched class for MC layer version of vrsub.vx.
    2023-06-07 [email protected] [LoongArch] Define `ual` feature and override `allowsMisalignedMemoryAccesses`
    2023-06-07 [email protected] [RISCV] Split scheduler classes for vector min/max from compares.
    2023-06-07 [email protected] [libc] Fix undefined behavior of left shifting signed integer in exp2f.cpp.
    2023-06-07 [email protected] [NFC][Driver] Change Multilib flag representation
    2023-06-07 [email protected] [NFC][Driver] Change MultilibBuilder interface
    2023-06-07 [email protected] [ValueTracking] Implied conditions for lshr
    2023-06-07 [email protected] [InstSimplify] Add tests for shl implied conditions
    2023-06-07 [email protected] [M68k,MSP430,VE,Xtensa] Migrate to new encodeInstruction that uses SmallVectorImpl<char>. NFC
    2023-06-07 [email protected] [flang][hlfir] Enable assignments with allocatable components.
    2023-06-07 [email protected] [Hexagon,Lanai,LoongArch,Sparc] Migrate to new encodeInstruction that uses SmallVectorImpl<char>. NFC
    2023-06-07 [email protected] [LoongArch] Define the LAELF v20230519 relocs
    2023-06-07 [email protected] [LICM] [Coroutines] Don't hoist threadlocals within presplit coroutines
    2023-06-07 [email protected] [AMDGPU][IGLP]: Add rules to SchedGroups
    2023-06-07 [email protected] [gn build] Port 1794532bb942
    2023-06-07 [email protected] [InstrProf] Move BPFunctionNode test to ProfileDataTests
    2023-06-07 [email protected] [Coroutines] [LICM] Precommit test for D151774
    2023-06-07 [email protected] [RISCV] Remove overly restrictive assert from negateFMAOpcode.
    2023-06-07 [email protected] [NFC] Remove unneeded semicolon after function definition
    2023-06-07 [email protected] [RISCV] Fix UBSan failure on signed integer overflow.
    2023-06-07 [email protected] Revert "[TypePromotion] Don't treat bitcast as a Source"
    2023-06-07 [email protected] [gn build] Port 44268271f61e
    2023-06-07 [email protected] [lldb/test] Fix target-label.test on Fuchsia
    2023-06-07 [email protected] Revert "Revert "[RISCV] Add special case to selectImm for constants that can be created with (ADD (SLLI C, 32), C).""
    2023-06-07 [email protected] Revert "[RISCV] Add special case to selectImm for constants that can be created with (ADD (SLLI C, 32), C)."
    2023-06-07 [email protected] Revert "[Sanitizers] Remove BuildId from sanitizers stacktrace on
    2023-06-07 [email protected] [scudo] Enable MTE in Trusty
    2023-06-06 [email protected] [gn build] Port 1117b9a284aa
    2023-06-06 [email protected] [Sanitizers] Remove BuildId from sanitizers stacktrace on Darwin
    2023-06-06 [email protected] [Fuchsia] Add llvm-strings to Fuchsia clang build.
    2023-06-06 [email protected] [mlir][sparse][gpu] add sm8.0+ tensor core 2:4 sparsity support
    2023-06-06 [email protected] [lldb] Disable some tests on windows
    2023-06-06 [email protected] Revert "Reland [compiler-rt][CMake] Properly set COMPILER_RT_HAS_LLD"
    2023-06-06 [email protected] [BOLT][NFC] Fix debug messages
    2023-06-06 [email protected] [lldb] Fix "NameError: name 'self' is not defined" when using crashlog -c
    2023-06-06 [email protected] [Driver] Change some Separate CC1 options to use the Joined = form
    2023-06-06 [email protected] clang/HIP: Inline frexp/frexpf implementations
    2023-06-06 [email protected] Fix LLVM Sphinx build; NFC
    2023-06-06 [email protected] [RISCV] Use const reference when looping over RISCVMatInt::InstSeq. NFC
    2023-06-06 [email protected] [NVPTX] Adapt tests to make them usable with CUDA-12.x
    2023-06-06 [email protected] LangRef: Try to fix sphinx bot error
    2023-06-06 [email protected] ValueTracking: Add baseline test for ldexp computeKnownFPClass
    2023-06-06 [email protected] [test] Restore x86-registered-target in Driver/as-warnings.c
    2023-06-06 [email protected] clang: Start emitting intrinsic for __builtin_ldexp*
    2023-06-06 [email protected] IR: Add llvm.ldexp and llvm.experimental.constrained.ldexp intrinsics
    2023-06-06 [email protected] AMDGPU/GlobalISel: Fix broken / copy paste error in sext_inreg test
    2023-06-06 [email protected] [libc++] Refactor __less
    2023-06-06 [email protected] [Driver] Change some Separate CC1 options to use the Joined = form
    2023-06-06 [email protected] [profi][NFC] Get rid of afdo_detail::TypeMap
    2023-06-06 [email protected] [bazel] Port 44268271f61e46636619623d52013c3be3e272c0
    2023-06-06 [email protected] [bazel] Port 1117b9a284aa6e4b1f3cbde31825605bd07a2384
    2023-06-06 [email protected] [libc++] Disable int128_t and ship filesystem on MSVC by default
    2023-06-06 [email protected] [RISCV] Use PACK in RISCVMatInt for constants that have the same lower and upper 32 bits.
    2023-06-06 [email protected] [libc++] Use Lit annotations for all .gen.py tests
    2023-06-06 [email protected] [RISCV] Add early out to generateInstSeq when the initial sequence is 1 or 2 instructions.
    2023-06-06 [email protected] [NFC][lld][COFF] Rename findFile* methods

Unfortunately a pretty long list landed there. I can bisect into that range, but @sbc100 I see you have a PR open already so maybe you know the cause of the regression already?

@akien-mga
Copy link

You've got 4 commits directly related to ldexp in that batch:

    2023-06-07 [email protected] [WebAssembly] Add ldexp{,f,l} libcall signatures
    2023-06-06 [email protected] ValueTracking: Add baseline test for ldexp computeKnownFPClass
    2023-06-06 [email protected] clang: Start emitting intrinsic for __builtin_ldexp*
    2023-06-06 [email protected] IR: Add llvm.ldexp and llvm.experimental.constrained.ldexp intrinsics

sbc100 added a commit that referenced this issue Sep 8, 2023
Continuation of #15497

Fixes: #19781
sbc100 added a commit that referenced this issue Sep 8, 2023
Continuation of #15497

Fixes: #19781
sbc100 added a commit that referenced this issue Sep 8, 2023
@kripken
Copy link
Member

kripken commented Sep 8, 2023

(For anyone like me confused by the connection between scalbnf and ldexp, it seems that "On binary systems (where FLT_RADIX is 2), scalbn is equivalent to ldexp").

@mosra
Copy link
Contributor

mosra commented Sep 9, 2023

Just to give more context on how I found that myself -- after seeing that none of the code on my end called scalbnf directly, I went looking for scalbnf inside musl, and found that ldexp() uses it: https://github.com/emscripten-core/emscripten/blob/main/system/lib/libc/musl/src/math/ldexpf.c

Which looked way more likely to actually get called, and indeed the stb_image and stb_vorbis libraries (which were the only two failing in my case) are using it. Same process could be possibly used for htons etc., although I didn't find anything immediately obvious when searching for them now.

Not sure what exactly LTO does that it doesn't see the dependency between the functions.

@huangy10
Copy link

This problem can be solved by adding -Wl,-u,scalbnl to the linker flags.

Read this: #16836

@justinzhuguangwen
Copy link

This problem can be solved by adding -Wl,-u,scalbnl to the linker flags.

Read this: #16836

Thanks, adding -Wl,-u,scalbnl to the linker flags worked perfectly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants