Skip to content

Commit e162eeb

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 640ba3f commit e162eeb

File tree

3 files changed

+80
-13
lines changed

3 files changed

+80
-13
lines changed

libc/cmake/modules/LLVMLibCLibraryRules.cmake

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ endfunction()
8989
# add_gpu_entrypoint_library(
9090
# DEPENDS <list of add_entrypoint_object targets>
9191
# )
92-
function(add_gpu_entrypoint_library target_name)
92+
function(add_gpu_entrypoint_library target_name base_target_name)
9393
cmake_parse_arguments(
9494
"ENTRYPOINT_LIBRARY"
9595
"" # No optional arguments
@@ -127,7 +127,7 @@ function(add_gpu_entrypoint_library target_name)
127127
COMMAND ${LIBC_CLANG_OFFLOAD_PACKAGER}
128128
"${prefix},file=$<JOIN:${object},,file=>" -o
129129
${CMAKE_CURRENT_BINARY_DIR}/binary/${name}.gpubin
130-
DEPENDS ${dep}
130+
DEPENDS ${dep} ${base_target_name}
131131
COMMENT "Packaging LLVM offloading binary for '${object}'"
132132
)
133133
add_custom_target(${dep}.__gpubin__ DEPENDS ${dep}
@@ -140,7 +140,7 @@ function(add_gpu_entrypoint_library target_name)
140140
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/stubs/${name}.cpp"
141141
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/stubs
142142
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/stubs/${name}.cpp
143-
DEPENDS ${dep} ${dep}.__gpubin__
143+
DEPENDS ${dep} ${dep}.__gpubin__ ${base_target_name}
144144
)
145145
add_custom_target(${dep}.__stub__
146146
DEPENDS ${dep}.__gpubin__ "${CMAKE_CURRENT_BINARY_DIR}/stubs/${name}.cpp")
@@ -156,7 +156,8 @@ function(add_gpu_entrypoint_library target_name)
156156
target_compile_options(${dep}.__fatbin__ PRIVATE
157157
--target=${LLVM_HOST_TRIPLE}
158158
"SHELL:-Xclang -fembed-offload-object=${CMAKE_CURRENT_BINARY_DIR}/binary/${name}.gpubin")
159-
add_dependencies(${dep}.__fatbin__ ${dep} ${dep}.__stub__ ${dep}.__gpubin__)
159+
add_dependencies(${dep}.__fatbin__
160+
${dep} ${dep}.__stub__ ${dep}.__gpubin__ ${base_target_name})
160161

161162
# Set the list of newly create fat binaries containing embedded device code.
162163
list(APPEND objects $<TARGET_OBJECTS:${dep}.__fatbin__>)
@@ -170,6 +171,46 @@ function(add_gpu_entrypoint_library target_name)
170171
set_target_properties(${target_name} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${LIBC_LIBRARY_DIR})
171172
endfunction(add_gpu_entrypoint_library)
172173

174+
# A rule to build a library from a collection of entrypoint objects and bundle
175+
# it in a single LLVM-IR bitcode file.
176+
# Usage:
177+
# add_gpu_entrypoint_library(
178+
# DEPENDS <list of add_entrypoint_object targets>
179+
# )
180+
function(add_bitcode_entrypoint_library target_name base_target_name)
181+
cmake_parse_arguments(
182+
"ENTRYPOINT_LIBRARY"
183+
"" # No optional arguments
184+
"" # No single value arguments
185+
"DEPENDS" # Multi-value arguments
186+
${ARGN}
187+
)
188+
if(NOT ENTRYPOINT_LIBRARY_DEPENDS)
189+
message(FATAL_ERROR "'add_entrypoint_library' target requires a DEPENDS list "
190+
"of 'add_entrypoint_object' targets.")
191+
endif()
192+
193+
get_fq_deps_list(fq_deps_list ${ENTRYPOINT_LIBRARY_DEPENDS})
194+
get_all_object_file_deps(all_deps "${fq_deps_list}")
195+
196+
set(objects "")
197+
foreach(dep IN LISTS all_deps)
198+
set(object $<$<STREQUAL:$<TARGET_NAME_IF_EXISTS:${dep}>,${dep}>:$<TARGET_OBJECTS:${dep}>>)
199+
list(APPEND objects ${object})
200+
endforeach()
201+
202+
set(output ${CMAKE_CURRENT_BINARY_DIR}/${target_name}.bc)
203+
add_custom_command(
204+
OUTPUT ${output}
205+
COMMAND ${LIBC_LLVM_LINK} ${objects} -o ${output}
206+
DEPENDS ${all_deps} ${base_target_name}
207+
COMMENT "Linking LLVM-IR bitcode for ${base_target_name}"
208+
COMMAND_EXPAND_LISTS
209+
)
210+
add_custom_target(${target_name} DEPENDS ${output} ${all_deps})
211+
set_target_properties(${target_name} PROPERTIES TARGET_OBJECT ${output})
212+
endfunction(add_bitcode_entrypoint_library)
213+
173214
# A rule to build a library from a collection of entrypoint objects.
174215
# Usage:
175216
# 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 & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,33 @@ foreach(archive IN ZIP_LISTS
4040
# Add the offloading version of the library for offloading languages. These
4141
# are installed in the standard search path separate from the other libraries.
4242
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-
4643
add_gpu_entrypoint_library(
47-
${libc_gpu_archive_target}
44+
${archive_1}gpu
45+
${archive_1}
4846
DEPENDS
4947
${${archive_2}}
5048
)
5149
set_target_properties(
52-
${libc_gpu_archive_target}
50+
${archive_1}gpu
5351
PROPERTIES
54-
ARCHIVE_OUTPUT_NAME ${libc_gpu_archive_name}
52+
ARCHIVE_OUTPUT_NAME ${archive_0}gpu-${LIBC_TARGET_ARCHITECTURE}
53+
ARCHIVE_OUTPUT_DIRECTORY ${LLVM_LIBRARY_OUTPUT_INTDIR}
54+
)
55+
list(APPEND added_gpu_archive_targets ${archive_1}gpu)
56+
57+
add_bitcode_entrypoint_library(
58+
${archive_1}bitcode
59+
${archive_1}
60+
DEPENDS
61+
${${archive_2}}
5562
)
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})
63+
set_target_properties(
64+
${archive_1}bitcode
65+
PROPERTIES
66+
OUTPUT_NAME ${archive_1}.bc
67+
)
68+
add_dependencies(${archive_1}gpu ${archive_1}bitcode)
69+
list(APPEND added_gpu_bitcode_targets ${archive_1}bitcode)
5970
endif()
6071
endforeach()
6172

@@ -71,6 +82,13 @@ if(LIBC_TARGET_OS_IS_GPU)
7182
ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
7283
COMPONENT libc
7384
)
85+
foreach(file ${added_gpu_bitcode_targets})
86+
install(FILES $<TARGET_PROPERTY:${file},TARGET_OBJECT>
87+
DESTINATION ${LIBC_INSTALL_LIBRARY_DIR}
88+
RENAME $<TARGET_PROPERTY:${file},OUTPUT_NAME>
89+
COMPONENT libc
90+
)
91+
endforeach()
7492
endif()
7593

7694
if(NOT LIBC_TARGET_OS_IS_BAREMETAL)

0 commit comments

Comments
 (0)