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