Skip to content

Commit c64bac6

Browse files
committed
[OpenMP] Automate PluginAdaptorTy API declaration
This automates only the declaration of types and member variables in two steps. As a consequence, we also get the exact names and types of the API declarations in PluginAPI.h. All oversights this exposed have been resolved.
1 parent fce4c0a commit c64bac6

File tree

8 files changed

+88
-115
lines changed

8 files changed

+88
-115
lines changed

openmp/libomptarget/include/PluginManager.h

Lines changed: 42 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define OMPTARGET_PLUGIN_MANAGER_H
1515

1616
#include "Shared/APITypes.h"
17+
#include "Shared/PluginAPI.h"
1718

1819
#include "device.h"
1920

@@ -25,49 +26,6 @@
2526
#include <mutex>
2627

2728
struct PluginAdaptorTy {
28-
typedef int32_t(init_plugin_ty)();
29-
typedef int32_t(is_valid_binary_ty)(void *);
30-
typedef int32_t(is_valid_binary_info_ty)(void *, void *);
31-
typedef int32_t(is_data_exchangable_ty)(int32_t, int32_t);
32-
typedef int32_t(number_of_devices_ty)();
33-
typedef int32_t(init_device_ty)(int32_t);
34-
typedef __tgt_target_table *(load_binary_ty)(int32_t, void *);
35-
typedef void *(data_alloc_ty)(int32_t, int64_t, void *, int32_t);
36-
typedef int32_t(data_submit_ty)(int32_t, void *, void *, int64_t);
37-
typedef int32_t(data_submit_async_ty)(int32_t, void *, void *, int64_t,
38-
__tgt_async_info *);
39-
typedef int32_t(data_retrieve_ty)(int32_t, void *, void *, int64_t);
40-
typedef int32_t(data_retrieve_async_ty)(int32_t, void *, void *, int64_t,
41-
__tgt_async_info *);
42-
typedef int32_t(data_exchange_ty)(int32_t, void *, int32_t, void *, int64_t);
43-
typedef int32_t(data_exchange_async_ty)(int32_t, void *, int32_t, void *,
44-
int64_t, __tgt_async_info *);
45-
typedef int32_t(data_delete_ty)(int32_t, void *, int32_t);
46-
typedef int32_t(launch_kernel_ty)(int32_t, void *, void **, ptrdiff_t *,
47-
const KernelArgsTy *, __tgt_async_info *);
48-
typedef int64_t(init_requires_ty)(int64_t);
49-
typedef int32_t(synchronize_ty)(int32_t, __tgt_async_info *);
50-
typedef int32_t(query_async_ty)(int32_t, __tgt_async_info *);
51-
typedef int32_t(supports_empty_images_ty)();
52-
typedef void(print_device_info_ty)(int32_t);
53-
typedef void(set_info_flag_ty)(uint32_t);
54-
typedef int32_t(create_event_ty)(int32_t, void **);
55-
typedef int32_t(record_event_ty)(int32_t, void *, __tgt_async_info *);
56-
typedef int32_t(wait_event_ty)(int32_t, void *, __tgt_async_info *);
57-
typedef int32_t(sync_event_ty)(int32_t, void *);
58-
typedef int32_t(destroy_event_ty)(int32_t, void *);
59-
typedef int32_t(release_async_info_ty)(int32_t, __tgt_async_info *);
60-
typedef int32_t(init_async_info_ty)(int32_t, __tgt_async_info **);
61-
typedef int64_t(init_device_into_ty)(int64_t, __tgt_device_info *,
62-
const char **);
63-
typedef int32_t(data_lock_ty)(int32_t, void *, int64_t, void **);
64-
typedef int32_t(data_unlock_ty)(int32_t, void *);
65-
typedef int32_t(data_notify_mapped_ty)(int32_t, void *, int64_t);
66-
typedef int32_t(data_notify_unmapped_ty)(int32_t, void *);
67-
typedef int32_t(set_device_offset_ty)(int32_t);
68-
typedef int32_t(activate_record_replay_ty)(int32_t, uint64_t, void *, bool,
69-
bool, uint64_t &);
70-
7129
int32_t Idx = -1; // RTL index, index is the number of devices
7230
// of other RTLs that were registered before,
7331
// i.e. the OpenMP index of the first device
@@ -80,43 +38,48 @@ struct PluginAdaptorTy {
8038
std::string RTLName;
8139
#endif
8240

41+
#define DEFINE_PLUGIN_API_HANDLE(NAME) \
42+
using NAME##_ty = decltype(__tgt_rtl_##NAME); \
43+
NAME##_ty *NAME = nullptr;
44+
8345
// Functions implemented in the RTL.
84-
init_plugin_ty *init_plugin = nullptr;
85-
is_valid_binary_ty *is_valid_binary = nullptr;
86-
is_valid_binary_info_ty *is_valid_binary_info = nullptr;
87-
is_data_exchangable_ty *is_data_exchangable = nullptr;
88-
number_of_devices_ty *number_of_devices = nullptr;
89-
init_device_ty *init_device = nullptr;
90-
load_binary_ty *load_binary = nullptr;
91-
data_alloc_ty *data_alloc = nullptr;
92-
data_submit_ty *data_submit = nullptr;
93-
data_submit_async_ty *data_submit_async = nullptr;
94-
data_retrieve_ty *data_retrieve = nullptr;
95-
data_retrieve_async_ty *data_retrieve_async = nullptr;
96-
data_exchange_ty *data_exchange = nullptr;
97-
data_exchange_async_ty *data_exchange_async = nullptr;
98-
data_delete_ty *data_delete = nullptr;
99-
launch_kernel_ty *launch_kernel = nullptr;
100-
init_requires_ty *init_requires = nullptr;
101-
synchronize_ty *synchronize = nullptr;
102-
query_async_ty *query_async = nullptr;
103-
supports_empty_images_ty *supports_empty_images = nullptr;
104-
set_info_flag_ty *set_info_flag = nullptr;
105-
print_device_info_ty *print_device_info = nullptr;
106-
create_event_ty *create_event = nullptr;
107-
record_event_ty *record_event = nullptr;
108-
wait_event_ty *wait_event = nullptr;
109-
sync_event_ty *sync_event = nullptr;
110-
destroy_event_ty *destroy_event = nullptr;
111-
init_async_info_ty *init_async_info = nullptr;
112-
init_device_into_ty *init_device_info = nullptr;
113-
release_async_info_ty *release_async_info = nullptr;
114-
data_lock_ty *data_lock = nullptr;
115-
data_unlock_ty *data_unlock = nullptr;
116-
data_notify_mapped_ty *data_notify_mapped = nullptr;
117-
data_notify_unmapped_ty *data_notify_unmapped = nullptr;
118-
set_device_offset_ty *set_device_offset = nullptr;
119-
activate_record_replay_ty *activate_record_replay = nullptr;
46+
DEFINE_PLUGIN_API_HANDLE(init_plugin);
47+
DEFINE_PLUGIN_API_HANDLE(is_valid_binary);
48+
DEFINE_PLUGIN_API_HANDLE(is_valid_binary_info);
49+
DEFINE_PLUGIN_API_HANDLE(is_data_exchangable);
50+
DEFINE_PLUGIN_API_HANDLE(number_of_devices);
51+
DEFINE_PLUGIN_API_HANDLE(init_device);
52+
DEFINE_PLUGIN_API_HANDLE(load_binary);
53+
DEFINE_PLUGIN_API_HANDLE(data_alloc);
54+
DEFINE_PLUGIN_API_HANDLE(data_submit);
55+
DEFINE_PLUGIN_API_HANDLE(data_submit_async);
56+
DEFINE_PLUGIN_API_HANDLE(data_retrieve);
57+
DEFINE_PLUGIN_API_HANDLE(data_retrieve_async);
58+
DEFINE_PLUGIN_API_HANDLE(data_exchange);
59+
DEFINE_PLUGIN_API_HANDLE(data_exchange_async);
60+
DEFINE_PLUGIN_API_HANDLE(data_delete);
61+
DEFINE_PLUGIN_API_HANDLE(launch_kernel);
62+
DEFINE_PLUGIN_API_HANDLE(init_requires);
63+
DEFINE_PLUGIN_API_HANDLE(synchronize);
64+
DEFINE_PLUGIN_API_HANDLE(query_async);
65+
DEFINE_PLUGIN_API_HANDLE(supports_empty_images);
66+
DEFINE_PLUGIN_API_HANDLE(set_info_flag);
67+
DEFINE_PLUGIN_API_HANDLE(print_device_info);
68+
DEFINE_PLUGIN_API_HANDLE(create_event);
69+
DEFINE_PLUGIN_API_HANDLE(record_event);
70+
DEFINE_PLUGIN_API_HANDLE(wait_event);
71+
DEFINE_PLUGIN_API_HANDLE(sync_event);
72+
DEFINE_PLUGIN_API_HANDLE(destroy_event);
73+
DEFINE_PLUGIN_API_HANDLE(init_async_info);
74+
DEFINE_PLUGIN_API_HANDLE(init_device_info);
75+
DEFINE_PLUGIN_API_HANDLE(data_lock);
76+
DEFINE_PLUGIN_API_HANDLE(data_unlock);
77+
DEFINE_PLUGIN_API_HANDLE(data_notify_mapped);
78+
DEFINE_PLUGIN_API_HANDLE(data_notify_unmapped);
79+
DEFINE_PLUGIN_API_HANDLE(set_device_offset);
80+
DEFINE_PLUGIN_API_HANDLE(initialize_record_replay);
81+
82+
#undef DEFINE_PLUGIN_API_HANDLE
12083

12184
// Are there images associated with this RTL.
12285
bool IsUsed = false;

openmp/libomptarget/include/Shared/APITypes.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,32 @@ struct __tgt_async_info {
8686
/// happening.
8787
KernelLaunchEnvironmentTy KernelLaunchEnvironment;
8888
};
89+
90+
/// This struct contains all of the arguments to a target kernel region launch.
91+
struct KernelArgsTy {
92+
uint32_t Version; // Version of this struct for ABI compatibility.
93+
uint32_t NumArgs; // Number of arguments in each input pointer.
94+
void **ArgBasePtrs; // Base pointer of each argument (e.g. a struct).
95+
void **ArgPtrs; // Pointer to the argument data.
96+
int64_t *ArgSizes; // Size of the argument data in bytes.
97+
int64_t *ArgTypes; // Type of the data (e.g. to / from).
98+
void **ArgNames; // Name of the data for debugging, possibly null.
99+
void **ArgMappers; // User-defined mappers, possibly null.
100+
uint64_t Tripcount; // Tripcount for the teams / distribute loop, 0 otherwise.
101+
struct {
102+
uint64_t NoWait : 1; // Was this kernel spawned with a `nowait` clause.
103+
uint64_t Unused : 63;
104+
} Flags;
105+
uint32_t NumTeams[3]; // The number of teams (for x,y,z dimension).
106+
uint32_t ThreadLimit[3]; // The number of threads (for x,y,z dimension).
107+
uint32_t DynCGroupMem; // Amount of dynamic cgroup memory requested.
108+
};
109+
static_assert(sizeof(KernelArgsTy().Flags) == sizeof(uint64_t),
110+
"Invalid struct size");
111+
static_assert(sizeof(KernelArgsTy) ==
112+
(8 * sizeof(int32_t) + 3 * sizeof(int64_t) +
113+
4 * sizeof(void **) + 2 * sizeof(int64_t *)),
114+
"Invalid struct size");
89115
}
90116

91117
#endif // OMPTARGET_SHARED_API_TYPES_H

openmp/libomptarget/include/Shared/PluginAPI.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,16 @@ int32_t __tgt_rtl_data_notify_unmapped(int32_t ID, void *HstPtr);
215215
// Set the global device identifier offset, such that the plugin may determine a
216216
// unique device number.
217217
int32_t __tgt_rtl_set_device_offset(int32_t DeviceIdOffset);
218+
219+
int32_t __tgt_rtl_launch_kernel(int32_t DeviceId, void *TgtEntryPtr,
220+
void **TgtArgs, ptrdiff_t *TgtOffsets,
221+
KernelArgsTy *KernelArgs,
222+
__tgt_async_info *AsyncInfoPtr);
223+
224+
int32_t __tgt_rtl_initialize_record_replay(int32_t DeviceId, int64_t MemorySize,
225+
void *VAddr, bool isRecord,
226+
bool SaveOutput,
227+
uint64_t &ReqPtrArgOffset);
218228
}
219229

