[libc-commits] [libc] [libc] Fix building bitcode library for GPU (PR #100491)
Joseph Huber via libc-commits
libc-commits at lists.llvm.org
Fri Jul 26 08:30:55 PDT 2024
https://github.com/jhuber6 updated https://github.com/llvm/llvm-project/pull/100491
>From 7475e68bc6c4f17b069247f844a10d6468fa622c 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/cmake/modules/LLVMLibCLibraryRules.cmake | 16 ++--------
.../modules/prepare_libc_gpu_build.cmake | 30 ++-----------------
libc/lib/CMakeLists.txt | 27 ++++++-----------
3 files changed, 14 insertions(+), 59 deletions(-)
diff --git a/libc/cmake/modules/LLVMLibCLibraryRules.cmake b/libc/cmake/modules/LLVMLibCLibraryRules.cmake
index e677b4cd2c28f..ad931d445720d 100644
--- a/libc/cmake/modules/LLVMLibCLibraryRules.cmake
+++ b/libc/cmake/modules/LLVMLibCLibraryRules.cmake
@@ -111,19 +111,9 @@ function(add_bitcode_entrypoint_library target_name base_target_name)
list(APPEND objects ${object})
endforeach()
- set(output ${CMAKE_CURRENT_BINARY_DIR}/${target_name}.bc)
- add_custom_command(
- OUTPUT ${output}
- COMMAND ${LIBC_LLVM_LINK} ${objects} -o ${output}
- DEPENDS ${all_deps} ${base_target_name}
- COMMENT "Linking LLVM-IR bitcode for ${base_target_name}"
- COMMAND_EXPAND_LISTS
- )
- add_custom_target(${target_name} DEPENDS ${output} ${all_deps})
- set_target_properties(${target_name} PROPERTIES TARGET_OBJECT ${output})
- if(TARGET llvm-link)
- add_dependencies(${target_name} llvm-link)
- endif()
+ add_executable(${target_name} ${objects})
+ target_link_options(${target_name} PRIVATE
+ "-r" "-nostdlib" "-flto" "-Wl,--lto-emit-llvm" "-march= ")
endfunction(add_bitcode_entrypoint_library)
# A rule to build a library from a collection of entrypoint objects.
diff --git a/libc/cmake/modules/prepare_libc_gpu_build.cmake b/libc/cmake/modules/prepare_libc_gpu_build.cmake
index e2a0908023a02..f3537f2634e5b 100644
--- a/libc/cmake/modules/prepare_libc_gpu_build.cmake
+++ b/libc/cmake/modules/prepare_libc_gpu_build.cmake
@@ -21,36 +21,10 @@ if(LIBC_TARGET_TRIPLE)
set(CMAKE_REQUIRED_FLAGS "--target=${LIBC_TARGET_TRIPLE}")
endif()
if(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
- set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nogpulib")
+ set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nogpulib -nostdlib")
elseif(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
set(CMAKE_REQUIRED_FLAGS
- "${CMAKE_REQUIRED_FLAGS} -flto -c -Wno-unused-command-line-argument")
-endif()
-
-# Identify the program used to package multiple images into a single binary.
-get_filename_component(compiler_path ${CMAKE_CXX_COMPILER} DIRECTORY)
-if(TARGET clang-offload-packager)
- get_target_property(LIBC_CLANG_OFFLOAD_PACKAGER clang-offload-packager LOCATION)
-else()
- find_program(LIBC_CLANG_OFFLOAD_PACKAGER
- NAMES clang-offload-packager NO_DEFAULT_PATH
- PATHS ${LLVM_BINARY_DIR}/bin ${compiler_path})
- if(NOT LIBC_CLANG_OFFLOAD_PACKAGER)
- message(FATAL_ERROR "Cannot find the 'clang-offload-packager' for the GPU "
- "build")
- endif()
-endif()
-
-# Identify llvm-link program so we can merge the output IR into a single blob.
-if(TARGET llvm-link)
- get_target_property(LIBC_LLVM_LINK llvm-link LOCATION)
-else()
- find_program(LIBC_LLVM_LINK
- NAMES llvm-link NO_DEFAULT_PATH
- PATHS ${LLVM_BINARY_DIR}/bin ${compiler_path})
- if(NOT LIBC_LLVM_LINK)
- message(FATAL_ERROR "Cannot find 'llvm-link' for the GPU build")
- endif()
+ "${CMAKE_REQUIRED_FLAGS} -flto -c -Wno-unused-command-line-argument -nostdlib")
endif()
# Optionally set up a job pool to limit the number of GPU tests run in parallel.
diff --git a/libc/lib/CMakeLists.txt b/libc/lib/CMakeLists.txt
index 4b7cfc4b76e2e..ce0b07fb6cb49 100644
--- a/libc/lib/CMakeLists.txt
+++ b/libc/lib/CMakeLists.txt
@@ -51,7 +51,7 @@ 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)
endif()
endforeach()
@@ -61,24 +61,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_FILE:${file}>
+ 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 +82,7 @@ endif()
add_custom_target(install-libc
DEPENDS ${added_archive_targets}
+ ${added_bitcode_targets}
${startup_target}
${header_install_target}
COMMAND "${CMAKE_COMMAND}"
@@ -100,6 +90,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}"
More information about the libc-commits
mailing list