25 #include <sycl/version.hpp>
38 #ifdef XPTI_ENABLE_INSTRUMENTATION
41 #include "xpti/xpti_trace_framework.h"
45 inline namespace _V1 {
52 const auto &Plugin = impl->getPlugin();
53 Plugin->call<UrApiKind::urContextSetExtendedDeleter>(
55 reinterpret_cast<ur_context_extended_deleter_t
>(
func), user_data);
59 #ifdef XPTI_ENABLE_INSTRUMENTATION
63 xpti_td *GSYCLGraphEvent =
nullptr;
66 template <sycl::backend BE>
74 "This operation is not supported by any existing backends.");
83 return (TraceLevelMask & Level) == Level;
87 ur_loader_config_handle_t LoaderConfig);
92 std::vector<PluginPtr> &
initializeUr(ur_loader_config_handle_t LoaderConfig) {
96 auto initializeHelper = [=]() {
99 if (std::getenv(
"SYCL_PI_TRACE")) {
100 std::cerr <<
"SYCL_PI_TRACE has been removed use SYCL_UR_TRACE instead\n";
107 static bool Initialized = initializeHelper();
108 std::ignore = Initialized;
114 ur_loader_config_handle_t LoaderConfig) {
115 #define CHECK_UR_SUCCESS(Call) __SYCL_CHECK_UR_CODE_NO_EXC(Call)
118 auto loaderConfigCreate =
121 auto loaderConfigEnableLayer =
122 loaderConfigEnableLayerInfo.getFuncPtrFromModule(
125 auto loaderConfigRelease =
128 loaderConfigSetCodeLocationCallbackInfo;
129 auto loaderConfigSetCodeLocationCallback =
130 loaderConfigSetCodeLocationCallbackInfo.getFuncPtrFromModule(
139 auto adapterGetInfo =
142 bool OwnLoaderConfig =
false;
146 OwnLoaderConfig =
true;
149 const char *LogOptions =
"level:info;output:stdout;flush:info";
152 _putenv_s(
"UR_LOG_TRACING", LogOptions);
154 setenv(
"UR_LOG_TRACING", LogOptions, 1);
156 CHECK_UR_SUCCESS(loaderConfigEnableLayer(LoaderConfig,
"UR_LAYER_TRACING"));
161 _putenv_s(
"UR_LOG_LOADER", LogOptions);
163 setenv(
"UR_LOG_LOADER", LogOptions, 1);
171 if (loaderConfigEnableLayer(LoaderConfig,
"UR_LAYER_ASAN")) {
172 loaderConfigRelease(LoaderConfig);
173 std::cerr <<
"Failed to enable ASAN layer\n";
182 if (loaderConfigEnableLayer(LoaderConfig,
"UR_LAYER_ASAN")) {
183 loaderConfigRelease(LoaderConfig);
184 std::cerr <<
"Failed to enable ASAN layer\n";
189 ur_device_init_flags_t device_flags = 0;
192 if (OwnLoaderConfig) {
196 uint32_t adapterCount = 0;
198 std::vector<ur_adapter_handle_t> adapters(adapterCount);
203 case UR_ADAPTER_BACKEND_LEVEL_ZERO:
205 case UR_ADAPTER_BACKEND_OPENCL:
207 case UR_ADAPTER_BACKEND_CUDA:
209 case UR_ADAPTER_BACKEND_HIP:
211 case UR_ADAPTER_BACKEND_NATIVE_CPU:
220 for (
const auto &adapter : adapters) {
221 ur_adapter_backend_t adapterBackend = UR_ADAPTER_BACKEND_UNKNOWN;
223 sizeof(adapterBackend), &adapterBackend,
225 auto syclBackend = UrToSyclBackend(adapterBackend);
226 Plugins.emplace_back(std::make_shared<plugin>(adapter, syclBackend));
229 #ifdef XPTI_ENABLE_INSTRUMENTATION
250 xpti::payload_t GraphPayload(
"application_graph");
251 uint64_t GraphInstanceNo;
253 xptiMakeEvent(
"application_graph", &GraphPayload, xpti::trace_graph_event,
254 xpti_at::active, &GraphInstanceNo);
255 if (GSYCLGraphEvent) {
258 xptiNotifySubscribers(StreamID, xpti::trace_graph_create,
nullptr,
259 GSYCLGraphEvent, GraphInstanceNo,
nullptr);
262 #undef CHECK_UR_SUCCESS
272 for (
auto &P : Plugins)
273 if (P->hasBackend(BE)) {
282 template const PluginPtr &getPlugin<backend::ext_oneapi_level_zero>();
283 template const PluginPtr &getPlugin<backend::ext_oneapi_cuda>();
284 template const PluginPtr &getPlugin<backend::ext_oneapi_hip>();
287 template <
typename ResT>
290 assert(NumBytes <=
sizeof(ResT));
293 for (
size_t I = 0; I < NumBytes; ++I) {
294 Result = (Result << 8) | static_cast<ResT>(Data[I]);
297 std::copy(Data, Data + NumBytes,
reinterpret_cast<char *
>(&Result));
304 const unsigned char *ImgData,
307 bool Is64bit = ImgData[4] == 2;
308 bool IsBigEndian = ImgData[5] == 2;
311 size_t SectionHeaderOffsetInfoOffset = Is64bit ? 0x28 : 0x20;
312 size_t SectionHeaderSizeInfoOffset = Is64bit ? 0x3A : 0x2E;
313 size_t SectionHeaderNumInfoOffset = Is64bit ? 0x3C : 0x30;
314 size_t SectionStringsHeaderIndexInfoOffset = Is64bit ? 0x3E : 0x32;
317 if (ImgSize < SectionStringsHeaderIndexInfoOffset + 2)
322 uint64_t SectionHeaderOffset = readELFValue<uint64_t>(
323 ImgData + SectionHeaderOffsetInfoOffset, Is64bit ? 8 : 4, IsBigEndian);
324 uint16_t SectionHeaderSize = readELFValue<uint16_t>(
325 ImgData + SectionHeaderSizeInfoOffset, 2, IsBigEndian);
326 uint16_t SectionHeaderNum = readELFValue<uint16_t>(
327 ImgData + SectionHeaderNumInfoOffset, 2, IsBigEndian);
328 uint16_t SectionStringsHeaderIndex = readELFValue<uint16_t>(
329 ImgData + SectionStringsHeaderIndexInfoOffset, 2, IsBigEndian);
333 if (ImgSize < SectionHeaderOffset +
static_cast<uint64_t
>(SectionHeaderNum) *
335 SectionStringsHeaderIndex >= SectionHeaderNum)
339 size_t SectionStringsInfoOffset = Is64bit ? 0x18 : 0x10;
340 const unsigned char *SectionStringsHeaderData =
341 ImgData + SectionHeaderOffset +
342 SectionStringsHeaderIndex * SectionHeaderSize;
343 uint64_t SectionStrings = readELFValue<uint64_t>(
344 SectionStringsHeaderData + SectionStringsInfoOffset, Is64bit ? 8 : 4,
346 const unsigned char *SectionStringsData = ImgData + SectionStrings;
350 for (
size_t I = 0; I < SectionHeaderNum; ++I) {
352 const unsigned char *HeaderData =
353 ImgData + SectionHeaderOffset + I * SectionHeaderSize;
354 uint32_t SectionNameOffset =
355 readELFValue<uint32_t>(HeaderData, 4, IsBigEndian);
359 const char *SectionName =
360 reinterpret_cast<const char *
>(SectionStringsData + SectionNameOffset);
361 if (SectionName == ExpectedSectionName)
370 assert(ImgSize >= 18 &&
"Not enough bytes to have an ELF header type.");
372 bool IsBigEndian = ImgData[5] == 2;
373 return readELFValue<uint16_t>(ImgData + 16, 2, IsBigEndian);
379 auto MatchMagicNumber = [&](
auto Number) {
380 return ImgSize >=
sizeof(Number) &&
381 std::memcmp(ImgData, &Number,
sizeof(Number)) == 0;
384 if (MatchMagicNumber(uint32_t{0x07230203}))
387 if (MatchMagicNumber(uint32_t{0xDEC04342}))
390 if (MatchMagicNumber(uint32_t{0x43544E49}))
396 if (ImgSize >= 18 && MatchMagicNumber(uint32_t{0x464c457F})) {
398 if (ELFHdrType == 0xFF04)
402 if (ELFHdrType == 0xFF12)
412 if (MatchMagicNumber(std::array{
'!',
'<',
'a',
'r',
'c',
'h',
'>',
'\n'}))
425 ur_program_metadata_t URMetadata{};
430 URMetadata.type = UR_PROGRAM_METADATA_TYPE_UINT32;
434 URMetadata.type = UR_PROGRAM_METADATA_TYPE_BYTE_ARRAY;
438 URMetadata.type = UR_PROGRAM_METADATA_TYPE_STRING;
439 URMetadata.value.pString =
The context class represents a SYCL context on which kernel functions may be executed.
XPTIRegistry & getXPTIRegistry()
std::vector< PluginPtr > & getPlugins()
static GlobalHandler & instance()
static ProgramManager & getInstance()
static const char * get()
void initializeFrameworkOnce()
void initializeStream(const std::string &StreamName, uint32_t MajVer, uint32_t MinVer, const std::string &VerStr)
Notifies XPTI subscribers about new stream.
std::function< void(interop_handle)> func
@ SYCL_PROPERTY_TYPE_STRING
@ SYCL_PROPERTY_TYPE_BYTE_ARRAY
@ SYCL_PROPERTY_TYPE_UINT32
sycl_device_binary_type
Types of device binary.
@ SYCL_DEVICE_BINARY_TYPE_SPIRV
@ SYCL_DEVICE_BINARY_TYPE_LLVMIR_BITCODE
@ SYCL_DEVICE_BINARY_TYPE_NONE
@ SYCL_DEVICE_BINARY_TYPE_NATIVE
__SYCL_EXTERN_STREAM_ATTRS ostream cerr
Linked to standard error (unbuffered)
void contextSetExtendedDeleter(const sycl::context &constext, pi_context_extended_deleter func, void *user_data)
sycl_device_binary_type getBinaryImageFormat(const unsigned char *ImgData, size_t ImgSize)
Tries to determine the device binary image foramat.
std::vector< PluginPtr > & initializeUr(ur_loader_config_handle_t LoaderConfig=nullptr)
bool trace(TraceLevel level)
static bool checkELFSectionPresent(const std::string &ExpectedSectionName, const unsigned char *ImgData, size_t ImgSize)
ur_program_metadata_t mapDeviceBinaryPropertyToProgramMetadata(const sycl_device_binary_property &DeviceBinaryProperty)
void * getURLoaderLibrary()
static uint16_t getELFHeaderType(const unsigned char *ImgData, size_t ImgSize)
static void initializePlugins(std::vector< PluginPtr > &Plugins, ur_loader_config_handle_t LoaderConfig)
static ResT readELFValue(const unsigned char *Data, size_t NumBytes, bool IsBigEndian)
const PluginPtr & getPlugin()
decltype(Obj::impl) const & getSyclObjImpl(const Obj &SyclObject)
constexpr const char * SYCL_STREAM_NAME
void * getPluginOpaqueData(void *opaquedata_arg)
std::shared_ptr< plugin > PluginPtr
ur_code_location_t codeLocationCallback(void *)
void copy(handler &CGH, const T *Src, T *Dest, size_t Count)
std::error_code make_error_code(sycl::errc E) noexcept
Constructs an error code using e and sycl_category()
#define CHECK_UR_SUCCESS(Call)
C++ utilities for Unified Runtime integration.
void(* pi_context_extended_deleter)(void *user_data)