Skip to content

Commit 6bd34a1

Browse files
jhuber6ronlieb
authored andcommitted
[OpenMP] Remove-libomptarget.devicertl.a-fatbinary
1 parent 3dbc0e0 commit 6bd34a1

File tree

5 files changed

+73
-134
lines changed

5 files changed

+73
-134
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9491,6 +9491,10 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
94919491
"--lto-newpm-passes=default-post-link<O" + OOpt + ">"));
94929492
}
94939493
}
9494+
// If this is OpenMP the device linker will need `-lompdevice`.
9495+
if (Kind == Action::OFK_OpenMP && !Args.hasArg(OPT_no_offloadlib) &&
9496+
(TC->getTriple().isAMDGPU() || TC->getTriple().isNVPTX()))
9497+
LinkerArgs.emplace_back("-lompdevice");
94949498

94959499
// Forward all of these to the appropriate toolchain.
94969500
for (StringRef Arg : CompilerArgs)

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,10 +1450,6 @@ bool tools::addOpenMPRuntime(const Compilation &C, ArgStringList &CmdArgs,
14501450
CmdArgs.push_back("-lomptarget");
14511451
}
14521452

1453-
if (IsOffloadingHost &&
1454-
Args.hasFlag(options::OPT_offloadlib, options::OPT_no_offloadlib, true))
1455-
CmdArgs.push_back("-lomptarget.devicertl");
1456-
14571453
addArchSpecificRPath(TC, Args, CmdArgs);
14581454

14591455
if (RTKind == Driver::OMPRT_OMP || RTKind == Driver::OMPRT_BOLT)

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

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

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

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

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

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

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

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

offload/DeviceRTL/CMakeLists.txt

Lines changed: 42 additions & 123 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()
@@ -97,8 +84,6 @@ set(src_files
9784
# propagation. That said, we will run the vectorizer again after the runtime
9885
# has been linked into the user program.
9986
set(clang_opt_flags -O3 -mllvm -openmp-opt-disable -DSHARED_SCRATCHPAD_SIZE=512 -mllvm -vectorize-slp=false )
100-
set(link_opt_flags -O3 -openmp-opt-disable -attributor-enable=module -vectorize-slp=false )
101-
set(link_export_flag -passes=internalize -internalize-public-api-file=${source_directory}/exports)
10287

10388
# If the user built with the GPU C library enabled we will use that instead.
10489
if(${LIBOMPTARGET_GPU_LIBC_SUPPORT})
@@ -151,15 +136,14 @@ function(compileDeviceRTLLibrary target_name target_triple)
151136
if(NOT _ocml_bc)
152137
message(FATAL_ERROR "Could not find ocml.bc")
153138
endif()
154-
list(APPEND bc_flags -Xclang -mlink-builtin-bitcode -Xclang ${_ockl_bc})
155-
list(APPEND bc_flags -Xclang -mlink-builtin-bitcode -Xclang ${_ocml_bc})
139+
list(APPEND target_bc_flags -Xclang -mlink-builtin-bitcode -Xclang ${_ockl_bc})
140+
list(APPEND target_bc_flags -Xclang -mlink-builtin-bitcode -Xclang ${_ocml_bc})
156141
endif()
157142

158-
set(bc_files "")
159143
foreach(src ${src_files})
160144
get_filename_component(infile ${src} ABSOLUTE)
161145
get_filename_component(outfile ${src} NAME)
162-
set(outfile "${outfile}-${target_name}.bc")
146+
set(outfile "${outfile}-${target_name}.o")
163147
set(depfile "${outfile}.d")
164148

165149
# Passing an empty CPU to -march= suppressed target specific metadata.
@@ -186,99 +170,42 @@ function(compileDeviceRTLLibrary target_name target_triple)
186170
endif()
187171
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${outfile})
188172

