From d7fc27ed8b77c7fb5b4c57c914c8afc76c6b2921 Mon Sep 17 00:00:00 2001 From: Michael de Lang Date: Mon, 4 Nov 2024 22:27:47 +0100 Subject: [PATCH] Fix compilation with mimalloc and LTO --- CMakeLists.txt | 30 +++++------- IchorConfig.cmake.in | 4 ++ .../beast_benchmark/boost_beast_benchmark.cpp | 1 + .../ichor_coroutine_benchmark.cpp | 1 + .../event_benchmark/ichor_event_benchmark.cpp | 1 + .../ichor_serializer_benchmark.cpp | 1 + .../start_benchmark/ichor_start_benchmark.cpp | 1 + .../ichor_start_stop_benchmark.cpp | 1 + .../utils_benchmark/ichor_utils_benchmark.cpp | 1 + examples/etcd_example/main.cpp | 1 + examples/event_statistics_example/main.cpp | 1 + examples/factory_example/main.cpp | 1 + examples/http_example/main.cpp | 1 + examples/http_ping_pong/ping.cpp | 1 + examples/http_ping_pong/pong.cpp | 1 + examples/introspection_example/main.cpp | 1 + examples/minimal_example/main.cpp | 1 + examples/multithreaded_example/main.cpp | 1 + examples/optional_dependency_example/main.cpp | 1 + examples/realtime_example/main.cpp | 1 + examples/serializer_example/main.cpp | 1 + examples/tcp_example/main.cpp | 1 + examples/timer_example/main.cpp | 1 + examples/websocket_example/main.cpp | 1 + examples/yielding_timer_example/main.cpp | 1 + include/ichor/ichor-mimalloc.h | 49 +++++++++++++++++++ src/ichor/DependencyManager.cpp | 48 ------------------ test/Common.h | 1 + 28 files changed, 90 insertions(+), 65 deletions(-) create mode 100644 include/ichor/ichor-mimalloc.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7b67a5d4..6334d873 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,6 +54,10 @@ if(CMAKE_BUILD_TYPE STREQUAL "Release") if(LTO_SUPPORTED) set(CMAKE_POLICY_DEFAULT_CMP0069 NEW) set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE) + if(ICHOR_COMPILER_ID STREQUAL "clang") + set(CMAKE_C_COMPILE_OPTIONS_IPO "-flto=full") + set(CMAKE_CXX_COMPILE_OPTIONS_IPO "-flto=full") + endif() set(ICHOR_LTO ON) message(STATUS "Enabled LTO") else() @@ -129,15 +133,12 @@ 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) + set(ICHOR_FRAMEWORK_SOURCES ${ICHOR_FRAMEWORK_SOURCES} ${ICHOR_TOP_DIR}/external/mimalloc/src/static.c) endif() +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_ENABLE_INTERNAL_DEBUGGING) target_compile_definitions(ichor PUBLIC ICHOR_ENABLE_INTERNAL_DEBUGGING) endif() @@ -486,29 +487,24 @@ 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-mimalloc PRIVATE MI_SECURE=4) + target_compile_definitions(ichor PRIVATE MI_SECURE=4) endif() if(ICHOR_ENABLE_INTERNAL_DEBUGGING) - target_compile_definitions(ichor-mimalloc PRIVATE MI_DEBUG=3) + target_compile_definitions(ichor PRIVATE MI_DEBUG=3) endif() if(ICHOR_USE_SANITIZERS AND NOT WIN32) - target_compile_definitions(ichor-mimalloc PRIVATE MI_TRACK_ASAN=1) + target_compile_definitions(ichor PRIVATE MI_TRACK_ASAN=1) endif() if(ICHOR_USE_THREAD_SANITIZER AND NOT WIN32) - target_compile_definitions(ichor-mimalloc PRIVATE MI_TSAN=1) + target_compile_definitions(ichor PRIVATE MI_TSAN=1) endif() if(ICHOR_MUSL) - target_compile_definitions(ichor-mimalloc PRIVATE MI_LIBC_MUSL=1) + target_compile_definitions(ichor 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 - $ - $) + target_compile_definitions(ichor PUBLIC ICHOR_USE_MIMALLOC) target_include_directories(ichor PUBLIC $ $) diff --git a/IchorConfig.cmake.in b/IchorConfig.cmake.in index 5983d064..31db2c04 100644 --- a/IchorConfig.cmake.in +++ b/IchorConfig.cmake.in @@ -31,6 +31,10 @@ if(ICHOR_LTO) endif() set(CMAKE_POLICY_DEFAULT_CMP0069 NEW) set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE) + if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + set(CMAKE_C_COMPILE_OPTIONS_IPO "-flto=full") + set(CMAKE_CXX_COMPILE_OPTIONS_IPO "-flto=full") + endif() message(STATUS "Enabled LTO") else() message(FATAL_ERROR "LTO not supported but Ichor built with LTO: ${LTO_ERROR}") diff --git a/benchmarks/beast_benchmark/boost_beast_benchmark.cpp b/benchmarks/beast_benchmark/boost_beast_benchmark.cpp index 90c9dab5..3fc2163b 100644 --- a/benchmarks/beast_benchmark/boost_beast_benchmark.cpp +++ b/benchmarks/beast_benchmark/boost_beast_benchmark.cpp @@ -28,6 +28,7 @@ #include #include #include +#include namespace beast = boost::beast; // from namespace http = beast::http; // from diff --git a/benchmarks/coroutine_benchmark/ichor_coroutine_benchmark.cpp b/benchmarks/coroutine_benchmark/ichor_coroutine_benchmark.cpp index cb7bc70b..cf7365dd 100644 --- a/benchmarks/coroutine_benchmark/ichor_coroutine_benchmark.cpp +++ b/benchmarks/coroutine_benchmark/ichor_coroutine_benchmark.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include diff --git a/benchmarks/event_benchmark/ichor_event_benchmark.cpp b/benchmarks/event_benchmark/ichor_event_benchmark.cpp index bcbdb730..d714c6b1 100644 --- a/benchmarks/event_benchmark/ichor_event_benchmark.cpp +++ b/benchmarks/event_benchmark/ichor_event_benchmark.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include diff --git a/benchmarks/serializer_benchmark/ichor_serializer_benchmark.cpp b/benchmarks/serializer_benchmark/ichor_serializer_benchmark.cpp index 12858106..6935b32c 100644 --- a/benchmarks/serializer_benchmark/ichor_serializer_benchmark.cpp +++ b/benchmarks/serializer_benchmark/ichor_serializer_benchmark.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include diff --git a/benchmarks/start_benchmark/ichor_start_benchmark.cpp b/benchmarks/start_benchmark/ichor_start_benchmark.cpp index 06257e9f..3e1de9ec 100644 --- a/benchmarks/start_benchmark/ichor_start_benchmark.cpp +++ b/benchmarks/start_benchmark/ichor_start_benchmark.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include diff --git a/benchmarks/start_stop_benchmark/ichor_start_stop_benchmark.cpp b/benchmarks/start_stop_benchmark/ichor_start_stop_benchmark.cpp index 5faa5c06..8df1ca13 100644 --- a/benchmarks/start_stop_benchmark/ichor_start_stop_benchmark.cpp +++ b/benchmarks/start_stop_benchmark/ichor_start_stop_benchmark.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include diff --git a/benchmarks/utils_benchmark/ichor_utils_benchmark.cpp b/benchmarks/utils_benchmark/ichor_utils_benchmark.cpp index 317aced0..c14fffb6 100644 --- a/benchmarks/utils_benchmark/ichor_utils_benchmark.cpp +++ b/benchmarks/utils_benchmark/ichor_utils_benchmark.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include "../../examples/common/lyra.hpp" diff --git a/examples/etcd_example/main.cpp b/examples/etcd_example/main.cpp index 4b37d53b..56ed92b1 100644 --- a/examples/etcd_example/main.cpp +++ b/examples/etcd_example/main.cpp @@ -6,6 +6,7 @@ #include #include #include +#include // Some compile time logic to instantiate a regular cout logger or to use the spdlog logger, if Ichor has been compiled with it. #ifdef ICHOR_USE_SPDLOG diff --git a/examples/event_statistics_example/main.cpp b/examples/event_statistics_example/main.cpp index c60f8c9b..6066867f 100644 --- a/examples/event_statistics_example/main.cpp +++ b/examples/event_statistics_example/main.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "UsingStatisticsService.h" // Some compile time logic to instantiate a regular cout logger or to use the spdlog logger, if Ichor has been compiled with it. diff --git a/examples/factory_example/main.cpp b/examples/factory_example/main.cpp index 191fbb47..467b4d81 100644 --- a/examples/factory_example/main.cpp +++ b/examples/factory_example/main.cpp @@ -3,6 +3,7 @@ #include "RuntimeCreatedService.h" #include #include +#include // Some compile time logic to instantiate a regular cout logger or to use the spdlog logger, if Ichor has been compiled with it. #ifdef ICHOR_USE_SPDLOG diff --git a/examples/http_example/main.cpp b/examples/http_example/main.cpp index 693c1c38..9d41c11d 100644 --- a/examples/http_example/main.cpp +++ b/examples/http_example/main.cpp @@ -8,6 +8,7 @@ #include #include #include +#include // Some compile time logic to instantiate a regular cout logger or to use the spdlog logger, if Ichor has been compiled with it. #ifdef ICHOR_USE_SPDLOG diff --git a/examples/http_ping_pong/ping.cpp b/examples/http_ping_pong/ping.cpp index 44c30927..8ce2141d 100644 --- a/examples/http_ping_pong/ping.cpp +++ b/examples/http_ping_pong/ping.cpp @@ -9,6 +9,7 @@ #include #include #include +#include // Some compile time logic to instantiate a regular cout logger or to use the spdlog logger, if Ichor has been compiled with it. #ifdef ICHOR_USE_SPDLOG diff --git a/examples/http_ping_pong/pong.cpp b/examples/http_ping_pong/pong.cpp index fa0f4afd..07a281a7 100644 --- a/examples/http_ping_pong/pong.cpp +++ b/examples/http_ping_pong/pong.cpp @@ -7,6 +7,7 @@ #include #include #include +#include // Some compile time logic to instantiate a regular cout logger or to use the spdlog logger, if Ichor has been compiled with it. #ifdef ICHOR_USE_SPDLOG diff --git a/examples/introspection_example/main.cpp b/examples/introspection_example/main.cpp index 84d6218e..8cff5ab4 100644 --- a/examples/introspection_example/main.cpp +++ b/examples/introspection_example/main.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include "IntrospectionService.h" // Some compile time logic to instantiate a regular cout logger or to use the spdlog logger, if Ichor has been compiled with it. diff --git a/examples/minimal_example/main.cpp b/examples/minimal_example/main.cpp index 0c9075d6..800cd113 100644 --- a/examples/minimal_example/main.cpp +++ b/examples/minimal_example/main.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include using namespace Ichor; diff --git a/examples/multithreaded_example/main.cpp b/examples/multithreaded_example/main.cpp index a79461a0..fcec3a90 100644 --- a/examples/multithreaded_example/main.cpp +++ b/examples/multithreaded_example/main.cpp @@ -2,6 +2,7 @@ #include "OtherService.h" #include #include +#include // Some compile time logic to instantiate a regular cout logger or to use the spdlog logger, if Ichor has been compiled with it. #ifdef ICHOR_USE_SPDLOG diff --git a/examples/optional_dependency_example/main.cpp b/examples/optional_dependency_example/main.cpp index 111bd754..410a6521 100644 --- a/examples/optional_dependency_example/main.cpp +++ b/examples/optional_dependency_example/main.cpp @@ -2,6 +2,7 @@ #include "OptionalService.h" #include #include +#include // Some compile time logic to instantiate a regular cout logger or to use the spdlog logger, if Ichor has been compiled with it. #ifdef ICHOR_USE_SPDLOG diff --git a/examples/realtime_example/main.cpp b/examples/realtime_example/main.cpp index 9a0bc6b8..556b5ee5 100644 --- a/examples/realtime_example/main.cpp +++ b/examples/realtime_example/main.cpp @@ -2,6 +2,7 @@ #include "OptionalService.h" #include #include +#include #include "GlobalRealtimeSettings.h" #if defined(NDEBUG) diff --git a/examples/serializer_example/main.cpp b/examples/serializer_example/main.cpp index 1dfe4776..222378bc 100644 --- a/examples/serializer_example/main.cpp +++ b/examples/serializer_example/main.cpp @@ -3,6 +3,7 @@ #include #include #include +#include // Some compile time logic to instantiate a regular cout logger or to use the spdlog logger, if Ichor has been compiled with it. #ifdef ICHOR_USE_SPDLOG diff --git a/examples/tcp_example/main.cpp b/examples/tcp_example/main.cpp index a5d2c0c4..7f1f7f8f 100644 --- a/examples/tcp_example/main.cpp +++ b/examples/tcp_example/main.cpp @@ -3,6 +3,7 @@ #include #include #include +#include // Some compile time logic to instantiate a regular cout logger or to use the spdlog logger, if Ichor has been compiled with it. #ifdef ICHOR_USE_SPDLOG diff --git a/examples/timer_example/main.cpp b/examples/timer_example/main.cpp index 25173cee..f56b0ff8 100644 --- a/examples/timer_example/main.cpp +++ b/examples/timer_example/main.cpp @@ -1,5 +1,6 @@ #include "UsingTimerService.h" #include +#include // Some compile time logic to instantiate a regular cout logger or to use the spdlog logger, if Ichor has been compiled with it. #ifdef ICHOR_USE_SPDLOG diff --git a/examples/websocket_example/main.cpp b/examples/websocket_example/main.cpp index db18d567..994b2d48 100644 --- a/examples/websocket_example/main.cpp +++ b/examples/websocket_example/main.cpp @@ -8,6 +8,7 @@ #include #include #include +#include // Some compile time logic to instantiate a regular cout logger or to use the spdlog logger, if Ichor has been compiled with it. #ifdef ICHOR_USE_SPDLOG diff --git a/examples/yielding_timer_example/main.cpp b/examples/yielding_timer_example/main.cpp index 8f35f2c3..dae6ac40 100644 --- a/examples/yielding_timer_example/main.cpp +++ b/examples/yielding_timer_example/main.cpp @@ -1,5 +1,6 @@ #include "UsingTimerService.h" #include +#include // Some compile time logic to instantiate a regular cout logger or to use the spdlog logger, if Ichor has been compiled with it. #ifdef ICHOR_USE_SPDLOG diff --git a/include/ichor/ichor-mimalloc.h b/include/ichor/ichor-mimalloc.h new file mode 100644 index 00000000..9a2dc734 --- /dev/null +++ b/include/ichor/ichor-mimalloc.h @@ -0,0 +1,49 @@ +#pragma once + +#if defined(ICHOR_USE_SYSTEM_MIMALLOC) || defined(ICHOR_USE_MIMALLOC) +#include + +#if (!defined(WIN32) && !defined(_WIN32) && !defined(__WIN32)) || defined(__CYGWIN__) +extern "C" { + void *malloc(size_t size) { + return mi_malloc(size); + } + + void free(void *ptr) { + mi_free(ptr); + } + + void *realloc(void *ptr, size_t new_size) { + return mi_realloc(ptr, new_size); + } + + void *calloc(size_t num, size_t size) { + return mi_calloc(num, size); + } + + void *aligned_alloc(size_t alignment, size_t size) { + return mi_aligned_alloc(alignment, size); + } + + size_t malloc_usable_size(void *ptr) { + return mi_malloc_usable_size(ptr); + } + + void *memalign(size_t alignment, size_t size) { + return mi_memalign(alignment, size); + } + + int posix_memalign(void **memptr, size_t alignment, size_t size) { + return mi_posix_memalign(memptr, alignment, size); + } + + void *valloc(size_t size) { + return mi_valloc(size); + } + + void *pvalloc(size_t size) { + return mi_pvalloc(size); + } +} +#endif +#endif \ No newline at end of file diff --git a/src/ichor/DependencyManager.cpp b/src/ichor/DependencyManager.cpp index 1487047e..457c1e23 100644 --- a/src/ichor/DependencyManager.cpp +++ b/src/ichor/DependencyManager.cpp @@ -9,54 +9,6 @@ #include #include -#if defined(ICHOR_USE_SYSTEM_MIMALLOC) || defined(ICHOR_USE_MIMALLOC) -#include - -#if (!defined(WIN32) && !defined(_WIN32) && !defined(__WIN32)) || defined(__CYGWIN__) -extern "C" { - void *malloc(size_t size) { - return mi_malloc(size); - } - - void free(void *ptr) { - mi_free(ptr); - } - - void *realloc(void *ptr, size_t new_size) { - return mi_realloc(ptr, new_size); - } - - void *calloc(size_t num, size_t size) { - return mi_calloc(num, size); - } - - void *aligned_alloc(size_t alignment, size_t size) { - return mi_aligned_alloc(alignment, size); - } - - size_t malloc_usable_size(void *ptr) { - return mi_malloc_usable_size(ptr); - } - - void *memalign(size_t alignment, size_t size) { - return mi_memalign(alignment, size); - } - - int posix_memalign(void **memptr, size_t alignment, size_t size) { - return mi_posix_memalign(memptr, alignment, size); - } - - void *valloc(size_t size) { - return mi_valloc(size); - } - - void *pvalloc(size_t size) { - return mi_pvalloc(size); - } -} -#endif -#endif - #if (defined(WIN32) || defined(_WIN32) || defined(__WIN32)) && !defined(__CYGWIN__) #include #include diff --git a/test/Common.h b/test/Common.h index 49122fc8..72821154 100644 --- a/test/Common.h +++ b/test/Common.h @@ -5,6 +5,7 @@ #include #include #include +#include using namespace Ichor;