Skip to content

Commit e4e6fd2

Browse files
fix comments
Signed-off-by: Tikhomirova, Kseniya <[email protected]>
1 parent 87442d1 commit e4e6fd2

File tree

12 files changed

+122
-133
lines changed

12 files changed

+122
-133
lines changed

libsycl/include/sycl/__impl/detail/obj_base.hpp

Lines changed: 0 additions & 90 deletions
This file was deleted.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
///
9+
/// \file
10+
/// This file contains helper functions for tranformation between implementation
11+
/// and SYCL's interface objects.
12+
///
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef _LIBSYCL___IMPL_DETAIL_OBJ_UTILS_HPP
16+
#define _LIBSYCL___IMPL_DETAIL_OBJ_UTILS_HPP
17+
18+
#include <sycl/__impl/detail/config.hpp>
19+
20+
#include <cassert>
21+
#include <memory>
22+
#include <optional>
23+
#include <type_traits>
24+
#include <utility>
25+
26+
_LIBSYCL_BEGIN_NAMESPACE_SYCL
27+
28+
namespace detail {
29+
30+
// Note! This class relies on the fact that all SYCL interface
31+
// classes contain "impl" field that points to implementation object. "impl"
32+
// field should be accessible from this class.
33+
struct ImplUtils {
34+
// Helper function for extracting implementation from SYCL's interface
35+
// objects.
36+
template <class Obj>
37+
static const decltype(Obj::impl) &getSyclObjImpl(const Obj &SyclObj) {
38+
assert(SyclObj.impl && "every constructor should create an impl");
39+
return SyclObj.impl;
40+
}
41+
42+
// Helper function for creation SYCL interface objects from implementations.
43+
template <typename SyclObject, typename From>
44+
static SyclObject createSyclObjFromImpl(From &&from) {
45+
if constexpr (std::is_same_v<decltype(SyclObject::impl),
46+
std::shared_ptr<std::decay_t<From>>>)
47+
return SyclObject{from.shared_from_this()};
48+
else
49+
return SyclObject{std::forward<From>(from)};
50+
}
51+
};
52+
53+
template <class Obj>
54+
auto getSyclObjImpl(const Obj &SyclObj)
55+
-> decltype(ImplUtils::getSyclObjImpl(SyclObj)) {
56+
return ImplUtils::getSyclObjImpl(SyclObj);
57+
}
58+
59+
template <typename SyclObject, typename From>
60+
SyclObject createSyclObjFromImpl(From &&from) {
61+
return ImplUtils::createSyclObjFromImpl<SyclObject>(std::forward<From>(from));
62+
}
63+
64+
// std::hash support (4.5.2. Common reference semantics)
65+
template <typename T> struct HashBase {
66+
size_t operator()(const T &Obj) const {
67+
#ifdef __SYCL_DEVICE_ONLY__
68+
(void)Obj;
69+
return 0;
70+
#else
71+
auto &Impl = sycl::detail::getSyclObjImpl(Obj);
72+
return std::hash<std::decay_t<decltype(Impl)>>{}(Impl);
73+
#endif
74+
}
75+
};
76+
77+
} // namespace detail
78+
79+
_LIBSYCL_END_NAMESPACE_SYCL
80+
81+
#endif // _LIBSYCL___IMPL_DETAIL_OBJ_UTILS_HPP

libsycl/include/sycl/__impl/platform.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
#include <sycl/__impl/backend.hpp>
1919
#include <sycl/__impl/detail/config.hpp>
20-
#include <sycl/__impl/detail/obj_base.hpp>
20+
#include <sycl/__impl/detail/obj_utils.hpp>
2121
#include <sycl/__impl/info/platform.hpp>
2222

2323
#include <memory>
@@ -30,8 +30,7 @@ class platform_impl;
3030
} // namespace detail
3131

