DPC++ Runtime
Runtime libraries for oneAPI DPC++
ur.hpp
Go to the documentation of this file.
1 //==---------- ur.hpp - Unified Runtime integration helpers ----------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
14 
15 #pragma once
16 
17 #include <sycl/backend_types.hpp>
18 #include <sycl/detail/export.hpp>
19 #include <sycl/detail/os_util.hpp>
20 #include <ur_api.h>
21 
22 #include <memory>
23 #include <type_traits>
24 #include <vector>
25 
29 #define UR_DEVICE_INFO_EXTENSION_DEVICELIB_ASSERT "cl_intel_devicelib_assert"
30 
31 typedef void (*pi_context_extended_deleter)(void *user_data);
32 
35 
36 #ifdef XPTI_ENABLE_INSTRUMENTATION
37 // Forward declarations
38 namespace xpti {
39 struct trace_event_data_t;
40 }
41 #endif
42 
43 namespace sycl {
44 inline namespace _V1 {
45 
46 class context;
47 
48 namespace detail {
49 
50 enum class UrApiKind {
51 #define _UR_API(api) api,
52 #include <ur_api_funcs.def>
53 #undef _UR_API
54 };
55 
56 struct UrFuncPtrMapT {
57 #define _UR_API(api) decltype(&::api) pfn_##api = nullptr;
58 #include <ur_api_funcs.def>
59 #undef _UR_API
60 };
61 
62 template <UrApiKind UrApiOffset> struct UrFuncInfo {};
63 
64 #ifdef _WIN32
65 void *GetWinProcAddress(void *module, const char *funcName);
66 inline void PopulateUrFuncPtrTable(UrFuncPtrMapT *funcs, void *module) {
67 #define _UR_API(api) \
68  funcs->pfn_##api = (decltype(&::api))GetWinProcAddress(module, #api);
69 #include <ur_api_funcs.def>
70 #undef _UR_API
71 }
72 
73 #define _UR_API(api) \
74  template <> struct UrFuncInfo<UrApiKind::api> { \
75  using FuncPtrT = decltype(&::api); \
76  inline const char *getFuncName() { return #api; } \
77  inline FuncPtrT getFuncPtr(const UrFuncPtrMapT *funcs) { \
78  return funcs->pfn_##api; \
79  } \
80  inline FuncPtrT getFuncPtrFromModule(void *module) { \
81  return (FuncPtrT)GetWinProcAddress(module, #api); \
82  } \
83  };
84 #include <ur_api_funcs.def>
85 #undef _UR_API
86 #else
87 #define _UR_API(api) \
88  template <> struct UrFuncInfo<UrApiKind::api> { \
89  using FuncPtrT = decltype(&::api); \
90  inline const char *getFuncName() { return #api; } \
91  constexpr inline FuncPtrT getFuncPtr(const void *) { return &api; } \
92  constexpr inline FuncPtrT getFuncPtrFromModule(void *) { return &api; } \
93  };
94 #include <ur_api_funcs.def>
95 #undef _UR_API
96 #endif
97 
98 namespace pi {
99 // This function is deprecated and it should be removed in the next release
100 // cycle (along with the definition for pi_context_extended_deleter).
101 __SYCL_EXPORT void contextSetExtendedDeleter(const sycl::context &constext,
103  void *user_data);
104 }
105 
106 class plugin;
107 using PluginPtr = std::shared_ptr<plugin>;
108 
109 // TODO: To be removed as this was only introduced for esimd which was removed.
110 template <sycl::backend BE>
111 __SYCL_EXPORT void *getPluginOpaqueData(void *opaquedata_arg);
112 
113 namespace ur {
114 // Function to load a shared library
115 // Implementation is OS dependent
116 void *loadOsLibrary(const std::string &Library);
117 
118 // Function to unload a shared library
119 // Implementation is OS dependent (see posix-ur.cpp and windows-ur.cpp)
120 int unloadOsLibrary(void *Library);
121 
122 // Function to get Address of a symbol defined in the shared
123 // library, implementation is OS dependent.
124 void *getOsLibraryFuncAddress(void *Library, const std::string &FunctionName);
125 
126 void *getURLoaderLibrary();
127 
128 // Performs UR one-time initialization.
129 std::vector<PluginPtr> &
130 initializeUr(ur_loader_config_handle_t LoaderConfig = nullptr);
131 
132 // Get the plugin serving given backend.
133 template <backend BE> const PluginPtr &getPlugin();
134 
135 // The SYCL_UR_TRACE sets what we will trace.
136 // This is a bit-mask of various things we'd want to trace.
137 enum TraceLevel { TRACE_BASIC = 0x1, TRACE_CALLS = 0x2, TRACE_ALL = -1 };
138 
139 // Return true if we want to trace UR related activities.
140 bool trace(TraceLevel level);
141 
142 // Want all the needed casts be explicit, do not define conversion operators.
143 template <class To, class From> To cast(From value);
144 
145 // Want all the needed casts be explicit, do not define conversion
146 // operators.
147 template <class To, class From> inline To cast(From value) {
148  // TODO: see if more sanity checks are possible.
149  static_assert(sizeof(From) == sizeof(To), "assert: cast failed size check");
150  return reinterpret_cast<To>(value);
151 }
152 
153 // Helper traits for identifying std::vector with arbitrary element type.
154 template <typename T> struct IsStdVector : std::false_type {};
155 template <typename T> struct IsStdVector<std::vector<T>> : std::true_type {};
156 
157 // Overload for vectors that applies the cast to all elements. This
158 // creates a new vector.
159 template <class To, class FromE> To cast(std::vector<FromE> Values) {
160  static_assert(IsStdVector<To>::value, "Return type must be a vector.");
161  To ResultVec;
162  ResultVec.reserve(Values.size());
163  for (FromE &Val : Values) {
164  ResultVec.push_back(cast<typename To::value_type>(Val));
165  }
166  return ResultVec;
167 }
168 
169 ur_program_metadata_t mapDeviceBinaryPropertyToProgramMetadata(
171 
172 } // namespace ur
173 } // namespace detail
174 } // namespace _V1
175 } // namespace sycl
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:50
The plugin class provides a unified interface to the underlying low-level runtimes for the device-agn...
Definition: plugin.hpp:44
std::function< void(interop_handle)> func
Definition: commands.cpp:328
void contextSetExtendedDeleter(const sycl::context &constext, pi_context_extended_deleter func, void *user_data)
Definition: ur.cpp:48
std::vector< PluginPtr > & initializeUr(ur_loader_config_handle_t LoaderConfig=nullptr)
Definition: ur.cpp:92
bool trace(TraceLevel level)
Definition: ur.cpp:81
void * getOsLibraryFuncAddress(void *Library, const std::string &FunctionName)
Definition: posix_ur.cpp:34
To cast(std::vector< cl_event > value)
ur_program_metadata_t mapDeviceBinaryPropertyToProgramMetadata(const sycl_device_binary_property &DeviceBinaryProperty)
Definition: ur.cpp:423
void * getURLoaderLibrary()
Definition: posix_ur.cpp:38
void * loadOsLibrary(const std::string &Library)
Definition: posix_ur.cpp:20
const PluginPtr & getPlugin()
Definition: ur.cpp:266
int unloadOsLibrary(void *Library)
Definition: posix_ur.cpp:32
void * getPluginOpaqueData(void *opaquedata_arg)
std::shared_ptr< plugin > PluginPtr
Definition: ur.hpp:107
void * GetWinProcAddress(void *module, const char *funcName)
Definition: windows_ur.cpp:24
Definition: access.hpp:18
void(* pi_context_extended_deleter)(void *user_data)
Definition: ur.hpp:31