blob: 2e661d6edf3f876a64c5dbb65e905622df8a9e61 [file] [log] [blame]
Jonas Hahnfeld43322802017-12-06 21:59:07 +00001//===------------ 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 <vector>
21
22// Forward declarations.
23struct DeviceTy;
24struct __tgt_bin_desc;
25
26struct 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
40 int32_t Idx; // RTL index, index is the number of devices
41 // of other RTLs that were registered before,
42 // i.e. the OpenMP index of the first device
43 // to be registered with this RTL.
44 int32_t NumberOfDevices; // Number of devices this RTL deals with.
45 std::vector<DeviceTy *> Devices; // one per device (NumberOfDevices in total).
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.
75 RTLInfoTy()
76 : Idx(-1), NumberOfDevices(-1), Devices(), 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
85 RTLInfoTy(const RTLInfoTy &r) : Mtx() {
86 Idx = r.Idx;
87 NumberOfDevices = r.NumberOfDevices;
88 Devices = r.Devices;
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 isUsed = r.isUsed;
104 }
105};
106
107/// RTLs identified in the system.
108class RTLsTy {
109private:
110 // Mutex-like object to guarantee thread-safety and unique initialization
111 // (i.e. the library attempts to load the RTLs (plugins) only once).
112 std::once_flag initFlag;
113 void LoadRTLs(); // not thread-safe
114
115public:
116 // List of the detected runtime libraries.
117 std::list<RTLInfoTy> AllRTLs;
118
119 // Array of pointers to the detected runtime libraries that have compatible
120 // binaries.
121 std::vector<RTLInfoTy *> UsedRTLs;
122
123 explicit RTLsTy() {}
124
125 // Register a shared library with all (compatible) RTLs.
126 void RegisterLib(__tgt_bin_desc *desc);
127
128 // Unregister a shared library from all RTLs.
129 void UnregisterLib(__tgt_bin_desc *desc);
130};
131extern RTLsTy RTLs;
132extern std::mutex RTLsMtx;
133
134
135/// Map between the host entry begin and the translation table. Each
136/// registered library gets one TranslationTable. Use the map from
137/// __tgt_offload_entry so that we may quickly determine whether we
138/// are trying to (re)register an existing lib or really have a new one.
139struct TranslationTable {
140 __tgt_target_table HostTable;
141
142 // Image assigned to a given device.
143 std::vector<__tgt_device_image *> TargetsImages; // One image per device ID.
144
145 // Table of entry points or NULL if it was not already computed.
146 std::vector<__tgt_target_table *> TargetsTable; // One table per device ID.
147};
148typedef std::map<__tgt_offload_entry *, TranslationTable>
149 HostEntriesBeginToTransTableTy;
150extern HostEntriesBeginToTransTableTy HostEntriesBeginToTransTable;
151extern std::mutex TrlTblMtx;
152
153/// Map between the host ptr and a table index
154struct TableMap {
155 TranslationTable *Table; // table associated with the host ptr.
156 uint32_t Index; // index in which the host ptr translated entry is found.
157 TableMap() : Table(0), Index(0) {}
158 TableMap(TranslationTable *table, uint32_t index)
159 : Table(table), Index(index) {}
160};
161typedef std::map<void *, TableMap> HostPtrToTableMapTy;
162extern HostPtrToTableMapTy HostPtrToTableMap;
163extern std::mutex TblMapMtx;
164
165#endif