Skip to content

Commit 1261e05

Browse files
authored
[SYCL][ESIMD] Implement rdtsc() - ReaD TimeStamp Counter API (#12315)
1 parent b13faa4 commit 1261e05

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

llvm/lib/SYCLLowerIR/ESIMD/LowerESIMD.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,8 @@ class ESIMDIntrinDescTable {
661661
{"addc", {"addc", {l(0)}}},
662662
{"subb", {"subb", {l(0)}}},
663663
{"bfn", {"bfn", {a(0), a(1), a(2), t(0)}}},
664-
{"srnd", {"srnd", {a(0), a(1)}}}};
664+
{"srnd", {"srnd", {a(0), a(1)}}},
665+
{"timestamp",{"timestamp",{}}}};
665666
}
666667
// clang-format on
667668

sycl/include/sycl/ext/intel/experimental/esimd/detail/math_intrin.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ __ESIMD_INTRIN __ESIMD_raw_vec_t(sycl::half, N)
152152
__ESIMD_DNS::vector_type_t<uint16_t, N> src2)
153153
__ESIMD_INTRIN_END;
154154

155+
__ESIMD_INTRIN __ESIMD_raw_vec_t(uint32_t, 4)
156+
__esimd_timestamp() __ESIMD_INTRIN_END;
157+
155158
#undef __ESIMD_raw_vec_t
156159
#undef __ESIMD_cpp_vec_t
157160

sycl/include/sycl/ext/intel/experimental/esimd/math.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,13 @@ __ESIMD_API std::enable_if_t<__ESIMD_DNS::is_esimd_scalar<T>::value &&
17231723
return __ESIMD_NS::bfn<FuncControl>(src0, src1, src2);
17241724
}
17251725

1726+
/// rdtsc - get the value of timestamp counter.
1727+
/// \return the current value of timestamp counter
1728+
ESIMD_INLINE uint64_t rdtsc() {
1729+
__ESIMD_NS::simd<uint32_t, 4> retv = __esimd_timestamp();
1730+
return retv.template bit_cast_view<uint64_t>()[0];
1731+
}
1732+
17261733
/// @} sycl_esimd_logical
17271734

17281735
} // namespace ext::intel::experimental::esimd

sycl/test-e2e/ESIMD/rdtsc.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// RUN: %{build} -o %t.out
2+
// RUN: %{run} %t.out
3+
//==- rdtsc.cpp - Test to verify rdtsc0 and sr0 functionlity----------------==//
4+
//
5+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6+
// See https://llvm.org/LICENSE.txt for license information.
7+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
//
9+
//===----------------------------------------------------------------------===//
10+
11+
// This is basic test to validate rdtsc function.
12+
13+
#include <cmath>
14+
#include <iostream>
15+
#include <sycl/ext/intel/esimd.hpp>
16+
#include <sycl/ext/intel/esimd/simd.hpp>
17+
#include <sycl/sycl.hpp>
18+
#include <vector>
19+
20+
int ErrCnt = 0;
21+
template <typename DataT>
22+
using shared_allocator = sycl::usm_allocator<DataT, sycl::usm::alloc::shared>;
23+
template <typename DataT>
24+
using shared_vector = std::vector<DataT, shared_allocator<DataT>>;
25+
26+
int test_rdtsc() {
27+
sycl::queue Queue;
28+
shared_allocator<uint64_t> Allocator(Queue);
29+
constexpr int32_t SIZE = 32;
30+
31+
shared_vector<uint64_t> VectorOutputRDTSC(SIZE, 0, Allocator);
32+
33+
auto GlobalRange = sycl::range<1>(SIZE);
34+
sycl::range<1> LocalRange{1};
35+
sycl::nd_range<1> Range(GlobalRange, LocalRange);
36+
37+
{
38+
Queue.submit([&](sycl::handler &cgh) {
39+
uint64_t *VectorOutputRDTSCPtr = VectorOutputRDTSC.data();
40+
41+
auto Kernel = ([=](sycl::nd_item<1> ndi) [[intel::sycl_explicit_simd]] {
42+
using namespace sycl::ext::intel::esimd;
43+
auto Idx = ndi.get_global_id(0);
44+
uint64_t StartCounter = sycl::ext::intel::experimental::esimd::rdtsc();
45+
simd<uint64_t, 1> VectorResultRDTSC(VectorOutputRDTSCPtr + Idx);
46+
uint64_t EndCounter = sycl::ext::intel::experimental::esimd::rdtsc();
47+
VectorResultRDTSC += EndCounter > StartCounter;
48+
49+
VectorResultRDTSC.copy_to(VectorOutputRDTSCPtr + Idx);
50+
});
51+
52+
cgh.parallel_for(Range, Kernel);
53+
});
54+
Queue.wait();
55+
}
56+
57+
return std::any_of(VectorOutputRDTSC.begin(), VectorOutputRDTSC.end(),
58+
[](uint64_t v) { return v == 0; });
59+
}
60+
61+
int main() {
62+
63+
int TestResult = test_rdtsc();
64+
65+
if (!TestResult) {
66+
std::cout << "Pass" << std::endl;
67+
}
68+
return TestResult;
69+
}

0 commit comments

Comments
 (0)