1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
|
/*
* Copyright (C) 2020-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/offline_compiler/source/ocloc_fatbinary.h"
#include "shared/offline_compiler/source/ocloc_api.h"
#include "shared/offline_compiler/source/ocloc_arg_helper.h"
#include "shared/offline_compiler/source/ocloc_interface.h"
#include "shared/offline_compiler/source/offline_compiler.h"
#include "shared/offline_compiler/source/utilities/safety_caller.h"
#include "shared/source/compiler_interface/compiler_options.h"
#include "shared/source/compiler_interface/intermediate_representations.h"
#include "shared/source/compiler_interface/tokenized_string.h"
#include "shared/source/device_binary_format/ar/ar_encoder.h"
#include "shared/source/device_binary_format/elf/elf_encoder.h"
#include "shared/source/device_binary_format/elf/ocl_elf.h"
#include "shared/source/helpers/file_io.h"
#include "shared/source/helpers/hw_info.h"
#include "shared/source/helpers/product_config_helper.h"
#include "shared/source/helpers/product_config_helper_former.h"
#include "shared/source/utilities/directory.h"
#include "neo_aot_platforms.h"
#include "neo_igfxfmid.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <set>
namespace NEO {
using Position = FormerProductConfigHelper::Position;
bool isSpvOnly(const std::vector<std::string> &args) {
return std::find(args.begin(), args.end(), "-spv_only") != args.end();
}
bool requestedFatBinary(ConstStringRef deviceArg, OclocArgHelper *helper) {
auto &prodHelper = *helper->productConfigHelper;
auto &formerProdHelper = *helper->formerProductConfigHelper;
auto deviceName = deviceArg.str();
ProductConfigHelper::adjustDeviceName(deviceName);
auto release = prodHelper.getReleaseFromDeviceName(deviceName);
auto family = prodHelper.getFamilyFromDeviceName(deviceName);
auto retVal = deviceArg.contains("*");
retVal |= deviceArg.contains(":");
retVal |= deviceArg.contains(",");
retVal |= family != AOT::UNKNOWN_FAMILY;
retVal |= release != AOT::UNKNOWN_RELEASE;
retVal |= formerProdHelper.isSupportedTarget<AOT::FAMILY>(deviceName);
retVal |= formerProdHelper.isSupportedTarget<AOT::RELEASE>(deviceName);
return retVal;
}
bool requestedFatBinary(const std::vector<std::string> &args, OclocArgHelper *helper) {
for (size_t argIndex = 1; argIndex < args.size(); argIndex++) {
const auto &currArg = args[argIndex];
const bool hasMoreArgs = (argIndex + 1 < args.size());
if ((ConstStringRef("-device") == currArg) && hasMoreArgs) {
return requestedFatBinary(args[argIndex + 1], helper);
}
}
return false;
}
template <>
void getProductsAcronymsForTarget<AOT::RELEASE>(std::vector<NEO::ConstStringRef> &out, AOT::RELEASE target, OclocArgHelper *argHelper) {
auto &prodHelper = *argHelper->productConfigHelper;
auto &allSuppportedProducts = prodHelper.getDeviceAotInfo();
auto hasDeviceAcronym = std::any_of(allSuppportedProducts.begin(), allSuppportedProducts.end(), ProductConfigHelper::findDeviceAcronymForRelease(target));
for (const auto &device : allSuppportedProducts) {
if (device.release == target) {
ConstStringRef acronym{};
if (hasDeviceAcronym) {
if (!device.deviceAcronyms.empty()) {
acronym = device.deviceAcronyms.front();
}
} else {
if (!device.rtlIdAcronyms.empty()) {
acronym = device.rtlIdAcronyms.front();
}
}
if (!acronym.empty() && std::find(out.begin(), out.end(), acronym) == out.end()) {
out.push_back(acronym);
}
}
}
}
template <>
void getProductsAcronymsForTarget<AOT::FAMILY>(std::vector<NEO::ConstStringRef> &out, AOT::FAMILY target, OclocArgHelper *argHelper) {
auto &prodHelper = *argHelper->productConfigHelper;
auto &allSuppportedProducts = prodHelper.getDeviceAotInfo();
std::vector<AOT::RELEASE> releases{};
for (const auto &device : allSuppportedProducts) {
if (device.family == target && std::find(releases.begin(), releases.end(), device.release) == releases.end()) {
releases.push_back(device.release);
}
}
for (const auto &release : releases) {
getProductsAcronymsForTarget<AOT::RELEASE>(out, release, argHelper);
}
}
template <typename T>
std::vector<ConstStringRef> getProductsForTargetRange(T targetFrom, T targetTo, OclocArgHelper *argHelper, const T maxValue) {
std::vector<ConstStringRef> ret{};
if (targetFrom > targetTo) {
std::swap(targetFrom, targetTo);
}
while (targetFrom <= targetTo && targetFrom < maxValue) {
getProductsAcronymsForTarget<T>(ret, targetFrom, argHelper);
targetFrom = static_cast<T>(static_cast<unsigned int>(targetFrom) + 1);
}
return ret;
}
std::vector<ConstStringRef> getProductsForRange(unsigned int productFrom, unsigned int productTo,
OclocArgHelper *argHelper) {
auto &prodHelper = *argHelper->productConfigHelper;
auto &formerProdHelper = *argHelper->formerProductConfigHelper;
std::vector<ConstStringRef> ret = {};
auto &formerData = formerProdHelper.getData();
uint32_t ipVersionAdded = 0;
for (const auto &[acronym, ipVersion] : formerData.acronyms) {
auto validAcronym = ipVersion >= productFrom;
validAcronym &= ipVersion <= productTo;
if (validAcronym && ipVersion != ipVersionAdded) {
ret.push_back(acronym.c_str());
ipVersionAdded = ipVersion;
}
}
auto &allSuppportedProducts = prodHelper.getDeviceAotInfo();
for (const auto &device : allSuppportedProducts) {
auto validAcronym = device.aotConfig.value >= productFrom;
validAcronym &= device.aotConfig.value <= productTo;
if (validAcronym) {
if (!device.deviceAcronyms.empty()) {
ret.push_back(device.deviceAcronyms.front());
} else if (!device.rtlIdAcronyms.empty()) {
ret.push_back(device.rtlIdAcronyms.front());
}
}
}
return ret;
}
std::vector<ConstStringRef> getProductsForFamilyClosedRange(const std::string &fromStr, const std::string &toStr, OclocArgHelper *argHelper) {
auto &prodHelper = *argHelper->productConfigHelper;
auto &formerProdHelper = *argHelper->formerProductConfigHelper;
auto getFamilyConfigWithFallback = [&](const std::string &familyStr, FormerProductConfigHelper::Position pos) {
auto family = prodHelper.getFamilyFromDeviceName(familyStr);
AOT::PRODUCT_CONFIG config = AOT::UNKNOWN_ISA;
if (family != AOT::UNKNOWN_FAMILY) {
config = prodHelper.getLastProductConfigFromFamilyName(family);
}
if (config == AOT::UNKNOWN_ISA) {
config = formerProdHelper.getProductConfigFromFamilyName(familyStr, pos);
}
return config;
};
auto familyFrom = prodHelper.getFamilyFromDeviceName(fromStr);
auto familyTo = prodHelper.getFamilyFromDeviceName(toStr);
if (familyFrom != AOT::UNKNOWN_FAMILY && familyTo != AOT::UNKNOWN_FAMILY) {
return getProductsForTargetRange(familyFrom, familyTo, argHelper, AOT::FAMILY_MAX);
}
AOT::PRODUCT_CONFIG prodConfigFrom = getFamilyConfigWithFallback(fromStr, Position::firstItem);
AOT::PRODUCT_CONFIG prodConfigTo = getFamilyConfigWithFallback(toStr, Position::lastItem);
if (prodConfigFrom != AOT::UNKNOWN_ISA && prodConfigTo != AOT::UNKNOWN_ISA) {
return getProductsForRange(prodConfigFrom, prodConfigTo, argHelper);
}
return {};
}
std::vector<ConstStringRef> getProductsForReleaseClosedRange(const std::string &fromStr, const std::string &toStr, OclocArgHelper *argHelper) {
auto &prodHelper = *argHelper->productConfigHelper;
auto &formerProdHelper = *argHelper->formerProductConfigHelper;
auto getReleaseConfigWithFallback = [&](const std::string &releaseStr, FormerProductConfigHelper::Position pos) {
auto release = prodHelper.getReleaseFromDeviceName(releaseStr);
auto config = prodHelper.getLastProductConfigFromReleaseName(release);
if (config == AOT::UNKNOWN_ISA) {
config = formerProdHelper.getProductConfigFromReleaseName(releaseStr, pos);
}
return config;
};
auto releaseFrom = prodHelper.getReleaseFromDeviceName(fromStr);
auto releaseTo = prodHelper.getReleaseFromDeviceName(toStr);
if (releaseFrom != AOT::UNKNOWN_RELEASE && releaseTo != AOT::UNKNOWN_RELEASE) {
return getProductsForTargetRange(releaseFrom, releaseTo, argHelper, AOT::RELEASE_MAX);
}
AOT::PRODUCT_CONFIG prodConfigFrom = getReleaseConfigWithFallback(fromStr, Position::firstItem);
AOT::PRODUCT_CONFIG prodConfigTo = getReleaseConfigWithFallback(toStr, Position::lastItem);
if (prodConfigFrom != AOT::UNKNOWN_ISA && prodConfigTo != AOT::UNKNOWN_ISA) {
return getProductsForRange(prodConfigFrom, prodConfigTo, argHelper);
}
return {};
}
std::vector<ConstStringRef> getProductsForProductClosedRange(const std::string &fromStr, const std::string &toStr, OclocArgHelper *argHelper) {
auto &prodHelper = *argHelper->productConfigHelper;
auto &formerProdHelper = *argHelper->formerProductConfigHelper;
auto getProductConfigWithFallback = [&](const std::string &deviceName) {
auto config = prodHelper.getProductConfigFromDeviceName(deviceName);
if (config == AOT::UNKNOWN_ISA) {
config = formerProdHelper.getProductConfigFromDeviceName(deviceName);
}
return config;
};
AOT::PRODUCT_CONFIG prodConfigFrom = getProductConfigWithFallback(fromStr);
AOT::PRODUCT_CONFIG prodConfigTo = getProductConfigWithFallback(toStr);
if (prodConfigFrom != AOT::UNKNOWN_ISA && prodConfigTo != AOT::UNKNOWN_ISA) {
if (prodConfigFrom > prodConfigTo) {
std::swap(prodConfigFrom, prodConfigTo);
}
return getProductsForRange(prodConfigFrom, prodConfigTo, argHelper);
}
return {};
}
std::vector<ConstStringRef> getProductForClosedRange(ConstStringRef rangeFrom, ConstStringRef rangeTo, OclocArgHelper *argHelper) {
auto rangeFromStr = rangeFrom.str();
auto rangeToStr = rangeTo.str();
ProductConfigHelper::adjustDeviceName(rangeFromStr);
ProductConfigHelper::adjustDeviceName(rangeToStr);
auto familyProducts = getProductsForFamilyClosedRange(rangeFromStr, rangeToStr, argHelper);
if (!familyProducts.empty()) {
return familyProducts;
}
auto releaseProducts = getProductsForReleaseClosedRange(rangeFromStr, rangeToStr, argHelper);
if (!releaseProducts.empty()) {
return releaseProducts;
}
auto productProducts = getProductsForProductClosedRange(rangeFromStr, rangeToStr, argHelper);
if (!productProducts.empty()) {
return productProducts;
}
auto target = rangeFromStr + ":" + rangeToStr;
argHelper->printf("Failed to parse target : %s.\n", target.c_str());
return {};
}
auto mergeProducts = [](std::vector<ConstStringRef> &dst, std::vector<ConstStringRef> &&src) {
for (auto &elem : src) {
if (std::find(dst.begin(), dst.end(), elem) == dst.end()) {
dst.push_back(std::move(elem));
}
}
};
std::vector<ConstStringRef> getProductsForFamilyOpenRange(const std::string &openRangeStr, OclocArgHelper *argHelper, bool rangeTo) {
auto &prodHelper = *argHelper->productConfigHelper;
auto &formerProdHelper = *argHelper->formerProductConfigHelper;
auto getFamilyConfigWithFallback = [&](const std::string &familyStr, FormerProductConfigHelper::Position pos) {
auto family = prodHelper.getFamilyFromDeviceName(familyStr);
AOT::PRODUCT_CONFIG config = AOT::UNKNOWN_ISA;
if (family != AOT::UNKNOWN_FAMILY) {
config = prodHelper.getLastProductConfigFromFamilyName(family);
}
if (config == AOT::UNKNOWN_ISA) {
config = formerProdHelper.getProductConfigFromFamilyName(familyStr, pos);
}
return config;
};
std::vector<ConstStringRef> requestedProducts;
auto family = prodHelper.getFamilyFromDeviceName(openRangeStr);
if (family != AOT::UNKNOWN_FAMILY) {
if (rangeTo) {
auto prodConfigFrom = getFamilyConfigWithFallback(formerProdHelper.getFamilyName(Position::firstItem), Position::firstItem);
auto prodConfigTo = getFamilyConfigWithFallback(formerProdHelper.getFamilyName(Position::lastItem), Position::lastItem);
if (prodConfigFrom != AOT::UNKNOWN_ISA && prodConfigTo != AOT::UNKNOWN_ISA) {
mergeProducts(requestedProducts, getProductsForRange(prodConfigFrom, prodConfigTo, argHelper));
}
unsigned int familyFrom = AOT::UNKNOWN_FAMILY + 1;
mergeProducts(requestedProducts, getProductsForTargetRange(static_cast<AOT::FAMILY>(familyFrom), family, argHelper, AOT::FAMILY_MAX));
return requestedProducts;
} else {
unsigned int familyTo = AOT::FAMILY_MAX;
return getProductsForTargetRange(family, static_cast<AOT::FAMILY>(familyTo), argHelper, AOT::FAMILY_MAX);
}
} else if (formerProdHelper.isFamilyName(openRangeStr)) {
if (rangeTo) {
auto prodConfigFrom = getFamilyConfigWithFallback(formerProdHelper.getFamilyName(Position::firstItem), Position::firstItem);
auto prodConfigTo = getFamilyConfigWithFallback(openRangeStr, Position::lastItem);
if (prodConfigFrom != AOT::UNKNOWN_ISA && prodConfigTo != AOT::UNKNOWN_ISA) {
return getProductsForRange(prodConfigFrom, prodConfigTo, argHelper);
}
} else {
auto prodConfigFrom = getFamilyConfigWithFallback(openRangeStr, Position::firstItem);
auto prodConfigTo = getFamilyConfigWithFallback(formerProdHelper.getFamilyName(Position::lastItem), Position::lastItem);
if (prodConfigFrom != AOT::UNKNOWN_ISA && prodConfigTo != AOT::UNKNOWN_ISA) {
mergeProducts(requestedProducts, getProductsForRange(prodConfigFrom, prodConfigTo, argHelper));
}
unsigned int familyFrom = AOT::UNKNOWN_FAMILY + 1;
unsigned int familyTo = AOT::FAMILY_MAX;
mergeProducts(requestedProducts, getProductsForTargetRange(static_cast<AOT::FAMILY>(familyFrom), static_cast<AOT::FAMILY>(familyTo), argHelper, AOT::FAMILY_MAX));
return requestedProducts;
}
}
return {};
}
std::vector<ConstStringRef> getProductsForReleaseOpenRange(const std::string &openRangeStr, OclocArgHelper *argHelper, bool rangeTo) {
auto &prodHelper = *argHelper->productConfigHelper;
auto &formerProdHelper = *argHelper->formerProductConfigHelper;
std::vector<ConstStringRef> requestedProducts;
auto getReleaseConfigWithFallback = [&](const std::string &releaseStr, FormerProductConfigHelper::Position pos) {
auto release = prodHelper.getReleaseFromDeviceName(releaseStr);
auto config = prodHelper.getLastProductConfigFromReleaseName(release);
if (config == AOT::UNKNOWN_ISA) {
config = formerProdHelper.getProductConfigFromReleaseName(releaseStr, pos);
}
return config;
};
auto release = prodHelper.getReleaseFromDeviceName(openRangeStr);
if (release != AOT::UNKNOWN_RELEASE) {
if (rangeTo) {
auto prodConfigFrom = getReleaseConfigWithFallback(formerProdHelper.getReleaseName(Position::firstItem), Position::firstItem);
auto prodConfigTo = getReleaseConfigWithFallback(formerProdHelper.getReleaseName(Position::lastItem), Position::lastItem);
if (prodConfigFrom != AOT::UNKNOWN_ISA && prodConfigTo != AOT::UNKNOWN_ISA) {
mergeProducts(requestedProducts, getProductsForRange(prodConfigFrom, prodConfigTo, argHelper));
}
unsigned int releaseFrom = AOT::UNKNOWN_RELEASE + 1;
mergeProducts(requestedProducts, getProductsForTargetRange(static_cast<AOT::RELEASE>(releaseFrom), release, argHelper, AOT::RELEASE_MAX));
return requestedProducts;
} else {
unsigned int releaseTo = AOT::RELEASE_MAX;
return getProductsForTargetRange(release, static_cast<AOT::RELEASE>(releaseTo), argHelper, AOT::RELEASE_MAX);
}
} else if (formerProdHelper.isReleaseName(openRangeStr)) {
if (rangeTo) {
auto prodConfigFrom = getReleaseConfigWithFallback(formerProdHelper.getReleaseName(Position::firstItem), Position::firstItem);
auto prodConfigTo = getReleaseConfigWithFallback(openRangeStr, Position::lastItem);
if (prodConfigFrom != AOT::UNKNOWN_ISA && prodConfigTo != AOT::UNKNOWN_ISA) {
return getProductsForRange(prodConfigFrom, prodConfigTo, argHelper);
}
} else {
auto prodConfigFrom = getReleaseConfigWithFallback(openRangeStr, Position::firstItem);
auto prodConfigTo = getReleaseConfigWithFallback(formerProdHelper.getReleaseName(Position::lastItem), Position::lastItem);
if (prodConfigFrom != AOT::UNKNOWN_ISA && prodConfigTo != AOT::UNKNOWN_ISA) {
mergeProducts(requestedProducts, getProductsForRange(prodConfigFrom, prodConfigTo, argHelper));
}
unsigned int releaseFrom = AOT::UNKNOWN_RELEASE + 1;
unsigned int releaseTo = AOT::RELEASE_MAX;
mergeProducts(requestedProducts, getProductsForTargetRange(static_cast<AOT::RELEASE>(releaseFrom), static_cast<AOT::RELEASE>(releaseTo), argHelper, AOT::RELEASE_MAX));
return requestedProducts;
}
}
return {};
}
std::vector<ConstStringRef> getProductsForProductOpenRange(const std::string &openRangeStr, OclocArgHelper *argHelper, bool rangeTo) {
auto &prodHelper = *argHelper->productConfigHelper;
auto &formerProdHelper = *argHelper->formerProductConfigHelper;
auto getProductConfigWithFallback = [&](const std::string &deviceName) {
auto config = prodHelper.getProductConfigFromDeviceName(deviceName);
if (config == AOT::UNKNOWN_ISA) {
config = formerProdHelper.getProductConfigFromDeviceName(deviceName);
}
return config;
};
auto product = getProductConfigWithFallback(openRangeStr);
if (product != AOT::UNKNOWN_ISA) {
if (rangeTo) {
unsigned int productFrom = formerProdHelper.getFirstProductConfig();
if (productFrom == AOT::UNKNOWN_ISA) {
++productFrom;
}
return getProductsForRange(productFrom, static_cast<unsigned int>(product), argHelper);
} else {
unsigned int productTo = AOT::getConfixMaxPlatform() - 1;
return getProductsForRange(product, static_cast<AOT::PRODUCT_CONFIG>(productTo), argHelper);
}
}
return {};
}
std::vector<ConstStringRef> getProductForOpenRange(ConstStringRef openRange, OclocArgHelper *argHelper, bool rangeTo) {
auto openRangeStr = openRange.str();
ProductConfigHelper::adjustDeviceName(openRangeStr);
auto familyProducts = getProductsForFamilyOpenRange(openRangeStr, argHelper, rangeTo);
if (!familyProducts.empty()) {
return familyProducts;
}
auto releaseProducts = getProductsForReleaseOpenRange(openRangeStr, argHelper, rangeTo);
if (!releaseProducts.empty()) {
return releaseProducts;
}
auto productProducts = getProductsForProductOpenRange(openRangeStr, argHelper, rangeTo);
if (!productProducts.empty()) {
return productProducts;
}
argHelper->printf("Failed to parse target : %s.\n", openRangeStr.c_str());
return {};
}
std::vector<ConstStringRef> getProductForSpecificTarget(const CompilerOptions::TokenizedString &targets, OclocArgHelper *argHelper) {
auto &prodHelper = *argHelper->productConfigHelper;
auto &formerProdHelper = *argHelper->formerProductConfigHelper;
std::vector<ConstStringRef> requestedConfigs;
auto getProductsAcronymsForFamilyWithFallback = [&](const std::string &targetStr) -> bool {
auto family = prodHelper.getFamilyFromDeviceName(targetStr);
if (family != AOT::UNKNOWN_FAMILY) {
getProductsAcronymsForTarget(requestedConfigs, family, argHelper);
return true;
}
if (formerProdHelper.isSupportedTarget<AOT::FAMILY>(targetStr)) {
mergeProducts(requestedConfigs, formerProdHelper.getProductAcronymsFromFamilyGroup(targetStr));
return true;
}
return false;
};
auto getProductsAcronymsForReleaseWithFallback = [&](const std::string &targetStr) -> bool {
auto release = prodHelper.getReleaseFromDeviceName(targetStr);
if (release != AOT::UNKNOWN_RELEASE) {
getProductsAcronymsForTarget(requestedConfigs, release, argHelper);
return true;
}
if (formerProdHelper.isSupportedTarget<AOT::RELEASE>(targetStr)) {
mergeProducts(requestedConfigs, formerProdHelper.getProductAcronymsFromReleaseGroup(targetStr));
return true;
}
return false;
};
auto getProductConfigWithFallback = [&](const ConstStringRef &target) -> bool {
auto product = prodHelper.getProductConfigFromAcronym(target.str());
if (product == AOT::UNKNOWN_ISA) {
product = formerProdHelper.getProductConfigFromDeviceName(target.str());
}
if (product != AOT::UNKNOWN_ISA) {
mergeProducts(requestedConfigs, std::vector<ConstStringRef>{target});
return true;
}
return false;
};
for (const auto &target : targets) {
auto targetStr = target.str();
ProductConfigHelper::adjustDeviceName(targetStr);
if (getProductsAcronymsForFamilyWithFallback(targetStr)) {
continue;
}
if (getProductsAcronymsForReleaseWithFallback(targetStr)) {
continue;
}
if (getProductConfigWithFallback(target)) {
continue;
}
if (getHwInfoForDeprecatedAcronym(targetStr) != nullptr) {
requestedConfigs.push_back(target);
continue;
}
argHelper->printf("Failed to parse target : %s - invalid device:\n", targetStr.c_str());
return {};
}
return requestedConfigs;
}
std::vector<ConstStringRef> getTargetProductsForFatbinary(ConstStringRef deviceArg, OclocArgHelper *argHelper) {
auto &prodHelper = *argHelper->productConfigHelper;
auto &formerProdHelper = *argHelper->formerProductConfigHelper;
std::vector<ConstStringRef> retVal;
if (deviceArg == "*") {
retVal = prodHelper.getRepresentativeProductAcronyms();
auto formerAcronyms = formerProdHelper.getRepresentativeProductAcronyms();
retVal.insert(retVal.begin(), formerAcronyms.begin(), formerAcronyms.end());
return retVal;
} else {
auto sets = CompilerOptions::tokenize(deviceArg, ',');
if (sets[0].contains(":")) {
auto range = CompilerOptions::tokenize(deviceArg, ':');
if (range.size() > 2) {
argHelper->printf("Invalid range : %s - should be from:to or :to or from:\n", sets[0].str().c_str());
return {};
}
if (range.size() == 1) {
bool rangeTo = (':' == sets[0][0]);
retVal = getProductForOpenRange(range[0], argHelper, rangeTo);
} else {
retVal = getProductForClosedRange(range[0], range[1], argHelper);
}
} else {
retVal = getProductForSpecificTarget(sets, argHelper);
}
}
return retVal;
}
int getDeviceArgValueIdx(const std::vector<std::string> &args) {
for (size_t argIndex = 0; argIndex < args.size(); ++argIndex) {
const auto &currArg = args[argIndex];
const bool hasMoreArgs = (argIndex + 1 < args.size());
if ((ConstStringRef("-device") == currArg) && hasMoreArgs) {
return static_cast<int>(argIndex + 1);
}
}
return -1;
}
int buildFatBinaryForTarget(int retVal, const std::vector<std::string> &argsCopy, std::string pointerSize, Ar::ArEncoder &fatbinary,
OfflineCompiler *pCompiler, OclocArgHelper *argHelper, const std::string &product) {
auto &prodHelper = *argHelper->productConfigHelper;
if (retVal == 0) {
retVal = buildWithSafetyGuard(pCompiler);
std::string buildLog = pCompiler->getBuildLog();
if (buildLog.empty() == false) {
argHelper->printf("%s\n", buildLog.c_str());
}
if (retVal == 0) {
if (!pCompiler->isQuiet()) {
argHelper->printf("Build succeeded for : %s.\n", product.c_str());
}
} else {
argHelper->printf("Build failed for : %s with error code: %d\n", product.c_str(), retVal);
argHelper->printf("Command was:");
for (const auto &arg : argsCopy) {
argHelper->printf(" %s", arg.c_str());
}
argHelper->printf("\n");
}
}
if (retVal) {
return retVal;
}
std::string entryName("");
if (product.find(".") != std::string::npos) {
entryName = product;
} else {
auto productConfig = prodHelper.getProductConfigFromDeviceName(product);
auto genericIdAcronymIt = std::find_if(AOT::genericIdAcronyms.begin(), AOT::genericIdAcronyms.end(),
[product](const std::pair<std::string, AOT::PRODUCT_CONFIG> &genericIdAcronym) {
return product == genericIdAcronym.first;
});
if (AOT::UNKNOWN_ISA != productConfig && genericIdAcronymIt == AOT::genericIdAcronyms.end()) {
entryName = ProductConfigHelper::parseMajorMinorRevisionValue(productConfig);
} else {
entryName = product;
}
}
fatbinary.appendFileEntry(pointerSize + "." + entryName, pCompiler->getPackedDeviceBinaryOutput());
return retVal;
}
int buildFatBinaryForFormerTarget(int retVal, const std::vector<std::string> &argsCopy, std::string pointerSize, Ar::ArEncoder &fatbinary,
OclocArgHelper *argHelper, const std::string &product) {
auto &formerProdHelper = *argHelper->formerProductConfigHelper;
uint32_t numOutputs = 0u;
unsigned char **dataOutputs = nullptr;
uint64_t *lenOutputs = nullptr;
char **nameOutputs = nullptr;
if (retVal == 0) {
std::vector<const char *> argvPtrs;
argvPtrs.reserve(argsCopy.size());
for (const auto &arg : argsCopy) {
argvPtrs.push_back(arg.c_str());
}
auto retValFormerOcloc = Ocloc::Commands::invokeFormerOclocWithHelper(argHelper, argvPtrs,
&numOutputs, &dataOutputs,
&lenOutputs, &nameOutputs);
if (retValFormerOcloc) {
retVal = retValFormerOcloc.value();
argHelper->dontSetupOutputs();
if (retVal == 0) {
argHelper->printf("Build succeeded for : %s.\n", product.c_str());
} else {
argHelper->printf("Build failed for : %s with error code: %d\n", product.c_str(), retVal);
argHelper->printf("Command was:");
for (const auto &arg : argsCopy) {
argHelper->printf(" %s", arg.c_str());
}
argHelper->printf("\n");
return retVal;
}
} else {
// Former ocloc couldn't be invoked at all
argHelper->printf("Build failed for : %s - could not invoke former ocloc\n", product.c_str());
return retVal;
}
for (size_t i = 0; i < numOutputs; ++i) {
std::string name = nameOutputs[i];
if (name.find(".bin") != std::string::npos) {
const ArrayRef<const uint8_t> fileData(dataOutputs[i], static_cast<size_t>(lenOutputs[i]));
std::string entryName("");
if (product.find(".") != std::string::npos) {
entryName = product;
} else {
auto productConfig = formerProdHelper.getProductConfigFromAcronym(product);
entryName = ProductConfigHelper::parseMajorMinorRevisionValue(productConfig);
}
fatbinary.appendFileEntry(pointerSize + "." + entryName, fileData);
}
}
}
// Use formerOclocFree since memory was allocated by former ocloc
auto freeResult = Ocloc::Commands::formerOclocFree(Ocloc::getOclocFormerLibName(), &numOutputs, &dataOutputs, &lenOutputs, &nameOutputs);
if (!freeResult) {
// Fallback to regular oclocFreeOutput if formerOclocFree fails
oclocFreeOutput(&numOutputs, &dataOutputs, &lenOutputs, &nameOutputs);
}
return retVal;
}
int buildFatBinary(const std::vector<std::string> &args, OclocArgHelper *argHelper) {
std::string pointerSizeInBits = (sizeof(void *) == 4) ? "32" : "64";
size_t deviceArgIndex = -1;
std::string inputFileName = "";
std::string outputFileName = "";
std::string outputDirectory = "";
bool spirvInput = false;
bool excludeIr = false;
std::set<std::string> deviceAcronymsFromDeviceOptions;
std::vector<std::string> argsCopy(args);
for (size_t argIndex = 1; argIndex < args.size(); argIndex++) {
const auto &currArg = args[argIndex];
const bool hasMoreArgs = (argIndex + 1 < args.size());
const bool hasAtLeast2MoreArgs = (argIndex + 2 < args.size());
if ((ConstStringRef("-device") == currArg) && hasMoreArgs) {
deviceArgIndex = argIndex + 1;
++argIndex;
} else if ((CompilerOptions::arch32bit == currArg) || (ConstStringRef("-32") == currArg)) {
pointerSizeInBits = "32";
} else if ((CompilerOptions::arch64bit == currArg) || (ConstStringRef("-64") == currArg)) {
pointerSizeInBits = "64";
} else if ((ConstStringRef("-file") == currArg) && hasMoreArgs) {
inputFileName = args[argIndex + 1];
++argIndex;
} else if (((ConstStringRef("-output") == currArg) || (ConstStringRef("-o") == currArg)) && hasMoreArgs) {
outputFileName = args[argIndex + 1];
++argIndex;
} else if ((ConstStringRef("-out_dir") == currArg) && hasMoreArgs) {
outputDirectory = args[argIndex + 1];
++argIndex;
} else if (ConstStringRef("-exclude_ir") == currArg) {
excludeIr = true;
} else if (ConstStringRef("-spirv_input") == currArg) {
spirvInput = true;
} else if (("-device_options" == currArg) && hasAtLeast2MoreArgs) {
const auto deviceAcronyms = CompilerOptions::tokenize(args[argIndex + 1], ',');
for (const auto &deviceAcronym : deviceAcronyms) {
deviceAcronymsFromDeviceOptions.insert(deviceAcronym.str());
}
argIndex += 2;
}
}
const bool shouldPreserveGenericIr = spirvInput && !excludeIr;
if (shouldPreserveGenericIr) {
argsCopy.push_back("-exclude_ir");
}
if (deviceArgIndex == static_cast<size_t>(-1)) {
argHelper->printf("Error! Command does not contain device argument!\n");
return OCLOC_INVALID_COMMAND_LINE;
}
Ar::ArEncoder fatbinary(true);
std::vector<ConstStringRef> targetProducts;
targetProducts = getTargetProductsForFatbinary(ConstStringRef(args[deviceArgIndex]), argHelper);
if (targetProducts.empty()) {
argHelper->printf("Failed to parse target devices from : %s\n", args[deviceArgIndex].c_str());
return 1;
}
for (const auto &deviceAcronym : deviceAcronymsFromDeviceOptions) {
if (std::find(targetProducts.begin(), targetProducts.end(), deviceAcronym) == targetProducts.end()) {
argHelper->printf("Warning! -device_options set for non-compiled device: %s\n", deviceAcronym.c_str());
}
}
std::string optionsForIr;
for (const auto &product : targetProducts) {
int retVal = OCLOC_SUCCESS;
argsCopy[deviceArgIndex] = product.str();
auto &formerProdHelper = *argHelper->formerProductConfigHelper;
auto formerProduct = formerProdHelper.getProductConfigFromDeviceName(product.str().c_str());
auto formerProductFallback = formerProdHelper.isSupportedProductConfig(formerProduct);
if (formerProductFallback) {
retVal = buildFatBinaryForFormerTarget(retVal, argsCopy, pointerSizeInBits, fatbinary, argHelper, product.str());
if (retVal) {
return retVal;
}
} else {
std::unique_ptr<OfflineCompiler> pCompiler{OfflineCompiler::create(argsCopy.size(), argsCopy, false, retVal, argHelper)};
if (OCLOC_SUCCESS != retVal) {
argHelper->printf("Error! Couldn't create OfflineCompiler. Exiting.\n");
return retVal;
}
retVal = buildFatBinaryForTarget(retVal, argsCopy, pointerSizeInBits, fatbinary, pCompiler.get(), argHelper, product.str());
if (retVal) {
return retVal;
}
if (optionsForIr.empty()) {
optionsForIr = pCompiler->getOptions();
}
}
}
if (shouldPreserveGenericIr) {
const auto errorCode = appendGenericIr(fatbinary, inputFileName, argHelper, optionsForIr);
if (errorCode != OCLOC_SUCCESS) {
argHelper->printf("Error! Couldn't append generic IR file!\n");
return errorCode;
}
}
auto fatbinaryData = fatbinary.encode();
std::string fatbinaryFileName = "";
if (!outputDirectory.empty() && outputDirectory != "/dev/null") {
fatbinaryFileName = outputDirectory + "/";
NEO::Directory(outputDirectory).parseDirectories(Directory::createDirs);
}
if (!outputFileName.empty()) {
fatbinaryFileName += outputFileName;
} else {
if (!inputFileName.empty()) {
fatbinaryFileName += OfflineCompiler::getFileNameTrunk(inputFileName) + ".ar";
}
}
argHelper->saveOutput(fatbinaryFileName, fatbinaryData.data(), fatbinaryData.size());
return 0;
}
int appendGenericIr(Ar::ArEncoder &fatbinary, const std::string &inputFile, OclocArgHelper *argHelper, std::string options) {
std::size_t fileSize = 0;
std::unique_ptr<char[]> fileContents = argHelper->loadDataFromFile(inputFile, fileSize);
if (fileSize == 0) {
argHelper->printf("Error! Couldn't read input file!\n");
return OCLOC_INVALID_FILE;
}
const auto ir = ArrayRef<const uint8_t>::fromAny(fileContents.get(), fileSize);
const auto opt = ArrayRef<const uint8_t>::fromAny(options.data(), options.size());
if (!isSpirVBitcode(ir)) {
argHelper->printf("Error! Input file is not in supported generic IR format! "
"Currently supported format is SPIR-V.\n");
return OCLOC_INVALID_FILE;
}
const auto encodedElf = createEncodedElfWithSpirv(ir, opt);
ArrayRef<const uint8_t> genericIrFile{encodedElf.data(), encodedElf.size()};
fatbinary.appendFileEntry("generic_ir", genericIrFile);
return OCLOC_SUCCESS;
}
std::vector<uint8_t> createEncodedElfWithSpirv(const ArrayRef<const uint8_t> &spirv, const ArrayRef<const uint8_t> &options) {
using namespace NEO::Elf;
ElfEncoder<EI_CLASS_64> elfEncoder;
elfEncoder.getElfFileHeader().type = ET_OPENCL_OBJECTS;
elfEncoder.appendSection(SHT_OPENCL_SPIRV, SectionNamesOpenCl::spirvObject, spirv);
elfEncoder.appendSection(SHT_OPENCL_OPTIONS, SectionNamesOpenCl::buildOptions, options);
return elfEncoder.encode();
}
} // namespace NEO
|