Skip to content

Commit 79401b4

Browse files
committed
[OpenMP][AMDGPU] Add support for linking libomptarget bitcode
This patch uses the existing logic of CUDA for searching libomptarget and extracts it to a common method. Reviewed By: JonChesterfield, tianshilei1992 Differential Revision: https://reviews.llvm.org/D96248
1 parent 56277e3 commit 79401b4

File tree

9 files changed

+85
-53
lines changed

9 files changed

+85
-53
lines changed

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ def err_drv_omp_host_target_not_supported : Error<
261261
def err_drv_expecting_fopenmp_with_fopenmp_targets : Error<
262262
"The option -fopenmp-targets must be used in conjunction with a -fopenmp option compatible with offloading, please use -fopenmp=libomp or -fopenmp=libiomp5.">;
263263
def err_drv_omp_offload_target_missingbcruntime : Error<
264-
"No library '%0' found in the default clang lib directory or in LIBRARY_PATH. Please use --libomptarget-nvptx-bc-path to specify nvptx bitcode library.">;
264+
"No library '%0' found in the default clang lib directory or in LIBRARY_PATH. Please use --libomptarget-%1-bc-path to specify %1 bitcode library.">;
265265
def err_drv_omp_offload_target_bcruntime_not_found : Error<"Bitcode library '%0' does not exist.">;
266266
def warn_drv_omp_offload_target_duplicate : Warning<
267267
"The OpenMP offloading target '%0' is similar to target '%1' already specified - will be ignored.">,

clang/include/clang/Driver/Options.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,8 @@ def fuse_cuid_EQ : Joined<["-"], "fuse-cuid=">,
942942
"file path and command line options) | 'random' (ID's generated as "
943943
"random numbers) | 'none' (disabled). Default is 'hash'. This option "
944944
"will be overriden by option '-cuid=[ID]' if it is specified." >;
945+
def libomptarget_amdgcn_bc_path_EQ : Joined<["--"], "libomptarget-amdgcn-bc-path=">, Group<i_Group>,
946+
HelpText<"Path to libomptarget-amdgcn bitcode library">;
945947
def libomptarget_nvptx_bc_path_EQ : Joined<["--"], "libomptarget-nvptx-bc-path=">, Group<i_Group>,
946948
HelpText<"Path to libomptarget-nvptx bitcode library">;
947949
def dD : Flag<["-"], "dD">, Group<d_Group>, Flags<[CC1Option]>,

clang/lib/Driver/ToolChains/AMDGPUOpenMP.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ void AMDGPUOpenMPToolChain::addClangTargetOptions(
190190
CC1Args.push_back(DriverArgs.MakeArgStringRef(GpuArch));
191191
CC1Args.push_back("-fcuda-is-device");
192192
CC1Args.push_back("-emit-llvm-bc");
193+
194+
if (DriverArgs.hasArg(options::OPT_nogpulib))
195+
return;
196+
std::string BitcodeSuffix = "amdgcn-" + GpuArch.str();
197+
addOpenMPDeviceRTL(getDriver(), DriverArgs, CC1Args, BitcodeSuffix,
198+
getTriple());
193199
}
194200

195201
llvm::opt::DerivedArgList *AMDGPUOpenMPToolChain::TranslateArgs(

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,3 +1627,63 @@ void tools::addMachineOutlinerArgs(const Driver &D,
16271627
}
16281628
}
16291629
}
1630+
1631+
void tools::addOpenMPDeviceRTL(const Driver &D,
1632+
const llvm::opt::ArgList &DriverArgs,
1633+
llvm::opt::ArgStringList &CC1Args,
1634+
StringRef BitcodeSuffix,
1635+
const llvm::Triple &Triple) {
1636+
SmallVector<StringRef, 8> LibraryPaths;
1637+
// Add user defined library paths from LIBRARY_PATH.
1638+
llvm::Optional<std::string> LibPath =
1639+
llvm::sys::Process::GetEnv("LIBRARY_PATH");
1640+
if (LibPath) {
1641+
SmallVector<StringRef, 8> Frags;
1642+
const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator, '\0'};
1643+
llvm::SplitString(*LibPath, Frags, EnvPathSeparatorStr);
1644+
for (StringRef Path : Frags)
1645+
LibraryPaths.emplace_back(Path.trim());
1646+
}
1647+
1648+
// Add path to lib / lib64 folder.
1649+
SmallString<256> DefaultLibPath = llvm::sys::path::parent_path(D.Dir);
1650+
llvm::sys::path::append(DefaultLibPath, Twine("lib") + CLANG_LIBDIR_SUFFIX);
1651+
LibraryPaths.emplace_back(DefaultLibPath.c_str());
1652+
1653+
OptSpecifier LibomptargetBCPathOpt =
1654+
Triple.isAMDGCN() ? options::OPT_libomptarget_amdgcn_bc_path_EQ
1655+
: options::OPT_libomptarget_nvptx_bc_path_EQ;
1656+
1657+
StringRef ArchPrefix = Triple.isAMDGCN() ? "amdgcn" : "nvptx";
1658+
// First check whether user specifies bc library
1659+
if (const Arg *A = DriverArgs.getLastArg(LibomptargetBCPathOpt)) {
1660+
std::string LibOmpTargetName(A->getValue());
1661+
if (llvm::sys::fs::exists(LibOmpTargetName)) {
1662+
CC1Args.push_back("-mlink-builtin-bitcode");
1663+
CC1Args.push_back(DriverArgs.MakeArgString(LibOmpTargetName));
1664+
} else {
1665+
D.Diag(diag::err_drv_omp_offload_target_bcruntime_not_found)
1666+
<< LibOmpTargetName;
1667+
}
1668+
} else {
1669+
bool FoundBCLibrary = false;
1670+
1671+
std::string LibOmpTargetName =
1672+
"libomptarget-" + BitcodeSuffix.str() + ".bc";
1673+
1674+
for (StringRef LibraryPath : LibraryPaths) {
1675+
SmallString<128> LibOmpTargetFile(LibraryPath);
1676+
llvm::sys::path::append(LibOmpTargetFile, LibOmpTargetName);
1677+
if (llvm::sys::fs::exists(LibOmpTargetFile)) {
1678+
CC1Args.push_back("-mlink-builtin-bitcode");
1679+
CC1Args.push_back(DriverArgs.MakeArgString(LibOmpTargetFile));
1680+
FoundBCLibrary = true;
1681+
break;
1682+
}
1683+
}
1684+
1685+
if (!FoundBCLibrary)
1686+
D.Diag(diag::err_drv_omp_offload_target_missingbcruntime)
1687+
<< LibOmpTargetName << ArchPrefix;
1688+
}
1689+
}

