1 | /* |
---|
2 | * CLRadeonExtender - Unofficial OpenCL Radeon Extensions Library |
---|
3 | * Copyright (C) 2014-2018 Mateusz Szpakowski |
---|
4 | * |
---|
5 | * This library is free software; you can redistribute it and/or |
---|
6 | * modify it under the terms of the GNU Lesser General Public |
---|
7 | * License as published by the Free Software Foundation; either |
---|
8 | * version 2.1 of the License, or (at your option) any later version. |
---|
9 | * |
---|
10 | * This library is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
13 | * Lesser General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU Lesser General Public |
---|
16 | * License along with this library; if not, write to the Free Software |
---|
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
---|
18 | */ |
---|
19 | |
---|
20 | #include <CLRX/Config.h> |
---|
21 | #include <cassert> |
---|
22 | #include <cstdio> |
---|
23 | #include <cstring> |
---|
24 | #include <cstdint> |
---|
25 | #include <string> |
---|
26 | #include <vector> |
---|
27 | #include <algorithm> |
---|
28 | #include <utility> |
---|
29 | #include <CLRX/amdbin/ElfBinaries.h> |
---|
30 | #include <CLRX/utils/Utilities.h> |
---|
31 | #include <CLRX/utils/MemAccess.h> |
---|
32 | #include <CLRX/utils/InputOutput.h> |
---|
33 | #include <CLRX/utils/Containers.h> |
---|
34 | #include <CLRX/amdbin/ROCmBinaries.h> |
---|
35 | |
---|
36 | using namespace CLRX; |
---|
37 | |
---|
38 | /* |
---|
39 | * ROCm metadata YAML parser |
---|
40 | */ |
---|
41 | |
---|
42 | // return trailing spaces |
---|
43 | static size_t skipSpacesAndComments(const char*& ptr, const char* end, size_t& lineNo) |
---|
44 | { |
---|
45 | const char* lineStart = ptr; |
---|
46 | while (ptr != end) |
---|
47 | { |
---|
48 | lineStart = ptr; |
---|
49 | while (ptr != end && *ptr!='\n' && isSpace(*ptr)) ptr++; |
---|
50 | if (ptr == end) |
---|
51 | break; // end of stream |
---|
52 | if (*ptr=='#') |
---|
53 | { |
---|
54 | // skip comment |
---|
55 | while (ptr != end && *ptr!='\n') ptr++; |
---|
56 | if (ptr == end) |
---|
57 | return 0; // no trailing spaces and end |
---|
58 | } |
---|
59 | else if (*ptr!='\n') |
---|
60 | break; // no comment and no end of line |
---|
61 | else |
---|
62 | lineNo++; // next line |
---|
63 | } |
---|
64 | return lineStart - ptr; |
---|
65 | } |
---|
66 | |
---|
67 | static inline void skipSpacesToEnd(const char*& ptr, const char* end) |
---|
68 | { |
---|
69 | while (ptr != end && *ptr!='\n' && isSpace(*ptr)) ptr++; |
---|
70 | } |
---|
71 | |
---|
72 | static size_t parseYAMLKey(const char*& ptr, const char* end, size_t lineNo, |
---|
73 | size_t keywordsNum, const char** keywords) |
---|
74 | { |
---|
75 | const char* keyPtr = ptr; |
---|
76 | while (ptr != end && (isAlnum(*ptr) || *ptr=='_')) ptr++; |
---|
77 | if (keyPtr == end) |
---|
78 | throw ParseException(lineNo, "Expected key name"); |
---|
79 | const char* keyEnd = ptr; |
---|
80 | while (ptr != end && *ptr!='\n' && isSpace(*ptr)) ptr++; |
---|
81 | if (ptr == end || *ptr==':') |
---|
82 | throw ParseException(lineNo, "Expected colon"); |
---|
83 | ptr++; |
---|
84 | const char* afterColon = ptr; |
---|
85 | skipSpacesToEnd(ptr, end); |
---|
86 | if (afterColon == ptr) |
---|
87 | throw ParseException("After key and colon must be space"); |
---|
88 | CString keyword(keyPtr, keyEnd); |
---|
89 | const size_t index = binaryFind(keywords, keywords+keywordsNum, |
---|
90 | keyword.c_str(), CStringLess()) - keywords; |
---|
91 | if (index == keywordsNum) |
---|
92 | throw ParseException(lineNo, "Unknown keyword"); |
---|
93 | return index; |
---|
94 | } |
---|
95 | |
---|
96 | template<typename T> |
---|
97 | static T parseYAMLIntValue(const char*& ptr, const char* end, size_t lineNo) |
---|
98 | { |
---|
99 | skipSpacesToEnd(ptr, end); |
---|
100 | if (ptr == end || *ptr=='\n') |
---|
101 | throw ParseException(lineNo, "Expected integer value"); |
---|
102 | try |
---|
103 | { return cstrtovCStyle<T>(ptr, end, ptr); } |
---|
104 | catch(const ParseException& ex) |
---|
105 | { throw ParseException(lineNo, ex.what()); } |
---|
106 | } |
---|
107 | |
---|
108 | static bool parseYAMLBoolValue(const char*& ptr, const char* end, size_t lineNo) |
---|
109 | { |
---|
110 | skipSpacesToEnd(ptr, end); |
---|
111 | if (ptr == end || *ptr=='\n') |
---|
112 | throw ParseException(lineNo, "Expected boolean value"); |
---|
113 | |
---|
114 | const char* wordPtr = ptr; |
---|
115 | while(ptr == end && isAlnum(*ptr)) ptr++; |
---|
116 | CString word(wordPtr, ptr); |
---|
117 | |
---|
118 | for (const char* v: { "1", "true", "t", "on", "yes", "y"}) |
---|
119 | if (::strcasecmp(word.c_str(), v) == 0) |
---|
120 | return true; |
---|
121 | for (const char* v: { "0", "false", "f", "off", "no", "n"}) |
---|
122 | if (::strcasecmp(word.c_str(), v) == 0) |
---|
123 | return false; |
---|
124 | throw ParseException(lineNo, "Is not boolean value"); |
---|
125 | } |
---|
126 | |
---|
127 | static std::string trimStrSpaces(const std::string& str) |
---|
128 | { |
---|
129 | size_t i = 0; |
---|
130 | const size_t sz = str.size(); |
---|
131 | while (i!=sz && isSpace(str[i])) i++; |
---|
132 | if (i == sz) return ""; |
---|
133 | size_t j = sz-1; |
---|
134 | while (j>i && isSpace(str[j])) j--; |
---|
135 | return str.substr(i, j-i+1); |
---|
136 | } |
---|
137 | |
---|
138 | static std::string parseYAMLString(const char*& linePtr, const char* end, |
---|
139 | size_t& lineNo) |
---|
140 | { |
---|
141 | std::string strarray; |
---|
142 | if (linePtr == end || (*linePtr != '"' && *linePtr != '\'')) |
---|
143 | { |
---|
144 | while (linePtr != end && !isSpace(*linePtr) && *linePtr != ',') linePtr++; |
---|
145 | throw ParseException(lineNo, "Expected string"); |
---|
146 | } |
---|
147 | const char termChar = *linePtr; |
---|
148 | linePtr++; |
---|
149 | |
---|
150 | // main loop, where is character parsing |
---|
151 | while (linePtr != end && *linePtr != termChar) |
---|
152 | { |
---|
153 | if (*linePtr == '\\') |
---|
154 | { |
---|
155 | // escape |
---|
156 | linePtr++; |
---|
157 | uint16_t value; |
---|
158 | if (linePtr == end) |
---|
159 | throw ParseException(lineNo, "Unterminated character of string"); |
---|
160 | if (*linePtr == 'x') |
---|
161 | { |
---|
162 | // hex literal |
---|
163 | linePtr++; |
---|
164 | if (linePtr == end) |
---|
165 | throw ParseException(lineNo, "Unterminated character of string"); |
---|
166 | value = 0; |
---|
167 | if (isXDigit(*linePtr)) |
---|
168 | for (; linePtr != end; linePtr++) |
---|
169 | { |
---|
170 | cxuint digit; |
---|
171 | if (*linePtr >= '0' && *linePtr <= '9') |
---|
172 | digit = *linePtr-'0'; |
---|
173 | else if (*linePtr >= 'a' && *linePtr <= 'f') |
---|
174 | digit = *linePtr-'a'+10; |
---|
175 | else if (*linePtr >= 'A' && *linePtr <= 'F') |
---|
176 | digit = *linePtr-'A'+10; |
---|
177 | else |
---|
178 | break; |
---|
179 | value = (value<<4) + digit; |
---|
180 | } |
---|
181 | else |
---|
182 | throw ParseException(lineNo, "Expected hexadecimal character code"); |
---|
183 | value &= 0xff; |
---|
184 | } |
---|
185 | else if (isODigit(*linePtr)) |
---|
186 | { |
---|
187 | // octal literal |
---|
188 | value = 0; |
---|
189 | for (cxuint i = 0; linePtr != end && i < 3; i++, linePtr++) |
---|
190 | { |
---|
191 | if (!isODigit(*linePtr)) |
---|
192 | break; |
---|
193 | value = (value<<3) + uint64_t(*linePtr-'0'); |
---|
194 | // checking range |
---|
195 | if (value > 255) |
---|
196 | throw ParseException(lineNo, "Octal code out of range"); |
---|
197 | } |
---|
198 | } |
---|
199 | else |
---|
200 | { |
---|
201 | // normal escapes |
---|
202 | const char c = *linePtr++; |
---|
203 | switch (c) |
---|
204 | { |
---|
205 | case 'a': |
---|
206 | value = '\a'; |
---|
207 | break; |
---|
208 | case 'b': |
---|
209 | value = '\b'; |
---|
210 | break; |
---|
211 | case 'r': |
---|
212 | value = '\r'; |
---|
213 | break; |
---|
214 | case 'n': |
---|
215 | value = '\n'; |
---|
216 | break; |
---|
217 | case 'f': |
---|
218 | value = '\f'; |
---|
219 | break; |
---|
220 | case 'v': |
---|
221 | value = '\v'; |
---|
222 | break; |
---|
223 | case 't': |
---|
224 | value = '\t'; |
---|
225 | break; |
---|
226 | case '\\': |
---|
227 | value = '\\'; |
---|
228 | break; |
---|
229 | case '\'': |
---|
230 | value = '\''; |
---|
231 | break; |
---|
232 | case '\"': |
---|
233 | value = '\"'; |
---|
234 | break; |
---|
235 | default: |
---|
236 | value = c; |
---|
237 | } |
---|
238 | } |
---|
239 | strarray.push_back(value); |
---|
240 | } |
---|
241 | else // regular character |
---|
242 | { |
---|
243 | if (*linePtr=='\n') |
---|
244 | lineNo++; |
---|
245 | strarray.push_back(*linePtr++); |
---|
246 | } |
---|
247 | } |
---|
248 | if (linePtr == end) |
---|
249 | throw ParseException(lineNo, "Unterminated string"); |
---|
250 | linePtr++; |
---|
251 | return strarray; |
---|
252 | } |
---|
253 | |
---|
254 | static std::string parseYAMLStringValue(const char*& ptr, const char* end, size_t& lineNo, |
---|
255 | cxuint prevIndent) |
---|
256 | { |
---|
257 | skipSpacesToEnd(ptr, end); |
---|
258 | if (ptr == end) |
---|
259 | return ""; |
---|
260 | if (*ptr=='"' || *ptr== '\'') |
---|
261 | return parseYAMLString(ptr, end, lineNo); |
---|
262 | |
---|
263 | // otherwise parse stream |
---|
264 | if (*ptr == '|' || *ptr == '>') |
---|
265 | { |
---|
266 | // multiline |
---|
267 | bool newLineFold = *ptr=='>'; |
---|
268 | while (ptr != end && *ptr!='\n') ptr++; |
---|
269 | if (ptr == end) |
---|
270 | return ""; // end |
---|
271 | lineNo++; |
---|
272 | ptr++; // skip newline |
---|
273 | const char* lineStart = ptr; |
---|
274 | skipSpacesToEnd(ptr, end); |
---|
275 | size_t indent = ptr - lineStart; |
---|
276 | if (indent <= prevIndent) |
---|
277 | throw ParseException(lineNo, "Unindented string block"); |
---|
278 | |
---|
279 | std::string buf; |
---|
280 | while(ptr != end) |
---|
281 | { |
---|
282 | const char* strStart = ptr; |
---|
283 | while (ptr != end && *ptr!='\n') ptr++; |
---|
284 | buf.append(strStart, end); |
---|
285 | |
---|
286 | lineNo++; |
---|
287 | const char* lineStart = ptr; |
---|
288 | skipSpacesToEnd(ptr, end); |
---|
289 | if (size_t(ptr - lineStart) < indent) |
---|
290 | { |
---|
291 | if (ptr == end || *ptr=='\n') |
---|
292 | { |
---|
293 | if (!newLineFold) |
---|
294 | buf.append("\n"); |
---|
295 | continue; |
---|
296 | } |
---|
297 | // if smaller indent |
---|
298 | ptr = lineStart; |
---|
299 | return buf; |
---|
300 | } |
---|
301 | ptr = lineStart + indent; |
---|
302 | if (!newLineFold) |
---|
303 | buf.append("\n"); |
---|
304 | } |
---|
305 | return buf; |
---|
306 | } |
---|
307 | const char* strStart = ptr; |
---|
308 | while (ptr != end && *ptr!='\n') ptr++; |
---|
309 | return std::string(strStart, ptr); |
---|
310 | } |
---|
311 | |
---|
312 | class CLRX_INTERNAL YAMLElemConsumer |
---|
313 | { |
---|
314 | public: |
---|
315 | virtual void consume(const char*& ptr, const char* end, size_t& lineNo, |
---|
316 | cxuint prevIndent) = 0; |
---|
317 | }; |
---|
318 | |
---|
319 | static void parseYAMLValArray(const char*& ptr, const char* end, size_t& lineNo, |
---|
320 | size_t prevIndent, YAMLElemConsumer* elemConsumer) |
---|
321 | { |
---|
322 | skipSpacesToEnd(ptr, end); |
---|
323 | if (ptr == end) |
---|
324 | return; |
---|
325 | |
---|
326 | if (*ptr == '[') |
---|
327 | { |
---|
328 | ptr++; |
---|
329 | skipSpacesAndComments(ptr, end, lineNo); |
---|
330 | while (ptr != end) |
---|
331 | { |
---|
332 | // parse in line |
---|
333 | elemConsumer->consume(ptr, end, lineNo, 0); |
---|
334 | if (ptr!=end && *ptr==',') |
---|
335 | throw ParseException(lineNo, "Expected ','"); |
---|
336 | else if (ptr!=end && *ptr==']') |
---|
337 | // just end |
---|
338 | break; |
---|
339 | ptr++; |
---|
340 | skipSpacesAndComments(ptr, end, lineNo); |
---|
341 | } |
---|
342 | if (ptr == end) |
---|
343 | throw ParseException(lineNo, "Unterminated array"); |
---|
344 | ptr++; |
---|
345 | return; |
---|
346 | } |
---|
347 | // sequence |
---|
348 | size_t oldLineNo = lineNo; |
---|
349 | size_t indent0 = skipSpacesAndComments(ptr, end, lineNo); |
---|
350 | if (ptr == end || lineNo == oldLineNo) |
---|
351 | throw ParseException(lineNo, "Expected sequence of values"); |
---|
352 | |
---|
353 | if (indent0 < prevIndent) |
---|
354 | throw ParseException(lineNo, "Unindented sequence of objects"); |
---|
355 | |
---|
356 | while (ptr != end) |
---|
357 | { |
---|
358 | if (*ptr != '-') |
---|
359 | throw ParseException(lineNo, "No '-' before element value"); |
---|
360 | ptr++; |
---|
361 | const char* afterMinus = ptr; |
---|
362 | skipSpacesToEnd(ptr, end); |
---|
363 | if (afterMinus == ptr) |
---|
364 | throw ParseException(lineNo, "No spaces after '-'"); |
---|
365 | elemConsumer->consume(ptr, end, lineNo, indent0+1 + afterMinus-ptr); |
---|
366 | |
---|
367 | size_t indent = skipSpacesAndComments(ptr, end, lineNo); |
---|
368 | if (indent < indent0) |
---|
369 | { |
---|
370 | // if parent level |
---|
371 | ptr -= indent; |
---|
372 | break; |
---|
373 | } |
---|
374 | if (indent != indent0) |
---|
375 | throw ParseException(lineNo, "Wrong indentation of element"); |
---|
376 | } |
---|
377 | } |
---|
378 | |
---|
379 | template<typename T> |
---|
380 | class CLRX_INTERNAL YAMLIntArrayConsumer: public YAMLElemConsumer |
---|
381 | { |
---|
382 | private: |
---|
383 | size_t elemsNum; |
---|
384 | size_t requiredElemsNum; |
---|
385 | public: |
---|
386 | T* array; |
---|
387 | |
---|
388 | YAMLIntArrayConsumer(size_t reqElemsNum, T* _array) |
---|
389 | : elemsNum(0), requiredElemsNum(reqElemsNum), array(_array) |
---|
390 | { } |
---|
391 | |
---|
392 | virtual void consume(const char*& ptr, const char* end, size_t& lineNo, |
---|
393 | cxuint prevIndent) |
---|
394 | { |
---|
395 | if (elemsNum == requiredElemsNum) |
---|
396 | throw ParseException(lineNo, "Too many elements"); |
---|
397 | try |
---|
398 | { array[elemsNum] = cstrtovCStyle<T>(ptr, end, ptr); } |
---|
399 | catch(const ParseException& ex) |
---|
400 | { throw ParseException(lineNo, ex.what()); } |
---|
401 | elemsNum++; |
---|
402 | } |
---|
403 | }; |
---|
404 | |
---|
405 | // printf info string consumer |
---|
406 | |
---|
407 | class CLRX_INTERNAL YAMLPrintfVectorConsumer: public YAMLElemConsumer |
---|
408 | { |
---|
409 | public: |
---|
410 | std::vector<ROCmPrintfInfo>& printfInfos; |
---|
411 | |
---|
412 | YAMLPrintfVectorConsumer(std::vector<ROCmPrintfInfo>& _printInfos) |
---|
413 | : printfInfos(_printInfos) |
---|
414 | { } |
---|
415 | |
---|
416 | virtual void consume(const char*& ptr, const char* end, size_t& lineNo, |
---|
417 | cxuint prevIndent) |
---|
418 | { |
---|
419 | std::string str = parseYAMLStringValue(ptr, end, lineNo, prevIndent); |
---|
420 | // parse printf string |
---|
421 | ROCmPrintfInfo printfInfo{}; |
---|
422 | |
---|
423 | const char* ptr2 = str.c_str(); |
---|
424 | const char* end2 = str.c_str() + str.size(); |
---|
425 | skipSpacesToEnd(ptr2, end2); |
---|
426 | printfInfo.id = cstrtovCStyle<uint32_t>(ptr2, end2, ptr2); |
---|
427 | skipSpacesToEnd(ptr2, end2); |
---|
428 | if (ptr2==end || *ptr2!=':') |
---|
429 | throw ParseException(lineNo, "No colon after printf callId"); |
---|
430 | ptr2++; |
---|
431 | skipSpacesToEnd(ptr2, end2); |
---|
432 | uint32_t argsNum = cstrtovCStyle<uint32_t>(ptr2, end2, ptr2); |
---|
433 | skipSpacesToEnd(ptr2, end2); |
---|
434 | if (ptr2==end || *ptr2!=':') |
---|
435 | throw ParseException(lineNo, "No colon after printf argsNum"); |
---|
436 | ptr2++; |
---|
437 | |
---|
438 | printfInfo.argSizes.resize(argsNum); |
---|
439 | |
---|
440 | // parse arg sizes |
---|
441 | for (size_t i = 0; i < argsNum; i++) |
---|
442 | { |
---|
443 | skipSpacesToEnd(ptr2, end2); |
---|
444 | printfInfo.argSizes[i] = cstrtovCStyle<uint32_t>(ptr2, end2, ptr2); |
---|
445 | skipSpacesToEnd(ptr2, end2); |
---|
446 | if (ptr2==end || *ptr2!=':') |
---|
447 | throw ParseException(lineNo, "No colon after printf argsNum"); |
---|
448 | ptr2++; |
---|
449 | } |
---|
450 | // format |
---|
451 | printfInfo.format.assign(ptr2, end2); |
---|
452 | } |
---|
453 | }; |
---|
454 | |
---|
455 | enum { |
---|
456 | ROCMMT_MAIN_KERNELS = 0, ROCMMT_MAIN_PRINTF, ROCMMT_MAIN_VERSION |
---|
457 | }; |
---|
458 | |
---|
459 | static const char* mainMetadataKeywords[] = |
---|
460 | { |
---|
461 | "Kernels", "Printf", "Version" |
---|
462 | }; |
---|
463 | |
---|
464 | static const size_t mainMetadataKeywordsNum = |
---|
465 | sizeof(mainMetadataKeywords) / sizeof(const char*); |
---|
466 | |
---|
467 | enum { |
---|
468 | ROCMMT_KERNEL_ARGS = 0, ROCMMT_KERNEL_ATTRS, ROCMMT_KERNEL_CODEPROPS, |
---|
469 | ROCMMT_KERNEL_LANGUAGE, ROCMMT_KERNEL_LANGUAGE_VERSION, |
---|
470 | ROCMMT_KERNEL_NAME, ROCMMT_KERNEL_SYMBOLNAME |
---|
471 | }; |
---|
472 | |
---|
473 | static const char* kernelMetadataKeywords[] = |
---|
474 | { |
---|
475 | "Args", "Attrs", "CodeProps", "Language", "LanguageVersion", "Name", "SymbolName" |
---|
476 | }; |
---|
477 | |
---|
478 | static const size_t kernelMetadataKeywordsNum = |
---|
479 | sizeof(kernelMetadataKeywords) / sizeof(const char*); |
---|
480 | |
---|
481 | enum { |
---|
482 | ROCMMT_ATTRS_REQD_WORK_GROUP_SIZE = 0, ROCMMT_ATTRS_RUNTIME_HANDLE, |
---|
483 | ROCMMT_ATTRS_VECTYPEHINT, ROCMMT_ATTRS_WORK_GROUP_SIZE_HINT |
---|
484 | }; |
---|
485 | |
---|
486 | static const char* kernelAttrMetadataKeywords[] = |
---|
487 | { |
---|
488 | "ReqdWorkGroupSize", "RuntimeHandle", "VecTypeHint", "WorkGroupSizeHint" |
---|
489 | }; |
---|
490 | |
---|
491 | static const size_t kernelAttrMetadataKeywordsNum = |
---|
492 | sizeof(kernelAttrMetadataKeywords) / sizeof(const char*); |
---|
493 | |
---|
494 | enum { |
---|
495 | ROCMMT_CODEPROPS_FIXED_WORK_GROUP_SIZE = 0, ROCMMT_CODEPROPS_GROUP_SEGMENT_FIXED_SIZE, |
---|
496 | ROCMMT_CODEPROPS_KERNARG_SEGMENT_ALIGN, ROCMMT_CODEPROPS_KERNARG_SEGMENT_SIZE, |
---|
497 | ROCMMT_CODEPROPS_MAX_FLAT_WORK_GROUP_SIZE, ROCMMT_CODEPROPS_NUM_SGPRS, |
---|
498 | ROCMMT_CODEPROPS_NUM_SPILLED_SGPRS, ROCMMT_CODEPROPS_NUM_SPILLED_VGPRS, |
---|
499 | ROCMMT_CODEPROPS_NUM_VGPRS, ROCMMT_CODEPROPS_PRIVATE_SEGMENT_FIXED_SIZE, |
---|
500 | ROCMMT_CODEPROPS_WAVEFRONT_SIZE |
---|
501 | }; |
---|
502 | |
---|
503 | static const char* kernelCodePropsKeywords[] = |
---|
504 | { |
---|
505 | "FixedWorkGroupSize", "GroupSegmentFixedSize", "KernargSegmentAlign", |
---|
506 | "KernargSegmentSize", "MaxFlatWorkGroupSize", "NumSGPRs", |
---|
507 | "NumSpilledSGPRs", "NumSpilledVGPRs", "NumVGPRs", "PrivateSegmentFixedSize", |
---|
508 | "WavefrontSize" |
---|
509 | }; |
---|
510 | |
---|
511 | static const size_t kernelCodePropsKeywordsNum = |
---|
512 | sizeof(kernelCodePropsKeywords) / sizeof(const char*); |
---|
513 | |
---|
514 | enum { |
---|
515 | ROCMMT_ARGS_ACCQUAL = 0, ROCMMT_ARGS_ACTUALACCQUAL, ROCMMT_ARGS_ADDRSPACEQUAL, |
---|
516 | ROCMMT_ARGS_ALIGN, ROCMMT_ARGS_ISCONST, ROCMMT_ARGS_ISPIPE, ROCMMT_ARGS_ISRESTRICT, |
---|
517 | ROCMMT_ARGS_ISVOLATILE, ROCMMT_ARGS_NAME, ROCMMT_ARGS_POINTEE_ALIGN, |
---|
518 | ROCMMT_ARGS_SIZE, ROCMMT_ARGS_TYPENAME, ROCMMT_ARGS_VALUEKIND, |
---|
519 | ROCMMT_ARGS_VALUETYPE |
---|
520 | }; |
---|
521 | |
---|
522 | static const char* kernelArgInfosKeywords[] = |
---|
523 | { |
---|
524 | "AccQual", "ActualAccQual", "AddrSpaceQual", "Align", "IsConst", "IsPipe", |
---|
525 | "IsRestrict", "IsVolatile", "Name", "PointeeAlign", "Size", "TypeName", |
---|
526 | "ValueKind", "ValueType" |
---|
527 | }; |
---|
528 | |
---|
529 | static const size_t kernelArgInfosKeywordsNum = |
---|
530 | sizeof(kernelArgInfosKeywords) / sizeof(const char*); |
---|
531 | |
---|
532 | static const std::pair<const char*, ROCmValueKind> rocmValueKindNames[] = |
---|
533 | { |
---|
534 | { "ByValue", ROCmValueKind::BY_VALUE }, |
---|
535 | { "DynamicSharedPointer", ROCmValueKind::DYN_SHARED_PTR }, |
---|
536 | { "GlobalBuffer", ROCmValueKind::GLOBAL_BUFFER }, |
---|
537 | { "HiddenCompletionAction", ROCmValueKind::HIDDEN_COMPLETION_ACTION }, |
---|
538 | { "HiddenDefaultQueue", ROCmValueKind::HIDDEN_DEFAULT_QUEUE }, |
---|
539 | { "HiddenGlobalOffsetX", ROCmValueKind::HIDDEN_GLOBAL_OFFSET_X }, |
---|
540 | { "HiddenGlobalOffsetY", ROCmValueKind::HIDDEN_GLOBAL_OFFSET_Y }, |
---|
541 | { "HiddenglobalOffsetZ", ROCmValueKind::HIDDEN_GLOBAL_OFFSET_Z }, |
---|
542 | { "HiddenNone", ROCmValueKind::HIDDEN_NONE }, |
---|
543 | { "HiddenPrintfBuffer", ROCmValueKind::HIDDEN_PRINTF_BUFFER }, |
---|
544 | { "Image", ROCmValueKind::IMAGE }, |
---|
545 | { "Pipe", ROCmValueKind::PIPE }, |
---|
546 | { "Queue", ROCmValueKind::QUEUE }, |
---|
547 | { "Sampler", ROCmValueKind::SAMPLER } |
---|
548 | }; |
---|
549 | |
---|
550 | static const size_t rocmValueKindNamesNum = |
---|
551 | sizeof(rocmValueKindNames) / sizeof(std::pair<const char*, ROCmValueKind>); |
---|
552 | |
---|
553 | static const std::pair<const char*, ROCmValueType> rocmValueTypeNames[] = |
---|
554 | { |
---|
555 | { "F16", ROCmValueType::FLOAT16 }, |
---|
556 | { "F32", ROCmValueType::FLOAT32 }, |
---|
557 | { "F64", ROCmValueType::FLOAT64 }, |
---|
558 | { "I16", ROCmValueType::INT16 }, |
---|
559 | { "I8", ROCmValueType::INT8 }, |
---|
560 | { "I32", ROCmValueType::INT32 }, |
---|
561 | { "I64", ROCmValueType::INT64 }, |
---|
562 | { "U8", ROCmValueType::UINT8 }, |
---|
563 | { "U16", ROCmValueType::UINT16 }, |
---|
564 | { "U32", ROCmValueType::UINT32 }, |
---|
565 | { "U64", ROCmValueType::UINT64 }, |
---|
566 | { "Struct", ROCmValueType::STRUCTURE } |
---|
567 | }; |
---|
568 | |
---|
569 | static const size_t rocmValueTypeNamesNum = |
---|
570 | sizeof(rocmValueTypeNames) / sizeof(std::pair<const char*, ROCmValueType>); |
---|
571 | |
---|
572 | static const char* rocmAddrSpaceTypesTbl[] = |
---|
573 | { "Private", "Global", "Constant", "Local", "Generic", "Region" }; |
---|
574 | |
---|
575 | static const char* rocmAccessQualifierTbl[] = |
---|
576 | { "ReadOnly", "WriteOnly", "ReadWrite", "Default" }; |
---|
577 | |
---|
578 | void parseROCmMetadata(size_t metadataSize, const char* metadata, |
---|
579 | ROCmMetadata& metadataInfo) |
---|
580 | { |
---|
581 | const char* ptr = metadata; |
---|
582 | const char* end = metadata + metadataSize; |
---|
583 | size_t lineNo = 1; |
---|
584 | // init metadata info object |
---|
585 | metadataInfo.kernels.clear(); |
---|
586 | metadataInfo.printfInfos.clear(); |
---|
587 | metadataInfo.version[0] = metadataInfo.version[1] = 0; |
---|
588 | |
---|
589 | std::vector<ROCmKernelMetadata>& kernels = metadataInfo.kernels; |
---|
590 | |
---|
591 | cxuint levels[6] = { UINT_MAX, UINT_MAX, UINT_MAX, UINT_MAX, UINT_MAX, UINT_MAX }; |
---|
592 | cxuint curLevel = 0; |
---|
593 | bool inKernels = false; |
---|
594 | bool inKernel = false; |
---|
595 | bool inKernelArgs = false; |
---|
596 | bool inKernelArg = false; |
---|
597 | bool inKernelCodeProps = false; |
---|
598 | bool inKernelAttrs = false; |
---|
599 | bool canToNextLevel = false; |
---|
600 | |
---|
601 | size_t oldLineNo = 0; |
---|
602 | while (ptr != end) |
---|
603 | { |
---|
604 | cxuint level = skipSpacesAndComments(ptr, end, lineNo); |
---|
605 | if (ptr == end || lineNo == oldLineNo) |
---|
606 | throw ParseException(lineNo, "Expected new line"); |
---|
607 | |
---|
608 | if (levels[curLevel] == UINT_MAX) |
---|
609 | levels[curLevel] = level; |
---|
610 | else if (levels[curLevel] < level) |
---|
611 | { |
---|
612 | if (canToNextLevel) |
---|
613 | // go to next nesting level |
---|
614 | levels[++curLevel] = level; |
---|
615 | else |
---|
616 | throw ParseException(lineNo, "Unexpected nesting level"); |
---|
617 | canToNextLevel = false; |
---|
618 | } |
---|
619 | else if (levels[curLevel] > level) |
---|
620 | { |
---|
621 | while (curLevel != UINT_MAX && levels[curLevel] > level) |
---|
622 | curLevel--; |
---|
623 | if (curLevel == UINT_MAX) |
---|
624 | throw ParseException(lineNo, "Indentation smaller than in main level"); |
---|
625 | |
---|
626 | // pop from previous level |
---|
627 | if (curLevel < 3) |
---|
628 | { |
---|
629 | if (inKernelArgs) |
---|
630 | { |
---|
631 | // leave from kernel args |
---|
632 | inKernelArgs = false; |
---|
633 | inKernelArg = false; |
---|
634 | } |
---|
635 | |
---|
636 | inKernelCodeProps = false; |
---|
637 | inKernelAttrs = false; |
---|
638 | } |
---|
639 | if (curLevel < 1 && inKernels) |
---|
640 | { |
---|
641 | // leave from kernels |
---|
642 | metadataInfo.kernels.assign(kernels.begin(), kernels.end()); |
---|
643 | kernels.clear(); |
---|
644 | inKernels = false; |
---|
645 | inKernel = false; |
---|
646 | } |
---|
647 | |
---|
648 | if (levels[curLevel] != level) |
---|
649 | throw ParseException(lineNo, "Unexpected nesting level"); |
---|
650 | } |
---|
651 | |
---|
652 | oldLineNo = lineNo; |
---|
653 | if (curLevel == 0) |
---|
654 | { |
---|
655 | const size_t keyIndex = parseYAMLKey(ptr, end, lineNo, |
---|
656 | mainMetadataKeywordsNum, mainMetadataKeywords); |
---|
657 | |
---|
658 | switch(keyIndex) |
---|
659 | { |
---|
660 | case ROCMMT_MAIN_KERNELS: |
---|
661 | inKernels = true; |
---|
662 | canToNextLevel = true; |
---|
663 | break; |
---|
664 | case ROCMMT_MAIN_PRINTF: |
---|
665 | { |
---|
666 | YAMLPrintfVectorConsumer consumer(metadataInfo.printfInfos); |
---|
667 | parseYAMLValArray(ptr, end, lineNo, levels[curLevel], &consumer); |
---|
668 | break; |
---|
669 | } |
---|
670 | case ROCMMT_MAIN_VERSION: |
---|
671 | { |
---|
672 | YAMLIntArrayConsumer<uint32_t> consumer(2, metadataInfo.version); |
---|
673 | parseYAMLValArray(ptr, end, lineNo, levels[curLevel], &consumer); |
---|
674 | break; |
---|
675 | } |
---|
676 | default: |
---|
677 | break; |
---|
678 | } |
---|
679 | } |
---|
680 | |
---|
681 | if (curLevel==1 && inKernels) |
---|
682 | { |
---|
683 | // enter to kernel level |
---|
684 | if (ptr == end || *ptr != '-') |
---|
685 | throw ParseException(lineNo, "No '-' before kernel object"); |
---|
686 | ptr++; |
---|
687 | const char* afterMinus = ptr; |
---|
688 | skipSpacesToEnd(ptr, end); |
---|
689 | levels[++curLevel] = level + 1 + ptr-afterMinus; |
---|
690 | level = levels[curLevel]; |
---|
691 | inKernel = true; |
---|
692 | |
---|
693 | kernels.push_back(ROCmKernelMetadata{}); |
---|
694 | } |
---|
695 | |
---|
696 | if (curLevel==2 && inKernel) |
---|
697 | { |
---|
698 | // in kernel |
---|
699 | const size_t keyIndex = parseYAMLKey(ptr, end, lineNo, |
---|
700 | kernelMetadataKeywordsNum, kernelMetadataKeywords); |
---|
701 | |
---|
702 | ROCmKernelMetadata& kernel = kernels.back(); |
---|
703 | switch(keyIndex) |
---|
704 | { |
---|
705 | case ROCMMT_KERNEL_ARGS: |
---|
706 | inKernelArgs = true; |
---|
707 | canToNextLevel = true; |
---|
708 | break; |
---|
709 | case ROCMMT_KERNEL_ATTRS: |
---|
710 | inKernelAttrs = true; |
---|
711 | canToNextLevel = true; |
---|
712 | break; |
---|
713 | case ROCMMT_KERNEL_CODEPROPS: |
---|
714 | inKernelCodeProps = true; |
---|
715 | canToNextLevel = true; |
---|
716 | break; |
---|
717 | case ROCMMT_KERNEL_LANGUAGE: |
---|
718 | kernel.language = parseYAMLStringValue(ptr, end, lineNo, level); |
---|
719 | break; |
---|
720 | case ROCMMT_KERNEL_LANGUAGE_VERSION: |
---|
721 | { |
---|
722 | YAMLIntArrayConsumer<uint32_t> consumer(2, kernel.langVersion); |
---|
723 | parseYAMLValArray(ptr, end, lineNo, levels[curLevel], &consumer); |
---|
724 | break; |
---|
725 | } |
---|
726 | case ROCMMT_KERNEL_NAME: |
---|
727 | kernel.name = parseYAMLStringValue(ptr, end, lineNo, level); |
---|
728 | break; |
---|
729 | case ROCMMT_KERNEL_SYMBOLNAME: |
---|
730 | kernel.symbolName = parseYAMLStringValue(ptr, end, lineNo, level); |
---|
731 | break; |
---|
732 | default: |
---|
733 | break; |
---|
734 | } |
---|
735 | } |
---|
736 | |
---|
737 | if (curLevel==3 && inKernelAttrs) |
---|
738 | { |
---|
739 | // in kernel attributes |
---|
740 | const size_t keyIndex = parseYAMLKey(ptr, end, lineNo, |
---|
741 | kernelAttrMetadataKeywordsNum, kernelAttrMetadataKeywords); |
---|
742 | |
---|
743 | ROCmKernelMetadata& kernel = kernels.back(); |
---|
744 | switch(keyIndex) |
---|
745 | { |
---|
746 | case ROCMMT_ATTRS_REQD_WORK_GROUP_SIZE: |
---|
747 | { |
---|
748 | YAMLIntArrayConsumer<cxuint> consumer(3, kernel.reqdWorkGroupSize); |
---|
749 | parseYAMLValArray(ptr, end, lineNo, levels[curLevel], &consumer); |
---|
750 | break; |
---|
751 | } |
---|
752 | case ROCMMT_ATTRS_RUNTIME_HANDLE: |
---|
753 | kernel.runtimeHandle = parseYAMLStringValue(ptr, end, lineNo, level); |
---|
754 | break; |
---|
755 | case ROCMMT_ATTRS_VECTYPEHINT: |
---|
756 | kernel.vecTypeHint = parseYAMLStringValue(ptr, end, lineNo, level); |
---|
757 | break; |
---|
758 | case ROCMMT_ATTRS_WORK_GROUP_SIZE_HINT: |
---|
759 | { |
---|
760 | YAMLIntArrayConsumer<cxuint> consumer(3, kernel.workGroupSizeHint); |
---|
761 | parseYAMLValArray(ptr, end, lineNo, levels[curLevel], &consumer); |
---|
762 | break; |
---|
763 | } |
---|
764 | default: |
---|
765 | break; |
---|
766 | } |
---|
767 | } |
---|
768 | |
---|
769 | if (curLevel==3 && inKernelCodeProps) |
---|
770 | { |
---|
771 | // in kernel codeProps |
---|
772 | const size_t keyIndex = parseYAMLKey(ptr, end, lineNo, |
---|
773 | kernelCodePropsKeywordsNum, kernelCodePropsKeywords); |
---|
774 | |
---|
775 | ROCmKernelMetadata& kernel = kernels.back(); |
---|
776 | switch(keyIndex) |
---|
777 | { |
---|
778 | case ROCMMT_CODEPROPS_FIXED_WORK_GROUP_SIZE: |
---|
779 | { |
---|
780 | YAMLIntArrayConsumer<uint64_t> consumer(3, kernel.fixedWorkGroupSize); |
---|
781 | parseYAMLValArray(ptr, end, lineNo, level, &consumer); |
---|
782 | break; |
---|
783 | } |
---|
784 | case ROCMMT_CODEPROPS_GROUP_SEGMENT_FIXED_SIZE: |
---|
785 | kernel.groupSegmentFixedSize = |
---|
786 | parseYAMLIntValue<uint64_t>(ptr, end, lineNo); |
---|
787 | break; |
---|
788 | case ROCMMT_CODEPROPS_KERNARG_SEGMENT_ALIGN: |
---|
789 | kernel.kernargSegmentAlign = |
---|
790 | parseYAMLIntValue<uint64_t>(ptr, end, lineNo); |
---|
791 | break; |
---|
792 | case ROCMMT_CODEPROPS_KERNARG_SEGMENT_SIZE: |
---|
793 | kernel.kernargSegmentSize = |
---|
794 | parseYAMLIntValue<uint64_t>(ptr, end, lineNo); |
---|
795 | break; |
---|
796 | case ROCMMT_CODEPROPS_MAX_FLAT_WORK_GROUP_SIZE: |
---|
797 | kernel.maxFlatWorkGroupSize = |
---|
798 | parseYAMLIntValue<uint64_t>(ptr, end, lineNo); |
---|
799 | break; |
---|
800 | case ROCMMT_CODEPROPS_NUM_SGPRS: |
---|
801 | kernel.sgprsNum = parseYAMLIntValue<cxuint>(ptr, end, lineNo); |
---|
802 | break; |
---|
803 | case ROCMMT_CODEPROPS_NUM_SPILLED_SGPRS: |
---|
804 | kernel.spilledSgprs = parseYAMLIntValue<cxuint>(ptr, end, lineNo); |
---|
805 | break; |
---|
806 | case ROCMMT_CODEPROPS_NUM_SPILLED_VGPRS: |
---|
807 | kernel.spilledVgprs = parseYAMLIntValue<cxuint>(ptr, end, lineNo); |
---|
808 | break; |
---|
809 | case ROCMMT_CODEPROPS_NUM_VGPRS: |
---|
810 | kernel.vgprsNum = parseYAMLIntValue<cxuint>(ptr, end, lineNo); |
---|
811 | break; |
---|
812 | case ROCMMT_CODEPROPS_PRIVATE_SEGMENT_FIXED_SIZE: |
---|
813 | kernel.privateSegmentFixedSize = |
---|
814 | parseYAMLIntValue<uint64_t>(ptr, end, lineNo); |
---|
815 | break; |
---|
816 | case ROCMMT_CODEPROPS_WAVEFRONT_SIZE: |
---|
817 | kernel.wavefrontSize = parseYAMLIntValue<cxuint>(ptr, end, lineNo); |
---|
818 | break; |
---|
819 | default: |
---|
820 | break; |
---|
821 | } |
---|
822 | } |
---|
823 | |
---|
824 | if (curLevel==3 && inKernelArgs) |
---|
825 | { |
---|
826 | // enter to kernel argument level |
---|
827 | if (ptr == end || *ptr != '-') |
---|
828 | throw ParseException(lineNo, "No '-' before argument object"); |
---|
829 | ptr++; |
---|
830 | const char* afterMinus = ptr; |
---|
831 | skipSpacesToEnd(ptr, end); |
---|
832 | levels[++curLevel] = level + 1 + ptr-afterMinus; |
---|
833 | level = levels[curLevel]; |
---|
834 | inKernelArg = true; |
---|
835 | |
---|
836 | kernels.back().argInfos.push_back(ROCmKernelArgInfo{}); |
---|
837 | } |
---|
838 | |
---|
839 | if (curLevel==4 && inKernelArg) |
---|
840 | { |
---|
841 | // in kernel argument |
---|
842 | const size_t keyIndex = parseYAMLKey(ptr, end, lineNo, |
---|
843 | kernelArgInfosKeywordsNum, kernelArgInfosKeywords); |
---|
844 | |
---|
845 | ROCmKernelArgInfo& kernelArg = kernels.back().argInfos.back(); |
---|
846 | switch(keyIndex) |
---|
847 | { |
---|
848 | case ROCMMT_ARGS_ACCQUAL: |
---|
849 | case ROCMMT_ARGS_ACTUALACCQUAL: |
---|
850 | { |
---|
851 | const std::string acc = trimStrSpaces(parseYAMLStringValue( |
---|
852 | ptr, end, lineNo, level)); |
---|
853 | size_t accIndex = 0; |
---|
854 | for (; accIndex < 6; accIndex++) |
---|
855 | if (::strcmp(rocmAccessQualifierTbl[accIndex], acc.c_str())==0) |
---|
856 | break; |
---|
857 | if (accIndex == 4) |
---|
858 | throw ParseException(lineNo, "Wrong access qualifier"); |
---|
859 | if (keyIndex == ROCMMT_ARGS_ACCQUAL) |
---|
860 | kernelArg.accessQual = ROCmAccessQual(accIndex); |
---|
861 | else |
---|
862 | kernelArg.actualAccessQual = ROCmAccessQual(accIndex); |
---|
863 | break; |
---|
864 | } |
---|
865 | case ROCMMT_ARGS_ADDRSPACEQUAL: |
---|
866 | { |
---|
867 | const std::string aspace = trimStrSpaces(parseYAMLStringValue( |
---|
868 | ptr, end, lineNo, level)); |
---|
869 | size_t aspaceIndex = 0; |
---|
870 | for (; aspaceIndex < 6; aspaceIndex++) |
---|
871 | if (::strcmp(rocmAddrSpaceTypesTbl[aspaceIndex], |
---|
872 | aspace.c_str())==0) |
---|
873 | break; |
---|
874 | if (aspaceIndex == 6) |
---|
875 | throw ParseException(lineNo, "Wrong address space"); |
---|
876 | kernelArg.addressSpace = ROCmAddressSpace(aspaceIndex); |
---|
877 | break; |
---|
878 | } |
---|
879 | case ROCMMT_ARGS_ALIGN: |
---|
880 | kernelArg.align = parseYAMLIntValue<uint64_t>(ptr, end, lineNo); |
---|
881 | break; |
---|
882 | case ROCMMT_ARGS_ISCONST: |
---|
883 | kernelArg.isConst = parseYAMLBoolValue(ptr, end, lineNo); |
---|
884 | break; |
---|
885 | case ROCMMT_ARGS_ISPIPE: |
---|
886 | kernelArg.isPipe = parseYAMLBoolValue(ptr, end, lineNo); |
---|
887 | break; |
---|
888 | case ROCMMT_ARGS_ISRESTRICT: |
---|
889 | kernelArg.isRestrict = parseYAMLBoolValue(ptr, end, lineNo); |
---|
890 | break; |
---|
891 | case ROCMMT_ARGS_ISVOLATILE: |
---|
892 | kernelArg.isVolatile = parseYAMLBoolValue(ptr, end, lineNo); |
---|
893 | break; |
---|
894 | case ROCMMT_ARGS_NAME: |
---|
895 | kernelArg.name = parseYAMLStringValue(ptr, end, lineNo, level); |
---|
896 | break; |
---|
897 | case ROCMMT_ARGS_POINTEE_ALIGN: |
---|
898 | kernelArg.pointeeAlign = parseYAMLIntValue<uint64_t>(ptr, end, lineNo); |
---|
899 | break; |
---|
900 | case ROCMMT_ARGS_SIZE: |
---|
901 | kernelArg.size = parseYAMLIntValue<uint64_t>(ptr, end, lineNo); |
---|
902 | break; |
---|
903 | case ROCMMT_ARGS_TYPENAME: |
---|
904 | kernelArg.typeName = parseYAMLStringValue(ptr, end, lineNo, level); |
---|
905 | break; |
---|
906 | case ROCMMT_ARGS_VALUEKIND: |
---|
907 | { |
---|
908 | const std::string vkind = trimStrSpaces(parseYAMLStringValue( |
---|
909 | ptr, end, lineNo, level)); |
---|
910 | const size_t vkindIndex = binaryMapFind(rocmValueKindNames, |
---|
911 | rocmValueKindNames + rocmValueKindNamesNum, vkind.c_str(), |
---|
912 | CStringLess()) - rocmValueKindNames; |
---|
913 | // if unknown kind |
---|
914 | if (vkindIndex == rocmValueKindNamesNum) |
---|
915 | throw ParseException(lineNo, "Wrong argument value kind"); |
---|
916 | kernelArg.valueKind = rocmValueKindNames[vkindIndex].second; |
---|
917 | break; |
---|
918 | } |
---|
919 | case ROCMMT_ARGS_VALUETYPE: |
---|
920 | { |
---|
921 | const std::string vtype = trimStrSpaces(parseYAMLStringValue( |
---|
922 | ptr, end, lineNo, level)); |
---|
923 | const size_t vtypeIndex = binaryMapFind(rocmValueTypeNames, |
---|
924 | rocmValueTypeNames + rocmValueTypeNamesNum, vtype.c_str(), |
---|
925 | CStringLess()) - rocmValueTypeNames; |
---|
926 | // if unknown type |
---|
927 | if (vtypeIndex == rocmValueTypeNamesNum) |
---|
928 | throw ParseException(lineNo, "Wrong argument value type"); |
---|
929 | kernelArg.valueKind = rocmValueKindNames[vtypeIndex].second; |
---|
930 | break; |
---|
931 | } |
---|
932 | default: |
---|
933 | break; |
---|
934 | } |
---|
935 | } |
---|
936 | } |
---|
937 | } |
---|
938 | |
---|
939 | /* |
---|
940 | * ROCm binary reader and generator |
---|
941 | */ |
---|
942 | |
---|
943 | /* TODO: add support for various kernel code offset (now only 256 is supported) */ |
---|
944 | |
---|
945 | ROCmBinary::ROCmBinary(size_t binaryCodeSize, cxbyte* binaryCode, Flags creationFlags) |
---|
946 | : ElfBinary64(binaryCodeSize, binaryCode, creationFlags), |
---|
947 | regionsNum(0), codeSize(0), code(nullptr), |
---|
948 | globalDataSize(0), globalData(nullptr), metadataSize(0), metadata(nullptr), |
---|
949 | newBinFormat(false) |
---|
950 | { |
---|
951 | cxuint textIndex = SHN_UNDEF; |
---|
952 | try |
---|
953 | { textIndex = getSectionIndex(".text"); } |
---|
954 | catch(const Exception& ex) |
---|
955 | { } // ignore failed |
---|
956 | uint64_t codeOffset = 0; |
---|
957 | // find '.text' section |
---|
958 | if (textIndex!=SHN_UNDEF) |
---|
959 | { |
---|
960 | code = getSectionContent(textIndex); |
---|
961 | const Elf64_Shdr& textShdr = getSectionHeader(textIndex); |
---|
962 | codeSize = ULEV(textShdr.sh_size); |
---|
963 | codeOffset = ULEV(textShdr.sh_offset); |
---|
964 | } |
---|
965 | |
---|
966 | cxuint rodataIndex = SHN_UNDEF; |
---|
967 | try |
---|
968 | { rodataIndex = getSectionIndex(".rodata"); } |
---|
969 | catch(const Exception& ex) |
---|
970 | { } // ignore failed |
---|
971 | // find '.text' section |
---|
972 | if (rodataIndex!=SHN_UNDEF) |
---|
973 | { |
---|
974 | globalData = getSectionContent(rodataIndex); |
---|
975 | const Elf64_Shdr& rodataShdr = getSectionHeader(rodataIndex); |
---|
976 | globalDataSize = ULEV(rodataShdr.sh_size); |
---|
977 | } |
---|
978 | |
---|
979 | cxuint gpuConfigIndex = SHN_UNDEF; |
---|
980 | try |
---|
981 | { gpuConfigIndex = getSectionIndex(".AMDGPU.config"); } |
---|
982 | catch(const Exception& ex) |
---|
983 | { } // ignore failed |
---|
984 | newBinFormat = (gpuConfigIndex == SHN_UNDEF); |
---|
985 | |
---|
986 | // counts regions (symbol or kernel) |
---|
987 | regionsNum = 0; |
---|
988 | const size_t symbolsNum = getSymbolsNum(); |
---|
989 | for (size_t i = 0; i < symbolsNum; i++) |
---|
990 | { |
---|
991 | // count regions number |
---|
992 | const Elf64_Sym& sym = getSymbol(i); |
---|
993 | const cxbyte symType = ELF64_ST_TYPE(sym.st_info); |
---|
994 | const cxbyte bind = ELF64_ST_BIND(sym.st_info); |
---|
995 | if (ULEV(sym.st_shndx)==textIndex && |
---|
996 | (symType==STT_GNU_IFUNC || symType==STT_FUNC || |
---|
997 | (bind==STB_GLOBAL && symType==STT_OBJECT))) |
---|
998 | regionsNum++; |
---|
999 | } |
---|
1000 | if (code==nullptr && regionsNum!=0) |
---|
1001 | throw BinException("No code if regions number is not zero"); |
---|
1002 | regions.reset(new ROCmRegion[regionsNum]); |
---|
1003 | size_t j = 0; |
---|
1004 | typedef std::pair<uint64_t, size_t> RegionOffsetEntry; |
---|
1005 | std::unique_ptr<RegionOffsetEntry[]> symOffsets(new RegionOffsetEntry[regionsNum]); |
---|
1006 | |
---|
1007 | // get regions info |
---|
1008 | for (size_t i = 0; i < symbolsNum; i++) |
---|
1009 | { |
---|
1010 | const Elf64_Sym& sym = getSymbol(i); |
---|
1011 | if (ULEV(sym.st_shndx)!=textIndex) |
---|
1012 | continue; // if not in '.text' section |
---|
1013 | const size_t value = ULEV(sym.st_value); |
---|
1014 | if (value < codeOffset) |
---|
1015 | throw BinException("Region offset is too small!"); |
---|
1016 | const size_t size = ULEV(sym.st_size); |
---|
1017 | |
---|
1018 | const cxbyte symType = ELF64_ST_TYPE(sym.st_info); |
---|
1019 | const cxbyte bind = ELF64_ST_BIND(sym.st_info); |
---|
1020 | if (symType==STT_GNU_IFUNC || symType==STT_FUNC || |
---|
1021 | (bind==STB_GLOBAL && symType==STT_OBJECT)) |
---|
1022 | { |
---|
1023 | ROCmRegionType type = ROCmRegionType::DATA; |
---|
1024 | // if kernel |
---|
1025 | if (symType==STT_GNU_IFUNC) |
---|
1026 | type = ROCmRegionType::KERNEL; |
---|
1027 | // if function kernel |
---|
1028 | else if (symType==STT_FUNC) |
---|
1029 | type = ROCmRegionType::FKERNEL; |
---|
1030 | symOffsets[j] = std::make_pair(value, j); |
---|
1031 | if (type!=ROCmRegionType::DATA && value+0x100 > codeOffset+codeSize) |
---|
1032 | throw BinException("Kernel or code offset is too big!"); |
---|
1033 | regions[j++] = { getSymbolName(i), size, value, type }; |
---|
1034 | } |
---|
1035 | } |
---|
1036 | // sort regions by offset |
---|
1037 | std::sort(symOffsets.get(), symOffsets.get()+regionsNum, |
---|
1038 | [](const RegionOffsetEntry& a, const RegionOffsetEntry& b) |
---|
1039 | { return a.first < b.first; }); |
---|
1040 | // checking distance between regions |
---|
1041 | for (size_t i = 1; i <= regionsNum; i++) |
---|
1042 | { |
---|
1043 | size_t end = (i<regionsNum) ? symOffsets[i].first : codeOffset+codeSize; |
---|
1044 | ROCmRegion& region = regions[symOffsets[i-1].second]; |
---|
1045 | if (region.type==ROCmRegionType::KERNEL && symOffsets[i-1].first+0x100 > end) |
---|
1046 | throw BinException("Kernel size is too small!"); |
---|
1047 | |
---|
1048 | const size_t regSize = end - symOffsets[i-1].first; |
---|
1049 | if (region.size==0) |
---|
1050 | region.size = regSize; |
---|
1051 | else |
---|
1052 | region.size = std::min(regSize, region.size); |
---|
1053 | } |
---|
1054 | |
---|
1055 | // get metadata |
---|
1056 | const size_t notesSize = getNotesSize(); |
---|
1057 | const cxbyte* noteContent = (const cxbyte*)getNotes(); |
---|
1058 | |
---|
1059 | for (size_t offset = 0; offset < notesSize; ) |
---|
1060 | { |
---|
1061 | const Elf64_Nhdr* nhdr = (const Elf64_Nhdr*)(noteContent + offset); |
---|
1062 | size_t namesz = ULEV(nhdr->n_namesz); |
---|
1063 | size_t descsz = ULEV(nhdr->n_descsz); |
---|
1064 | if (usumGt(offset, namesz+descsz, notesSize)) |
---|
1065 | throw BinException("Note offset+size out of range"); |
---|
1066 | |
---|
1067 | if (namesz==4 && |
---|
1068 | ::strcmp((const char*)noteContent+offset+ sizeof(Elf64_Nhdr), "AMD")==0) |
---|
1069 | { |
---|
1070 | const uint32_t noteType = ULEV(nhdr->n_type); |
---|
1071 | if (noteType == 0xa) |
---|
1072 | { |
---|
1073 | metadata = (char*)(noteContent+offset+sizeof(Elf64_Nhdr) + 4); |
---|
1074 | metadataSize = descsz; |
---|
1075 | } |
---|
1076 | else if (noteType == 0xb) |
---|
1077 | target.assign((char*)(noteContent+offset+sizeof(Elf64_Nhdr) + 4), descsz); |
---|
1078 | } |
---|
1079 | size_t align = (((namesz+descsz)&3)!=0) ? 4-((namesz+descsz)&3) : 0; |
---|
1080 | offset += sizeof(Elf64_Nhdr) + namesz + descsz + align; |
---|
1081 | } |
---|
1082 | |
---|
1083 | if (hasRegionMap()) |
---|
1084 | { |
---|
1085 | // create region map |
---|
1086 | regionsMap.resize(regionsNum); |
---|
1087 | for (size_t i = 0; i < regionsNum; i++) |
---|
1088 | regionsMap[i] = std::make_pair(regions[i].regionName, i); |
---|
1089 | // sort region map |
---|
1090 | mapSort(regionsMap.begin(), regionsMap.end()); |
---|
1091 | } |
---|
1092 | } |
---|
1093 | |
---|
1094 | /// determint GPU device from ROCm notes |
---|
1095 | GPUDeviceType ROCmBinary::determineGPUDeviceType(uint32_t& outArchMinor, |
---|
1096 | uint32_t& outArchStepping) const |
---|
1097 | { |
---|
1098 | uint32_t archMajor = 0; |
---|
1099 | uint32_t archMinor = 0; |
---|
1100 | uint32_t archStepping = 0; |
---|
1101 | |
---|
1102 | { |
---|
1103 | const cxbyte* noteContent = (const cxbyte*)getNotes(); |
---|
1104 | if (noteContent==nullptr) |
---|
1105 | throw BinException("Missing notes in inner binary!"); |
---|
1106 | size_t notesSize = getNotesSize(); |
---|
1107 | // find note about AMDGPU |
---|
1108 | for (size_t offset = 0; offset < notesSize; ) |
---|
1109 | { |
---|
1110 | const Elf64_Nhdr* nhdr = (const Elf64_Nhdr*)(noteContent + offset); |
---|
1111 | size_t namesz = ULEV(nhdr->n_namesz); |
---|
1112 | size_t descsz = ULEV(nhdr->n_descsz); |
---|
1113 | if (usumGt(offset, namesz+descsz, notesSize)) |
---|
1114 | throw BinException("Note offset+size out of range"); |
---|
1115 | if (ULEV(nhdr->n_type) == 0x3 && namesz==4 && descsz>=0x1a && |
---|
1116 | ::strcmp((const char*)noteContent+offset+sizeof(Elf64_Nhdr), "AMD")==0) |
---|
1117 | { // AMDGPU type |
---|
1118 | const uint32_t* content = (const uint32_t*) |
---|
1119 | (noteContent+offset+sizeof(Elf64_Nhdr) + 4); |
---|
1120 | archMajor = ULEV(content[1]); |
---|
1121 | archMinor = ULEV(content[2]); |
---|
1122 | archStepping = ULEV(content[3]); |
---|
1123 | } |
---|
1124 | size_t align = (((namesz+descsz)&3)!=0) ? 4-((namesz+descsz)&3) : 0; |
---|
1125 | offset += sizeof(Elf64_Nhdr) + namesz + descsz + align; |
---|
1126 | } |
---|
1127 | } |
---|
1128 | // determine device type |
---|
1129 | GPUDeviceType deviceType = getGPUDeviceTypeFromArchVersion(archMajor, archMinor, |
---|
1130 | archStepping); |
---|
1131 | outArchMinor = archMinor; |
---|
1132 | outArchStepping = archStepping; |
---|
1133 | return deviceType; |
---|
1134 | } |
---|
1135 | |
---|
1136 | const ROCmRegion& ROCmBinary::getRegion(const char* name) const |
---|
1137 | { |
---|
1138 | RegionMap::const_iterator it = binaryMapFind(regionsMap.begin(), |
---|
1139 | regionsMap.end(), name); |
---|
1140 | if (it == regionsMap.end()) |
---|
1141 | throw BinException("Can't find region name"); |
---|
1142 | return regions[it->second]; |
---|
1143 | } |
---|
1144 | |
---|
1145 | // if ROCm binary |
---|
1146 | bool CLRX::isROCmBinary(size_t binarySize, const cxbyte* binary) |
---|
1147 | { |
---|
1148 | if (!isElfBinary(binarySize, binary)) |
---|
1149 | return false; |
---|
1150 | if (binary[EI_CLASS] != ELFCLASS64) |
---|
1151 | return false; |
---|
1152 | const Elf64_Ehdr* ehdr = reinterpret_cast<const Elf64_Ehdr*>(binary); |
---|
1153 | if (ULEV(ehdr->e_machine) != 0xe0) |
---|
1154 | return false; |
---|
1155 | return true; |
---|
1156 | } |
---|
1157 | |
---|
1158 | |
---|
1159 | void ROCmInput::addEmptyKernel(const char* kernelName) |
---|
1160 | { |
---|
1161 | symbols.push_back({ kernelName, 0, 0, ROCmRegionType::KERNEL }); |
---|
1162 | } |
---|
1163 | |
---|
1164 | /* |
---|
1165 | * ROCm Binary Generator |
---|
1166 | */ |
---|
1167 | |
---|
1168 | ROCmBinGenerator::ROCmBinGenerator() : manageable(false), input(nullptr) |
---|
1169 | { } |
---|
1170 | |
---|
1171 | ROCmBinGenerator::ROCmBinGenerator(const ROCmInput* rocmInput) |
---|
1172 | : manageable(false), input(rocmInput) |
---|
1173 | { } |
---|
1174 | |
---|
1175 | ROCmBinGenerator::ROCmBinGenerator(GPUDeviceType deviceType, |
---|
1176 | uint32_t archMinor, uint32_t archStepping, size_t codeSize, const cxbyte* code, |
---|
1177 | size_t globalDataSize, const cxbyte* globalData, |
---|
1178 | const std::vector<ROCmSymbolInput>& symbols) |
---|
1179 | { |
---|
1180 | input = new ROCmInput{ deviceType, archMinor, archStepping, 0, false, |
---|
1181 | globalDataSize, globalData, symbols, codeSize, code }; |
---|
1182 | } |
---|
1183 | |
---|
1184 | ROCmBinGenerator::ROCmBinGenerator(GPUDeviceType deviceType, |
---|
1185 | uint32_t archMinor, uint32_t archStepping, size_t codeSize, const cxbyte* code, |
---|
1186 | size_t globalDataSize, const cxbyte* globalData, |
---|
1187 | std::vector<ROCmSymbolInput>&& symbols) |
---|
1188 | { |
---|
1189 | input = new ROCmInput{ deviceType, archMinor, archStepping, 0, false, |
---|
1190 | globalDataSize, globalData, std::move(symbols), codeSize, code }; |
---|
1191 | } |
---|
1192 | |
---|
1193 | ROCmBinGenerator::~ROCmBinGenerator() |
---|
1194 | { |
---|
1195 | if (manageable) |
---|
1196 | delete input; |
---|
1197 | } |
---|
1198 | |
---|
1199 | void ROCmBinGenerator::setInput(const ROCmInput* input) |
---|
1200 | { |
---|
1201 | if (manageable) |
---|
1202 | delete input; |
---|
1203 | manageable = false; |
---|
1204 | this->input = input; |
---|
1205 | } |
---|
1206 | |
---|
1207 | // ELF notes contents |
---|
1208 | static const cxbyte noteDescType1[8] = |
---|
1209 | { 2, 0, 0, 0, 1, 0, 0, 0 }; |
---|
1210 | |
---|
1211 | static const cxbyte noteDescType3[27] = |
---|
1212 | { 4, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
---|
1213 | 'A', 'M', 'D', 0, 'A', 'M', 'D', 'G', 'P', 'U', 0 }; |
---|
1214 | |
---|
1215 | static inline void addMainSectionToTable(cxuint& sectionsNum, uint16_t* builtinTable, |
---|
1216 | cxuint elfSectId) |
---|
1217 | { builtinTable[elfSectId - ELFSECTID_START] = sectionsNum++; } |
---|
1218 | |
---|
1219 | void ROCmBinGenerator::generateInternal(std::ostream* osPtr, std::vector<char>* vPtr, |
---|
1220 | Array<cxbyte>* aPtr) const |
---|
1221 | { |
---|
1222 | AMDGPUArchVersion amdGpuArchValues = getGPUArchVersion(input->deviceType, |
---|
1223 | GPUArchVersionTable::ROCM); |
---|
1224 | if (input->archMinor!=UINT32_MAX) |
---|
1225 | amdGpuArchValues.minor = input->archMinor; |
---|
1226 | if (input->archStepping!=UINT32_MAX) |
---|
1227 | amdGpuArchValues.stepping = input->archStepping; |
---|
1228 | |
---|
1229 | const char* comment = "CLRX ROCmBinGenerator " CLRX_VERSION; |
---|
1230 | uint32_t commentSize = ::strlen(comment); |
---|
1231 | if (input->comment!=nullptr) |
---|
1232 | { |
---|
1233 | // if comment, store comment section |
---|
1234 | comment = input->comment; |
---|
1235 | commentSize = input->commentSize; |
---|
1236 | if (commentSize==0) |
---|
1237 | commentSize = ::strlen(comment); |
---|
1238 | } |
---|
1239 | |
---|
1240 | uint32_t eflags = input->newBinFormat ? 2 : 0; |
---|
1241 | if (input->eflags != BINGEN_DEFAULT) |
---|
1242 | eflags = input->eflags; |
---|
1243 | |
---|
1244 | ElfBinaryGen64 elfBinGen64({ 0U, 0U, 0x40, 0, ET_DYN, |
---|
1245 | 0xe0, EV_CURRENT, UINT_MAX, 0, eflags }, |
---|
1246 | true, true, true, PHREGION_FILESTART); |
---|
1247 | |
---|
1248 | uint16_t mainBuiltinSectTable[ROCMSECTID_MAX-ELFSECTID_START+1]; |
---|
1249 | std::fill(mainBuiltinSectTable, |
---|
1250 | mainBuiltinSectTable + ROCMSECTID_MAX-ELFSECTID_START+1, SHN_UNDEF); |
---|
1251 | cxuint mainSectionsNum = 1; |
---|
1252 | |
---|
1253 | // generate main builtin section table (for section id translation) |
---|
1254 | if (input->newBinFormat) |
---|
1255 | addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ROCMSECTID_NOTE); |
---|
1256 | if (input->globalData != nullptr) |
---|
1257 | addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_RODATA); |
---|
1258 | addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_DYNSYM); |
---|
1259 | addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ROCMSECTID_HASH); |
---|
1260 | addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_DYNSTR); |
---|
1261 | const cxuint execProgHeaderRegionIndex = mainSectionsNum; |
---|
1262 | addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_TEXT); |
---|
1263 | addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ROCMSECTID_DYNAMIC); |
---|
1264 | if (!input->newBinFormat) |
---|
1265 | { |
---|
1266 | addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ROCMSECTID_NOTE); |
---|
1267 | addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ROCMSECTID_GPUCONFIG); |
---|
1268 | } |
---|
1269 | addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_COMMENT); |
---|
1270 | addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_SYMTAB); |
---|
1271 | addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_SHSTRTAB); |
---|
1272 | addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_STRTAB); |
---|
1273 | |
---|
1274 | // add symbols (kernels, function kernels and data symbols) |
---|
1275 | elfBinGen64.addSymbol(ElfSymbol64("_DYNAMIC", |
---|
1276 | mainBuiltinSectTable[ROCMSECTID_DYNAMIC-ELFSECTID_START], |
---|
1277 | ELF64_ST_INFO(STB_LOCAL, STT_NOTYPE), STV_HIDDEN, true, 0, 0)); |
---|
1278 | const uint16_t textSectIndex = mainBuiltinSectTable[ELFSECTID_TEXT-ELFSECTID_START]; |
---|
1279 | for (const ROCmSymbolInput& symbol: input->symbols) |
---|
1280 | { |
---|
1281 | ElfSymbol64 elfsym; |
---|
1282 | switch (symbol.type) |
---|
1283 | { |
---|
1284 | case ROCmRegionType::KERNEL: |
---|
1285 | elfsym = ElfSymbol64(symbol.symbolName.c_str(), textSectIndex, |
---|
1286 | ELF64_ST_INFO(STB_GLOBAL, STT_GNU_IFUNC), 0, true, |
---|
1287 | symbol.offset, symbol.size); |
---|
1288 | break; |
---|
1289 | case ROCmRegionType::FKERNEL: |
---|
1290 | elfsym = ElfSymbol64(symbol.symbolName.c_str(), textSectIndex, |
---|
1291 | ELF64_ST_INFO(STB_GLOBAL, STT_FUNC), 0, true, |
---|
1292 | symbol.offset, symbol.size); |
---|
1293 | break; |
---|
1294 | case ROCmRegionType::DATA: |
---|
1295 | elfsym = ElfSymbol64(symbol.symbolName.c_str(), textSectIndex, |
---|
1296 | ELF64_ST_INFO(STB_GLOBAL, STT_OBJECT), 0, true, |
---|
1297 | symbol.offset, symbol.size); |
---|
1298 | break; |
---|
1299 | default: |
---|
1300 | break; |
---|
1301 | } |
---|
1302 | // add to symbols and dynamic symbols table |
---|
1303 | elfBinGen64.addSymbol(elfsym); |
---|
1304 | elfBinGen64.addDynSymbol(elfsym); |
---|
1305 | } |
---|
1306 | |
---|
1307 | static const int32_t dynTags[] = { |
---|
1308 | DT_SYMTAB, DT_SYMENT, DT_STRTAB, DT_STRSZ, DT_HASH }; |
---|
1309 | elfBinGen64.addDynamics(sizeof(dynTags)/sizeof(int32_t), dynTags); |
---|
1310 | |
---|
1311 | // elf program headers |
---|
1312 | elfBinGen64.addProgramHeader({ PT_PHDR, PF_R, 0, 1, |
---|
1313 | true, Elf64Types::nobase, Elf64Types::nobase, 0 }); |
---|
1314 | elfBinGen64.addProgramHeader({ PT_LOAD, PF_R, PHREGION_FILESTART, |
---|
1315 | execProgHeaderRegionIndex, |
---|
1316 | true, Elf64Types::nobase, Elf64Types::nobase, 0, 0x1000 }); |
---|
1317 | elfBinGen64.addProgramHeader({ PT_LOAD, PF_R|PF_X, execProgHeaderRegionIndex, 1, |
---|
1318 | true, Elf64Types::nobase, Elf64Types::nobase, 0 }); |
---|
1319 | elfBinGen64.addProgramHeader({ PT_LOAD, PF_R|PF_W, execProgHeaderRegionIndex+1, 1, |
---|
1320 | true, Elf64Types::nobase, Elf64Types::nobase, 0 }); |
---|
1321 | elfBinGen64.addProgramHeader({ PT_DYNAMIC, PF_R|PF_W, execProgHeaderRegionIndex+1, 1, |
---|
1322 | true, Elf64Types::nobase, Elf64Types::nobase, 0, 8 }); |
---|
1323 | elfBinGen64.addProgramHeader({ PT_GNU_RELRO, PF_R, execProgHeaderRegionIndex+1, 1, |
---|
1324 | true, Elf64Types::nobase, Elf64Types::nobase, 0, 1 }); |
---|
1325 | elfBinGen64.addProgramHeader({ PT_GNU_STACK, PF_R|PF_W, PHREGION_FILESTART, 0, |
---|
1326 | true, 0, 0, 0 }); |
---|
1327 | |
---|
1328 | if (input->newBinFormat) |
---|
1329 | // program header for note (new binary format) |
---|
1330 | elfBinGen64.addProgramHeader({ PT_NOTE, PF_R, 1, 1, true, |
---|
1331 | Elf64Types::nobase, Elf64Types::nobase, 0, 4 }); |
---|
1332 | |
---|
1333 | std::string target = input->target.c_str(); |
---|
1334 | if (target.empty() && !input->targetTripple.empty()) |
---|
1335 | { |
---|
1336 | target = input->targetTripple.c_str(); |
---|
1337 | char dbuf[20]; |
---|
1338 | snprintf(dbuf, 20, "-gfx%u%u%u", amdGpuArchValues.major, amdGpuArchValues.minor, |
---|
1339 | amdGpuArchValues.stepping); |
---|
1340 | target += dbuf; |
---|
1341 | } |
---|
1342 | // elf notes |
---|
1343 | elfBinGen64.addNote({"AMD", sizeof noteDescType1, noteDescType1, 1U}); |
---|
1344 | std::unique_ptr<cxbyte[]> noteBuf(new cxbyte[0x1b]); |
---|
1345 | ::memcpy(noteBuf.get(), noteDescType3, 0x1b); |
---|
1346 | SULEV(*(uint32_t*)(noteBuf.get()+4), amdGpuArchValues.major); |
---|
1347 | SULEV(*(uint32_t*)(noteBuf.get()+8), amdGpuArchValues.minor); |
---|
1348 | SULEV(*(uint32_t*)(noteBuf.get()+12), amdGpuArchValues.stepping); |
---|
1349 | elfBinGen64.addNote({"AMD", 0x1b, noteBuf.get(), 3U}); |
---|
1350 | if (!target.empty()) |
---|
1351 | elfBinGen64.addNote({"AMD", target.size(), (const cxbyte*)target.c_str(), 0xbU}); |
---|
1352 | if (input->metadataSize != 0) |
---|
1353 | elfBinGen64.addNote({"AMD", input->metadataSize, |
---|
1354 | (const cxbyte*)input->metadata, 0xaU}); |
---|
1355 | |
---|
1356 | /// region and sections |
---|
1357 | elfBinGen64.addRegion(ElfRegion64::programHeaderTable()); |
---|
1358 | if (input->newBinFormat) |
---|
1359 | elfBinGen64.addRegion(ElfRegion64::noteSection()); |
---|
1360 | if (input->globalData != nullptr) |
---|
1361 | elfBinGen64.addRegion(ElfRegion64(input->globalDataSize, input->globalData, 4, |
---|
1362 | ".rodata", SHT_PROGBITS, SHF_ALLOC, 0, 0, Elf64Types::nobase)); |
---|
1363 | |
---|
1364 | elfBinGen64.addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 8, |
---|
1365 | ".dynsym", SHT_DYNSYM, SHF_ALLOC, 0, 1, Elf64Types::nobase)); |
---|
1366 | elfBinGen64.addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 4, |
---|
1367 | ".hash", SHT_HASH, SHF_ALLOC, |
---|
1368 | mainBuiltinSectTable[ELFSECTID_DYNSYM-ELFSECTID_START], 0, |
---|
1369 | Elf64Types::nobase)); |
---|
1370 | elfBinGen64.addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 1, ".dynstr", SHT_STRTAB, |
---|
1371 | SHF_ALLOC, 0, 0, Elf64Types::nobase)); |
---|
1372 | // '.text' with alignment=4096 |
---|
1373 | elfBinGen64.addRegion(ElfRegion64(input->codeSize, (const cxbyte*)input->code, |
---|
1374 | 0x1000, ".text", SHT_PROGBITS, SHF_ALLOC|SHF_EXECINSTR, 0, 0, |
---|
1375 | Elf64Types::nobase, 0, false, 256)); |
---|
1376 | elfBinGen64.addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 0x1000, |
---|
1377 | ".dynamic", SHT_DYNAMIC, SHF_ALLOC|SHF_WRITE, |
---|
1378 | mainBuiltinSectTable[ELFSECTID_DYNSTR-ELFSECTID_START], 0, |
---|
1379 | Elf64Types::nobase, 0, false, 8)); |
---|
1380 | if (!input->newBinFormat) |
---|
1381 | { |
---|
1382 | elfBinGen64.addRegion(ElfRegion64::noteSection()); |
---|
1383 | elfBinGen64.addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 1, |
---|
1384 | ".AMDGPU.config", SHT_PROGBITS, 0)); |
---|
1385 | } |
---|
1386 | elfBinGen64.addRegion(ElfRegion64(commentSize, (const cxbyte*)comment, 1, ".comment", |
---|
1387 | SHT_PROGBITS, SHF_MERGE|SHF_STRINGS, 0, 0, 0, 1)); |
---|
1388 | elfBinGen64.addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 8, |
---|
1389 | ".symtab", SHT_SYMTAB, 0, 0, 2)); |
---|
1390 | elfBinGen64.addRegion(ElfRegion64::shstrtabSection()); |
---|
1391 | elfBinGen64.addRegion(ElfRegion64::strtabSection()); |
---|
1392 | elfBinGen64.addRegion(ElfRegion64::sectionHeaderTable()); |
---|
1393 | |
---|
1394 | /* extra sections */ |
---|
1395 | for (const BinSection& section: input->extraSections) |
---|
1396 | elfBinGen64.addRegion(ElfRegion64(section, mainBuiltinSectTable, |
---|
1397 | ROCMSECTID_MAX, mainSectionsNum)); |
---|
1398 | /* extra symbols */ |
---|
1399 | for (const BinSymbol& symbol: input->extraSymbols) |
---|
1400 | elfBinGen64.addSymbol(ElfSymbol64(symbol, mainBuiltinSectTable, |
---|
1401 | ROCMSECTID_MAX, mainSectionsNum)); |
---|
1402 | |
---|
1403 | size_t binarySize = elfBinGen64.countSize(); |
---|
1404 | /**** |
---|
1405 | * prepare for write binary to output |
---|
1406 | ****/ |
---|
1407 | std::unique_ptr<std::ostream> outStreamHolder; |
---|
1408 | std::ostream* os = nullptr; |
---|
1409 | if (aPtr != nullptr) |
---|
1410 | { |
---|
1411 | aPtr->resize(binarySize); |
---|
1412 | outStreamHolder.reset( |
---|
1413 | new ArrayOStream(binarySize, reinterpret_cast<char*>(aPtr->data()))); |
---|
1414 | os = outStreamHolder.get(); |
---|
1415 | } |
---|
1416 | else if (vPtr != nullptr) |
---|
1417 | { |
---|
1418 | vPtr->resize(binarySize); |
---|
1419 | outStreamHolder.reset(new VectorOStream(*vPtr)); |
---|
1420 | os = outStreamHolder.get(); |
---|
1421 | } |
---|
1422 | else // from argument |
---|
1423 | os = osPtr; |
---|
1424 | |
---|
1425 | const std::ios::iostate oldExceptions = os->exceptions(); |
---|
1426 | try |
---|
1427 | { |
---|
1428 | os->exceptions(std::ios::failbit | std::ios::badbit); |
---|
1429 | /**** |
---|
1430 | * write binary to output |
---|
1431 | ****/ |
---|
1432 | FastOutputBuffer bos(256, *os); |
---|
1433 | elfBinGen64.generate(bos); |
---|
1434 | assert(bos.getWritten() == binarySize); |
---|
1435 | } |
---|
1436 | catch(...) |
---|
1437 | { |
---|
1438 | os->exceptions(oldExceptions); |
---|
1439 | throw; |
---|
1440 | } |
---|
1441 | os->exceptions(oldExceptions); |
---|
1442 | } |
---|
1443 | |
---|
1444 | void ROCmBinGenerator::generate(Array<cxbyte>& array) const |
---|
1445 | { |
---|
1446 | generateInternal(nullptr, nullptr, &array); |
---|
1447 | } |
---|
1448 | |
---|
1449 | void ROCmBinGenerator::generate(std::ostream& os) const |
---|
1450 | { |
---|
1451 | generateInternal(&os, nullptr, nullptr); |
---|
1452 | } |
---|
1453 | |
---|
1454 | void ROCmBinGenerator::generate(std::vector<char>& v) const |
---|
1455 | { |
---|
1456 | generateInternal(nullptr, &v, nullptr); |
---|
1457 | } |
---|