Skip to content

Commit 2af0599

Browse files
authored
[SYCL][XPTI] Pass plugin information to subscribers (#4121)
This patch makes the following additional information available to XPTI subscribers. All streams: - Actual major and minor versions of SYCL runtime (instead of dummy values) as well as their string variant. `sycl.pi.debug` stream: - Backend type, which is defined as a `uint8_t` value of `sycl::backend` enum. - Pointer to PI plugin to provide some degree of application flow variance (e.g. query additional info about device, USM pointers, memory, etc).
1 parent 904967e commit 2af0599

File tree

6 files changed

+55
-31
lines changed

6 files changed

+55
-31
lines changed

sycl/include/CL/sycl/detail/pi.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,10 @@ void emitFunctionEndTrace(uint64_t CorrelationID, const char *FName);
189189
/// \param FuncID is the API hash ID from PiApiID type trait.
190190
/// \param FName The name of the PI API call.
191191
/// \param ArgsData is a pointer to packed function call arguments.
192+
/// \param Plugin is the plugin, which is used to make call.
192193
uint64_t emitFunctionWithArgsBeginTrace(uint32_t FuncID, const char *FName,
193-
unsigned char *ArgsData);
194+
unsigned char *ArgsData,
195+
pi_plugin Plugin);
194196

195197
/// Notifies XPTI subscribers about PI function call result.
196198
///
@@ -200,9 +202,10 @@ uint64_t emitFunctionWithArgsBeginTrace(uint32_t FuncID, const char *FName,
200202
/// \param FName The name of the PI API call.
201203
/// \param ArgsData is a pointer to packed function call arguments.
202204
/// \param Result is function call result value.
205+
/// \param Plugin is the plugin, which is used to make call.
203206
void emitFunctionWithArgsEndTrace(uint64_t CorrelationID, uint32_t FuncID,
204207
const char *FName, unsigned char *ArgsData,
205-
pi_result Result);
208+
pi_result Result, pi_plugin Plugin);
206209