clang/lib/Driver/ToolChains/CommonArgs.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ unsigned getOrCheckAMDGPUCodeObjectVersion(const Driver &D,
145145
void addMachineOutlinerArgs(const Driver &D, const llvm::opt::ArgList &Args,
146146
llvm::opt::ArgStringList &CmdArgs,
147147
const llvm::Triple &Triple, bool IsLTO);
148+
149+
void addOpenMPDeviceRTL(const Driver &D, const llvm::opt::ArgList &DriverArgs,
150+
llvm::opt::ArgStringList &CC1Args,
151+
StringRef BitcodeSuffix, const llvm::Triple &Triple);
148152
} // end namespace tools
149153
} // end namespace driver
150154
} // end namespace clang

clang/lib/Driver/ToolChains/Cuda.cpp

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -749,56 +749,10 @@ void CudaToolChain::addClangTargetOptions(
749749
CudaVersionToString(CudaInstallation.version())));
750750

751751
if (DeviceOffloadingKind == Action::OFK_OpenMP) {
752-
SmallVector<StringRef, 8> LibraryPaths;
753-
// Add user defined library paths from LIBRARY_PATH.
754-
llvm::Optional<std::string> LibPath =
755-
llvm::sys::Process::GetEnv("LIBRARY_PATH");
756-
if (LibPath) {
757-
SmallVector<StringRef, 8> Frags;
758-
const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator, '\0'};
759-
llvm::SplitString(*LibPath, Frags, EnvPathSeparatorStr);
760-
for (StringRef Path : Frags)
761-
LibraryPaths.emplace_back(Path.trim());
762-
}
763-
764-
// Add path to lib / lib64 folder.
765-
SmallString<256> DefaultLibPath =
766-
llvm::sys::path::parent_path(getDriver().Dir);
767-
llvm::sys::path::append(DefaultLibPath, Twine("lib") + CLANG_LIBDIR_SUFFIX);
768-
LibraryPaths.emplace_back(DefaultLibPath.c_str());
769-
770-
// First check whether user specifies bc library
771-
if (const Arg *A =
772-
DriverArgs.getLastArg(options::OPT_libomptarget_nvptx_bc_path_EQ)) {
773-
std::string LibOmpTargetName(A->getValue());
774-
if (llvm::sys::fs::exists(LibOmpTargetName)) {
775-
CC1Args.push_back("-mlink-builtin-bitcode");
776-
CC1Args.push_back(DriverArgs.MakeArgString(LibOmpTargetName));
777-
} else {
778-
getDriver().Diag(diag::err_drv_omp_offload_target_bcruntime_not_found)
779-
<< LibOmpTargetName;
780-
}
781-
} else {
782-
bool FoundBCLibrary = false;
783-
784-
std::string LibOmpTargetName = "libomptarget-nvptx-cuda_" +
785-
CudaVersionStr + "-" + GpuArch.str() +
786-
".bc";
787-
788-
for (StringRef LibraryPath : LibraryPaths) {
789-
SmallString<128> LibOmpTargetFile(LibraryPath);
790-
llvm::sys::path::append(LibOmpTargetFile, LibOmpTargetName);
791-
if (llvm::sys::fs::exists(LibOmpTargetFile)) {
792-
CC1Args.push_back("-mlink-builtin-bitcode");
793-
CC1Args.push_back(DriverArgs.MakeArgString(LibOmpTargetFile));
794-
FoundBCLibrary = true;
795-
break;
796-
}
797-
}
798-
if (!FoundBCLibrary)
799-
getDriver().Diag(diag::err_drv_omp_offload_target_missingbcruntime)
800-
<< LibOmpTargetName;
801-
}
752+
std::string BitcodeSuffix =
753+
"nvptx-cuda_" + CudaVersionStr + "-" + GpuArch.str();
754+
addOpenMPDeviceRTL(getDriver(), DriverArgs, CC1Args, BitcodeSuffix,
755+
getTriple());
802756
}
803757
}
804758

