Skip to content

Commit 9a0a28f

Browse files
authored
[Libomptarget] Rework Record & Replay to be a plugin member (#88928)
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 0a789ea commit 9a0a28f

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

openmp/libomptarget/plugins-nextgen/common/include/PluginInterface.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
#include "llvm/Support/raw_ostream.h"
4646
#include "llvm/TargetParser/Triple.h"
4747

48+
struct RecordReplayTy;
49+
4850
namespace llvm {
4951
namespace omp {
5052
namespace target {
@@ -1031,6 +1033,12 @@ struct GenericPluginTy {
10311033
return *RPCServer;
10321034
}
10331035

1036+
/// Get a reference to the R&R interface for this plugin.
1037+
RecordReplayTy &getRecordAndReplay() const {
1038+
assert(RecordReplay && "R&R not initialized");
1039+
return *RecordReplay;
1040+
}
1041+
10341042
/// Get the OpenMP requires flags set for this plugin.
10351043
int64_t getRequiresFlags() const { return RequiresFlags; }
10361044

@@ -1220,6 +1228,9 @@ struct GenericPluginTy {
12201228

12211229
/// The interface between the plugin and the GPU for host services.
12221230
RPCServerTy *RPCServer;
1231+
1232+
/// The interface into the record-and-replay functionality.
1233+
RecordReplayTy *RecordReplay;
12231234
};
12241235

12251236
namespace Plugin {

openmp/libomptarget/plugins-nextgen/common/src/PluginInterface.cpp

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,6 @@ struct RecordReplayTy {
362362
}
363363
};
364364

365-
static RecordReplayTy RecordReplay;
366-
367365
// Extract the mapping of host function pointers to device function pointers
368366
// from the entry table. Functions marked as 'indirect' in OpenMP will have
369367
// offloading entries generated for them which map the host's function pointer
@@ -473,7 +471,8 @@ GenericKernelTy::getKernelLaunchEnvironment(
473471
// Ctor/Dtor have no arguments, replaying uses the original kernel launch
474472
// environment. Older versions of the compiler do not generate a kernel
475473
// launch environment.
476-
if (isCtorOrDtor() || RecordReplay.isReplaying() ||
474+
if (isCtorOrDtor() ||
475+
GenericDevice.Plugin.getRecordAndReplay().isReplaying() ||
477476
Version < OMP_KERNEL_ARG_MIN_VERSION_WITH_DYN_PTR)
478477
return nullptr;
479478

@@ -562,6 +561,7 @@ Error GenericKernelTy::launch(GenericDeviceTy &GenericDevice, void **ArgPtrs,
562561

563562
// Record the kernel description after we modified the argument count and num
564563
// blocks/threads.
564+
RecordReplayTy &RecordReplay = GenericDevice.Plugin.getRecordAndReplay();
565565
if (RecordReplay.isRecording()) {
566566
RecordReplay.saveImage(getName(), getImage());
567567
RecordReplay.saveKernelInput(getName(), getImage());
@@ -839,9 +839,6 @@ Error GenericDeviceTy::deinit(GenericPluginTy &Plugin) {
839839
delete MemoryManager;
840840
MemoryManager = nullptr;
841841

842-
if (RecordReplay.isRecordingOrReplaying())
843-
RecordReplay.deinit();
844-
845842
if (RPCServer)
846843
if (auto Err = RPCServer->deinitDevice(*this))
847844
return Err;
@@ -858,6 +855,7 @@ Error GenericDeviceTy::deinit(GenericPluginTy &Plugin) {
858855

859856
return deinitImpl();
860857
}
858+
861859
Expected<DeviceImageTy *>
862860
GenericDeviceTy::loadBinary(GenericPluginTy &Plugin,
863861
const __tgt_device_image *InputTgtImage) {
@@ -892,7 +890,8 @@ GenericDeviceTy::loadBinary(GenericPluginTy &Plugin,
892890
return std::move(Err);
893891

894892
// Setup the global device memory pool if needed.
895-
if (!RecordReplay.isReplaying() && shouldSetupDeviceMemoryPool()) {
893+
if (!Plugin.getRecordAndReplay().isReplaying() &&
894+
shouldSetupDeviceMemoryPool()) {
896895
uint64_t HeapSize;
897896
auto SizeOrErr = getDeviceHeapSize(HeapSize);
898897
if (SizeOrErr) {
@@ -1307,8 +1306,8 @@ Expected<void *> GenericDeviceTy::dataAlloc(int64_t Size, void *HostPtr,
13071306
TargetAllocTy Kind) {
13081307
void *Alloc = nullptr;
13091308

1310-
if (RecordReplay.isRecordingOrReplaying())
1311-
return RecordReplay.alloc(Size);
1309+
if (Plugin.getRecordAndReplay().isRecordingOrReplaying())
1310+
return Plugin.getRecordAndReplay().alloc(Size);
13121311

13131312
switch (Kind) {
13141313
case TARGET_ALLOC_DEFAULT:
@@ -1344,7 +1343,7 @@ Expected<void *> GenericDeviceTy::dataAlloc(int64_t Size, void *HostPtr,
13441343

13451344
Error GenericDeviceTy::dataDelete(void *TgtPtr, TargetAllocTy Kind) {
13461345
// Free is a noop when recording or replaying.
1347-
if (RecordReplay.isRecordingOrReplaying())
1346+
if (Plugin.getRecordAndReplay().isRecordingOrReplaying())
13481347
return Plugin::success();
13491348

13501349
int Res;
@@ -1396,6 +1395,7 @@ Error GenericDeviceTy::launchKernel(void *EntryPtr, void **ArgPtrs,
13961395
ptrdiff_t *ArgOffsets,
13971396
KernelArgsTy &KernelArgs,
13981397
__tgt_async_info *AsyncInfo) {
1398+
RecordReplayTy &RecordReplay = Plugin.getRecordAndReplay();
13991399
AsyncInfoWrapperTy AsyncInfoWrapper(
14001400
*this, RecordReplay.isRecordingOrReplaying() ? nullptr : AsyncInfo);
14011401

@@ -1495,6 +1495,9 @@ Error GenericPluginTy::init() {
14951495
RPCServer = new RPCServerTy(*this);
14961496
assert(RPCServer && "Invalid RPC server");
14971497

1498+
RecordReplay = new RecordReplayTy();
1499+
assert(RecordReplay && "Invalid Record and Replay handler");
1500+
14981501
return Plugin::success();
14991502
}
15001503

@@ -1508,13 +1511,19 @@ Error GenericPluginTy::deinit() {
15081511
assert(!Devices[DeviceId] && "Device was not deinitialized");
15091512
}
15101513

1514+
if (RecordReplay && RecordReplay->isRecordingOrReplaying())
1515+
RecordReplay->deinit();
1516+
15111517
// There is no global handler if no device is available.
15121518
if (GlobalHandler)
15131519
delete GlobalHandler;
15141520

15151521
if (RPCServer)
15161522
delete RPCServer;
15171523

1524+
if (RecordReplay)
1525+
delete RecordReplay;
1526+
15181527
// Perform last deinitializations on the plugin.
15191528
return deinitImpl();
15201529
}
@@ -1630,12 +1639,12 @@ int32_t GenericPluginTy::initialize_record_replay(int32_t DeviceId,
16301639
isRecord ? RecordReplayTy::RRStatusTy::RRRecording
16311640
: RecordReplayTy::RRStatusTy::RRReplaying;
16321641

1633-
if (auto Err = RecordReplay.init(&Device, MemorySize, VAddr, Status,
1634-
SaveOutput, ReqPtrArgOffset)) {
1642+
if (auto Err = RecordReplay->init(&Device, MemorySize, VAddr, Status,
1643+
SaveOutput, ReqPtrArgOffset)) {
16351644
REPORT("WARNING RR did not intialize RR-properly with %lu bytes"
16361645
"(Error: %s)\n",
16371646
MemorySize, toString(std::move(Err)).data());
1638-
RecordReplay.setStatus(RecordReplayTy::RRStatusTy::RRDeactivated);
1647+
RecordReplay->setStatus(RecordReplayTy::RRStatusTy::RRDeactivated);
16391648

16401649
if (!isRecord) {
16411650
return OFFLOAD_FAIL;
@@ -1984,8 +1993,8 @@ int32_t GenericPluginTy::get_global(__tgt_device_binary Binary, uint64_t Size,
19841993
assert(DevicePtr && "Invalid device global's address");
19851994

19861995
// Save the loaded globals if we are recording.
1987-
if (RecordReplay.isRecording())
1988-
RecordReplay.addEntry(Name, Size, *DevicePtr);
1996+
if (getRecordAndReplay().isRecording())
1997+
getRecordAndReplay().addEntry(Name, Size, *DevicePtr);
19891998

19901999
return OFFLOAD_SUCCESS;
19912000
}

0 commit comments

Comments
 (0)