Skip to content

Commit fa44cc1

Browse files
feature: add wrappers to waitpkg intrinsics
Related-To: NEO-9737 Signed-off-by: Zbigniew Zdanowicz <[email protected]>
1 parent 570b4d3 commit fa44cc1

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

CMakeLists.txt

+9-1
Original file line numberDiff line numberDiff line change
@@ -726,15 +726,23 @@ if(NOT MSVC)
726726
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ftemplate-depth=1024")
727727
endif()
728728

729-
# _mm_clflushopt support
729+
# intrinsics (_mm_clflushopt and waitpkg) support
730730
if(NOT MSVC)
731731
check_cxx_compiler_flag(-mclflushopt SUPPORTS_CLFLUSHOPT)
732+
check_cxx_compiler_flag(-mwaitpkg SUPPORTS_WAITPKG)
732733
if(SUPPORTS_CLFLUSHOPT)
733734
add_compile_definitions(SUPPORTS_CLFLUSHOPT)
734735
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mclflushopt")
735736
endif()
737+
if(SUPPORTS_WAITPKG)
738+
add_compile_definitions(SUPPORTS_WAITPKG)
739+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mwaitpkg")
740+
else()
741+
message(WARNING "-mwaitpkg flag is not supported by the compiler")
742+
endif()
736743
else()
737744
add_compile_definitions(SUPPORTS_CLFLUSHOPT)
745+
add_compile_definitions(SUPPORTS_WAITPKG)
738746
endif()
739747

740748
# Compiler warning flags

shared/source/utilities/cpuintrinsics.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
#include "shared/source/utilities/cpuintrinsics.h"
99

1010
#if defined(_WIN32)
11+
#include <immintrin.h>
1112
#include <intrin.h>
13+
#pragma intrinsic(__rdtsc)
1214
#else
15+
#include <immintrin.h>
1316
#include <x86intrin.h>
1417
#endif
1518

@@ -42,5 +45,23 @@ void pause() {
4245
_mm_pause();
4346
}
4447

48+
unsigned char umwait(unsigned int ctrl, uint64_t counter) {
49+
#ifdef SUPPORTS_WAITPKG
50+
return _umwait(ctrl, counter);
51+
#else
52+
return 0;
53+
#endif
54+
}
55+
56+
void umonitor(void *a) {
57+
#ifdef SUPPORTS_WAITPKG
58+
_umonitor(a);
59+
#endif
60+
}
61+
62+
uint64_t rdtsc() {
63+
return __rdtsc();
64+
}
65+
4566
} // namespace CpuIntrinsics
4667
} // namespace NEO

shared/source/utilities/cpuintrinsics.h

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#pragma once
99

10+
#include <cstdint>
11+
1012
namespace NEO {
1113
namespace CpuIntrinsics {
1214

@@ -18,5 +20,11 @@ void clFlushOpt(void *ptr);
1820

1921
void pause();
2022

23+
unsigned char umwait(unsigned int ctrl, uint64_t counter);
24+
25+
void umonitor(void *a);
26+
27+
uint64_t rdtsc();
28+
2129
} // namespace CpuIntrinsics
2230
} // namespace NEO

shared/test/common/utilities/cpuintrinsics.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,23 @@ std::atomic<uint32_t> clFlushCounter(0u);
2121
std::atomic<uint32_t> pauseCounter(0u);
2222
std::atomic<uint32_t> sfenceCounter(0u);
2323

24+
std::atomic<uint64_t> lastUmwaitCounter(0u);
25+
std::atomic<unsigned int> lastUmwaitControl(0u);
26+
std::atomic<uint32_t> umwaitCounter(0u);
27+
28+
std::atomic<uintptr_t> lastUmonitorPtr(0u);
29+
std::atomic<uint32_t> umonitorCounter(0u);
30+
31+
std::atomic<uint32_t> rdtscCounter(0u);
32+
2433
volatile TagAddressType *pauseAddress = nullptr;
2534
TaskCountType pauseValue = 0u;
2635
uint32_t pauseOffset = 0u;
36+
uint64_t rdtscRetValue = 0;
37+
unsigned char umwaitRetValue = 0;
2738

2839
std::function<void()> setupPauseAddress;
40+
std::function<unsigned char()> controlUmwait;
2941
} // namespace CpuIntrinsicsTests
3042

3143
namespace NEO {
@@ -56,5 +68,26 @@ void pause() {
5668
}
5769
}
5870

71+
unsigned char umwait(unsigned int ctrl, uint64_t counter) {
72+
CpuIntrinsicsTests::lastUmwaitControl = ctrl;
73+
CpuIntrinsicsTests::lastUmwaitCounter = counter;
74+
CpuIntrinsicsTests::umwaitCounter++;
75+
if (CpuIntrinsicsTests::controlUmwait) {
76+
return CpuIntrinsicsTests::controlUmwait();
77+
} else {
78+
return CpuIntrinsicsTests::umwaitRetValue;
79+
}
80+
}
81+
82+
void umonitor(void *a) {
83+
CpuIntrinsicsTests::lastUmonitorPtr = reinterpret_cast<uintptr_t>(a);
84+
CpuIntrinsicsTests::umonitorCounter++;
85+
}
86+
87+
uint64_t rdtsc() {
88+
CpuIntrinsicsTests::rdtscCounter++;
89+
return CpuIntrinsicsTests::rdtscRetValue;
90+
}
91+
5992
} // namespace CpuIntrinsics
6093
} // namespace NEO

0 commit comments

Comments
 (0)