Skip to content

[libc][stdlib] Bring all GPU's alloc/free entrypoints under the same conditional. #96373

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

Merged
merged 1 commit into from
Jun 22, 2024

Conversation

lntue
Copy link
Contributor

@lntue lntue commented Jun 22, 2024

No description provided.

@lntue lntue requested a review from jhuber6 June 22, 2024 03:19
@llvmbot llvmbot added the libc label Jun 22, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 22, 2024

@llvm/pr-subscribers-libc

Author: None (lntue)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/96373.diff

1 Files Affected:

  • (modified) libc/src/stdlib/CMakeLists.txt (+104-104)
diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index a5d5ac5f32b06..61c05f0738959 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -317,97 +317,121 @@ add_entrypoint_object(
     libc.include.stdlib
 )
 
-if(LLVM_LIBC_INCLUDE_SCUDO)
-  set(SCUDO_DEPS "")
+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_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})
+
+    list(APPEND SCUDO_DEPS
+      RTGwpAsan.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
+      RTGwpAsanBacktraceLibc.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
+      RTGwpAsanSegvHandler.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
+      )
 
-  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_ARCHITECTURE_IS_RISCV64)
-    set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO riscv64)
-  elseif(LIBC_TARGET_ARCHITECTURE_IS_RISCV32)
-    set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO riscv32)
-  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}
+    )
+  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
+      COMPILE_OPTIONS
+        -DLIBC_FREELIST_MALLOC_SIZE=${LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE}
+    )
+    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()
 
-  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.")
+    add_entrypoint_external(
+      free
+    )
+    add_entrypoint_external(
+      calloc
+    )
+    add_entrypoint_external(
+      realloc
+    )
+    add_entrypoint_external(
+      aligned_alloc
+    )
   endif()
+endif()
 
-  list(APPEND SCUDO_DEPS RTScudoStandalone.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
-      RTScudoStandaloneCWrappers.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO})
+if(NOT LLVM_LIBC_FULL_BUILD)
+  return()
+endif()
 
-  list(APPEND SCUDO_DEPS
-    RTGwpAsan.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
-    RTGwpAsanBacktraceLibc.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
-    RTGwpAsanSegvHandler.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
-    )
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+  add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+endif()
 
-  add_entrypoint_external(
+if(LIBC_TARGET_OS_IS_GPU)
+  add_entrypoint_object(
     malloc
+    ALIAS
     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}
-  )
-elseif(LIBC_TARGET_OS_IS_GPU)
-  add_entrypoint_external(
-    calloc
-  )
-  add_entrypoint_external(
-    realloc
-  )
-  add_entrypoint_external(
-    aligned_alloc
+      .${LIBC_TARGET_OS}.malloc
   )
-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
-    COMPILE_OPTIONS
-      -DLIBC_FREELIST_MALLOC_SIZE=${LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE}
-  )
-  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
+    ALIAS
+    DEPENDS
+      .${LIBC_TARGET_OS}.free
   )
   add_entrypoint_external(
     calloc
@@ -420,14 +444,6 @@ else()
   )
 endif()
 
-if(NOT LLVM_LIBC_FULL_BUILD)
-  return()
-endif()
-
-if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
-  add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
-endif()
-
 add_entrypoint_object(
   _Exit
   SRCS
@@ -499,19 +515,3 @@ add_entrypoint_object(
   DEPENDS
     .${LIBC_TARGET_OS}.abort
 )
-
-if(LIBC_TARGET_OS_IS_GPU)
-  add_entrypoint_object(
-    malloc
-    ALIAS
-    DEPENDS
-      .${LIBC_TARGET_OS}.malloc
-  )
-
-  add_entrypoint_object(
-    free
-    ALIAS
-    DEPENDS
-      .${LIBC_TARGET_OS}.free
-  )
-endif()

Copy link
Contributor

@jhuber6 jhuber6 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably do something similar here to what we do for all the printf's in stdio, might make sense in the future.

@lntue lntue merged commit c2735d2 into llvm:main Jun 22, 2024
6 of 7 checks passed
@lntue lntue deleted the gpu branch June 22, 2024 03:29
AlexisPerry pushed a commit to llvm-project-tlp/llvm-project that referenced this pull request Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants