Skip to content

Commit 2c7b9fc

Browse files
authored
1 parent 4315ff2 commit 2c7b9fc

7 files changed

Lines changed: 140 additions & 2 deletions

File tree

llvm/include/llvm/SYCLLowerIR/DeviceConfigFile.td

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ def AspectExt_oneapi_async_memory_alloc : Aspect<"ext_oneapi_async_memory_alloc"
9494
def AspectExt_intel_device_info_luid : Aspect<"ext_intel_device_info_luid">;
9595
def AspectExt_intel_device_info_node_mask : Aspect<"ext_intel_device_info_node_mask">;
9696
def Aspectext_oneapi_exportable_device_mem : Aspect<"ext_oneapi_exportable_device_mem">;
97+
def Aspectext_oneapi_clock_sub_group : Aspect<"ext_oneapi_clock_sub_group">;
98+
def Aspectext_oneapi_clock_work_group : Aspect<"ext_oneapi_clock_work_group">;
99+
def Aspectext_oneapi_clock_device : Aspect<"ext_oneapi_clock_device">;
97100

98101
// Deprecated aspects
99102
def AspectInt64_base_atomics : Aspect<"int64_base_atomics">;
@@ -168,7 +171,10 @@ def : TargetInfo<"__TestAspectList",
168171
AspectExt_oneapi_async_memory_alloc,
169172
AspectExt_intel_device_info_luid,
170173
AspectExt_intel_device_info_node_mask,
171-
Aspectext_oneapi_exportable_device_mem],
174+
Aspectext_oneapi_exportable_device_mem,
175+
Aspectext_oneapi_clock_sub_group,
176+
Aspectext_oneapi_clock_work_group,
177+
Aspectext_oneapi_clock_device],
172178
[]>;
173179
// This definition serves the only purpose of testing whether the deprecated aspect list defined in here and in SYCL RT
174180
// match.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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

sycl/include/sycl/info/aspects.def

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,6 @@ __SYCL_ASPECT(ext_oneapi_async_memory_alloc, 87)
8080
__SYCL_ASPECT(ext_intel_device_info_luid, 88)
8181
__SYCL_ASPECT(ext_intel_device_info_node_mask, 89)
8282
__SYCL_ASPECT(ext_oneapi_exportable_device_mem, 90)
83-
83+
__SYCL_ASPECT(ext_oneapi_clock_sub_group, 91)
84+
__SYCL_ASPECT(ext_oneapi_clock_work_group, 92)
85+
__SYCL_ASPECT(ext_oneapi_clock_device, 93)

sycl/include/sycl/sycl.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ can be disabled by setting SYCL_DISABLE_FSYCL_SYCLHPP_WARNING macro.")
113113
#include <sycl/ext/oneapi/experimental/bfloat16_math.hpp>
114114
#include <sycl/ext/oneapi/experimental/builtins.hpp>
115115
#include <sycl/ext/oneapi/experimental/chunk.hpp>
116+
#include <sycl/ext/oneapi/experimental/clock.hpp>
116117
#include <sycl/ext/oneapi/experimental/cluster_group_prop.hpp>
117118
#include <sycl/ext/oneapi/experimental/composite_device.hpp>
118119
#include <sycl/ext/oneapi/experimental/cuda/barrier.hpp>

sycl/source/detail/device_impl.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,6 +1579,18 @@ class device_impl : public std::enable_shared_from_this<device_impl> {
15791579
UR_DEVICE_INFO_MEMORY_EXPORT_EXPORTABLE_DEVICE_MEM_EXP>()
15801580
.value_or(0);
15811581
}
1582+
CASE(ext_oneapi_clock_sub_group) {
1583+
// Will be updated in a follow-up UR patch.
1584+
return false;
1585+
}
1586+
CASE(ext_oneapi_clock_work_group) {
1587+
// Will be updated in a follow-up UR patch.
1588+
return false;
1589+
}
1590+
CASE(ext_oneapi_clock_device) {
1591+
// Will be updated in a follow-up UR patch.
1592+
return false;
1593+
}
15821594
else {
15831595
return false; // This device aspect has not been implemented yet.
15841596
}

sycl/source/feature_test.hpp.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ inline namespace _V1 {
120120
#define SYCL_KHR_FREE_FUNCTION_COMMANDS 1
121121
#define SYCL_KHR_QUEUE_EMPTY_QUERY 1
122122
#define SYCL_EXT_ONEAPI_MEMORY_EXPORT 1
123+
#define SYCL_EXT_ONEAPI_CLOCK 1
123124
// In progress yet
124125
#define SYCL_EXT_ONEAPI_ATOMIC16 0
125126
#define SYCL_KHR_DEFAULT_CONTEXT 1
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// REQUIRES: aspect-usm_shared_allocations
2+
// REQUIRES: aspect-ext_oneapi_clock_sub_group || aspect-ext_oneapi_clock_work_group || aspect-ext_oneapi_clock_device
3+
// RUN: %{build} -o %t.out
4+
// RUN: %{run} %t.out
5+
6+
#include <sycl/detail/core.hpp>
7+
#include <sycl/ext/oneapi/experimental/clock.hpp>
8+
#include <sycl/usm.hpp>
9+
10+
namespace syclex = sycl::ext::oneapi::experimental;
11+
12+
template <syclex::clock_scope scope, sycl::aspect aspect> void test() {
13+
sycl::queue q;
14+
if (!q.get_device().has(aspect))
15+
return;
16+
17+
uint64_t *data = sycl::malloc_shared<uint64_t>(2, q);
18+
19+
q.parallel_for(2, [=](sycl::id<1> idx) {
20+
if (idx == 0) {
21+
data[0] = syclex::clock<scope>();
22+
int count = 0;
23+
for (int i = 0; i < 1e6; ++i)
24+
count++;
25+
data[1] = syclex::clock<scope>();
26+
}
27+
});
28+
q.wait();
29+
30+
assert(data[1] > data[0]);
31+
sycl::free(data, q);
32+
}
33+
34+
int main() {
35+
test<syclex::clock_scope::sub_group,
36+
sycl::aspect::ext_oneapi_clock_sub_group>();
37+
test<syclex::clock_scope::work_group,
38+
sycl::aspect::ext_oneapi_clock_work_group>();
39+
test<syclex::clock_scope::device, sycl::aspect::ext_oneapi_clock_device>();
40+
41+
return 0;
42+
}

0 commit comments

Comments
 (0)