@@ -49,40 +49,87 @@ struct RecordReplayTy {
49
49
void *MemoryStart;
50
50
void *MemoryPtr;
51
51
size_t MemorySize;
52
+ size_t TotalSize;
52
53
GenericDeviceTy *Device;
53
54
std::mutex AllocationLock;
54
55
55
56
RRStatusTy Status;
56
57
bool ReplaySaveOutput;
57
- uint64_t DeviceMemorySize;
58
-
59
- // Record/replay pre-allocates the largest possible device memory using the
60
- // default kind.
61
- // TODO: Expand allocation to include other kinds (device, host, shared) and
62
- // possibly use a MemoryManager to track (de-)allocations for
63
- // storing/retrieving when recording/replaying.
64
- Error preallocateDeviceMemory (uint64_t DeviceMemorySize) {
65
- // Pre-allocate memory on device. Starts with 64GB and subtracts in steps
66
- // of 1GB until allocation succeeds.
67
- const size_t MAX_MEMORY_ALLOCATION = DeviceMemorySize;
58
+
59
+ void *suggestAddress (uint64_t MaxMemoryAllocation) {
60
+ // Get a valid pointer address for this system
61
+ void *Addr =
62
+ Device->allocate (1024 , /* HstPtr */ nullptr , TARGET_ALLOC_DEFAULT);
63
+ Device->free (Addr);
64
+ // Align Address to MaxMemoryAllocation
65
+ Addr = (void *)alignPtr ((Addr), MaxMemoryAllocation);
66
+ return Addr;
67
+ }
68
+
69
+ Error preAllocateVAMemory (uint64_t MaxMemoryAllocation, void *VAddr) {
70
+ size_t ASize = MaxMemoryAllocation;
71
+
72
+ if (!VAddr && isRecording ())
73
+ VAddr = suggestAddress (MaxMemoryAllocation);
74
+
75
+ DP (" Request %ld bytes allocated at %p\n " , MaxMemoryAllocation, VAddr);
76
+
77
+ if (auto Err = Device->memoryVAMap (&MemoryStart, VAddr, &ASize))
78
+ return Err;
79
+
80
+ if (isReplaying () && VAddr != MemoryStart) {
81
+ return Plugin::error (" Record-Replay cannot assign the"
82
+ " requested recorded address (%p, %p)" ,
83
+ VAddr, MemoryStart);
84
+ }
85
+
86
+ INFO (OMP_INFOTYPE_PLUGIN_KERNEL, Device->getDeviceId (),
87
+ " Allocated %" PRIu64 " bytes at %p for replay.\n " , ASize, MemoryStart);
88
+
89
+ MemoryPtr = MemoryStart;
90
+ MemorySize = 0 ;
91
+ TotalSize = ASize;
92
+ return Plugin::success ();
93
+ }
94
+
95
+ Error preAllocateHeuristic (uint64_t MaxMemoryAllocation, void *VAddr) {
96
+ const size_t MAX_MEMORY_ALLOCATION = MaxMemoryAllocation;
68
97
constexpr size_t STEP = 1024 * 1024 * 1024ULL ;
69
98
MemoryStart = nullptr ;
70
- for (size_t Try = MAX_MEMORY_ALLOCATION; Try > 0 ; Try -= STEP) {
71
- MemoryStart =
72
- Device-> allocate (Try, /* HstPtr */ nullptr , TARGET_ALLOC_DEFAULT);
99
+ for (TotalSize = MAX_MEMORY_ALLOCATION; TotalSize > 0 ; TotalSize -= STEP) {
100
+ MemoryStart = Device-> allocate (TotalSize, /* HstPtr */ nullptr ,
101
+ TARGET_ALLOC_DEFAULT);
73
102
if (MemoryStart)
74
103
break ;
75
104
}
76
105
106
+ INFO (OMP_INFOTYPE_PLUGIN_KERNEL, Device->getDeviceId (),
107
+ " Allocated %" PRIu64 " bytes at %p for replay.\n " , TotalSize,
108
+ MemoryStart);
109
+
77
110
if (!MemoryStart)
78
111
return Plugin::error (" Allocating record/replay memory" );
79
112
113
+ if (VAddr && VAddr != MemoryStart)
114
+ return Plugin::error (" Cannot allocate recorded address" );
115
+
80
116
MemoryPtr = MemoryStart;
81
117
MemorySize = 0 ;
82
118
83
119
return Plugin::success ();
84
120
}
85
121
122
+ Error preallocateDeviceMemory (uint64_t DeviceMemorySize, void *ReqVAddr) {
123
+ if (Device->supportVAManagement ())
124
+ return preAllocateVAMemory (DeviceMemorySize, ReqVAddr);
125
+
126
+ uint64_t DevMemSize;
127
+ if (Device->getDeviceMemorySize (DevMemSize))
128
+ return Plugin::error (" Cannot determine Device Memory Size" );
129
+
130
+ return preAllocateHeuristic (DevMemSize, ReqVAddr);
131
+ }
132
+
86
133
void dumpDeviceMemory (StringRef Filename) {
87
134
ErrorOr<std::unique_ptr<WritableMemoryBuffer>> DeviceMemoryMB =
88
135
WritableMemoryBuffer::getNewUninitMemBuffer (MemorySize);
@@ -114,8 +161,7 @@ struct RecordReplayTy {
114
161
bool isSaveOutputEnabled () const { return ReplaySaveOutput; }
115
162
116
163
RecordReplayTy ()
117
- : Status(RRStatusTy::RRDeactivated), ReplaySaveOutput(false ),
118
- DeviceMemorySize (-1 ) {}
164
+ : Status(RRStatusTy::RRDeactivated), ReplaySaveOutput(false ) {}
119
165
120
166
void saveImage (const char *Name, const DeviceImageTy &Image) {
121
167
SmallString<128 > ImageName = {Name, " .image" };
@@ -197,6 +243,7 @@ struct RecordReplayTy {
197
243
JsonKernelInfo[" LoopTripCount" ] = LoopTripCount;
198
244
JsonKernelInfo[" DeviceMemorySize" ] = MemorySize;
199
245
JsonKernelInfo[" DeviceId" ] = Device->getDeviceId ();
246
+ JsonKernelInfo[" BumpAllocVAStart" ] = (intptr_t )MemoryStart;
200
247
201
248
json::Array JsonArgPtrs;
202
249
for (int I = 0 ; I < NumArgs; ++I)
@@ -244,27 +291,33 @@ struct RecordReplayTy {
244
291
return Alloc;
245
292
}
246
293
247
- Error init (GenericDeviceTy *Device, uint64_t MemSize, RRStatusTy Status ,
248
- bool SaveOutput) {
294
+ Error init (GenericDeviceTy *Device, uint64_t MemSize, void *VAddr ,
295
+ RRStatusTy Status, bool SaveOutput) {
249
296
this ->Device = Device;
250
297
this ->Status = Status;
251
- this ->DeviceMemorySize = MemSize;
252
298
this ->ReplaySaveOutput = SaveOutput;
253
299
254
- if (auto Err = preallocateDeviceMemory (MemSize))
300
+ if (auto Err = preallocateDeviceMemory (MemSize, VAddr ))
255
301
return Err;
256
302
257
303
INFO (OMP_INFOTYPE_PLUGIN_KERNEL, Device->getDeviceId (),
258
304
" Record Replay Initialized (%p)"
259
305
" as starting address, %lu Memory Size"
260
306
" and set on status %s\n " ,
261
- MemoryStart, MemSize ,
307
+ MemoryStart, TotalSize ,
262
308
Status == RRStatusTy::RRRecording ? " Recording" : " Replaying" );
263
309
264
310
return Plugin::success ();
265
311
}
266
312
267
- void deinit () { Device->free (MemoryStart); }
313
+ void deinit () {
314
+ if (Device->supportVAManagement ()) {
315
+ if (auto Err = Device->memoryVAUnMap (MemoryStart, TotalSize))
316
+ report_fatal_error (" Error on releasing virtual memory space" );
317
+ } else {
318
+ Device->free (MemoryStart);
319
+ }
320
+ }
268
321
269
322
} RecordReplay;
270
323
@@ -1184,6 +1237,19 @@ Error GenericDeviceTy::queryAsync(__tgt_async_info *AsyncInfo) {
1184
1237
return queryAsyncImpl (*AsyncInfo);
1185
1238
}
1186
1239
1240
+ Error GenericDeviceTy::memoryVAMap (void **Addr, void *VAddr, size_t *RSize) {
1241
+ return Plugin::error (" Device does not suppport VA Management" );
1242
+ }
1243
+
1244
+ Error GenericDeviceTy::memoryVAUnMap (void *VAddr, size_t Size) {
1245
+ return Plugin::error (" Device does not suppport VA Management" );
1246
+ }
1247
+
1248
+ Error GenericDeviceTy::getDeviceMemorySize (uint64_t &DSize) {
1249
+ return Plugin::error (
1250
+ " Mising getDeviceMemorySize impelmentation (required by RR-heuristic" );
1251
+ }
1252
+
1187
1253
Expected<void *> GenericDeviceTy::dataAlloc (int64_t Size, void *HostPtr,
1188
1254
TargetAllocTy Kind) {
1189
1255
void *Alloc = nullptr ;
@@ -1552,16 +1618,17 @@ int32_t __tgt_rtl_is_data_exchangable(int32_t SrcDeviceId,
1552
1618
return Plugin::get ().isDataExchangable (SrcDeviceId, DstDeviceId);
1553
1619
}
1554
1620
1555
- int32_t __tgt_rtl_initialize_record_replay (int32_t DeviceId,
1556
- uint64_t MemorySize , bool isRecord,
1621
+ int32_t __tgt_rtl_initialize_record_replay (int32_t DeviceId, int64_t MemorySize,
1622
+ void *VAddr , bool isRecord,
1557
1623
bool SaveOutput) {
1558
1624
GenericPluginTy &Plugin = Plugin::get ();
1559
1625
GenericDeviceTy &Device = Plugin.getDevice (DeviceId);
1560
1626
RecordReplayTy::RRStatusTy Status =
1561
1627
isRecord ? RecordReplayTy::RRStatusTy::RRRecording
1562
1628
: RecordReplayTy::RRStatusTy::RRReplaying;
1563
1629
1564
- if (auto Err = RecordReplay.init (&Device, MemorySize, Status, SaveOutput)) {
1630
+ if (auto Err =
1631
+ RecordReplay.init (&Device, MemorySize, VAddr, Status, SaveOutput)) {
1565
1632
REPORT (" WARNING RR did not intialize RR-properly with %lu bytes"
1566
1633
" (Error: %s)\n " ,
1567
1634
MemorySize, toString (std::move (Err)).data ());
0 commit comments