Skip to content

Commit e3ea278

Browse files
gregrodgersronlieb
authored andcommitted
[OPENMP] fix for finding rocm and llvm rt libs for lib,lib-debug,lib-perf, and LIBRARY_PATH
Change-Id: I4a3510b836cd91febb2defe78840c64ab9e54d5b
1 parent 897e75b commit e3ea278

File tree

3 files changed

+55
-59
lines changed

3 files changed

+55
-59
lines changed

clang/lib/Driver/ToolChains/AMDGPUOpenMP.cpp

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,6 @@ const char *amdgpu::dlr::getLinkCommandArgs(
232232
LibSuffix.append("/asan");
233233
}
234234

235-
// If device debugging turned on, add specially built bc files
236-
StringRef libpath = Args.MakeArgString(C.getDriver().Dir + "/../" + LibSuffix);
237-
std::string lib_debug_perf_path = FindDebugPerfInLibraryPath(LibSuffix);
238-
if (!lib_debug_perf_path.empty())
239-
libpath = lib_debug_perf_path;
240-
241235
llvm::SmallVector<std::string, 12> BCLibs;
242236

243237
std::string AsanRTL;
@@ -260,29 +254,30 @@ const char *amdgpu::dlr::getLinkCommandArgs(
260254
// not the installed compiler.
261255
std::string LibDeviceName = "/libomptarget-amdgpu-" + GPUArch.str() + ".bc";
262256

263-
SmallString<128> Path(Args.MakeArgString(libpath + LibDeviceName));
264-
if (LibSuffix != "lib" || llvm::sys::fs::exists(Path)) {
265-
BCLibs.push_back(Args.MakeArgString(Path));
266-
} else {
267-
// Check if the device library can be found in
268-
// one of the LIBRARY_PATH directories.
269-
bool EnvOmpLibDeviceFound = false;
270-
for (auto &EnvLibraryPath : EnvironmentLibraryPaths) {
271-
std::string EnvOmpLibDevice = EnvLibraryPath + LibDeviceName;
272-
if (llvm::sys::fs::exists(EnvOmpLibDevice)) {
273-
EnvOmpLibDeviceFound = true;
274-
BCLibs.push_back(EnvOmpLibDevice);
275-
break;
276-
}
277-
}
278-
// If LIBRARY_PATH doesn't point to the device library,
279-
// then use the default one.
280-
if (!EnvOmpLibDeviceFound) {
281-
std::string RtDir = "/../runtimes/runtimes-bins/offload";
282-
BCLibs.push_back(Args.MakeArgString(libpath + RtDir + LibDeviceName));
257+
// Check if libomptarget device bitcode can be found in a LIBRARY_PATH dir
258+
bool EnvOmpLibDeviceFound = false;
259+
for (auto &EnvLibraryPath : EnvironmentLibraryPaths) {
260+
std::string EnvOmpLibDevice = EnvLibraryPath + LibDeviceName;
261+
if (llvm::sys::fs::exists(EnvOmpLibDevice)) {
262+
EnvOmpLibDeviceFound = true;
263+
BCLibs.push_back(EnvOmpLibDevice);
264+
break;
283265
}
284266
}
285267

268+
// If not found in LIBRARY_PATH, use default for the correct LibSuffix.
269+
if (!EnvOmpLibDeviceFound) {
270+
StringRef bc_file_suf = Args.MakeArgString(C.getDriver().Dir + "/../" +
271+
LibSuffix + LibDeviceName);
272+
StringRef bc_file_lib =
273+
Args.MakeArgString(C.getDriver().Dir + "/../lib" + LibDeviceName);
274+
if (llvm::sys::fs::exists(bc_file_suf))
275+
BCLibs.push_back(Args.MakeArgString(bc_file_suf));
276+
else if (llvm::sys::fs::exists(bc_file_lib))
277+
// In case a LibSuffix version not found, use suffix "lib"
278+
BCLibs.push_back(Args.MakeArgString(bc_file_lib));
279+
}
280+
286281
if (!AsanRTL.empty()) {
287282
if (!Args.hasArg(options::OPT_nogpulib)) {
288283
// asanrtl is dependent on ockl so for every asanrtl bitcode linking

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,29 +1185,6 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
11851185
Args.MakeArgString(Twine(PluginOptPrefix) + "-implicit-mapsyms"));
11861186
}
11871187

1188-
std::string tools::FindDebugPerfInLibraryPath(const std::string &RLib) {
1189-
const char *DirList = ::getenv("LIBRARY_PATH");
1190-
if (!DirList)
1191-
return "";
1192-
StringRef Dirs(DirList);
1193-
if (Dirs.empty()) // Empty string should not add '.'.
1194-
return "";
1195-
1196-
StringRef::size_type Delim;
1197-
while ((Delim = Dirs.find(llvm::sys::EnvPathSeparator)) != StringRef::npos) {
1198-
if (Delim != 0) { // Leading colon.
1199-
if (Dirs.substr(0, Delim).ends_with(RLib))
1200-
return Dirs.substr(0, Delim).str();
1201-
}
1202-
Dirs = Dirs.substr(Delim + 1);
1203-
}
1204-
if (!Dirs.empty()) {
1205-
if (Dirs.ends_with(RLib))
1206-
return Dirs.str();
1207-
}
1208-
return "";
1209-
}
1210-
12111188
void tools::addOpenMPRuntimeSpecificRPath(const ToolChain &TC,
12121189
const ArgList &Args,
12131190
ArgStringList &CmdArgs) {
@@ -1223,9 +1200,17 @@ void tools::addOpenMPRuntimeSpecificRPath(const ToolChain &TC,
12231200
if (TC.getSanitizerArgs(Args).needsAsanRt())
12241201
LibSuffix.append("/asan");
12251202
}
1226-
std::string CandidateRPath = FindDebugPerfInLibraryPath(LibSuffix);
1227-
if (CandidateRPath.empty())
1228-
CandidateRPath = D.Dir + "/../" + LibSuffix;
1203+
1204+
// Check if the device library can be found in
1205+
// one of the LIBRARY_PATH directories.
1206+
ArgStringList EnvLibraryPaths;
1207+
addDirectoryList(Args, EnvLibraryPaths, "", "LIBRARY_PATH");
1208+
for (auto &EnvLibraryPath : EnvLibraryPaths) {
1209+
if (llvm::sys::fs::exists(EnvLibraryPath)) {
1210+
CmdArgs.push_back("-rpath");
1211+
CmdArgs.push_back(Args.MakeArgString(EnvLibraryPath));
1212+
}
1213+
}
12291214

12301215
if (Args.hasFlag(options::OPT_fopenmp_implicit_rpath,
12311216
options::OPT_fno_openmp_implicit_rpath, true)) {
@@ -1239,13 +1224,31 @@ void tools::addOpenMPRuntimeSpecificRPath(const ToolChain &TC,
12391224
CmdArgs.push_back(Args.MakeArgString(TC.getCompilerRTPath()));
12401225
}
12411226

1227+
// In case LibSuffix was not built, try lib
1228+
std::string CandidateRPath_suf = D.Dir + "/../" + LibSuffix;
12421229
CmdArgs.push_back("-rpath");
1243-
CmdArgs.push_back(Args.MakeArgString(CandidateRPath.c_str()));
1244-
std::string rocmPath = Args.getLastArgValue(clang::driver::options::OPT_rocm_path_EQ).str();
1245-
if (rocmPath.size() != 0){
1246-
rocmPath = rocmPath + "/lib";
1230+
CmdArgs.push_back(Args.MakeArgString(CandidateRPath_suf.c_str()));
1231+
1232+
// Add lib directory in case LibSuffix does not exist
1233+
std::string CandidateRPath_lib = D.Dir + "/../lib";
1234+
if ((!llvm::sys::fs::exists(CandidateRPath_suf)) &&
1235+
(llvm::sys::fs::exists(CandidateRPath_lib))) {
12471236
CmdArgs.push_back("-rpath");
1248-
CmdArgs.push_back(Args.MakeArgString(rocmPath.c_str()));
1237+
CmdArgs.push_back(Args.MakeArgString(CandidateRPath_lib.c_str()));
1238+
}
1239+
1240+
std::string rocmPath =
1241+
Args.getLastArgValue(clang::driver::options::OPT_rocm_path_EQ).str();
1242+
if (rocmPath.size() != 0) {
1243+
std::string rocmPath_lib = rocmPath + "/lib";
1244+
std::string rocmPath_suf = rocmPath + "/" + LibSuffix;
1245+
if (llvm::sys::fs::exists(rocmPath_suf)) {
1246+
CmdArgs.push_back("-rpath");
1247+
CmdArgs.push_back(Args.MakeArgString(rocmPath_suf.c_str()));
1248+
} else if (llvm::sys::fs::exists(rocmPath_lib)) {
1249+
CmdArgs.push_back("-rpath");
1250+
CmdArgs.push_back(Args.MakeArgString(rocmPath_lib.c_str()));
1251+
}
12491252
}
12501253
if (llvm::find_if(CmdArgs, [](StringRef str) {
12511254
return !str.compare("--enable-new-dtags");

clang/lib/Driver/ToolChains/CommonArgs.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,6 @@ void AddAssemblerKPIC(const ToolChain &ToolChain,
119119
const llvm::opt::ArgList &Args,
120120
llvm::opt::ArgStringList &CmdArgs);
121121

122-
std::string FindDebugPerfInLibraryPath(const std::string &RLib);
123-
124122
void addOpenMPRuntimeSpecificRPath(const ToolChain &TC,
125123
const llvm::opt::ArgList &Args,
126124
llvm::opt::ArgStringList &CmdArgs);

0 commit comments

Comments
 (0)