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 <unordered_set> |
---|
30 | #include <CLRX/amdbin/ElfBinaries.h> |
---|
31 | #include <CLRX/utils/Utilities.h> |
---|
32 | #include <CLRX/utils/MemAccess.h> |
---|
33 | #include <CLRX/utils/InputOutput.h> |
---|
34 | #include <CLRX/utils/Containers.h> |
---|
35 | #include <CLRX/amdbin/ROCmBinaries.h> |
---|
36 | |
---|
37 | using namespace CLRX; |
---|
38 | |
---|
39 | /* |
---|
40 | * ROCm metadata YAML parser |
---|
41 | */ |
---|
42 | |
---|
43 | void ROCmKernelMetadata::initialize() |
---|
44 | { |
---|
45 | langVersion[0] = langVersion[1] = BINGEN_NOTSUPPLIED; |
---|
46 | reqdWorkGroupSize[0] = reqdWorkGroupSize[1] = reqdWorkGroupSize[2] = 0; |
---|
47 | workGroupSizeHint[0] = workGroupSizeHint[1] = workGroupSizeHint[2] = 0; |
---|
48 | kernargSegmentSize = BINGEN64_NOTSUPPLIED; |
---|
49 | groupSegmentFixedSize = BINGEN64_NOTSUPPLIED; |
---|
50 | privateSegmentFixedSize = BINGEN64_NOTSUPPLIED; |
---|
51 | kernargSegmentAlign = BINGEN64_NOTSUPPLIED; |
---|
52 | wavefrontSize = BINGEN_NOTSUPPLIED; |
---|
53 | sgprsNum = BINGEN_NOTSUPPLIED; |
---|
54 | vgprsNum = BINGEN_NOTSUPPLIED; |
---|
55 | maxFlatWorkGroupSize = BINGEN64_NOTSUPPLIED; |
---|
56 | fixedWorkGroupSize[0] = fixedWorkGroupSize[1] = fixedWorkGroupSize[2] = 0; |
---|
57 | spilledSgprs = BINGEN_NOTSUPPLIED; |
---|
58 | spilledVgprs = BINGEN_NOTSUPPLIED; |
---|
59 | } |
---|
60 | |
---|
61 | void ROCmMetadata::initialize() |
---|
62 | { |
---|
63 | version[0] = 1; |
---|
64 | version[1] = 0; |
---|
65 | } |
---|
66 | |
---|
67 | // return trailing spaces |
---|
68 | static size_t skipSpacesAndComments(const char*& ptr, const char* end, size_t& lineNo) |
---|
69 | { |
---|
70 | const char* lineStart = ptr; |
---|
71 | while (ptr != end) |
---|
72 | { |
---|
73 | lineStart = ptr; |
---|
74 | while (ptr != end && *ptr!='\n' && isSpace(*ptr)) ptr++; |
---|
75 | if (ptr == end) |
---|
76 | break; // end of stream |
---|
77 | if (*ptr=='#') |
---|
78 | { |
---|
79 | // skip comment |
---|
80 | while (ptr != end && *ptr!='\n') ptr++; |
---|
81 | if (ptr == end) |
---|
82 | return 0; // no trailing spaces and end |
---|
83 | } |
---|
84 | else if (*ptr!='\n') |
---|
85 | break; // no comment and no end of line |
---|
86 | else |
---|
87 | { |
---|
88 | ptr++; |
---|
89 | lineNo++; // next line |
---|
90 | } |
---|
91 | } |
---|
92 | return ptr - lineStart; |
---|
93 | } |
---|
94 | |
---|
95 | static inline void skipSpacesToLineEnd(const char*& ptr, const char* end) |
---|
96 | { |
---|
97 | while (ptr != end && *ptr!='\n' && isSpace(*ptr)) ptr++; |
---|
98 | } |
---|
99 | |
---|
100 | static void skipSpacesToNextLine(const char*& ptr, const char* end, size_t& lineNo) |
---|
101 | { |
---|
102 | skipSpacesToLineEnd(ptr, end); |
---|
103 | if (ptr != end && *ptr != '\n' && *ptr!='#') |
---|
104 | throw ParseException(lineNo, "Garbages at line"); |
---|
105 | if (ptr != end && *ptr == '#') |
---|
106 | // skip comment at end of line |
---|
107 | while (ptr!=end && *ptr!='\n') ptr++; |
---|
108 | if (ptr!=end) |
---|
109 | { // newline |
---|
110 | ptr++; |
---|
111 | lineNo++; |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | enum class YAMLValType |
---|
116 | { |
---|
117 | NONE, |
---|
118 | NIL, |
---|
119 | BOOL, |
---|
120 | INT, |
---|
121 | FLOAT, |
---|
122 | STRING, |
---|
123 | SEQ |
---|
124 | }; |
---|
125 | |
---|
126 | static YAMLValType parseYAMLType(const char*& ptr, const char* end, size_t lineNo) |
---|
127 | { |
---|
128 | if (ptr+2>end || *ptr!='!' || ptr[1]!='!') |
---|
129 | return YAMLValType::NONE; // no type |
---|
130 | if (ptr+7 && ::strncmp(ptr+2, "null", 4)==0 && isSpace(ptr[6]) && ptr[6]!='\n') |
---|
131 | { |
---|
132 | ptr += 6; |
---|
133 | return YAMLValType::NIL; |
---|
134 | } |
---|
135 | else if (ptr+7 && ::strncmp(ptr+2, "bool", 4)==0 && isSpace(ptr[6]) && ptr[6]!='\n') |
---|
136 | { |
---|
137 | ptr += 6; |
---|
138 | return YAMLValType::BOOL; |
---|
139 | } |
---|
140 | else if (ptr+6 && ::strncmp(ptr+2, "int", 3)==0 && isSpace(ptr[5]) && ptr[5]!='\n') |
---|
141 | { |
---|
142 | ptr += 5; |
---|
143 | return YAMLValType::INT; |
---|
144 | } |
---|
145 | else if (ptr+8 && ::strncmp(ptr+2, "float", 5)==0 && isSpace(ptr[7]) && ptr[7]!='\n') |
---|
146 | { |
---|
147 | ptr += 7; |
---|
148 | return YAMLValType::FLOAT; |
---|
149 | } |
---|
150 | else if (ptr+6 && ::strncmp(ptr+2, "str", 3)==0 && isSpace(ptr[5]) && ptr[5]!='\n') |
---|
151 | { |
---|
152 | ptr += 5; |
---|
153 | return YAMLValType::STRING; |
---|
154 | } |
---|
155 | else if (ptr+6 && ::strncmp(ptr+2, "seq", 3)==0 && isSpace(ptr[5]) && ptr[5]!='\n') |
---|
156 | { |
---|
157 | ptr += 5; |
---|
158 | return YAMLValType::SEQ; |
---|
159 | } |
---|
160 | throw ParseException(lineNo, "Unknown YAML value type"); |
---|
161 | } |
---|
162 | |
---|
163 | // parse YAML key (keywords - recognized keys) |
---|
164 | static size_t parseYAMLKey(const char*& ptr, const char* end, size_t lineNo, |
---|
165 | size_t keywordsNum, const char** keywords) |
---|
166 | { |
---|
167 | const char* keyPtr = ptr; |
---|
168 | while (ptr != end && (isAlnum(*ptr) || *ptr=='_')) ptr++; |
---|
169 | if (keyPtr == end) |
---|
170 | throw ParseException(lineNo, "Expected key name"); |
---|
171 | const char* keyEnd = ptr; |
---|
172 | skipSpacesToLineEnd(ptr, end); |
---|
173 | if (ptr == end || *ptr!=':') |
---|
174 | throw ParseException(lineNo, "Expected colon"); |
---|
175 | ptr++; |
---|
176 | const char* afterColon = ptr; |
---|
177 | skipSpacesToLineEnd(ptr, end); |
---|
178 | if (afterColon == ptr && ptr != end && *ptr!='\n') |
---|
179 | // only if not immediate newline |
---|
180 | throw ParseException(lineNo, "After key and colon must be space"); |
---|
181 | CString keyword(keyPtr, keyEnd); |
---|
182 | const size_t index = binaryFind(keywords, keywords+keywordsNum, |
---|
183 | keyword.c_str(), CStringLess()) - keywords; |
---|
184 | return index; |
---|
185 | } |
---|
186 | |
---|
187 | // parse YAML integer value |
---|
188 | template<typename T> |
---|
189 | static T parseYAMLIntValue(const char*& ptr, const char* end, size_t& lineNo, |
---|
190 | bool singleValue = false) |
---|
191 | { |
---|
192 | skipSpacesToLineEnd(ptr, end); |
---|
193 | if (ptr == end || *ptr=='\n') |
---|
194 | throw ParseException(lineNo, "Expected integer value"); |
---|
195 | |
---|
196 | // skip !!int |
---|
197 | YAMLValType valType = parseYAMLType(ptr, end, lineNo); |
---|
198 | if (valType == YAMLValType::INT) |
---|
199 | { // if |
---|
200 | skipSpacesToLineEnd(ptr, end); |
---|
201 | if (ptr == end || *ptr=='\n') |
---|
202 | throw ParseException(lineNo, "Expected integer value"); |
---|
203 | } |
---|
204 | else if (valType != YAMLValType::NONE) |
---|
205 | throw ParseException(lineNo, "Expected value of integer type"); |
---|
206 | |
---|
207 | T value = 0; |
---|
208 | try |
---|
209 | { value = cstrtovCStyle<T>(ptr, end, ptr); } |
---|
210 | catch(const ParseException& ex) |
---|
211 | { throw ParseException(lineNo, ex.what()); } |
---|
212 | |
---|
213 | if (singleValue) |
---|
214 | skipSpacesToNextLine(ptr, end, lineNo); |
---|
215 | return value; |
---|
216 | } |
---|
217 | |
---|
218 | // parse YAML boolean value |
---|
219 | static bool parseYAMLBoolValue(const char*& ptr, const char* end, size_t& lineNo, |
---|
220 | bool singleValue = false) |
---|
221 | { |
---|
222 | skipSpacesToLineEnd(ptr, end); |
---|
223 | if (ptr == end || *ptr=='\n') |
---|
224 | throw ParseException(lineNo, "Expected boolean value"); |
---|
225 | |
---|
226 | // skip !!bool |
---|
227 | YAMLValType valType = parseYAMLType(ptr, end, lineNo); |
---|
228 | if (valType == YAMLValType::BOOL) |
---|
229 | { // if |
---|
230 | skipSpacesToLineEnd(ptr, end); |
---|
231 | if (ptr == end || *ptr=='\n') |
---|
232 | throw ParseException(lineNo, "Expected boolean value"); |
---|
233 | } |
---|
234 | else if (valType != YAMLValType::NONE) |
---|
235 | throw ParseException(lineNo, "Expected value of boolean type"); |
---|
236 | |
---|
237 | const char* wordPtr = ptr; |
---|
238 | while(ptr != end && isAlnum(*ptr)) ptr++; |
---|
239 | CString word(wordPtr, ptr); |
---|
240 | |
---|
241 | bool value = false; |
---|
242 | bool isSet = false; |
---|
243 | for (const char* v: { "1", "true", "t", "on", "yes", "y"}) |
---|
244 | if (::strcasecmp(word.c_str(), v) == 0) |
---|
245 | { |
---|
246 | isSet = true; |
---|
247 | value = true; |
---|
248 | break; |
---|
249 | } |
---|
250 | if (!isSet) |
---|
251 | for (const char* v: { "0", "false", "f", "off", "no", "n"}) |
---|
252 | if (::strcasecmp(word.c_str(), v) == 0) |
---|
253 | { |
---|
254 | isSet = true; |
---|
255 | value = false; |
---|
256 | break; |
---|
257 | } |
---|
258 | if (!isSet) |
---|
259 | throw ParseException(lineNo, "This is not boolean value"); |
---|
260 | |
---|
261 | if (singleValue) |
---|
262 | skipSpacesToNextLine(ptr, end, lineNo); |
---|
263 | return value; |
---|
264 | } |
---|
265 | |
---|
266 | // trim spaces (remove spaces from start and end) |
---|
267 | static std::string trimStrSpaces(const std::string& str) |
---|
268 | { |
---|
269 | size_t i = 0; |
---|
270 | const size_t sz = str.size(); |
---|
271 | while (i!=sz && isSpace(str[i])) i++; |
---|
272 | if (i == sz) return ""; |
---|
273 | size_t j = sz-1; |
---|
274 | while (j>i && isSpace(str[j])) j--; |
---|
275 | return str.substr(i, j-i+1); |
---|
276 | } |
---|
277 | |
---|
278 | static std::string parseYAMLString(const char*& linePtr, const char* end, |
---|
279 | size_t& lineNo) |
---|
280 | { |
---|
281 | std::string strarray; |
---|
282 | if (linePtr == end || (*linePtr != '"' && *linePtr != '\'')) |
---|
283 | { |
---|
284 | while (linePtr != end && !isSpace(*linePtr) && *linePtr != ',') linePtr++; |
---|
285 | throw ParseException(lineNo, "Expected string"); |
---|
286 | } |
---|
287 | const char termChar = *linePtr; |
---|
288 | linePtr++; |
---|
289 | |
---|
290 | // main loop, where is character parsing |
---|
291 | while (linePtr != end && *linePtr != termChar) |
---|
292 | { |
---|
293 | if (*linePtr == '\\') |
---|
294 | { |
---|
295 | // escape |
---|
296 | linePtr++; |
---|
297 | uint16_t value; |
---|
298 | if (linePtr == end) |
---|
299 | throw ParseException(lineNo, "Unterminated character of string"); |
---|
300 | if (*linePtr == 'x') |
---|
301 | { |
---|
302 | // hex literal |
---|
303 | linePtr++; |
---|
304 | if (linePtr == end) |
---|
305 | throw ParseException(lineNo, "Unterminated character of string"); |
---|
306 | value = 0; |
---|
307 | if (isXDigit(*linePtr)) |
---|
308 | for (; linePtr != end; linePtr++) |
---|
309 | { |
---|
310 | cxuint digit; |
---|
311 | if (*linePtr >= '0' && *linePtr <= '9') |
---|
312 | digit = *linePtr-'0'; |
---|
313 | else if (*linePtr >= 'a' && *linePtr <= 'f') |
---|
314 | digit = *linePtr-'a'+10; |
---|
315 | else if (*linePtr >= 'A' && *linePtr <= 'F') |
---|
316 | digit = *linePtr-'A'+10; |
---|
317 | else |
---|
318 | break; |
---|
319 | value = (value<<4) + digit; |
---|
320 | } |
---|
321 | else |
---|
322 | throw ParseException(lineNo, "Expected hexadecimal character code"); |
---|
323 | value &= 0xff; |
---|
324 | } |
---|
325 | else if (isODigit(*linePtr)) |
---|
326 | { |
---|
327 | // octal literal |
---|
328 | value = 0; |
---|
329 | for (cxuint i = 0; linePtr != end && i < 3; i++, linePtr++) |
---|
330 | { |
---|
331 | if (!isODigit(*linePtr)) |
---|
332 | break; |
---|
333 | value = (value<<3) + uint64_t(*linePtr-'0'); |
---|
334 | // checking range |
---|
335 | if (value > 255) |
---|
336 | throw ParseException(lineNo, "Octal code out of range"); |
---|
337 | } |
---|
338 | } |
---|
339 | else |
---|
340 | { |
---|
341 | // normal escapes |
---|
342 | const char c = *linePtr++; |
---|
343 | switch (c) |
---|
344 | { |
---|
345 | case 'a': |
---|
346 | value = '\a'; |
---|
347 | break; |
---|
348 | case 'b': |
---|
349 | value = '\b'; |
---|
350 | break; |
---|
351 | case 'r': |
---|
352 | value = '\r'; |
---|
353 | break; |
---|
354 | case 'n': |
---|
355 | value = '\n'; |
---|
356 | break; |
---|
357 | case 'f': |
---|
358 | value = '\f'; |
---|
359 | break; |
---|
360 | case 'v': |
---|
361 | value = '\v'; |
---|
362 | break; |
---|
363 | case 't': |
---|
364 | value = '\t'; |
---|
365 | break; |
---|
366 | case '\\': |
---|
367 | value = '\\'; |
---|
368 | break; |
---|
369 | case '\'': |
---|
370 | value = '\''; |
---|
371 | break; |
---|
372 | case '\"': |
---|
373 | value = '\"'; |
---|
374 | break; |
---|
375 | default: |
---|
376 | value = c; |
---|
377 | } |
---|
378 | } |
---|
379 | strarray.push_back(value); |
---|
380 | } |
---|
381 | else // regular character |
---|
382 | { |
---|
383 | if (*linePtr=='\n') |
---|
384 | lineNo++; |
---|
385 | strarray.push_back(*linePtr++); |
---|
386 | } |
---|
387 | } |
---|
388 | if (linePtr == end) |
---|
389 | throw ParseException(lineNo, "Unterminated string"); |
---|
390 | linePtr++; |
---|
391 | return strarray; |
---|
392 | } |
---|
393 | |
---|
394 | static std::string parseYAMLStringValue(const char*& ptr, const char* end, size_t& lineNo, |
---|
395 | cxuint prevIndent, bool singleValue = false, bool blockAccept = true) |
---|
396 | { |
---|
397 | skipSpacesToLineEnd(ptr, end); |
---|
398 | if (ptr == end) |
---|
399 | return ""; |
---|
400 | |
---|
401 | // skip !!str |
---|
402 | YAMLValType valType = parseYAMLType(ptr, end, lineNo); |
---|
403 | if (valType == YAMLValType::STRING) |
---|
404 | { // if |
---|
405 | skipSpacesToLineEnd(ptr, end); |
---|
406 | if (ptr == end) |
---|
407 | return ""; |
---|
408 | } |
---|
409 | else if (valType != YAMLValType::NONE) |
---|
410 | throw ParseException(lineNo, "Expected value of string type"); |
---|
411 | |
---|
412 | std::string buf; |
---|
413 | if (*ptr=='"' || *ptr== '\'') |
---|
414 | buf = parseYAMLString(ptr, end, lineNo); |
---|
415 | // otherwise parse stream |
---|
416 | else if (*ptr == '|' || *ptr == '>') |
---|
417 | { |
---|
418 | if (!blockAccept) |
---|
419 | throw ParseException(lineNo, "Illegal block string start"); |
---|
420 | // multiline |
---|
421 | bool newLineFold = *ptr=='>'; |
---|
422 | ptr++; |
---|
423 | skipSpacesToLineEnd(ptr, end); |
---|
424 | if (ptr!=end && *ptr!='\n') |
---|
425 | throw ParseException(lineNo, "Garbages at string block"); |
---|
426 | if (ptr == end) |
---|
427 | return ""; // end |
---|
428 | lineNo++; |
---|
429 | ptr++; // skip newline |
---|
430 | const char* lineStart = ptr; |
---|
431 | skipSpacesToLineEnd(ptr, end); |
---|
432 | size_t indent = ptr - lineStart; |
---|
433 | if (indent <= prevIndent) |
---|
434 | throw ParseException(lineNo, "Unindented string block"); |
---|
435 | |
---|
436 | std::string buf; |
---|
437 | while(ptr != end) |
---|
438 | { |
---|
439 | const char* strStart = ptr; |
---|
440 | while (ptr != end && *ptr!='\n') ptr++; |
---|
441 | buf.append(strStart, ptr); |
---|
442 | |
---|
443 | if (ptr != end) // if new line |
---|
444 | { |
---|
445 | lineNo++; |
---|
446 | ptr++; |
---|
447 | } |
---|
448 | else // end of stream |
---|
449 | break; |
---|
450 | |
---|
451 | const char* lineStart = ptr; |
---|
452 | skipSpacesToLineEnd(ptr, end); |
---|
453 | bool emptyLines = false; |
---|
454 | while (size_t(ptr - lineStart) <= indent) |
---|
455 | { |
---|
456 | if (ptr != end && *ptr=='\n') |
---|
457 | { |
---|
458 | // empty line |
---|
459 | buf.append("\n"); |
---|
460 | ptr++; |
---|
461 | lineNo++; |
---|
462 | lineStart = ptr; |
---|
463 | skipSpacesToLineEnd(ptr, end); |
---|
464 | emptyLines = true; |
---|
465 | continue; |
---|
466 | } |
---|
467 | // if smaller indent |
---|
468 | if (size_t(ptr - lineStart) < indent) |
---|
469 | { |
---|
470 | buf.append("\n"); // always add newline at last line |
---|
471 | if (ptr != end) |
---|
472 | ptr = lineStart; |
---|
473 | return buf; |
---|
474 | } |
---|
475 | else // if this same and not end of line |
---|
476 | break; |
---|
477 | } |
---|
478 | |
---|
479 | if (!emptyLines || !newLineFold) |
---|
480 | // add missing newline after line with text |
---|
481 | // only if no emptyLines or no newLineFold |
---|
482 | buf.append(newLineFold ? " " : "\n"); |
---|
483 | // to indent |
---|
484 | ptr = lineStart + indent; |
---|
485 | } |
---|
486 | return buf; |
---|
487 | } |
---|
488 | else |
---|
489 | { |
---|
490 | // single line string (unquoted) |
---|
491 | const char* strStart = ptr; |
---|
492 | // automatically trim spaces at ends |
---|
493 | const char* strEnd = ptr; |
---|
494 | while (ptr != end && *ptr!='\n' && *ptr!='#') |
---|
495 | { |
---|
496 | if (!isSpace(*ptr)) |
---|
497 | strEnd = ptr; // to trim at end |
---|
498 | ptr++; |
---|
499 | } |
---|
500 | if (strEnd != end && !isSpace(*strEnd)) |
---|
501 | strEnd++; |
---|
502 | |
---|
503 | buf.assign(strStart, strEnd); |
---|
504 | } |
---|
505 | |
---|
506 | if (singleValue) |
---|
507 | skipSpacesToNextLine(ptr, end, lineNo); |
---|
508 | return buf; |
---|
509 | } |
---|
510 | |
---|
511 | /// element consumer class |
---|
512 | class CLRX_INTERNAL YAMLElemConsumer |
---|
513 | { |
---|
514 | public: |
---|
515 | virtual void consume(const char*& ptr, const char* end, size_t& lineNo, |
---|
516 | cxuint prevIndent, bool singleValue, bool blockAccept) = 0; |
---|
517 | }; |
---|
518 | |
---|
519 | static void parseYAMLValArray(const char*& ptr, const char* end, size_t& lineNo, |
---|
520 | size_t prevIndent, YAMLElemConsumer* elemConsumer, bool singleValue = false) |
---|
521 | { |
---|
522 | skipSpacesToLineEnd(ptr, end); |
---|
523 | if (ptr == end) |
---|
524 | return; |
---|
525 | |
---|
526 | // skip !!int |
---|
527 | YAMLValType valType = parseYAMLType(ptr, end, lineNo); |
---|
528 | if (valType == YAMLValType::SEQ) |
---|
529 | { // if |
---|
530 | skipSpacesToLineEnd(ptr, end); |
---|
531 | if (ptr == end) |
---|
532 | return; |
---|
533 | } |
---|
534 | else if (valType != YAMLValType::NONE) |
---|
535 | throw ParseException(lineNo, "Expected value of sequence type"); |
---|
536 | |
---|
537 | if (*ptr == '[') |
---|
538 | { |
---|
539 | // parse array [] |
---|
540 | ptr++; |
---|
541 | skipSpacesAndComments(ptr, end, lineNo); |
---|
542 | while (ptr != end) |
---|
543 | { |
---|
544 | // parse in line |
---|
545 | elemConsumer->consume(ptr, end, lineNo, 0, false, false); |
---|
546 | skipSpacesAndComments(ptr, end, lineNo); |
---|
547 | if (ptr!=end && *ptr==']') |
---|
548 | // just end |
---|
549 | break; |
---|
550 | else if (ptr==end || *ptr!=',') |
---|
551 | throw ParseException(lineNo, "Expected ','"); |
---|
552 | ptr++; |
---|
553 | skipSpacesAndComments(ptr, end, lineNo); |
---|
554 | } |
---|
555 | if (ptr == end) |
---|
556 | throw ParseException(lineNo, "Unterminated array"); |
---|
557 | ptr++; |
---|
558 | |
---|
559 | if (singleValue) |
---|
560 | skipSpacesToNextLine(ptr, end, lineNo); |
---|
561 | return; |
---|
562 | } |
---|
563 | // parse sequence |
---|
564 | size_t oldLineNo = lineNo; |
---|
565 | size_t indent0 = skipSpacesAndComments(ptr, end, lineNo); |
---|
566 | if (ptr == end || lineNo == oldLineNo) |
---|
567 | throw ParseException(lineNo, "Expected sequence of values"); |
---|
568 | |
---|
569 | if (indent0 < prevIndent) |
---|
570 | throw ParseException(lineNo, "Unindented sequence of objects"); |
---|
571 | |
---|
572 | // main loop to parse sequence |
---|
573 | while (ptr != end) |
---|
574 | { |
---|
575 | if (*ptr != '-') |
---|
576 | throw ParseException(lineNo, "No '-' before element value"); |
---|
577 | ptr++; |
---|
578 | const char* afterMinus = ptr; |
---|
579 | skipSpacesToLineEnd(ptr, end); |
---|
580 | if (afterMinus == ptr) |
---|
581 | throw ParseException(lineNo, "No spaces after '-'"); |
---|
582 | elemConsumer->consume(ptr, end, lineNo, indent0, true, true); |
---|
583 | |
---|
584 | size_t indent = skipSpacesAndComments(ptr, end, lineNo); |
---|
585 | if (indent < indent0) |
---|
586 | { |
---|
587 | // if parent level |
---|
588 | ptr -= indent; |
---|
589 | break; |
---|
590 | } |
---|
591 | if (indent != indent0) |
---|
592 | throw ParseException(lineNo, "Wrong indentation of element"); |
---|
593 | } |
---|
594 | } |
---|
595 | |
---|
596 | // integer element consumer |
---|
597 | template<typename T> |
---|
598 | class CLRX_INTERNAL YAMLIntArrayConsumer: public YAMLElemConsumer |
---|
599 | { |
---|
600 | private: |
---|
601 | size_t elemsNum; |
---|
602 | size_t requiredElemsNum; |
---|
603 | public: |
---|
604 | T* array; |
---|
605 | |
---|
606 | YAMLIntArrayConsumer(size_t reqElemsNum, T* _array) |
---|
607 | : elemsNum(0), requiredElemsNum(reqElemsNum), array(_array) |
---|
608 | { } |
---|
609 | |
---|
610 | virtual void consume(const char*& ptr, const char* end, size_t& lineNo, |
---|
611 | cxuint prevIndent, bool singleValue, bool blockAccept) |
---|
612 | { |
---|
613 | if (elemsNum == requiredElemsNum) |
---|
614 | throw ParseException(lineNo, "Too many elements"); |
---|
615 | try |
---|
616 | { array[elemsNum] = cstrtovCStyle<T>(ptr, end, ptr); } |
---|
617 | catch(const ParseException& ex) |
---|
618 | { throw ParseException(lineNo, ex.what()); } |
---|
619 | elemsNum++; |
---|
620 | if (singleValue) |
---|
621 | skipSpacesToNextLine(ptr, end, lineNo); |
---|
622 | } |
---|
623 | }; |
---|
624 | |
---|
625 | // printf info string consumer |
---|
626 | class CLRX_INTERNAL YAMLPrintfVectorConsumer: public YAMLElemConsumer |
---|
627 | { |
---|
628 | private: |
---|
629 | std::unordered_set<cxuint> printfIds; |
---|
630 | public: |
---|
631 | std::vector<ROCmPrintfInfo>& printfInfos; |
---|
632 | |
---|
633 | YAMLPrintfVectorConsumer(std::vector<ROCmPrintfInfo>& _printInfos) |
---|
634 | : printfInfos(_printInfos) |
---|
635 | { } |
---|
636 | |
---|
637 | virtual void consume(const char*& ptr, const char* end, size_t& lineNo, |
---|
638 | cxuint prevIndent, bool singleValue, bool blockAccept) |
---|
639 | { |
---|
640 | const size_t oldLineNo = lineNo; |
---|
641 | std::string str = parseYAMLStringValue(ptr, end, lineNo, prevIndent, |
---|
642 | singleValue, blockAccept); |
---|
643 | // parse printf string |
---|
644 | ROCmPrintfInfo printfInfo{}; |
---|
645 | |
---|
646 | const char* ptr2 = str.c_str(); |
---|
647 | const char* end2 = str.c_str() + str.size(); |
---|
648 | skipSpacesToLineEnd(ptr2, end2); |
---|
649 | try |
---|
650 | { printfInfo.id = cstrtovCStyle<uint32_t>(ptr2, end2, ptr2); } |
---|
651 | catch(const ParseException& ex) |
---|
652 | { throw ParseException(oldLineNo, ex.what()); } |
---|
653 | |
---|
654 | // check printf id uniqueness |
---|
655 | if (!printfIds.insert(printfInfo.id).second) |
---|
656 | throw ParseException(oldLineNo, "Duplicate of printf id"); |
---|
657 | |
---|
658 | skipSpacesToLineEnd(ptr2, end2); |
---|
659 | if (ptr2==end || *ptr2!=':') |
---|
660 | throw ParseException(oldLineNo, "No colon after printf callId"); |
---|
661 | ptr2++; |
---|
662 | skipSpacesToLineEnd(ptr2, end2); |
---|
663 | uint32_t argsNum = cstrtovCStyle<uint32_t>(ptr2, end2, ptr2); |
---|
664 | skipSpacesToLineEnd(ptr2, end2); |
---|
665 | if (ptr2==end || *ptr2!=':') |
---|
666 | throw ParseException(oldLineNo, "No colon after printf argsNum"); |
---|
667 | ptr2++; |
---|
668 | |
---|
669 | printfInfo.argSizes.resize(argsNum); |
---|
670 | |
---|
671 | // parse arg sizes |
---|
672 | for (size_t i = 0; i < argsNum; i++) |
---|
673 | { |
---|
674 | skipSpacesToLineEnd(ptr2, end2); |
---|
675 | printfInfo.argSizes[i] = cstrtovCStyle<uint32_t>(ptr2, end2, ptr2); |
---|
676 | skipSpacesToLineEnd(ptr2, end2); |
---|
677 | if (ptr2==end || *ptr2!=':') |
---|
678 | throw ParseException(lineNo, "No colon after printf argsNum"); |
---|
679 | ptr2++; |
---|
680 | } |
---|
681 | // format |
---|
682 | printfInfo.format.assign(ptr2, end2); |
---|
683 | |
---|
684 | printfInfos.push_back(printfInfo); |
---|
685 | } |
---|
686 | }; |
---|
687 | |
---|
688 | // skip YAML value after key |
---|
689 | static void skipYAMLValue(const char*& ptr, const char* end, size_t& lineNo, |
---|
690 | cxuint prevIndent, bool singleValue = true) |
---|
691 | { |
---|
692 | skipSpacesToLineEnd(ptr, end); |
---|
693 | if (ptr+2 >= end && ptr[0]=='!' && ptr[1]=='!') |
---|
694 | { // skip !!xxxxx |
---|
695 | ptr+=2; |
---|
696 | while (ptr!=end && isAlpha(*ptr)) ptr++; |
---|
697 | skipSpacesToLineEnd(ptr, end); |
---|
698 | } |
---|
699 | |
---|
700 | if (ptr==end || (*ptr!='\'' && *ptr!='"' && *ptr!='|' && *ptr!='>' && *ptr !='[' && |
---|
701 | *ptr!='#' && *ptr!='\n')) |
---|
702 | { |
---|
703 | while (ptr!=end && *ptr!='\n') ptr++; |
---|
704 | skipSpacesToNextLine(ptr, end, lineNo); |
---|
705 | return; |
---|
706 | } |
---|
707 | // string |
---|
708 | if (*ptr=='\'' || *ptr=='"') |
---|
709 | { |
---|
710 | const char delim = *ptr++; |
---|
711 | bool escape = false; |
---|
712 | while(ptr!=end && (escape || *ptr!=delim)) |
---|
713 | { |
---|
714 | if (!escape && *ptr=='\\') |
---|
715 | escape = true; |
---|
716 | else if (escape) |
---|
717 | escape = false; |
---|
718 | if (*ptr=='\n') lineNo++; |
---|
719 | ptr++; |
---|
720 | } |
---|
721 | if (ptr==end) |
---|
722 | throw ParseException(lineNo, "Unterminated string"); |
---|
723 | ptr++; |
---|
724 | if (singleValue) |
---|
725 | skipSpacesToNextLine(ptr, end, lineNo); |
---|
726 | } |
---|
727 | else if (*ptr=='[') |
---|
728 | { // otherwise [array] |
---|
729 | ptr++; |
---|
730 | skipSpacesAndComments(ptr, end, lineNo); |
---|
731 | while (ptr != end) |
---|
732 | { |
---|
733 | // parse in line |
---|
734 | if (ptr!=end && (*ptr=='\'' || *ptr=='"')) |
---|
735 | // skip YAML string |
---|
736 | skipYAMLValue(ptr, end, lineNo, 0, false); |
---|
737 | else |
---|
738 | while (ptr!=end && *ptr!='\n' && |
---|
739 | *ptr!='#' && *ptr!=',' && *ptr!=']') ptr++; |
---|
740 | skipSpacesAndComments(ptr, end, lineNo); |
---|
741 | |
---|
742 | if (ptr!=end && *ptr==']') |
---|
743 | // just end |
---|
744 | break; |
---|
745 | else if (ptr!=end && *ptr!=',') |
---|
746 | throw ParseException(lineNo, "Expected ','"); |
---|
747 | ptr++; |
---|
748 | skipSpacesAndComments(ptr, end, lineNo); |
---|
749 | } |
---|
750 | if (ptr == end) |
---|
751 | throw ParseException(lineNo, "Unterminated array"); |
---|
752 | ptr++; |
---|
753 | skipSpacesToNextLine(ptr, end, lineNo); |
---|
754 | } |
---|
755 | else |
---|
756 | { // block value |
---|
757 | bool blockValue = false; |
---|
758 | if (ptr!=end && (*ptr=='|' || *ptr=='>')) |
---|
759 | { |
---|
760 | ptr++; // skip '|' or '>' |
---|
761 | blockValue = true; |
---|
762 | } |
---|
763 | if (ptr!=end && *ptr=='#') |
---|
764 | while (ptr!=end && *ptr!='\n') ptr++; |
---|
765 | else |
---|
766 | skipSpacesToLineEnd(ptr, end); |
---|
767 | if (ptr!=end && *ptr!='\n') |
---|
768 | throw ParseException(lineNo, "Garbages before block or children"); |
---|
769 | ptr++; |
---|
770 | lineNo++; |
---|
771 | // skip all lines indented beyound previous level |
---|
772 | while (ptr != end) |
---|
773 | { |
---|
774 | const char* lineStart = ptr; |
---|
775 | skipSpacesToLineEnd(ptr, end); |
---|
776 | if (ptr == end) |
---|
777 | { |
---|
778 | ptr++; |
---|
779 | lineNo++; |
---|
780 | continue; |
---|
781 | } |
---|
782 | if (size_t(ptr-lineStart) <= prevIndent && *ptr!='\n' && |
---|
783 | (blockValue || *ptr!='#')) |
---|
784 | // if indent is short and not empty line (same spaces) or |
---|
785 | // or with only comment and not blockValue |
---|
786 | { |
---|
787 | ptr = lineStart; |
---|
788 | break; |
---|
789 | } |
---|
790 | |
---|
791 | while (ptr!=end && *ptr!='\n') ptr++; |
---|
792 | if (ptr!=end) |
---|
793 | { |
---|
794 | lineNo++; |
---|
795 | ptr++; |
---|
796 | } |
---|
797 | } |
---|
798 | } |
---|
799 | } |
---|
800 | |
---|
801 | enum { |
---|
802 | ROCMMT_MAIN_KERNELS = 0, ROCMMT_MAIN_PRINTF, ROCMMT_MAIN_VERSION |
---|
803 | }; |
---|
804 | |
---|
805 | static const char* mainMetadataKeywords[] = |
---|
806 | { |
---|
807 | "Kernels", "Printf", "Version" |
---|
808 | }; |
---|
809 | |
---|
810 | static const size_t mainMetadataKeywordsNum = |
---|
811 | sizeof(mainMetadataKeywords) / sizeof(const char*); |
---|
812 | |
---|
813 | enum { |
---|
814 | ROCMMT_KERNEL_ARGS = 0, ROCMMT_KERNEL_ATTRS, ROCMMT_KERNEL_CODEPROPS, |
---|
815 | ROCMMT_KERNEL_LANGUAGE, ROCMMT_KERNEL_LANGUAGE_VERSION, |
---|
816 | ROCMMT_KERNEL_NAME, ROCMMT_KERNEL_SYMBOLNAME |
---|
817 | }; |
---|
818 | |
---|
819 | static const char* kernelMetadataKeywords[] = |
---|
820 | { |
---|
821 | "Args", "Attrs", "CodeProps", "Language", "LanguageVersion", "Name", "SymbolName" |
---|
822 | }; |
---|
823 | |
---|
824 | static const size_t kernelMetadataKeywordsNum = |
---|
825 | sizeof(kernelMetadataKeywords) / sizeof(const char*); |
---|
826 | |
---|
827 | enum { |
---|
828 | ROCMMT_ATTRS_REQD_WORK_GROUP_SIZE = 0, ROCMMT_ATTRS_RUNTIME_HANDLE, |
---|
829 | ROCMMT_ATTRS_VECTYPEHINT, ROCMMT_ATTRS_WORK_GROUP_SIZE_HINT |
---|
830 | }; |
---|
831 | |
---|
832 | static const char* kernelAttrMetadataKeywords[] = |
---|
833 | { |
---|
834 | "ReqdWorkGroupSize", "RuntimeHandle", "VecTypeHint", "WorkGroupSizeHint" |
---|
835 | }; |
---|
836 | |
---|
837 | static const size_t kernelAttrMetadataKeywordsNum = |
---|
838 | sizeof(kernelAttrMetadataKeywords) / sizeof(const char*); |
---|
839 | |
---|
840 | enum { |
---|
841 | ROCMMT_CODEPROPS_FIXED_WORK_GROUP_SIZE = 0, ROCMMT_CODEPROPS_GROUP_SEGMENT_FIXED_SIZE, |
---|
842 | ROCMMT_CODEPROPS_KERNARG_SEGMENT_ALIGN, ROCMMT_CODEPROPS_KERNARG_SEGMENT_SIZE, |
---|
843 | ROCMMT_CODEPROPS_MAX_FLAT_WORK_GROUP_SIZE, ROCMMT_CODEPROPS_NUM_SGPRS, |
---|
844 | ROCMMT_CODEPROPS_NUM_SPILLED_SGPRS, ROCMMT_CODEPROPS_NUM_SPILLED_VGPRS, |
---|
845 | ROCMMT_CODEPROPS_NUM_VGPRS, ROCMMT_CODEPROPS_PRIVATE_SEGMENT_FIXED_SIZE, |
---|
846 | ROCMMT_CODEPROPS_WAVEFRONT_SIZE |
---|
847 | }; |
---|
848 | |
---|
849 | static const char* kernelCodePropsKeywords[] = |
---|
850 | { |
---|
851 | "FixedWorkGroupSize", "GroupSegmentFixedSize", "KernargSegmentAlign", |
---|
852 | "KernargSegmentSize", "MaxFlatWorkGroupSize", "NumSGPRs", |
---|
853 | "NumSpilledSGPRs", "NumSpilledVGPRs", "NumVGPRs", "PrivateSegmentFixedSize", |
---|
854 | "WavefrontSize" |
---|
855 | }; |
---|
856 | |
---|
857 | static const size_t kernelCodePropsKeywordsNum = |
---|
858 | sizeof(kernelCodePropsKeywords) / sizeof(const char*); |
---|
859 | |
---|
860 | enum { |
---|
861 | ROCMMT_ARGS_ACCQUAL = 0, ROCMMT_ARGS_ACTUALACCQUAL, ROCMMT_ARGS_ADDRSPACEQUAL, |
---|
862 | ROCMMT_ARGS_ALIGN, ROCMMT_ARGS_ISCONST, ROCMMT_ARGS_ISPIPE, ROCMMT_ARGS_ISRESTRICT, |
---|
863 | ROCMMT_ARGS_ISVOLATILE, ROCMMT_ARGS_NAME, ROCMMT_ARGS_POINTEE_ALIGN, |
---|
864 | ROCMMT_ARGS_SIZE, ROCMMT_ARGS_TYPENAME, ROCMMT_ARGS_VALUEKIND, |
---|
865 | ROCMMT_ARGS_VALUETYPE |
---|
866 | }; |
---|
867 | |
---|
868 | static const char* kernelArgInfosKeywords[] = |
---|
869 | { |
---|
870 | "AccQual", "ActualAccQual", "AddrSpaceQual", "Align", "IsConst", "IsPipe", |
---|
871 | "IsRestrict", "IsVolatile", "Name", "PointeeAlign", "Size", "TypeName", |
---|
872 | "ValueKind", "ValueType" |
---|
873 | }; |
---|
874 | |
---|
875 | static const size_t kernelArgInfosKeywordsNum = |
---|
876 | sizeof(kernelArgInfosKeywords) / sizeof(const char*); |
---|
877 | |
---|
878 | static const std::pair<const char*, ROCmValueKind> rocmValueKindNamesMap[] = |
---|
879 | { |
---|
880 | { "ByValue", ROCmValueKind::BY_VALUE }, |
---|
881 | { "DynamicSharedPointer", ROCmValueKind::DYN_SHARED_PTR }, |
---|
882 | { "GlobalBuffer", ROCmValueKind::GLOBAL_BUFFER }, |
---|
883 | { "HiddenCompletionAction", ROCmValueKind::HIDDEN_COMPLETION_ACTION }, |
---|
884 | { "HiddenDefaultQueue", ROCmValueKind::HIDDEN_DEFAULT_QUEUE }, |
---|
885 | { "HiddenGlobalOffsetX", ROCmValueKind::HIDDEN_GLOBAL_OFFSET_X }, |
---|
886 | { "HiddenGlobalOffsetY", ROCmValueKind::HIDDEN_GLOBAL_OFFSET_Y }, |
---|
887 | { "HiddenGlobalOffsetZ", ROCmValueKind::HIDDEN_GLOBAL_OFFSET_Z }, |
---|
888 | { "HiddenNone", ROCmValueKind::HIDDEN_NONE }, |
---|
889 | { "HiddenPrintfBuffer", ROCmValueKind::HIDDEN_PRINTF_BUFFER }, |
---|
890 | { "Image", ROCmValueKind::IMAGE }, |
---|
891 | { "Pipe", ROCmValueKind::PIPE }, |
---|
892 | { "Queue", ROCmValueKind::QUEUE }, |
---|
893 | { "Sampler", ROCmValueKind::SAMPLER } |
---|
894 | }; |
---|
895 | |
---|
896 | static const size_t rocmValueKindNamesNum = |
---|
897 | sizeof(rocmValueKindNamesMap) / sizeof(std::pair<const char*, ROCmValueKind>); |
---|
898 | |
---|
899 | static const std::pair<const char*, ROCmValueType> rocmValueTypeNamesMap[] = |
---|
900 | { |
---|
901 | { "F16", ROCmValueType::FLOAT16 }, |
---|
902 | { "F32", ROCmValueType::FLOAT32 }, |
---|
903 | { "F64", ROCmValueType::FLOAT64 }, |
---|
904 | { "I16", ROCmValueType::INT16 }, |
---|
905 | { "I32", ROCmValueType::INT32 }, |
---|
906 | { "I64", ROCmValueType::INT64 }, |
---|
907 | { "I8", ROCmValueType::INT8 }, |
---|
908 | { "Struct", ROCmValueType::STRUCTURE }, |
---|
909 | { "U16", ROCmValueType::UINT16 }, |
---|
910 | { "U32", ROCmValueType::UINT32 }, |
---|
911 | { "U64", ROCmValueType::UINT64 }, |
---|
912 | { "U8", ROCmValueType::UINT8 } |
---|
913 | }; |
---|
914 | |
---|
915 | static const size_t rocmValueTypeNamesNum = |
---|
916 | sizeof(rocmValueTypeNamesMap) / sizeof(std::pair<const char*, ROCmValueType>); |
---|
917 | |
---|
918 | static const char* rocmAddrSpaceTypesTbl[] = |
---|
919 | { "Private", "Global", "Constant", "Local", "Generic", "Region" }; |
---|
920 | |
---|
921 | static const char* rocmAccessQualifierTbl[] = |
---|
922 | { "Default", "ReadOnly", "WriteOnly", "ReadWrite" }; |
---|
923 | |
---|
924 | static void parseROCmMetadata(size_t metadataSize, const char* metadata, |
---|
925 | ROCmMetadata& metadataInfo) |
---|
926 | { |
---|
927 | const char* ptr = metadata; |
---|
928 | const char* end = metadata + metadataSize; |
---|
929 | size_t lineNo = 1; |
---|
930 | // init metadata info object |
---|
931 | metadataInfo.kernels.clear(); |
---|
932 | metadataInfo.printfInfos.clear(); |
---|
933 | metadataInfo.version[0] = metadataInfo.version[1] = 0; |
---|
934 | |
---|
935 | std::vector<ROCmKernelMetadata>& kernels = metadataInfo.kernels; |
---|
936 | |
---|
937 | cxuint levels[6] = { UINT_MAX, UINT_MAX, UINT_MAX, UINT_MAX, UINT_MAX, UINT_MAX }; |
---|
938 | cxuint curLevel = 0; |
---|
939 | bool inKernels = false; |
---|
940 | bool inKernel = false; |
---|
941 | bool inKernelArgs = false; |
---|
942 | bool inKernelArg = false; |
---|
943 | bool inKernelCodeProps = false; |
---|
944 | bool inKernelAttrs = false; |
---|
945 | bool canToNextLevel = false; |
---|
946 | |
---|
947 | size_t oldLineNo = 0; |
---|
948 | while (ptr != end) |
---|
949 | { |
---|
950 | cxuint level = skipSpacesAndComments(ptr, end, lineNo); |
---|
951 | if (ptr == end || lineNo == oldLineNo) |
---|
952 | throw ParseException(lineNo, "Expected new line"); |
---|
953 | |
---|
954 | if (levels[curLevel] == UINT_MAX) |
---|
955 | levels[curLevel] = level; |
---|
956 | else if (levels[curLevel] < level) |
---|
957 | { |
---|
958 | if (canToNextLevel) |
---|
959 | // go to next nesting level |
---|
960 | levels[++curLevel] = level; |
---|
961 | else |
---|
962 | throw ParseException(lineNo, "Unexpected nesting level"); |
---|
963 | canToNextLevel = false; |
---|
964 | } |
---|
965 | else if (levels[curLevel] > level) |
---|
966 | { |
---|
967 | while (curLevel != UINT_MAX && levels[curLevel] > level) |
---|
968 | curLevel--; |
---|
969 | if (curLevel == UINT_MAX) |
---|
970 | throw ParseException(lineNo, "Indentation smaller than in main level"); |
---|
971 | |
---|
972 | // pop from previous level |
---|
973 | if (curLevel < 3) |
---|
974 | { |
---|
975 | if (inKernelArgs) |
---|
976 | { |
---|
977 | // leave from kernel args |
---|
978 | inKernelArgs = false; |
---|
979 | inKernelArg = false; |
---|
980 | } |
---|
981 | |
---|
982 | inKernelCodeProps = false; |
---|
983 | inKernelAttrs = false; |
---|
984 | } |
---|
985 | if (curLevel < 1 && inKernels) |
---|
986 | { |
---|
987 | // leave from kernels |
---|
988 | inKernels = false; |
---|
989 | inKernel = false; |
---|
990 | } |
---|
991 | |
---|
992 | if (levels[curLevel] != level) |
---|
993 | throw ParseException(lineNo, "Unexpected nesting level"); |
---|
994 | } |
---|
995 | |
---|
996 | oldLineNo = lineNo; |
---|
997 | if (curLevel == 0) |
---|
998 | { |
---|
999 | if (lineNo==1 && ptr+3 <= end && *ptr=='-' && ptr[1]=='-' && ptr[2]=='-' && |
---|
1000 | (ptr+3==end || (ptr+3 < end && ptr[3]=='\n'))) |
---|
1001 | { |
---|
1002 | ptr += 3; |
---|
1003 | if (ptr!=end) |
---|
1004 | { |
---|
1005 | lineNo++; |
---|
1006 | ptr++; // to newline |
---|
1007 | } |
---|
1008 | continue; // skip document start |
---|
1009 | } |
---|
1010 | |
---|
1011 | if (ptr+3 <= end && *ptr=='.' && ptr[1]=='.' && ptr[2]=='.' && |
---|
1012 | (ptr+3==end || (ptr+3 < end && ptr[3]=='\n'))) |
---|
1013 | break; // end of the document |
---|
1014 | |
---|
1015 | const size_t keyIndex = parseYAMLKey(ptr, end, lineNo, |
---|
1016 | mainMetadataKeywordsNum, mainMetadataKeywords); |
---|
1017 | |
---|
1018 | switch(keyIndex) |
---|
1019 | { |
---|
1020 | case ROCMMT_MAIN_KERNELS: |
---|
1021 | inKernels = true; |
---|
1022 | canToNextLevel = true; |
---|
1023 | break; |
---|
1024 | case ROCMMT_MAIN_PRINTF: |
---|
1025 | { |
---|
1026 | YAMLPrintfVectorConsumer consumer(metadataInfo.printfInfos); |
---|
1027 | parseYAMLValArray(ptr, end, lineNo, levels[curLevel], &consumer, true); |
---|
1028 | break; |
---|
1029 | } |
---|
1030 | case ROCMMT_MAIN_VERSION: |
---|
1031 | { |
---|
1032 | YAMLIntArrayConsumer<uint32_t> consumer(2, metadataInfo.version); |
---|
1033 | parseYAMLValArray(ptr, end, lineNo, levels[curLevel], &consumer, true); |
---|
1034 | break; |
---|
1035 | } |
---|
1036 | default: |
---|
1037 | skipYAMLValue(ptr, end, lineNo, level); |
---|
1038 | break; |
---|
1039 | } |
---|
1040 | } |
---|
1041 | |
---|
1042 | if (curLevel==1 && inKernels) |
---|
1043 | { |
---|
1044 | // enter to kernel level |
---|
1045 | if (ptr == end || *ptr != '-') |
---|
1046 | throw ParseException(lineNo, "No '-' before kernel object"); |
---|
1047 | ptr++; |
---|
1048 | const char* afterMinus = ptr; |
---|
1049 | skipSpacesToLineEnd(ptr, end); |
---|
1050 | levels[++curLevel] = level + 1 + ptr-afterMinus; |
---|
1051 | level = levels[curLevel]; |
---|
1052 | inKernel = true; |
---|
1053 | |
---|
1054 | kernels.push_back(ROCmKernelMetadata()); |
---|
1055 | kernels.back().initialize(); |
---|
1056 | } |
---|
1057 | |
---|
1058 | if (curLevel==2 && inKernel) |
---|
1059 | { |
---|
1060 | // in kernel |
---|
1061 | const size_t keyIndex = parseYAMLKey(ptr, end, lineNo, |
---|
1062 | kernelMetadataKeywordsNum, kernelMetadataKeywords); |
---|
1063 | |
---|
1064 | ROCmKernelMetadata& kernel = kernels.back(); |
---|
1065 | switch(keyIndex) |
---|
1066 | { |
---|
1067 | case ROCMMT_KERNEL_ARGS: |
---|
1068 | inKernelArgs = true; |
---|
1069 | canToNextLevel = true; |
---|
1070 | kernel.argInfos.clear(); |
---|
1071 | break; |
---|
1072 | case ROCMMT_KERNEL_ATTRS: |
---|
1073 | inKernelAttrs = true; |
---|
1074 | canToNextLevel = true; |
---|
1075 | // initialize kernel attributes values |
---|
1076 | kernel.reqdWorkGroupSize[0] = 0; |
---|
1077 | kernel.reqdWorkGroupSize[1] = 0; |
---|
1078 | kernel.reqdWorkGroupSize[2] = 0; |
---|
1079 | kernel.workGroupSizeHint[0] = 0; |
---|
1080 | kernel.workGroupSizeHint[1] = 0; |
---|
1081 | kernel.workGroupSizeHint[2] = 0; |
---|
1082 | kernel.runtimeHandle.clear(); |
---|
1083 | kernel.vecTypeHint.clear(); |
---|
1084 | break; |
---|
1085 | case ROCMMT_KERNEL_CODEPROPS: |
---|
1086 | // initialize CodeProps values |
---|
1087 | kernel.kernargSegmentSize = BINGEN64_DEFAULT; |
---|
1088 | kernel.groupSegmentFixedSize = BINGEN64_DEFAULT; |
---|
1089 | kernel.privateSegmentFixedSize = BINGEN64_DEFAULT; |
---|
1090 | kernel.kernargSegmentAlign = BINGEN64_DEFAULT; |
---|
1091 | kernel.wavefrontSize = BINGEN_DEFAULT; |
---|
1092 | kernel.sgprsNum = BINGEN_DEFAULT; |
---|
1093 | kernel.vgprsNum = BINGEN_DEFAULT; |
---|
1094 | kernel.spilledSgprs = BINGEN_NOTSUPPLIED; |
---|
1095 | kernel.spilledVgprs = BINGEN_NOTSUPPLIED; |
---|
1096 | kernel.maxFlatWorkGroupSize = BINGEN64_DEFAULT; |
---|
1097 | kernel.fixedWorkGroupSize[0] = 0; |
---|
1098 | kernel.fixedWorkGroupSize[1] = 0; |
---|
1099 | kernel.fixedWorkGroupSize[2] = 0; |
---|
1100 | inKernelCodeProps = true; |
---|
1101 | canToNextLevel = true; |
---|
1102 | break; |
---|
1103 | case ROCMMT_KERNEL_LANGUAGE: |
---|
1104 | kernel.language = parseYAMLStringValue(ptr, end, lineNo, level, true); |
---|
1105 | break; |
---|
1106 | case ROCMMT_KERNEL_LANGUAGE_VERSION: |
---|
1107 | { |
---|
1108 | YAMLIntArrayConsumer<uint32_t> consumer(2, kernel.langVersion); |
---|
1109 | parseYAMLValArray(ptr, end, lineNo, levels[curLevel], &consumer); |
---|
1110 | break; |
---|
1111 | } |
---|
1112 | case ROCMMT_KERNEL_NAME: |
---|
1113 | kernel.name = parseYAMLStringValue(ptr, end, lineNo, level, true); |
---|
1114 | break; |
---|
1115 | case ROCMMT_KERNEL_SYMBOLNAME: |
---|
1116 | kernel.symbolName = parseYAMLStringValue(ptr, end, lineNo, level, true); |
---|
1117 | break; |
---|
1118 | default: |
---|
1119 | skipYAMLValue(ptr, end, lineNo, level); |
---|
1120 | break; |
---|
1121 | } |
---|
1122 | } |
---|
1123 | |
---|
1124 | if (curLevel==3 && inKernelAttrs) |
---|
1125 | { |
---|
1126 | // in kernel attributes |
---|
1127 | const size_t keyIndex = parseYAMLKey(ptr, end, lineNo, |
---|
1128 | kernelAttrMetadataKeywordsNum, kernelAttrMetadataKeywords); |
---|
1129 | |
---|
1130 | ROCmKernelMetadata& kernel = kernels.back(); |
---|
1131 | switch(keyIndex) |
---|
1132 | { |
---|
1133 | case ROCMMT_ATTRS_REQD_WORK_GROUP_SIZE: |
---|
1134 | { |
---|
1135 | YAMLIntArrayConsumer<cxuint> consumer(3, kernel.reqdWorkGroupSize); |
---|
1136 | parseYAMLValArray(ptr, end, lineNo, level, &consumer); |
---|
1137 | break; |
---|
1138 | } |
---|
1139 | case ROCMMT_ATTRS_RUNTIME_HANDLE: |
---|
1140 | kernel.runtimeHandle = parseYAMLStringValue( |
---|
1141 | ptr, end, lineNo, level, true); |
---|
1142 | break; |
---|
1143 | case ROCMMT_ATTRS_VECTYPEHINT: |
---|
1144 | kernel.vecTypeHint = parseYAMLStringValue( |
---|
1145 | ptr, end, lineNo, level, true); |
---|
1146 | break; |
---|
1147 | case ROCMMT_ATTRS_WORK_GROUP_SIZE_HINT: |
---|
1148 | { |
---|
1149 | YAMLIntArrayConsumer<cxuint> consumer(3, kernel.workGroupSizeHint); |
---|
1150 | parseYAMLValArray(ptr, end, lineNo, level, &consumer, true); |
---|
1151 | break; |
---|
1152 | } |
---|
1153 | default: |
---|
1154 | skipYAMLValue(ptr, end, lineNo, level); |
---|
1155 | break; |
---|
1156 | } |
---|
1157 | } |
---|
1158 | |
---|
1159 | if (curLevel==3 && inKernelCodeProps) |
---|
1160 | { |
---|
1161 | // in kernel codeProps |
---|
1162 | const size_t keyIndex = parseYAMLKey(ptr, end, lineNo, |
---|
1163 | kernelCodePropsKeywordsNum, kernelCodePropsKeywords); |
---|
1164 | |
---|
1165 | ROCmKernelMetadata& kernel = kernels.back(); |
---|
1166 | switch(keyIndex) |
---|
1167 | { |
---|
1168 | case ROCMMT_CODEPROPS_FIXED_WORK_GROUP_SIZE: |
---|
1169 | { |
---|
1170 | YAMLIntArrayConsumer<cxuint> consumer(3, kernel.fixedWorkGroupSize); |
---|
1171 | parseYAMLValArray(ptr, end, lineNo, level, &consumer); |
---|
1172 | break; |
---|
1173 | } |
---|
1174 | case ROCMMT_CODEPROPS_GROUP_SEGMENT_FIXED_SIZE: |
---|
1175 | kernel.groupSegmentFixedSize = |
---|
1176 | parseYAMLIntValue<cxuint>(ptr, end, lineNo, true); |
---|
1177 | break; |
---|
1178 | case ROCMMT_CODEPROPS_KERNARG_SEGMENT_ALIGN: |
---|
1179 | kernel.kernargSegmentAlign = |
---|
1180 | parseYAMLIntValue<uint64_t>(ptr, end, lineNo, true); |
---|
1181 | break; |
---|
1182 | case ROCMMT_CODEPROPS_KERNARG_SEGMENT_SIZE: |
---|
1183 | kernel.kernargSegmentSize = |
---|
1184 | parseYAMLIntValue<uint64_t>(ptr, end, lineNo, true); |
---|
1185 | break; |
---|
1186 | case ROCMMT_CODEPROPS_MAX_FLAT_WORK_GROUP_SIZE: |
---|
1187 | kernel.maxFlatWorkGroupSize = |
---|
1188 | parseYAMLIntValue<uint64_t>(ptr, end, lineNo, true); |
---|
1189 | break; |
---|
1190 | case ROCMMT_CODEPROPS_NUM_SGPRS: |
---|
1191 | kernel.sgprsNum = parseYAMLIntValue<cxuint>(ptr, end, lineNo, true); |
---|
1192 | break; |
---|
1193 | case ROCMMT_CODEPROPS_NUM_SPILLED_SGPRS: |
---|
1194 | kernel.spilledSgprs = |
---|
1195 | parseYAMLIntValue<cxuint>(ptr, end, lineNo, true); |
---|
1196 | break; |
---|
1197 | case ROCMMT_CODEPROPS_NUM_SPILLED_VGPRS: |
---|
1198 | kernel.spilledVgprs = |
---|
1199 | parseYAMLIntValue<cxuint>(ptr, end, lineNo, true); |
---|
1200 | break; |
---|
1201 | case ROCMMT_CODEPROPS_NUM_VGPRS: |
---|
1202 | kernel.vgprsNum = parseYAMLIntValue<cxuint>(ptr, end, lineNo, true); |
---|
1203 | break; |
---|
1204 | case ROCMMT_CODEPROPS_PRIVATE_SEGMENT_FIXED_SIZE: |
---|
1205 | kernel.privateSegmentFixedSize = |
---|
1206 | parseYAMLIntValue<uint64_t>(ptr, end, lineNo, true); |
---|
1207 | break; |
---|
1208 | case ROCMMT_CODEPROPS_WAVEFRONT_SIZE: |
---|
1209 | kernel.wavefrontSize = |
---|
1210 | parseYAMLIntValue<cxuint>(ptr, end, lineNo, true); |
---|
1211 | break; |
---|
1212 | default: |
---|
1213 | skipYAMLValue(ptr, end, lineNo, level); |
---|
1214 | break; |
---|
1215 | } |
---|
1216 | } |
---|
1217 | |
---|
1218 | if (curLevel==3 && inKernelArgs) |
---|
1219 | { |
---|
1220 | // enter to kernel argument level |
---|
1221 | if (ptr == end || *ptr != '-') |
---|
1222 | throw ParseException(lineNo, "No '-' before argument object"); |
---|
1223 | ptr++; |
---|
1224 | const char* afterMinus = ptr; |
---|
1225 | skipSpacesToLineEnd(ptr, end); |
---|
1226 | levels[++curLevel] = level + 1 + ptr-afterMinus; |
---|
1227 | level = levels[curLevel]; |
---|
1228 | inKernelArg = true; |
---|
1229 | |
---|
1230 | kernels.back().argInfos.push_back(ROCmKernelArgInfo{}); |
---|
1231 | } |
---|
1232 | |
---|
1233 | if (curLevel==4 && inKernelArg) |
---|
1234 | { |
---|
1235 | // in kernel argument |
---|
1236 | const size_t keyIndex = parseYAMLKey(ptr, end, lineNo, |
---|
1237 | kernelArgInfosKeywordsNum, kernelArgInfosKeywords); |
---|
1238 | |
---|
1239 | ROCmKernelArgInfo& kernelArg = kernels.back().argInfos.back(); |
---|
1240 | |
---|
1241 | size_t valLineNo = lineNo; |
---|
1242 | switch(keyIndex) |
---|
1243 | { |
---|
1244 | case ROCMMT_ARGS_ACCQUAL: |
---|
1245 | case ROCMMT_ARGS_ACTUALACCQUAL: |
---|
1246 | { |
---|
1247 | const std::string acc = trimStrSpaces(parseYAMLStringValue( |
---|
1248 | ptr, end, lineNo, level, true)); |
---|
1249 | size_t accIndex = 0; |
---|
1250 | for (; accIndex < 6; accIndex++) |
---|
1251 | if (::strcmp(rocmAccessQualifierTbl[accIndex], acc.c_str())==0) |
---|
1252 | break; |
---|
1253 | if (accIndex == 4) |
---|
1254 | throw ParseException(lineNo, "Wrong access qualifier"); |
---|
1255 | if (keyIndex == ROCMMT_ARGS_ACCQUAL) |
---|
1256 | kernelArg.accessQual = ROCmAccessQual(accIndex); |
---|
1257 | else |
---|
1258 | kernelArg.actualAccessQual = ROCmAccessQual(accIndex); |
---|
1259 | break; |
---|
1260 | } |
---|
1261 | case ROCMMT_ARGS_ADDRSPACEQUAL: |
---|
1262 | { |
---|
1263 | const std::string aspace = trimStrSpaces(parseYAMLStringValue( |
---|
1264 | ptr, end, lineNo, level, true)); |
---|
1265 | size_t aspaceIndex = 0; |
---|
1266 | for (; aspaceIndex < 6; aspaceIndex++) |
---|
1267 | if (::strcmp(rocmAddrSpaceTypesTbl[aspaceIndex], |
---|
1268 | aspace.c_str())==0) |
---|
1269 | break; |
---|
1270 | if (aspaceIndex == 6) |
---|
1271 | throw ParseException(valLineNo, "Wrong address space"); |
---|
1272 | kernelArg.addressSpace = ROCmAddressSpace(aspaceIndex+1); |
---|
1273 | break; |
---|
1274 | } |
---|
1275 | case ROCMMT_ARGS_ALIGN: |
---|
1276 | kernelArg.align = parseYAMLIntValue<uint64_t>(ptr, end, lineNo, true); |
---|
1277 | break; |
---|
1278 | case ROCMMT_ARGS_ISCONST: |
---|
1279 | kernelArg.isConst = parseYAMLBoolValue(ptr, end, lineNo, true); |
---|
1280 | break; |
---|
1281 | case ROCMMT_ARGS_ISPIPE: |
---|
1282 | kernelArg.isPipe = parseYAMLBoolValue(ptr, end, lineNo, true); |
---|
1283 | break; |
---|
1284 | case ROCMMT_ARGS_ISRESTRICT: |
---|
1285 | kernelArg.isRestrict = parseYAMLBoolValue(ptr, end, lineNo, true); |
---|
1286 | break; |
---|
1287 | case ROCMMT_ARGS_ISVOLATILE: |
---|
1288 | kernelArg.isVolatile = parseYAMLBoolValue(ptr, end, lineNo, true); |
---|
1289 | break; |
---|
1290 | case ROCMMT_ARGS_NAME: |
---|
1291 | kernelArg.name = parseYAMLStringValue(ptr, end, lineNo, level, true); |
---|
1292 | break; |
---|
1293 | case ROCMMT_ARGS_POINTEE_ALIGN: |
---|
1294 | kernelArg.pointeeAlign = |
---|
1295 | parseYAMLIntValue<uint64_t>(ptr, end, lineNo, true); |
---|
1296 | break; |
---|
1297 | case ROCMMT_ARGS_SIZE: |
---|
1298 | kernelArg.size = parseYAMLIntValue<uint64_t>(ptr, end, lineNo); |
---|
1299 | break; |
---|
1300 | case ROCMMT_ARGS_TYPENAME: |
---|
1301 | kernelArg.typeName = |
---|
1302 | parseYAMLStringValue(ptr, end, lineNo, level, true); |
---|
1303 | break; |
---|
1304 | case ROCMMT_ARGS_VALUEKIND: |
---|
1305 | { |
---|
1306 | const std::string vkind = trimStrSpaces(parseYAMLStringValue( |
---|
1307 | ptr, end, lineNo, level, true)); |
---|
1308 | const size_t vkindIndex = binaryMapFind(rocmValueKindNamesMap, |
---|
1309 | rocmValueKindNamesMap + rocmValueKindNamesNum, vkind.c_str(), |
---|
1310 | CStringLess()) - rocmValueKindNamesMap; |
---|
1311 | // if unknown kind |
---|
1312 | if (vkindIndex == rocmValueKindNamesNum) |
---|
1313 | throw ParseException(valLineNo, "Wrong argument value kind"); |
---|
1314 | kernelArg.valueKind = rocmValueKindNamesMap[vkindIndex].second; |
---|
1315 | break; |
---|
1316 | } |
---|
1317 | case ROCMMT_ARGS_VALUETYPE: |
---|
1318 | { |
---|
1319 | const std::string vtype = trimStrSpaces(parseYAMLStringValue( |
---|
1320 | ptr, end, lineNo, level, true)); |
---|
1321 | const size_t vtypeIndex = binaryMapFind(rocmValueTypeNamesMap, |
---|
1322 | rocmValueTypeNamesMap + rocmValueTypeNamesNum, vtype.c_str(), |
---|
1323 | CStringLess()) - rocmValueTypeNamesMap; |
---|
1324 | // if unknown type |
---|
1325 | if (vtypeIndex == rocmValueTypeNamesNum) |
---|
1326 | throw ParseException(valLineNo, "Wrong argument value type"); |
---|
1327 | kernelArg.valueType = rocmValueTypeNamesMap[vtypeIndex].second; |
---|
1328 | break; |
---|
1329 | } |
---|
1330 | default: |
---|
1331 | skipYAMLValue(ptr, end, lineNo, level); |
---|
1332 | break; |
---|
1333 | } |
---|
1334 | } |
---|
1335 | } |
---|
1336 | } |
---|
1337 | |
---|
1338 | void ROCmMetadata::parse(size_t metadataSize, const char* metadata) |
---|
1339 | { |
---|
1340 | parseROCmMetadata(metadataSize, metadata, *this); |
---|
1341 | } |
---|
1342 | |
---|
1343 | /* |
---|
1344 | * ROCm binary reader and generator |
---|
1345 | */ |
---|
1346 | |
---|
1347 | /* TODO: add support for various kernel code offset (now only 256 is supported) */ |
---|
1348 | |
---|
1349 | ROCmBinary::ROCmBinary(size_t binaryCodeSize, cxbyte* binaryCode, Flags creationFlags) |
---|
1350 | : ElfBinary64(binaryCodeSize, binaryCode, creationFlags), |
---|
1351 | regionsNum(0), codeSize(0), code(nullptr), |
---|
1352 | globalDataSize(0), globalData(nullptr), metadataSize(0), metadata(nullptr), |
---|
1353 | newBinFormat(false) |
---|
1354 | { |
---|
1355 | cxuint textIndex = SHN_UNDEF; |
---|
1356 | try |
---|
1357 | { textIndex = getSectionIndex(".text"); } |
---|
1358 | catch(const Exception& ex) |
---|
1359 | { } // ignore failed |
---|
1360 | uint64_t codeOffset = 0; |
---|
1361 | // find '.text' section |
---|
1362 | if (textIndex!=SHN_UNDEF) |
---|
1363 | { |
---|
1364 | code = getSectionContent(textIndex); |
---|
1365 | const Elf64_Shdr& textShdr = getSectionHeader(textIndex); |
---|
1366 | codeSize = ULEV(textShdr.sh_size); |
---|
1367 | codeOffset = ULEV(textShdr.sh_offset); |
---|
1368 | } |
---|
1369 | |
---|
1370 | cxuint rodataIndex = SHN_UNDEF; |
---|
1371 | try |
---|
1372 | { rodataIndex = getSectionIndex(".rodata"); } |
---|
1373 | catch(const Exception& ex) |
---|
1374 | { } // ignore failed |
---|
1375 | // find '.text' section |
---|
1376 | if (rodataIndex!=SHN_UNDEF) |
---|
1377 | { |
---|
1378 | globalData = getSectionContent(rodataIndex); |
---|
1379 | const Elf64_Shdr& rodataShdr = getSectionHeader(rodataIndex); |
---|
1380 | globalDataSize = ULEV(rodataShdr.sh_size); |
---|
1381 | } |
---|
1382 | |
---|
1383 | cxuint gpuConfigIndex = SHN_UNDEF; |
---|
1384 | try |
---|
1385 | { gpuConfigIndex = getSectionIndex(".AMDGPU.config"); } |
---|
1386 | catch(const Exception& ex) |
---|
1387 | { } // ignore failed |
---|
1388 | newBinFormat = (gpuConfigIndex == SHN_UNDEF); |
---|
1389 | |
---|
1390 | // counts regions (symbol or kernel) |
---|
1391 | regionsNum = 0; |
---|
1392 | const size_t symbolsNum = getSymbolsNum(); |
---|
1393 | for (size_t i = 0; i < symbolsNum; i++) |
---|
1394 | { |
---|
1395 | // count regions number |
---|
1396 | const Elf64_Sym& sym = getSymbol(i); |
---|
1397 | const cxbyte symType = ELF64_ST_TYPE(sym.st_info); |
---|
1398 | const cxbyte bind = ELF64_ST_BIND(sym.st_info); |
---|
1399 | if (ULEV(sym.st_shndx)==textIndex && |
---|
1400 | (symType==STT_GNU_IFUNC || symType==STT_FUNC || |
---|
1401 | (bind==STB_GLOBAL && symType==STT_OBJECT))) |
---|
1402 | regionsNum++; |
---|
1403 | } |
---|
1404 | if (code==nullptr && regionsNum!=0) |
---|
1405 | throw BinException("No code if regions number is not zero"); |
---|
1406 | regions.reset(new ROCmRegion[regionsNum]); |
---|
1407 | size_t j = 0; |
---|
1408 | typedef std::pair<uint64_t, size_t> RegionOffsetEntry; |
---|
1409 | std::unique_ptr<RegionOffsetEntry[]> symOffsets(new RegionOffsetEntry[regionsNum]); |
---|
1410 | |
---|
1411 | // get regions info |
---|
1412 | for (size_t i = 0; i < symbolsNum; i++) |
---|
1413 | { |
---|
1414 | const Elf64_Sym& sym = getSymbol(i); |
---|
1415 | if (ULEV(sym.st_shndx)!=textIndex) |
---|
1416 | continue; // if not in '.text' section |
---|
1417 | const size_t value = ULEV(sym.st_value); |
---|
1418 | if (value < codeOffset) |
---|
1419 | throw BinException("Region offset is too small!"); |
---|
1420 | const size_t size = ULEV(sym.st_size); |
---|
1421 | |
---|
1422 | const cxbyte symType = ELF64_ST_TYPE(sym.st_info); |
---|
1423 | const cxbyte bind = ELF64_ST_BIND(sym.st_info); |
---|
1424 | if (symType==STT_GNU_IFUNC || symType==STT_FUNC || |
---|
1425 | (bind==STB_GLOBAL && symType==STT_OBJECT)) |
---|
1426 | { |
---|
1427 | ROCmRegionType type = ROCmRegionType::DATA; |
---|
1428 | // if kernel |
---|
1429 | if (symType==STT_GNU_IFUNC) |
---|
1430 | type = ROCmRegionType::KERNEL; |
---|
1431 | // if function kernel |
---|
1432 | else if (symType==STT_FUNC) |
---|
1433 | type = ROCmRegionType::FKERNEL; |
---|
1434 | symOffsets[j] = std::make_pair(value, j); |
---|
1435 | if (type!=ROCmRegionType::DATA && value+0x100 > codeOffset+codeSize) |
---|
1436 | throw BinException("Kernel or code offset is too big!"); |
---|
1437 | regions[j++] = { getSymbolName(i), size, value, type }; |
---|
1438 | } |
---|
1439 | } |
---|
1440 | // sort regions by offset |
---|
1441 | std::sort(symOffsets.get(), symOffsets.get()+regionsNum, |
---|
1442 | [](const RegionOffsetEntry& a, const RegionOffsetEntry& b) |
---|
1443 | { return a.first < b.first; }); |
---|
1444 | // checking distance between regions |
---|
1445 | for (size_t i = 1; i <= regionsNum; i++) |
---|
1446 | { |
---|
1447 | size_t end = (i<regionsNum) ? symOffsets[i].first : codeOffset+codeSize; |
---|
1448 | ROCmRegion& region = regions[symOffsets[i-1].second]; |
---|
1449 | if (region.type==ROCmRegionType::KERNEL && symOffsets[i-1].first+0x100 > end) |
---|
1450 | throw BinException("Kernel size is too small!"); |
---|
1451 | |
---|
1452 | const size_t regSize = end - symOffsets[i-1].first; |
---|
1453 | if (region.size==0) |
---|
1454 | region.size = regSize; |
---|
1455 | else |
---|
1456 | region.size = std::min(regSize, region.size); |
---|
1457 | } |
---|
1458 | |
---|
1459 | // get metadata |
---|
1460 | const size_t notesSize = getNotesSize(); |
---|
1461 | const cxbyte* noteContent = (const cxbyte*)getNotes(); |
---|
1462 | |
---|
1463 | for (size_t offset = 0; offset < notesSize; ) |
---|
1464 | { |
---|
1465 | const Elf64_Nhdr* nhdr = (const Elf64_Nhdr*)(noteContent + offset); |
---|
1466 | size_t namesz = ULEV(nhdr->n_namesz); |
---|
1467 | size_t descsz = ULEV(nhdr->n_descsz); |
---|
1468 | if (usumGt(offset, namesz+descsz, notesSize)) |
---|
1469 | throw BinException("Note offset+size out of range"); |
---|
1470 | |
---|
1471 | if (namesz==4 && |
---|
1472 | ::strcmp((const char*)noteContent+offset+ sizeof(Elf64_Nhdr), "AMD")==0) |
---|
1473 | { |
---|
1474 | const uint32_t noteType = ULEV(nhdr->n_type); |
---|
1475 | if (noteType == 0xa) |
---|
1476 | { |
---|
1477 | metadata = (char*)(noteContent+offset+sizeof(Elf64_Nhdr) + 4); |
---|
1478 | metadataSize = descsz; |
---|
1479 | } |
---|
1480 | else if (noteType == 0xb) |
---|
1481 | target.assign((char*)(noteContent+offset+sizeof(Elf64_Nhdr) + 4), descsz); |
---|
1482 | } |
---|
1483 | size_t align = (((namesz+descsz)&3)!=0) ? 4-((namesz+descsz)&3) : 0; |
---|
1484 | offset += sizeof(Elf64_Nhdr) + namesz + descsz + align; |
---|
1485 | } |
---|
1486 | |
---|
1487 | if (hasRegionMap()) |
---|
1488 | { |
---|
1489 | // create region map |
---|
1490 | regionsMap.resize(regionsNum); |
---|
1491 | for (size_t i = 0; i < regionsNum; i++) |
---|
1492 | regionsMap[i] = std::make_pair(regions[i].regionName, i); |
---|
1493 | // sort region map |
---|
1494 | mapSort(regionsMap.begin(), regionsMap.end()); |
---|
1495 | } |
---|
1496 | |
---|
1497 | if ((creationFlags & ROCMBIN_CREATE_METADATAINFO) != 0 && |
---|
1498 | metadata != nullptr && metadataSize != 0) |
---|
1499 | { |
---|
1500 | metadataInfo.reset(new ROCmMetadata()); |
---|
1501 | parseROCmMetadata(metadataSize, metadata, *metadataInfo); |
---|
1502 | |
---|
1503 | if (hasKernelInfoMap()) |
---|
1504 | { |
---|
1505 | const std::vector<ROCmKernelMetadata>& kernels = metadataInfo->kernels; |
---|
1506 | kernelInfosMap.resize(kernels.size()); |
---|
1507 | for (size_t i = 0; i < kernelInfosMap.size(); i++) |
---|
1508 | kernelInfosMap[i] = std::make_pair(kernels[i].name, i); |
---|
1509 | // sort region map |
---|
1510 | mapSort(kernelInfosMap.begin(), kernelInfosMap.end()); |
---|
1511 | } |
---|
1512 | } |
---|
1513 | } |
---|
1514 | |
---|
1515 | /// determint GPU device from ROCm notes |
---|
1516 | GPUDeviceType ROCmBinary::determineGPUDeviceType(uint32_t& outArchMinor, |
---|
1517 | uint32_t& outArchStepping) const |
---|
1518 | { |
---|
1519 | uint32_t archMajor = 0; |
---|
1520 | uint32_t archMinor = 0; |
---|
1521 | uint32_t archStepping = 0; |
---|
1522 | |
---|
1523 | { |
---|
1524 | const cxbyte* noteContent = (const cxbyte*)getNotes(); |
---|
1525 | if (noteContent==nullptr) |
---|
1526 | throw BinException("Missing notes in inner binary!"); |
---|
1527 | size_t notesSize = getNotesSize(); |
---|
1528 | // find note about AMDGPU |
---|
1529 | for (size_t offset = 0; offset < notesSize; ) |
---|
1530 | { |
---|
1531 | const Elf64_Nhdr* nhdr = (const Elf64_Nhdr*)(noteContent + offset); |
---|
1532 | size_t namesz = ULEV(nhdr->n_namesz); |
---|
1533 | size_t descsz = ULEV(nhdr->n_descsz); |
---|
1534 | if (usumGt(offset, namesz+descsz, notesSize)) |
---|
1535 | throw BinException("Note offset+size out of range"); |
---|
1536 | if (ULEV(nhdr->n_type) == 0x3 && namesz==4 && descsz>=0x1a && |
---|
1537 | ::strcmp((const char*)noteContent+offset+sizeof(Elf64_Nhdr), "AMD")==0) |
---|
1538 | { // AMDGPU type |
---|
1539 | const uint32_t* content = (const uint32_t*) |
---|
1540 | (noteContent+offset+sizeof(Elf64_Nhdr) + 4); |
---|
1541 | archMajor = ULEV(content[1]); |
---|
1542 | archMinor = ULEV(content[2]); |
---|
1543 | archStepping = ULEV(content[3]); |
---|
1544 | } |
---|
1545 | size_t align = (((namesz+descsz)&3)!=0) ? 4-((namesz+descsz)&3) : 0; |
---|
1546 | offset += sizeof(Elf64_Nhdr) + namesz + descsz + align; |
---|
1547 | } |
---|
1548 | } |
---|
1549 | // determine device type |
---|
1550 | GPUDeviceType deviceType = getGPUDeviceTypeFromArchVersion(archMajor, archMinor, |
---|
1551 | archStepping); |
---|
1552 | outArchMinor = archMinor; |
---|
1553 | outArchStepping = archStepping; |
---|
1554 | return deviceType; |
---|
1555 | } |
---|
1556 | |
---|
1557 | const ROCmRegion& ROCmBinary::getRegion(const char* name) const |
---|
1558 | { |
---|
1559 | RegionMap::const_iterator it = binaryMapFind(regionsMap.begin(), |
---|
1560 | regionsMap.end(), name); |
---|
1561 | if (it == regionsMap.end()) |
---|
1562 | throw BinException("Can't find region name"); |
---|
1563 | return regions[it->second]; |
---|
1564 | } |
---|
1565 | |
---|
1566 | const ROCmKernelMetadata& ROCmBinary::getKernelInfo(const char* name) const |
---|
1567 | { |
---|
1568 | if (!hasMetadataInfo()) |
---|
1569 | throw BinException("Can't find kernel info name"); |
---|
1570 | RegionMap::const_iterator it = binaryMapFind(kernelInfosMap.begin(), |
---|
1571 | kernelInfosMap.end(), name); |
---|
1572 | if (it == kernelInfosMap.end()) |
---|
1573 | throw BinException("Can't find kernel info name"); |
---|
1574 | return metadataInfo->kernels[it->second]; |
---|
1575 | } |
---|
1576 | |
---|
1577 | // if ROCm binary |
---|
1578 | bool CLRX::isROCmBinary(size_t binarySize, const cxbyte* binary) |
---|
1579 | { |
---|
1580 | if (!isElfBinary(binarySize, binary)) |
---|
1581 | return false; |
---|
1582 | if (binary[EI_CLASS] != ELFCLASS64) |
---|
1583 | return false; |
---|
1584 | const Elf64_Ehdr* ehdr = reinterpret_cast<const Elf64_Ehdr*>(binary); |
---|
1585 | if (ULEV(ehdr->e_machine) != 0xe0) |
---|
1586 | return false; |
---|
1587 | return true; |
---|
1588 | } |
---|
1589 | |
---|
1590 | |
---|
1591 | void ROCmInput::addEmptyKernel(const char* kernelName) |
---|
1592 | { |
---|
1593 | symbols.push_back({ kernelName, 0, 0, ROCmRegionType::KERNEL }); |
---|
1594 | } |
---|
1595 | |
---|
1596 | /* |
---|
1597 | * ROCm YAML metadata generator |
---|
1598 | */ |
---|
1599 | |
---|
1600 | static const char* rocmValueKindNames[] = |
---|
1601 | { |
---|
1602 | "ByValue", "GlobalBuffer", "DynamicSharedPointer", "Sampler", "Image", "Pipe", "Queue", |
---|
1603 | "HiddenGlobalOffsetX", "HiddenGlobalOffsetY", "HiddenGlobalOffsetZ", "HiddenNone", |
---|
1604 | "HiddenPrintfBuffer", "HiddenDefaultQueue", "HiddenCompletionAction" |
---|
1605 | }; |
---|
1606 | |
---|
1607 | static const char* rocmValueTypeNames[] = |
---|
1608 | { |
---|
1609 | "Struct", "I8", "U8", "I16", "U16", "F16", "I32", "U32", "F32", "I64", "U64", "F64" |
---|
1610 | }; |
---|
1611 | |
---|
1612 | static void genArrayValue(cxuint n, const cxuint* values, std::string& output) |
---|
1613 | { |
---|
1614 | char numBuf[24]; |
---|
1615 | output += "[ "; |
---|
1616 | for (cxuint i = 0; i < n; i++) |
---|
1617 | { |
---|
1618 | itocstrCStyle(values[i], numBuf, 24); |
---|
1619 | output += numBuf; |
---|
1620 | output += (i+1<n) ? ", " : " ]\n"; |
---|
1621 | } |
---|
1622 | } |
---|
1623 | |
---|
1624 | // helper for checking whether value is supplied |
---|
1625 | static inline bool hasValue(cxuint value) |
---|
1626 | { return value!=BINGEN_NOTSUPPLIED && value!=BINGEN_DEFAULT; } |
---|
1627 | |
---|
1628 | static inline bool hasValue(uint64_t value) |
---|
1629 | { return value!=BINGEN64_NOTSUPPLIED && value!=BINGEN64_DEFAULT; } |
---|
1630 | |
---|
1631 | // get escaped YAML string if needed, otherwise get this same string |
---|
1632 | static std::string escapeYAMLString(const CString& input) |
---|
1633 | { |
---|
1634 | bool toEscape = false; |
---|
1635 | const char* s; |
---|
1636 | for (s = input.c_str(); *s!=0; s++) |
---|
1637 | { |
---|
1638 | cxbyte c = *s; |
---|
1639 | if (c < 0x20 || c >= 0x80 || c=='*' || c=='&' || c=='!' || c=='@' || |
---|
1640 | c=='\'' || c=='\"') |
---|
1641 | toEscape = true; |
---|
1642 | } |
---|
1643 | // if spaces in begin and end |
---|
1644 | if (isSpace(input[0]) || isDigit(input[0]) || |
---|
1645 | (!input.empty() && isSpace(s[-1]))) |
---|
1646 | toEscape = true; |
---|
1647 | |
---|
1648 | if (toEscape) |
---|
1649 | { |
---|
1650 | std::string out = "'"; |
---|
1651 | out += escapeStringCStyle(s-input.c_str(), input.c_str()); |
---|
1652 | out += "'"; |
---|
1653 | return out; |
---|
1654 | } |
---|
1655 | return input.c_str(); |
---|
1656 | } |
---|
1657 | |
---|
1658 | static std::string escapePrintfFormat(const std::string& fmt) |
---|
1659 | { |
---|
1660 | std::string out; |
---|
1661 | out.reserve(fmt.size()); |
---|
1662 | for (char c: fmt) |
---|
1663 | if (c!=':') |
---|
1664 | out.push_back(c); |
---|
1665 | else |
---|
1666 | out += "\\72"; |
---|
1667 | return out; |
---|
1668 | } |
---|
1669 | |
---|
1670 | static void generateROCmMetadata(const ROCmMetadata& mdInfo, |
---|
1671 | const ROCmKernelConfig** kconfigs, std::string& output) |
---|
1672 | { |
---|
1673 | output.clear(); |
---|
1674 | char numBuf[24]; |
---|
1675 | output += "---\n"; |
---|
1676 | // version |
---|
1677 | output += "Version: "; |
---|
1678 | if (hasValue(mdInfo.version[0])) |
---|
1679 | genArrayValue(2, mdInfo.version, output); |
---|
1680 | else // default |
---|
1681 | output += "[ 1, 0 ]\n"; |
---|
1682 | if (!mdInfo.printfInfos.empty()) |
---|
1683 | output += "Printf: \n"; |
---|
1684 | // check print ids uniquness |
---|
1685 | { |
---|
1686 | std::unordered_set<cxuint> printfIds; |
---|
1687 | for (const ROCmPrintfInfo& printfInfo: mdInfo.printfInfos) |
---|
1688 | if (printfInfo.id!=BINGEN_DEFAULT) |
---|
1689 | if (!printfIds.insert(printfInfo.id).second) |
---|
1690 | throw BinGenException("Duplicate of printf id"); |
---|
1691 | // printfs |
---|
1692 | uint32_t freePrintfId = 1; |
---|
1693 | for (const ROCmPrintfInfo& printfInfo: mdInfo.printfInfos) |
---|
1694 | { |
---|
1695 | // skip used printfids; |
---|
1696 | uint32_t printfId = printfInfo.id; |
---|
1697 | if (printfId == BINGEN_DEFAULT) |
---|
1698 | { |
---|
1699 | // skip used printfids |
---|
1700 | for (; printfIds.find(freePrintfId) != printfIds.end(); ++freePrintfId); |
---|
1701 | // just use this free printfid |
---|
1702 | printfId = freePrintfId++; |
---|
1703 | } |
---|
1704 | |
---|
1705 | output += " - '"; |
---|
1706 | itocstrCStyle(printfId, numBuf, 24); |
---|
1707 | output += numBuf; |
---|
1708 | output += ':'; |
---|
1709 | itocstrCStyle(printfInfo.argSizes.size(), numBuf, 24); |
---|
1710 | output += numBuf; |
---|
1711 | output += ':'; |
---|
1712 | for (size_t argSize: printfInfo.argSizes) |
---|
1713 | { |
---|
1714 | itocstrCStyle(argSize, numBuf, 24); |
---|
1715 | output += numBuf; |
---|
1716 | output += ':'; |
---|
1717 | } |
---|
1718 | // printf format |
---|
1719 | std::string escapedFmt = escapeStringCStyle(printfInfo.format); |
---|
1720 | escapedFmt = escapePrintfFormat(escapedFmt); |
---|
1721 | output += escapedFmt; |
---|
1722 | output += "'\n"; |
---|
1723 | } |
---|
1724 | } |
---|
1725 | |
---|
1726 | if (!mdInfo.kernels.empty()) |
---|
1727 | output += "Kernels: \n"; |
---|
1728 | // kernels |
---|
1729 | for (size_t i = 0; i < mdInfo.kernels.size(); i++) |
---|
1730 | { |
---|
1731 | const ROCmKernelMetadata& kernel = mdInfo.kernels[i]; |
---|
1732 | output += " - Name: "; |
---|
1733 | output.append(kernel.name.c_str(), kernel.name.size()); |
---|
1734 | output += "\n SymbolName: "; |
---|
1735 | if (!kernel.symbolName.empty()) |
---|
1736 | output += escapeYAMLString(kernel.symbolName); |
---|
1737 | else |
---|
1738 | { |
---|
1739 | // default is kernel name + '@kd' |
---|
1740 | std::string symName = kernel.name.c_str(); |
---|
1741 | symName += "@kd"; |
---|
1742 | output += escapeYAMLString(symName); |
---|
1743 | } |
---|
1744 | output += "\n"; |
---|
1745 | if (!kernel.language.empty()) |
---|
1746 | { |
---|
1747 | output += " Language: "; |
---|
1748 | output += escapeYAMLString(kernel.language); |
---|
1749 | output += "\n"; |
---|
1750 | } |
---|
1751 | if (kernel.langVersion[0] != BINGEN_NOTSUPPLIED) |
---|
1752 | { |
---|
1753 | output += " LanguageVersion: "; |
---|
1754 | genArrayValue(2, kernel.langVersion, output); |
---|
1755 | } |
---|
1756 | // kernel attributes |
---|
1757 | if (kernel.reqdWorkGroupSize[0] != 0 || kernel.reqdWorkGroupSize[1] != 0 || |
---|
1758 | kernel.reqdWorkGroupSize[2] != 0 || |
---|
1759 | kernel.workGroupSizeHint[0] != 0 || kernel.workGroupSizeHint[1] != 0 || |
---|
1760 | kernel.workGroupSizeHint[2] != 0 || |
---|
1761 | !kernel.vecTypeHint.empty() || !kernel.runtimeHandle.empty()) |
---|
1762 | { |
---|
1763 | output += " Attrs: \n"; |
---|
1764 | if (kernel.workGroupSizeHint[0] != 0 || kernel.workGroupSizeHint[1] != 0 || |
---|
1765 | kernel.workGroupSizeHint[2] != 0) |
---|
1766 | { |
---|
1767 | output += " WorkGroupSizeHint: "; |
---|
1768 | genArrayValue(3, kernel.workGroupSizeHint, output); |
---|
1769 | } |
---|
1770 | if (kernel.reqdWorkGroupSize[0] != 0 || kernel.reqdWorkGroupSize[1] != 0 || |
---|
1771 | kernel.reqdWorkGroupSize[2] != 0) |
---|
1772 | { |
---|
1773 | output += " ReqdWorkGroupSize: "; |
---|
1774 | genArrayValue(3, kernel.reqdWorkGroupSize, output); |
---|
1775 | } |
---|
1776 | if (!kernel.vecTypeHint.empty()) |
---|
1777 | { |
---|
1778 | output += " VecTypeHint: "; |
---|
1779 | output += escapeYAMLString(kernel.vecTypeHint); |
---|
1780 | output += "\n"; |
---|
1781 | } |
---|
1782 | if (!kernel.runtimeHandle.empty()) |
---|
1783 | { |
---|
1784 | output += " RuntimeHandle: "; |
---|
1785 | output += escapeYAMLString(kernel.runtimeHandle); |
---|
1786 | output += "\n"; |
---|
1787 | } |
---|
1788 | } |
---|
1789 | // kernel arguments |
---|
1790 | if (!kernel.argInfos.empty()) |
---|
1791 | output += " Args: \n"; |
---|
1792 | for (const ROCmKernelArgInfo& argInfo: kernel.argInfos) |
---|
1793 | { |
---|
1794 | output += " - "; |
---|
1795 | if (!argInfo.name.empty()) |
---|
1796 | { |
---|
1797 | output += "Name: "; |
---|
1798 | output += escapeYAMLString(argInfo.name); |
---|
1799 | output += "\n "; |
---|
1800 | } |
---|
1801 | if (!argInfo.typeName.empty()) |
---|
1802 | { |
---|
1803 | output += "TypeName: "; |
---|
1804 | output += escapeYAMLString(argInfo.typeName); |
---|
1805 | output += "\n "; |
---|
1806 | } |
---|
1807 | output += "Size: "; |
---|
1808 | itocstrCStyle(argInfo.size, numBuf, 24); |
---|
1809 | output += numBuf; |
---|
1810 | output += "\n Align: "; |
---|
1811 | itocstrCStyle(argInfo.align, numBuf, 24); |
---|
1812 | output += numBuf; |
---|
1813 | output += "\n ValueKind: "; |
---|
1814 | |
---|
1815 | if (argInfo.valueKind > ROCmValueKind::MAX_VALUE) |
---|
1816 | throw BinGenException("Unknown ValueKind"); |
---|
1817 | output += rocmValueKindNames[cxuint(argInfo.valueKind)]; |
---|
1818 | |
---|
1819 | if (argInfo.valueType > ROCmValueType::MAX_VALUE) |
---|
1820 | throw BinGenException("Unknown ValueType"); |
---|
1821 | output += "\n ValueType: "; |
---|
1822 | output += rocmValueTypeNames[cxuint(argInfo.valueType)]; |
---|
1823 | output += "\n"; |
---|
1824 | |
---|
1825 | if (argInfo.valueKind == ROCmValueKind::DYN_SHARED_PTR) |
---|
1826 | { |
---|
1827 | output += " PointeeAlign: "; |
---|
1828 | itocstrCStyle(argInfo.pointeeAlign, numBuf, 24); |
---|
1829 | output += numBuf; |
---|
1830 | output += "\n"; |
---|
1831 | } |
---|
1832 | if (argInfo.valueKind == ROCmValueKind::DYN_SHARED_PTR || |
---|
1833 | argInfo.valueKind == ROCmValueKind::GLOBAL_BUFFER) |
---|
1834 | { |
---|
1835 | if (argInfo.addressSpace > ROCmAddressSpace::MAX_VALUE || |
---|
1836 | argInfo.addressSpace == ROCmAddressSpace::NONE) |
---|
1837 | throw BinGenException("Unknown AddressSpace"); |
---|
1838 | output += " AddrSpaceQual: "; |
---|
1839 | output += rocmAddrSpaceTypesTbl[cxuint(argInfo.addressSpace)-1]; |
---|
1840 | output += "\n"; |
---|
1841 | } |
---|
1842 | if (argInfo.valueKind == ROCmValueKind::IMAGE || |
---|
1843 | argInfo.valueKind == ROCmValueKind::PIPE) |
---|
1844 | { |
---|
1845 | if (argInfo.accessQual> ROCmAccessQual::MAX_VALUE) |
---|
1846 | throw BinGenException("Unknown AccessQualifier"); |
---|
1847 | output += " AccQual: "; |
---|
1848 | output += rocmAccessQualifierTbl[cxuint(argInfo.accessQual)]; |
---|
1849 | output += "\n"; |
---|
1850 | } |
---|
1851 | if (argInfo.valueKind == ROCmValueKind::GLOBAL_BUFFER || |
---|
1852 | argInfo.valueKind == ROCmValueKind::IMAGE || |
---|
1853 | argInfo.valueKind == ROCmValueKind::PIPE) |
---|
1854 | { |
---|
1855 | if (argInfo.actualAccessQual> ROCmAccessQual::MAX_VALUE) |
---|
1856 | throw BinGenException("Unknown ActualAccessQualifier"); |
---|
1857 | output += " ActualAccQual: "; |
---|
1858 | output += rocmAccessQualifierTbl[cxuint(argInfo.actualAccessQual)]; |
---|
1859 | output += "\n"; |
---|
1860 | } |
---|
1861 | if (argInfo.isConst) |
---|
1862 | output += " IsConst: true\n"; |
---|
1863 | if (argInfo.isRestrict) |
---|
1864 | output += " IsRestrict: true\n"; |
---|
1865 | if (argInfo.isVolatile) |
---|
1866 | output += " IsVolatile: true\n"; |
---|
1867 | if (argInfo.isPipe) |
---|
1868 | output += " IsPipe: true\n"; |
---|
1869 | } |
---|
1870 | |
---|
1871 | // kernel code properties |
---|
1872 | const ROCmKernelConfig& kconfig = *kconfigs[i]; |
---|
1873 | |
---|
1874 | output += " CodeProps: \n"; |
---|
1875 | output += " KernargSegmentSize: "; |
---|
1876 | itocstrCStyle(hasValue(kernel.kernargSegmentSize) ? |
---|
1877 | kernel.kernargSegmentSize : ULEV(kconfig.kernargSegmentSize), |
---|
1878 | numBuf, 24); |
---|
1879 | output += numBuf; |
---|
1880 | output += "\n GroupSegmentFixedSize: "; |
---|
1881 | itocstrCStyle(hasValue(kernel.groupSegmentFixedSize) ? |
---|
1882 | kernel.groupSegmentFixedSize : |
---|
1883 | uint64_t(ULEV(kconfig.workgroupGroupSegmentSize)), |
---|
1884 | numBuf, 24); |
---|
1885 | output += numBuf; |
---|
1886 | output += "\n PrivateSegmentFixedSize: "; |
---|
1887 | itocstrCStyle(hasValue(kernel.privateSegmentFixedSize) ? |
---|
1888 | kernel.privateSegmentFixedSize : |
---|
1889 | uint64_t(ULEV(kconfig.workitemPrivateSegmentSize)), |
---|
1890 | numBuf, 24); |
---|
1891 | output += numBuf; |
---|
1892 | output += "\n KernargSegmentAlign: "; |
---|
1893 | itocstrCStyle(hasValue(kernel.kernargSegmentAlign) ? |
---|
1894 | kernel.kernargSegmentAlign : |
---|
1895 | uint64_t(1ULL<<kconfig.kernargSegmentAlignment), |
---|
1896 | numBuf, 24); |
---|
1897 | output += numBuf; |
---|
1898 | output += "\n WavefrontSize: "; |
---|
1899 | itocstrCStyle(hasValue(kernel.wavefrontSize) ? kernel.wavefrontSize : |
---|
1900 | cxuint(1U<<kconfig.wavefrontSize), numBuf, 24); |
---|
1901 | output += numBuf; |
---|
1902 | output += "\n NumSGPRs: "; |
---|
1903 | itocstrCStyle(hasValue(kernel.sgprsNum) ? kernel.sgprsNum : |
---|
1904 | cxuint(ULEV(kconfig.wavefrontSgprCount)), numBuf, 24); |
---|
1905 | output += numBuf; |
---|
1906 | output += "\n NumVGPRs: "; |
---|
1907 | itocstrCStyle(hasValue(kernel.vgprsNum) ? kernel.vgprsNum : |
---|
1908 | cxuint(ULEV(kconfig.workitemVgprCount)), numBuf, 24); |
---|
1909 | output += numBuf; |
---|
1910 | // spilled registers |
---|
1911 | if (hasValue(kernel.spilledSgprs)) |
---|
1912 | { |
---|
1913 | output += "\n NumSpilledSGPRs: "; |
---|
1914 | itocstrCStyle(kernel.spilledSgprs, numBuf, 24); |
---|
1915 | output += numBuf; |
---|
1916 | } |
---|
1917 | if (hasValue(kernel.spilledVgprs)) |
---|
1918 | { |
---|
1919 | output += "\n NumSpilledVGPRs: "; |
---|
1920 | itocstrCStyle(kernel.spilledVgprs, numBuf, 24); |
---|
1921 | output += numBuf; |
---|
1922 | } |
---|
1923 | output += "\n MaxFlatWorkGroupSize: "; |
---|
1924 | itocstrCStyle(hasValue(kernel.maxFlatWorkGroupSize) ? |
---|
1925 | kernel.maxFlatWorkGroupSize : uint64_t(256), numBuf, 24); |
---|
1926 | output += numBuf; |
---|
1927 | output += "\n"; |
---|
1928 | if (kernel.fixedWorkGroupSize[0] != 0 || kernel.fixedWorkGroupSize[1] != 0 || |
---|
1929 | kernel.fixedWorkGroupSize[2] != 0) |
---|
1930 | { |
---|
1931 | output += " FixedWorkGroupSize: "; |
---|
1932 | genArrayValue(3, kernel.fixedWorkGroupSize, output); |
---|
1933 | } |
---|
1934 | } |
---|
1935 | output += "...\n"; |
---|
1936 | } |
---|
1937 | |
---|
1938 | /* |
---|
1939 | * ROCm Binary Generator |
---|
1940 | */ |
---|
1941 | |
---|
1942 | ROCmBinGenerator::ROCmBinGenerator() : manageable(false), input(nullptr) |
---|
1943 | { } |
---|
1944 | |
---|
1945 | ROCmBinGenerator::ROCmBinGenerator(const ROCmInput* rocmInput) |
---|
1946 | : manageable(false), input(rocmInput) |
---|
1947 | { } |
---|
1948 | |
---|
1949 | ROCmBinGenerator::ROCmBinGenerator(GPUDeviceType deviceType, |
---|
1950 | uint32_t archMinor, uint32_t archStepping, size_t codeSize, const cxbyte* code, |
---|
1951 | size_t globalDataSize, const cxbyte* globalData, |
---|
1952 | const std::vector<ROCmSymbolInput>& symbols) |
---|
1953 | { |
---|
1954 | input = new ROCmInput{ deviceType, archMinor, archStepping, 0, false, |
---|
1955 | globalDataSize, globalData, symbols, codeSize, code }; |
---|
1956 | } |
---|
1957 | |
---|
1958 | ROCmBinGenerator::ROCmBinGenerator(GPUDeviceType deviceType, |
---|
1959 | uint32_t archMinor, uint32_t archStepping, size_t codeSize, const cxbyte* code, |
---|
1960 | size_t globalDataSize, const cxbyte* globalData, |
---|
1961 | std::vector<ROCmSymbolInput>&& symbols) |
---|
1962 | { |
---|
1963 | input = new ROCmInput{ deviceType, archMinor, archStepping, 0, false, |
---|
1964 | globalDataSize, globalData, std::move(symbols), codeSize, code }; |
---|
1965 | } |
---|
1966 | |
---|
1967 | ROCmBinGenerator::~ROCmBinGenerator() |
---|
1968 | { |
---|
1969 | if (manageable) |
---|
1970 | delete input; |
---|
1971 | } |
---|
1972 | |
---|
1973 | void ROCmBinGenerator::setInput(const ROCmInput* input) |
---|
1974 | { |
---|
1975 | if (manageable) |
---|
1976 | delete input; |
---|
1977 | manageable = false; |
---|
1978 | this->input = input; |
---|
1979 | } |
---|
1980 | |
---|
1981 | // ELF notes contents |
---|
1982 | static const cxbyte noteDescType1[8] = |
---|
1983 | { 2, 0, 0, 0, 1, 0, 0, 0 }; |
---|
1984 | |
---|
1985 | static const cxbyte noteDescType3[27] = |
---|
1986 | { 4, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
---|
1987 | 'A', 'M', 'D', 0, 'A', 'M', 'D', 'G', 'P', 'U', 0 }; |
---|
1988 | |
---|
1989 | static inline void addMainSectionToTable(cxuint& sectionsNum, uint16_t* builtinTable, |
---|
1990 | cxuint elfSectId) |
---|
1991 | { builtinTable[elfSectId - ELFSECTID_START] = sectionsNum++; } |
---|
1992 | |
---|
1993 | // TODO: add GLOBAL OFFSET TABLE dynamic and (static?) relocations |
---|
1994 | |
---|
1995 | void ROCmBinGenerator::prepareBinaryGen() |
---|
1996 | { |
---|
1997 | AMDGPUArchVersion amdGpuArchValues = getGPUArchVersion(input->deviceType, |
---|
1998 | GPUArchVersionTable::ROCM); |
---|
1999 | if (input->archMinor!=UINT32_MAX) |
---|
2000 | amdGpuArchValues.minor = input->archMinor; |
---|
2001 | if (input->archStepping!=UINT32_MAX) |
---|
2002 | amdGpuArchValues.stepping = input->archStepping; |
---|
2003 | |
---|
2004 | comment = "CLRX ROCmBinGenerator " CLRX_VERSION; |
---|
2005 | commentSize = ::strlen(comment); |
---|
2006 | if (input->comment!=nullptr) |
---|
2007 | { |
---|
2008 | // if comment, store comment section |
---|
2009 | comment = input->comment; |
---|
2010 | commentSize = input->commentSize; |
---|
2011 | if (commentSize==0) |
---|
2012 | commentSize = ::strlen(comment); |
---|
2013 | } |
---|
2014 | |
---|
2015 | uint32_t eflags = input->newBinFormat ? 2 : 0; |
---|
2016 | if (input->eflags != BINGEN_DEFAULT) |
---|
2017 | eflags = input->eflags; |
---|
2018 | |
---|
2019 | elfBinGen64.reset(new ElfBinaryGen64({ 0U, 0U, 0x40, 0, ET_DYN, |
---|
2020 | 0xe0, EV_CURRENT, UINT_MAX, 0, eflags }, |
---|
2021 | true, true, true, PHREGION_FILESTART)); |
---|
2022 | |
---|
2023 | std::fill(mainBuiltinSectTable, |
---|
2024 | mainBuiltinSectTable + ROCMSECTID_MAX-ELFSECTID_START+1, SHN_UNDEF); |
---|
2025 | cxuint mainSectionsNum = 1; |
---|
2026 | |
---|
2027 | // generate main builtin section table (for section id translation) |
---|
2028 | if (input->newBinFormat) |
---|
2029 | addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ROCMSECTID_NOTE); |
---|
2030 | if (input->globalData != nullptr) |
---|
2031 | addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_RODATA); |
---|
2032 | addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_DYNSYM); |
---|
2033 | addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ROCMSECTID_HASH); |
---|
2034 | addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_DYNSTR); |
---|
2035 | const cxuint execProgHeaderRegionIndex = mainSectionsNum; |
---|
2036 | addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_TEXT); |
---|
2037 | addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ROCMSECTID_DYNAMIC); |
---|
2038 | if (!input->newBinFormat) |
---|
2039 | { |
---|
2040 | addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ROCMSECTID_NOTE); |
---|
2041 | addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ROCMSECTID_GPUCONFIG); |
---|
2042 | } |
---|
2043 | addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_COMMENT); |
---|
2044 | addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_SYMTAB); |
---|
2045 | addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_SHSTRTAB); |
---|
2046 | addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_STRTAB); |
---|
2047 | |
---|
2048 | // add symbols (kernels, function kernels and data symbols) |
---|
2049 | elfBinGen64->addSymbol(ElfSymbol64("_DYNAMIC", |
---|
2050 | mainBuiltinSectTable[ROCMSECTID_DYNAMIC-ELFSECTID_START], |
---|
2051 | ELF64_ST_INFO(STB_LOCAL, STT_NOTYPE), STV_HIDDEN, true, 0, 0)); |
---|
2052 | const uint16_t textSectIndex = mainBuiltinSectTable[ELFSECTID_TEXT-ELFSECTID_START]; |
---|
2053 | for (const ROCmSymbolInput& symbol: input->symbols) |
---|
2054 | { |
---|
2055 | ElfSymbol64 elfsym; |
---|
2056 | switch (symbol.type) |
---|
2057 | { |
---|
2058 | case ROCmRegionType::KERNEL: |
---|
2059 | elfsym = ElfSymbol64(symbol.symbolName.c_str(), textSectIndex, |
---|
2060 | ELF64_ST_INFO(STB_GLOBAL, STT_GNU_IFUNC), 0, true, |
---|
2061 | symbol.offset, symbol.size); |
---|
2062 | break; |
---|
2063 | case ROCmRegionType::FKERNEL: |
---|
2064 | elfsym = ElfSymbol64(symbol.symbolName.c_str(), textSectIndex, |
---|
2065 | ELF64_ST_INFO(STB_GLOBAL, STT_FUNC), 0, true, |
---|
2066 | symbol.offset, symbol.size); |
---|
2067 | break; |
---|
2068 | case ROCmRegionType::DATA: |
---|
2069 | elfsym = ElfSymbol64(symbol.symbolName.c_str(), textSectIndex, |
---|
2070 | ELF64_ST_INFO(STB_GLOBAL, STT_OBJECT), 0, true, |
---|
2071 | symbol.offset, symbol.size); |
---|
2072 | break; |
---|
2073 | default: |
---|
2074 | break; |
---|
2075 | } |
---|
2076 | // add to symbols and dynamic symbols table |
---|
2077 | elfBinGen64->addSymbol(elfsym); |
---|
2078 | elfBinGen64->addDynSymbol(elfsym); |
---|
2079 | } |
---|
2080 | |
---|
2081 | static const int32_t dynTags[] = { |
---|
2082 | DT_SYMTAB, DT_SYMENT, DT_STRTAB, DT_STRSZ, DT_HASH }; |
---|
2083 | elfBinGen64->addDynamics(sizeof(dynTags)/sizeof(int32_t), dynTags); |
---|
2084 | |
---|
2085 | // elf program headers |
---|
2086 | elfBinGen64->addProgramHeader({ PT_PHDR, PF_R, 0, 1, |
---|
2087 | true, Elf64Types::nobase, Elf64Types::nobase, 0 }); |
---|
2088 | elfBinGen64->addProgramHeader({ PT_LOAD, PF_R, PHREGION_FILESTART, |
---|
2089 | execProgHeaderRegionIndex, |
---|
2090 | true, Elf64Types::nobase, Elf64Types::nobase, 0, 0x1000 }); |
---|
2091 | elfBinGen64->addProgramHeader({ PT_LOAD, PF_R|PF_X, execProgHeaderRegionIndex, 1, |
---|
2092 | true, Elf64Types::nobase, Elf64Types::nobase, 0 }); |
---|
2093 | elfBinGen64->addProgramHeader({ PT_LOAD, PF_R|PF_W, execProgHeaderRegionIndex+1, 1, |
---|
2094 | true, Elf64Types::nobase, Elf64Types::nobase, 0 }); |
---|
2095 | elfBinGen64->addProgramHeader({ PT_DYNAMIC, PF_R|PF_W, execProgHeaderRegionIndex+1, 1, |
---|
2096 | true, Elf64Types::nobase, Elf64Types::nobase, 0, 8 }); |
---|
2097 | elfBinGen64->addProgramHeader({ PT_GNU_RELRO, PF_R, execProgHeaderRegionIndex+1, 1, |
---|
2098 | true, Elf64Types::nobase, Elf64Types::nobase, 0, 1 }); |
---|
2099 | elfBinGen64->addProgramHeader({ PT_GNU_STACK, PF_R|PF_W, PHREGION_FILESTART, 0, |
---|
2100 | true, 0, 0, 0 }); |
---|
2101 | |
---|
2102 | if (input->newBinFormat) |
---|
2103 | // program header for note (new binary format) |
---|
2104 | elfBinGen64->addProgramHeader({ PT_NOTE, PF_R, 1, 1, true, |
---|
2105 | Elf64Types::nobase, Elf64Types::nobase, 0, 4 }); |
---|
2106 | |
---|
2107 | target = input->target.c_str(); |
---|
2108 | if (target.empty() && !input->targetTripple.empty()) |
---|
2109 | { |
---|
2110 | target = input->targetTripple.c_str(); |
---|
2111 | char dbuf[20]; |
---|
2112 | snprintf(dbuf, 20, "-gfx%u%u%u", amdGpuArchValues.major, amdGpuArchValues.minor, |
---|
2113 | amdGpuArchValues.stepping); |
---|
2114 | target += dbuf; |
---|
2115 | } |
---|
2116 | // elf notes |
---|
2117 | elfBinGen64->addNote({"AMD", sizeof noteDescType1, noteDescType1, 1U}); |
---|
2118 | noteBuf.reset(new cxbyte[0x1b]); |
---|
2119 | ::memcpy(noteBuf.get(), noteDescType3, 0x1b); |
---|
2120 | SULEV(*(uint32_t*)(noteBuf.get()+4), amdGpuArchValues.major); |
---|
2121 | SULEV(*(uint32_t*)(noteBuf.get()+8), amdGpuArchValues.minor); |
---|
2122 | SULEV(*(uint32_t*)(noteBuf.get()+12), amdGpuArchValues.stepping); |
---|
2123 | elfBinGen64->addNote({"AMD", 0x1b, noteBuf.get(), 3U}); |
---|
2124 | if (!target.empty()) |
---|
2125 | elfBinGen64->addNote({"AMD", target.size(), (const cxbyte*)target.c_str(), 0xbU}); |
---|
2126 | |
---|
2127 | metadataSize = input->metadataSize; |
---|
2128 | metadata = input->metadata; |
---|
2129 | if (input->useMetadataInfo) |
---|
2130 | { |
---|
2131 | // generate ROCm metadata |
---|
2132 | std::vector<std::pair<CString, size_t> > symbolIndices(input->symbols.size()); |
---|
2133 | // create sorted indices of symbols by its name |
---|
2134 | for (size_t k = 0; k < input->symbols.size(); k++) |
---|
2135 | symbolIndices[k] = std::make_pair(input->symbols[k].symbolName, k); |
---|
2136 | mapSort(symbolIndices.begin(), symbolIndices.end()); |
---|
2137 | |
---|
2138 | const size_t mdKernelsNum = input->metadataInfo.kernels.size(); |
---|
2139 | std::unique_ptr<const ROCmKernelConfig*[]> kernelConfigPtrs( |
---|
2140 | new const ROCmKernelConfig*[mdKernelsNum]); |
---|
2141 | // generate ROCm kernel config pointers |
---|
2142 | for (size_t k = 0; k < mdKernelsNum; k++) |
---|
2143 | { |
---|
2144 | auto it = binaryMapFind(symbolIndices.begin(), symbolIndices.end(), |
---|
2145 | input->metadataInfo.kernels[k].name); |
---|
2146 | if (it == symbolIndices.end() || |
---|
2147 | (input->symbols[it->second].type != ROCmRegionType::FKERNEL && |
---|
2148 | input->symbols[it->second].type != ROCmRegionType::KERNEL)) |
---|
2149 | throw BinGenException("Kernel in metadata doesn't exists in code"); |
---|
2150 | kernelConfigPtrs[k] = reinterpret_cast<const ROCmKernelConfig*>( |
---|
2151 | input->code + input->symbols[it->second].offset); |
---|
2152 | } |
---|
2153 | // just generate ROCm metadata from info |
---|
2154 | generateROCmMetadata(input->metadataInfo, kernelConfigPtrs.get(), metadataStr); |
---|
2155 | metadataSize = metadataStr.size(); |
---|
2156 | metadata = metadataStr.c_str(); |
---|
2157 | } |
---|
2158 | |
---|
2159 | if (metadataSize != 0) |
---|
2160 | elfBinGen64->addNote({"AMD", metadataSize, (const cxbyte*)metadata, 0xaU}); |
---|
2161 | |
---|
2162 | /// region and sections |
---|
2163 | elfBinGen64->addRegion(ElfRegion64::programHeaderTable()); |
---|
2164 | if (input->newBinFormat) |
---|
2165 | elfBinGen64->addRegion(ElfRegion64::noteSection()); |
---|
2166 | if (input->globalData != nullptr) |
---|
2167 | elfBinGen64->addRegion(ElfRegion64(input->globalDataSize, input->globalData, 4, |
---|
2168 | ".rodata", SHT_PROGBITS, SHF_ALLOC, 0, 0, Elf64Types::nobase)); |
---|
2169 | |
---|
2170 | elfBinGen64->addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 8, |
---|
2171 | ".dynsym", SHT_DYNSYM, SHF_ALLOC, 0, BINGEN_DEFAULT, Elf64Types::nobase)); |
---|
2172 | elfBinGen64->addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 4, |
---|
2173 | ".hash", SHT_HASH, SHF_ALLOC, |
---|
2174 | mainBuiltinSectTable[ELFSECTID_DYNSYM-ELFSECTID_START], 0, |
---|
2175 | Elf64Types::nobase)); |
---|
2176 | elfBinGen64->addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 1, ".dynstr", SHT_STRTAB, |
---|
2177 | SHF_ALLOC, 0, 0, Elf64Types::nobase)); |
---|
2178 | // '.text' with alignment=4096 |
---|
2179 | elfBinGen64->addRegion(ElfRegion64(input->codeSize, (const cxbyte*)input->code, |
---|
2180 | 0x1000, ".text", SHT_PROGBITS, SHF_ALLOC|SHF_EXECINSTR, 0, 0, |
---|
2181 | Elf64Types::nobase, 0, false, 256)); |
---|
2182 | elfBinGen64->addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 0x1000, |
---|
2183 | ".dynamic", SHT_DYNAMIC, SHF_ALLOC|SHF_WRITE, |
---|
2184 | mainBuiltinSectTable[ELFSECTID_DYNSTR-ELFSECTID_START], 0, |
---|
2185 | Elf64Types::nobase, 0, false, 8)); |
---|
2186 | if (!input->newBinFormat) |
---|
2187 | { |
---|
2188 | elfBinGen64->addRegion(ElfRegion64::noteSection()); |
---|
2189 | elfBinGen64->addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 1, |
---|
2190 | ".AMDGPU.config", SHT_PROGBITS, 0)); |
---|
2191 | } |
---|
2192 | elfBinGen64->addRegion(ElfRegion64(commentSize, (const cxbyte*)comment, 1, ".comment", |
---|
2193 | SHT_PROGBITS, SHF_MERGE|SHF_STRINGS, 0, 0, 0, 1)); |
---|
2194 | elfBinGen64->addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 8, |
---|
2195 | ".symtab", SHT_SYMTAB, 0, 0, BINGEN_DEFAULT)); |
---|
2196 | elfBinGen64->addRegion(ElfRegion64::shstrtabSection()); |
---|
2197 | elfBinGen64->addRegion(ElfRegion64::strtabSection()); |
---|
2198 | elfBinGen64->addRegion(ElfRegion64::sectionHeaderTable()); |
---|
2199 | |
---|
2200 | /* extra sections */ |
---|
2201 | for (const BinSection& section: input->extraSections) |
---|
2202 | elfBinGen64->addRegion(ElfRegion64(section, mainBuiltinSectTable, |
---|
2203 | ROCMSECTID_MAX, mainSectionsNum)); |
---|
2204 | /* extra symbols */ |
---|
2205 | for (const BinSymbol& symbol: input->extraSymbols) |
---|
2206 | { |
---|
2207 | ElfSymbol64 sym(symbol, mainBuiltinSectTable, |
---|
2208 | ROCMSECTID_MAX, mainSectionsNum); |
---|
2209 | elfBinGen64->addSymbol(sym); |
---|
2210 | elfBinGen64->addDynSymbol(sym); |
---|
2211 | } |
---|
2212 | binarySize = elfBinGen64->countSize(); |
---|
2213 | } |
---|
2214 | |
---|
2215 | void ROCmBinGenerator::generateInternal(std::ostream* osPtr, std::vector<char>* vPtr, |
---|
2216 | Array<cxbyte>* aPtr) |
---|
2217 | { |
---|
2218 | if (elfBinGen64 == nullptr) |
---|
2219 | prepareBinaryGen(); |
---|
2220 | /**** |
---|
2221 | * prepare for write binary to output |
---|
2222 | ****/ |
---|
2223 | std::unique_ptr<std::ostream> outStreamHolder; |
---|
2224 | std::ostream* os = nullptr; |
---|
2225 | if (aPtr != nullptr) |
---|
2226 | { |
---|
2227 | aPtr->resize(binarySize); |
---|
2228 | outStreamHolder.reset( |
---|
2229 | new ArrayOStream(binarySize, reinterpret_cast<char*>(aPtr->data()))); |
---|
2230 | os = outStreamHolder.get(); |
---|
2231 | } |
---|
2232 | else if (vPtr != nullptr) |
---|
2233 | { |
---|
2234 | vPtr->resize(binarySize); |
---|
2235 | outStreamHolder.reset(new VectorOStream(*vPtr)); |
---|
2236 | os = outStreamHolder.get(); |
---|
2237 | } |
---|
2238 | else // from argument |
---|
2239 | os = osPtr; |
---|
2240 | |
---|
2241 | const std::ios::iostate oldExceptions = os->exceptions(); |
---|
2242 | try |
---|
2243 | { |
---|
2244 | os->exceptions(std::ios::failbit | std::ios::badbit); |
---|
2245 | /**** |
---|
2246 | * write binary to output |
---|
2247 | ****/ |
---|
2248 | FastOutputBuffer bos(256, *os); |
---|
2249 | elfBinGen64->generate(bos); |
---|
2250 | assert(bos.getWritten() == binarySize); |
---|
2251 | } |
---|
2252 | catch(...) |
---|
2253 | { |
---|
2254 | os->exceptions(oldExceptions); |
---|
2255 | throw; |
---|
2256 | } |
---|
2257 | os->exceptions(oldExceptions); |
---|
2258 | } |
---|
2259 | |
---|
2260 | void ROCmBinGenerator::generate(Array<cxbyte>& array) |
---|
2261 | { |
---|
2262 | generateInternal(nullptr, nullptr, &array); |
---|
2263 | } |
---|
2264 | |
---|
2265 | void ROCmBinGenerator::generate(std::ostream& os) |
---|
2266 | { |
---|
2267 | generateInternal(&os, nullptr, nullptr); |
---|
2268 | } |
---|
2269 | |
---|
2270 | void ROCmBinGenerator::generate(std::vector<char>& v) |
---|
2271 | { |
---|
2272 | generateInternal(nullptr, &v, nullptr); |
---|
2273 | } |
---|