File: libUtils.h

package info (click to toggle)
rocm-hipamd 6.4.3-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 23,036 kB
  • sloc: cpp: 211,057; ansic: 35,860; sh: 755; python: 623; perl: 275; asm: 166; makefile: 27
file content (438 lines) | stat: -rw-r--r-- 12,277 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/* Copyright (c) 2011 - 2021 Advanced Micro Devices, Inc.

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE. */

#ifndef _CL_LIB_UTILS_0_8_H_
#define _CL_LIB_UTILS_0_8_H_
#include "acl.h"
#include <string>
#include <sstream>
#include <iterator>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include "library.hpp"
#include "utils/bif_section_labels.hpp"
#include "utils/options.hpp"
using namespace bif;

// Utility function to set a flag in option structure
// of the aclDevCaps.
void
setFlag(aclDevCaps *elf, compDeviceCaps option);

// Utility function to flip a flag in option structure
// of the aclDevCaps.
void
flipFlag(aclDevCaps *elf, compDeviceCaps option);

// Utility function to clear a flag in option structure
// of the aclDevCaps.
void
clearFlag(aclDevCaps *elf, compDeviceCaps option);

// Utility function to check that a flag in option structure
// of the aclDevCaps is set.
bool
checkFlag(aclDevCaps *elf, compDeviceCaps option);

// Utility function to initialize and elf device capabilities
void
initElfDeviceCaps(aclBinary *elf);

// Append the string to the aclCompiler log string.
void
appendLogToCL(aclCompiler *cl, const std::string &logStr);

const char *getDeviceName(const aclTargetInfo &target);

// Select the correct library from the target information.
amd::LibrarySelector getLibraryType(const aclTargetInfo *target);

// get family_enum from the target information.
unsigned getFamilyEnum(const aclTargetInfo *target);

// get chip_enum from the target information.
unsigned getChipEnum(const aclTargetInfo *target);

// get isa type name (compute capability) from the target information.
const std::string &getIsaTypeName(const aclTargetInfo *target);

// get isa type  (compute capability) from the target information.
int getIsaType(const aclTargetInfo *target);

// get Feature String for target.
std::string getFeatureString(const aclTargetInfo& target, amd::option::Options *OptionsObj);

// Create a copy of an ELF and duplicate all sections/symbols
aclBinary*
createELFCopy(aclBinary *src);

// Create a BIF2.1 elf from a BIF 2.0 elf
aclBinary*
convertBIF20ToBIF21(aclBinary *src);

// Create a BIF3.0 elf from a BIF 2.0 elf
aclBinary*
convertBIF20ToBIF30(aclBinary *src);

// Create a BIF3.1 elf from a BIF 2.0 elf
aclBinary*
convertBIF20ToBIF31(aclBinary *src);

// Create a BIF2.0 elf from a BIF 2.1 elf
aclBinary*
convertBIF21ToBIF20(aclBinary *src);

// Create a BIF3.0 elf from a BIF 2.1 elf
aclBinary*
convertBIF21ToBIF30(aclBinary *src);

// Create a BIF3.1 elf from a BIF 2.1 elf
aclBinary*
convertBIF21ToBIF31(aclBinary *src);

// Create a BIF2.0 elf from a BIF 3.0 elf
aclBinary*
convertBIF30ToBIF20(aclBinary *src);

// Create a BIF2.1 elf from a BIF 3.0 elf
aclBinary*
convertBIF30ToBIF21(aclBinary *src);

// Create a BIF3.1 elf from a BIF 3.0 elf
aclBinary*
convertBIF30ToBIF31(aclBinary *src);

// Create a BIF2.0 elf from a BIF 3.1 elf
aclBinary*
convertBIF31ToBIF20(aclBinary *src);

// Create a BIF2.1 elf from a BIF 3.1 elf
aclBinary*
convertBIF31ToBIF21(aclBinary *src);

// Create a BIF3.0 elf from a BIF 3.1 elf
aclBinary*
convertBIF31ToBIF30(aclBinary *src);

// get a pointer to the aclBIF irrespective of the
// binary version.
aclBIF*
aclutGetBIF(aclBinary*);

// Get a pointer to the aclOptions irrespective of
// the binary version.
aclOptions*
aclutGetOptions(aclBinary*);

// Get a pointer to the aclBinaryOptions struct
// irrespective of the binary version.
aclBinaryOptions*
aclutGetBinOpts(aclBinary*);

// Get a pointer to the target info struct
// irrespective of the binary version.
aclTargetInfo*
aclutGetTargetInfo(aclBinary*);

