Skip to content

Commit 8d75f1c

Browse files
[SYCL] Make backend a property of a platform (#9153)
Signed-off-by: Sergey V Maslov <[email protected]>
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);

0 commit comments

Comments
 (0)