Skip to content

Commit 8d75f1c

Browse files
[SYCL] Make backend a property of a platform (#9153)
Signed-off-by: Sergey V Maslov <sergey.v.maslov@intel.com>
1 parent 2a3aaee commit 8d75f1c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+456
-293
lines changed

sycl/include/sycl/backend.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ namespace detail {
5151
// TODO each backend can have its own custom errc enumeration
5252
// but the details for this are not fully specified yet
5353
enum class backend_errc : unsigned int {};
54+
55+
// Convert from PI backend to SYCL backend enum
56+
backend convertBackend(pi_platform_backend PiBackend);
5457
} // namespace detail
5558

5659
template <backend Backend> class backend_traits {

sycl/include/sycl/detail/pi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ typedef enum {
210210
PI_EXT_PLATFORM_BACKEND_OPENCL = 2, ///< The backend is OpenCL
211211
PI_EXT_PLATFORM_BACKEND_CUDA = 3, ///< The backend is CUDA
212212
PI_EXT_PLATFORM_BACKEND_HIP = 4, ///< The backend is HIP
213+
PI_EXT_PLATFORM_BACKEND_ESIMD = 5, ///< The backend is ESIMD
213214
} _pi_platform_backend;
214215

215216
typedef enum {

sycl/include/sycl/detail/pi.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ void handleUnknownParamName(const char *functionName, T parameter) {
117117
using PiPlugin = ::pi_plugin;
118118
using PiResult = ::pi_result;
119119
using PiPlatform = ::pi_platform;
120+
using PiPlatformBackend = ::pi_platform_backend;
120121
using PiDevice = ::pi_device;
121122
using PiDeviceType = ::pi_device_type;
122123
using PiDeviceInfo = ::pi_device_info;

sycl/plugins/esimd_emulator/pi_esimd_emulator.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,9 @@ pi_result piPlatformGetInfo(pi_platform Platform, pi_platform_info ParamName,
485485
return ReturnValue("");
486486

487487
case PI_EXT_PLATFORM_INFO_BACKEND:
488-
return getInfo<pi_platform_backend>(
489-
ParamValueSize, ParamValue, ParamValueSizeRet,
490-
PI_EXT_PLATFORM_BACKEND_UNKNOWN); // TODO: add ESIMD to UR?
488+
return getInfo<pi_platform_backend>(ParamValueSize, ParamValue,
489+
ParamValueSizeRet,
490+
PI_EXT_PLATFORM_BACKEND_ESIMD);
491491

492492
default:
493493
// TODO: implement other parameters

sycl/source/backend.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,27 @@ static const plugin &getPlugin(backend Backend) {
3838
case backend::ext_oneapi_cuda:
3939
return pi::getPlugin<backend::ext_oneapi_cuda>();
4040
default:
41-
throw sycl::runtime_error{"Unsupported backend",
41+
throw sycl::runtime_error{"getPlugin: Unsupported backend",
42+
PI_ERROR_INVALID_OPERATION};
43+
}
44+
}
45+
46+
backend convertBackend(pi_platform_backend PiBackend) {
47+
switch (PiBackend) {
48+
case PI_EXT_PLATFORM_BACKEND_UNKNOWN:
49+
return backend::all; // No specific backend
50+
case PI_EXT_PLATFORM_BACKEND_LEVEL_ZERO:
51+
return backend::ext_oneapi_level_zero;
52+
case PI_EXT_PLATFORM_BACKEND_OPENCL:
53+
return backend::opencl;
54+
case PI_EXT_PLATFORM_BACKEND_CUDA:
55+
return backend::ext_oneapi_cuda;
56+
case PI_EXT_PLATFORM_BACKEND_HIP:
57+
return backend::ext_oneapi_hip;
58+
case PI_EXT_PLATFORM_BACKEND_ESIMD:
59+
return backend::ext_intel_esimd_emulator;
60+
default:
61+
throw sycl::runtime_error{"convertBackend: Unsupported backend",
4262
PI_ERROR_INVALID_OPERATION};
4363
}
4464
}
@@ -176,7 +196,7 @@ make_kernel_bundle(pi_native_handle NativeHandle, const context &TargetContext,
176196
pi::PiProgram PiProgram = nullptr;
177197
Plugin.call<PiApiKind::piextProgramCreateWithNativeHandle>(
178198
NativeHandle, ContextImpl->getHandleRef(), !KeepOwnership, &PiProgram);
179-
if (Plugin.getBackend() == backend::opencl)
199+
if (ContextImpl->getBackend() == backend::opencl)
180200
Plugin.call<PiApiKind::piProgramRetain>(PiProgram);
181201

182202
std::vector<pi::PiDevice> ProgramDevices;

sycl/source/context.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ bool context::is_host() const {
130130
return IsHost;
131131
}
132132

133-
backend context::get_backend() const noexcept { return getImplBackend(impl); }
133+
backend context::get_backend() const noexcept { return impl->getBackend(); }
134134

135135
platform context::get_platform() const {
136136
return impl->get_info<info::context::platform>();

sycl/source/detail/allowlist.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8-
98
#include <detail/allowlist.hpp>
109
#include <detail/config.hpp>
1110
#include <detail/device_impl.hpp>
11+
#include <detail/device_info.hpp>
1212
#include <detail/platform_info.hpp>
13+
#include <sycl/backend.hpp>
1314

1415
#include <algorithm>
1516
#include <regex>
@@ -335,15 +336,17 @@ bool deviceIsAllowed(const DeviceDescT &DeviceDesc,
335336

336337
void applyAllowList(std::vector<RT::PiDevice> &PiDevices,
337338
RT::PiPlatform PiPlatform, const plugin &Plugin) {
339+
338340
AllowListParsedT AllowListParsed =
339341
parseAllowList(SYCLConfig<SYCL_DEVICE_ALLOWLIST>::get());
340342
if (AllowListParsed.empty())
341343
return;
342344

345+
// Get platform's backend and put it to DeviceDesc
343346
DeviceDescT DeviceDesc;
347+
auto PlatformImpl = platform_impl::getOrMakePlatformImpl(PiPlatform, Plugin);
348+
backend Backend = PlatformImpl->getBackend();
344349

345-
// get BackendName value and put it to DeviceDesc
346-
sycl::backend Backend = Plugin.getBackend();
347350
for (const auto &SyclBe : getSyclBeMap()) {
348351
if (SyclBe.second == Backend) {
349352
DeviceDesc.emplace(BackendNameKeyName, SyclBe.first);
@@ -361,6 +364,7 @@ void applyAllowList(std::vector<RT::PiDevice> &PiDevices,
361364

362365
int InsertIDx = 0;
363366
for (RT::PiDevice Device : PiDevices) {
367+
auto DeviceImpl = PlatformImpl->getOrMakeDeviceImpl(Device, PlatformImpl);
364368
// get DeviceType value and put it to DeviceDesc
365369
RT::PiDeviceType PiDevType;
366370
Plugin.call<PiApiKind::piDeviceGetInfo>(Device, PI_DEVICE_INFO_TYPE,
@@ -376,19 +380,18 @@ void applyAllowList(std::vector<RT::PiDevice> &PiDevices,
376380
}
377381
// get DeviceVendorId value and put it to DeviceDesc
378382
uint32_t DeviceVendorIdUInt =
379-
sycl::detail::get_device_info<info::device::vendor_id>(Device, Plugin);
383+
sycl::detail::get_device_info<info::device::vendor_id>(DeviceImpl);
380384
std::stringstream DeviceVendorIdHexStringStream;
381385
DeviceVendorIdHexStringStream << "0x" << std::hex << DeviceVendorIdUInt;
382386
const auto &DeviceVendorIdValue = DeviceVendorIdHexStringStream.str();
383387
DeviceDesc[DeviceVendorIdKeyName] = DeviceVendorIdValue;
384388
// get DriverVersion value and put it to DeviceDesc
385389
const std::string &DriverVersionValue =
386-
sycl::detail::get_device_info<info::device::driver_version>(Device,
387-
Plugin);
390+
sycl::detail::get_device_info<info::device::driver_version>(DeviceImpl);
388391
DeviceDesc[DriverVersionKeyName] = DriverVersionValue;
389392
// get DeviceName value and put it to DeviceDesc
390393
const std::string &DeviceNameValue =
391-
sycl::detail::get_device_info<info::device::name>(Device, Plugin);
394+
sycl::detail::get_device_info<info::device::name>(DeviceImpl);
392395
DeviceDesc[DeviceNameKeyName] = DeviceNameValue;
393396

394397
// check if we can allow device with such device description DeviceDesc

sycl/source/detail/backend_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace detail {
1616

1717
template <class T> backend getImplBackend(const T &Impl) {
1818
assert(!Impl->is_host() && "Cannot get the backend for host.");
19-
return Impl->getPlugin().getBackend();
19+
return Impl->getContextImplPtr()->getBackend();
2020
}
2121

2222
} // namespace detail

sycl/source/detail/buffer_impl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ buffer_impl::getNativeVector(backend BackendName) const {
7676
continue;
7777
auto Plugin = Platform->getPlugin();
7878

79-
if (Plugin.getBackend() != BackendName)
79+
if (Platform->getBackend() != BackendName)
8080
continue;
81-
if (Plugin.getBackend() == backend::opencl) {
81+
if (Platform->getBackend() == backend::opencl) {
8282
Plugin.call<PiApiKind::piMemRetain>(NativeMem);
8383
}
8484

sycl/source/detail/context_impl.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ context_impl::context_impl(const std::vector<sycl::device> Devices,
5050
DeviceIds.push_back(getSyclObjImpl(D)->getHandleRef());
5151
}
5252

53-
const auto Backend = getPlugin().getBackend();
54-
if (Backend == backend::ext_oneapi_cuda) {
53+
if (getBackend() == backend::ext_oneapi_cuda) {
5554
const bool UseCUDAPrimaryContext = MPropList.has_property<
5655
ext::oneapi::cuda::property::context::use_primary_context>();
5756
const pi_context_properties Props[] = {
@@ -102,7 +101,7 @@ context_impl::context_impl(RT::PiContext PiContext, async_handler AsyncHandler,
102101
//
103102
// TODO: Move this backend-specific retain of the context to SYCL-2020 style
104103
// make_context<backend::opencl> interop, when that is created.
105-
if (getPlugin().getBackend() == sycl::backend::opencl) {
104+
if (getBackend() == sycl::backend::opencl) {
106105
getPlugin().call<PiApiKind::piContextRetain>(MContext);
107106
}
108107
MKernelProgramCache.setContextPtr(this);
@@ -257,7 +256,7 @@ context_impl::findMatchingDeviceImpl(RT::PiDevice &DevicePI) const {
257256

258257
pi_native_handle context_impl::getNative() const {
259258
auto Plugin = getPlugin();
260-
if (Plugin.getBackend() == backend::opencl)
259+
if (getBackend() == backend::opencl)
261260
Plugin.call<PiApiKind::piContextRetain>(getHandleRef());
262261
pi_native_handle Handle;
263262
Plugin.call<PiApiKind::piextContextGetNativeHandle>(getHandleRef(), &Handle);

sycl/source/detail/context_impl.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ class context_impl {
173173
// OpenCL does not support using descendants of context members within that
174174
// context yet.
175175
// TODO remove once this limitation is lifted
176-
if (!is_host() && getPlugin().getBackend() == backend::opencl)
176+
if (!is_host() && Device->getBackend() == backend::opencl)
177177
return hasDevice(Device);
178178

179179
while (!hasDevice(Device)) {
@@ -186,6 +186,9 @@ class context_impl {
186186
return true;
187187
}
188188

189+
// Returns the backend of this context
190+
backend getBackend() const { return MPlatform->getBackend(); }
191+
189192
/// Given a PiDevice, returns the matching shared_ptr<device_impl>
190193
/// within this context. May return nullptr if no match discovered.
191194
DeviceImplPtr findMatchingDeviceImpl(RT::PiDevice &DevicePI) const;

sycl/source/detail/device_image_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ class device_image_impl {
237237
const auto &ContextImplPtr = detail::getSyclObjImpl(MContext);
238238
const plugin &Plugin = ContextImplPtr->getPlugin();
239239

240-
if (Plugin.getBackend() == backend::opencl)
240+
if (ContextImplPtr->getBackend() == backend::opencl)
241241
Plugin.call<PiApiKind::piProgramRetain>(MProgram);
242242
pi_native_handle NativeProgram = 0;
243243
Plugin.call<PiApiKind::piextProgramGetNativeHandle>(MProgram,

sycl/source/detail/device_impl.cpp

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include <detail/device_impl.hpp>
10+
#include <detail/device_info.hpp>
1011
#include <detail/platform_impl.hpp>
1112
#include <sycl/device.hpp>
1213

@@ -110,14 +111,39 @@ platform device_impl::get_platform() const {
110111
return createSyclObjFromImpl<platform>(MPlatform);
111112
}
112113

114+
template <typename Param>
115+
typename Param::return_type device_impl::get_info() const {
116+
if (is_host()) {
117+
return get_device_info_host<Param>();
118+
}
119+
return get_device_info<Param>(MPlatform->getDeviceImpl(MDevice));
120+
}
121+
// Explicitly instantiate all device info traits
122+
#define __SYCL_PARAM_TRAITS_SPEC(DescType, Desc, ReturnT, PiCode) \
123+
template ReturnT device_impl::get_info<info::device::Desc>() const;
124+
125+
#define __SYCL_PARAM_TRAITS_SPEC_SPECIALIZED(DescType, Desc, ReturnT, PiCode) \
126+
template ReturnT device_impl::get_info<info::device::Desc>() const;
127+
128+
#include <sycl/info/device_traits.def>
129+
#undef __SYCL_PARAM_TRAITS_SPEC_SPECIALIZED
130+
#undef __SYCL_PARAM_TRAITS_SPEC
131+
132+
#define __SYCL_PARAM_TRAITS_SPEC(Namespace, DescType, Desc, ReturnT, PiCode) \
133+
template __SYCL_EXPORT ReturnT \
134+
device_impl::get_info<Namespace::info::DescType::Desc>() const;
135+
136+
#include <sycl/info/ext_codeplay_device_traits.def>
137+
#include <sycl/info/ext_intel_device_traits.def>
138+
#include <sycl/info/ext_oneapi_device_traits.def>
139+
#undef __SYCL_PARAM_TRAITS_SPEC
140+
113141
bool device_impl::has_extension(const std::string &ExtensionName) const {
114142
if (MIsHostDevice)
115143
// TODO: implement extension management for host device;
116144
return false;
117-
118-
std::string AllExtensionNames = get_device_info_string(
119-
this->getHandleRef(), PiInfoCode<info::device::extensions>::value,
120-
this->getPlugin());
145+
std::string AllExtensionNames =
146+
get_device_info_string(PiInfoCode<info::device::extensions>::value);
121147
return (AllExtensionNames.find(ExtensionName) != std::string::npos);
122148
}
123149

@@ -275,7 +301,7 @@ std::vector<device> device_impl::create_sub_devices() const {
275301

276302
pi_native_handle device_impl::getNative() const {
277303
auto Plugin = getPlugin();
278-
if (Plugin.getBackend() == backend::opencl)
304+
if (getBackend() == backend::opencl)
279305
Plugin.call<PiApiKind::piDeviceRetain>(getHandleRef());
280306
pi_native_handle Handle;
281307
Plugin.call<PiApiKind::piextDeviceGetNativeHandle>(getHandleRef(), &Handle);
@@ -327,18 +353,17 @@ bool device_impl::has(aspect Aspect) const {
327353
return get_info<info::device::usm_host_allocations>();
328354
case aspect::usm_atomic_host_allocations:
329355
return is_host() ||
330-
(get_device_info_impl<
331-
pi_usm_capabilities,
332-
info::device::usm_host_allocations>::get(MDevice, getPlugin()) &
356+
(get_device_info_impl<pi_usm_capabilities,
357+
info::device::usm_host_allocations>::
358+
get(MPlatform->getDeviceImpl(MDevice)) &
333359
PI_USM_CONCURRENT_ATOMIC_ACCESS);
334360
case aspect::usm_shared_allocations:
335361
return get_info<info::device::usm_shared_allocations>();
336362
case aspect::usm_atomic_shared_allocations:
337363
return is_host() ||
338-
(get_device_info_impl<
339-
pi_usm_capabilities,
340-
info::device::usm_shared_allocations>::get(MDevice,
341-
getPlugin()) &
364+
(get_device_info_impl<pi_usm_capabilities,
365+
info::device::usm_shared_allocations>::
366+
get(MPlatform->getDeviceImpl(MDevice)) &
342367
PI_USM_CONCURRENT_ATOMIC_ACCESS);
343368
case aspect::usm_restricted_shared_allocations:
344369
return get_info<info::device::usm_restricted_shared_allocations>();

sycl/source/detail/device_impl.hpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#pragma once
1010

11-
#include <detail/device_info.hpp>
1211
#include <detail/platform_impl.hpp>
1312
#include <sycl/aspects.hpp>
1413
#include <sycl/detail/cl.h>
@@ -198,12 +197,7 @@ class device_impl {
198197
/// returning the type associated with the param parameter.
199198
///
200199
/// \return device info of type described in Table 4.20.
201-
template <typename Param> typename Param::return_type get_info() const {
202-
if (is_host()) {
203-
return get_device_info_host<Param>();
204-
}
205-
return get_device_info<Param>(this->getHandleRef(), this->getPlugin());
206-
}
200+
template <typename Param> typename Param::return_type get_info() const;
207201

208202
/// Check if affinity partitioning by specified domain is supported by
209203
/// device
@@ -242,6 +236,16 @@ class device_impl {
242236
/// @throw sycl::feature_not_supported if feature is not supported on device
243237
uint64_t getCurrentDeviceTime();
244238

239+
/// Get the backend of this device
240+
backend getBackend() const { return MPlatform->getBackend(); }
241+
242+
/// @brief Get the platform impl serving this device
243+
/// @return PlatformImplPtr
244+
PlatformImplPtr getPlatformImpl() const { return MPlatform; }
245+
246+
/// Get device info string
247+
std::string get_device_info_string(RT::PiDeviceInfo InfoCode) const;
248+
245249
private:
246250
explicit device_impl(pi_native_handle InteropDevice, RT::PiDevice Device,
247251
PlatformImplPtr Platform, const plugin &Plugin);

0 commit comments

Comments
 (0)