From 118a2d552684538349b5ec0673bd8634678864a3 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 12 Jan 2026 17:59:37 -0800 Subject: [PATCH] Fix LTO build * Don't use LTO for startup objects * Put startup objects in the normal sysroot without LTO * Pass `-flto` in linker flags * Disable a test that fails with LTO enabled (unsure why) --- .github/workflows/main.yml | 6 ++++++ CMakeLists.txt | 2 ++ libc-bottom-half/CMakeLists.txt | 32 +++++++++++++++++--------------- test/CMakeLists.txt | 3 +++ 4 files changed, 28 insertions(+), 15 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b337b55ac..4ac5454c6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -144,6 +144,12 @@ jobs: test: true args: -DMALLOC=emmalloc + - name: Test LTO + os: ubuntu-24.04 + clang_version: 19 + test: true + args: -DLTO=full -DTARGET_TRIPLE=wasm32-wasip2 -DCHECK_SYMBOLS=OFF + steps: - uses: actions/checkout@v4.1.7 with: diff --git a/CMakeLists.txt b/CMakeLists.txt index 3da53accd..160c7c4ff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -119,8 +119,10 @@ if(LTO) set(SYSROOT_LIB ${SYSROOT_LIB}/llvm-lto/${clang_version}) if(LTO STREQUAL "thin") add_compile_options(-flto=thin) + add_link_options(-flto=thin) elseif(LTO STREQUAL "full") add_compile_options(-flto) + add_link_options(-flto) else() message(FATAL_ERROR "Unknown LTO mode: ${LTO}") endif() diff --git a/libc-bottom-half/CMakeLists.txt b/libc-bottom-half/CMakeLists.txt index b6445f924..a95a8c475 100644 --- a/libc-bottom-half/CMakeLists.txt +++ b/libc-bottom-half/CMakeLists.txt @@ -195,21 +195,23 @@ foreach(file crt/crt1-command.c clang_format_target(${stem}) target_link_libraries(${stem} PRIVATE musl-top-half-interface) set_pic(${stem}) - target_compile_options(${stem} PRIVATE -fvisibility=default) + target_compile_options(${stem} PRIVATE -fvisibility=default -fno-lto) endforeach() +set(crt_sysroot ${SYSROOT}/lib/${TARGET_TRIPLE}) + # crt1-reactor.o is a straight copy of what CMake produces add_custom_command( - OUTPUT ${SYSROOT_LIB}/crt1-reactor.o - COMMAND ${CMAKE_COMMAND} -E copy $ ${SYSROOT_LIB}/crt1-reactor.o + OUTPUT ${crt_sysroot}/crt1-reactor.o + COMMAND ${CMAKE_COMMAND} -E copy $ ${crt_sysroot}/crt1-reactor.o DEPENDS crt1-reactor $ ) if (WASI STREQUAL "p1") # wasip1: crt1-command.o is a straight copy of what CMake produces add_custom_command( - OUTPUT ${SYSROOT_LIB}/crt1-command.o - COMMAND ${CMAKE_COMMAND} -E copy $ ${SYSROOT_LIB}/crt1-command.o + OUTPUT ${crt_sysroot}/crt1-command.o + COMMAND ${CMAKE_COMMAND} -E copy $ ${crt_sysroot}/crt1-command.o DEPENDS crt1-command $ ) elseif (WASI STREQUAL "p2") @@ -232,24 +234,24 @@ elseif (WASI STREQUAL "p2") # please feel free to open an issue or a PR and maintainers can work with you # on getting that integrated. add_custom_command( - OUTPUT ${SYSROOT_LIB}/crt1-command.o + OUTPUT ${crt_sysroot}/crt1-command.o COMMAND ${wasm_tools} component embed ${wasip2_wit_dir} $ --world wasi:cli/command@0.2.0 - -o ${SYSROOT_LIB}/crt1-command.o + -o ${crt_sysroot}/crt1-command.o DEPENDS crt1-command wasip2-wits $ wasm-tools ) elseif (WASI STREQUAL "p3") add_custom_command( - OUTPUT ${SYSROOT_LIB}/crt1-command.o + OUTPUT ${crt_sysroot}/crt1-command.o COMMAND ${wasm_tools} component embed ${wasip3_wit_dir} $ --world wasi:cli/command@0.3.0-rc-2025-09-16 - -o ${SYSROOT_LIB}/crt1-command.o + -o ${crt_sysroot}/crt1-command.o DEPENDS crt1-command wasip3-wits $ wasm-tools ) else() @@ -259,15 +261,15 @@ endif() # Provide a plain crt1.o for toolchain compatibility, identical to # `crt1-command.c` add_custom_command( - OUTPUT ${SYSROOT_LIB}/crt1.o - COMMAND ${CMAKE_COMMAND} -E copy ${SYSROOT_LIB}/crt1-command.o ${SYSROOT_LIB}/crt1.o - DEPENDS ${SYSROOT_LIB}/crt1-command.o + OUTPUT ${crt_sysroot}/crt1.o + COMMAND ${CMAKE_COMMAND} -E copy ${crt_sysroot}/crt1-command.o ${crt_sysroot}/crt1.o + DEPENDS ${crt_sysroot}/crt1-command.o ) add_custom_target(sysroot-startup-objects DEPENDS - ${SYSROOT_LIB}/crt1-reactor.o - ${SYSROOT_LIB}/crt1-command.o - ${SYSROOT_LIB}/crt1.o + ${crt_sysroot}/crt1-reactor.o + ${crt_sysroot}/crt1-command.o + ${crt_sysroot}/crt1.o ) add_dependencies(sysroot sysroot-startup-objects) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index e33c39a02..95ce97228 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -193,6 +193,9 @@ if (MALLOC STREQUAL "emmalloc") set_tests_properties(libc_test_functional_search_hsearch.wasm PROPERTIES WILL_FAIL TRUE) endif() add_libc_test(functional/search_insque.c) +if (LTO STREQUAL "full") + set_tests_properties(libc_test_functional_search_insque.wasm PROPERTIES WILL_FAIL TRUE) +endif() add_libc_test(functional/search_lsearch.c) add_libc_test(functional/search_tsearch.c) add_libc_test(functional/snprintf.c)