Skip to content

Commit d0156b4

Browse files
authored
[emscripten] Simply LTO flag setup. NFC (#7997)
We have a setting (BYN_ENABLE_LTO) which controls if LTO is enabled, and this is enabled for emscripten by default. With emscripten we also want to add `-flto=thin` to the linker flags (on other targets its not needed but in emscripten we need this so the linker knows to use specific LTO system libraries) I'm not sure if was intentional to use `-flto` and some places and `-flto=thin` in others, but this change consistency does thing LTO. If we want to add option for full LTO we could do that later. Also, don't enable LTO by default in debug build.
1 parent bc79e4e commit d0156b4

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

CMakeLists.txt

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ endif()
3131
# more useful error reports from users.
3232
option(BYN_ENABLE_ASSERTIONS "Enable assertions" ON)
3333

34-
option(BYN_ENABLE_LTO "Build with LTO" ${EMSCRIPTEN})
34+
if(EMSCRIPTEN AND NOT "${CMAKE_BUILD_TYPE}" MATCHES "Debug")
35+
# in opt builds, LTO helps so much (>20%) it's worth slow compile times,
36+
# so we enable LTO by default in non-debug emscripten builds.
37+
set(DEFAULT_LTO ON)
38+
else()
39+
set(DEFAULT_LTO OFF)
40+
endif()
41+
option(BYN_ENABLE_LTO "Build with LTO" ${DEFAULT_LTO})
3542

3643
# Turn this off to avoid the dependency on gtest.
3744
option(BUILD_TESTS "Build GTest-based tests" ON)
@@ -222,6 +229,11 @@ if(BYN_ENABLE_LTO)
222229
set_property(GLOBAL APPEND PROPERTY JOB_POOLS link_job_pool=2)
223230
set(CMAKE_JOB_POOL_LINK link_job_pool)
224231
add_compile_flag("-flto=thin")
232+
if(EMSCRIPTEN)
233+
# Under emscripten we also add `-flto` to the linker flags to tell the
234+
# compiler driver to choose LTO versions of the system libraries.
235+
add_link_flag("-flto=thin")
236+
endif()
225237
endif()
226238

227239
if(MSVC)
@@ -353,10 +365,6 @@ if(EMSCRIPTEN)
353365
# On Node.js, make the tools immediately usable.
354366
add_link_flag("-sNODERAWFS")
355367
endif()
356-
if (BYN_ENABLE_LTO)
357-
# in opt builds, LTO helps so much (>20%) it's worth slow compile times
358-
add_nondebug_compile_flag("-flto")
359-
endif()
360368
if(EMSCRIPTEN_ENABLE_WASM64)
361369
add_compile_flag("-sMEMORY64")
362370
add_link_flag("-sMEMORY64")
@@ -560,9 +568,6 @@ if(EMSCRIPTEN)
560568
target_link_libraries(binaryen_wasm PRIVATE optimized "--closure=1")
561569
# TODO: Fix closure warnings! (#5062)
562570
target_link_libraries(binaryen_wasm PRIVATE optimized "-Wno-error=closure")
563-
if (BYN_ENABLE_LTO)
564-
target_link_libraries(binaryen_wasm PRIVATE optimized "-flto")
565-
endif()
566571
target_link_libraries(binaryen_wasm PRIVATE debug "--profiling")
567572
# Avoid catching exit as that can confuse error reporting in Node,
568573
# see https://github.com/emscripten-core/emscripten/issues/17228
@@ -614,9 +619,6 @@ if(EMSCRIPTEN)
614619
endif()
615620
# TODO: Fix closure warnings! (#5062)
616621
target_link_libraries(binaryen_js PRIVATE optimized "-Wno-error=closure")
617-
if(BYN_ENABLE_LTO)
618-
target_link_libraries(binaryen_js PRIVATE optimized "-flto")
619-
endif()
620622
target_link_libraries(binaryen_js PRIVATE debug "--profiling")
621623
target_link_libraries(binaryen_js PRIVATE debug "-sASSERTIONS")
622624
# Avoid catching exit as that can confuse error reporting in Node,

0 commit comments

Comments
 (0)