Skip to content

Commit c0b49d8

Browse files
committed
[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 20d653f commit c0b49d8

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

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

Lines changed: 11 additions & 5 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 {
@@ -1286,11 +1297,6 @@ class PluginTy {
12861297
static Error deinit() {
12871298
assert(SpecificPlugin && "Plugin no longer valid");
12881299

1289-
for (int32_t DevNo = 0, NumDev = SpecificPlugin->getNumDevices();
1290-
DevNo < NumDev; ++DevNo)
1291-
if (auto Err = SpecificPlugin->deinitDevice(DevNo))
1292-
return Err;
1293-
12941300
// Deinitialize the plugin.
12951301
if (auto Err = SpecificPlugin->deinit())
12961302
return Err;

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

Lines changed: 23 additions & 14 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,8 +839,8 @@ Error GenericDeviceTy::deinit(GenericPluginTy &Plugin) {
839839
delete MemoryManager;
840840
MemoryManager = nullptr;
841841

842-
if (RecordReplay.isRecordingOrReplaying())
843-
RecordReplay.deinit();
842+
if (Plugin.getRecordAndReplay().isRecordingOrReplaying())
843+
Plugin.getRecordAndReplay().deinit();
844844

845845
if (RPCServer)
846846
if (auto Err = RPCServer->deinitDevice(*this))
@@ -858,6 +858,7 @@ Error GenericDeviceTy::deinit(GenericPluginTy &Plugin) {
858858

859859
return deinitImpl();
860860
}
861+
861862
Expected<DeviceImageTy *>
862863
GenericDeviceTy::loadBinary(GenericPluginTy &Plugin,
863864
const __tgt_device_image *InputTgtImage) {
@@ -892,7 +893,8 @@ GenericDeviceTy::loadBinary(GenericPluginTy &Plugin,
892893
return std::move(Err);
893894

894895
// Setup the global device memory pool if needed.
895-
if (!RecordReplay.isReplaying() && shouldSetupDeviceMemoryPool()) {
896+
if (!Plugin.getRecordAndReplay().isReplaying() &&
897+
shouldSetupDeviceMemoryPool()) {
896898
uint64_t HeapSize;
897899
auto SizeOrErr = getDeviceHeapSize(HeapSize);
898900
if (SizeOrErr) {
@@ -1307,8 +1309,8 @@ Expected<void *> GenericDeviceTy::dataAlloc(int64_t Size, void *HostPtr,
13071309
TargetAllocTy Kind) {
13081310
void *Alloc = nullptr;
13091311

1310-
if (RecordReplay.isRecordingOrReplaying())
1311-
return RecordReplay.alloc(Size);
1312+
if (Plugin.getRecordAndReplay().isRecordingOrReplaying())
1313+
return Plugin.getRecordAndReplay().alloc(Size);
13121314

13131315
switch (Kind) {
13141316
case TARGET_ALLOC_DEFAULT:
@@ -1344,7 +1346,7 @@ Expected<void *> GenericDeviceTy::dataAlloc(int64_t Size, void *HostPtr,
13441346

13451347
Error GenericDeviceTy::dataDelete(void *TgtPtr, TargetAllocTy Kind) {
13461348
// Free is a noop when recording or replaying.
1347-
if (RecordReplay.isRecordingOrReplaying())
1349+
if (Plugin.getRecordAndReplay().isRecordingOrReplaying())
13481350
return Plugin::success();
13491351

13501352
int Res;
@@ -1396,6 +1398,7 @@ Error GenericDeviceTy::launchKernel(void *EntryPtr, void **ArgPtrs,
13961398
ptrdiff_t *ArgOffsets,
13971399
KernelArgsTy &KernelArgs,
13981400
__tgt_async_info *AsyncInfo) {
1401+
RecordReplayTy &RecordReplay = Plugin.getRecordAndReplay();
13991402
AsyncInfoWrapperTy AsyncInfoWrapper(
14001403
*this, RecordReplay.isRecordingOrReplaying() ? nullptr : AsyncInfo);
14011404

@@ -1495,6 +1498,9 @@ Error GenericPluginTy::init() {
14951498
RPCServer = new RPCServerTy(*this);
14961499
assert(RPCServer && "Invalid RPC server");
14971500

1501+
RecordReplay = new RecordReplayTy();
1502+
assert(RecordReplay && "Invalid Record and Replay handler");
1503+
14981504
return Plugin::success();
14991505
}
15001506

@@ -1515,6 +1521,9 @@ Error GenericPluginTy::deinit() {
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 = getRecordAndReplay().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)