diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt --- a/llvm/CMakeLists.txt +++ b/llvm/CMakeLists.txt @@ -570,6 +570,16 @@ option (LLVM_VERSION_PRINTER_SHOW_HOST_TARGET_INFO "Show target and host info when tools are invoked with --version." ON) +option(LLVM_INTEGRATED_CRT_ALLOC "Replace the Windows CRT allocator with any of {rpmalloc|mimalloc|snmalloc}. Only works with /MT enabled." OFF) +if(LLVM_INTEGRATED_CRT_ALLOC) + if (CMAKE_BUILD_TYPE AND NOT ${LLVM_USE_CRT_${uppercase_CMAKE_BUILD_TYPE}} MATCHES "^(MT|MTd)$") + message(FATAL_ERROR "LLVM_INTEGRATED_CRT_ALLOC only works with /MT or /MTd. Use LLVM_USE_CRT_${uppercase_CMAKE_BUILD_TYPE} to set the appropriate option.") + endif() + if (LLVM_USE_SANITIZER) + message(FATAL_ERROR "LLVM_INTEGRATED_CRT_ALLOC cannot be used along with LLVM_USE_SANITIZER!") + endif() +endif() + # You can configure which libraries from LLVM you want to include in the # shared library by setting LLVM_DYLIB_COMPONENTS to a semi-colon delimited # list of LLVM components. All component names handled by llvm-config are valid. diff --git a/llvm/lib/Support/CMakeLists.txt b/llvm/lib/Support/CMakeLists.txt --- a/llvm/lib/Support/CMakeLists.txt +++ b/llvm/lib/Support/CMakeLists.txt @@ -51,6 +51,17 @@ set(Z3_LINK_FILES "") endif() +string(REGEX REPLACE "/$" "" LLVM_INTEGRATED_CRT_ALLOC "${LLVM_INTEGRATED_CRT_ALLOC}") + +if(LLVM_INTEGRATED_CRT_ALLOC MATCHES "rpmalloc$") + set(ALLOCATOR_FILES "${LLVM_INTEGRATED_CRT_ALLOC}/rpmalloc/rpmalloc.c" "${LLVM_INTEGRATED_CRT_ALLOC}/rpmalloc/malloc.c") +elseif(LLVM_INTEGRATED_CRT_ALLOC MATCHES "snmalloc$") + set(ALLOCATOR_FILES "${LLVM_INTEGRATED_CRT_ALLOC}/src/override/malloc.cc" "${LLVM_INTEGRATED_CRT_ALLOC}/src/override/new.cc") + set(system_libs ${system_libs} "mincore.lib" "-INCLUDE:malloc") +elseif(LLVM_INTEGRATED_CRT_ALLOC MATCHES "mimalloc$") + set(system_libs ${system_libs} "${LLVM_INTEGRATED_CRT_ALLOC}/out/msvc-x64/Release/mimalloc-static.lib" "-INCLUDE:malloc") +endif() + add_llvm_component_library(LLVMSupport AArch64TargetParser.cpp ABIBreak.cpp @@ -171,6 +182,8 @@ xxhash.cpp Z3Solver.cpp + ${ALLOCATOR_FILES} + # System Atomic.cpp DynamicLibrary.cpp diff --git a/llvm/tools/llvm-shlib/CMakeLists.txt b/llvm/tools/llvm-shlib/CMakeLists.txt --- a/llvm/tools/llvm-shlib/CMakeLists.txt +++ b/llvm/tools/llvm-shlib/CMakeLists.txt @@ -172,4 +172,9 @@ # Finally link the target. add_llvm_library(LLVM-C SHARED INSTALL_WITH_TOOLCHAIN ${SOURCES} DEPENDS intrinsics_gen) + if (LLVM_INTEGRATED_CRT_ALLOC AND MSVC) + # Make sure we search LLVMSupport first, before the CRT libs + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -INCLUDE:malloc") + endif() + endif() diff --git a/llvm/tools/remarks-shlib/CMakeLists.txt b/llvm/tools/remarks-shlib/CMakeLists.txt --- a/llvm/tools/remarks-shlib/CMakeLists.txt +++ b/llvm/tools/remarks-shlib/CMakeLists.txt @@ -10,6 +10,11 @@ add_llvm_library(Remarks SHARED INSTALL_WITH_TOOLCHAIN ${SOURCES}) +if (LLVM_INTEGRATED_CRT_ALLOC AND MSVC) + # Make sure we search LLVMSupport first, before the CRT libs + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -INCLUDE:malloc") +endif() + install(FILES ${LLVM_MAIN_INCLUDE_DIR}/llvm-c/Remarks.h DESTINATION include/llvm-c COMPONENT Remarks) diff --git a/llvm/unittests/Support/DynamicLibrary/CMakeLists.txt b/llvm/unittests/Support/DynamicLibrary/CMakeLists.txt --- a/llvm/unittests/Support/DynamicLibrary/CMakeLists.txt +++ b/llvm/unittests/Support/DynamicLibrary/CMakeLists.txt @@ -38,6 +38,15 @@ ) add_dependencies(DynamicLibraryTests ${NAME}) + + # We need to link in the Support lib for the Memory allocator override, + # otherwise the DynamicLibrary.Shutdown test will fail, because it would + # allocate memory with the CRT allocator, and release it with our custom + # allocator (see llvm/lib/Support/Windows/Memory.inc). + # /INCLUDE:malloc is there to force searching into LLVMSupport before libucrt + llvm_map_components_to_libnames(llvm_libs Support) + target_link_libraries(${NAME} ${llvm_libs} "-INCLUDE:malloc") + endfunction(dynlib_add_module) # Revert -Wl,-z,nodelete on this test since it relies on the file