3232
// 4.6.2. Platform class
33-
class _LIBSYCL_EXPORT platform
34-
: public detail::ObjBase<detail::platform_impl *, platform> {
33+
class _LIBSYCL_EXPORT platform {
3534
public:
3635
/// Constructs a platform object that is a copy of the platform which contains
3736
/// the device returned by default_selector_v.
@@ -108,9 +107,10 @@ class _LIBSYCL_EXPORT platform
108107
static std::vector<platform> get_platforms();
109108

110109
private:
111-
platform(detail::platform_impl *Impl) : ObjBase(Impl) {}
110+
platform(detail::platform_impl &Impl) : impl(&Impl) {}
111+
detail::platform_impl *impl;
112112

113-
friend detail::ObjBase<detail::platform_impl *, platform>;
113+
friend sycl::detail::ImplUtils;
114114
}; // class platform
115115

116116
_LIBSYCL_END_NAMESPACE_SYCL

libsycl/src/detail/global_objects.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ std::vector<detail::OffloadTopology> &getOffloadTopologies() {
2424
return Topologies;
2525
}
2626

27-
std::vector<platform_impl> &getPlatformCache() {
28-
static std::vector<platform_impl> PlatformCache{};
27+
std::vector<PlatformImplUPtr> &getPlatformCache() {
28+
static std::vector<PlatformImplUPtr> PlatformCache{};
2929
return PlatformCache;
3030
}
3131

libsycl/src/detail/global_objects.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class platform_impl;
2424
// Offload topologies (one per backend) discovered from liboffload.
2525
std::vector<detail::OffloadTopology> &getOffloadTopologies();
2626

27-
std::vector<platform_impl> &getPlatformCache();
27+
std::vector<std::unique_ptr<platform_impl>> &getPlatformCache();
2828

2929
} // namespace detail
3030
_LIBSYCL_END_NAMESPACE_SYCL

libsycl/src/detail/offload/offload_utils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
_LIBSYCL_BEGIN_NAMESPACE_SYCL
1212
namespace detail {
1313

14-
const char *stringifyErrorCode(int32_t error) {
14+
const char *stringifyErrorCode(ol_errc_t error) {
1515
switch (error) {
1616
#define _OFFLOAD_ERRC(NAME) \
1717
case NAME: \

libsycl/src/detail/offload/offload_utils.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ _LIBSYCL_BEGIN_NAMESPACE_SYCL
1919

2020
namespace detail {
2121

22-
const char *stringifyErrorCode(int32_t error);
22+
const char *stringifyErrorCode(ol_errc_t error);
2323

24-
inline std::string formatCodeString(int32_t code) {
25-
return std::to_string(code) + " (" + std::string(stringifyErrorCode(code)) +
26-
")";
24+
inline std::string formatCodeString(ol_result_t Result) {
25+
return std::to_string(Result->Code) + " (" +
26+
std::string(stringifyErrorCode(Result->Code)) + ")" + Result->Details;
2727
}
2828

2929
template <sycl::errc errc = sycl::errc::runtime>
3030
void checkAndThrow(ol_result_t Result) {
3131
if (Result != OL_SUCCESS) {
3232
throw sycl::exception(sycl::make_error_code(errc),
33-
detail::formatCodeString(Result->Code));
33+
detail::formatCodeString(Result));
3434
}
3535
}
3636

libsycl/src/detail/platform_impl.cpp

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

99
#include <sycl/__impl/detail/config.hpp>
10-
#include <sycl/__impl/detail/obj_base.hpp>
10+
#include <sycl/__impl/detail/obj_utils.hpp>
1111

1212
#include <detail/global_objects.hpp>
1313
#include <detail/platform_impl.hpp>
@@ -19,8 +19,8 @@ namespace detail {
1919
platform_impl &platform_impl::getPlatformImpl(ol_platform_handle_t Platform) {
2020
auto &PlatformCache = getPlatformCache();
2121
for (auto &PlatImpl : PlatformCache) {
22-
if (PlatImpl.getHandleRef() == Platform)
23-
return PlatImpl;
22+
if (PlatImpl->getHandleRef() == Platform)
23+
return *PlatImpl;
2424
}
2525

2626
throw sycl::exception(
@@ -29,25 +29,24 @@ platform_impl &platform_impl::getPlatformImpl(ol_platform_handle_t Platform) {
2929
"the list of platforms discovered by liboffload");
3030
}
3131

32-
range_view<platform_impl> platform_impl::getPlatforms() {
32+
const std::vector<PlatformImplUPtr> &platform_impl::getPlatforms() {
3333
[[maybe_unused]] static auto InitPlatformsOnce = []() {
3434
discoverOffloadDevices();
3535
auto &PlatformCache = getPlatformCache();
3636
for (const auto &Topo : getOffloadTopologies()) {
3737
size_t PlatformIndex = 0;
3838
for (const auto &OffloadPlatform : Topo.platforms()) {
39-
PlatformCache.emplace_back(
40-
platform_impl(OffloadPlatform, PlatformIndex++));
39+
PlatformCache.emplace_back(std::make_unique<platform_impl>(
40+
OffloadPlatform, PlatformIndex++, private_tag{}));
4141
}
4242
}
4343
return true;
4444
}();
45-
auto &PlatformCache = getPlatformCache();
46-
return {PlatformCache.data(), PlatformCache.size()};
45+
return getPlatformCache();
4746
}
4847

4948
platform_impl::platform_impl(ol_platform_handle_t Platform,
50-
size_t PlatformIndex)
49+
size_t PlatformIndex, private_tag)
5150
: MOffloadPlatform(Platform), MOffloadPlatformIndex(PlatformIndex) {
5251
ol_platform_backend_t Backend = OL_PLATFORM_BACKEND_UNKNOWN;
5352
call_and_throw(olGetPlatformInfo, MOffloadPlatform, OL_PLATFORM_INFO_BACKEND,

libsycl/src/detail/platform_impl.hpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,31 @@ _LIBSYCL_BEGIN_NAMESPACE_SYCL
2727

2828
namespace detail {
2929

30+
using PlatformImplUPtr = std::unique_ptr<platform_impl>;
31+
3032
class platform_impl {
33+
struct private_tag {
34+
explicit private_tag() = default;
35+
};
36+
3137
public:
3238
/// Constructs platform_impl from a platform handle.
3339
///
3440
/// \param Platform is a raw offload library handle representing platform.
3541
/// \param PlatformIndex is a platform index in a backend (needed for a proper
3642
/// indexing in device selector).
37-
//
38-
// Platforms can only be created under `GlobalHandler`'s ownership via
39-
// `platform_impl::getOrMakePlatformImpl` method.
40-
explicit platform_impl(ol_platform_handle_t Platform, size_t PlatformIndex);
43+
/// All platform impls are created during first getPlatforms() call.
44+
explicit platform_impl(ol_platform_handle_t Platform, size_t PlatformIndex,
45+
private_tag);
4146

4247
~platform_impl() = default;
4348

4449
/// Returns the backend associated with this platform.
4550
backend getBackend() const noexcept { return MBackend; }
4651

47-
/// Returns range-view to all SYCL platforms from all backends that are
52+
/// Returns all SYCL platforms from all backends that are
4853
/// available in the system.
49-
static range_view<platform_impl> getPlatforms();
54+
static const std::vector<PlatformImplUPtr> &getPlatforms();
5055

5156
/// Returns raw underlying offload platform handle.
5257
///

libsycl/src/exception.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ const std::error_category &exception::category() const noexcept {
5050

5151
const char *exception::what() const noexcept { return MMessage->c_str(); }
5252

53-
bool exception::has_context() const noexcept { /*return (MContext != nullptr);*/
54-
return false;
55-
}
53+
bool exception::has_context() const noexcept { return false; }
5654

5755
_LIBSYCL_END_NAMESPACE_SYCL

0 commit comments

Comments
 (0)