Fix ocloc fatbinary for better gen/sku detection

Signed-off-by: Kacper Nowak <kacper.nowak@intel.com>
This commit is contained in:
Kacper Nowak
2021-04-16 15:25:00 +02:00
committed by Compute-Runtime-Automation
parent 2ef41e4b18
commit e5a1d33a2b
8 changed files with 133 additions and 81 deletions

View File

@ -7,6 +7,7 @@
#include "opencl/test/unit_test/offline_compiler/ocloc_fatbinary_tests.h"
#include "shared/offline_compiler/source/ocloc_arg_helper.h"
#include "shared/source/helpers/hw_helper.h"
#include <algorithm>
@ -17,18 +18,21 @@ namespace NEO {
TEST(OclocFatBinaryRequestedFatBinary, WhenDeviceArgMissingThenReturnsFalse) {
const char *args[] = {"ocloc", "-aaa", "*", "-device", "*"};
EXPECT_FALSE(NEO::requestedFatBinary(0, nullptr));
EXPECT_FALSE(NEO::requestedFatBinary(1, args));
EXPECT_FALSE(NEO::requestedFatBinary(2, args));
EXPECT_FALSE(NEO::requestedFatBinary(3, args));
EXPECT_FALSE(NEO::requestedFatBinary(4, args));
std::unique_ptr<OclocArgHelper> argHelper = std::make_unique<OclocArgHelper>();
EXPECT_FALSE(NEO::requestedFatBinary(0, nullptr, argHelper.get()));
EXPECT_FALSE(NEO::requestedFatBinary(1, args, argHelper.get()));
EXPECT_FALSE(NEO::requestedFatBinary(2, args, argHelper.get()));
EXPECT_FALSE(NEO::requestedFatBinary(3, args, argHelper.get()));
EXPECT_FALSE(NEO::requestedFatBinary(4, args, argHelper.get()));
}
TEST(OclocFatBinaryRequestedFatBinary, WhenDeviceArgProvidedAndContainsFatbinaryArgFormatThenReturnsTrue) {
TEST(OclocFatBinaryRequestedFatBinary, GivenDeviceArgProvidedWhenFatBinaryFormatWithRangeIsPassedThenTrueIsReturned) {
std::unique_ptr<OclocArgHelper> argHelper = std::make_unique<OclocArgHelper>();
const char *allPlatforms[] = {"ocloc", "-device", "*"};
const char *manyPlatforms[] = {"ocloc", "-device", "a,b"};
const char *manyGens[] = {"ocloc", "-device", "gen0,gen1"};
const char *gen[] = {"ocloc", "-device", "gen0"};
const char *rangePlatformFrom[] = {"ocloc", "-device", "skl-"};
const char *rangePlatformTo[] = {"ocloc", "-device", "-skl"};
const char *rangePlatformBounds[] = {"ocloc", "-device", "skl-icllp"};
@ -36,21 +40,41 @@ TEST(OclocFatBinaryRequestedFatBinary, WhenDeviceArgProvidedAndContainsFatbinary
const char *rangeGenTo[] = {"ocloc", "-device", "-gen5"};
const char *rangeGenBounds[] = {"ocloc", "-device", "gen0-gen5"};
EXPECT_TRUE(NEO::requestedFatBinary(3, allPlatforms));
EXPECT_TRUE(NEO::requestedFatBinary(3, manyPlatforms));
EXPECT_TRUE(NEO::requestedFatBinary(3, manyGens));
EXPECT_TRUE(NEO::requestedFatBinary(3, gen));
EXPECT_TRUE(NEO::requestedFatBinary(3, rangePlatformFrom));
EXPECT_TRUE(NEO::requestedFatBinary(3, rangePlatformTo));
EXPECT_TRUE(NEO::requestedFatBinary(3, rangePlatformBounds));
EXPECT_TRUE(NEO::requestedFatBinary(3, rangeGenFrom));
EXPECT_TRUE(NEO::requestedFatBinary(3, rangeGenTo));
EXPECT_TRUE(NEO::requestedFatBinary(3, rangeGenBounds));
EXPECT_TRUE(NEO::requestedFatBinary(3, allPlatforms, argHelper.get()));
EXPECT_TRUE(NEO::requestedFatBinary(3, manyPlatforms, argHelper.get()));
EXPECT_TRUE(NEO::requestedFatBinary(3, manyGens, argHelper.get()));
EXPECT_TRUE(NEO::requestedFatBinary(3, rangePlatformFrom, argHelper.get()));
EXPECT_TRUE(NEO::requestedFatBinary(3, rangePlatformTo, argHelper.get()));
EXPECT_TRUE(NEO::requestedFatBinary(3, rangePlatformBounds, argHelper.get()));
EXPECT_TRUE(NEO::requestedFatBinary(3, rangeGenFrom, argHelper.get()));
EXPECT_TRUE(NEO::requestedFatBinary(3, rangeGenTo, argHelper.get()));
EXPECT_TRUE(NEO::requestedFatBinary(3, rangeGenBounds, argHelper.get()));
}
TEST(OclocFatBinaryRequestedFatBinary, GivenDeviceArgProvidedWhenUnknownGenNameIsPassedThenRequestedFatBinaryReturnsFalse) {
std::unique_ptr<OclocArgHelper> argHelper = std::make_unique<OclocArgHelper>();
const char *unknownGen[] = {"ocloc", "-device", "gen0"};
const char *unknownGenCaseInsensitive[] = {"ocloc", "-device", "Gen0"};
EXPECT_FALSE(NEO::requestedFatBinary(3, unknownGen, argHelper.get()));
EXPECT_FALSE(NEO::requestedFatBinary(3, unknownGenCaseInsensitive, argHelper.get()));
}
TEST(OclocFatBinaryRequestedFatBinary, GivenDeviceArgProvidedWhenKnownGenNameIsPassedThenRequestedFatBinaryReturnsTrue) {
std::unique_ptr<OclocArgHelper> argHelper = std::make_unique<OclocArgHelper>();
unsigned int i = 0;
for (; i < IGFX_MAX_CORE; ++i) {
if (NEO::familyName[i] != nullptr)
break;
}
const char *genFromFamilyName[] = {"ocloc", "-device", NEO::familyName[i]};
EXPECT_TRUE(NEO::requestedFatBinary(3, genFromFamilyName, argHelper.get()));
}
TEST(OclocFatBinaryRequestedFatBinary, WhenDeviceArgProvidedButDoesnNotContainFatbinaryArgFormatThenReturnsFalse) {
std::unique_ptr<OclocArgHelper> argHelper = std::make_unique<OclocArgHelper>();
const char *skl[] = {"ocloc", "-device", "skl"};
EXPECT_FALSE(NEO::requestedFatBinary(3, skl));
EXPECT_FALSE(NEO::requestedFatBinary(3, skl, argHelper.get()));
}
TEST(OclocFatBinaryGetAllSupportedTargetPlatforms, WhenRequestedThenReturnsAllPlatformsWithNonNullHardwarePrefixes) {
@ -91,21 +115,51 @@ TEST(OclocFatBinaryAsProductId, GivenDisabledPlatformNameThenReturnsUnknownPlatf
}
TEST(OclocFatBinaryAsGfxCoreIdList, GivenEnabledGfxCoreNameThenReturnsNonEmptyList) {
std::unique_ptr<OclocArgHelper> argHelper = std::make_unique<OclocArgHelper>();
for (unsigned int coreId = 0; coreId < IGFX_MAX_CORE; ++coreId) {
if (nullptr != NEO::familyName[coreId]) {
EXPECT_FALSE(NEO::asGfxCoreIdList(ConstStringRef(NEO::familyName[coreId], strlen(NEO::familyName[coreId]))).empty());
std::string caseInsesitive = NEO::familyName[coreId];
caseInsesitive[0] = 'g';
EXPECT_FALSE(NEO::asGfxCoreIdList(caseInsesitive).empty());
EXPECT_TRUE(argHelper->isGen(ConstStringRef(NEO::familyName[coreId]).str()));
std::string caseInsensitive = NEO::familyName[coreId];
std::transform(caseInsensitive.begin(), caseInsensitive.begin() + 1, caseInsensitive.begin(), ::tolower);
EXPECT_TRUE(argHelper->isGen(caseInsensitive));
}
}
}
TEST(OclocFatBinaryAsGfxCoreIdList, GivenDisabledGfxCoreNameThenReturnsEmptyList) {
EXPECT_TRUE(NEO::asGfxCoreIdList(ConstStringRef("genA")).empty());
EXPECT_TRUE(NEO::asGfxCoreIdList(ConstStringRef("gen0")).empty());
EXPECT_TRUE(NEO::asGfxCoreIdList(ConstStringRef("gen1")).empty());
EXPECT_TRUE(NEO::asGfxCoreIdList(ConstStringRef("gen2")).empty());
std::unique_ptr<OclocArgHelper> argHelper = std::make_unique<OclocArgHelper>();
EXPECT_FALSE(argHelper->isGen(ConstStringRef("genA").str()));
EXPECT_FALSE(argHelper->isGen(ConstStringRef("gen0").str()));
EXPECT_FALSE(argHelper->isGen(ConstStringRef("gen1").str()));
EXPECT_FALSE(argHelper->isGen(ConstStringRef("gen2").str()));
}
TEST(OclocFatBinaryAsGfxCoreIdList, GivenEnabledGfxCoreNameThenReturnsNonNullIGFX) {
std::unique_ptr<OclocArgHelper> argHelper = std::make_unique<OclocArgHelper>();
for (unsigned int coreId = 0; coreId < IGFX_MAX_CORE; ++coreId) {
if (nullptr != NEO::familyName[coreId]) {
EXPECT_EQ(argHelper->returnIGFXforGen(ConstStringRef(NEO::familyName[coreId]).str()), coreId);
std::string caseInsensitive = NEO::familyName[coreId];
std::transform(caseInsensitive.begin(), caseInsensitive.begin() + 1, caseInsensitive.begin(), ::tolower);
EXPECT_EQ(argHelper->returnIGFXforGen(caseInsensitive), coreId);
}
}
}
TEST(OclocFatBinaryAsGfxCoreIdList, GivenDisabledGfxCoreNameThenReturnsNullIGFX) {
std::unique_ptr<OclocArgHelper> argHelper = std::make_unique<OclocArgHelper>();
EXPECT_EQ(argHelper->returnIGFXforGen(ConstStringRef("genA").str()), 0u);
EXPECT_EQ(argHelper->returnIGFXforGen(ConstStringRef("gen0").str()), 0u);
EXPECT_EQ(argHelper->returnIGFXforGen(ConstStringRef("gen1").str()), 0u);
EXPECT_EQ(argHelper->returnIGFXforGen(ConstStringRef("gen2").str()), 0u);
}
TEST(OclocFatBinaryAppendPlatformsForGfxCore, GivenCoreIdThenAppendsEnabledProductIdsThatMatch) {

View File

@ -16,7 +16,7 @@ namespace NEO {
int MultiCommand::singleBuild(const std::vector<std::string> &args) {
int retVal = OfflineCompiler::ErrorCode::SUCCESS;
if (requestedFatBinary(args)) {
if (requestedFatBinary(args, argHelper)) {
retVal = buildFatBinary(args, argHelper);
} else {
std::unique_ptr<OfflineCompiler> pCompiler{OfflineCompiler::create(args.size(), args, true, retVal, argHelper)};

View File

@ -102,7 +102,7 @@ int oclocInvoke(unsigned int numArgs, const char *argv[],
int retValue = OfflineCompiler::ErrorCode::SUCCESS;
std::unique_ptr<MultiCommand> pMulti{(MultiCommand::create(allArgs, retValue, helper.get()))};
return retValue;
} else if (requestedFatBinary(allArgs)) {
} else if (requestedFatBinary(allArgs, helper.get())) {
return buildFatBinary(allArgs, helper.get());
} else if (numArgs > 1 && ConstStringRef("validate") == allArgs[1]) {
return NEO::Ocloc::validate(allArgs, helper.get());

View File

@ -13,6 +13,7 @@
#include "hw_cmds.h"
#include <algorithm>
#include <cstring>
#include <sstream>
@ -60,6 +61,13 @@ OclocArgHelper::OclocArgHelper(const uint32_t numSources, const uint8_t **dataSo
for (uint32_t i = 0; i < numInputHeaders; ++i) {
headers.push_back(Source(dataInputHeaders[i], static_cast<size_t>(lenInputHeaders[i]), nameInputHeaders[i]));
}
for (unsigned int i = 0; i < IGFX_MAX_CORE; ++i) {
if (NEO::familyName[i] == nullptr)
continue;
std::string gen = NEO::familyName[i];
std::transform(gen.begin(), gen.end(), gen.begin(), ::tolower);
genIGFXMap.insert({gen, i});
}
}
OclocArgHelper::OclocArgHelper() : OclocArgHelper(0, nullptr, nullptr, nullptr, 0, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr) {}
@ -170,4 +178,20 @@ std::string OclocArgHelper::returnProductNameForDevice(unsigned short deviceId)
}
}
return res;
}
bool OclocArgHelper::isGen(const std::string &device) {
std::string buf(device);
std::transform(buf.begin(), buf.end(), buf.begin(), ::tolower);
auto it = genIGFXMap.find(buf);
return it == genIGFXMap.end() ? false : true;
}
unsigned int OclocArgHelper::returnIGFXforGen(const std::string &device) {
std::string buf(device);
std::transform(buf.begin(), buf.end(), buf.begin(), ::tolower);
auto it = genIGFXMap.find(buf);
if (it == genIGFXMap.end())
return 0;
return it->second;
}

View File

@ -11,6 +11,7 @@
#include <cctype>
#include <fstream>
#include <map>
#include <memory>
#include <string>
#include <vector>
@ -53,6 +54,7 @@ class OclocArgHelper {
uint64_t **lenOutputs = nullptr;
bool hasOutput = false;
const std::vector<DeviceProduct> deviceProductTable;
std::map<std::string, unsigned int> genIGFXMap;
void moveOutputs();
MessagePrinter messagePrinter;
Source *findSourceFile(const std::string &filename);
@ -102,4 +104,6 @@ class OclocArgHelper {
messagePrinter.printf(format, std::forward<Args>(args)...);
}
std::string returnProductNameForDevice(unsigned short deviceId);
bool isGen(const std::string &device);
unsigned int returnIGFXforGen(const std::string &device);
};

View File

@ -23,13 +23,13 @@
namespace NEO {
bool requestedFatBinary(const std::vector<std::string> &args) {
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) {
ConstStringRef deviceArg(args[argIndex + 1]);
return deviceArg.contains("*") || deviceArg.contains("-") || deviceArg.contains(",") || deviceArg.containsCaseInsensitive("gen") || deviceArg.startsWith("XE_");
return deviceArg.contains("*") || deviceArg.contains("-") || deviceArg.contains(",") || helper->isGen(deviceArg.str());
}
}
return false;
@ -56,34 +56,6 @@ PRODUCT_FAMILY asProductId(ConstStringRef product, const std::vector<PRODUCT_FAM
return IGFX_UNKNOWN;
}
std::vector<GFXCORE_FAMILY> asGfxCoreIdList(ConstStringRef core) {
std::vector<GFXCORE_FAMILY> result;
constexpr size_t genPrefixLength = 3;
auto coreSuffixBeg = core.begin() + genPrefixLength;
size_t coreSuffixLength = core.end() - coreSuffixBeg;
ConstStringRef coreSuffix(coreSuffixBeg, coreSuffixLength);
for (unsigned int coreId = 0; coreId < IGFX_MAX_CORE; ++coreId) {
auto name = familyName[coreId];
if (name == nullptr)
continue;
auto nameSuffix = name + genPrefixLength;
auto nameNumberEnd = nameSuffix;
for (; *nameNumberEnd != '\0' && isdigit(*nameNumberEnd); ++nameNumberEnd)
;
size_t nameNumberLength = nameNumberEnd - nameSuffix;
if (nameNumberLength > coreSuffixLength)
continue;
if (ConstStringRef(nameSuffix, std::min(coreSuffixLength, strlen(nameSuffix))) == coreSuffix)
result.push_back(static_cast<GFXCORE_FAMILY>(coreId));
}
return result;
}
void appendPlatformsForGfxCore(GFXCORE_FAMILY core, const std::vector<PRODUCT_FAMILY> &allSupportedPlatforms, std::vector<PRODUCT_FAMILY> &out) {
for (auto family : allSupportedPlatforms) {
if (core == hardwareInfoTable[family]->platform.eRenderCoreFamily) {
@ -112,12 +84,14 @@ std::vector<ConstStringRef> getTargetPlatformsForFatbinary(ConstStringRef device
if (range.size() == 1) {
// open range , from-max or min-to
if (range[0].containsCaseInsensitive("gen") || range[0].startsWith("XE_")) {
auto coreIdList = asGfxCoreIdList(range[0]);
if (coreIdList.empty()) {
if (argHelper->isGen(range[0].str())) {
std::vector<GFXCORE_FAMILY> coreIdList;
auto coreId = argHelper->returnIGFXforGen(range[0].str());
if (coreId == 0) {
argHelper->printf("Unknown device : %s\n", set.str().c_str());
return {};
}
coreIdList.push_back(static_cast<GFXCORE_FAMILY>(coreId));
if ('-' == set[0]) {
// to
auto coreId = coreIdList.back();
@ -152,25 +126,22 @@ std::vector<ConstStringRef> getTargetPlatformsForFatbinary(ConstStringRef device
}
}
} else {
if (range[0].contains("gen") || range[0].startsWith("XE_")) {
if (false == range[1].contains("gen") && false == range[1].startsWith("XE_")) {
if (argHelper->isGen(range[0].str())) {
if (false == argHelper->isGen(range[1].str())) {
argHelper->printf("Ranges mixing platforms and gfxCores is not supported : %s - should be genFrom-genTo or platformFrom-platformTo\n", set.str().c_str());
return {};
}
auto coreFromList = asGfxCoreIdList(range[0]);
auto coreToList = asGfxCoreIdList(range[1]);
if (coreFromList.empty()) {
auto coreFrom = argHelper->returnIGFXforGen(range[0].str());
auto coreTo = argHelper->returnIGFXforGen(range[1].str());
if (coreFrom == 0) {
argHelper->printf("Unknown device : %s\n", set.str().c_str());
return {};
}
if (coreToList.empty()) {
if (coreTo == 0) {
argHelper->printf("Unknown device : %s\n", set.str().c_str());
return {};
}
auto coreFrom = coreFromList.front();
auto coreTo = coreToList.back();
if (coreFrom > coreTo) {
if (static_cast<GFXCORE_FAMILY>(coreFrom) > static_cast<GFXCORE_FAMILY>(coreTo)) {
std::swap(coreFrom, coreTo);
}
while (coreFrom <= coreTo) {
@ -197,17 +168,16 @@ std::vector<ConstStringRef> getTargetPlatformsForFatbinary(ConstStringRef device
requestedPlatforms.insert(requestedPlatforms.end(), from, to);
}
}
} else if (set.containsCaseInsensitive("gen") || deviceArg.startsWith("XE_")) {
} else if (argHelper->isGen(set.str())) {
if (set.size() == genArg.size()) {
argHelper->printf("Invalid gen-based device : %s - gen should be followed by a number\n", set.str().c_str());
} else {
auto coreIdList = asGfxCoreIdList(set);
if (coreIdList.empty()) {
auto coreId = argHelper->returnIGFXforGen(set.str());
if (coreId == 0) {
argHelper->printf("Unknown device : %s\n", set.str().c_str());
return {};
}
for (auto coreId : coreIdList)
appendPlatformsForGfxCore(coreId, allSupportedPlatforms, requestedPlatforms);
appendPlatformsForGfxCore(static_cast<GFXCORE_FAMILY>(coreId), allSupportedPlatforms, requestedPlatforms);
}
} else {
auto prodId = asProductId(set, allSupportedPlatforms);

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2020 Intel Corporation
* Copyright (C) 2020-2021 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@ -17,11 +17,11 @@
class OclocArgHelper;
namespace NEO {
bool requestedFatBinary(const std::vector<std::string> &args);
inline bool requestedFatBinary(int argc, const char *argv[]) {
bool requestedFatBinary(const std::vector<std::string> &args, OclocArgHelper *helper);
inline bool requestedFatBinary(int argc, const char *argv[], OclocArgHelper *helper) {
std::vector<std::string> args;
args.assign(argv, argv + argc);
return requestedFatBinary(args);
return requestedFatBinary(args, helper);
}
int buildFatBinary(const std::vector<std::string> &args, OclocArgHelper *argHelper);
@ -34,7 +34,6 @@ inline int buildFatBinary(int argc, const char *argv[], OclocArgHelper *argHelpe
std::vector<PRODUCT_FAMILY> getAllSupportedTargetPlatforms();
std::vector<ConstStringRef> toProductNames(const std::vector<PRODUCT_FAMILY> &productIds);
PRODUCT_FAMILY asProductId(ConstStringRef product, const std::vector<PRODUCT_FAMILY> &allSupportedPlatforms);
std::vector<GFXCORE_FAMILY> asGfxCoreIdList(ConstStringRef core);
void appendPlatformsForGfxCore(GFXCORE_FAMILY core, const std::vector<PRODUCT_FAMILY> &allSupportedPlatforms, std::vector<PRODUCT_FAMILY> &out);
std::vector<ConstStringRef> getTargetPlatformsForFatbinary(ConstStringRef deviceArg, OclocArgHelper *argHelper);

View File

@ -289,6 +289,7 @@ int OfflineCompiler::getHardwareInfo(std::string deviceName) {
int retVal = INVALID_DEVICE;
overridePlatformName(deviceName);
std::transform(deviceName.begin(), deviceName.end(), deviceName.begin(), ::tolower);
const char hexPrefix = 2;
std::string product("");