Skip to content

Commit

Permalink
[Comgr] use shared_ptr for rocm path detection
Browse files Browse the repository at this point in the history
This shows up as a leak. Make sure the memory is released after the
program is done executing.

Change-Id: I5b3e0a3f0e2e744cb19a9a42be2eb525d5c2bd9d
  • Loading branch information
cjatin authored and lamb-j committed Jan 7, 2025
1 parent 1e0fda7 commit e5bf7e5
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions amd/comgr/src/comgr-env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "llvm/Support/VirtualFileSystem.h"

#include <fstream>
#include <memory>
#include <stdlib.h>

using namespace llvm;
Expand Down Expand Up @@ -245,40 +246,35 @@ class SpackInstallationDetector : public InstallationDetector {
}
};

InstallationDetector *CreatePathDetector(StringRef Path,
bool isComgrPath = false) {
std::shared_ptr<InstallationDetector>
CreatePathDetector(StringRef Path, bool isComgrPath = false) {
StringRef DirName = llvm::sys::path::filename(Path);
if ((!isComgrPath && DirName.starts_with("rocm-cmake-")) ||
(isComgrPath && DirName.starts_with("comgr-"))) {
return new SpackInstallationDetector(Path, isComgrPath);
return std::make_shared<SpackInstallationDetector>(Path, isComgrPath);
}

return new InstallationDetector(Path, isComgrPath);
return std::make_shared<InstallationDetector>(Path, isComgrPath);
}

InstallationDetector *getDetectorImpl() {
std::shared_ptr<InstallationDetector> getDetectorImpl() {
SmallString<128> ROCmInstallPath;

static const char *EnvROCMPath = std::getenv("ROCM_PATH");
if (EnvROCMPath) {
ROCmInstallPath = EnvROCMPath;
}

InstallationDetector *Detector;
if (ROCmInstallPath == "") {
std::string ComgrInstallationPath = getComgrInstallPathFromExecutable();
Detector =
CreatePathDetector(ComgrInstallationPath, true /* isComgrPath */);
} else {
Detector = CreatePathDetector(ROCmInstallPath);
return CreatePathDetector(ComgrInstallationPath, true /* isComgrPath */);
}

return Detector;
return CreatePathDetector(ROCmInstallPath);
}

InstallationDetector *getDetector() {
static InstallationDetector *Detector = getDetectorImpl();
return Detector;
static auto Detector = getDetectorImpl();
return Detector.get();
}

llvm::StringRef getROCMPath() { return getDetector()->getROCmPath(); }
Expand Down

0 comments on commit e5bf7e5

Please sign in to comment.