Skip to content

Commit ea890db

Browse files
committed
[OpenMP] Remove 'libomptarget.devicertl.a' fatbinary and use static library
Summary: Currently, we build a single `libomptarget.devicertl.a` which is a fatbinary. It is a host object file that contains the embedded archive files for both the NVIDIA and AMDGPU targets. This was done primarily as a convenience due to naming conflicts. Now that the clang driver for the GPU targets can appropriate link via the per-target runtime-dir, we can just make two separate static libraries and remove the indirection. This patch creates two new static libraries that get installed into ``` /lib/amdgcn-amd-amdhsa/libomp.a /lib/nvptx64-nvidia-cuda/libomp.a ``` for AMDGPU and NVPTX respectively. The link job created by the linker wrapper now simply needs to do `-lomp` and it will search those directories and link those static libraries. This requires far less special handling. This patch is a precursor to changing the build system entirely to be a runtimes based one. Soon this target will be a standard `add_library` and done through the GPU runtime targets. NOTE that this actually does remove an additional optimization step. Previously we merged all of the files into a single bitcode object and forcibly internalized some definitions. This, instead, just treats them like a normal static library. This may possibly affect performance for some files, but I think it's better overall to use static library semantics because it allows us to have an 'include-what-you-use' relationship with the library. Performance testing will be required. If we really need the merged blob then we can simply pack that into a new static library.
1 parent fe58eee commit ea890db

File tree

6 files changed

+42
-150
lines changed

6 files changed

+42
-150
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9209,6 +9209,10 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
92099209
A->render(Args, LinkerArgs);
92109210
}
92119211

