Skip to content

Commit ed094f2

Browse files
[SYCL][NFC] Add urGetInfoString helper (#18292)
1 parent f5a7239 commit ed094f2

File tree

8 files changed

+47
-136
lines changed

8 files changed

+47
-136
lines changed

sycl/source/backend/opencl.cpp

+4-39
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,9 @@ __SYCL_EXPORT bool has_extension(const sycl::platform &SyclPlatform,
3232
"has_extension can only be used with an OpenCL backend");
3333
}
3434

35-
std::shared_ptr<sycl::detail::platform_impl> PlatformImpl =
36-
getSyclObjImpl(SyclPlatform);
37-
ur_platform_handle_t AdapterPlatform = PlatformImpl->getHandleRef();
38-
const AdapterPtr &Adapter = PlatformImpl->getAdapter();
35+
std::string ExtensionsString = urGetInfoString<UrApiKind::urPlatformGetInfo>(
36+
*getSyclObjImpl(SyclPlatform), UR_PLATFORM_INFO_EXTENSIONS);
3937

40-
// Manual invocation of UR API to avoid using deprecated
41-
// info::platform::extensions call.
42-
size_t ResultSize = 0;
43-
Adapter->call<UrApiKind::urPlatformGetInfo>(
44-
AdapterPlatform, UR_PLATFORM_INFO_EXTENSIONS,
45-
/*propSize=*/0,
46-
/*pPropValue=*/nullptr, &ResultSize);
47-
if (ResultSize == 0)
48-
return false;
49-
50-
std::unique_ptr<char[]> Result(new char[ResultSize]);
51-
Adapter->call<UrApiKind::urPlatformGetInfo>(
52-
AdapterPlatform, UR_PLATFORM_INFO_EXTENSIONS, ResultSize, Result.get(),
53-
nullptr);
54-
55-
std::string_view ExtensionsString(Result.get());
5638
return ExtensionsString.find(std::string_view{Extension.data()}) !=
5739
std::string::npos;
5840
}
@@ -65,26 +47,9 @@ __SYCL_EXPORT bool has_extension(const sycl::device &SyclDevice,
6547
"has_extension can only be used with an OpenCL backend");
6648
}
6749

68-
detail::device_impl &DeviceImpl = *getSyclObjImpl(SyclDevice);
69-
ur_device_handle_t AdapterDevice = DeviceImpl.getHandleRef();
70-
const AdapterPtr &Adapter = DeviceImpl.getAdapter();
71-
72-
// Manual invocation of UR API to avoid using deprecated
73-
// info::device::extensions call.
74-
size_t ResultSize = 0;
75-
Adapter->call<UrApiKind::urDeviceGetInfo>(
76-
AdapterDevice, UR_DEVICE_INFO_EXTENSIONS,
77-
/*propSize=*/0,
78-
/*pPropValue=*/nullptr, &ResultSize);
79-
if (ResultSize == 0)
80-
return false;
81-
82-
std::unique_ptr<char[]> Result(new char[ResultSize]);
83-
Adapter->call<UrApiKind::urDeviceGetInfo>(AdapterDevice,
84-
UR_DEVICE_INFO_EXTENSIONS,
85-
ResultSize, Result.get(), nullptr);
50+
std::string ExtensionsString = urGetInfoString<UrApiKind::urDeviceGetInfo>(
51+
*getSyclObjImpl(SyclDevice), UR_DEVICE_INFO_EXTENSIONS);
8652

87-
std::string_view ExtensionsString(Result.get());
8853
return ExtensionsString.find(std::string_view{Extension.data()}) !=
8954
std::string::npos;
9055
}

sycl/source/detail/device_image_impl.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <detail/mem_alloc_helper.hpp>
2020
#include <detail/persistent_device_code_cache.hpp>
2121
#include <detail/program_manager/program_manager.hpp>
22+
#include <detail/split_string.hpp>
2223
#include <sycl/context.hpp>
2324
#include <sycl/detail/common.hpp>
2425
#include <sycl/detail/ur.hpp>

sycl/source/detail/device_info.hpp

