Skip to content

Commit

Permalink
Enable LTO for release builds
Browse files Browse the repository at this point in the history
  • Loading branch information
Oipo committed Nov 3, 2024
1 parent f051a97 commit 4274aaa
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
52 changes: 43 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ else()
set(ICHOR_BUILDING_DEBUG OFF)
endif()


if(CMAKE_BUILD_TYPE STREQUAL "Release")
include(CheckIPOSupported)
check_ipo_supported(RESULT LTO_SUPPORTED OUTPUT LTO_ERROR)
if(LTO_SUPPORTED)
set(CMAKE_POLICY_DEFAULT_CMP0069 NEW)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
set(ICHOR_LTO ON)
message(STATUS "Enabled LTO")
else()
set(ICHOR_LTO OFF)
message(STATUS "LTO not supported: ${LTO_ERROR}")
endif()
endif()

option(ICHOR_BUILD_EXAMPLES "Build examples" ON)
option(ICHOR_BUILD_BENCHMARKS "Build benchmarks" ON)
option(ICHOR_BUILD_TESTING "Build tests" ON)
Expand Down Expand Up @@ -111,14 +126,18 @@ if(ICHOR_USE_LIBURING)
set(ICHOR_TCP_SOURCES ${ICHOR_TCP_SOURCES} ${ICHOR_TOP_DIR}/src/services/network/tcp/IOUringTcpConnectionService.cpp ${ICHOR_TOP_DIR}/src/services/network/tcp/IOUringTcpHostService.cpp)
set(ICHOR_TIMER_SOURCES ${ICHOR_TIMER_SOURCES} ${ICHOR_TOP_DIR}/src/services/timer/IOUringTimerFactoryFactory.cpp ${ICHOR_TOP_DIR}/src/services/timer/IOUringTimer.cpp)
endif()
if(ICHOR_USE_MIMALLOC AND NOT ICHOR_USE_SYSTEM_MIMALLOC)
set(ICHOR_FRAMEWORK_SOURCES ${ICHOR_FRAMEWORK_SOURCES} ${ICHOR_TOP_DIR}/external/mimalloc/src/static.c)
endif()

