[libc-commits] [libc] [libc][NFC] modularize malloc (PR #119259)
Schrodinger ZHU Yifan via libc-commits
libc-commits at lists.llvm.org
Mon Dec 9 12:07:19 PST 2024
https://github.com/SchrodingerZhu created https://github.com/llvm/llvm-project/pull/119259
None
>From bfeb474a359cee34994966290a7a736b013588eb Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Mon, 9 Dec 2024 15:05:29 -0500
Subject: [PATCH] [libc][NFC] modularize malloc
---
.../modules/LLVMLibCTargetNameUtils.cmake | 6 +
libc/src/stdlib/CMakeLists.txt | 105 +-----------------
libc/src/stdlib/allocator/CMakeLists.txt | 34 ++++++
.../stdlib/allocator/external/CMakeLists.txt | 15 +++
.../stdlib/allocator/freelist/CMakeLists.txt | 32 ++++++
.../freelist}/freelist_malloc.cpp | 0
.../src/stdlib/allocator/scudo/CMakeLists.txt | 66 +++++++++++
7 files changed, 154 insertions(+), 104 deletions(-)
create mode 100644 libc/src/stdlib/allocator/CMakeLists.txt
create mode 100644 libc/src/stdlib/allocator/external/CMakeLists.txt
create mode 100644 libc/src/stdlib/allocator/freelist/CMakeLists.txt
rename libc/src/stdlib/{ => allocator/freelist}/freelist_malloc.cpp (100%)
create mode 100644 libc/src/stdlib/allocator/scudo/CMakeLists.txt
diff --git a/libc/cmake/modules/LLVMLibCTargetNameUtils.cmake b/libc/cmake/modules/LLVMLibCTargetNameUtils.cmake
index 4fbc06b4e85842..5fa6761d31a7c4 100644
--- a/libc/cmake/modules/LLVMLibCTargetNameUtils.cmake
+++ b/libc/cmake/modules/LLVMLibCTargetNameUtils.cmake
@@ -1,4 +1,10 @@
function(get_fq_target_name local_name target_name_var)
+ # this is the absolute path to the target
+ if (local_name MATCHES "^@.+")
+ string (SUBSTRING ${local_name} 1 -1 local_name)
+ set(${target_name_var} ${local_name} PARENT_SCOPE)
+ return()
+ endif()
file(RELATIVE_PATH rel_path ${LIBC_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
string(REPLACE "/" "." fq_name "libc.${rel_path}.${local_name}")
set(${target_name_var} ${fq_name} PARENT_SCOPE)
diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index 14d06534a6049a..a8c59bda4e2007 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -323,110 +323,7 @@ add_entrypoint_object(
.rand_util
)
-if(NOT LIBC_TARGET_OS_IS_GPU)
- if(LLVM_LIBC_INCLUDE_SCUDO)
- set(SCUDO_DEPS "")
-
- include(${LIBC_SOURCE_DIR}/../compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake)
-
- # scudo distinguishes riscv32 and riscv64, so we need to translate the architecture
- # set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO ${LIBC_TARGET_ARCHITECTURE})
- if (LIBC_TARGET_OS_IS_DARWIN AND (LIBC_TARGET_ARCHITECTURE STREQUAL "arm"))
- set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO arm64)
- else()
- set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO ${LIBC_TARGET_ARCHITECTURE})
- endif()
- if(LIBC_TARGET_ARCHITECTURE_IS_RISCV64)
- set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO riscv64)
- elseif(LIBC_TARGET_ARCHITECTURE_IS_RISCV32)
- set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO riscv32)
- endif()
-
- if(NOT (LIBC_TARGET_ARCHITECTURE_FOR_SCUDO IN_LIST ALL_SCUDO_STANDALONE_SUPPORTED_ARCH))
- message(FATAL_ERROR "Architecture ${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO} is not supported by SCUDO.
- Either disable LLVM_LIBC_INCLUDE_SCUDO or change your target architecture.")
- endif()
-
- list(APPEND SCUDO_DEPS RTScudoStandalone.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
- RTScudoStandaloneCWrappers.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO})
-
- if (COMPILER_RT_BUILD_GWP_ASAN)
- list(APPEND SCUDO_DEPS
- RTGwpAsan.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
- RTGwpAsanBacktraceLibc.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
- RTGwpAsanSegvHandler.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
- )
- endif()
-
- add_entrypoint_external(
- malloc
- DEPENDS
- ${SCUDO_DEPS}
- )
- add_entrypoint_external(
- calloc
- DEPENDS
- ${SCUDO_DEPS}
- )
- add_entrypoint_external(
- realloc
- DEPENDS
- ${SCUDO_DEPS}
- )
- add_entrypoint_external(
- aligned_alloc
- DEPENDS
- ${SCUDO_DEPS}
- )
- add_entrypoint_external(
- free
- DEPENDS
- ${SCUDO_DEPS}
- )
- add_entrypoint_external(
- mallopt
- DEPENDS
- ${SCUDO_DEPS}
- )
- else()
- # Only use freelist malloc for baremetal targets.
- add_entrypoint_object(
- freelist_malloc
- SRCS
- freelist_malloc.cpp
- HDRS
- malloc.h
- DEPENDS
- libc.src.__support.freelist_heap
- )
- get_target_property(freelist_malloc_is_skipped libc.src.stdlib.freelist_malloc "SKIPPED")
- if(LIBC_TARGET_OS_IS_BAREMETAL AND NOT freelist_malloc_is_skipped)
- add_entrypoint_object(
- malloc
- ALIAS
- DEPENDS
- .freelist_malloc
- )
- else()
- add_entrypoint_external(
- malloc
- )
- endif()
-
- add_entrypoint_external(
- free
- )
- add_entrypoint_external(
- calloc
- )
- add_entrypoint_external(
- realloc
- )
- add_entrypoint_external(
- aligned_alloc
- )
- endif()
-endif()
+add_subdirectory(allocator)
if(NOT LLVM_LIBC_FULL_BUILD)
return()
diff --git a/libc/src/stdlib/allocator/CMakeLists.txt b/libc/src/stdlib/allocator/CMakeLists.txt
new file mode 100644
index 00000000000000..d0569b6377a961
--- /dev/null
+++ b/libc/src/stdlib/allocator/CMakeLists.txt
@@ -0,0 +1,34 @@
+if(LIBC_TARGET_OS_IS_GPU)
+ message(STATUS "Skipping allocators for GPU targets.")
+ return()
+endif()
+
+# TODO: we should really use option to dispatch the allocator. Currently, there are
+# other behaviors control the allocator selection. We simulate the existing behavior
+# prior to the modularization of allocator but this should be cleaned up.
+
+if(LLVM_LIBC_INCLUDE_SCUDO)
+ set(DEFAULT_ALLOCATOR_VARIANT "scudo")
+elseif(LIBC_TARGET_OS_IS_BAREMETAL)
+ set(DEFAULT_ALLOCATOR_VARIANT "freelist")
+else()
+ set(DEFAULT_ALLOCATOR_VARIANT "external")
+endif()
+
+set(LIBC_ALLOCATOR_VARIANT ${DEFAULT_ALLOCATOR_VARIANT} CACHE STRING
+ "The allocator variant to use. Options are: scudo, freelist, external.")
+
+message(STATUS "Using ${LIBC_ALLOCATOR_VARIANT} allocator.")
+
+if (LIBC_ALLOCATOR_VARIANT STREQUAL "scudo")
+ if (NOT LLVM_LIBC_INCLUDE_SCUDO)
+ message(FATAL_ERROR "SCUDO allocator is not enabled.")
+ endif()
+ add_subdirectory(scudo)
+elseif (LIBC_ALLOCATOR_VARIANT STREQUAL "freelist")
+ add_subdirectory(freelist)
+elseif (LIBC_ALLOCATOR_VARIANT STREQUAL "external")
+ add_subdirectory(external)
+else()
+ message(FATAL_ERROR "Unknown allocator variant: ${LIBC_ALLOCATOR_VARIANT}")
+endif()
diff --git a/libc/src/stdlib/allocator/external/CMakeLists.txt b/libc/src/stdlib/allocator/external/CMakeLists.txt
new file mode 100644
index 00000000000000..e3bf2d88ddbe39
--- /dev/null
+++ b/libc/src/stdlib/allocator/external/CMakeLists.txt
@@ -0,0 +1,15 @@
+add_entrypoint_external(
+ @libc.src.stdlib.malloc
+)
+add_entrypoint_external(
+ @libc.src.stdlib.free
+)
+add_entrypoint_external(
+ @libc.src.stdlib.calloc
+)
+add_entrypoint_external(
+ @libc.src.stdlib.realloc
+)
+add_entrypoint_external(
+ @libc.src.stdlib.aligned_alloc
+)
diff --git a/libc/src/stdlib/allocator/freelist/CMakeLists.txt b/libc/src/stdlib/allocator/freelist/CMakeLists.txt
new file mode 100644
index 00000000000000..87daba35ed7181
--- /dev/null
+++ b/libc/src/stdlib/allocator/freelist/CMakeLists.txt
@@ -0,0 +1,32 @@
+# Only use freelist malloc for baremetal targets.
+add_entrypoint_object(
+ @libc.src.stdlib.freelist_malloc
+ SRCS
+ freelist_malloc.cpp
+ HDRS
+ malloc.h
+ DEPENDS
+ libc.src.__support.freelist_heap
+)
+
+get_target_property(freelist_malloc_is_skipped libc.src.stdlib.freelist_malloc "SKIPPED")
+
+add_entrypoint_object(
+ @libc.src.stdlib.malloc
+ ALIAS
+ DEPENDS
+ .freelist_malloc
+)
+
+add_entrypoint_external(
+ @libc.src.stdlib.free
+)
+add_entrypoint_external(
+ @libc.src.stdlib.calloc
+)
+add_entrypoint_external(
+ @libc.src.stdlib.realloc
+)
+add_entrypoint_external(
+ @libc.src.stdlib.aligned_alloc
+)
diff --git a/libc/src/stdlib/freelist_malloc.cpp b/libc/src/stdlib/allocator/freelist/freelist_malloc.cpp
similarity index 100%
rename from libc/src/stdlib/freelist_malloc.cpp
rename to libc/src/stdlib/allocator/freelist/freelist_malloc.cpp
diff --git a/libc/src/stdlib/allocator/scudo/CMakeLists.txt b/libc/src/stdlib/allocator/scudo/CMakeLists.txt
new file mode 100644
index 00000000000000..2e0dd1ccdef075
--- /dev/null
+++ b/libc/src/stdlib/allocator/scudo/CMakeLists.txt
@@ -0,0 +1,66 @@
+set(SCUDO_DEPS "")
+
+include(${LIBC_SOURCE_DIR}/../compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake)
+
+# scudo distinguishes riscv32 and riscv64, so we need to translate the architecture
+# set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO ${LIBC_TARGET_ARCHITECTURE})
+if (LIBC_TARGET_OS_IS_DARWIN AND (LIBC_TARGET_ARCHITECTURE STREQUAL "arm"))
+ set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO arm64)
+else()
+ set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO ${LIBC_TARGET_ARCHITECTURE})
+endif()
+ if(LIBC_TARGET_ARCHITECTURE_IS_RISCV64)
+ set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO riscv64)
+ elseif(LIBC_TARGET_ARCHITECTURE_IS_RISCV32)
+ set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO riscv32)
+endif()
+
+if(NOT (LIBC_TARGET_ARCHITECTURE_FOR_SCUDO IN_LIST ALL_SCUDO_STANDALONE_SUPPORTED_ARCH))
+ message(FATAL_ERROR "Architecture ${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO} is not supported by SCUDO.
+ Either disable LLVM_LIBC_INCLUDE_SCUDO or change your target architecture.")
+endif()
+
+list(APPEND SCUDO_DEPS RTScudoStandalone.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
+ RTScudoStandaloneCWrappers.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO})
+
+if (COMPILER_RT_BUILD_GWP_ASAN)
+ list(APPEND SCUDO_DEPS
+ RTGwpAsan.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
+ RTGwpAsanBacktraceLibc.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
+ RTGwpAsanSegvHandler.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
+ )
+endif()
+
+add_entrypoint_external(
+ @libc.src.stdlib.malloc
+ DEPENDS
+ ${SCUDO_DEPS}
+)
+
+add_entrypoint_external(
+ @libc.src.stdlib.calloc
+ DEPENDS
+ ${SCUDO_DEPS}
+)
+
+add_entrypoint_external(
+ @libc.src.stdlib.realloc
+ DEPENDS
+ ${SCUDO_DEPS}
+)
+
+add_entrypoint_external(
+ @libc.src.stdlib.aligned_alloc
+ DEPENDS
+ ${SCUDO_DEPS}
+)
+add_entrypoint_external(
+ @libc.src.stdlib.free
+ DEPENDS
+ ${SCUDO_DEPS}
+)
+add_entrypoint_external(
+ @libc.src.stdlib.mallopt
+ DEPENDS
+ ${SCUDO_DEPS}
+)
More information about the libc-commits
mailing list