|
| 1 | +//==-------- clock.hpp --- SYCL extension for clock() free function --------==// |
| 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 | +#pragma once |
| 10 | + |
| 11 | +#include <sycl/__spirv/spirv_ops.hpp> |
| 12 | +#include <sycl/aspects.hpp> |
| 13 | +#include <sycl/exception.hpp> |
| 14 | + |
| 15 | +namespace sycl { |
| 16 | +inline namespace _V1 { |
| 17 | +namespace ext::oneapi::experimental { |
| 18 | + |
| 19 | +enum class clock_scope : int { |
| 20 | + // Aligned with SPIR-V Scope<id> values. |
| 21 | + device = 1, |
| 22 | + work_group = 2, |
| 23 | + sub_group = 3 |
| 24 | +}; |
| 25 | + |
| 26 | +namespace detail { |
| 27 | +template <clock_scope Scope> inline uint64_t clock_impl() { |
| 28 | +#ifdef __SYCL_DEVICE_ONLY__ |
| 29 | +#if defined(__NVPTX__) || defined(__AMDGCN__) |
| 30 | + // Currently clock() is not supported on NVPTX and AMDGCN. |
| 31 | + return 0; |
| 32 | +#else |
| 33 | + return __spirv_ReadClockKHR(static_cast<int>(Scope)); |
| 34 | +#endif // defined(__NVPTX__) || defined(__AMDGCN__) |
| 35 | +#else |
| 36 | + throw sycl::exception( |
| 37 | + make_error_code(errc::runtime), |
| 38 | + "sycl::ext::oneapi::experimental::clock() is not supported on host."); |
| 39 | +#endif // __SYCL_DEVICE_ONLY__ |
| 40 | +} |
| 41 | +} // namespace detail |
| 42 | + |
| 43 | +template <clock_scope Scope> inline uint64_t clock(); |
| 44 | + |
| 45 | +// Specialization for device. |
| 46 | +template <> |
| 47 | +#ifdef __SYCL_DEVICE_ONLY__ |
| 48 | +[[__sycl_detail__::__uses_aspects__(sycl::aspect::ext_oneapi_clock_device)]] |
| 49 | +#endif |
| 50 | +inline uint64_t clock<clock_scope::device>() { |
| 51 | + return detail::clock_impl<clock_scope::device>(); |
| 52 | +} |
| 53 | + |
| 54 | +// Specialization for work-group. |
| 55 | +template <> |
| 56 | +#ifdef __SYCL_DEVICE_ONLY__ |
| 57 | +[[__sycl_detail__::__uses_aspects__(sycl::aspect::ext_oneapi_clock_work_group)]] |
| 58 | +#endif |
| 59 | +inline uint64_t clock<clock_scope::work_group>() { |
| 60 | + return detail::clock_impl<clock_scope::work_group>(); |
| 61 | +} |
| 62 | + |
| 63 | +// Specialization for sub-group. |
| 64 | +template <> |
| 65 | +#ifdef __SYCL_DEVICE_ONLY__ |
| 66 | +[[__sycl_detail__::__uses_aspects__(sycl::aspect::ext_oneapi_clock_sub_group)]] |
| 67 | +#endif |
| 68 | +inline uint64_t clock<clock_scope::sub_group>() { |
| 69 | + return detail::clock_impl<clock_scope::sub_group>(); |
| 70 | +} |
| 71 | + |
| 72 | +} // namespace ext::oneapi::experimental |
| 73 | +} // namespace _V1 |
| 74 | +} // namespace sycl |
0 commit comments