1 //===------------ rtl.h - Target independent OpenMP target RTL ------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.txt for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Declarations for handling RTL plugins.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef _OMPTARGET_RTL_H
15 #define _OMPTARGET_RTL_H
16 
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);
34   typedef int32_t(data_retrieve_ty)(int32_t, void *, void *, int64_t);
35   typedef int32_t(data_delete_ty)(int32_t, void *);
36   typedef int32_t(run_region_ty)(int32_t, void *, void **, ptrdiff_t *,
37                                  int32_t);
38   typedef int32_t(run_team_region_ty)(int32_t, void *, void **, ptrdiff_t *,
39                                       int32_t, int32_t, int32_t, uint64_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 
65   // Are there images associated with this RTL.
66   bool isUsed;
67 
68   // Mutex for thread-safety when calling RTL interface functions.
69   // It is easier to enforce thread-safety at the libomptarget level,
70   // so that developers of new RTLs do not have to worry about it.
71   std::mutex Mtx;
72 
73   // The existence of the mutex above makes RTLInfoTy non-copyable.
74   // We need to provide a copy constructor explicitly.
RTLInfoTyRTLInfoTy75   RTLInfoTy()
76       : Idx(-1), NumberOfDevices(-1), LibraryHandler(0),
77 #ifdef OMPTARGET_DEBUG
78         RTLName(),
79 #endif
80         is_valid_binary(0), number_of_devices(0), init_device(0),
81         load_binary(0), data_alloc(0), data_submit(0), data_retrieve(0),
82         data_delete(0), run_region(0), run_team_region(0), isUsed(false),
83         Mtx() {}
84 
RTLInfoTyRTLInfoTy85   RTLInfoTy(const RTLInfoTy &r) : Mtx() {
86     Idx = r.Idx;
87     NumberOfDevices = r.NumberOfDevices;
88     LibraryHandler = r.LibraryHandler;
89 #ifdef OMPTARGET_DEBUG
90     RTLName = r.RTLName;
91 #endif
92     is_valid_binary = r.is_valid_binary;
93     number_of_devices = r.number_of_devices;
94     init_device = r.init_device;
95     load_binary = r.load_binary;
96     data_alloc = r.data_alloc;
97     data_submit = r.data_submit;
98     data_retrieve = r.data_retrieve;
99     data_delete = r.data_delete;
100     run_region = r.run_region;
101     run_team_region = r.run_team_region;
102     isUsed = r.isUsed;
103   }
104 };
105 
106 /// RTLs identified in the system.
107 class RTLsTy {
108 private:
109   // Mutex-like object to guarantee thread-safety and unique initialization
110   // (i.e. the library attempts to load the RTLs (plugins) only once).
111   std::once_flag initFlag;
112   void LoadRTLs(); // not thread-safe
113 
114 public:
115   // List of the detected runtime libraries.
116   std::list<RTLInfoTy> AllRTLs;
117 
118   // Array of pointers to the detected runtime libraries that have compatible
119   // binaries.
120   std::vector<RTLInfoTy *> UsedRTLs;
121 
RTLsTy()122   explicit RTLsTy() {}
123 
124   // Register a shared library with all (compatible) RTLs.
125   void RegisterLib(__tgt_bin_desc *desc);
126 
127   // Unregister a shared library from all RTLs.
128   void UnregisterLib(__tgt_bin_desc *desc);
129 };
130 extern RTLsTy RTLs;
131 extern std::mutex RTLsMtx;
132 
133 
134 /// Map between the host entry begin and the translation table. Each
135 /// registered library gets one TranslationTable. Use the map from
136 /// __tgt_offload_entry so that we may quickly determine whether we
137 /// are trying to (re)register an existing lib or really have a new one.
138 struct TranslationTable {
139   __tgt_target_table HostTable;
140 
141   // Image assigned to a given device.
142   std::vector<__tgt_device_image *> TargetsImages; // One image per device ID.
143 
144   // Table of entry points or NULL if it was not already computed.
145   std::vector<__tgt_target_table *> TargetsTable; // One table per device ID.
146 };
147 typedef std::map<__tgt_offload_entry *, TranslationTable>
148     HostEntriesBeginToTransTableTy;
149 extern HostEntriesBeginToTransTableTy HostEntriesBeginToTransTable;
150 extern std::mutex TrlTblMtx;
151 
152 /// Map between the host ptr and a table index
153 struct TableMap {
154   TranslationTable *Table; // table associated with the host ptr.
155   uint32_t Index; // index in which the host ptr translated entry is found.
TableMapTableMap156   TableMap() : Table(0), Index(0) {}
TableMapTableMap157   TableMap(TranslationTable *table, uint32_t index)
158       : Table(table), Index(index) {}
159 };
160 typedef std::map<void *, TableMap> HostPtrToTableMapTy;
161 extern HostPtrToTableMapTy HostPtrToTableMap;
162 extern std::mutex TblMapMtx;
163 
164 #endif
165