9212+
// If this is OpenMP the device linker will need `-lomp`.
9213+
if (Kind == Action::OFK_OpenMP && !Args.hasArg(OPT_nogpulib))
9214+
LinkerArgs.emplace_back("-lomp");
9215+
92129216
// Forward all of these to the appropriate toolchain.
92139217
for (StringRef Arg : CompilerArgs)
92149218
CmdArgs.push_back(Args.MakeArgString(

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,9 +1289,6 @@ bool tools::addOpenMPRuntime(const Compilation &C, ArgStringList &CmdArgs,
12891289
if (IsOffloadingHost)
12901290
CmdArgs.push_back("-lomptarget");
12911291

1292-
if (IsOffloadingHost && !Args.hasArg(options::OPT_nogpulib))
1293-
CmdArgs.push_back("-lomptarget.devicertl");
1294-
12951292
addArchSpecificRPath(TC, Args, CmdArgs);
12961293

12971294
addOpenMPRuntimeLibraryPath(TC, Args, CmdArgs);

clang/test/Driver/openmp-offload-gpu.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,18 +324,18 @@
324324
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp=libomp --offload-arch=sm_52 --cuda-path=%S/Inputs/CUDA_102/usr/local/cuda \
325325
// RUN: -foffload-lto %s 2>&1 | FileCheck --check-prefix=CHECK-LTO-LIBRARY %s
326326

327-
// CHECK-LTO-LIBRARY: {{.*}}-lomptarget{{.*}}-lomptarget.devicertl
327+
// CHECK-LTO-LIBRARY: --device-linker={{.*}}=-lomp{{.*}}-lomptarget{{.*}}
328328

329329
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp=libomp --offload-arch=sm_52 --cuda-path=%S/Inputs/CUDA_102/usr/local/cuda \
330330
// RUN: --libomptarget-nvptx-bc-path=%S/Inputs/libomptarget/libomptarget-nvptx-test.bc %s 2>&1 \
331331
// RUN: | FileCheck --check-prefix=CHECK-NO-LTO-LIBRARY %s
332332

333-
// CHECK-NO-LTO-LIBRARY: {{.*}}-lomptarget{{.*}}-lomptarget.devicertl
333+
// CHECK-NO-LTO-LIBRARY: --device-linker={{.*}}=-lomp{{.*}}-lomptarget{{.*}}
334334

335335
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp=libomp --offload-arch=sm_52 -nogpulib \
336336
// RUN: -foffload-lto %s 2>&1 | FileCheck --check-prefix=CHECK-NO-LIBRARY %s
337337

338-
// CHECK-NO-LIBRARY-NOT: {{.*}}-lomptarget{{.*}}-lomptarget.devicertl
338+
// CHECK-NO-LIBRARY-NOT: --device-linker={{.*}}=-lomp{{.*}}-lomptarget{{.*}}
339339

340340
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp=libomp --offload-arch=sm_52 -nogpulib \
341341
// RUN: -Xoffload-linker a -Xoffload-linker-nvptx64-nvidia-cuda b -Xoffload-linker-nvptx64 c \

offload/DeviceRTL/CMakeLists.txt

Lines changed: 30 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,10 @@ if (LLVM_DIR)
1616
# Builds that use pre-installed LLVM have LLVM_DIR set.
1717
# A standalone or LLVM_ENABLE_RUNTIMES=openmp build takes this route
1818
find_program(CLANG_TOOL clang PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH)
19-
find_program(PACKAGER_TOOL clang-offload-packager PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH)
20-
find_program(LINK_TOOL llvm-link PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH)
21-
find_program(OPT_TOOL opt PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH)
22-
if ((NOT CLANG_TOOL) OR (NOT LINK_TOOL) OR (NOT OPT_TOOL) OR (NOT PACKAGER_TOOL))
23-
message(STATUS "Not building DeviceRTL. Missing clang: ${CLANG_TOOL}, llvm-link: ${LINK_TOOL}, opt: ${OPT_TOOL}, or clang-offload-packager: ${PACKAGER_TOOL}")
24-
return()
25-
else()
26-
message(STATUS "Building DeviceRTL. Using clang: ${CLANG_TOOL}, llvm-link: ${LINK_TOOL} and opt: ${OPT_TOOL}")
27-
endif()
2819
elseif (LLVM_TOOL_CLANG_BUILD AND NOT CMAKE_CROSSCOMPILING AND NOT OPENMP_STANDALONE_BUILD)
2920
# LLVM in-tree builds may use CMake target names to discover the tools.
3021
# A LLVM_ENABLE_PROJECTS=openmp build takes this route
3122
set(CLANG_TOOL $<TARGET_FILE:clang>)
32-
set(PACKAGER_TOOL $<TARGET_FILE:clang-offload-packager>)
33-
set(LINK_TOOL $<TARGET_FILE:llvm-link>)
34-
set(OPT_TOOL $<TARGET_FILE:opt>)
35-
message(STATUS "Building DeviceRTL. Using clang from in-tree build")
3623
else()
3724
message(STATUS "Not building DeviceRTL. No appropriate clang found")
3825
return()
@@ -82,8 +69,6 @@ set(src_files
8269
# propagation. That said, we will run the vectorizer again after the runtime
8370
# has been linked into the user program.
8471
set(clang_opt_flags -O3 -mllvm -openmp-opt-disable -DSHARED_SCRATCHPAD_SIZE=512 -mllvm -vectorize-slp=false )
85-
set(link_opt_flags -O3 -openmp-opt-disable -attributor-enable=module -vectorize-slp=false )
86-
set(link_export_flag -passes=internalize -internalize-public-api-file=${source_directory}/exports)
8772

8873
# If the user built with the GPU C library enabled we will use that instead.
8974
if(${LIBOMPTARGET_GPU_LIBC_SUPPORT})
@@ -107,15 +92,15 @@ set(bc_flags -c -flto -std=c++17 -fvisibility=hidden
10792
)
10893

10994
# first create an object target
110-
add_library(omptarget.devicertl.all_objs OBJECT IMPORTED)
11195
function(compileDeviceRTLLibrary target_name target_triple)
11296
set(target_bc_flags ${ARGN})
11397

11498
set(bc_files "")
99+
add_library(omp.${target_name}.all_objs OBJECT IMPORTED)
115100
foreach(src ${src_files})
116101
get_filename_component(infile ${src} ABSOLUTE)
117102
get_filename_component(outfile ${src} NAME)
118-
set(outfile "${outfile}-${target_name}.bc")
103+
set(outfile "${outfile}-${target_name}.o")
119104
set(depfile "${outfile}.d")
120105

121106
# Passing an empty CPU to -march= suppressed target specific metadata.
@@ -142,99 +127,36 @@ function(compileDeviceRTLLibrary target_name target_triple)
142127
endif()
143128
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${outfile})
144129

145-
list(APPEND bc_files ${outfile})
130+
list(APPEND obj_files ${CMAKE_CURRENT_BINARY_DIR}/${outfile})
146131
endforeach()
147-
148-
set(bclib_name "libomptarget-${target_name}.bc")
149-
150-
# Link to a bitcode library.
151-
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name}
152-
COMMAND ${LINK_TOOL}
153-
-o ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name} ${bc_files}
154-
DEPENDS ${bc_files}
155-
COMMENT "Linking LLVM bitcode ${bclib_name}"
156-
)
157-
158-
if(TARGET llvm-link)
159-
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name}
160-
DEPENDS llvm-link
161-
APPEND)
162-
endif()
163-
164-
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/internalized_${bclib_name}
165-
COMMAND ${OPT_TOOL} ${link_export_flag} ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name}
166-
-o ${CMAKE_CURRENT_BINARY_DIR}/internalized_${bclib_name}
167-
DEPENDS ${source_directory}/exports ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name}
168-
COMMENT "Internalizing LLVM bitcode ${bclib_name}"
132+
set_property(TARGET omp.${target_name}.all_objs
133+
APPEND PROPERTY IMPORTED_OBJECTS ${obj_files})
134+
135+
# Archive all the object files generated above into a static library
136+
add_library(omp.${target_name} STATIC)
137+
set_target_properties(omp.${target_name} PROPERTIES
138+
ARCHIVE_OUTPUT_DIRECTORY "${LIBOMPTARGET_LLVM_LIBRARY_INTDIR}/${target_triple}"
139+
ARCHIVE_OUTPUT_NAME omp
140+
LINKER_LANGUAGE CXX
169141
)
170-
if(TARGET opt)
171-
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/internalized_${bclib_name}
172-
DEPENDS opt
173-
APPEND)
174-
endif()
175-
176-
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}
177-
COMMAND ${OPT_TOOL} ${link_opt_flags} ${CMAKE_CURRENT_BINARY_DIR}/internalized_${bclib_name}
178-
-o ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}
179-
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/internalized_${bclib_name}
180-
COMMENT "Optimizing LLVM bitcode ${bclib_name}"
181-
)
182-
if(TARGET opt)
183-
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}
184-
DEPENDS opt
185-
APPEND)
186-
endif()
187-
188-
set(bclib_target_name "omptarget-${target_name}-bc")
189-
add_custom_target(${bclib_target_name} DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name})
190-
191-
# Copy library to destination.
192-
add_custom_command(TARGET ${bclib_target_name} POST_BUILD
193-
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}
194-
${LIBOMPTARGET_LIBRARY_DIR})
195-
add_dependencies(omptarget.devicertl.${target_name} ${bclib_target_name})
196-
197-
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${bclib_name} ${LIBOMPTARGET_LIBRARY_DIR}/${bclib_name})
198-
199-
# Install bitcode library under the lib destination folder.
200-
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name} DESTINATION "${OFFLOAD_INSTALL_LIBDIR}")
201-
202-
set(target_feature "")
203-
if("${target_triple}" STREQUAL "nvptx64-nvidia-cuda")
204-
set(target_feature "feature=+ptx63")
205-
endif()
206-
207-
# Package the bitcode in the bitcode and embed it in an ELF for the static library
208-
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name}
209-
COMMAND ${PACKAGER_TOOL} -o ${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name}
210-
"--image=file=${CMAKE_CURRENT_BINARY_DIR}/${bclib_name},${target_feature},triple=${target_triple},arch=generic,kind=openmp"
211-
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}
212-
COMMENT "Packaging LLVM offloading binary ${bclib_name}.out"
213-
)
214-
if(TARGET clang-offload-packager)
215-
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name}
216-
DEPENDS clang-offload-packager
217-
APPEND)
218-
endif()
219-
220-
set(output_name "${CMAKE_CURRENT_BINARY_DIR}/devicertl-${target_name}.o")
221-
add_custom_command(OUTPUT ${output_name}
222-
COMMAND ${CLANG_TOOL} --std=c++17 -c -nostdlib
223-
-Xclang -fembed-offload-object=${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name}
224-
-o ${output_name}
225-
${source_directory}/Stub.cpp
226-
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name} ${source_directory}/Stub.cpp
227-
COMMENT "Embedding LLVM offloading binary in devicertl-${target_name}.o"
228-
VERBATIM
229-
)
230-
if(TARGET clang)
231-
add_custom_command(OUTPUT ${output_name}
232-
DEPENDS clang
233-
APPEND)
234-
endif()
235-
236-
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${output_name})
237-
set_property(TARGET omptarget.devicertl.all_objs APPEND PROPERTY IMPORTED_OBJECTS ${output_name})
142+
target_link_libraries(omp.${target_name} PRIVATE omp.${target_name}.all_objs)
143+
144+
install(TARGETS omp.${target_name}
145+
ARCHIVE DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/${target_triple}")
146+
147+
# Trick to combine these into a bitcode file via the linker's LTO pass. This
148+
# is used to provide the legacy `libomptarget-<name>.bc` files.
149+
add_executable(libomptarget-${target_name} ${obj_files})
150+
set_target_properties(libomptarget-${target_name} PROPERTIES
151+
RUNTIME_OUTPUT_DIRECTORY ${LIBOMPTARGET_LLVM_LIBRARY_INTDIR}
152+
LINKER_LANGUAGE CXX
153+
RUNTIME_OUTPUT_NAME libomptarget-${target_name}.bc)
154+
target_compile_options(libomptarget-${target_name} PRIVATE "--target=${target_triple}")
155+
target_link_options(libomptarget-${target_name} PRIVATE "--target=${target_triple}"
156+
"-r" "-nostdlib" "-flto" "-Wl,--lto-emit-llvm")
157+
install(TARGETS libomptarget-${target_name}
158+
PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ
159+
DESTINATION ${OFFLOAD_INSTALL_LIBDIR})
238160

239161
if (CMAKE_EXPORT_COMPILE_COMMANDS)
240162
set(ide_target_name omptarget-ide-${target_name})
@@ -254,18 +176,6 @@ function(compileDeviceRTLLibrary target_name target_triple)
254176
endif()
255177
endfunction()
256178

257-
add_custom_target(omptarget.devicertl.amdgpu)
258179
compileDeviceRTLLibrary(amdgpu amdgcn-amd-amdhsa -Xclang -mcode-object-version=none)
259180

260-
add_custom_target(omptarget.devicertl.nvptx)
261181
compileDeviceRTLLibrary(nvptx nvptx64-nvidia-cuda --cuda-feature=+ptx63)
262-
263-
# Archive all the object files generated above into a static library
264-
add_library(omptarget.devicertl STATIC)
265-
set_target_properties(omptarget.devicertl PROPERTIES
266-
ARCHIVE_OUTPUT_DIRECTORY "${LIBOMPTARGET_LLVM_LIBRARY_INTDIR}"
267-
LINKER_LANGUAGE CXX
268-
)
269-
target_link_libraries(omptarget.devicertl PRIVATE omptarget.devicertl.all_objs)
270-
271-
install(TARGETS omptarget.devicertl ARCHIVE DESTINATION ${OFFLOAD_INSTALL_LIBDIR})

offload/DeviceRTL/src/exports

Lines changed: 0 additions & 19 deletions
This file was deleted.

offload/test/lit.cfg

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ else: # Unices
164164
if config.cuda_libdir:
165165
config.test_flags += " -Wl,-rpath," + config.cuda_libdir
166166
if config.libomptarget_current_target.startswith('nvptx'):
167-
config.test_flags_clang += " --libomptarget-nvptx-bc-path=" + config.library_dir + '/DeviceRTL'
167+
config.test_flags_clang += " --libomptarget-nvptx-bc-path=" + config.llvm_library_intdir
168168
if config.libomptarget_current_target.endswith('-LTO'):
169169
config.test_flags += " -foffload-lto"
170170
if config.libomptarget_current_target.endswith('-JIT-LTO') and evaluate_bool_env(
@@ -183,11 +183,11 @@ def remove_suffix_if_present(name):
183183

184184
def add_libraries(source):
185185
if config.libomptarget_has_libc:
186-
return source + " -Xoffload-linker " + "-lc " + \
187-
"-Xoffload-linker " + "-lm " + \
188-
config.llvm_library_intdir + "/libomptarget.devicertl.a"
186+
return source + " -Xoffload-linker -lc " + \
187+
"-Xoffload-linker -lm " + \
188+
"-Xoffload-linker -lomp "
189189
else:
190-
return source + " " + config.llvm_library_intdir + "/libomptarget.devicertl.a"
190+
return source + " " + "-Xoffload-lnker -lomp"
191191

192192
# Add platform targets
193193
host_targets = [

0 commit comments

Comments
 (0)