Skip to content

[SYCL] Add size to detail::string_view #18661

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

Merged
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
9 changes: 9 additions & 0 deletions sycl/include/sycl/detail/kernel_name_str_t.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#pragma once

#include <sycl/detail/string.hpp>
#include <sycl/detail/string_view.hpp>
Expand All @@ -23,6 +24,14 @@ using KernelNameStrRefT = const std::string &;
using ABINeutralKernelNameStrT = detail::string;
#endif

inline KernelNameStrT toKernelNameStrT(const ABINeutralKernelNameStrT &str) {
#ifdef __INTEL_PREVIEW_BREAKING_CHANGES
return std::string_view(str);
#else
return str.data();
#endif
}

} // namespace detail
} // namespace _V1
} // namespace sycl
2 changes: 1 addition & 1 deletion sycl/include/sycl/detail/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace detail {

// This class and detail::string_view class are intended to support
// different ABIs between libsycl and the user program.
// This class is not inteded to replace std::string for general purpose usage.
// This class is not intended to replace std::string for general purpose usage.
class string {
char *str = nullptr;

Expand Down
54 changes: 51 additions & 3 deletions sycl/include/sycl/detail/string_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,80 @@ namespace detail {

// This class and detail::string class are intended to support
// different ABIs between libsycl and the user program.
// This class is not inteded to replace std::string_view for general purpose
// This class is not intended to replace std::string_view for general purpose
// usage.

class string_view {
const char *str = nullptr;
#ifdef __INTEL_PREVIEW_BREAKING_CHANGES
size_t len = 0;
#endif

public:
string_view() noexcept = default;
string_view(const string_view &strn) noexcept = default;
string_view(string_view &&strn) noexcept = default;
string_view(std::string_view strn) noexcept : str(strn.data()) {}
string_view(const sycl::detail::string &strn) noexcept : str(strn.c_str()) {}
string_view(std::string_view strn) noexcept
: str(strn.data())
#ifdef __INTEL_PREVIEW_BREAKING_CHANGES
,
len(strn.size())
#endif
{
}
string_view(const sycl::detail::string &strn) noexcept
: str(strn.c_str())
#ifdef __INTEL_PREVIEW_BREAKING_CHANGES
,
len(strlen(strn.c_str()))
#endif
{
}

string_view &operator=(string_view &&strn) noexcept = default;
string_view &operator=(const string_view &strn) noexcept = default;

string_view &operator=(std::string_view strn) noexcept {
str = strn.data();
#ifdef __INTEL_PREVIEW_BREAKING_CHANGES
len = strn.size();
#endif
return *this;
}

string_view &operator=(const sycl::detail::string &strn) noexcept {
str = strn.c_str();
#ifdef __INTEL_PREVIEW_BREAKING_CHANGES
len = strlen(strn.c_str());
#endif
return *this;
}

const char *data() const noexcept { return str ? str : ""; }

explicit operator std::string_view() const noexcept {
#ifdef __INTEL_PREVIEW_BREAKING_CHANGES
return std::string_view(str, len);
#else
return std::string_view(str);
#endif
}

#ifdef __INTEL_PREVIEW_BREAKING_CHANGES
friend bool operator==(string_view lhs, std::string_view rhs) noexcept {
return rhs == std::string_view(lhs);
}
friend bool operator==(std::string_view lhs, string_view rhs) noexcept {
return lhs == std::string_view(rhs);
}

friend bool operator!=(string_view lhs, std::string_view rhs) noexcept {
return rhs != std::string_view(lhs);
}
friend bool operator!=(std::string_view lhs, string_view rhs) noexcept {
return lhs != std::string_view(rhs);
}
#else
friend bool operator==(string_view lhs, std::string_view rhs) noexcept {
return rhs == lhs.data();
}
Expand All @@ -57,6 +104,7 @@ class string_view {
friend bool operator!=(std::string_view lhs, string_view rhs) noexcept {
return lhs != rhs.data();
}
#endif
};

} // namespace detail
Expand Down
14 changes: 9 additions & 5 deletions sycl/include/sycl/handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,11 +537,12 @@ class __SYCL_EXPORT handler {
template <typename LambdaNameT> bool lambdaAndKernelHaveEqualName() {
// TODO It is unclear a kernel and a lambda/functor must to be equal or not
// for parallel_for with sycl::kernel and lambda/functor together
// Now if they are equal we extract argumets from lambda/functor for the
// Now if they are equal we extract arguments from lambda/functor for the
// kernel. Else it is necessary use set_atg(s) for resolve the order and
// values of arguments for the kernel.
assert(MKernel && "MKernel is not initialized");
const std::string LambdaName = detail::getKernelName<LambdaNameT>();
constexpr std::string_view LambdaName =
detail::getKernelName<LambdaNameT>();
detail::ABINeutralKernelNameStrT KernelName = getKernelName();
return KernelName == LambdaName;
}
Expand Down Expand Up @@ -865,7 +866,9 @@ class __SYCL_EXPORT handler {
&(detail::getKernelParamDesc<KernelName>),
detail::isKernelESIMD<KernelName>(), HasSpecialCapt);

MKernelName = detail::getKernelName<KernelName>();
constexpr std::string_view KernelNameStr =
detail::getKernelName<KernelName>();
MKernelName = KernelNameStr;
} else {
// In case w/o the integration header it is necessary to process
// accessors from the list(which are associated with this handler) as
Expand Down Expand Up @@ -1325,8 +1328,9 @@ class __SYCL_EXPORT handler {
KernelWrapper<WrapAs::parallel_for, NameT, KernelType, TransformedArgType,
PropertiesT>::wrap(this, KernelFunc);
#ifndef __SYCL_DEVICE_ONLY__
verifyUsedKernelBundleInternal(
detail::string_view{detail::getKernelName<NameT>()});
constexpr std::string_view Name{detail::getKernelName<NameT>()};

verifyUsedKernelBundleInternal(detail::string_view{Name});
processProperties<detail::isKernelESIMD<NameT>(), PropertiesT>(Props);
detail::checkValueRange<Dims>(UserRange);
setNDRangeDescriptor(std::move(UserRange));
Expand Down
4 changes: 2 additions & 2 deletions sycl/source/backend/opencl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ __SYCL_EXPORT bool has_extension(const sycl::platform &SyclPlatform,
std::string ExtensionsString = urGetInfoString<UrApiKind::urPlatformGetInfo>(
*getSyclObjImpl(SyclPlatform), UR_PLATFORM_INFO_EXTENSIONS);

return ExtensionsString.find(std::string_view{Extension.data()}) !=
return ExtensionsString.find(std::string_view{Extension}) !=
std::string::npos;
}

Expand All @@ -50,7 +50,7 @@ __SYCL_EXPORT bool has_extension(const sycl::device &SyclDevice,
std::string ExtensionsString = urGetInfoString<UrApiKind::urDeviceGetInfo>(
*getSyclObjImpl(SyclDevice), UR_DEVICE_INFO_EXTENSIONS);

return ExtensionsString.find(std::string_view{Extension.data()}) !=
return ExtensionsString.find(std::string_view{Extension}) !=
std::string::npos;
}
} // namespace detail
Expand Down
2 changes: 1 addition & 1 deletion sycl/source/detail/device_image_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ class device_image_impl {
extractXsFlags(const std::vector<sycl::detail::string_view> &BuildOptions) {
std::stringstream SS;
for (sycl::detail::string_view Option : BuildOptions) {
std::string_view OptionSV{Option.data()};
std::string_view OptionSV{Option};
auto Where = OptionSV.find("-Xs");
if (Where != std::string_view::npos) {
Where += 3;
Expand Down
2 changes: 1 addition & 1 deletion sycl/source/detail/graph_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1918,7 +1918,7 @@ void modifiable_command_graph::end_recording(

void modifiable_command_graph::print_graph(sycl::detail::string_view pathstr,
bool verbose) const {
std::string path{pathstr.data()};
std::string path{std::string_view(pathstr)};
graph_impl::ReadLock Lock(impl->MMutex);
if (path.substr(path.find_last_of(".") + 1) == "dot") {
impl->printGraphAsDot(std::move(path), verbose);
Expand Down
2 changes: 1 addition & 1 deletion sycl/source/detail/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ markBufferAsInternal(const std::shared_ptr<buffer_impl> &BufImpl) {
}

std::tuple<const RTDeviceBinaryImage *, ur_program_handle_t>
retrieveKernelBinary(queue_impl &Queue, const char *KernelName,
retrieveKernelBinary(queue_impl &Queue, KernelNameStrRefT KernelName,
CGExecKernel *KernelCG) {
device_impl &Dev = Queue.getDeviceImpl();
bool isNvidia = Dev.getBackend() == backend::ext_oneapi_cuda;
Expand Down
4 changes: 3 additions & 1 deletion sycl/source/detail/helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//

#include <sycl/detail/kernel_name_str_t.hpp>

#include <ur_api.h>

#include <memory>
Expand All @@ -27,7 +29,7 @@ void waitEvents(std::vector<sycl::event> DepEvents);
#endif

std::tuple<const RTDeviceBinaryImage *, ur_program_handle_t>
retrieveKernelBinary(queue_impl &Queue, const char *KernelName,
retrieveKernelBinary(queue_impl &Queue, KernelNameStrRefT KernelName,
CGExecKernel *CGKernel = nullptr);
} // namespace detail
} // namespace _V1
Expand Down
2 changes: 1 addition & 1 deletion sycl/source/detail/scheduler/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3264,7 +3264,7 @@ ur_result_t ExecCGCommand::enqueueImpQueue() {
const RTDeviceBinaryImage *BinImage = nullptr;
if (detail::SYCLConfig<detail::SYCL_JIT_AMDGCN_PTX_KERNELS>::get()) {
std::tie(BinImage, std::ignore) =
retrieveKernelBinary(*MQueue, KernelName.data());
retrieveKernelBinary(*MQueue, KernelName);
assert(BinImage && "Failed to obtain a binary image.");
}
enqueueImpKernel(
Expand Down
6 changes: 3 additions & 3 deletions sycl/source/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ template __SYCL_EXPORT std::vector<device> device::create_sub_devices<
info::partition_property::ext_intel_partition_by_cslice>() const;

bool device::has_extension(detail::string_view ext_name) const {
return impl->has_extension(ext_name.data());
return impl->has_extension(std::string(std::string_view(ext_name)));
}

template <typename Param>
Expand Down Expand Up @@ -292,7 +292,7 @@ bool device::ext_oneapi_supports_cl_c_feature(detail::string_view Feature) {
return false;

return ext::oneapi::experimental::detail::OpenCLC_Feature_Available(
Feature.data(), ipVersion);
std::string(std::string_view(Feature)), ipVersion);
}

bool device::ext_oneapi_supports_cl_c_version(
Expand Down Expand Up @@ -321,7 +321,7 @@ bool device::ext_oneapi_supports_cl_extension(
return false;

return ext::oneapi::experimental::detail::OpenCLC_Supports_Extension(
Name.data(), VersionPtr, ipVersion);
std::string(std::string_view(Name)), VersionPtr, ipVersion);
}

detail::string device::ext_oneapi_cl_profile_impl() const {
Expand Down
3 changes: 2 additions & 1 deletion sycl/source/device_selector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ int accelerator_selector::operator()(const device &dev) const {
namespace ext::oneapi {

filter_selector::filter_selector(sycl::detail::string_view Input)
: impl(std::make_shared<detail::filter_selector_impl>(Input.data())) {}
: impl(std::make_shared<detail::filter_selector_impl>(
std::string(std::string_view(Input)))) {}

int filter_selector::operator()(const device &Dev) const {
return impl->operator()(Dev);
Expand Down
23 changes: 13 additions & 10 deletions sycl/source/handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,13 +480,13 @@ event handler::finalize() {
!(MKernel && MKernel->isInterop()) &&
(KernelBundleImpPtr->empty() ||
KernelBundleImpPtr->hasSYCLOfflineImages()) &&
!KernelBundleImpPtr->tryGetKernel(MKernelName.data(),
!KernelBundleImpPtr->tryGetKernel(toKernelNameStrT(MKernelName),
KernelBundleImpPtr)) {
auto Dev =
impl->MGraph ? impl->MGraph->getDevice() : MQueue->get_device();
kernel_id KernelID =
detail::ProgramManager::getInstance().getSYCLKernelID(
MKernelName.data());
toKernelNameStrT(MKernelName));
bool KernelInserted = KernelBundleImpPtr->add_kernel(KernelID, Dev);
// If kernel was not inserted and the bundle is in input mode we try
// building it and trying to find the kernel in executable mode
Expand Down Expand Up @@ -550,7 +550,7 @@ event handler::finalize() {
bool KernelUsesAssert =
!(MKernel && MKernel->isInterop()) &&
detail::ProgramManager::getInstance().kernelUsesAssert(
MKernelName.data(), impl->MKernelNameBasedCachePtr);
toKernelNameStrT(MKernelName), impl->MKernelNameBasedCachePtr);
DiscardEvent = !KernelUsesAssert;
}

Expand Down Expand Up @@ -581,14 +581,15 @@ event handler::finalize() {
#endif
const detail::RTDeviceBinaryImage *BinImage = nullptr;
if (detail::SYCLConfig<detail::SYCL_JIT_AMDGCN_PTX_KERNELS>::get()) {
std::tie(BinImage, std::ignore) =
detail::retrieveKernelBinary(*MQueue, MKernelName.data());
std::tie(BinImage, std::ignore) = detail::retrieveKernelBinary(
*MQueue, toKernelNameStrT(MKernelName));
assert(BinImage && "Failed to obtain a binary image.");
}
enqueueImpKernel(
MQueue, impl->MNDRDesc, impl->MArgs, KernelBundleImpPtr,
MKernel.get(), MKernelName.data(), impl->MKernelNameBasedCachePtr,
RawEvents, DiscardEvent ? nullptr : LastEventImpl.get(), nullptr,
MKernel.get(), toKernelNameStrT(MKernelName),
impl->MKernelNameBasedCachePtr, RawEvents,
DiscardEvent ? nullptr : LastEventImpl.get(), nullptr,
impl->MKernelCacheConfig, impl->MKernelIsCooperative,
impl->MKernelUsesClusterLaunch, impl->MKernelWorkGroupMemorySize,
BinImage, impl->MKernelFuncPtr, impl->MKernelNumArgs,
Expand Down Expand Up @@ -638,13 +639,15 @@ event handler::finalize() {
std::unique_ptr<detail::CG> CommandGroup;
switch (type) {
case detail::CGType::Kernel: {
#ifndef __INTEL_PREVIEW_BREAKING_CHANGES
// Copy kernel name here instead of move so that it's available after
// running of this method by reductions implementation. This allows for
// assert feature to check if kernel uses assertions
#endif
CommandGroup.reset(new detail::CGExecKernel(
std::move(impl->MNDRDesc), std::move(MHostKernel), std::move(MKernel),
std::move(impl->MKernelBundle), std::move(impl->CGData),
std::move(impl->MArgs), MKernelName.data(),
std::move(impl->MArgs), toKernelNameStrT(MKernelName),
impl->MKernelNameBasedCachePtr, std::move(MStreamStorage),
std::move(impl->MAuxiliaryResources), getType(),
impl->MKernelCacheConfig, impl->MKernelIsCooperative,
Expand Down Expand Up @@ -2093,7 +2096,7 @@ backend handler::getDeviceBackend() const {

void handler::ext_intel_read_host_pipe(detail::string_view Name, void *Ptr,
size_t Size, bool Block) {
impl->HostPipeName = Name.data();
impl->HostPipeName = std::string_view(Name);
impl->HostPipePtr = Ptr;
impl->HostPipeTypeSize = Size;
impl->HostPipeBlocking = Block;
Expand All @@ -2103,7 +2106,7 @@ void handler::ext_intel_read_host_pipe(detail::string_view Name, void *Ptr,

void handler::ext_intel_write_host_pipe(detail::string_view Name, void *Ptr,
size_t Size, bool Block) {
impl->HostPipeName = Name.data();
impl->HostPipeName = std::string_view(Name);
impl->HostPipePtr = Ptr;
impl->HostPipeTypeSize = Size;
impl->HostPipeBlocking = Block;
Expand Down
Loading