Skip to content

Commit a6a37a2

Browse files
committed
[Support] On Windows, add optional support for {rpmalloc|snmalloc|mimalloc}
This patch optionally replaces the CRT allocator (i.e., malloc and free) with rpmalloc (mixed public domain licence/MIT licence) or snmalloc (MIT licence) or mimalloc (MIT licence). Please note that the source code for these allocators must be available outside of LLVM's tree. To enable, use `cmake ... -DLLVM_INTEGRATED_CRT_ALLOC=D:/git/rpmalloc -DLLVM_USE_CRT_RELEASE=MT` where `D:/git/rpmalloc` has already been git clone'd from `https://github.com/mjansson/rpmalloc`. The same applies to snmalloc and mimalloc. When enabled, the allocator will be embeded (statically linked) into the LLVM tools & libraries. This currently only works with the static CRT (/MT), although using the dynamic CRT (/MD) could potentially work as well in the future. When enabled, this changes the memory stack from: new/delete -> MS VC++ CRT malloc/free -> HeapAlloc -> VirtualAlloc to: new/delete -> {rpmalloc|snmalloc|mimalloc} -> VirtualAlloc The goal of this patch is to bypass the application's global heap - which is thread-safe thus inducing locking - and instead take advantage of a modern lock-free, thread cache, allocator. On a 6-core Xeon Skylake we observe a 2.5x decrease in execution time when linking a large scale application with LLD and ThinLTO (12 min 20 sec -> 5 min 34 sec), when all hardware threads are being used (using LLD's flag /opt:lldltojobs=all). On a dual 36-core Xeon Skylake with all hardware threads used, we observe a 24x decrease in execution time (1 h 2 min -> 2 min 38 sec) when linking a large application with LLD and ThinLTO. Clang build times also see a decrease in the range 5-10% depending on the configuration. Differential Revision: https://reviews.llvm.org/D71786
1 parent 6923b0a commit a6a37a2

File tree

6 files changed

+79
-0
lines changed

6 files changed

+79
-0
lines changed

