ladybird/Meta/CMake/serenity_compile_options.cmake
Daniel Bertalan c0cf6e3744 CMake: Set -fvisibility-inlines-hidden on ELF platforms
The C++ semantics of `inline` dictate that such functions might be
defined in multiple translation units. As with all other functions, they
must have the same address in all TUs. Because of this, the compiler
emits `inline` functions as weak symbols, which the dynamic linker can
then resolve to the same address everywhere.

This rule is responsible for a significant overhead at startup, as such
lookups happen by name. Namely, 86'000 of the 114'000 by-name symbol
lookups when loading LibWeb can be attributed to this. Most of these
are only ever defined in a single object, making this even more
pointless.

As nothing in our code relies on either ELF symbol interposition rules
or function address equality, we can use the -fvisibility-inlines-hidden
escape hatch, which causes this rule to be disregarded. As the symbols
are now hidden, no load-time symbol lookup is needed. This flag is used
without issues in other large C++ codebases like Chromium and LLVM.

Some relevant light reading, suggested by Nico:
- https://ridiculousfish.com/blog/posts/i-didnt-order-that-so-why-is-it-on-my-bill-episode-1.html
- https://www.cs.dartmouth.edu/~sergey/cs258/ABI/UlrichDrepper-How-To-Write-Shared-Libraries.pdf
- https://blog.llvm.org/2018/11/30-faster-windows-builds-with-clang-cl_14.html
- https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Dialect-Options.html#index-fvisibility-inlines-hidden
2023-08-12 05:14:20 +02:00

47 lines
2 KiB
CMake

include(${CMAKE_CURRENT_LIST_DIR}/common_compile_options.cmake)
# The following warnings are sorted by the "base" name (the part excluding the initial Wno or W).
add_compile_options(-Wno-address-of-packed-member)
add_compile_options(-Wcast-qual)
add_compile_options(-Wdeprecated-copy)
add_compile_options(-Wduplicated-cond)
add_compile_options(-Wformat=2)
add_compile_options(-Wimplicit-fallthrough)
add_compile_options(-Wlogical-op)
add_compile_options(-Wmisleading-indentation)
add_compile_options(-Wmissing-declarations)
add_compile_options(-Wnon-virtual-dtor)
add_compile_options(-Wsuggest-override)
add_compile_options(-Wundef)
add_compile_options(-Wunused)
add_compile_options(-Wwrite-strings)
add_compile_options(-fno-delete-null-pointer-checks)
add_compile_options(-ffile-prefix-map=${SerenityOS_SOURCE_DIR}=.)
add_compile_options(-fsized-deallocation)
add_compile_options(-fstack-clash-protection)
add_compile_options(-fstack-protector-strong)
add_link_options(-fstack-protector-strong)
add_compile_options(-g1)
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_compile_options(-Wno-maybe-uninitialized)
add_compile_options(-Wcast-align)
add_compile_options(-Wdouble-promotion)
elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang$")
add_compile_options(-Wno-atomic-alignment)
add_compile_options(-Wno-unused-const-variable)
add_compile_options(-Wno-unused-private-field)
# Clang doesn't add compiler_rt to the search path when compiling with -nostdlib.
string(REGEX REPLACE "\\.(.*)" "" LLVM_MAJOR_VERSION "${CMAKE_CXX_COMPILER_VERSION}")
link_directories(${TOOLCHAIN_ROOT}/lib/clang/${LLVM_MAJOR_VERSION}/lib/${SERENITY_ARCH}-pc-serenity/)
endif()
if ("${SERENITY_ARCH}" STREQUAL "aarch64")
# Unaligned memory access will cause a trap, so to make sure the compiler doesn't generate
# those unaligned accesses, the strict-align flag is added.
# FIXME: Remove -Wno-cast-align when we are able to build everything without this warning turned on.
add_compile_options(-mstrict-align -Wno-cast-align)
endif()