Skip to content

Commit 0d5aa84

Browse files
committedMar 13, 2018
[OpenMP] Add flag for linking runtime bitcode library
Summary: This patch adds an additional flag to the OpenMP device offloading toolchain to link in the runtime library bitcode. Reviewers: Hahnfeld, ABataev, carlo.bertolli, caomhin, grokos, hfinkel Reviewed By: ABataev, grokos Subscribers: jholewinski, guansong, cfe-commits Differential Revision: https://reviews.llvm.org/D43197 llvm-svn: 327460
1 parent e8f3b07 commit 0d5aa84

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed
 

‎clang/include/clang/Basic/DiagnosticDriverKinds.td

+3
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ def err_drv_expecting_fopenmp_with_fopenmp_targets : Error<
203203
def warn_drv_omp_offload_target_duplicate : Warning<
204204
"The OpenMP offloading target '%0' is similar to target '%1' already specified - will be ignored.">,
205205
InGroup<OpenMPTarget>;
206+
def warn_drv_omp_offload_target_missingbcruntime : Warning<
207+
"No library '%0' found in the default clang lib directory or in LIBRARY_PATH. Expect degraded performance due to no inlining of runtime functions on target devices.">,
208+
InGroup<OpenMPTarget>;
206209
def err_drv_bitcode_unsupported_on_toolchain : Error<
207210
"-fembed-bitcode is not supported on versions of iOS prior to 6.0">;
208211

‎clang/lib/Driver/ToolChains/Cuda.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,44 @@ void CudaToolChain::addClangTargetOptions(
581581
CC1Args.push_back("-target-feature");
582582
CC1Args.push_back("+ptx42");
583583
}
584+
585+
if (DeviceOffloadingKind == Action::OFK_OpenMP) {
586+
SmallVector<StringRef, 8> LibraryPaths;
587+
// Add path to lib and/or lib64 folders.
588+
SmallString<256> DefaultLibPath =
589+
llvm::sys::path::parent_path(getDriver().Dir);
590+
llvm::sys::path::append(DefaultLibPath,
591+
Twine("lib") + CLANG_LIBDIR_SUFFIX);
592+
LibraryPaths.emplace_back(DefaultLibPath.c_str());
593+
594+
// Add user defined library paths from LIBRARY_PATH.
595+
llvm::Optional<std::string> LibPath =
596+
llvm::sys::Process::GetEnv("LIBRARY_PATH");
597+
if (LibPath) {
598+
SmallVector<StringRef, 8> Frags;
599+
const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator, '\0'};
600+
llvm::SplitString(*LibPath, Frags, EnvPathSeparatorStr);
601+
for (StringRef Path : Frags)
602+
LibraryPaths.emplace_back(Path.trim());
603+
}
604+
605+
std::string LibOmpTargetName =
606+
"libomptarget-nvptx-" + GpuArch.str() + ".bc";
607+
bool FoundBCLibrary = false;
608+
for (StringRef LibraryPath : LibraryPaths) {
609+
SmallString<128> LibOmpTargetFile(LibraryPath);
610+
llvm::sys::path::append(LibOmpTargetFile, LibOmpTargetName);
611+
if (llvm::sys::fs::exists(LibOmpTargetFile)) {
612+
CC1Args.push_back("-mlink-cuda-bitcode");
613+
CC1Args.push_back(DriverArgs.MakeArgString(LibOmpTargetFile));
614+
FoundBCLibrary = true;
615+
break;
616+
}
617+
}
618+
if (!FoundBCLibrary)
619+
getDriver().Diag(diag::warn_drv_omp_offload_target_missingbcruntime)
620+
<< LibOmpTargetName;
621+
}
584622
}
585623

586624
void CudaToolChain::AddCudaIncludeArgs(const ArgList &DriverArgs,

‎clang/test/Driver/Inputs/libomptarget/libomptarget-nvptx-sm_20.bc

Whitespace-only changes.

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

+23
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,26 @@
142142
// RUN: | FileCheck -check-prefix=CHK-NOLIBDEVICE %s
143143

144144
// CHK-NOLIBDEVICE-NOT: error:{{.*}}sm_60
145+
146+
/// ###########################################################################
147+
148+
/// Check that the runtime bitcode library is part of the compile line. Create a bogus
149+
/// bitcode library and add it to the LIBRARY_PATH.
150+
// RUN: env LIBRARY_PATH=%S/Inputs/libomptarget %clang -### -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda \
151+
// RUN: -Xopenmp-target -march=sm_20 --cuda-path=%S/Inputs/CUDA_80/usr/local/cuda \
152+
// RUN: -fopenmp-relocatable-target -save-temps -no-canonical-prefixes %s 2>&1 \
153+
// RUN: | FileCheck -check-prefix=CHK-BCLIB %s
154+
155+
// CHK-BCLIB: clang{{.*}}-triple{{.*}}nvptx64-nvidia-cuda{{.*}}-mlink-cuda-bitcode{{.*}}libomptarget-nvptx-sm_20.bc
156+
// CHK-BCLIB-NOT: {{error:|warning:}}
157+
158+
/// ###########################################################################
159+
160+
/// Check that the warning is thrown when the libomptarget bitcode library is not found.
161+
/// Libomptarget requires sm_35 or newer so an sm_20 bitcode library should never exist.
162+
// RUN: %clang -### -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda \
163+
// RUN: -Xopenmp-target -march=sm_20 --cuda-path=%S/Inputs/CUDA_80/usr/local/cuda \
164+
// RUN: -fopenmp-relocatable-target -save-temps -no-canonical-prefixes %s 2>&1 \
165+
// RUN: | FileCheck -check-prefix=CHK-BCLIB-WARN %s
166+
167+
// CHK-BCLIB-WARN: No library 'libomptarget-nvptx-sm_20.bc' found in the default clang lib directory or in LIBRARY_PATH. Expect degraded performance due to no inlining of runtime functions on target devices.

0 commit comments

Comments
 (0)
Please sign in to comment.