Skip to content

Commit 294deaf

Browse files
committed
[libc] Install a single LLVM-IR version of the GPU library
Summary: Recent patches have allowed us to treat these libraries as direct builds. This makes it easier to simply build them to a single LLVM-IR file. This matches the way these files are presented by the ROCm and CUDA toolchains and makes it easier to work with.
1 parent b8a7d81 commit 294deaf

File tree

3 files changed

+75
-16
lines changed

3 files changed

+75
-16
lines changed

libc/cmake/modules/LLVMLibCLibraryRules.cmake

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,46 @@ function(add_gpu_entrypoint_library target_name)
170170
set_target_properties(${target_name} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${LIBC_LIBRARY_DIR})
171171
endfunction(add_gpu_entrypoint_library)
172172

173+
# A rule to build a library from a collection of entrypoint objects and bundle
174+
# it in a single LLVM-IR bitcode file.
175+
# Usage:
176+
# add_gpu_entrypoint_library(
177+
# DEPENDS <list of add_entrypoint_object targets>
178+
# )
179+
function(add_bitcode_entrypoint_library target_name)
180+
cmake_parse_arguments(
181+
"ENTRYPOINT_LIBRARY"
182+
"" # No optional arguments
183+
"" # No single value arguments
184+
"DEPENDS" # Multi-value arguments
185+
${ARGN}
186+
)
187+
if(NOT ENTRYPOINT_LIBRARY_DEPENDS)
188+
message(FATAL_ERROR "'add_entrypoint_library' target requires a DEPENDS list "
189+
"of 'add_entrypoint_object' targets.")
190+
endif()
191+
192+
get_fq_deps_list(fq_deps_list ${ENTRYPOINT_LIBRARY_DEPENDS})
193+
get_all_object_file_deps(all_deps "${fq_deps_list}")
194+
195+
set(objects "")
196+
foreach(dep IN LISTS all_deps)
197+
set(object $<$<STREQUAL:$<TARGET_NAME_IF_EXISTS:${dep}>,${dep}>:$<TARGET_OBJECTS:${dep}>>)
198+
list(APPEND objects ${object})
199+
endforeach()
200+
201+
set(output ${CMAKE_CURRENT_BINARY_DIR}/${target_name}.bc)
202+
add_custom_command(
203+
OUTPUT ${output}
204+
COMMAND ${LIBC_LLVM_LINK} ${objects} -o ${output}
205+
DEPENDS ${all_deps}
206+
COMMENT "Linking LLVM-IR bitcode for ${target_name}"
207+
COMMAND_EXPAND_LISTS
208+
)
209+
add_custom_target(${target_name} DEPENDS ${output} ${all_deps})
210+
set_target_properties(${target_name} PROPERTIES TARGET_OBJECT ${output})
211+
endfunction(add_bitcode_entrypoint_library)
212+
173213
# A rule to build a library from a collection of entrypoint objects.
174214
# Usage:
175215
# add_entrypoint_library(

libc/cmake/modules/prepare_libc_gpu_build.cmake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ if(NOT LIBC_CLANG_OFFLOAD_PACKAGER)
2626
"build")
2727
endif()
2828

29+
# Identify llvm-link program so we can merge the output IR into a single blob.
30+
find_program(LIBC_LLVM_LINK
31+
NAMES llvm-link NO_DEFAULT_PATH
32+
PATHS ${LLVM_BINARY_DIR}/bin ${compiler_path})
33+
if(NOT LIBC_LLVM_LINK)
34+
message(FATAL_ERROR "Cannot find 'llvm-link' for the GPU build")
35+
endif()
36+
2937
# Optionally set up a job pool to limit the number of GPU tests run in parallel.
3038
# This is sometimes necessary as running too many tests in parallel can cause
3139
# the GPU or driver to run out of resources.

libc/lib/CMakeLists.txt

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,41 @@ foreach(archive IN ZIP_LISTS
3737
endif()
3838
list(APPEND added_archive_targets ${archive_1})
3939

40-
# Add the offloading version of the library for offloading languages. These
41-
# are installed in the standard search path separate from the other libraries.
40+
# The GPU build additionally exports the libraries as both a fat binary
41+
# archive and an LLVM-IR bitcode blob.
42+
# FIXME: These don't properly re-run if the source gets modified. We willy
43+
# likely need source file dependencies for them.
4244
if(LIBC_TARGET_OS_IS_GPU)
43-
set(libc_gpu_archive_target ${archive_1}gpu)
44-
set(libc_gpu_archive_name ${archive_0}gpu-${LIBC_TARGET_ARCHITECTURE})
45-
4645
add_gpu_entrypoint_library(
47-
${libc_gpu_archive_target}
46+
${archive_1}.__fatbin__
4847
DEPENDS
4948
${${archive_2}}
5049
)
5150
set_target_properties(
52-
${libc_gpu_archive_target}
51+
${archive_1}.__fatbin__
5352
PROPERTIES
54-
ARCHIVE_OUTPUT_NAME ${libc_gpu_archive_name}
53+
ARCHIVE_OUTPUT_NAME ${archive_0}gpu-${LIBC_TARGET_ARCHITECTURE}
54+
ARCHIVE_OUTPUT_DIRECTORY ${LLVM_LIBRARY_OUTPUT_INTDIR}
55+
)
56+
57+
add_bitcode_entrypoint_library(
58+
${archive_1}.__bitcode__
59+
DEPENDS
60+
${${archive_2}}
61+
)
62+
add_dependencies(${archive_1} ${archive_1}.__bitcode__)
63+
64+
install(
65+
TARGETS ${added_gpu_archive_targets}
66+
ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
67+
COMPONENT libc
68+
)
69+
70+
install(FILES $<TARGET_PROPERTY:${archive_1}.__bitcode__,TARGET_OBJECT>
71+
DESTINATION ${LIBC_INSTALL_LIBRARY_DIR}
72+
RENAME ${archive_1}.bc
73+
COMPONENT libc
5574
)
56-
set_target_properties(${libc_gpu_archive_target} PROPERTIES
57-
ARCHIVE_OUTPUT_DIRECTORY ${LLVM_LIBRARY_OUTPUT_INTDIR})
58-
list(APPEND added_gpu_archive_targets ${libc_gpu_archive_target})
5975
endif()
6076
endforeach()
6177

@@ -66,11 +82,6 @@ install(
6682
)
6783

6884
if(LIBC_TARGET_OS_IS_GPU)
69-
install(
70-
TARGETS ${added_gpu_archive_targets}
71-
ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
72-
COMPONENT libc
73-
)
7485
endif()
7586

7687
if(NOT LIBC_TARGET_OS_IS_BAREMETAL)

0 commit comments

Comments
 (0)