@@ -186,6 +186,12 @@ RocmInstallationDetector::getInstallationPathCandidates() {
186
186
ROCmSearchDirs.emplace_back (RocmPathArg.str ());
187
187
DoPrintROCmSearchDirs ();
188
188
return ROCmSearchDirs;
189
+ } else if (const char *RocmPathEnv = ::getenv (" ROCM_PATH" )) {
190
+ if (!StringRef (RocmPathEnv).empty ()) {
191
+ ROCmSearchDirs.emplace_back (RocmPathEnv);
192
+ DoPrintROCmSearchDirs ();
193
+ return ROCmSearchDirs;
194
+ }
189
195
}
190
196
191
197
// Try to find relative to the compiler binary.
@@ -247,6 +253,43 @@ RocmInstallationDetector::getInstallationPathCandidates() {
247
253
248
254
ROCmSearchDirs.emplace_back (D.SysRoot + " /opt/rocm" ,
249
255
/* StrictChecking=*/ true );
256
+
257
+ // Find the latest /opt/rocm-{release} directory.
258
+ std::error_code EC;
259
+ std::string LatestROCm;
260
+ llvm::VersionTuple LatestVer;
261
+ // Get ROCm version from ROCm directory name.
262
+ auto GetROCmVersion = [](StringRef DirName) {
263
+ llvm::VersionTuple V;
264
+ std::string VerStr = DirName.drop_front (strlen (" rocm-" )).str ();
265
+ // The ROCm directory name follows the format of
266
+ // rocm-{major}.{minor}.{subMinor}[-{build}]
267
+ std::replace (VerStr.begin (), VerStr.end (), ' -' , ' .' );
268
+ V.tryParse (VerStr);
269
+ return V;
270
+ };
271
+ for (llvm::vfs::directory_iterator
272
+ File = D.getVFS ().dir_begin (D.SysRoot + " /opt" , EC),
273
+ FileEnd;
274
+ File != FileEnd && !EC; File.increment (EC)) {
275
+ llvm::StringRef FileName = llvm::sys::path::filename (File->path ());
276
+ if (!FileName.startswith (" rocm-" ))
277
+ continue ;
278
+ if (LatestROCm.empty ()) {
279
+ LatestROCm = FileName.str ();
280
+ LatestVer = GetROCmVersion (LatestROCm);
281
+ continue ;
282
+ }
283
+ auto Ver = GetROCmVersion (FileName);
284
+ if (LatestVer < Ver) {
285
+ LatestROCm = FileName.str ();
286
+ LatestVer = Ver;
287
+ }
288
+ }
289
+ if (!LatestROCm.empty ())
290
+ ROCmSearchDirs.emplace_back (D.SysRoot + " /opt/" + LatestROCm,
291
+ /* StrictChecking=*/ true );
292
+
250
293
DoPrintROCmSearchDirs ();
251
294
return ROCmSearchDirs;
252
295
}
0 commit comments