Skip to content

Commit 5999f9f

Browse files
authored
[Comgr] Remove HIP_PATH and ROCM_PATH logic, simplify LLVM_PATH (llvm#99)
Previously, a significant amount of code/logic in comgr-env.cpp was dedicated to "guessing" the correct ROCM, HIP, and LLVM system paths for HIP/OpenCL compilations. However, these paths can change with restructuring to the ROCm directory layout. As is, in many configurations the paths are currently incorrect. Also, many of the includes/etc. the paths were used for have been removed.
1 parent dc7d034 commit 5999f9f

File tree

6 files changed

+10
-245
lines changed

6 files changed

+10
-245
lines changed

amd/comgr/README.md

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,22 +108,11 @@ required. If the value is used, it is read once at the time it is needed, and
108108
then cached. The exact behavior when changing these values during the execution
109109
of a process after Comgr APIs have been invoked is undefined.
110110

111-
Comgr supports some environment variables to locate parts of the ROCm stack.
112-
These include:
113-
114-
* `ROCM_PATH`: If this is set it is used as an absolute path to the root of the
115-
ROCm installation, which is used when determining the default values for
116-
`HIP_PATH` and `LLVM_PATH` (see below). If this is not set and if a ROCM
117-
package is provided during the build, ROCM path is set to it. Otherwise Comgr
118-
tries to construct the ROCM path from the path where amd_comgr library
119-
is located.
120-
* `HIP_PATH`: If this is set it is used as an absolute path to the root of the
121-
HIP installation. If this is not set, it has a default value of
122-
"${ROCM_PATH}/hip".
123-
* `LLVM_PATH`: If this is set it is used as an absolute path to the root of the
124-
LLVM installation, which is currently used for HIP compilation to locate
125-
certain runtime headers. If this is not set, it has a default value of
126-
"${ROCM_PATH}/llvm".
111+
Comgr supports an environment variable to help locate LLVM:
112+
113+
* `LLVM_PATH`: If set, it is used as an absolute path to the root of the LLVM
114+
installation, which is currently used to locate the clang resource directory
115+
and clang binary path, allowing for additional optimizations.
127116

128117
Comgr also supports some environment variables to aid in debugging. These
129118
include:

amd/comgr/docs/ReleaseNotes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ directly relying on HIP
213213
validate generated artifacts with command-line tools like llvm-dis,
214214
llvm-objdump, etc. Moving forward, most new Comgr tests should be written as
215215
lit tests, and tests in comgr/test should be transitioned to comgr/test-lit.
216+
- Removed HIP\_PATH and ROCM\_PATH environment variables. These were used for
217+
now-removed Comgr actions, such as \*COMPILE\_SOURCE\_TO\_FATBIN.
216218

217219
New Targets
218220
-----------

amd/comgr/src/comgr-compiler.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,10 +1001,6 @@ AMDGPUCompiler::addTargetIdentifierFlags(llvm::StringRef IdentStr,
10011001
}
10021002

10031003
amd_comgr_status_t AMDGPUCompiler::addCompilationFlags() {
1004-
HIPIncludePath = (Twine(env::getHIPPath()) + "/include").str();
1005-
// HIP headers depend on hsa.h which is in ROCM_DIR/include.
1006-
ROCMIncludePath = (Twine(env::getROCMPath()) + "/include").str();
1007-
10081004
// Default to O3 for all contexts
10091005
Args.push_back("-O3");
10101006

@@ -1027,10 +1023,6 @@ amd_comgr_status_t AMDGPUCompiler::addCompilationFlags() {
10271023
case AMD_COMGR_LANGUAGE_HIP:
10281024
Args.push_back("hip");
10291025
Args.push_back("--offload-device-only");
1030-
Args.push_back("-isystem");
1031-
Args.push_back(ROCMIncludePath.c_str());
1032-
Args.push_back("-isystem");
1033-
Args.push_back(HIPIncludePath.c_str());
10341026
break;
10351027
default:
10361028
return AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT;

amd/comgr/src/comgr-compiler.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,6 @@ class AMDGPUCompiler {
5252
DataAction *ActionInfo;
5353
DataSet *InSet;
5454
amd_comgr_data_set_t OutSetT;
55-
/// ROCM include Path
56-
std::string ROCMIncludePath;
57-
/// HIP and Clang Include Paths
58-
std::string HIPIncludePath;
59-
std::string ClangIncludePath;
60-
std::string ClangIncludePath2;
6155
/// Precompiled header file paths.
6256
llvm::SmallVector<llvm::SmallString<128>, 2> PrecompiledHeaders;
6357
/// Arguments common to all driver invocations in the current action.

amd/comgr/src/comgr-env.cpp

Lines changed: 3 additions & 207 deletions
Original file line numberDiff line numberDiff line change
@@ -69,214 +69,10 @@ bool shouldEmitVerboseLogs() {
6969
return VerboseLogs && StringRef(VerboseLogs) != "0";
7070
}
7171

72-
StringRef StripGNUInstallLibDir(StringRef Path) {
73-
// Comgr library may be installed under lib or lib64 or
74-
// lib/<multiarch-tuple> on Debian.
75-
StringRef ParentDir = sys::path::parent_path(Path);
76-
StringRef ParentName = sys::path::filename(ParentDir);
77-
78-
StringRef SecondLevelParentDir = sys::path::parent_path(ParentDir);
79-
StringRef SecondLevelParentName = sys::path::filename(SecondLevelParentDir);
80-
81-
if (ParentName == "lib" || ParentName == "lib64") {
82-
ParentDir = sys::path::parent_path(ParentDir);
83-
} else if (SecondLevelParentName == "lib") {
84-
ParentDir = sys::path::parent_path(SecondLevelParentDir);
85-
}
86-
87-
return ParentDir;
88-
}
89-
90-
std::string getComgrInstallPathFromExecutable() {
91-
92-
#if !defined(_WIN32) && !defined(_WIN64)
93-
FILE *ProcMaps = fopen("/proc/self/maps", "r");
94-
if (ProcMaps == NULL)
95-
return "";
96-
97-
char *Line = NULL;
98-
size_t len = 0;
99-
uintptr_t Address = reinterpret_cast<uintptr_t>(getROCMPath);
100-
101-
// TODO: switch POSIX getline() to C++-based getline() once Pytorch resolves
102-
// build issues with libstdc++ ABI
103-
while (getline(&Line, &len, ProcMaps) != -1) {
104-
SmallVector<StringRef, 6> Tokens;
105-
StringRef(Line).split(Tokens, ' ', -1 /* MaxSplit */,
106-
false /* KeepEmpty */);
107-
108-
unsigned long long LowAddress, HighAddress;
109-
if (consumeUnsignedInteger(Tokens[0], 16 /* Radix */, LowAddress)) {
110-
fclose(ProcMaps);
111-
free(Line);
112-
return "";
113-
}
114-
115-
if (!Tokens[0].consume_front("-")) {
116-
fclose(ProcMaps);
117-
free(Line);
118-
return "";
119-
}
120-
121-
if (consumeUnsignedInteger(Tokens[0], 16 /* Radix */, HighAddress)) {
122-
fclose(ProcMaps);
123-
free(Line);
124-
return "";
125-
}
126-
127-
if ((Address >= LowAddress && Address <= HighAddress)) {
128-
StringRef Path = Tokens[5].ltrim();
129-
/* Not a mapped file or File path empty */
130-
if (Tokens[4] == "0" || Path == "") {
131-
fclose(ProcMaps);
132-
free(Line);
133-
return "";
134-
}
135-
136-
std::string rv = StripGNUInstallLibDir(Path).str();
137-
fclose(ProcMaps);
138-
free(Line);
139-
return rv;
140-
}
141-
}
142-
143-
fclose(ProcMaps);
144-
free(Line);
145-
#endif
146-
147-
return "";
72+
llvm::StringRef getLLVMPath() {
73+
static const char *EnvLLVMPath = std::getenv("LLVM_PATH");
74+
return EnvLLVMPath;
14875
}
14976

150-
class InstallationDetector {
151-
public:
152-
InstallationDetector(StringRef ROCmPath, bool isComgrPath)
153-
: ROCmInstallPath(ROCmPath) {}
154-
virtual ~InstallationDetector() = default;
155-
156-
const StringRef getROCmPath() const { return ROCmInstallPath; }
157-
void setROCmInstallPath(StringRef Path) { ROCmInstallPath = Path; }
158-
159-
virtual SmallString<128> getLLVMPathImpl() {
160-
SmallString<128> LLVMPath = getROCmPath();
161-
sys::path::append(LLVMPath, "llvm");
162-
163-
return LLVMPath;
164-
}
165-
166-
virtual SmallString<128> getHIPPathImpl() {
167-
SmallString<128> HIPPath = getROCmPath();
168-
sys::path::append(HIPPath, "hip");
169-
170-
return HIPPath;
171-
}
172-
173-
StringRef getLLVMPath() {
174-
static const char *EnvLLVMPath = std::getenv("LLVM_PATH");
175-
if (EnvLLVMPath) {
176-
return EnvLLVMPath;
177-
}
178-
179-
if (LLVMInstallationPath.empty()) {
180-
LLVMInstallationPath = getLLVMPathImpl();
181-
}
182-
183-
return LLVMInstallationPath;
184-
}
185-
186-
StringRef getHIPPath() {
187-
static const char *EnvHIPPath = std::getenv("HIP_PATH");
188-
if (EnvHIPPath) {
189-
return EnvHIPPath;
190-
}
191-
192-
if (HIPInstallationPath.empty()) {
193-
HIPInstallationPath = getHIPPathImpl();
194-
}
195-
196-
return HIPInstallationPath;
197-
}
198-
199-
SmallString<128> getSiblingDirWithPrefix(StringRef DirName,
200-
StringRef Prefix) {
201-
StringRef ParentDir = sys::path::parent_path(DirName);
202-
std::error_code EC;
203-
204-
for (sys::fs::directory_iterator Dir(ParentDir, EC), DirEnd;
205-
Dir != DirEnd && !EC; Dir.increment(EC)) {
206-
const StringRef Path = sys::path::filename(Dir->path());
207-
if (Path.starts_with(Prefix)) {
208-
return StringRef(Dir->path());
209-
}
210-
}
211-
212-
return SmallString<128>();
213-
}
214-
215-
private:
216-
SmallString<128> ROCmInstallPath;
217-
SmallString<128> HIPInstallationPath;
218-
SmallString<128> LLVMInstallationPath;
219-
};
220-
221-
// If the ROCmInstallPath is Spack based it should be in the format
222-
// rocm-cmake-${rocm-version}-${hash}. Detect corresponding LLVM and HIP
223-
// Paths existing at the same level at ROCM. It should be in the format
224-
// llvm-amdgpu-${rocm-version}-${hash} and hip-${rocm-version}-${hash}.
225-
class SpackInstallationDetector : public InstallationDetector {
226-
public:
227-
SpackInstallationDetector(StringRef Path, bool isComgrPath)
228-
: InstallationDetector(Path, isComgrPath) {
229-
if (isComgrPath) {
230-
auto ROCmInstallPath = getSiblingDirWithPrefix(Path, "rocm-cmake-");
231-
setROCmInstallPath(ROCmInstallPath);
232-
}
233-
}
234-
235-
virtual SmallString<128> getLLVMPathImpl() override {
236-
return getSiblingDirWithPrefix(getROCmPath(), "llvm-amdgpu-");
237-
}
238-
239-
virtual SmallString<128> getHIPPathImpl() override {
240-
return getSiblingDirWithPrefix(getROCmPath(), "hip-");
241-
}
242-
};
243-
244-
std::shared_ptr<InstallationDetector>
245-
CreatePathDetector(StringRef Path, bool isComgrPath = false) {
246-
StringRef DirName = sys::path::filename(Path);
247-
if ((!isComgrPath && DirName.starts_with("rocm-cmake-")) ||
248-
(isComgrPath && DirName.starts_with("comgr-"))) {
249-
return std::make_shared<SpackInstallationDetector>(Path, isComgrPath);
250-
}
251-
252-
return std::make_shared<InstallationDetector>(Path, isComgrPath);
253-
}
254-
255-
std::shared_ptr<InstallationDetector> getDetectorImpl() {
256-
SmallString<128> ROCmInstallPath;
257-
258-
static const char *EnvROCMPath = std::getenv("ROCM_PATH");
259-
if (EnvROCMPath) {
260-
ROCmInstallPath = EnvROCMPath;
261-
}
262-
263-
if (ROCmInstallPath == "") {
264-
std::string ComgrInstallationPath = getComgrInstallPathFromExecutable();
265-
return CreatePathDetector(ComgrInstallationPath, true /* isComgrPath */);
266-
}
267-
return CreatePathDetector(ROCmInstallPath);
268-
}
269-
270-
InstallationDetector *getDetector() {
271-
static auto Detector = getDetectorImpl();
272-
return Detector.get();
273-
}
274-
275-
StringRef getROCMPath() { return getDetector()->getROCmPath(); }
276-
277-
StringRef getHIPPath() { return getDetector()->getHIPPath(); }
278-
279-
StringRef getLLVMPath() { return getDetector()->getLLVMPath(); }
280-
28177
} // namespace env
28278
} // namespace COMGR

amd/comgr/src/comgr-env.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,6 @@ bool shouldEmitVerboseLogs();
5454
/// Return whether the environment requests time statistics collection.
5555
bool needTimeStatistics();
5656

57-
/// If environment variable ROCM_PATH is set, return the environment varaible,
58-
/// otherwise return the default ROCM path.
59-
llvm::StringRef getROCMPath();
60-
61-
/// If environment variable HIP_PATH is set, return the environment variable,
62-
/// otherwise return the default HIP path.
63-
llvm::StringRef getHIPPath();
64-
6557
/// If environment variable LLVM_PATH is set, return the environment variable,
6658
/// otherwise return the default LLVM path.
6759
llvm::StringRef getLLVMPath();

0 commit comments

Comments
 (0)