Skip to content

Commit f8cd4c5

Browse files
authoredJul 26, 2024
[libc] Fix building bitcode library for GPU (#100491)
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.
1 parent 372a6be commit f8cd4c5

File tree

3 files changed

+14
-59
lines changed

3 files changed

+14
-59
lines changed
 

‎libc/cmake/modules/LLVMLibCLibraryRules.cmake

+3-13
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,9 @@ function(add_bitcode_entrypoint_library target_name base_target_name)
111111
list(APPEND objects ${object})
112112
endforeach()
113113

114-
set(output ${CMAKE_CURRENT_BINARY_DIR}/${target_name}.bc)
115-
add_custom_command(
116-
OUTPUT ${output}
117-
COMMAND ${LIBC_LLVM_LINK} ${objects} -o ${output}
118-
DEPENDS ${all_deps} ${base_target_name}
119-
COMMENT "Linking LLVM-IR bitcode for ${base_target_name}"
120-
COMMAND_EXPAND_LISTS
121-
)
122-
add_custom_target(${target_name} DEPENDS ${output} ${all_deps})
123-
set_target_properties(${target_name} PROPERTIES TARGET_OBJECT ${output})
124-
if(TARGET llvm-link)
125-
add_dependencies(${target_name} llvm-link)
126-
endif()
114+
add_executable(${target_name} ${objects})
115+
target_link_options(${target_name} PRIVATE
116+
"-r" "-nostdlib" "-flto" "-Wl,--lto-emit-llvm" "-march= ")
127117
endfunction(add_bitcode_entrypoint_library)
128118

129119
# A rule to build a library from a collection of entrypoint objects.

‎libc/cmake/modules/prepare_libc_gpu_build.cmake

+2-28
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,10 @@ if(LIBC_TARGET_TRIPLE)
2121
set(CMAKE_REQUIRED_FLAGS "--target=${LIBC_TARGET_TRIPLE}")
2222
endif()
2323
if(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
24-
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nogpulib")
24+
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nogpulib -nostdlib")
2525
elseif(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
2626
set(CMAKE_REQUIRED_FLAGS
27-
"${CMAKE_REQUIRED_FLAGS} -flto -c -Wno-unused-command-line-argument")
28-
endif()
29-
30-
# Identify the program used to package multiple images into a single binary.
31-
get_filename_component(compiler_path ${CMAKE_CXX_COMPILER} DIRECTORY)
32-
if(TARGET clang-offload-packager)
33-
get_target_property(LIBC_CLANG_OFFLOAD_PACKAGER clang-offload-packager LOCATION)
34-
else()
35-
find_program(LIBC_CLANG_OFFLOAD_PACKAGER
36-
NAMES clang-offload-packager NO_DEFAULT_PATH
37-
PATHS ${LLVM_BINARY_DIR}/bin ${compiler_path})
38-
if(NOT LIBC_CLANG_OFFLOAD_PACKAGER)
39-
message(FATAL_ERROR "Cannot find the 'clang-offload-packager' for the GPU "
40-
"build")
41-
endif()
42-
endif()
43-
44-
# Identify llvm-link program so we can merge the output IR into a single blob.
45-
if(TARGET llvm-link)
46-
get_target_property(LIBC_LLVM_LINK llvm-link LOCATION)
47-
else()
48-
find_program(LIBC_LLVM_LINK
49-
NAMES llvm-link NO_DEFAULT_PATH
50-
PATHS ${LLVM_BINARY_DIR}/bin ${compiler_path})
51-
if(NOT LIBC_LLVM_LINK)
52-
message(FATAL_ERROR "Cannot find 'llvm-link' for the GPU build")
53-
endif()
27+
"${CMAKE_REQUIRED_FLAGS} -flto -c -Wno-unused-command-line-argument -nostdlib")
5428
endif()
5529

5630
# Optionally set up a job pool to limit the number of GPU tests run in parallel.

‎libc/lib/CMakeLists.txt

+9-18
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ foreach(archive IN ZIP_LISTS
5151
PROPERTIES
5252
OUTPUT_NAME ${archive_1}.bc
5353
)
54-
list(APPEND added_gpu_bitcode_targets ${archive_1}bitcode)
54+
list(APPEND added_bitcode_targets ${archive_1}bitcode)
5555
endif()
5656
endforeach()
5757

@@ -61,24 +61,13 @@ install(
6161
COMPONENT libc
6262
)
6363

64-
if(LIBC_TARGET_OS_IS_GPU)
65-
set(gpu_install_dir lib${LLVM_LIBDIR_SUFFIX})
66-
if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR)
67-
set(gpu_install_dir lib${LLVM_LIBDIR_SUFFIX}/${LLVM_HOST_TRIPLE})
68-
endif()
69-
install(
70-
TARGETS ${added_gpu_archive_targets}
71-
ARCHIVE DESTINATION ${gpu_install_dir}
72-
COMPONENT libc
64+
foreach(file ${added_bitcode_targets})
65+
install(FILES $<TARGET_FILE:${file}>
66+
DESTINATION ${LIBC_INSTALL_LIBRARY_DIR}
67+
RENAME $<TARGET_PROPERTY:${file},OUTPUT_NAME>
68+
COMPONENT libc
7369
)
74-
foreach(file ${added_gpu_bitcode_targets})
75-
install(FILES $<TARGET_PROPERTY:${file},TARGET_OBJECT>
76-
DESTINATION ${LIBC_INSTALL_LIBRARY_DIR}
77-
RENAME $<TARGET_PROPERTY:${file},OUTPUT_NAME>
78-
COMPONENT libc
79-
)
80-
endforeach()
81-
endif()
70+
endforeach()
8271

8372
if(NOT LIBC_TARGET_OS_IS_BAREMETAL)
8473
# For now we will disable libc-startup installation for baremetal. The
@@ -93,13 +82,15 @@ endif()
9382

9483
add_custom_target(install-libc
9584
DEPENDS ${added_archive_targets}
85+
${added_bitcode_targets}
9686
${startup_target}
9787
${header_install_target}
9888
COMMAND "${CMAKE_COMMAND}"
9989
-DCMAKE_INSTALL_COMPONENT=libc
10090
-P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
10191
add_custom_target(install-libc-stripped
10292
DEPENDS ${added_archive_targets}
93+
${added_bitcode_targets}
10394
${startup_target}
10495
${header_install_target}
10596
COMMAND "${CMAKE_COMMAND}"

0 commit comments

Comments
 (0)
Please sign in to comment.