1 //===------------ rtl.h - Target independent OpenMP target RTL ------------===//
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 //
9 // Declarations for handling RTL plugins.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef _OMPTARGET_RTL_H
14 #define _OMPTARGET_RTL_H
15 
16 #include "omptarget.h"
17 #include <list>
18 #include <map>
19 #include <mutex>
20 #include <string>
21 #include <vector>
22 
23 // Forward declarations.
24 struct DeviceTy;
25 struct __tgt_bin_desc;
26 
27 struct RTLInfoTy {
28   typedef int32_t(init_plugin_ty)();
29   typedef int32_t(deinit_plugin_ty)();
30   typedef int32_t(is_valid_binary_ty)(void *);
31   typedef int32_t(is_valid_binary_info_ty)(void *, void *);
32   typedef int32_t(is_data_exchangable_ty)(int32_t, int32_t);
33   typedef int32_t(number_of_devices_ty)();
34   typedef int32_t(init_device_ty)(int32_t);
35   typedef int32_t(deinit_device_ty)(int32_t);
36   typedef __tgt_target_table *(load_binary_ty)(int32_t, void *);
37   typedef void *(data_alloc_ty)(int32_t, int64_t, void *, int32_t);
38   typedef int32_t(data_submit_ty)(int32_t, void *, void *, int64_t);
39   typedef int32_t(data_submit_async_ty)(int32_t, void *, void *, int64_t,
40                                         __tgt_async_info *);
41   typedef int32_t(data_retrieve_ty)(int32_t, void *, void *, int64_t);
42   typedef int32_t(data_retrieve_async_ty)(int32_t, void *, void *, int64_t,
43                                           __tgt_async_info *);
44   typedef int32_t(data_exchange_ty)(int32_t, void *, int32_t, void *, int64_t);
45   typedef int32_t(data_exchange_async_ty)(int32_t, void *, int32_t, void *,
46                                           int64_t, __tgt_async_info *);
47   typedef int32_t(data_delete_ty)(int32_t, void *);
48   typedef int32_t(run_region_ty)(int32_t, void *, void **, ptrdiff_t *,
49                                  int32_t);
50   typedef int32_t(run_region_async_ty)(int32_t, void *, void **, ptrdiff_t *,
51                                        int32_t, __tgt_async_info *);
52   typedef int32_t(run_team_region_ty)(int32_t, void *, void **, ptrdiff_t *,
53                                       int32_t, int32_t, int32_t, uint64_t);
54   typedef int32_t(run_team_region_async_ty)(int32_t, void *, void **,
55                                             ptrdiff_t *, int32_t, int32_t,
56                                             int32_t, uint64_t,
57                                             __tgt_async_info *);
58   typedef int64_t(init_requires_ty)(int64_t);
59   typedef int32_t(synchronize_ty)(int32_t, __tgt_async_info *);
60   typedef int32_t (*register_lib_ty)(__tgt_bin_desc *);
61   typedef int32_t(supports_empty_images_ty)();
62   typedef void(print_device_info_ty)(int32_t);
63   typedef void(set_info_flag_ty)(uint32_t);
64   typedef int32_t(create_event_ty)(int32_t, void **);
65   typedef int32_t(record_event_ty)(int32_t, void *, __tgt_async_info *);
66   typedef int32_t(wait_event_ty)(int32_t, void *, __tgt_async_info *);
67   typedef int32_t(sync_event_ty)(int32_t, void *);
68   typedef int32_t(destroy_event_ty)(int32_t, void *);
69   typedef int32_t(release_async_info_ty)(int32_t, __tgt_async_info *);
70   typedef int32_t(init_async_info_ty)(int32_t, __tgt_async_info **);
71   typedef int64_t(init_device_into_ty)(int64_t, __tgt_device_info *,
72                                        const char **);
73 
74   int32_t Idx = -1;             // RTL index, index is the number of devices
75                                 // of other RTLs that were registered before,
76                                 // i.e. the OpenMP index of the first device
77                                 // to be registered with this RTL.
78   int32_t NumberOfDevices = -1; // Number of devices this RTL deals with.
79 
80   void *LibraryHandler = nullptr;
81 
82 #ifdef OMPTARGET_DEBUG
83   std::string RTLName;
84 #endif
85 
86   // Functions implemented in the RTL.
87   init_plugin_ty *init_plugin = nullptr;
88   deinit_plugin_ty *deinit_plugin = nullptr;
89   is_valid_binary_ty *is_valid_binary = nullptr;
90   is_valid_binary_info_ty *is_valid_binary_info = nullptr;
91   is_data_exchangable_ty *is_data_exchangable = nullptr;
92   number_of_devices_ty *number_of_devices = nullptr;
93   init_device_ty *init_device = nullptr;
94   deinit_device_ty *deinit_device = nullptr;
95   load_binary_ty *load_binary = nullptr;
96   data_alloc_ty *data_alloc = nullptr;
97   data_submit_ty *data_submit = nullptr;
98   data_submit_async_ty *data_submit_async = nullptr;
99   data_retrieve_ty *data_retrieve = nullptr;
100   data_retrieve_async_ty *data_retrieve_async = nullptr;
101   data_exchange_ty *data_exchange = nullptr;
102   data_exchange_async_ty *data_exchange_async = nullptr;
103   data_delete_ty *data_delete = nullptr;
104   run_region_ty *run_region = nullptr;
105   run_region_async_ty *run_region_async = nullptr;
106   run_team_region_ty *run_team_region = nullptr;
107   run_team_region_async_ty *run_team_region_async = nullptr;
108   init_requires_ty *init_requires = nullptr;
109   synchronize_ty *synchronize = nullptr;
110   register_lib_ty register_lib = nullptr;
111   register_lib_ty unregister_lib = nullptr;
112   supports_empty_images_ty *supports_empty_images = nullptr;
113   set_info_flag_ty *set_info_flag = nullptr;
114   print_device_info_ty *print_device_info = nullptr;
115   create_event_ty *create_event = nullptr;
116   record_event_ty *record_event = nullptr;
117   wait_event_ty *wait_event = nullptr;
118   sync_event_ty *sync_event = nullptr;
119   destroy_event_ty *destroy_event = nullptr;
120   init_async_info_ty *init_async_info = nullptr;
121   init_device_into_ty *init_device_info = nullptr;
122   release_async_info_ty *release_async_info = nullptr;
123 
124   // Are there images associated with this RTL.
125   bool IsUsed = false;
126 
127   // Mutex for thread-safety when calling RTL interface functions.
128   // It is easier to enforce thread-safety at the libomptarget level,
129   // so that developers of new RTLs do not have to worry about it.
130   std::mutex Mtx;
131 };
132 
133 /// RTLs identified in the system.
134 struct RTLsTy {
135   // List of the detected runtime libraries.
136   std::list<RTLInfoTy> AllRTLs;
137 
138   // Array of pointers to the detected runtime libraries that have compatible
139   // binaries.
140   std::vector<RTLInfoTy *> UsedRTLs;
141 
142   int64_t RequiresFlags = OMP_REQ_UNDEFINED;
143 
144   explicit RTLsTy() = default;
145 
146   // Register the clauses of the requires directive.
147   void registerRequires(int64_t Flags);
148 
149   // Initialize RTL if it has not been initialized
150   void initRTLonce(RTLInfoTy &RTL);
151 
152   // Initialize all RTLs
153   void initAllRTLs();
154 
155   // Register a shared library with all (compatible) RTLs.
156   void registerLib(__tgt_bin_desc *Desc);
157 
158   // Unregister a shared library from all RTLs.
159   void unregisterLib(__tgt_bin_desc *Desc);
160 
161   // Mutex-like object to guarantee thread-safety and unique initialization
162   // (i.e. the library attempts to load the RTLs (plugins) only once).
163   std::once_flag InitFlag;
164   void loadRTLs(); // not thread-safe
165 };
166 
167 /// Map between the host entry begin and the translation table. Each
168 /// registered library gets one TranslationTable. Use the map from
169 /// __tgt_offload_entry so that we may quickly determine whether we
170 /// are trying to (re)register an existing lib or really have a new one.
171 struct TranslationTable {
172   __tgt_target_table HostTable;
173 
174   // Image assigned to a given device.
175   std::vector<__tgt_device_image *> TargetsImages; // One image per device ID.
176 
177   // Table of entry points or NULL if it was not already computed.
178   std::vector<__tgt_target_table *> TargetsTable; // One table per device ID.
179 };
180 typedef std::map<__tgt_offload_entry *, TranslationTable>
181     HostEntriesBeginToTransTableTy;
182 
183 /// Map between the host ptr and a table index
184 struct TableMap {
185   TranslationTable *Table = nullptr; // table associated with the host ptr.
186   uint32_t Index = 0; // index in which the host ptr translated entry is found.
187   TableMap() = default;
TableMapTableMap188   TableMap(TranslationTable *Table, uint32_t Index)
189       : Table(Table), Index(Index) {}
190 };
191 typedef std::map<void *, TableMap> HostPtrToTableMapTy;
192 
193 #endif
194