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