@@ -69,214 +69,10 @@ bool shouldEmitVerboseLogs() {
69
69
return VerboseLogs && StringRef (VerboseLogs) != " 0" ;
70
70
}
71
71
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;
148
75
}
149
76
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
-
281
77
} // namespace env
282
78
} // namespace COMGR
0 commit comments