Skip to content

Commit 8060a4e

Browse files
committed
[SYCL][XPTI] Pass plugin information to subscribers
1 parent a8fe4a5 commit 8060a4e

File tree

6 files changed

+78
-23
lines changed

6 files changed

+78
-23
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ void emitFunctionEndTrace(uint64_t CorrelationID, const char *FName);
190190
/// \param FName The name of the PI API call.
191191
/// \param ArgsData is a pointer to packed function call arguments.
192192
uint64_t emitFunctionWithArgsBeginTrace(uint32_t FuncID, const char *FName,
193-
unsigned char *ArgsData);
193+
unsigned char *ArgsData,
194+
backend Backend, pi_plugin Plugin);
194195

195196
/// Notifies XPTI subscribers about PI function call result.
196197
///
@@ -202,7 +203,8 @@ uint64_t emitFunctionWithArgsBeginTrace(uint32_t FuncID, const char *FName,
202203
/// \param Result is function call result value.
203204
void emitFunctionWithArgsEndTrace(uint64_t CorrelationID, uint32_t FuncID,
204205
const char *FName, unsigned char *ArgsData,
205-
pi_result Result);
206+
pi_result Result, backend Backend,
207+
pi_plugin Plugin);
206208

207209
// A wrapper for passing around byte array properties
208210
class ByteArray {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//==----------- xpti_plugin_info.hpp - Plugin info wrapper for XPTI --------==//
2+
//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#pragma once
11+
12+
#include <CL/sycl/detail/common.hpp>
13+
#include <CL/sycl/detail/pi.h>
14+
15+
__SYCL_INLINE_NAMESPACE(cl) {
16+
namespace sycl {
17+
namespace detail {
18+
struct XPTIPluginInfo {
19+
uint8_t backend; // ID of the backend, same as in sycl::backend.
20+
pi_plugin plugin; // Plugin, that was used to perform PI call.
21+
void *next; // [Provisional] Pointer to the extended call function info.
22+
};
23+
} // namespace detail
24+
} // namespace sycl
25+
} // __SYCL_INLINE_NAMESPACE(cl)

sycl/source/detail/pi.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
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/detail/xpti_plugin_info.hpp>
21+
#include <CL/sycl/version.hpp>
2022
#include <detail/config.hpp>
2123
#include <detail/global_handler.hpp>
2224
#include <detail/plugin.hpp>
@@ -36,6 +38,10 @@
3638
#include "xpti_trace_framework.h"
3739
#endif
3840

41+
#define STR(x) #x
42+
#define SYCL_VERSION_STR \
43+
"sycl " STR(__LIBSYCL_MAJOR_VERSION) "." STR(__LIBSYCL_MINOR_VERSION)
44+
3945
__SYCL_INLINE_NAMESPACE(cl) {
4046
namespace sycl {
4147
namespace detail {
@@ -50,9 +56,9 @@ xpti_td *GPICallEvent = nullptr;
5056
xpti_td *GPIArgCallEvent = nullptr;
5157
/// Constants being used as placeholder until one is able to reliably get the
5258
/// 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";
59+
constexpr uint32_t GMajVer = __LIBSYCL_MAJOR_VERSION;
60+
constexpr uint32_t GMinVer = __LIBSYCL_MINOR_VERSION;
61+
constexpr const char *GVerStr = SYCL_VERSION_STR;
5662
#endif // XPTI_ENABLE_INSTRUMENTATION
5763

5864
template <cl::sycl::backend BE>
@@ -138,15 +144,18 @@ void emitFunctionEndTrace(uint64_t CorrelationID, const char *FName) {
138144
}
139145

140146
uint64_t emitFunctionWithArgsBeginTrace(uint32_t FuncID, const char *FuncName,
141-
unsigned char *ArgsData) {
147+
unsigned char *ArgsData,
148+
backend Backend, pi_plugin Plugin) {
142149
uint64_t CorrelationID = 0;
143150
#ifdef XPTI_ENABLE_INSTRUMENTATION
144151
if (xptiTraceEnabled()) {
145152
uint8_t StreamID = xptiRegisterStream(SYCL_PIDEBUGCALL_STREAM_NAME);
146153
CorrelationID = xptiGetUniqueId();
147154

155+
XPTIPluginInfo Info{static_cast<uint8_t>(Backend), Plugin, nullptr};
156+
148157
xpti::function_with_args_t Payload{FuncID, FuncName, ArgsData, nullptr,
149-
nullptr};
158+
&Info};
150159

151160
xptiNotifySubscribers(
152161
StreamID, (uint16_t)xpti::trace_point_type_t::function_with_args_begin,
@@ -158,13 +167,15 @@ uint64_t emitFunctionWithArgsBeginTrace(uint32_t FuncID, const char *FuncName,
158167

159168
void emitFunctionWithArgsEndTrace(uint64_t CorrelationID, uint32_t FuncID,
160169
const char *FuncName, unsigned char *ArgsData,
161-
pi_result Result) {
170+
pi_result Result, backend Backend,
171+
pi_plugin Plugin) {
162172
#ifdef XPTI_ENABLE_INSTRUMENTATION
163173
if (xptiTraceEnabled()) {
164174
uint8_t StreamID = xptiRegisterStream(SYCL_PIDEBUGCALL_STREAM_NAME);
165175

176+
XPTIPluginInfo Info{static_cast<uint8_t>(Backend), Plugin, nullptr};
166177
xpti::function_with_args_t Payload{FuncID, FuncName, ArgsData, &Result,
167-
nullptr};
178+
&Info};
168179

169180
xptiNotifySubscribers(
170181
StreamID, (uint16_t)xpti::trace_point_type_t::function_with_args_end,

sycl/source/detail/plugin.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ 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(), MBackend,
148+
MPlugin);
148149
#endif
149150
RT::PiResult R;
150151
if (pi::trace(pi::TraceLevel::PI_TRACE_CALLS)) {
@@ -163,9 +164,9 @@ class plugin {
163164
#ifdef XPTI_ENABLE_INSTRUMENTATION
164165
// Close the function begin with a call to function end
165166
pi::emitFunctionEndTrace(CorrelationID, PIFnName);
166-
pi::emitFunctionWithArgsEndTrace(CorrelationIDWithArgs,
167-
static_cast<uint32_t>(PiApiOffset),
168-
PIFnName, ArgsData.data(), R);
167+
pi::emitFunctionWithArgsEndTrace(
168+
CorrelationIDWithArgs, static_cast<uint32_t>(PiApiOffset), PIFnName,
169+
ArgsData.data(), R, MBackend, MPlugin);
169170
#endif
170171
return R;
171172
}

sycl/tools/pi-trace/pi_trace.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ XPTI_CALLBACK_API void xptiTraceInit(unsigned int major_version,
5151
tpCallback);
5252

5353
#define _PI_API(api) \
54-
ArgHandler.set##_##api([](auto &&... Args) { \
54+
ArgHandler.set##_##api([](sycl::detail::XPTIPluginInfo, \
55+
std::optional<pi_result>, auto &&...Args) { \
5556
std::cout << "---> " << #api << "(" \
5657
<< "\n"; \
5758
sycl::detail::pi::printArgs(Args...); \
@@ -77,8 +78,11 @@ 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 =
82+
static_cast<sycl::detail::XPTIPluginInfo *>(Data->user_data);
8083

81-
ArgHandler.handle(Data->function_id, Data->args_data);
84+
ArgHandler.handle(Data->function_id, *Plugin, std::nullopt,
85+
Data->args_data);
8286
std::cout << *static_cast<pi_result *>(Data->ret_data) << "\n";
8387
}
8488
}

sycl/tools/xpti_helpers/pi_arguments_handler.hpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010

1111
#include <CL/sycl/detail/pi.hpp>
1212
#include <CL/sycl/detail/type_traits.hpp>
13+
#include <CL/sycl/detail/xpti_plugin_info.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 = std::function<void(detail::XPTIPluginInfo,
47+
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, detail::XPTIPluginInfo 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>
@@ -71,23 +75,31 @@ class PiArgumentsHandler {
7175

7276
#define _PI_API(api) \
7377
void set##_##api( \
74-
const typename to_function<typename detail::function_traits<decltype( \
75-
api)>::args_type>::type &Handler) { \
76-
MHandler##_##api = [Handler](void *Data) { \
78+
const typename to_function< \
79+
typename detail::function_traits<decltype(api)>::args_type>::type \
80+
&Handler) { \
81+
MHandler##_##api = [Handler](detail::XPTIPluginInfo Plugin, \
82+
std::optional<pi_result> Res, void *Data) { \
7783
using TupleT = \
7884
typename detail::function_traits<decltype(api)>::args_type; \
7985
TupleT Tuple = unpack<TupleT>( \
8086
(char *)Data, \
8187
std::make_index_sequence<std::tuple_size<TupleT>::value>{}); \
82-
std::apply(Handler, Tuple); \
88+
const auto Wrapper = [Plugin, Res, Handler](auto &...Args) { \
89+
Handler(Plugin, Res, Args...); \
90+
}; \
91+
std::apply(Wrapper, Tuple); \
8392
}; \
8493
}
8594
#include <CL/sycl/detail/pi.def>
8695
#undef _PI_API
8796

8897
private:
8998
#define _PI_API(api) \
90-
std::function<void(void *)> MHandler##_##api = [](void *) {};
99+
std::function<void(detail::XPTIPluginInfo, std::optional<pi_result>, \
100+
void *)> \
101+
MHandler##_##api = \
102+
[](detail::XPTIPluginInfo, std::optional<pi_result>, void *) {};
91103
#include <CL/sycl/detail/pi.def>
92104
#undef _PI_API
93105
};

0 commit comments

Comments
 (0)