Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using optimized funcs std::find, strchr with char param, constexpr calc len out-of-loop #791

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions opencl/source/command_queue/command_queue_staging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ cl_int CommandQueue::enqueueStagingBufferMemcpy(cl_bool blockingCopy, void *dstP
CsrSelectionArgs csrSelectionArgs{CL_COMMAND_SVM_MEMCPY, &size};
csrSelectionArgs.direction = TransferDirection::hostToLocal;
auto csr = &selectCsrForBuiltinOperation(csrSelectionArgs);
cl_event profilingEvent;
cl_event profilingEvent = nullptr;

bool isSingleTransfer = false;
ChunkCopyFunction chunkCopy = [&](void *chunkSrc, void *chunkDst, size_t chunkSize) -> int32_t {
Expand All @@ -46,7 +46,7 @@ cl_int CommandQueue::enqueueStagingWriteImage(Image *dstImage, cl_bool blockingC
size_t inputRowPitch, size_t inputSlicePitch, const void *ptr, cl_event *event) {
CsrSelectionArgs csrSelectionArgs{CL_COMMAND_WRITE_IMAGE, nullptr, dstImage, this->getDevice().getRootDeviceIndex(), globalRegion, nullptr, globalOrigin};
auto &csr = selectCsrForBuiltinOperation(csrSelectionArgs);
cl_event profilingEvent;
cl_event profilingEvent = nullptr;

bool isSingleTransfer = false;
ChunkTransferImageFunc chunkWrite = [&](void *stagingBuffer, const size_t *origin, const size_t *region) -> int32_t {
Expand All @@ -69,7 +69,7 @@ cl_int CommandQueue::enqueueStagingReadImage(Image *srcImage, cl_bool blockingCo
size_t inputRowPitch, size_t inputSlicePitch, const void *ptr, cl_event *event) {
CsrSelectionArgs csrSelectionArgs{CL_COMMAND_READ_IMAGE, srcImage, nullptr, this->getDevice().getRootDeviceIndex(), globalRegion, nullptr, globalOrigin};
auto &csr = selectCsrForBuiltinOperation(csrSelectionArgs);
cl_event profilingEvent;
cl_event profilingEvent = nullptr;

bool isSingleTransfer = false;
ChunkTransferImageFunc chunkRead = [&](void *stagingBuffer, const size_t *origin, const size_t *region) -> int32_t {
Expand Down
6 changes: 3 additions & 3 deletions shared/offline_compiler/source/decoder/binary_decoder.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2024 Intel Corporation
* Copyright (C) 2018-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
Expand Down Expand Up @@ -237,7 +237,7 @@ void BinaryDecoder::parseTokens() {
break;
} else if (patchList[i].find("PATCH_TOKEN") == std::string::npos) {
continue;
} else if (patchList[i].find("@") == std::string::npos) {
} else if (patchList[i].find('@') == std::string::npos) {
continue;
}

Expand All @@ -254,7 +254,7 @@ void BinaryDecoder::parseTokens() {
nameEndPos = patchList[i].find(',', nameStartPos);
patchTokenPtr->name = patchList[i].substr(nameStartPos, nameEndPos - nameStartPos);

nameStartPos = patchList[i].find("@");
nameStartPos = patchList[i].find('@');
nameEndPos = patchList[i].find('@', nameStartPos + 1);
if (nameEndPos == std::string::npos) {
continue;
Expand Down
4 changes: 2 additions & 2 deletions shared/offline_compiler/source/multi_command.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019-2024 Intel Corporation
* Copyright (C) 2019-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
Expand Down Expand Up @@ -189,7 +189,7 @@ int MultiCommand::splitLineInSeparateArgs(std::vector<std::string> &qargs, const
continue;
} else {
start = i;
end = commandsLine.find(" ", start);
end = commandsLine.find(' ', start);
end = (end == std::string::npos) ? commandsLine.length() : end;
}
if (end == std::string::npos) {
Expand Down
4 changes: 2 additions & 2 deletions shared/offline_compiler/source/ocloc_fatbinary.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020-2024 Intel Corporation
* Copyright (C) 2020-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
Expand Down Expand Up @@ -306,7 +306,7 @@ int buildFatBinaryForTarget(int retVal, const std::vector<std::string> &argsCopy
}

std::string entryName("");
if (product.find(".") != std::string::npos) {
if (product.find('.') != std::string::npos) {
entryName = product;
} else {
auto productConfig = argHelper->productConfigHelper->getProductConfigFromDeviceName(product);
Expand Down
10 changes: 5 additions & 5 deletions shared/offline_compiler/source/offline_compiler.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2024 Intel Corporation
* Copyright (C) 2018-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
Expand Down Expand Up @@ -247,7 +247,7 @@ std::string formatNameVersionString(std::vector<NameVersionPair> extensions, boo
for (const auto &ext : extensions) {
formatedExtensions.push_back({});
auto it = formatedExtensions.rbegin();
bool needsQuoutes = (nullptr != strstr(ext.name, " "));
bool needsQuoutes = (nullptr != strchr(ext.name, ' '));
it->reserve(strnlen_s(ext.name, sizeof(ext.name)) + (needsQuoutes ? 2 : 0) + (needVersions ? 16 : 0));
if (needsQuoutes) {
it->append("\"");
Expand Down Expand Up @@ -888,7 +888,7 @@ int OfflineCompiler::initialize(size_t numArgs, const std::vector<std::string> &

if (options.empty()) {
// try to read options from file if not provided by commandline
size_t extStart = inputFile.find_last_of(".");
size_t extStart = inputFile.find_last_of('.');
if (extStart != std::string::npos) {
std::string oclocOptionsFileName = inputFile.substr(0, extStart);
oclocOptionsFileName.append("_ocloc_options.txt");
Expand Down Expand Up @@ -1236,7 +1236,7 @@ std::string OfflineCompiler::parseBinAsCharArray(uint8_t *binary, size_t size, s

std::string OfflineCompiler::getFileNameTrunk(std::string &filePath) {
size_t slashPos = filePath.find_last_of("\\/", filePath.size()) + 1;
size_t extPos = filePath.find_last_of(".", filePath.size());
size_t extPos = filePath.find_last_of('.', filePath.size());
if (extPos == std::string::npos) {
extPos = filePath.size();
}
Expand Down Expand Up @@ -1576,7 +1576,7 @@ void OfflineCompiler::writeOutAllFiles() {
if (outputFile.empty()) {
elfOutputFile = generateFilePath(outputDirectory, fileBase, ".bin");
} else {
size_t extPos = fileBase.find_last_of(".", fileBase.size());
size_t extPos = fileBase.find_last_of('.', fileBase.size());
std::string fileExt = ".bin";
if (extPos != std::string::npos) {
auto existingExt = fileBase.substr(extPos, fileBase.size());
Expand Down
4 changes: 2 additions & 2 deletions shared/source/device_binary_format/zebin/zeinfo_decoder.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2023-2024 Intel Corporation
* Copyright (C) 2023-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
Expand Down Expand Up @@ -354,7 +354,7 @@ void populateKernelMiscInfo(KernelDescriptor &dst, KernelMiscArgInfos &kernelMis
dstTypeTraits.typeQualifiers = KernelArgMetadata::parseTypeQualifiers(dstMetadata.typeQualifiers);
dst.payloadMappings.explicitArgs.at(srcMetadata.index).getTraits() = std::move(dstTypeTraits);

dstMetadata.type = dstMetadata.type.substr(0U, dstMetadata.type.find(";"));
dstMetadata.type = dstMetadata.type.substr(0U, dstMetadata.type.find(';'));
dst.explicitArgsExtendedMetadata.at(srcMetadata.index) = std::move(dstMetadata);
}
}
Expand Down
12 changes: 6 additions & 6 deletions shared/source/helpers/product_config_helper.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Intel Corporation
* Copyright (C) 2022-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
Expand Down Expand Up @@ -47,7 +47,7 @@ void ProductConfigHelper::adjustDeviceName(std::string &device) {
device = device.substr(0, findCore);
}

auto findUnderscore = device.find("_");
auto findUnderscore = device.find('_');
if (findUnderscore != std::string::npos) {
device.erase(std::remove(device.begin(), device.end(), '_'), device.end());
}
Expand Down Expand Up @@ -146,7 +146,7 @@ bool ProductConfigHelper::isSupportedProductConfig(uint32_t config) const {

AOT::PRODUCT_CONFIG ProductConfigHelper::getProductConfigFromDeviceName(const std::string &device) const {
uint32_t config = AOT::UNKNOWN_ISA;
if (device.find(".") != std::string::npos) {
if (device.find('.') != std::string::npos) {
config = getProductConfigFromVersionValue(device);
} else if (std::all_of(device.begin(), device.end(), (::isdigit))) {
config = static_cast<uint32_t>(std::stoul(device));
Expand Down Expand Up @@ -212,7 +212,7 @@ std::vector<NEO::ConstStringRef> ProductConfigHelper::getAllProductAcronyms() {

PRODUCT_FAMILY ProductConfigHelper::getProductFamilyFromDeviceName(const std::string &device) const {
std::vector<DeviceAotInfo>::const_iterator it;
if (device.find(".") != std::string::npos) {
if (device.find('.') != std::string::npos) {
it = std::find_if(deviceAotInfo.begin(), deviceAotInfo.end(), findProductConfig(getProductConfigFromVersionValue(device)));
} else {
it = std::find_if(deviceAotInfo.begin(), deviceAotInfo.end(), findAcronym(device));
Expand Down Expand Up @@ -271,13 +271,13 @@ int ProductConfigHelper::parseProductConfigFromString(const std::string &device,
}

uint32_t ProductConfigHelper::getProductConfigFromVersionValue(const std::string &device) {
auto majorPos = device.find(".");
auto majorPos = device.find('.');
auto major = parseProductConfigFromString(device, 0, majorPos);
if (major == ConfigStatus::MismatchedValue || majorPos == std::string::npos) {
return AOT::UNKNOWN_ISA;
}

auto minorPos = device.find(".", ++majorPos);
auto minorPos = device.find('.', ++majorPos);
auto minor = parseProductConfigFromString(device, majorPos, minorPos);

if (minor == ConfigStatus::MismatchedValue || minorPos == std::string::npos) {
Expand Down
15 changes: 9 additions & 6 deletions shared/source/os_interface/linux/drm_neo.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2024 Intel Corporation
* Copyright (C) 2018-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
Expand Down Expand Up @@ -659,15 +659,17 @@ std::vector<std::unique_ptr<HwDeviceId>> Drm::discoverDevices(ExecutionEnvironme

do {
const char *renderDeviceSuffix = "-render";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const char *renderDeviceSuffix = "-render";
constexpr const char *renderDeviceSuffix = "-render";

to allow compile time size calculation

constexpr size_t renderDevSufSize = std::char_traits<char>::length(renderDeviceSuffix);
constexpr size_t pciDevDirLen = std::char_traits<char>::length(Os::pciDevicesDirectory);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Os::pciDevicesDirectory is not known at compile time as it is defined differently for ULT and for runtime, hence the size cannot be constexpr

for (std::vector<std::string>::iterator file = files.begin(); file != files.end(); ++file) {
std::string_view devicePathView(file->c_str(), file->size());
devicePathView = devicePathView.substr(strlen(Os::pciDevicesDirectory));
devicePathView = devicePathView.substr(pciDevDirLen);

auto rdsPos = devicePathView.rfind(renderDeviceSuffix);
if (rdsPos == std::string::npos) {
continue;
}
if (rdsPos < devicePathView.size() - strlen(renderDeviceSuffix)) {
if (rdsPos < devicePathView.size() - renderDevSufSize) {
continue;
}
// at least 'pci-0000:00:00.0' -> 16
Expand Down Expand Up @@ -1091,9 +1093,12 @@ bool Drm::completionFenceSupport() {

void Drm::setupIoctlHelper(const PRODUCT_FAMILY productFamily) {
if (!this->ioctlHelper) {
auto drmVersion = Drm::getDrmVersion(getFileDescriptor());
auto productSpecificIoctlHelperCreator = ioctlHelperFactory[productFamily];
if (productSpecificIoctlHelperCreator && !debugManager.flags.IgnoreProductSpecificIoctlHelper.get()) {
this->ioctlHelper = productSpecificIoctlHelperCreator.value()(*this);
} else if ("xe" == drmVersion) {
this->ioctlHelper = IoctlHelperXe::create(*this);
} else {
std::string prelimVersion = "";
getPrelimVersion(prelimVersion);
Expand Down Expand Up @@ -1726,10 +1731,8 @@ bool Drm::isDrmSupported(int fileDescriptor) {
bool Drm::queryDeviceIdAndRevision() {
auto drmVersion = Drm::getDrmVersion(getFileDescriptor());
if ("xe" == drmVersion) {
this->ioctlHelper = IoctlHelperXe::create(*this);
auto xeIoctlHelperPtr = static_cast<IoctlHelperXe *>(this->ioctlHelper.get());
this->setPerContextVMRequired(false);
return xeIoctlHelperPtr->initialize();
return IoctlHelperXe::queryDeviceIdAndRevision(*this);
}
return IoctlHelperI915::queryDeviceIdAndRevision(*this);
}
Expand Down
4 changes: 0 additions & 4 deletions shared/source/os_interface/linux/xe/ioctl_helper_xe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,6 @@ bool IoctlHelperXe::initialize() {

euDebugInterface = EuDebugInterface::create(drm.getSysFsPciPath());

if (!IoctlHelperXe::queryDeviceIdAndRevision(this->drm)) {
return false;
}

drm_xe_device_query queryConfig = {};
queryConfig.query = DRM_XE_DEVICE_QUERY_CONFIG;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ using namespace NEO;

using IoctlHelperTestsDg1 = ::testing::Test;

DG1TEST_F(IoctlHelperTestsDg1, givenDg1WhenSetupIoctlHelperThenDg1SpecificHelperIsAvailable) {
EXPECT_TRUE(ioctlHelperFactory[IGFX_DG1].has_value());
}

DG1TEST_F(IoctlHelperTestsDg1, givenDg1WhenCreateGemExtThenReturnCorrectValue) {
auto executionEnvironment = std::make_unique<MockExecutionEnvironment>();
auto drm = std::make_unique<DrmTipMock>(*executionEnvironment->rootDeviceEnvironments[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1959,13 +1959,10 @@ TEST_F(IoctlHelperXeTest, whenInitializeThenProperHwInfoIsSet) {

auto hwInfo = executionEnvironment->rootDeviceEnvironments[0]->getMutableHardwareInfo();

hwInfo->platform.usDeviceID = defaultHwInfo->platform.usDeviceID + 1;
hwInfo->platform.usRevId = defaultHwInfo->platform.usRevId + 1;
hwInfo->capabilityTable.gpuAddressSpace = 0;

ioctlHelper->initialize();
EXPECT_EQ(defaultHwInfo->platform.usRevId, hwInfo->platform.usRevId);
EXPECT_EQ(defaultHwInfo->platform.usDeviceID, hwInfo->platform.usDeviceID);

EXPECT_EQ((1ull << 48) - 1, hwInfo->capabilityTable.gpuAddressSpace);
EXPECT_EQ(static_cast<uint32_t>(DrmMockXe::mockDefaultCxlType), hwInfo->capabilityTable.cxlType);

Expand Down