file(GLOB SPDLOG_SOURCES ${ICHOR_EXTERNAL_DIR}/spdlog/src/*.cpp)

add_library(ichor ${FMT_SOURCES} ${ICHOR_FRAMEWORK_SOURCES} ${ICHOR_LOGGING_SOURCES} ${ICHOR_TCP_SOURCES} ${ICHOR_METRICS_SOURCES} ${ICHOR_TIMER_SOURCES} ${ICHOR_IO_SOURCES} ${ICHOR_BASE64_SOURCES} ${ICHOR_STL_SOURCES})

if(ICHOR_USE_MIMALLOC AND NOT ICHOR_USE_SYSTEM_MIMALLOC)
set(MIMALLOC_SOURCES ${ICHOR_TOP_DIR}/external/mimalloc/src/static.c)
add_library(ichor-mimalloc SHARED ${MIMALLOC_SOURCES})
target_link_libraries(ichor PUBLIC ichor-mimalloc)
set(MIMALLOC_TARGET ichor-mimalloc)
endif()

if(ICHOR_ENABLE_INTERNAL_DEBUGGING)
target_compile_definitions(ichor PUBLIC ICHOR_ENABLE_INTERNAL_DEBUGGING)
endif()
Expand Down Expand Up @@ -165,6 +184,16 @@ endif()
if(ICHOR_BUILDING_DEBUG)
target_compile_definitions(ichor PUBLIC ICHOR_BUILDING_DEBUG)
endif()
if(ICHOR_LTO)
if(ICHOR_COMPILER_ID STREQUAL "clang")
target_compile_options(ichor PUBLIC -fwhole-program-vtables)
target_link_options(ichor PUBLIC -Wl,--exclude-libs,ALL)
endif()
if(ICHOR_COMPILER_ID STREQUAL "gnu")
target_compile_options(ichor PUBLIC -fwhole-program)
target_link_options(ichor PUBLIC -Wl,--exclude-libs,ALL)
endif()
endif()

if(NOT WIN32)
# Everything is c++ specific so that the liburing source files, which don't meet Ichor's standards, spam warnings all over.
Expand Down Expand Up @@ -457,24 +486,29 @@ if(ICHOR_USE_MIMALLOC)
target_compile_definitions(ichor PRIVATE ICHOR_USE_SYSTEM_MIMALLOC)
else()
if(ICHOR_USE_HARDENING OR ICHOR_ENABLE_INTERNAL_DEBUGGING)
target_compile_definitions(ichor PRIVATE MI_SECURE=4)
target_compile_definitions(ichor-mimalloc PRIVATE MI_SECURE=4)
endif()
if(ICHOR_ENABLE_INTERNAL_DEBUGGING)
target_compile_definitions(ichor PRIVATE MI_DEBUG=3)
target_compile_definitions(ichor-mimalloc PRIVATE MI_DEBUG=3)
endif()
if(ICHOR_USE_SANITIZERS AND NOT WIN32)
target_compile_definitions(ichor PRIVATE MI_TRACK_ASAN=1)
target_compile_definitions(ichor-mimalloc PRIVATE MI_TRACK_ASAN=1)
endif()
if(ICHOR_USE_THREAD_SANITIZER AND NOT WIN32)
target_compile_definitions(ichor PRIVATE MI_TSAN=1)
target_compile_definitions(ichor-mimalloc PRIVATE MI_TSAN=1)
endif()
if(ICHOR_MUSL)
target_compile_definitions(ichor PRIVATE MI_LIBC_MUSL=1)
target_compile_definitions(ichor-mimalloc PRIVATE MI_LIBC_MUSL=1)
endif()
if(NOT WIN32)
target_compile_options(ichor-mimalloc PUBLIC -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free)
target_compile_options(ichor PUBLIC -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free)
endif()
target_compile_definitions(ichor PRIVATE ICHOR_USE_MIMALLOC)
target_compile_definitions(ichor-mimalloc PRIVATE ICHOR_USE_MIMALLOC)
target_include_directories(ichor-mimalloc PUBLIC
$<BUILD_INTERFACE:${ICHOR_EXTERNAL_DIR}/mimalloc/include>
$<INSTALL_INTERFACE:include/ichor/external>)
target_include_directories(ichor PUBLIC
$<BUILD_INTERFACE:${ICHOR_EXTERNAL_DIR}/mimalloc/include>
$<INSTALL_INTERFACE:include/ichor/external>)
Expand Down Expand Up @@ -689,7 +723,7 @@ install(TARGETS ichor ${MIMALLOC_TARGET}
ARCHIVE DESTINATION lib
RUNTIME DESTINATION bin
INCLUDES DESTINATION include
)
)

if(ICHOR_USE_MIMALLOC AND NOT ICHOR_USE_SYSTEM_MIMALLOC)
set(mi_version "2.0")
Expand Down
16 changes: 16 additions & 0 deletions IchorConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set(ICHOR_USE_SYSTEM_MIMALLOC @ICHOR_USE_SYSTEM_MIMALLOC@)
set(ICHOR_USE_SDEVENT @ICHOR_USE_SDEVENT@)
set(ICHOR_USE_BOOST_BEAST @ICHOR_USE_BOOST_BEAST@)
set(ICHOR_USE_HIREDIS @ICHOR_USE_HIREDIS@)
set(ICHOR_LTO @ICHOR_LTO@)

if(ICHOR_USE_SYSTEM_MIMALLOC)
find_dependency(mimalloc REQUIRED)
Expand All @@ -20,5 +21,20 @@ endif()
if(ICHOR_USE_HIREDIS)
find_dependency(hiredis REQUIRED)
endif()
if(ICHOR_LTO)
include(CheckIPOSupported)
check_ipo_supported(RESULT LTO_SUPPORTED OUTPUT LTO_ERROR)
if(LTO_SUPPORTED)
list(LENGTH BUILDSYSTEM_TARGETS ICHOR_TOTAL_TARGETS)
if(${ICHOR_TOTAL_TARGET} GREATER "0")
message(FATAL_ERROR "Ichor LTO enabled but built targets have been defined before inclusion of Ichor, resulting in potential mismatch. Please include ichor first.")
endif()
set(CMAKE_POLICY_DEFAULT_CMP0069 NEW)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
message(STATUS "Enabled LTO")
else()
message(FATAL_ERROR "LTO not supported but Ichor built with LTO: ${LTO_ERROR}")
endif()
endif()

include("${CMAKE_CURRENT_LIST_DIR}/IchorTargets.cmake")

0 comments on commit 4274aaa

Please sign in to comment.