+5-27
Original file line numberDiff line numberDiff line change
@@ -199,22 +199,7 @@ template <typename Param> struct get_device_info_impl<platform, Param> {
199199
// for string return type in other specializations.
200200
inline std::string
201201
device_impl::get_device_info_string(ur_device_info_t InfoCode) const {
202-
size_t resultSize = 0;
203-
getAdapter()->call<UrApiKind::urDeviceGetInfo>(getHandleRef(), InfoCode, 0,
204-
nullptr, &resultSize);
205-
if (resultSize == 0) {
206-
return std::string();
207-
}
208-
std::string result;
209-
// C++23's `resize_and_overwrite` would be better...
210-
//
211-
// UR counts null terminator in the size, std::string doesn't. Adjust by "-1"
212-
// for that.
213-
result.resize(resultSize - 1);
214-
getAdapter()->call<UrApiKind::urDeviceGetInfo>(
215-
getHandleRef(), InfoCode, resultSize, result.data(), nullptr);
216-
217-
return result;
202+
return urGetInfoString<UrApiKind::urDeviceGetInfo>(*this, InfoCode);
218203
}
219204

220205
// Specialization for string return type, variable return size
@@ -737,17 +722,10 @@ struct get_device_info_impl<
737722
}
738723
return ext::oneapi::experimental::architecture::unknown;
739724
};
740-
size_t ResultSize = 0;
741-
Dev.getAdapter()->call<UrApiKind::urDeviceGetInfo>(
742-
Dev.getHandleRef(), UrInfoCode<info::device::version>::value, 0,
743-
nullptr, &ResultSize);
744-
std::unique_ptr<char[]> DeviceArch(new char[ResultSize]);
745-
Dev.getAdapter()->call<UrApiKind::urDeviceGetInfo>(
746-
Dev.getHandleRef(), UrInfoCode<info::device::version>::value,
747-
ResultSize, DeviceArch.get(), nullptr);
748-
std::string DeviceArchCopy(DeviceArch.get());
749-
std::string DeviceArchSubstr =
750-
DeviceArchCopy.substr(0, DeviceArchCopy.find(":"));
725+
std::string DeviceArch = urGetInfoString<UrApiKind::urDeviceGetInfo>(
726+
Dev, UrInfoCode<info::device::version>::value);
727+
std::string_view DeviceArchSubstr =
728+
std::string_view{DeviceArch}.substr(0, DeviceArch.find(":"));
751729
return MapArchIDToArchName(DeviceArchSubstr.data());
752730
} else if (Dev.is_cpu() && backend::opencl == CurrentBackend) {
753731
return LookupIPVersion(IntelCPUArchitectures)

sycl/source/detail/platform_impl.cpp

+11-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <detail/device_impl.hpp>
1212
#include <detail/global_handler.hpp>
1313
#include <detail/platform_impl.hpp>
14-
#include <detail/platform_info.hpp>
14+
#include <detail/split_string.hpp>
1515
#include <detail/ur_info_code.hpp>
1616
#include <sycl/backend_types.hpp>
1717
#include <sycl/detail/iostream_proxy.hpp>
@@ -547,9 +547,9 @@ platform_impl::get_devices(info::device_type DeviceType) const {
547547
}
548548

549549
bool platform_impl::has_extension(const std::string &ExtensionName) const {
550-
std::string AllExtensionNames = get_platform_info_string_impl(
551-
MPlatform, getAdapter(),
552-
detail::UrInfoCode<info::platform::extensions>::value);
550+
551+
std::string AllExtensionNames = urGetInfoString<UrApiKind::urPlatformGetInfo>(
552+
*this, detail::UrInfoCode<info::platform::extensions>::value);
553553
return (AllExtensionNames.find(ExtensionName) != std::string::npos);
554554
}
555555

@@ -567,7 +567,13 @@ ur_native_handle_t platform_impl::getNative() const {
567567

568568
template <typename Param>
569569
typename Param::return_type platform_impl::get_info() const {
570-
return get_platform_info<Param>(this->getHandleRef(), getAdapter());
570+
std::string InfoStr = urGetInfoString<UrApiKind::urPlatformGetInfo>(
571+
*this, detail::UrInfoCode<Param>::value);
572+
if constexpr (std::is_same_v<Param, info::platform::extensions>) {
573+
return split_string(InfoStr, ' ');
574+
} else {
575+
return InfoStr;
576+
}
571577
}
572578

573579
#ifndef __INTEL_PREVIEW_BREAKING_CHANGES

sycl/source/detail/platform_impl.hpp

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#pragma once
1010

1111
#include <detail/adapter.hpp>
12-
#include <detail/platform_info.hpp>
1312
#include <detail/ur.hpp>
1413
#include <detail/ur_info_code.hpp>
1514
#include <sycl/backend_types.hpp>

sycl/source/detail/platform_info.hpp

-64
This file was deleted.

sycl/source/detail/ur.hpp

+25
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <ur_api.h>
1818

1919
#include <memory>
20+
#include <string>
2021
#include <vector>
2122

2223
namespace sycl {
@@ -40,6 +41,30 @@ template <backend BE> const AdapterPtr &getAdapter();
4041
// Convert from UR backend to SYCL backend enum
4142
backend convertUrBackend(ur_platform_backend_t UrBackend);
4243

44+
template <auto ApiKind, typename SyclImplTy, typename DescTy>
45+
std::string urGetInfoString(SyclImplTy &SyclImpl, DescTy Desc) {
46+
// Avoid explicit type to keep template-type-dependent.
47+
auto &Adapter = SyclImpl.getAdapter();
48+
size_t ResultSize = 0;
49+
auto Handle = SyclImpl.getHandleRef();
50+
Adapter->template call<ApiKind>(Handle, Desc,
51+
/*propSize=*/0,
52+
/*pPropValue=*/nullptr, &ResultSize);
53+
if (ResultSize == 0)
54+
return std::string{};
55+
56+
std::string Result;
57+
// C++23's `resize_and_overwrite` would be better...
58+
//
59+
// UR counts null terminator in the size, std::string doesn't. Adjust by "-1"
60+
// for that.
61+
Result.resize(ResultSize - 1);
62+
Adapter->template call<ApiKind>(Handle, Desc, ResultSize, Result.data(),
63+
nullptr);
64+
65+
return Result;
66+
}
67+
4368
} // namespace detail
4469
} // namespace _V1
4570
} // namespace sycl

sycl/unittests/kernel-and-program/DeviceInfo.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include <detail/context_impl.hpp>
10+
#include <detail/split_string.hpp>
1011
#include <gtest/gtest.h>
1112
#include <helpers/UrMock.hpp>
1213
#include <sycl/sycl.hpp>

0 commit comments

Comments
 (0)