[libc-commits] [libc] [libc] Fix building bitcode library for GPU (PR #100491)

Joseph Huber via libc-commits libc-commits at lists.llvm.org
Thu Jul 25 08:43:34 PDT 2024


https://github.com/jhuber6 updated https://github.com/llvm/llvm-project/pull/100491

>From 06f7c13788455c44aa8805ef3ab00e53df58b2d9 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Wed, 24 Jul 2024 20:51:40 -0500
Subject: [PATCH] [libc] Fix building bitcode library for GPU

Summary:
The GPU build provides a bitcode version of the library to mimic how
NVIDIA and AMD provide their libraries. Previously we had the fatbinary
archive that created the necessary dependency for this to actually
build. Since it was removed we no longer had anything triggering this to
build.

I have no idea if there's a better way to force a custom command to
actually be run in the same space as libraries, but the only thing I
could come up with was a dummy target.
---
 libc/lib/CMakeLists.txt | 33 +++++++++++++++------------------
 libc/lib/stub.cpp       |  1 +
 2 files changed, 16 insertions(+), 18 deletions(-)
 create mode 100644 libc/lib/stub.cpp

diff --git a/libc/lib/CMakeLists.txt b/libc/lib/CMakeLists.txt
index 4b7cfc4b76e2e..6f7f01723947b 100644
--- a/libc/lib/CMakeLists.txt
+++ b/libc/lib/CMakeLists.txt
@@ -51,7 +51,13 @@ foreach(archive IN ZIP_LISTS
       PROPERTIES
         OUTPUT_NAME ${archive_1}.bc
     )
-    list(APPEND added_gpu_bitcode_targets ${archive_1}bitcode)
+    list(APPEND added_bitcode_targets ${archive_1}bitcode)
+
+    # A dummy library required make the custom command that creates the bitcode
+    # actually build so it can be installed.
+    add_library(${archive_1}_stub STATIC stub.cpp)
+    target_compile_options(${archive_1}_stub PRIVATE -ffreestanding -nogpulib -flto)
+    add_dependencies(${archive_1}_stub ${archive_1}bitcode)
   endif()
 endforeach()
 
@@ -61,24 +67,13 @@ install(
   COMPONENT libc
 )
 
-if(LIBC_TARGET_OS_IS_GPU)
-  set(gpu_install_dir lib${LLVM_LIBDIR_SUFFIX})
-  if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR)
-    set(gpu_install_dir lib${LLVM_LIBDIR_SUFFIX}/${LLVM_HOST_TRIPLE})
-  endif()
-  install(
-    TARGETS ${added_gpu_archive_targets}
-    ARCHIVE DESTINATION ${gpu_install_dir}
-    COMPONENT libc
+foreach(file ${added_bitcode_targets})
+  install(FILES $<TARGET_PROPERTY:${file},TARGET_OBJECT>
+          DESTINATION ${LIBC_INSTALL_LIBRARY_DIR}
+          RENAME $<TARGET_PROPERTY:${file},OUTPUT_NAME>
+          COMPONENT libc
   )
-  foreach(file ${added_gpu_bitcode_targets})
-    install(FILES $<TARGET_PROPERTY:${file},TARGET_OBJECT>
-            DESTINATION ${LIBC_INSTALL_LIBRARY_DIR}
-            RENAME $<TARGET_PROPERTY:${file},OUTPUT_NAME>
-            COMPONENT libc
-    )
-  endforeach()
-endif()
+endforeach()
 
 if(NOT LIBC_TARGET_OS_IS_BAREMETAL)
   # For now we will disable libc-startup installation for baremetal. The
@@ -93,6 +88,7 @@ endif()
 
 add_custom_target(install-libc
                   DEPENDS ${added_archive_targets}
+                          ${added_bitcode_targets}
                           ${startup_target}
                           ${header_install_target}
                   COMMAND "${CMAKE_COMMAND}"
@@ -100,6 +96,7 @@ add_custom_target(install-libc
                           -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
 add_custom_target(install-libc-stripped
                   DEPENDS ${added_archive_targets}
+                          ${added_bitcode_targets}
                           ${startup_target}
                           ${header_install_target}
                   COMMAND "${CMAKE_COMMAND}"
diff --git a/libc/lib/stub.cpp b/libc/lib/stub.cpp
new file mode 100644
index 0000000000000..800d5e2078b36
--- /dev/null
+++ b/libc/lib/stub.cpp
@@ -0,0 +1 @@
+// Empty file.



More information about the libc-commits mailing list