Skip to content

Commit 293c4f5

Browse files
committed
[Libomptarget] Rework Record & Replay to be a plugin member
Summary: Previously, the R&R support was global state initialized by a global constructor. This is bad because it prevents us from adequately constraining the lifetime of the library. Additionally, we want to minimize the amount of global state floating around. This patch moves the R&R support into a plugin member like everything else. This means there will be multiple copies of the R&R implementation floating around, but this was already the case given the fact that we currently handle everything with dynamic libraries.
1 parent 033fa81 commit 293c4f5

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

offload/plugins-nextgen/common/include/PluginInterface.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ namespace plugin {
5454
struct GenericPluginTy;
5555
struct GenericKernelTy;
5656
struct GenericDeviceTy;
57+
struct RecordReplayTy;
5758

5859
/// Class that wraps the __tgt_async_info to simply its usage. In case the
5960
/// object is constructed without a valid __tgt_async_info, the object will use
@@ -958,7 +959,8 @@ struct GenericPluginTy {
958959

959960
/// Construct a plugin instance.
960961
GenericPluginTy(Triple::ArchType TA)
961-
: GlobalHandler(nullptr), JIT(TA), RPCServer(nullptr) {}
962+
: GlobalHandler(nullptr), JIT(TA), RPCServer(nullptr),
963+
RecordReplay(nullptr) {}
962964

963965
virtual ~GenericPluginTy() {}
964966

@@ -1027,6 +1029,12 @@ struct GenericPluginTy {
10271029
return *RPCServer;
10281030
}
10291031

1032+
/// Get a reference to the record and replay interface for the plugin.
1033+
RecordReplayTy &getRecordReplay() {
1034+
assert(RecordReplay && "RR interface not initialized");
1035+
return *RecordReplay;
1036+
}
1037+
10301038
/// Initialize a device within the plugin.
10311039
Error initDevice(int32_t DeviceId);
10321040

@@ -1204,6 +1212,9 @@ struct GenericPluginTy {
12041212

12051213
/// The interface between the plugin and the GPU for host services.
12061214
RPCServerTy *RPCServer;
1215+
1216+
/// The interface between the plugin and the GPU for host services.
1217+
RecordReplayTy *RecordReplay;
12071218
};
12081219

12091220
namespace Plugin {

offload/plugins-nextgen/common/src/PluginInterface.cpp

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ using namespace target;
3939
using namespace plugin;
4040

4141
// TODO: Fix any thread safety issues for multi-threaded kernel recording.
42+
namespace llvm::omp::target::plugin {
4243
struct RecordReplayTy {
4344

4445
// Describes the state of the record replay mechanism.
@@ -358,8 +359,7 @@ struct RecordReplayTy {
358359
}
359360
}
360361
};
361-
362-
static RecordReplayTy RecordReplay;
362+
} // namespace llvm::omp::target::plugin
363363

364364
// Extract the mapping of host function pointers to device function pointers
365365
// from the entry table. Functions marked as 'indirect' in OpenMP will have
@@ -470,7 +470,7 @@ GenericKernelTy::getKernelLaunchEnvironment(
470470
// Ctor/Dtor have no arguments, replaying uses the original kernel launch
471471
// environment. Older versions of the compiler do not generate a kernel
472472
// launch environment.
473-
if (RecordReplay.isReplaying() ||
473+
if (GenericDevice.Plugin.getRecordReplay().isReplaying() ||
474474
Version < OMP_KERNEL_ARG_MIN_VERSION_WITH_DYN_PTR)
475475
return nullptr;
476476

@@ -559,6 +559,7 @@ Error GenericKernelTy::launch(GenericDeviceTy &GenericDevice, void **ArgPtrs,
559559

560560
// Record the kernel description after we modified the argument count and num
561561
// blocks/threads.
562+
RecordReplayTy &RecordReplay = GenericDevice.Plugin.getRecordReplay();
562563
if (RecordReplay.isRecording()) {
563564
RecordReplay.saveImage(getName(), getImage());
564565
RecordReplay.saveKernelInput(getName(), getImage());
@@ -833,6 +834,7 @@ Error GenericDeviceTy::deinit(GenericPluginTy &Plugin) {
833834
delete MemoryManager;
834835
MemoryManager = nullptr;
835836

837+
RecordReplayTy &RecordReplay = Plugin.getRecordReplay();
836838
if (RecordReplay.isRecordingOrReplaying())
837839
RecordReplay.deinit();
838840

@@ -886,7 +888,8 @@ GenericDeviceTy::loadBinary(GenericPluginTy &Plugin,
886888
return std::move(Err);
887889

888890
// Setup the global device memory pool if needed.
889-
if (!RecordReplay.isReplaying() && shouldSetupDeviceMemoryPool()) {
891+
if (!Plugin.getRecordReplay().isReplaying() &&
892+
shouldSetupDeviceMemoryPool()) {
890893
uint64_t HeapSize;
891894
auto SizeOrErr = getDeviceHeapSize(HeapSize);
892895
if (SizeOrErr) {
@@ -1301,8 +1304,8 @@ Expected<void *> GenericDeviceTy::dataAlloc(int64_t Size, void *HostPtr,
13011304
TargetAllocTy Kind) {
13021305
void *Alloc = nullptr;
13031306

1304-
if (RecordReplay.isRecordingOrReplaying())
1305-
return RecordReplay.alloc(Size);
1307+
if (Plugin.getRecordReplay().isRecordingOrReplaying())
1308+
return Plugin.getRecordReplay().alloc(Size);
13061309

13071310
switch (Kind) {
13081311
case TARGET_ALLOC_DEFAULT:
@@ -1338,7 +1341,7 @@ Expected<void *> GenericDeviceTy::dataAlloc(int64_t Size, void *HostPtr,
13381341

13391342
Error GenericDeviceTy::dataDelete(void *TgtPtr, TargetAllocTy Kind) {
13401343
// Free is a noop when recording or replaying.
1341-
if (RecordReplay.isRecordingOrReplaying())
1344+
if (Plugin.getRecordReplay().isRecordingOrReplaying())
13421345
return Plugin::success();
13431346

13441347
int Res;
@@ -1405,7 +1408,8 @@ Error GenericDeviceTy::launchKernel(void *EntryPtr, void **ArgPtrs,
14051408
KernelArgsTy &KernelArgs,
14061409
__tgt_async_info *AsyncInfo) {
14071410
AsyncInfoWrapperTy AsyncInfoWrapper(
1408-
*this, RecordReplay.isRecordingOrReplaying() ? nullptr : AsyncInfo);
1411+
*this,
1412+
Plugin.getRecordReplay().isRecordingOrReplaying() ? nullptr : AsyncInfo);
14091413

14101414
GenericKernelTy &GenericKernel =
14111415
*reinterpret_cast<GenericKernelTy *>(EntryPtr);
@@ -1416,6 +1420,7 @@ Error GenericDeviceTy::launchKernel(void *EntryPtr, void **ArgPtrs,
14161420
// 'finalize' here to guarantee next record-replay actions are in-sync
14171421
AsyncInfoWrapper.finalize(Err);
14181422

1423+
RecordReplayTy &RecordReplay = Plugin.getRecordReplay();
14191424
if (RecordReplay.isRecordingOrReplaying() &&
14201425
RecordReplay.isSaveOutputEnabled())
14211426
RecordReplay.saveKernelOutputInfo(GenericKernel.getName());
@@ -1503,6 +1508,9 @@ Error GenericPluginTy::init() {
15031508
RPCServer = new RPCServerTy(*this);
15041509
assert(RPCServer && "Invalid RPC server");
15051510

1511+
RecordReplay = new RecordReplayTy();
1512+
assert(RecordReplay && "Invalid RR interface");
1513+
15061514
return Plugin::success();
15071515
}
15081516

@@ -1523,6 +1531,9 @@ Error GenericPluginTy::deinit() {
15231531
if (RPCServer)
15241532
delete RPCServer;
15251533

1534+
if (RecordReplay)
1535+
delete RecordReplay;
1536+
15261537
// Perform last deinitializations on the plugin.
15271538
return deinitImpl();
15281539
}
@@ -1633,12 +1644,12 @@ int32_t GenericPluginTy::initialize_record_replay(int32_t DeviceId,
16331644
isRecord ? RecordReplayTy::RRStatusTy::RRRecording
16341645
: RecordReplayTy::RRStatusTy::RRReplaying;
16351646

1636-
if (auto Err = RecordReplay.init(&Device, MemorySize, VAddr, Status,
1637-
SaveOutput, ReqPtrArgOffset)) {
1647+
if (auto Err = RecordReplay->init(&Device, MemorySize, VAddr, Status,
1648+
SaveOutput, ReqPtrArgOffset)) {
16381649
REPORT("WARNING RR did not intialize RR-properly with %lu bytes"
16391650
"(Error: %s)\n",
16401651
MemorySize, toString(std::move(Err)).data());
1641-
RecordReplay.setStatus(RecordReplayTy::RRStatusTy::RRDeactivated);
1652+
RecordReplay->setStatus(RecordReplayTy::RRStatusTy::RRDeactivated);
16421653

16431654
if (!isRecord) {
16441655
return OFFLOAD_FAIL;
@@ -1982,6 +1993,7 @@ int32_t GenericPluginTy::get_global(__tgt_device_binary Binary, uint64_t Size,
19821993
assert(DevicePtr && "Invalid device global's address");
19831994

19841995
// Save the loaded globals if we are recording.
1996+
RecordReplayTy &RecordReplay = Device.Plugin.getRecordReplay();
19851997
if (RecordReplay.isRecording())
19861998
RecordReplay.addEntry(Name, Size, *DevicePtr);
19871999

0 commit comments

Comments
 (0)