// Get a pointer to the device caps
// irrespective of the binary version.
aclDevCaps*
aclutGetCaps(aclBinary*);

// Copy two binary option structures irrespective
// of the binary version and uses defaults when
// things don't match up.
void
aclutCopyBinOpts(aclBinaryOptions *dst,
    const aclBinaryOptions *src,
    bool is64bit);

// Retrieve kernel statistics from binary
// and insert to elf as symbol
acl_error aclutInsertKernelStatistics(aclCompiler*, aclBinary*);

// Returns target chip name.
std::string aclutGetCodegenName(const aclTargetInfo &tgtInfo);

// Helper function that returns the
// allocation function from the binary.
AllocFunc
aclutAlloc(const aclBinary *bin);

// Helper function that returns the
// de-allocation function from the binary.
FreeFunc
aclutFree(const aclBinary *bin);


// Helper function that returns the
// allocation function from the compiler.
AllocFunc
aclutAlloc(const aclCompiler *bin);

// Helper function that returns the
// de-allocation function from the compiler.
FreeFunc
aclutFree(const aclCompiler *bin);

// Helper function that returns the
// allocation function from the compiler options.
AllocFunc
aclutAlloc(const aclCompilerOptions *bin);

// Helper function that returns the
// de-allocation function from the compiler options.
FreeFunc
aclutFree(const aclCompilerOptions *bin);

inline std::vector<std::string> splitSpaceSeparatedString(char *str)
{
  std::string s(str);
  std::stringstream ss(s);
  std::istream_iterator<std::string> beg(ss), end;
  std::vector<std::string> vec(beg, end);
  return vec;
}

// Helper function that returns OpenCL mangled kernel name.
inline std::string
aclutOpenclMangledKernelName(const std::string& kernel_name)
{
  const oclBIFSymbolStruct* sym = findBIF30SymStruct(symOpenclKernel);
  assert(sym && "symbol not found");
  return std::string("&") + sym->str[PRE] + kernel_name + sym->str[POST];
}

// Helper function that returns OpenCL mangled kernel metadata symbol name.
inline std::string
aclutOpenclMangledKernelMetadataName(const std::string& kernel_name)
{
  const oclBIFSymbolStruct* sym = findBIF30SymStruct(symOpenclMeta);
  assert(sym && "symbol not found");
  return sym->str[PRE] + aclutOpenclMangledKernelName(kernel_name) + sym->str[POST];
}

#ifdef WITH_TARGET_HSAIL
// Helper function that updates metadata for all the kernels in binary;
// the updated attribute is the number of hidden kernel arguments.
inline acl_error
aclutUpdateMetadataWithHiddenKernargsNum(aclCompiler* cl, aclBinary* bin, uint32_t num) {
  if (num == MAX_HIDDEN_KERNARGS_NUM) {
    return ACL_SUCCESS;
  }
  const oclBIFSymbolStruct* sym = findBIF30SymStruct(symOpenclMeta);
  assert(sym && "symbol not found");
  aclSections secID = sym->sections[0];
  size_t kernelNamesSize = 0;
  acl_error error_code = aclQueryInfo(cl, bin, RT_KERNEL_NAMES, NULL, NULL, &kernelNamesSize);
  if (error_code != ACL_SUCCESS) {
    return error_code;
  }
  char* kernelNames = new char[kernelNamesSize];
  error_code = aclQueryInfo(cl, bin, RT_KERNEL_NAMES, NULL, kernelNames, &kernelNamesSize);
  if (error_code != ACL_SUCCESS) {
    delete[] kernelNames;
    return error_code;
  }
  std::vector<std::string> vKernels = splitSpaceSeparatedString(kernelNames);
  delete[] kernelNames;
  size_t roSize = 0;
  for (auto it = vKernels.begin(); it != vKernels.end(); ++it) {
    std::string symbol = aclutOpenclMangledKernelMetadataName(*it);
    void* roSec = const_cast<void*>(aclExtractSymbol(cl, bin, &roSize, secID, symbol.c_str(), &error_code));
    if (error_code != ACL_SUCCESS) {
      return error_code;
    }
    if (!roSec || roSize == 0) {
      error_code = ACL_ELF_ERROR;
      return error_code;
    }
    aclMetadata *md = reinterpret_cast<aclMetadata*>(roSec);
    md->numHiddenKernelArgs = num;
    error_code = aclRemoveSymbol(cl, bin, secID, symbol.c_str());
    if (error_code != ACL_SUCCESS) {
      return error_code;
    }
    error_code = aclInsertSymbol(cl, bin, md, roSize, secID, symbol.c_str());
    if (error_code != ACL_SUCCESS) {
      return error_code;
    }
  }
  return error_code;
}
#endif

