1 //===------------ rtl.h - Target independent OpenMP target RTL ------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Declarations for handling RTL plugins.
11 //===----------------------------------------------------------------------===//
13 #ifndef _OMPTARGET_RTL_H
14 #define _OMPTARGET_RTL_H
16 #include "omptarget.h"
17 #include "llvm/ADT/DenseSet.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/Support/DynamicLibrary.h"
21 #include "omptarget.h"
28 // Forward declarations.
30 struct __tgt_bin_desc
;
33 typedef int32_t(init_plugin_ty
)();
34 typedef int32_t(deinit_plugin_ty
)();
35 typedef int32_t(is_valid_binary_ty
)(void *);
36 typedef int32_t(is_valid_binary_info_ty
)(void *, void *);
37 typedef int32_t(is_data_exchangable_ty
)(int32_t, int32_t);
38 typedef int32_t(number_of_devices_ty
)();
39 typedef int32_t(init_device_ty
)(int32_t);
40 typedef int32_t(deinit_device_ty
)(int32_t);
41 typedef __tgt_target_table
*(load_binary_ty
)(int32_t, void *);
42 typedef void *(data_alloc_ty
)(int32_t, int64_t, void *, int32_t);
43 typedef int32_t(data_submit_ty
)(int32_t, void *, void *, int64_t);
44 typedef int32_t(data_submit_async_ty
)(int32_t, void *, void *, int64_t,
46 typedef int32_t(data_retrieve_ty
)(int32_t, void *, void *, int64_t);
47 typedef int32_t(data_retrieve_async_ty
)(int32_t, void *, void *, int64_t,
49 typedef int32_t(data_exchange_ty
)(int32_t, void *, int32_t, void *, int64_t);
50 typedef int32_t(data_exchange_async_ty
)(int32_t, void *, int32_t, void *,
51 int64_t, __tgt_async_info
*);
52 typedef int32_t(data_delete_ty
)(int32_t, void *, int32_t);
53 typedef int32_t(launch_kernel_ty
)(int32_t, void *, void **, ptrdiff_t *,
54 const KernelArgsTy
*, __tgt_async_info
*);
55 typedef int64_t(init_requires_ty
)(int64_t);
56 typedef int32_t(synchronize_ty
)(int32_t, __tgt_async_info
*);
57 typedef int32_t(query_async_ty
)(int32_t, __tgt_async_info
*);
58 typedef int32_t (*register_lib_ty
)(__tgt_bin_desc
*);
59 typedef int32_t(supports_empty_images_ty
)();
60 typedef void(print_device_info_ty
)(int32_t);
61 typedef void(set_info_flag_ty
)(uint32_t);
62 typedef int32_t(create_event_ty
)(int32_t, void **);
63 typedef int32_t(record_event_ty
)(int32_t, void *, __tgt_async_info
*);
64 typedef int32_t(wait_event_ty
)(int32_t, void *, __tgt_async_info
*);
65 typedef int32_t(sync_event_ty
)(int32_t, void *);
66 typedef int32_t(destroy_event_ty
)(int32_t, void *);
67 typedef int32_t(release_async_info_ty
)(int32_t, __tgt_async_info
*);
68 typedef int32_t(init_async_info_ty
)(int32_t, __tgt_async_info
**);
69 typedef int64_t(init_device_into_ty
)(int64_t, __tgt_device_info
*,
71 typedef int32_t(data_lock_ty
)(int32_t, void *, int64_t, void **);
72 typedef int32_t(data_unlock_ty
)(int32_t, void *);
73 typedef int32_t(data_notify_mapped_ty
)(int32_t, void *, int64_t);
74 typedef int32_t(data_notify_unmapped_ty
)(int32_t, void *);
75 typedef int32_t(set_device_offset_ty
)(int32_t);
76 typedef int32_t(activate_record_replay_ty
)(int32_t, uint64_t, void *, bool,
79 int32_t Idx
= -1; // RTL index, index is the number of devices
80 // of other RTLs that were registered before,
81 // i.e. the OpenMP index of the first device
82 // to be registered with this RTL.
83 int32_t NumberOfDevices
= -1; // Number of devices this RTL deals with.
85 std::unique_ptr
<llvm::sys::DynamicLibrary
> LibraryHandler
;
87 #ifdef OMPTARGET_DEBUG
91 // Functions implemented in the RTL.
92 init_plugin_ty
*init_plugin
= nullptr;
93 deinit_plugin_ty
*deinit_plugin
= nullptr;
94 is_valid_binary_ty
*is_valid_binary
= nullptr;
95 is_valid_binary_info_ty
*is_valid_binary_info
= nullptr;
96 is_data_exchangable_ty
*is_data_exchangable
= nullptr;
97 number_of_devices_ty
*number_of_devices
= nullptr;
98 init_device_ty
*init_device
= nullptr;
99 deinit_device_ty
*deinit_device
= nullptr;
100 load_binary_ty
*load_binary
= nullptr;
101 data_alloc_ty
*data_alloc
= nullptr;
102 data_submit_ty
*data_submit
= nullptr;
103 data_submit_async_ty
*data_submit_async
= nullptr;
104 data_retrieve_ty
*data_retrieve
= nullptr;
105 data_retrieve_async_ty
*data_retrieve_async
= nullptr;
106 data_exchange_ty
*data_exchange
= nullptr;
107 data_exchange_async_ty
*data_exchange_async
= nullptr;
108 data_delete_ty
*data_delete
= nullptr;
109 launch_kernel_ty
*launch_kernel
= nullptr;
110 init_requires_ty
*init_requires
= nullptr;
111 synchronize_ty
*synchronize
= nullptr;
112 query_async_ty
*query_async
= nullptr;
113 register_lib_ty register_lib
= nullptr;
114 register_lib_ty unregister_lib
= nullptr;
115 supports_empty_images_ty
*supports_empty_images
= nullptr;
116 set_info_flag_ty
*set_info_flag
= nullptr;
117 print_device_info_ty
*print_device_info
= nullptr;
118 create_event_ty
*create_event
= nullptr;
119 record_event_ty
*record_event
= nullptr;
120 wait_event_ty
*wait_event
= nullptr;
121 sync_event_ty
*sync_event
= nullptr;
122 destroy_event_ty
*destroy_event
= nullptr;
123 init_async_info_ty
*init_async_info
= nullptr;
124 init_device_into_ty
*init_device_info
= nullptr;
125 release_async_info_ty
*release_async_info
= nullptr;
126 data_lock_ty
*data_lock
= nullptr;
127 data_unlock_ty
*data_unlock
= nullptr;
128 data_notify_mapped_ty
*data_notify_mapped
= nullptr;
129 data_notify_unmapped_ty
*data_notify_unmapped
= nullptr;
130 set_device_offset_ty
*set_device_offset
= nullptr;
131 activate_record_replay_ty
*activate_record_replay
= nullptr;
133 // Are there images associated with this RTL.
136 llvm::DenseSet
<const __tgt_device_image
*> UsedImages
;
138 // Mutex for thread-safety when calling RTL interface functions.
139 // It is easier to enforce thread-safety at the libomptarget level,
140 // so that developers of new RTLs do not have to worry about it.
144 /// RTLs identified in the system.
146 // List of the detected runtime libraries.
147 std::list
<RTLInfoTy
> AllRTLs
;
149 // Array of pointers to the detected runtime libraries that have compatible
151 llvm::SmallVector
<RTLInfoTy
*> UsedRTLs
;
153 int64_t RequiresFlags
= OMP_REQ_UNDEFINED
;
155 explicit RTLsTy() = default;
157 // Register the clauses of the requires directive.
158 void registerRequires(int64_t Flags
);
160 // Initialize RTL if it has not been initialized
161 void initRTLonce(RTLInfoTy
&RTL
);
163 // Initialize all RTLs
166 // Register a shared library with all (compatible) RTLs.
167 void registerLib(__tgt_bin_desc
*Desc
);
169 // Unregister a shared library from all RTLs.
170 void unregisterLib(__tgt_bin_desc
*Desc
);
172 // not thread-safe, called from global constructor (i.e. once)
176 static bool attemptLoadRTL(const std::string
&RTLName
, RTLInfoTy
&RTL
);
179 /// Map between the host entry begin and the translation table. Each
180 /// registered library gets one TranslationTable. Use the map from
181 /// __tgt_offload_entry so that we may quickly determine whether we
182 /// are trying to (re)register an existing lib or really have a new one.
183 struct TranslationTable
{
184 __tgt_target_table HostTable
;
186 // Image assigned to a given device.
187 llvm::SmallVector
<__tgt_device_image
*>
188 TargetsImages
; // One image per device ID.
190 // Table of entry points or NULL if it was not already computed.
191 llvm::SmallVector
<__tgt_target_table
*>
192 TargetsTable
; // One table per device ID.
194 typedef std::map
<__tgt_offload_entry
*, TranslationTable
>
195 HostEntriesBeginToTransTableTy
;
197 /// Map between the host ptr and a table index
199 TranslationTable
*Table
= nullptr; // table associated with the host ptr.
200 uint32_t Index
= 0; // index in which the host ptr translated entry is found.
201 TableMap() = default;
202 TableMap(TranslationTable
*Table
, uint32_t Index
)
203 : Table(Table
), Index(Index
) {}
205 typedef std::map
<void *, TableMap
> HostPtrToTableMapTy
;