[PATCH] D101427: Linux support for mimalloc as a custom allocator

Michael via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 28 00:34:54 PDT 2021


michaeljclark created this revision.
michaeljclark added a reviewer: aganea.
Herald added subscribers: hiraditya, mgorny.
michaeljclark requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Adds support for mimalloc with static linking on Linux, for testing purposes, the following combination works:

-` -DLLVM_INTEGRATED_CRT_ALLOC=mimalloc` and `-DLLVM_LINK_LLVM_DYLIB=OFF`

I have been working on testing rpmalloc and mimalloc as custom allocators with clang on Linux. Neither of them works out of the box with dynamic linking due to a hang bug in glibc 2.31 (Ubuntu 20.04LTS) elf/rtld.c:_dl_start_final which I have not yet debugged fully. As a result this patch just adds support for mimalloc with static linking (LLVM_LINK_LLVM_DYLIB=OFF) which was the only configuration I could get working.

Most of the patch is implementing CMake constraints conditions similar to the Windows support so that CMake will report errors for combinations that do not work, so it checks LLVM_LINK_LLVM_DYLIB and only allows mimalloc on Linux. The patch expects mimalloc to be checked out next to the LLVM directory same as the Windows support. Unlike the windows patch, mimalloc is added as a CMake subdirectory and mimalloc-static is properly exported into LLVM's CMake.

It adds the following error messages:

- LLVM_INTEGRATED_CRT_ALLOC is only supported on Windows and Linux.
- LLVM_INTEGRATED_CRT_ALLOC currently only supports mimalloc on Linux!"
- LLVM_INTEGRATED_CRT_ALLOC cannot be used with LLVM_LINK_LLVM_DYLIB on Linux!"


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D101427

Files:
  llvm/CMakeLists.txt
  llvm/lib/Support/CMakeLists.txt


Index: llvm/lib/Support/CMakeLists.txt
===================================================================
--- llvm/lib/Support/CMakeLists.txt
+++ llvm/lib/Support/CMakeLists.txt
@@ -72,11 +72,18 @@
     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(MIMALLOC_LIB "${LLVM_INTEGRATED_CRT_ALLOC}/out/msvc-x64/Release/mimalloc-static.lib")
-    if(NOT EXISTS "${MIMALLOC_LIB}")
-	  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")
+    if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
+      set(MI_OVERRIDE ON CACHE BOOL "Override the standard malloc")
+      add_subdirectory(${LLVM_INTEGRATED_CRT_ALLOC} mimalloc)
+      export(EXPORT mimalloc)
+      set(system_libs ${system_libs} "mimalloc-static")
+    else()
+      set(MIMALLOC_LIB "${LLVM_INTEGRATED_CRT_ALLOC}/out/msvc-x64/Release/mimalloc-static.lib")
+      if(NOT EXISTS "${MIMALLOC_LIB}")
+         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")
+      endif()
+      set(system_libs ${system_libs} "${MIMALLOC_LIB}" "-INCLUDE:malloc")
     endif()
-    set(system_libs ${system_libs} "${MIMALLOC_LIB}" "-INCLUDE:malloc")
   endif()
 endif()
 
Index: llvm/CMakeLists.txt
===================================================================
--- llvm/CMakeLists.txt
+++ llvm/CMakeLists.txt
@@ -520,8 +520,14 @@
 
 set(LLVM_INTEGRATED_CRT_ALLOC "" CACHE PATH "Replace the Windows CRT allocator with any of {rpmalloc|mimalloc|snmalloc}. Only works with /MT enabled.")
 if(LLVM_INTEGRATED_CRT_ALLOC)
-  if(NOT WIN32)
-    message(FATAL_ERROR "LLVM_INTEGRATED_CRT_ALLOC is only supported on Windows.")
+  if(NOT (CMAKE_SYSTEM_NAME MATCHES "(Linux|Windows)"))
+    message(FATAL_ERROR "LLVM_INTEGRATED_CRT_ALLOC is only supported on Windows and Linux.")
+  elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
+    if(NOT (LLVM_INTEGRATED_CRT_ALLOC MATCHES "mimalloc$"))
+      message(FATAL_ERROR "LLVM_INTEGRATED_CRT_ALLOC currently only supports mimalloc on Linux!")
+    elseif(LLVM_BUILD_LLVM_DYLIB AND LLVM_LINK_LLVM_DYLIB)
+      message(FATAL_ERROR "LLVM_INTEGRATED_CRT_ALLOC cannot be used with LLVM_LINK_LLVM_DYLIB on Linux!")
+    endif()
   endif()
   if(LLVM_USE_SANITIZER)
     message(FATAL_ERROR "LLVM_INTEGRATED_CRT_ALLOC cannot be used along with LLVM_USE_SANITIZER!")


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D101427.341081.patch
Type: text/x-patch
Size: 2841 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210428/6474a953/attachment.bin>


More information about the llvm-commits mailing list