DPC++ Runtime
Runtime libraries for oneAPI DPC++
pi.hpp
Go to the documentation of this file.
1 //==---------- pi.hpp - Plugin Interface for SYCL RT -----------------------==//
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 
13 
14 #pragma once
15 
16 #include <sycl/backend_types.hpp> // for backend
17 #include <sycl/detail/export.hpp> // for __SYCL_EXPORT
18 #include <sycl/detail/os_util.hpp> // for __SYCL_RT_OS_LINUX
19 #include <sycl/detail/pi.h> // for piContextCreate, piContextGetInfo
20 
21 #include <cstdint> // for uint64_t, uint32_t
22 #include <memory> // for shared_ptr
23 #include <sstream> // for operator<<, basic_ostream, string...
24 #include <stddef.h> // for size_t
25 #include <string> // for char_traits, string
26 #include <type_traits> // for false_type, true_type
27 #include <vector> // for vector
28 
29 #ifdef XPTI_ENABLE_INSTRUMENTATION
30 // Forward declarations
31 namespace xpti {
32 struct trace_event_data_t;
33 }
34 #endif
35 
36 namespace sycl {
37 inline namespace _V1 {
38 
39 class context;
40 
41 namespace detail {
42 
43 enum class PiApiKind {
44 #define _PI_API(api) api,
45 #include <sycl/detail/pi.def>
46 };
47 class plugin;
48 using PluginPtr = std::shared_ptr<plugin>;
49 
50 template <sycl::backend BE>
51 __SYCL_EXPORT void *getPluginOpaqueData(void *opaquedata_arg);
52 
53 namespace pi {
54 
55 // The SYCL_PI_TRACE sets what we will trace.
56 // This is a bit-mask of various things we'd want to trace.
57 enum TraceLevel {
60  PI_TRACE_ALL = -1
61 };
62 
63 // Return true if we want to trace PI related activities.
64 bool trace(TraceLevel level);
65 
66 #ifdef __SYCL_RT_OS_WINDOWS
67 // these same constants are used by pi_win_proxy_loader.dll
68 // if a plugin is added here, add it there as well.
69 #ifdef _MSC_VER
70 #define __SYCL_OPENCL_PLUGIN_NAME "pi_opencl.dll"
71 #define __SYCL_LEVEL_ZERO_PLUGIN_NAME "pi_level_zero.dll"
72 #define __SYCL_CUDA_PLUGIN_NAME "pi_cuda.dll"
73 #define __SYCL_ESIMD_EMULATOR_PLUGIN_NAME "pi_esimd_emulator.dll"
74 #define __SYCL_HIP_PLUGIN_NAME "pi_hip.dll"
75 #define __SYCL_UR_PLUGIN_NAME "pi_unified_runtime.dll"
76 #define __SYCL_NATIVE_CPU_PLUGIN_NAME "pi_native_cpu.dll"
77 #else
78 #define __SYCL_OPENCL_PLUGIN_NAME "libpi_opencl.dll"
79 #define __SYCL_LEVEL_ZERO_PLUGIN_NAME "libpi_level_zero.dll"
80 #define __SYCL_CUDA_PLUGIN_NAME "libpi_cuda.dll"
81 #define __SYCL_ESIMD_EMULATOR_PLUGIN_NAME "libpi_esimd_emulator.dll"
82 #define __SYCL_HIP_PLUGIN_NAME "libpi_hip.dll"
83 #define __SYCL_UR_PLUGIN_NAME "libpi_unified_runtime.dll"
84 #define __SYCL_NATIVE_CPU_PLUGIN_NAME "libpi_native_cpu.dll"
85 #endif
86 #elif defined(__SYCL_RT_OS_LINUX)
87 #define __SYCL_OPENCL_PLUGIN_NAME "libpi_opencl.so"
88 #define __SYCL_LEVEL_ZERO_PLUGIN_NAME "libpi_level_zero.so"
89 #define __SYCL_CUDA_PLUGIN_NAME "libpi_cuda.so"
90 #define __SYCL_ESIMD_EMULATOR_PLUGIN_NAME "libpi_esimd_emulator.so"
91 #define __SYCL_HIP_PLUGIN_NAME "libpi_hip.so"
92 #define __SYCL_UR_PLUGIN_NAME "libpi_unified_runtime.so"
93 #define __SYCL_NATIVE_CPU_PLUGIN_NAME "libpi_native_cpu.so"
94 #elif defined(__SYCL_RT_OS_DARWIN)
95 #define __SYCL_OPENCL_PLUGIN_NAME "libpi_opencl.dylib"
96 #define __SYCL_LEVEL_ZERO_PLUGIN_NAME "libpi_level_zero.dylib"
97 #define __SYCL_CUDA_PLUGIN_NAME "libpi_cuda.dylib"
98 #define __SYCL_ESIMD_EMULATOR_PLUGIN_NAME "libpi_esimd_emulator.dylib"
99 #define __SYCL_HIP_PLUGIN_NAME "libpi_hip.dylib"
100 #define __SYCL_UR_PLUGIN_NAME "libpi_unified_runtime.dylib"
101 #define __SYCL_NATIVE_CPU_PLUGIN_NAME "libpi_native_cpu.dylib"
102 #else
103 #error "Unsupported OS"
104 #endif
105 
106 // Report error and no return (keeps compiler happy about no return statements).
107 [[noreturn]] __SYCL_EXPORT void die(const char *Message);
108 
109 __SYCL_EXPORT void assertion(bool Condition, const char *Message = nullptr);
110 
111 template <typename T>
112 void handleUnknownParamName(const char *functionName, T parameter) {
113  std::stringstream stream;
114  stream << "Unknown parameter " << parameter << " passed to " << functionName
115  << "\n";
116  auto str = stream.str();
117  auto msg = str.c_str();
118  die(msg);
119 }
120 
121 // This macro is used to report invalid enumerators being passed to PI API
122 // GetInfo functions. It will print the name of the function that invoked it
123 // and the value of the unknown enumerator.
124 #define __SYCL_PI_HANDLE_UNKNOWN_PARAM_NAME(parameter) \
125  { sycl::detail::pi::handleUnknownParamName(__func__, parameter); }
126 
141 using PiMem = ::pi_mem;
168 
169 __SYCL_EXPORT void contextSetExtendedDeleter(const sycl::context &constext,
171  void *user_data);
172 
173 // Function to load a shared library
174 // Implementation is OS dependent
175 void *loadOsLibrary(const std::string &Library);
176 
177 // Function to unload a shared library
178 // Implementation is OS dependent (see posix-pi.cpp and windows-pi.cpp)
179 int unloadOsLibrary(void *Library);
180 
181 // Function to load the shared plugin library
182 // On Windows, this will have been pre-loaded by proxy loader.
183 // Implementation is OS dependent.
184 void *loadOsPluginLibrary(const std::string &Library);
185 
186 // Function to unload the shared plugin library
187 // Implementation is OS dependent (see posix-pi.cpp and windows-pi.cpp)
188 int unloadOsPluginLibrary(void *Library);
189 
190 // OS agnostic function to unload the shared library
191 int unloadPlugin(void *Library);
192 
193 // Function to get Address of a symbol defined in the shared
194 // library, implementation is OS dependent.
195 void *getOsLibraryFuncAddress(void *Library, const std::string &FunctionName);
196 
197 // Get a string representing a _pi_platform_info enum
198 std::string platformInfoToString(pi_platform_info info);
199 
200 // Want all the needed casts be explicit, do not define conversion operators.
201 template <class To, class From> To cast(From value);
202 
203 // Holds the PluginInformation for the plugin that is bound.
204 // Currently a global variable is used to store OpenCL plugin information to be
205 // used with SYCL Interoperability Constructors.
206 // TODO: GlobalPlugin does not seem to be needed anymore. Consider removing it!
207 extern std::shared_ptr<plugin> GlobalPlugin;
208 
209 // Performs PI one-time initialization.
210 std::vector<PluginPtr> &initialize();
211 
212 // Get the plugin serving given backend.
213 template <backend BE> __SYCL_EXPORT const PluginPtr &getPlugin();
214 
215 // Utility Functions to get Function Name for a PI Api.
216 template <PiApiKind PiApiOffset> struct PiFuncInfo {};
217 
218 #define _PI_API(api) \
219  template <> struct PiFuncInfo<PiApiKind::api> { \
220  using FuncPtrT = decltype(&::api); \
221  inline const char *getFuncName() { return #api; } \
222  inline FuncPtrT getFuncPtr(PiPlugin MPlugin) { \
223  return MPlugin.PiFunctionTable.api; \
224  } \
225  };
226 #include <sycl/detail/pi.def>
227 
232 uint64_t emitFunctionBeginTrace(const char *FName);
233 
238 void emitFunctionEndTrace(uint64_t CorrelationID, const char *FName);
239 
246 uint64_t emitFunctionWithArgsBeginTrace(uint32_t FuncID, const char *FName,
247  unsigned char *ArgsData,
248  pi_plugin Plugin);
249 
259 void emitFunctionWithArgsEndTrace(uint64_t CorrelationID, uint32_t FuncID,
260  const char *FName, unsigned char *ArgsData,
261  pi_result Result, pi_plugin Plugin);
262 
265 PiDeviceBinaryType getBinaryImageFormat(const unsigned char *ImgData,
266  size_t ImgSize);
267 
268 } // namespace pi
269 
270 // Workaround for build with GCC 5.x
271 // An explicit specialization shall be declared in the namespace block.
272 // Having namespace as part of template name is not supported by GCC
273 // older than 7.x.
274 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480
275 namespace pi {
276 // Want all the needed casts be explicit, do not define conversion
277 // operators.
278 template <class To, class From> inline To cast(From value) {
279  // TODO: see if more sanity checks are possible.
280  sycl::detail::pi::assertion((sizeof(From) == sizeof(To)),
281  "assert: cast failed size check");
282  return (To)(value);
283 }
284 
285 // Helper traits for identifying std::vector with arbitrary element type.
286 template <typename T> struct IsStdVector : std::false_type {};
287 template <typename T> struct IsStdVector<std::vector<T>> : std::true_type {};
288 
289 // Overload for vectors that applies the cast to all elements. This
290 // creates a new vector.
291 template <class To, class FromE> To cast(std::vector<FromE> Values) {
292  static_assert(IsStdVector<To>::value, "Return type must be a vector.");
293  To ResultVec;
294  ResultVec.reserve(Values.size());
295  for (FromE &Val : Values)
296  ResultVec.push_back(cast<typename To::value_type>(Val));
297  return ResultVec;
298 }
299 
300 } // namespace pi
301 } // namespace detail
302 
303 } // namespace _V1
304 } // namespace sycl
305 
306 #undef _PI_API
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:51
const PluginPtr & getPlugin()
Definition: pi.cpp:498
void contextSetExtendedDeleter(const sycl::context &constext, pi_context_extended_deleter func, void *user_data)
Definition: pi.cpp:193
void emitFunctionEndTrace(uint64_t CorrelationID, const char *FName)
Emits an XPTI trace after the PI API call has been made.
Definition: pi.cpp:127
int unloadOsLibrary(void *Library)
Definition: posix_pi.cpp:37
void * loadOsPluginLibrary(const std::string &Library)
Definition: posix_pi.cpp:33
std::vector< PluginPtr > & initialize()
Definition: pi.cpp:372
::pi_image_mem_handle PiImageMemHandle
Definition: pi.hpp:162
void die(const char *Message)
Definition: pi.cpp:526
::pi_mem_flags PiMemFlags
Definition: pi.hpp:142
uint64_t emitFunctionBeginTrace(const char *FName)
Emits an XPTI trace before a PI API call is made.
Definition: pi.cpp:82
::pi_interop_mem_handle PiInteropMemHandle
Definition: pi.hpp:164
void assertion(bool Condition, const char *Message=nullptr)
Definition: pi.cpp:531
PiProgram cast(cl_program)=delete
PiDeviceBinaryType getBinaryImageFormat(const unsigned char *ImgData, size_t ImgSize)
Tries to determine the device binary image foramat.
Definition: pi.cpp:626
void handleUnknownParamName(const char *functionName, T parameter)
Definition: pi.hpp:112
bool trace(TraceLevel level)
Definition: pi.cpp:366
uint64_t emitFunctionWithArgsBeginTrace(uint32_t FuncID, const char *FName, unsigned char *ArgsData, pi_plugin Plugin)
Notifies XPTI subscribers about PI function calls and packs call arguments.
Definition: pi.cpp:143
void * loadOsLibrary(const std::string &Library)
Definition: posix_pi.cpp:20
std::string platformInfoToString(pi_platform_info info)
Definition: pi.cpp:203
std::shared_ptr< plugin > GlobalPlugin
Definition: pi.cpp:283
void emitFunctionWithArgsEndTrace(uint64_t CorrelationID, uint32_t FuncID, const char *FName, unsigned char *ArgsData, pi_result Result, pi_plugin Plugin)
Notifies XPTI subscribers about PI function call result.
Definition: pi.cpp:175
::pi_sampler_properties PiSamplerProperties
Definition: pi.hpp:146
::pi_image_handle PiImageHandle
Definition: pi.hpp:161
::pi_ext_sync_point PiExtSyncPoint
Definition: pi.hpp:156
::pi_queue_properties PiQueueProperties
Definition: pi.hpp:140
::pi_device_binary_type PiDeviceBinaryType
Definition: pi.hpp:134
void * getOsLibraryFuncAddress(void *Library, const std::string &FunctionName)
Definition: posix_pi.cpp:47
int unloadOsPluginLibrary(void *Library)
Definition: posix_pi.cpp:39
::pi_interop_semaphore_handle PiInteropSemaphoreHandle
Definition: pi.hpp:165
::pi_ext_command_buffer_command PiExtCommandBufferCommand
Definition: pi.hpp:159
int unloadPlugin(void *Library)
Definition: pi.cpp:338
void * getPluginOpaqueData(void *opaquedata_arg)
Definition: pi.cpp:61
std::shared_ptr< plugin > PluginPtr
Definition: pi.hpp:48
Definition: access.hpp:18
_pi_platform_backend pi_platform_backend
Definition: pi.h:819
_pi_kernel_cache_config
Definition: pi.h:808
_pi_context_info pi_context_info
Definition: pi.h:825
_pi_image_info pi_image_info
Definition: pi.h:827
_pi_device_info pi_device_info
Definition: pi.h:823
_pi_result
Definition: pi.h:224
_pi_device_info
Definition: pi.h:304
_pi_image_channel_type
Definition: pi.h:658
_pi_sampler_info pi_sampler_info
Definition: pi.h:841
_pi_program * pi_program
Definition: pi.h:1153
_pi_image_format pi_image_format
Definition: pi.h:1180
_pi_mem * pi_mem
Definition: pi.h:1152
pi_uint64 pi_image_handle
Definition: pi.h:1157
_pi_ext_command_buffer_command * pi_ext_command_buffer_command
Definition: pi.h:2351
_pi_kernel * pi_kernel
Definition: pi.h:1154
_pi_sampler * pi_sampler
Definition: pi.h:1156
_pi_platform_info
Definition: pi.h:239
_pi_plugin pi_plugin
Definition: pi.h:1211
_pi_image_info
Definition: pi.h:528
_pi_mem_type pi_mem_type
Definition: pi.h:833
pi_uint32 pi_ext_sync_point
Definition: pi.h:2352
_pi_image_channel_order
Definition: pi.h:640
_pi_device_type
Definition: pi.h:272
_pi_image_channel_type pi_image_channel_type
Definition: pi.h:836
pi_bitfield pi_queue_properties
Definition: pi.h:793
_pi_image_desc pi_image_desc
Definition: pi.h:1181
uint8_t pi_device_binary_type
Types of device binary.
Definition: pi.h:939
_pi_image_copy_flags
Definition: pi.h:676
_pi_sampler_filter_mode pi_sampler_filter_mode
Definition: pi.h:839
_pi_result pi_result
Definition: pi.h:817
_pi_event * pi_event
Definition: pi.h:1155
pi_bitfield pi_sampler_properties
Definition: pi.h:724
pi_bitfield pi_mem_flags
Definition: pi.h:758
_pi_device * pi_device
Definition: pi.h:1149
_pi_queue * pi_queue
Definition: pi.h:1151
void(* pi_context_extended_deleter)(void *user_data)
Definition: pi.h:1341
_pi_mem_type
Definition: pi.h:588
_pi_peer_attr pi_peer_attr
Definition: pi.h:1195
_pi_sampler_addressing_mode pi_sampler_addressing_mode
Definition: pi.h:838
_pi_sampler_info
Definition: pi.h:687
_pi_ext_command_buffer * pi_ext_command_buffer
Definition: pi.h:2350
_pi_image_copy_flags pi_image_copy_flags
Definition: pi.h:850
_pi_platform * pi_platform
Definition: pi.h:1148
_pi_context_info
Definition: pi.h:479
_pi_sampler_filter_mode
Definition: pi.h:706
_pi_kernel_cache_config pi_kernel_cache_config
Definition: pi.h:848
void * pi_image_mem_handle
Definition: pi.h:1158
_pi_sampler_addressing_mode
Definition: pi.h:698
_pi_platform_backend
Definition: pi.h:283
_pi_device_type pi_device_type
Definition: pi.h:820
_pi_image_channel_order pi_image_channel_order
Definition: pi.h:835
pi_uint64 pi_interop_semaphore_handle
Definition: pi.h:1160
_pi_peer_attr
Definition: pi.h:1185
_pi_context * pi_context
Definition: pi.h:1150
pi_uint64 pi_interop_mem_handle
Definition: pi.h:1159