Skip to content

[libc][NFC] modularize malloc #119259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions libc/cmake/modules/LLVMLibCTargetNameUtils.cmake
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
105 changes: 1 addition & 104 deletions libc/src/stdlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
34 changes: 34 additions & 0 deletions libc/src/stdlib/allocator/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
15 changes: 15 additions & 0 deletions libc/src/stdlib/allocator/external/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
)
32 changes: 32 additions & 0 deletions libc/src/stdlib/allocator/freelist/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
)
66 changes: 66 additions & 0 deletions libc/src/stdlib/allocator/scudo/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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}
)
Loading