220230
#endif // OMPTARGET_SHARED_PLUGIN_API_H

openmp/libomptarget/include/device.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ struct DeviceTy {
150150

151151
// calls to RTL
152152
int32_t initOnce();
153-
__tgt_target_table *loadBinary(void *Img);
153+
__tgt_target_table *loadBinary(__tgt_device_image *Img);
154154

155155
// device memory allocation/deallocation routines
156156
/// Allocates \p Size bytes on the device, host or shared memory space
@@ -192,7 +192,7 @@ struct DeviceTy {
192192

193193
// Launch the kernel identified by \p TgtEntryPtr with the given arguments.
194194
int32_t launchKernel(void *TgtEntryPtr, void **TgtVarsPtr,
195-
ptrdiff_t *TgtOffsets, const KernelArgsTy &KernelArgs,
195+
ptrdiff_t *TgtOffsets, KernelArgsTy &KernelArgs,
196196
AsyncInfoTy &AsyncInfo);
197197

198198
/// Synchronize device/queue/event based on \p AsyncInfo and return

openmp/libomptarget/include/omptarget.h

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -121,29 +121,6 @@ enum TargetAllocTy : int32_t {
121121
TARGET_ALLOC_DEFAULT
122122
};
123123

124-
/// This struct contains all of the arguments to a target kernel region launch.
125-
struct KernelArgsTy {
126-
uint32_t Version; // Version of this struct for ABI compatibility.
127-
uint32_t NumArgs; // Number of arguments in each input pointer.
128-
void **ArgBasePtrs; // Base pointer of each argument (e.g. a struct).
129-
void **ArgPtrs; // Pointer to the argument data.
130-
int64_t *ArgSizes; // Size of the argument data in bytes.
131-
int64_t *ArgTypes; // Type of the data (e.g. to / from).
132-
void **ArgNames; // Name of the data for debugging, possibly null.
133-
void **ArgMappers; // User-defined mappers, possibly null.
134-
uint64_t Tripcount; // Tripcount for the teams / distribute loop, 0 otherwise.
135-
struct {
136-
uint64_t NoWait : 1; // Was this kernel spawned with a `nowait` clause.
137-
uint64_t Unused : 63;
138-
} Flags;
139-
uint32_t NumTeams[3]; // The number of teams (for x,y,z dimension).
140-
uint32_t ThreadLimit[3]; // The number of threads (for x,y,z dimension).
141-
uint32_t DynCGroupMem; // Amount of dynamic cgroup memory requested.
142-
};
143-
static_assert(sizeof(KernelArgsTy().Flags) == sizeof(uint64_t),
144-
"Invalid struct size");
145-
static_assert(sizeof(KernelArgsTy) == (8 * sizeof(int32_t) + 3 * sizeof(int64_t) + 4 * sizeof(void**) + 2 * sizeof(int64_t*)),
146-
"Invalid struct size");
147124
inline KernelArgsTy CTorDTorKernelArgs = {1, 0, nullptr, nullptr,
148125
nullptr, nullptr, nullptr, nullptr,
149126
0, {0,0}, {1, 0, 0}, {1, 0, 0}, 0};

openmp/libomptarget/src/device.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,8 @@ void DeviceTy::init() {
542542
"LIBOMPTARGET_RR_SAVE_OUTPUT", false);
543543

544544
uint64_t ReqPtrArgOffset;
545-
RTL->activate_record_replay(RTLDeviceID, 0, nullptr, true,
546-
OMPX_ReplaySaveOutput, ReqPtrArgOffset);
545+
RTL->initialize_record_replay(RTLDeviceID, 0, nullptr, true,
546+
OMPX_ReplaySaveOutput, ReqPtrArgOffset);
547547
}
548548

549549
IsInit = true;
@@ -565,7 +565,7 @@ int32_t DeviceTy::initOnce() {
565565
}
566566

567567
// Load binary to device.
568-
__tgt_target_table *DeviceTy::loadBinary(void *Img) {
568+
__tgt_target_table *DeviceTy::loadBinary(__tgt_device_image *Img) {
569569
std::lock_guard<decltype(RTL->Mtx)> LG(RTL->Mtx);
570570
return RTL->load_binary(RTLDeviceID, Img);
571571
}
@@ -702,8 +702,7 @@ int32_t DeviceTy::notifyDataUnmapped(void *HstPtr) {
702702

703703
// Run region on device
704704
int32_t DeviceTy::launchKernel(void *TgtEntryPtr, void **TgtVarsPtr,
705-
ptrdiff_t *TgtOffsets,
706-
const KernelArgsTy &KernelArgs,
705+
ptrdiff_t *TgtOffsets, KernelArgsTy &KernelArgs,
707706
AsyncInfoTy &AsyncInfo) {
708707
return RTL->launch_kernel(RTLDeviceID, TgtEntryPtr, TgtVarsPtr, TgtOffsets,
709708
&KernelArgs, AsyncInfo);

openmp/libomptarget/src/omptarget.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1733,9 +1733,9 @@ int target(ident_t *Loc, DeviceTy &Device, void *HostPtr,
17331733
int target_activate_rr(DeviceTy &Device, uint64_t MemorySize, void *VAddr,
17341734
bool IsRecord, bool SaveOutput,
17351735
uint64_t &ReqPtrArgOffset) {
1736-
return Device.RTL->activate_record_replay(Device.DeviceID, MemorySize, VAddr,
1737-
IsRecord, SaveOutput,
1738-
ReqPtrArgOffset);
1736+
return Device.RTL->initialize_record_replay(Device.DeviceID, MemorySize,
1737+
VAddr, IsRecord, SaveOutput,
1738+
ReqPtrArgOffset);
17391739
}
17401740

17411741
/// Executes a kernel using pre-recorded information for loading to

openmp/libomptarget/src/rtl.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,6 @@ bool PluginAdaptorManagerTy::attemptLoadRTL(const std::string &RTLName, PluginAd
227227
DynLibrary->getAddressOfSymbol("__tgt_rtl_sync_event");
228228
*((void **)&RTL.destroy_event) =
229229
DynLibrary->getAddressOfSymbol("__tgt_rtl_destroy_event");
230-
*((void **)&RTL.release_async_info) =
231-
DynLibrary->getAddressOfSymbol("__tgt_rtl_release_async_info");
232230
*((void **)&RTL.init_async_info) =
233231
DynLibrary->getAddressOfSymbol("__tgt_rtl_init_async_info");
234232
*((void **)&RTL.init_device_info) =
@@ -245,7 +243,7 @@ bool PluginAdaptorManagerTy::attemptLoadRTL(const std::string &RTLName, PluginAd
245243
DynLibrary->getAddressOfSymbol("__tgt_rtl_set_device_offset");
246244

247245
// Record Replay RTL
248-
*((void **)&RTL.activate_record_replay) =
246+
*((void **)&RTL.initialize_record_replay) =
249247
DynLibrary->getAddressOfSymbol("__tgt_rtl_initialize_record_replay");
250248

251249
RTL.LibraryHandler = std::move(DynLibrary);

0 commit comments

Comments
 (0)