clang/test/Driver/Inputs/hip_dev_lib/libomptarget-amdgcn-gfx803.bc

Whitespace-only changes.

clang/test/Driver/Inputs/hip_dev_lib/libomptarget-amdgcn-gfx906.bc

Whitespace-only changes.

clang/test/Driver/amdgpu-openmp-toolchain.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// REQUIRES: amdgpu-registered-target
2-
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa -Xopenmp-target=amdgcn-amd-amdhsa -march=gfx906 %s 2>&1 \
2+
// RUN: env LIBRARY_PATH=%S/Inputs/hip_dev_lib %clang -### --target=x86_64-unknown-linux-gnu -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa -Xopenmp-target=amdgcn-amd-amdhsa -march=gfx906 %s 2>&1 \
33
// RUN: | FileCheck %s
44

55
// verify the tools invocations
66
// CHECK: clang{{.*}}"-cc1" "-triple" "x86_64-unknown-linux-gnu"{{.*}}"-x" "c"{{.*}}
77
// CHECK: clang{{.*}}"-cc1" "-triple" "x86_64-unknown-linux-gnu"{{.*}}"-x" "ir"{{.*}}
8-
// CHECK: clang{{.*}}"-cc1"{{.*}}"-triple" "amdgcn-amd-amdhsa"{{.*}}"-target-cpu" "gfx906" "-fcuda-is-device" "-emit-llvm-bc"{{.*}}
8+
// CHECK: clang{{.*}}"-cc1"{{.*}}"-triple" "amdgcn-amd-amdhsa"{{.*}}"-target-cpu" "gfx906" "-fcuda-is-device" "-emit-llvm-bc" "-mlink-builtin-bitcode"{{.*}}libomptarget-amdgcn-gfx906.bc"{{.*}}
99
// CHECK: llvm-link{{.*}}"-o" "{{.*}}amdgpu-openmp-toolchain-{{.*}}-gfx906-linked-{{.*}}.bc"
1010
// CHECK: llc{{.*}}amdgpu-openmp-toolchain-{{.*}}-gfx906-linked-{{.*}}.bc" "-mtriple=amdgcn-amd-amdhsa" "-mcpu=gfx906" "-filetype=obj" "-o"{{.*}}amdgpu-openmp-toolchain-{{.*}}-gfx906-{{.*}}.o"
1111
// CHECK: lld{{.*}}"-flavor" "gnu" "--no-undefined" "-shared" "-o"{{.*}}amdgpu-openmp-toolchain-{{.*}}.out" "{{.*}}amdgpu-openmp-toolchain-{{.*}}-gfx906-{{.*}}.o"
@@ -34,3 +34,9 @@
3434
// CHECK-PHASES: 15: assembler, {14}, object, (host-openmp)
3535
// CHECK-PHASES: 16: linker, {4, 15}, image, (host-openmp)
3636

37+
// handling of --libomptarget-amdgcn-bc-path
38+
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa -Xopenmp-target=amdgcn-amd-amdhsa -march=gfx803 --libomptarget-amdgcn-bc-path=%S/Inputs/hip_dev_lib/libomptarget-amdgcn-gfx803.bc %s 2>&1 | FileCheck %s --check-prefix=CHECK-LIBOMPTARGET
39+
// CHECK-LIBOMPTARGET: clang{{.*}}"-cc1"{{.*}}"-triple" "amdgcn-amd-amdhsa"{{.*}}"-target-cpu" "gfx803" "-fcuda-is-device" "-emit-llvm-bc" "-mlink-builtin-bitcode"{{.*}}Inputs/hip_dev_lib/libomptarget-amdgcn-gfx803.bc"{{.*}}
40+
41+
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa -Xopenmp-target=amdgcn-amd-amdhsa -march=gfx803 -nogpulib %s 2>&1 | FileCheck %s --check-prefix=CHECK-NOGPULIB
42+
// CHECK-NOGPULIB-NOT: clang{{.*}}"-cc1"{{.*}}"-triple" "amdgcn-amd-amdhsa"{{.*}}"-target-cpu" "gfx803" "-fcuda-is-device" "-emit-llvm-bc" "-mlink-builtin-bitcode"{{.*}}libomptarget-amdgcn-gfx803.bc"{{.*}}

0 commit comments

Comments
 (0)