189-
list(APPEND bc_files ${outfile})
173+
list(APPEND obj_files ${CMAKE_CURRENT_BINARY_DIR}/${outfile})
190174
endforeach()
191-
192-
set(bclib_name "libomptarget-${target_name}.bc")
193-
194-
# Link to a bitcode library.
195-
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name}
196-
COMMAND ${LINK_TOOL}
197-
-o ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name} ${bc_files}
198-
DEPENDS ${bc_files}
199-
COMMENT "Linking LLVM bitcode ${bclib_name}"
200-
)
201-
202-
if(TARGET llvm-link)
203-
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name}
204-
DEPENDS llvm-link
205-
APPEND)
206-
endif()
207-
208-
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/internalized_${bclib_name}
209-
COMMAND ${OPT_TOOL} ${link_export_flag} ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name}
210-
-o ${CMAKE_CURRENT_BINARY_DIR}/internalized_${bclib_name}
211-
DEPENDS ${source_directory}/exports ${CMAKE_CURRENT_BINARY_DIR}/linked_${bclib_name}
212-
COMMENT "Internalizing LLVM bitcode ${bclib_name}"
213-
)
214-
if(TARGET opt)
215-
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/internalized_${bclib_name}
216-
DEPENDS opt
217-
APPEND)
218-
endif()
219-
220-
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}
221-
COMMAND ${OPT_TOOL} ${link_opt_flags} ${CMAKE_CURRENT_BINARY_DIR}/internalized_${bclib_name}
222-
-o ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}
223-
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/internalized_${bclib_name}
224-
COMMENT "Optimizing LLVM bitcode ${bclib_name}"
225-
)
226-
if(TARGET opt)
227-
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}
228-
DEPENDS opt
229-
APPEND)
230-
endif()
231-
232-
set(bclib_target_name "omptarget-${target_name}-bc")
233-
add_custom_target(${bclib_target_name} DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name})
234-
235-
# Copy library to destination.
236-
add_custom_command(TARGET ${bclib_target_name} POST_BUILD
237-
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}
238-
${LIBOMPTARGET_LIBRARY_DIR})
239-
add_dependencies(omptarget.devicertl.${target_name} ${bclib_target_name})
240-
241-
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${bclib_name} ${LIBOMPTARGET_LIBRARY_DIR}/${bclib_name})
242-
243-
# Install bitcode library under the lib destination folder.
244-
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name} DESTINATION "${OFFLOAD_INSTALL_LIBDIR}")
245-
246-
set(target_feature "")
247-
if("${target_triple}" STREQUAL "nvptx64-nvidia-cuda")
248-
set(target_feature "feature=+ptx63")
249-
endif()
250-
251-
# Package the bitcode in the bitcode and embed it in an ELF for the static library
252-
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name}
253-
COMMAND ${PACKAGER_TOOL} -o ${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name}
254-
"--image=file=${CMAKE_CURRENT_BINARY_DIR}/${bclib_name},${target_feature},triple=${target_triple},arch=generic,kind=openmp"
255-
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${bclib_name}
256-
COMMENT "Packaging LLVM offloading binary ${bclib_name}.out"
257-
)
258-
if(TARGET clang-offload-packager)
259-
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name}
260-
DEPENDS clang-offload-packager
261-
APPEND)
262-
endif()
263-
264-
set(output_name "${CMAKE_CURRENT_BINARY_DIR}/devicertl-${target_name}.o")
265-
add_custom_command(OUTPUT ${output_name}
266-
COMMAND ${CLANG_TOOL} --std=c++17 -c -nostdlib
267-
-Xclang -fembed-offload-object=${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name}
268-
-o ${output_name}
269-
${source_directory}/Stub.cpp
270-
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/packaged_${bclib_name} ${source_directory}/Stub.cpp
271-
COMMENT "Embedding LLVM offloading binary in devicertl-${target_name}.o"
272-
VERBATIM
175+
# Trick to combine these into a bitcode file via the linker's LTO pass. This
176+
# is used to provide the legacy `libomptarget-<name>.bc` files. Hack this
177+
# through as an executable to get it to use the relocatable link.
178+
add_executable(libomptarget-${target_name})
179+
target_sources(libomptarget-${target_name} PRIVATE ${obj_files})
180+
set_target_properties(libomptarget-${target_name} PROPERTIES
181+
RUNTIME_OUTPUT_DIRECTORY ${LIBOMPTARGET_LLVM_LIBRARY_INTDIR}
182+
LINKER_LANGUAGE CXX
183+
BUILD_RPATH ""
184+
INSTALL_RPATH ""
185+
RUNTIME_OUTPUT_NAME libomptarget-${target_name}.bc)
186+
target_compile_options(libomptarget-${target_name} PRIVATE "--target=${target_triple}")
187+
target_link_options(libomptarget-${target_name} PRIVATE "--target=${target_triple}"
188+
"-r" "-nostdlib" "-flto" "-Wl,--lto-emit-llvm")
189+
install(TARGETS libomptarget-${target_name}
190+
PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ
191+
DESTINATION ${OFFLOAD_INSTALL_LIBDIR})
192+
193+
add_library(omptarget.${target_name}.all_objs OBJECT IMPORTED)
194+
set_property(TARGET omptarget.${target_name}.all_objs APPEND PROPERTY IMPORTED_OBJECTS
195+
${LIBOMPTARGET_LLVM_LIBRARY_INTDIR}/libomptarget-${target_name}.bc)
196+
197+
# Archive all the object files generated above into a static library
198+
add_library(omptarget.${target_name} STATIC)
199+
set_target_properties(omptarget.${target_name} PROPERTIES
200+
ARCHIVE_OUTPUT_DIRECTORY "${LIBOMPTARGET_LLVM_LIBRARY_INTDIR}/${target_triple}"
201+
ARCHIVE_OUTPUT_NAME ompdevice
202+
LINKER_LANGUAGE CXX
273203
)
274-
if(TARGET clang)
275-
add_custom_command(OUTPUT ${output_name}
276-
DEPENDS clang
277-
APPEND)
278-
endif()
204+
add_dependencies(omptarget.${target_name} libomptarget-${target_name})
205+
target_link_libraries(omptarget.${target_name} PRIVATE omptarget.${target_name}.all_objs)
279206

280-
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${output_name})
281-
set_property(TARGET omptarget.devicertl.all_objs APPEND PROPERTY IMPORTED_OBJECTS ${output_name})
207+
install(TARGETS omptarget.${target_name}
208+
ARCHIVE DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/${target_triple}")
282209

283210
if (CMAKE_EXPORT_COMPILE_COMMANDS)
284211
set(ide_target_name omptarget-ide-${target_name})
@@ -298,18 +225,10 @@ function(compileDeviceRTLLibrary target_name target_triple)
298225
endif()
299226
endfunction()
300227

301-
add_custom_target(omptarget.devicertl.amdgpu)
302-
compileDeviceRTLLibrary(amdgpu amdgcn-amd-amdhsa -Xclang -mcode-object-version=none)
303-
304-
add_custom_target(omptarget.devicertl.nvptx)
305-
compileDeviceRTLLibrary(nvptx nvptx64-nvidia-cuda --cuda-feature=+ptx63)
306-
307-
# Archive all the object files generated above into a static library
308-
add_library(omptarget.devicertl STATIC)
309-
set_target_properties(omptarget.devicertl PROPERTIES
310-
ARCHIVE_OUTPUT_DIRECTORY "${LIBOMPTARGET_LLVM_LIBRARY_INTDIR}"
311-
LINKER_LANGUAGE CXX
312-
)
313-
target_link_libraries(omptarget.devicertl PRIVATE omptarget.devicertl.all_objs)
228+
if(NOT LLVM_TARGETS_TO_BUILD OR "AMDGPU" IN_LIST LLVM_TARGETS_TO_BUILD)
229+
compileDeviceRTLLibrary(amdgpu amdgcn-amd-amdhsa -Xclang -mcode-object-version=none)
230+
endif()
314231

315-
install(TARGETS omptarget.devicertl ARCHIVE DESTINATION ${OFFLOAD_INSTALL_LIBDIR})
232+
if(NOT LLVM_TARGETS_TO_BUILD OR "NVPTX" IN_LIST LLVM_TARGETS_TO_BUILD)
233+
compileDeviceRTLLibrary(nvptx nvptx64-nvidia-cuda --cuda-feature=+ptx63)
234+
endif()

offload/test/lit.cfg

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,14 @@ def remove_suffix_if_present(name):
195195
return name
196196

197197
def add_libraries(source):
198+
if "gpu" not in config.available_features:
199+
return source
198200
if config.libomptarget_has_libc:
199-
return source + " -Xoffload-linker " + "-lc " + \
200-
"-Xoffload-linker " + "-lm " + \
201-
config.llvm_library_intdir + "/../runtimes/runtimes-bins/offload/libomptarget.devicertl.a"
201+
return source + " -Xoffload-linker -lc " + \
202+
"-Xoffload-linker -lm " + \
203+
"-Xoffload-linker -lompdevice"
202204
else:
203-
return source + " " + config.llvm_library_intdir + "/../runtimes/runtimes-bins/offload/libomptarget.devicertl.a"
205+
return source + " " + "-Xoffload-lnker -lompdevice"
204206

205207
def get_arch_from_target(libomptarget_target):
206208
if libomptarget_target.startswith('amdgcn'):
@@ -236,6 +238,24 @@ if config.libomptarget_current_target.startswith('amdgcn'):
236238
if config.libomptarget_current_target in host_targets:
237239
config.available_features.add('host')
238240

241+
def remove_suffix_if_present(name):
242+
if name.endswith('-LTO'):
243+
return name[:-4]
244+
elif name.endswith('-JIT-LTO'):
245+
return name[:-8]
246+
else:
247+
return name
248+
249+
def add_libraries(source):
250+
if "gpu" not in config.available_features:
251+
return source
252+
if config.libomptarget_has_libc:
253+
return source + " -Xoffload-linker -lc " + \
254+
"-Xoffload-linker -lm " + \
255+
"-Xoffload-linker -lompdevice"
256+
else:
257+
return source + " " + "-Xoffload-lnker -lompdevice"
258+
239259
# substitutions
240260
# - for targets that exist in the system create the actual command.
241261
# - for valid targets that do not exist in the system, return false, so that the

0 commit comments

Comments
 (0)