llvm/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,19 @@ option (LLVM_BUILD_EXTERNAL_COMPILER_RT
567567
option (LLVM_VERSION_PRINTER_SHOW_HOST_TARGET_INFO
568568
"Show target and host info when tools are invoked with --version." ON)
569569

570+
option(LLVM_INTEGRATED_CRT_ALLOC "Replace the Windows CRT allocator with any of {rpmalloc|mimalloc|snmalloc}. Only works with /MT enabled." OFF)
571+
if(LLVM_INTEGRATED_CRT_ALLOC)
572+
if(NOT WIN32)
573+
message(FATAL_ERROR "LLVM_INTEGRATED_CRT_ALLOC is only supported on Windows.")
574+
endif()
575+
if(LLVM_USE_SANITIZER)
576+
message(FATAL_ERROR "LLVM_INTEGRATED_CRT_ALLOC cannot be used along with LLVM_USE_SANITIZER!")
577+
endif()
578+
if(CMAKE_BUILD_TYPE AND uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
579+
message(FATAL_ERROR "The Debug target isn't supported along with LLVM_INTEGRATED_CRT_ALLOC!")
580+
endif()
581+
endif()
582+
570583
# You can configure which libraries from LLVM you want to include in the
571584
# shared library by setting LLVM_DYLIB_COMPONENTS to a semi-colon delimited
572585
# list of LLVM components. All component names handled by llvm-config are valid.

llvm/docs/CMake.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,23 @@ LLVM-specific variables
461461
**LLVM_PARALLEL_LINK_JOBS**:STRING
462462
Define the maximum number of concurrent link jobs.
463463

464+
**LLVM_USE_CRT_{target}**:STRING
465+
On Windows, tells which version of the C runtime library (CRT) should be used.
466+
For example, -DLLVM_USE_CRT_RELEASE=MT would statically link the CRT into the
467+
LLVM tools and library.
468+
469+
**LLVM_INTEGRATED_CRT_ALLOC**:PATH
470+
On Windows, allows embedding a different C runtime allocator into the LLVM
471+
tools and libraries. Using a lock-free allocator such as the ones listed below
472+
greatly decreases ThinLTO link time by about an order of magnitude. It also
473+
midly improves Clang build times, by about 5-10%. At the moment, rpmalloc,
474+
snmalloc and mimalloc are supported. Use the path to `git clone` to select
475+
the respective allocator, for example:
476+
D:\git> git clone https://github.com/mjansson/rpmalloc
477+
D:\llvm-project> cmake ... -DLLVM_INTEGRATED_CRT_ALLOC=D:\git\rpmalloc
478+
This flag needs to be used along with the static CRT, ie. if building the
479+
Release target, add -DLLVM_USE_CRT_RELEASE=MT.
480+
464481
**LLVM_BUILD_DOCS**:BOOL
465482
Adds all *enabled* documentation targets (i.e. Doxgyen and Sphinx targets) as
466483
dependencies of the default build targets. This results in all of the (enabled)

llvm/lib/Support/CMakeLists.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,34 @@ if(LLVM_WITH_Z3)
6060
set(system_libs ${system_libs} ${Z3_LIBRARIES})
6161
endif()
6262

63+
# Override the C runtime allocator on Windows and embed it into LLVM tools & libraries
64+
if(LLVM_INTEGRATED_CRT_ALLOC)
65+
if (CMAKE_BUILD_TYPE AND NOT ${LLVM_USE_CRT_${uppercase_CMAKE_BUILD_TYPE}} MATCHES "^(MT|MTd)$")
66+
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.")
67+
endif()
68+
69+
string(REGEX REPLACE "(/|\\\\)$" "" LLVM_INTEGRATED_CRT_ALLOC "${LLVM_INTEGRATED_CRT_ALLOC}")
70+
71+
if(NOT EXISTS "${LLVM_INTEGRATED_CRT_ALLOC}")
72+
message(FATAL_ERROR "Cannot find the path to `git clone` for the CRT allocator! (${LLVM_INTEGRATED_CRT_ALLOC}). Currently, rpmalloc, snmalloc and mimalloc are supported.")
73+
endif()
74+
75+
if(LLVM_INTEGRATED_CRT_ALLOC MATCHES "rpmalloc$")
76+
add_definitions(-DENABLE_OVERRIDE -DENABLE_PRELOAD)
77+
set(ALLOCATOR_FILES "${LLVM_INTEGRATED_CRT_ALLOC}/rpmalloc/rpmalloc.c")
78+
elseif(LLVM_INTEGRATED_CRT_ALLOC MATCHES "snmalloc$")
79+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++17" PARENT_SCOPE)
80+
set(ALLOCATOR_FILES "${LLVM_INTEGRATED_CRT_ALLOC}/src/override/malloc.cc" "${LLVM_INTEGRATED_CRT_ALLOC}/src/override/new.cc")
81+
set(system_libs ${system_libs} "mincore.lib" "-INCLUDE:malloc")
82+
elseif(LLVM_INTEGRATED_CRT_ALLOC MATCHES "mimalloc$")
83+
set(MIMALLOC_LIB "${LLVM_INTEGRATED_CRT_ALLOC}/out/msvc-x64/Release/mimalloc-static.lib")
84+
if(NOT EXISTS "${MIMALLOC_LIB}")
85+
message(FATAL_ERROR "Cannot find the mimalloc static library. To build it, first apply the patch from https://github.com/microsoft/mimalloc/issues/268 then build the Release x64 target through ${LLVM_INTEGRATED_CRT_ALLOC}\\ide\\vs2019\\mimalloc.sln")
86+
endif()
87+
set(system_libs ${system_libs} "${MIMALLOC_LIB}" "-INCLUDE:malloc")
88+
endif()
89+
endif()
90+
6391
add_llvm_component_library(LLVMSupport
6492
AArch64TargetParser.cpp
6593
ABIBreak.cpp
@@ -181,6 +209,8 @@ add_llvm_component_library(LLVMSupport
181209
xxhash.cpp
182210
Z3Solver.cpp
183211

212+
${ALLOCATOR_FILES}
213+
184214
# System
185215
Atomic.cpp
186216
DynamicLibrary.cpp

llvm/tools/llvm-shlib/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,9 @@ if(LLVM_BUILD_LLVM_C_DYLIB AND MSVC)
176176
# Finally link the target.
177177
add_llvm_library(LLVM-C SHARED INSTALL_WITH_TOOLCHAIN ${SOURCES} DEPENDS intrinsics_gen)
178178

179+
if (LLVM_INTEGRATED_CRT_ALLOC AND MSVC)
180+
# Make sure we search LLVMSupport first, before the CRT libs
181+
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -INCLUDE:malloc")
182+
endif()
183+
179184
endif()

llvm/tools/remarks-shlib/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_CURRENT_SOURCE_DIR}/Remarks.exports)
1010

1111
add_llvm_library(Remarks SHARED INSTALL_WITH_TOOLCHAIN ${SOURCES})
1212

13+
if (LLVM_INTEGRATED_CRT_ALLOC AND MSVC)
14+
# Make sure we search LLVMSupport first, before the CRT libs
15+
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -INCLUDE:malloc")
16+
endif()
17+
1318
install(FILES ${LLVM_MAIN_INCLUDE_DIR}/llvm-c/Remarks.h
1419
DESTINATION include/llvm-c
1520
COMPONENT Remarks)

llvm/unittests/Support/DynamicLibrary/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ function(dynlib_add_module NAME)
3838
)
3939

4040
add_dependencies(DynamicLibraryTests ${NAME})
41+
42+
# We need to link in the Support lib for the Memory allocator override,
43+
# otherwise the DynamicLibrary.Shutdown test will fail, because it would
44+
# allocate memory with the CRT allocator, and release it with our custom
45+
# allocator (see llvm/lib/Support/Windows/Memory.inc).
46+
# /INCLUDE:malloc is there to force searching into LLVMSupport before libucrt
47+
llvm_map_components_to_libnames(llvm_libs Support)
48+
target_link_libraries(${NAME} ${llvm_libs} "-INCLUDE:malloc")
49+
4150
endfunction(dynlib_add_module)
4251

4352
# Revert -Wl,-z,nodelete on this test since it relies on the file

0 commit comments

Comments
 (0)