Repository navigation
[libsycl] Add sycl::context stub #182826
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
KseniyaTikhomirova
merged 6 commits into
llvm:main
from
KseniyaTikhomirova:usm_part1_context
Feb 27, 2026
Merged
[libsycl] Add sycl::context stub #182826
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
211571b
[libsycl] add sycl::context stub
KseniyaTikhomirova 1da0d4d
Merge branch 'main' into usm_part1_context
KseniyaTikhomirova 76834e4
fix comments
KseniyaTikhomirova be094d3
fix comments
KseniyaTikhomirova 8e46a4f
fix last comments
KseniyaTikhomirova 991aecd
Merge branch 'main' into usm_part1_context
KseniyaTikhomirova File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| /// | ||
| /// \file | ||
| /// This file contains the declaration of the SYCL context class, which | ||
| /// represents the runtime data structures and state required by a SYCL backend | ||
| /// API to interact with a group of devices associated with a platform. | ||
| /// | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef _LIBSYCL___IMPL_CONTEXT_HPP | ||
| #define _LIBSYCL___IMPL_CONTEXT_HPP | ||
|
|
||
| #include <sycl/__impl/backend.hpp> | ||
| #include <sycl/__impl/info/desc_base.hpp> | ||
|
|
||
| #include <sycl/__impl/detail/config.hpp> | ||
| #include <sycl/__impl/detail/obj_utils.hpp> | ||
|
|
||
| #include <memory> | ||
| #include <vector> | ||
|
|
||
| _LIBSYCL_BEGIN_NAMESPACE_SYCL | ||
|
|
||
| class context; | ||
| class device; | ||
| class platform; | ||
|
|
||
| namespace detail { | ||
| class ContextImpl; | ||
| template <typename T> | ||
| using is_context_info_desc_t = typename is_info_desc<T, context>::return_type; | ||
| } // namespace detail | ||
|
|
||
| // SYCL 2020 4.6.3. Context class | ||
| class _LIBSYCL_EXPORT context { | ||
| public: | ||
| context(const context &rhs) = default; | ||
|
|
||
| context(context &&rhs) = default; | ||
|
|
||
| context &operator=(const context &rhs) = default; | ||
|
|
||
| context &operator=(context &&rhs) = default; | ||
|
|
||
| friend bool operator==(const context &lhs, const context &rhs) { | ||
| return lhs.impl == rhs.impl; | ||
| } | ||
|
|
||
| friend bool operator!=(const context &lhs, const context &rhs) { | ||
| return !(lhs == rhs); | ||
| } | ||
|
|
||
| /// \return the backend associated with this context. | ||
| backend get_backend() const noexcept; | ||
|
|
||
| /// \return the platform associated with this SYCL context. | ||
| platform get_platform() const; | ||
|
|
||
| /// \return a vector of valid SYCL devices associated with this SYCL context. | ||
| std::vector<device> get_devices() const; | ||
|
|
||
| /// Queries this SYCL context for information. | ||
| /// | ||
| /// The return type depends on information being queried. | ||
| template <typename Param> | ||
| detail::is_context_info_desc_t<Param> get_info() const; | ||
|
|
||
| /// Queries this SYCL context for SYCL backend-specific information. | ||
| /// | ||
| /// The return type depends on information being queried. | ||
| template <typename Param> | ||
| typename Param::return_type get_backend_info() const; | ||
|
|
||
| private: | ||
| context(const std::shared_ptr<detail::ContextImpl> &Impl) : impl(Impl) {} | ||
| std::shared_ptr<detail::ContextImpl> impl; | ||
|
|
||
| friend sycl::detail::ImplUtils; | ||
| }; // class context | ||
|
|
||
| _LIBSYCL_END_NAMESPACE_SYCL | ||
|
|
||
| template <> | ||
| struct std::hash<sycl::context> : public sycl::detail::HashBase<sycl::context> { | ||
| }; | ||
|
|
||
| #endif // _LIBSYCL___IMPL_CONTEXT_HPP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include <sycl/__impl/device.hpp> | ||
|
|
||
| #include <detail/context_impl.hpp> | ||
| #include <detail/platform_impl.hpp> | ||
|
|
||
| #include <cassert> | ||
|
|
||
| _LIBSYCL_BEGIN_NAMESPACE_SYCL | ||
|
|
||
| backend context::get_backend() const noexcept { return impl->getBackend(); } | ||
|
|
||
| platform context::get_platform() const { | ||
| return detail::createSyclObjFromImpl<platform>(impl->getPlatformImpl()); | ||
| } | ||
|
|
||
| std::vector<device> context::get_devices() const { | ||
| std::vector<device> Devices; | ||
|
|
||
| impl->iterateDevices([&Devices](detail::DeviceImpl *DevImpl) { | ||
| assert(DevImpl && "Device impl can't be nullptr"); | ||
| Devices.push_back(detail::createSyclObjFromImpl<device>(*DevImpl)); | ||
| }); | ||
|
|
||
| return Devices; | ||
| } | ||
|
|
||
| _LIBSYCL_END_NAMESPACE_SYCL | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include <detail/context_impl.hpp> | ||
| #include <detail/platform_impl.hpp> | ||
|
|
||
| _LIBSYCL_BEGIN_NAMESPACE_SYCL | ||
|
|
||
| namespace detail { | ||
|
|
||
| void ContextImpl::iterateDevices( | ||
| const std::function<void(DeviceImpl *)> &callback) const { | ||
| // TODO: Intentionally don't store devices in context now. This class should | ||
| // be reimplemented once liboffload adds context support. Treat context as | ||
| // default context that is associated with all devices in the platform. | ||
| return MPlatform.iterateDevices(info::device_type::all, callback); | ||
| } | ||
|
|
||
| backend ContextImpl::getBackend() const { return MPlatform.getBackend(); } | ||
|
|
||
| } // namespace detail | ||
| _LIBSYCL_END_NAMESPACE_SYCL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef _LIBSYCL_CONTEXT_IMPL | ||
| #define _LIBSYCL_CONTEXT_IMPL | ||
|
|
||
| #include <sycl/__impl/context.hpp> | ||
| #include <sycl/__impl/detail/config.hpp> | ||
|
|
||
| #include <OffloadAPI.h> | ||
|
|
||
| #include <functional> | ||
|
|
||
| _LIBSYCL_BEGIN_NAMESPACE_SYCL | ||
| namespace detail { | ||
|
|
||
| class PlatformImpl; | ||
| class DeviceImpl; | ||
|
|
||
| /// Context dummy (w/o liboffload handle) that represents all devices | ||
| /// in platform. | ||
|
bader marked this conversation as resolved.
Outdated
|
||
| /// | ||
| /// Presence of context object is essential for many APIs. This dummy is a way | ||
| /// to support them in case of absence of context support in liboffload. For | ||
| /// backends where context exists and participates in operations liboffload | ||
| /// plugins create and use default context that represents all devices in that | ||
| /// platform. Duplicating this logic here. | ||
| class ContextImpl : public std::enable_shared_from_this<ContextImpl> { | ||
| struct Private { | ||
| explicit Private() = default; | ||
| }; | ||
|
|
||
| public: | ||
| /// Constructs a ContextImpl using a platform. | ||
| /// | ||
| /// Newly created instance represents all devices in platform. | ||
| /// | ||
| /// \param Platform is a platform to associate this context with. | ||
| ContextImpl(PlatformImpl &Platform, Private) : MPlatform(Platform) {} | ||
|
|
||
| /// Constructs a ContextImpl with a provided arguments. Variadic helper. | ||
| /// Restrics ways of ContextImpl creation. | ||
| template <typename... Ts> | ||
| static std::shared_ptr<ContextImpl> create(Ts &&...args) { | ||
| return std::make_shared<ContextImpl>(std::forward<Ts>(args)..., Private{}); | ||
| } | ||
|
|
||
| /// \return the platform this context is associated with. | ||
| PlatformImpl &getPlatformImpl() const { return MPlatform; } | ||
|
|
||
| /// Calls "callback" with every device associated | ||
| /// with this context. | ||
| void iterateDevices(const std::function<void(DeviceImpl *)> &callback) const; | ||
|
|
||
| /// \return backend of the platform this context is associated with. | ||
| backend getBackend() const; | ||
|
|
||
| private: | ||
| PlatformImpl &MPlatform; | ||
| }; | ||
|
|
||
| } // namespace detail | ||
|
|
||
| _LIBSYCL_END_NAMESPACE_SYCL | ||
|
|
||
| #endif // _LIBSYCL_CONTEXT_IMPL | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.