struct _target_mappings_rec;
typedef _target_mappings_rec TargetMapping;

// Returns the TargetMapping for the specific target device.
const TargetMapping& getTargetMapping(const aclTargetInfo &target);

inline bool is64BitTarget(const aclTargetInfo& target)
{
  return (target.arch_id == aclX64 ||
          target.arch_id == aclAMDIL64 ||
          target.arch_id == aclHSAIL64);
}

inline bool isCpuTarget(const aclTargetInfo& target)
{
  return (target.arch_id == aclX64 || target.arch_id == aclX86);
}

inline bool isGpuTarget(const aclTargetInfo& target)
{
  return (target.arch_id == aclAMDIL || target.arch_id == aclAMDIL64 ||
          target.arch_id == aclHSAIL || target.arch_id == aclHSAIL64);
}

inline bool isAMDILTarget(const aclTargetInfo& target)
{
  return (target.arch_id == aclAMDIL || target.arch_id == aclAMDIL64);
}

inline bool isHSAILTarget(const aclTargetInfo& target)
{
  return (target.arch_id == aclHSAIL || target.arch_id == aclHSAIL64);
}

const std::string& getLegacyLibName();

inline bool isValidTarget(const aclTargetInfo& target)
{
  return (target.arch_id && target.chip_id);
}

bool isChipSupported(const aclTargetInfo& target);

enum scId {
  SC_AMDIL = 0,
  SC_HSAIL = 0,
  SC_LAST,
};

// Helper function that allocates an aligned memory.
inline void*
alignedMalloc(size_t size, size_t alignment)
{
#if defined(_WIN32)
  return ::_aligned_malloc(size, alignment);
#else
  void * ptr = NULL;
  if (0 == ::posix_memalign(&ptr, alignment, size)) {
    return ptr;
  }
  return NULL;
#endif
}

// Helper function that frees an aligned memory.
inline void
alignedFree(void *ptr)
{
#if defined(_WIN32)
  ::_aligned_free(ptr);
#else
  free(ptr);
#endif
}

#if defined(_WIN32)
inline void convertLongAbsFilePathIfNeeded(std::string &filename)
{
  if (filename.empty()) {
    return;
  }
  std::wstring ws(filename.begin(), filename.end());
  wchar_t abs_path[_MAX_ENV];
  _wfullpath(abs_path, ws.c_str(), _MAX_ENV);
  std::wstring ws_abs = std::wstring(abs_path);
  if (ws_abs.size() >= _MAX_PATH) {
    std::string s(ws_abs.begin(), ws_abs.end());
    filename = "\\\\?\\" + s;
  }
}
#endif

inline char* readFile(std::string source_filename, size_t& size)
{
#if defined(_WIN32)
  convertLongAbsFilePathIfNeeded(source_filename);
#endif
  FILE *fp = ::fopen( source_filename.c_str(), "rb" );
  unsigned int length;
  size_t offset = 0;
  char *ptr;
  if (!fp) {
    return NULL;
  }
  // obtain file size
  ::fseek (fp , 0 , SEEK_END);
  length = ::ftell (fp);
  ::rewind (fp);
  ptr = reinterpret_cast<char*>(::malloc(offset + length + 1));
  if (length != fread(&ptr[offset], 1, length, fp))
  {
    ::free(ptr);
    ::fclose(fp);
    return NULL;
  }
  ptr[offset + length] = '\0';
  size = offset + length;
  ::fclose(fp);
  return ptr;
}

inline bool writeFile(std::string source_filename, const char *source, size_t size)
{
#if defined(_WIN32)
  convertLongAbsFilePathIfNeeded(source_filename);
#endif
  FILE *fp = ::fopen(source_filename.c_str(), "wb");
  if (!fp) {
    return EXIT_FAILURE;
  }
  if (!::fwrite(source, size, 1, fp)) {
    ::fclose(fp);
    return EXIT_FAILURE;
  }
  ::fclose(fp);
  return EXIT_SUCCESS;
}

#if !defined(BCMAG)
#define BCMAG  "BC"
#define SBCMAG 2
#endif
// Helper predicate returns true if p starts with bit code signature.
// TODO: Move it into Compiler Lib back in new 1_0 API
inline static bool
isBcMagic(const char* p)
{
    if (p==NULL || strncmp(p, BCMAG, SBCMAG) != 0) {
        return false;
    }
    return true;
}

void dump(aclBinary *bin);

#endif // _CL_LIB_UTILS_0_8_H_