Skip to content

Commit b80b5f1

Browse files
authored
[OpenMP] Replace copy and paste code with instantiation (#73991)
1 parent 4e2216e commit b80b5f1

File tree

9 files changed

+116
-211
lines changed

9 files changed

+116
-211
lines changed

openmp/libomptarget/include/PluginManager.h

Lines changed: 7 additions & 80 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,12 @@ struct PluginAdaptorTy {
8038
std::string RTLName;
8139
#endif
8240

83-
// 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;
41+
#define PLUGIN_API_HANDLE(NAME, MANDATORY) \
42+
using NAME##_ty = decltype(__tgt_rtl_##NAME); \
43+
NAME##_ty *NAME = nullptr;
44+
45+
#include "Shared/PluginAPI.inc"
46+
#undef PLUGIN_API_HANDLE
12047

12148
// Are there images associated with this RTL.
12249
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
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//===-- Shared/PluginAPI.inc - Target independent plugin API ----*- C++ -*-===//
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+
// This file defines the names of the interface functions between target
10+
// independent offload runtime library and target dependent plugins.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
// No include guards!
15+
16+
PLUGIN_API_HANDLE(init_plugin, true);
17+
PLUGIN_API_HANDLE(is_valid_binary, true);
18+
PLUGIN_API_HANDLE(is_valid_binary_info, false);
19+
PLUGIN_API_HANDLE(is_data_exchangable, false);
20+
PLUGIN_API_HANDLE(number_of_devices, true);
21+
PLUGIN_API_HANDLE(init_device, true);
22+
PLUGIN_API_HANDLE(load_binary, true);
23+
PLUGIN_API_HANDLE(data_alloc, true);
24+
PLUGIN_API_HANDLE(data_submit, true);
25+
PLUGIN_API_HANDLE(data_submit_async, false);
26+
PLUGIN_API_HANDLE(data_retrieve, true);
27+
PLUGIN_API_HANDLE(data_retrieve_async, false);
28+
PLUGIN_API_HANDLE(data_exchange, false);
29+
PLUGIN_API_HANDLE(data_exchange_async, false);
30+
PLUGIN_API_HANDLE(data_delete, true);
31+
PLUGIN_API_HANDLE(launch_kernel, true);
32+
PLUGIN_API_HANDLE(init_requires, false);
33+
PLUGIN_API_HANDLE(synchronize, false);
34+
PLUGIN_API_HANDLE(query_async, false);
35+
PLUGIN_API_HANDLE(supports_empty_images, false);
36+
PLUGIN_API_HANDLE(set_info_flag, false);
37+
PLUGIN_API_HANDLE(print_device_info, false);
38+
PLUGIN_API_HANDLE(create_event, false);
39+
PLUGIN_API_HANDLE(record_event, false);
40+
PLUGIN_API_HANDLE(wait_event, false);
41+
PLUGIN_API_HANDLE(sync_event, false);
42+
PLUGIN_API_HANDLE(destroy_event, false);
43+
PLUGIN_API_HANDLE(init_async_info, false);
44+
PLUGIN_API_HANDLE(init_device_info, false);
45+
PLUGIN_API_HANDLE(data_lock, false);
46+
PLUGIN_API_HANDLE(data_unlock, false);
47+
PLUGIN_API_HANDLE(data_notify_mapped, false);
48+
PLUGIN_API_HANDLE(data_notify_unmapped, false);
49+
PLUGIN_API_HANDLE(set_device_offset, false);
50+
PLUGIN_API_HANDLE(initialize_record_replay, false);

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

0 commit comments

Comments
 (0)