207210
// A wrapper for passing around byte array properties
208211
class ByteArray {

sycl/source/detail/pi.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <CL/sycl/detail/device_filter.hpp>
1818
#include <CL/sycl/detail/pi.hpp>
1919
#include <CL/sycl/detail/stl_type_traits.hpp>
20+
#include <CL/sycl/version.hpp>
2021
#include <detail/config.hpp>
2122
#include <detail/global_handler.hpp>
2223
#include <detail/plugin.hpp>
@@ -36,6 +37,10 @@
3637
#include "xpti_trace_framework.h"
3738
#endif
3839

40+
#define STR(x) #x
41+
#define SYCL_VERSION_STR \
42+
"sycl " STR(__LIBSYCL_MAJOR_VERSION) "." STR(__LIBSYCL_MINOR_VERSION)
43+
3944
__SYCL_INLINE_NAMESPACE(cl) {
4045
namespace sycl {
4146
namespace detail {
@@ -50,9 +55,9 @@ xpti_td *GPICallEvent = nullptr;
5055
xpti_td *GPIArgCallEvent = nullptr;
5156
/// Constants being used as placeholder until one is able to reliably get the
5257
/// version of the SYCL runtime
53-
constexpr uint32_t GMajVer = 1;
54-
constexpr uint32_t GMinVer = 0;
55-
constexpr const char *GVerStr = "sycl 1.0";
58+
constexpr uint32_t GMajVer = __LIBSYCL_MAJOR_VERSION;
59+
constexpr uint32_t GMinVer = __LIBSYCL_MINOR_VERSION;
60+
constexpr const char *GVerStr = SYCL_VERSION_STR;
5661
#endif // XPTI_ENABLE_INSTRUMENTATION
5762

5863
template <cl::sycl::backend BE>
@@ -138,15 +143,16 @@ void emitFunctionEndTrace(uint64_t CorrelationID, const char *FName) {
138143
}
139144

140145
uint64_t emitFunctionWithArgsBeginTrace(uint32_t FuncID, const char *FuncName,
141-
unsigned char *ArgsData) {
146+
unsigned char *ArgsData,
147+
pi_plugin Plugin) {
142148
uint64_t CorrelationID = 0;
143149
#ifdef XPTI_ENABLE_INSTRUMENTATION
144150
if (xptiTraceEnabled()) {
145151
uint8_t StreamID = xptiRegisterStream(SYCL_PIDEBUGCALL_STREAM_NAME);
146152
CorrelationID = xptiGetUniqueId();
147153

148154
xpti::function_with_args_t Payload{FuncID, FuncName, ArgsData, nullptr,
149-
nullptr};
155+
&Plugin};
150156

151157
xptiNotifySubscribers(
152158
StreamID, (uint16_t)xpti::trace_point_type_t::function_with_args_begin,
@@ -158,13 +164,13 @@ uint64_t emitFunctionWithArgsBeginTrace(uint32_t FuncID, const char *FuncName,
158164

159165
void emitFunctionWithArgsEndTrace(uint64_t CorrelationID, uint32_t FuncID,
160166
const char *FuncName, unsigned char *ArgsData,
161-
pi_result Result) {
167+
pi_result Result, pi_plugin Plugin) {
162168
#ifdef XPTI_ENABLE_INSTRUMENTATION
163169
if (xptiTraceEnabled()) {
164170
uint8_t StreamID = xptiRegisterStream(SYCL_PIDEBUGCALL_STREAM_NAME);
165171

166172
xpti::function_with_args_t Payload{FuncID, FuncName, ArgsData, &Result,
167-
nullptr};
173+
&Plugin};
168174

169175
xptiNotifySubscribers(
170176
StreamID, (uint16_t)xpti::trace_point_type_t::function_with_args_end,

sycl/source/detail/plugin.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class plugin {
144144
auto ArgsData =
145145
packCallArguments<PiApiOffset>(std::forward<ArgsT>(Args)...);
146146
uint64_t CorrelationIDWithArgs = pi::emitFunctionWithArgsBeginTrace(
147-
static_cast<uint32_t>(PiApiOffset), PIFnName, ArgsData.data());
147+
static_cast<uint32_t>(PiApiOffset), PIFnName, ArgsData.data(), MPlugin);
148148
#endif
149149
RT::PiResult R;
150150
if (pi::trace(pi::TraceLevel::PI_TRACE_CALLS)) {
@@ -165,7 +165,7 @@ class plugin {
165165
pi::emitFunctionEndTrace(CorrelationID, PIFnName);
166166
pi::emitFunctionWithArgsEndTrace(CorrelationIDWithArgs,
167167
static_cast<uint32_t>(PiApiOffset),
168-
PIFnName, ArgsData.data(), R);
168+
PIFnName, ArgsData.data(), R, MPlugin);
169169
#endif
170170
return R;
171171
}

sycl/tools/pi-trace/pi_trace.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,13 @@ XPTI_CALLBACK_API void xptiTraceInit(unsigned int /*major_version*/,
5151
tpCallback);
5252

5353
#define _PI_API(api) \
54-
ArgHandler.set##_##api([](auto &&... Args) { \
55-
std::cout << "---> " << #api << "(" \
56-
<< "\n"; \
57-
sycl::detail::pi::printArgs(Args...); \
58-
std::cout << ") ---> "; \
59-
});
54+
ArgHandler.set##_##api( \
55+
[](const pi_plugin &, std::optional<pi_result>, auto &&... Args) { \
56+
std::cout << "---> " << #api << "(" \
57+
<< "\n"; \
58+
sycl::detail::pi::printArgs(Args...); \
59+
std::cout << ") ---> "; \
60+
});
6061
#include <CL/sycl/detail/pi.def>
6162
#undef _PI_API
6263
}
@@ -77,8 +78,10 @@ XPTI_CALLBACK_API void tpCallback(uint16_t TraceType,
7778

7879
const auto *Data =
7980
static_cast<const xpti::function_with_args_t *>(UserData);
81+
const auto *Plugin = static_cast<pi_plugin *>(Data->user_data);
8082

81-
ArgHandler.handle(Data->function_id, Data->args_data);
83+
ArgHandler.handle(Data->function_id, *Plugin, std::nullopt,
84+
Data->args_data);
8285
std::cout << *static_cast<pi_result *>(Data->ret_data) << "\n";
8386
}
8487
}

sycl/tools/xpti_helpers/pi_arguments_handler.hpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//==---------- pi_arguments_handler.hpp - PI call arguments handler --------==//
2+
// i
23
//
34
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45
// See https://llvm.org/LICENSE.txt for license information.
@@ -12,6 +13,7 @@
1213
#include <CL/sycl/detail/type_traits.hpp>
1314

1415
#include <functional>
16+
#include <optional>
1517
#include <tuple>
1618

1719
__SYCL_INLINE_NAMESPACE(cl) {
@@ -41,7 +43,8 @@ inline TupleT unpack(char *Data,
4143
template <typename T> struct to_function {};
4244

4345
template <typename... Args> struct to_function<std::tuple<Args...>> {
44-
using type = std::function<void(Args...)>;
46+
using type =
47+
std::function<void(const pi_plugin &, std::optional<pi_result>, Args...)>;
4548
};
4649

4750
/// PiArgumentsHandler is a helper class to process incoming XPTI function call
@@ -59,10 +62,11 @@ template <typename... Args> struct to_function<std::tuple<Args...>> {
5962
/// See sycl/tools/pi-trace/ for an example.
6063
class PiArgumentsHandler {
6164
public:
62-
void handle(uint32_t ID, void *ArgsData) {
65+
void handle(uint32_t ID, const pi_plugin &Plugin,
66+
std::optional<pi_result> Result, void *ArgsData) {
6367
#define _PI_API(api) \
6468
if (ID == static_cast<uint32_t>(detail::PiApiKind::api)) { \
65-
MHandler##_##api(ArgsData); \
69+
MHandler##_##api(Plugin, Result, ArgsData); \
6670
return; \
6771
}
6872
#include <CL/sycl/detail/pi.def>
@@ -73,21 +77,27 @@ class PiArgumentsHandler {
7377
void set##_##api( \
7478
const typename to_function<typename detail::function_traits<decltype( \
7579
api)>::args_type>::type &Handler) { \
76-
MHandler##_##api = [Handler](void *Data) { \
80+
MHandler##_##api = [Handler](const pi_plugin &Plugin, \
81+
std::optional<pi_result> Res, void *Data) { \
7782
using TupleT = \
7883
typename detail::function_traits<decltype(api)>::args_type; \
7984
TupleT Tuple = unpack<TupleT>( \
8085
(char *)Data, \
8186
std::make_index_sequence<std::tuple_size<TupleT>::value>{}); \
82-
std::apply(Handler, Tuple); \
87+
const auto Wrapper = [&Plugin, Res, Handler](auto &... Args) { \
88+
Handler(Plugin, Res, Args...); \
89+
}; \
90+
std::apply(Wrapper, Tuple); \
8391
}; \
8492
}
8593
#include <CL/sycl/detail/pi.def>
8694
#undef _PI_API
8795

8896
private:
8997
#define _PI_API(api) \
90-
std::function<void(void *)> MHandler##_##api = [](void *) {};
98+
std::function<void(const pi_plugin &, std::optional<pi_result>, void *)> \
99+
MHandler##_##api = \
100+
[](const pi_plugin &, std::optional<pi_result>, void *) {};
91101
#include <CL/sycl/detail/pi.def>
92102
#undef _PI_API
93103
};

sycl/unittests/pi/pi_arguments_handler.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,23 @@ TEST(PiArgumentsHandlerTest, CanUnpackArguments) {
2020
const pi_uint32 NumPlatforms = 42;
2121
pi_platform *Platforms = new pi_platform[NumPlatforms];
2222

23-
Handler.set_piPlatformsGet(
24-
[&](pi_uint32 NP, pi_platform *Plts, pi_uint32 *Ret) {
25-
EXPECT_EQ(NP, NumPlatforms);
26-
EXPECT_EQ(Platforms, Plts);
27-
EXPECT_EQ(Ret, nullptr);
28-
});
23+
Handler.set_piPlatformsGet([&](const pi_plugin &, std::optional<pi_result>,
24+
pi_uint32 NP, pi_platform *Plts,
25+
pi_uint32 *Ret) {
26+
EXPECT_EQ(NP, NumPlatforms);
27+
EXPECT_EQ(Platforms, Plts);
28+
EXPECT_EQ(Ret, nullptr);
29+
});
2930

3031
constexpr size_t Size = sizeof(pi_uint32) + 2 * sizeof(void *);
3132
std::array<unsigned char, Size> Data{0};
3233
*reinterpret_cast<pi_uint32 *>(Data.data()) = NumPlatforms;
3334
*reinterpret_cast<pi_platform **>(Data.data() + sizeof(pi_uint32)) =
3435
Platforms;
3536

37+
pi_plugin Plugin{};
3638
uint32_t ID = static_cast<uint32_t>(sycl::detail::PiApiKind::piPlatformsGet);
37-
Handler.handle(ID, Data.data());
39+
Handler.handle(ID, Plugin, std::nullopt, Data.data());
3840

3941
delete[] Platforms;
4042
}

0 commit comments

Comments
 (0)