150
|
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
|
173
|
16 #include "omptarget.h"
|
150
|
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(is_valid_binary_ty)(void *);
|
|
29 typedef int32_t(number_of_devices_ty)();
|
|
30 typedef int32_t(init_device_ty)(int32_t);
|
|
31 typedef __tgt_target_table *(load_binary_ty)(int32_t, void *);
|
|
32 typedef void *(data_alloc_ty)(int32_t, int64_t, void *);
|
|
33 typedef int32_t(data_submit_ty)(int32_t, void *, void *, int64_t);
|
173
|
34 typedef int32_t(data_submit_async_ty)(int32_t, void *, void *, int64_t,
|
|
35 __tgt_async_info *);
|
150
|
36 typedef int32_t(data_retrieve_ty)(int32_t, void *, void *, int64_t);
|
173
|
37 typedef int32_t(data_retrieve_async_ty)(int32_t, void *, void *, int64_t,
|
|
38 __tgt_async_info *);
|
150
|
39 typedef int32_t(data_delete_ty)(int32_t, void *);
|
|
40 typedef int32_t(run_region_ty)(int32_t, void *, void **, ptrdiff_t *,
|
|
41 int32_t);
|
173
|
42 typedef int32_t(run_region_async_ty)(int32_t, void *, void **, ptrdiff_t *,
|
|
43 int32_t, __tgt_async_info *);
|
150
|
44 typedef int32_t(run_team_region_ty)(int32_t, void *, void **, ptrdiff_t *,
|
|
45 int32_t, int32_t, int32_t, uint64_t);
|
173
|
46 typedef int32_t(run_team_region_async_ty)(int32_t, void *, void **,
|
|
47 ptrdiff_t *, int32_t, int32_t,
|
|
48 int32_t, uint64_t,
|
|
49 __tgt_async_info *);
|
150
|
50 typedef int64_t(init_requires_ty)(int64_t);
|
173
|
51 typedef int64_t(synchronize_ty)(int64_t, __tgt_async_info *);
|
150
|
52
|
173
|
53 int32_t Idx = -1; // RTL index, index is the number of devices
|
|
54 // of other RTLs that were registered before,
|
|
55 // i.e. the OpenMP index of the first device
|
|
56 // to be registered with this RTL.
|
|
57 int32_t NumberOfDevices = -1; // Number of devices this RTL deals with.
|
150
|
58
|
173
|
59 void *LibraryHandler = nullptr;
|
150
|
60
|
|
61 #ifdef OMPTARGET_DEBUG
|
|
62 std::string RTLName;
|
|
63 #endif
|
|
64
|
|
65 // Functions implemented in the RTL.
|
173
|
66 is_valid_binary_ty *is_valid_binary = nullptr;
|
|
67 number_of_devices_ty *number_of_devices = nullptr;
|
|
68 init_device_ty *init_device = nullptr;
|
|
69 load_binary_ty *load_binary = nullptr;
|
|
70 data_alloc_ty *data_alloc = nullptr;
|
|
71 data_submit_ty *data_submit = nullptr;
|
|
72 data_submit_async_ty *data_submit_async = nullptr;
|
|
73 data_retrieve_ty *data_retrieve = nullptr;
|
|
74 data_retrieve_async_ty *data_retrieve_async = nullptr;
|
|
75 data_delete_ty *data_delete = nullptr;
|
|
76 run_region_ty *run_region = nullptr;
|
|
77 run_region_async_ty *run_region_async = nullptr;
|
|
78 run_team_region_ty *run_team_region = nullptr;
|
|
79 run_team_region_async_ty *run_team_region_async = nullptr;
|
|
80 init_requires_ty *init_requires = nullptr;
|
|
81 synchronize_ty *synchronize = nullptr;
|
150
|
82
|
|
83 // Are there images associated with this RTL.
|
173
|
84 bool isUsed = false;
|
150
|
85
|
|
86 // Mutex for thread-safety when calling RTL interface functions.
|
|
87 // It is easier to enforce thread-safety at the libomptarget level,
|
|
88 // so that developers of new RTLs do not have to worry about it.
|
|
89 std::mutex Mtx;
|
|
90
|
|
91 // The existence of the mutex above makes RTLInfoTy non-copyable.
|
|
92 // We need to provide a copy constructor explicitly.
|
173
|
93 RTLInfoTy() = default;
|
150
|
94
|
173
|
95 RTLInfoTy(const RTLInfoTy &r) {
|
150
|
96 Idx = r.Idx;
|
|
97 NumberOfDevices = r.NumberOfDevices;
|
|
98 LibraryHandler = r.LibraryHandler;
|
|
99 #ifdef OMPTARGET_DEBUG
|
|
100 RTLName = r.RTLName;
|
|
101 #endif
|
|
102 is_valid_binary = r.is_valid_binary;
|
|
103 number_of_devices = r.number_of_devices;
|
|
104 init_device = r.init_device;
|
|
105 load_binary = r.load_binary;
|
|
106 data_alloc = r.data_alloc;
|
|
107 data_submit = r.data_submit;
|
173
|
108 data_submit_async = r.data_submit_async;
|
150
|
109 data_retrieve = r.data_retrieve;
|
173
|
110 data_retrieve_async = r.data_retrieve_async;
|
150
|
111 data_delete = r.data_delete;
|
|
112 run_region = r.run_region;
|
173
|
113 run_region_async = r.run_region_async;
|
150
|
114 run_team_region = r.run_team_region;
|
173
|
115 run_team_region_async = r.run_team_region_async;
|
150
|
116 init_requires = r.init_requires;
|
|
117 isUsed = r.isUsed;
|
173
|
118 synchronize = r.synchronize;
|
150
|
119 }
|
|
120 };
|
|
121
|
|
122 /// RTLs identified in the system.
|
|
123 class RTLsTy {
|
|
124 private:
|
|
125 // Mutex-like object to guarantee thread-safety and unique initialization
|
|
126 // (i.e. the library attempts to load the RTLs (plugins) only once).
|
|
127 std::once_flag initFlag;
|
|
128 void LoadRTLs(); // not thread-safe
|
|
129
|
|
130 public:
|
|
131 // List of the detected runtime libraries.
|
|
132 std::list<RTLInfoTy> AllRTLs;
|
|
133
|
|
134 // Array of pointers to the detected runtime libraries that have compatible
|
|
135 // binaries.
|
|
136 std::vector<RTLInfoTy *> UsedRTLs;
|
|
137
|
173
|
138 int64_t RequiresFlags = OMP_REQ_UNDEFINED;
|
150
|
139
|
173
|
140 explicit RTLsTy() = default;
|
150
|
141
|
|
142 // Register the clauses of the requires directive.
|
|
143 void RegisterRequires(int64_t flags);
|
|
144
|
|
145 // Register a shared library with all (compatible) RTLs.
|
|
146 void RegisterLib(__tgt_bin_desc *desc);
|
|
147
|
|
148 // Unregister a shared library from all RTLs.
|
|
149 void UnregisterLib(__tgt_bin_desc *desc);
|
|
150 };
|
173
|
151 extern RTLsTy *RTLs;
|
|
152 extern std::mutex *RTLsMtx;
|
150
|
153
|
|
154
|
|
155 /// Map between the host entry begin and the translation table. Each
|
|
156 /// registered library gets one TranslationTable. Use the map from
|
|
157 /// __tgt_offload_entry so that we may quickly determine whether we
|
|
158 /// are trying to (re)register an existing lib or really have a new one.
|
|
159 struct TranslationTable {
|
|
160 __tgt_target_table HostTable;
|
|
161
|
|
162 // Image assigned to a given device.
|
|
163 std::vector<__tgt_device_image *> TargetsImages; // One image per device ID.
|
|
164
|
|
165 // Table of entry points or NULL if it was not already computed.
|
|
166 std::vector<__tgt_target_table *> TargetsTable; // One table per device ID.
|
|
167 };
|
|
168 typedef std::map<__tgt_offload_entry *, TranslationTable>
|
|
169 HostEntriesBeginToTransTableTy;
|
173
|
170 extern HostEntriesBeginToTransTableTy *HostEntriesBeginToTransTable;
|
|
171 extern std::mutex *TrlTblMtx;
|
150
|
172
|
|
173 /// Map between the host ptr and a table index
|
|
174 struct TableMap {
|
173
|
175 TranslationTable *Table = nullptr; // table associated with the host ptr.
|
|
176 uint32_t Index = 0; // index in which the host ptr translated entry is found.
|
|
177 TableMap() = default;
|
150
|
178 TableMap(TranslationTable *table, uint32_t index)
|
|
179 : Table(table), Index(index) {}
|
|
180 };
|
|
181 typedef std::map<void *, TableMap> HostPtrToTableMapTy;
|
173
|
182 extern HostPtrToTableMapTy *HostPtrToTableMap;
|
|
183 extern std::mutex *TblMapMtx;
|
150
|
184
|
|
185 #endif
|