Repository navigation
Expand file tree
/
Copy pathadd.cpp
More file actions
144 lines (120 loc) · 4.6 KB
/
Copy pathadd.cpp
File metadata and controls
144 lines (120 loc) · 4.6 KB
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
#include <hipFatbin.h>
#include <algorithm>
#include <cstring>
#include "handle.hpp"
#include "offload.hpp"
extern "C" {
hipFatbinResult hipFatbinAddCubin(hipFatbinHandle handle, const void *code,
size_t size, const char *arch,
const char *identifier) {
if (handle == nullptr || code == nullptr || arch == nullptr) {
return HIPFATBIN_ERROR_NULL_POINTER;
}
if (size == 0) {
return HIPFATBIN_ERROR_EMPTY_INPUT;
}
// Bare minimum validation of ELF
if(std::memcmp(reinterpret_cast<const char*>(code) + 1, "ELF", 3) != 0) {
return HIPFATBIN_ERROR_UNSUPPORTED_ACTION;
}
fatbin::BinContainer c;
c.type_ = fatbin::BinaryType::BINARY;
c.arch_ = std::string{arch};
c.name_ = std::string{identifier};
c.bin_ = std::vector<unsigned char>((unsigned char *)code,
((unsigned char *)code) + size);
auto h = reinterpret_cast<fatbin::Handle *>(handle);
h->built_ = false;
h->inputs_.emplace_back(std::move(c));
return HIPFATBIN_SUCCESS;
}
hipFatbinResult hipFatbinAddSPIRV(hipFatbinHandle handle, const char *code,
size_t size, const char *identifier) {
if (handle == nullptr || code == nullptr) {
return HIPFATBIN_ERROR_NULL_POINTER;
}
if (size == 0) {
return HIPFATBIN_ERROR_EMPTY_INPUT;
}
fatbin::BinContainer c;
c.type_ = fatbin::BinaryType::SPIRV;
c.arch_ = std::string{"amdgcnspirv"};
c.name_ = std::string{identifier};
c.bin_ = std::vector<unsigned char>((unsigned char *)code,
((unsigned char *)code) + size);
c.opts_ = "";
auto h = reinterpret_cast<fatbin::Handle *>(handle);
h->built_ = false;
h->inputs_.emplace_back(std::move(c));
return HIPFATBIN_SUCCESS;
}
hipFatbinResult hipFatbinAddFatbin(hipFatbinHandle handle, const void *code,
size_t size) {
if (handle == nullptr || code == nullptr) {
return HIPFATBIN_ERROR_NULL_POINTER;
}
if (size == 0) {
return HIPFATBIN_ERROR_EMPTY_INPUT;
}
auto h = reinterpret_cast<fatbin::Handle *>(handle);
h->built_ = false;
// Unpack the fabin and insert each of its code objects
auto *header = reinterpret_cast<const __ClangOffloadBundleHeader *>(code);
std::string m(header->magic, CLANG_OFFLOAD_BUNDLER_MAGIC_STR.size());
if (m != CLANG_OFFLOAD_BUNDLER_MAGIC_STR) {
return HIPFATBIN_ERROR_INVALID_FATBIN; // magic string missing
}
const auto *desc = &header->desc[0];
for (size_t i = 0; i < header->numOfCodeObjects;
++i, desc = reinterpret_cast<const __ClangOffloadBundleInfo *>(
reinterpret_cast<uintptr_t>(&desc->bundleEntryId[0]) +
desc->bundleEntryIdSize)) {
const void *image = reinterpret_cast<const void *>(
reinterpret_cast<uintptr_t>(header) + desc->offset);
const size_t image_size = desc->size;
std::string bundleEntryId{desc->bundleEntryId, desc->bundleEntryIdSize};
// Handle the host bundle
if (bundleEntryId.find("host-x86_64") != std::string::npos) {
fatbin::BinContainer c;
c.type_ = fatbin::BinaryType::HOST;
c.arch_ = bundleEntryId;
c.name_ = "";
c.bin_ = std::vector<unsigned char>(
(unsigned char *)image, ((unsigned char *)image) + image_size);
h->inputs_.emplace_back(std::move(c));
continue;
}
fatbin::BinContainer c;
if (bundleEntryId.find("amdgcnspirv") != std::string::npos) {
c.type_ = fatbin::BinaryType::SPIRV;
c.arch_ = std::string{"amdgcnspirv"};
} else {
constexpr std::string_view triple = "hipv4-amdgcn-amd-amdhsa--";
c.type_ = fatbin::BinaryType::BINARY;
c.arch_ = bundleEntryId.substr(triple.size());
}
c.name_ = "";
c.bin_ = std::vector<unsigned char>((unsigned char *)image,
((unsigned char *)image) + image_size);
h->inputs_.emplace_back(std::move(c));
}
return HIPFATBIN_SUCCESS;
}
hipFatbinResult hipFatbinRemoveArch(hipFatbinHandle handle, const char *arch) {
if (handle == nullptr) {
return HIPFATBIN_ERROR_NULL_POINTER;
}
if (arch == nullptr) {
return HIPFATBIN_ERROR_EMPTY_INPUT;
}
auto h = reinterpret_cast<fatbin::Handle *>(handle);
h->built_ = false;
auto count = std::erase_if(
h->inputs_, [&](const auto &i) { return i.arch_ == std::string(arch); });
return count == 1 ? HIPFATBIN_SUCCESS : HIPFATBIN_ERROR_NOT_FOUND;
}
hipFatbinResult hipFatbinAddReloc(hipFatbinHandle /* handle */,
const void * /* code */, size_t /* size */) {
return HIPFATBIN_ERROR_UNSUPPORTED_ACTION;
}
}