@@ -224,11 +224,19 @@ ur_result_t MsanShadowMemoryGPU::Destory() {
224
224
static ur_result_t Result = [this ]() {
225
225
auto Result = getContext ()->urDdiTable .VirtualMem .pfnFree (
226
226
Context, (const void *)ShadowBegin, GetShadowSize ());
227
+
227
228
if (PrivateShadowOffset != 0 ) {
228
229
UR_CALL (getContext ()->urDdiTable .USM .pfnFree (
229
230
Context, (void *)PrivateShadowOffset));
230
231
PrivateShadowOffset = 0 ;
231
232
}
233
+
234
+ if (PrivateBasePtr != 0 ) {
235
+ UR_CALL (getContext ()->urDdiTable .USM .pfnFree (Context,
236
+ (void *)PrivateBasePtr));
237
+ PrivateBasePtr = 0 ;
238
+ }
239
+
232
240
if (LocalShadowOffset != 0 ) {
233
241
UR_CALL (getContext ()->urDdiTable .USM .pfnFree (Context,
234
242
(void *)LocalShadowOffset));
@@ -409,45 +417,74 @@ ur_result_t MsanShadowMemoryGPU::AllocPrivateShadow(ur_queue_handle_t Queue,
409
417
uint64_t NumWI,
410
418
uint32_t NumWG, uptr *&Base,
411
419
uptr &Begin, uptr &End) {
412
- {
413
- const size_t Size = NumWI * sizeof (uptr);
414
- ur_usm_desc_t Properties{UR_STRUCTURE_TYPE_USM_DESC, nullptr ,
415
- UR_USM_ADVICE_FLAG_DEFAULT, sizeof (uptr)};
416
- UR_CALL (getContext ()->urDdiTable .USM .pfnDeviceAlloc (
417
- Context, Device, &Properties, nullptr , Size, (void **)&Base));
418
- }
420
+ // Trying to allocate private base array and private shadow, and any one of
421
+ // them fail to allocate would be a failure
422
+ static size_t LastPrivateBaseAllocedSize = 0 ;
423
+ static size_t LastPrivateShadowAllocedSize = 0 ;
424
+
425
+ try {
426
+ const size_t NewPrivateBaseSize = NumWI * sizeof (uptr);
427
+ if (NewPrivateBaseSize > LastPrivateBaseAllocedSize) {
428
+ if (PrivateBasePtr) {
429
+ UR_CALL_THROWS (getContext ()->urDdiTable .USM .pfnFree (
430
+ Context, (void *)PrivateBasePtr));
431
+ PrivateBasePtr = 0 ;
432
+ LastPrivateBaseAllocedSize = 0 ;
433
+ }
434
+
435
+ ur_usm_desc_t PrivateBaseProps{UR_STRUCTURE_TYPE_USM_DESC, nullptr ,
436
+ UR_USM_ADVICE_FLAG_DEFAULT, sizeof (uptr)};
437
+ UR_CALL_THROWS (getContext ()->urDdiTable .USM .pfnDeviceAlloc (
438
+ Context, Device, &PrivateBaseProps, nullptr , NewPrivateBaseSize,
439
+ (void **)&PrivateBasePtr));
440
+
441
+ // No need to clean the shadow base, their should be set by work item on
442
+ // launch
443
+
444
+ // FIXME: we should add private base to statistic
445
+ LastPrivateBaseAllocedSize = NewPrivateBaseSize;
446
+ }
447
+
448
+ const size_t NewPrivateShadowSize = NumWG * MSAN_PRIVATE_SIZE;
449
+ if (NewPrivateShadowSize > LastPrivateShadowAllocedSize) {
419
450
420
- {
421
- const size_t RequiredShadowSize = NumWG * MSAN_PRIVATE_SIZE;
422
- static size_t LastAllocedSize = 0 ;
423
- if (RequiredShadowSize > LastAllocedSize) {
424
- auto ContextInfo = getMsanInterceptor ()->getContextInfo (Context);
425
451
if (PrivateShadowOffset) {
426
- UR_CALL (getContext ()->urDdiTable .USM .pfnFree (
452
+ UR_CALL_THROWS (getContext ()->urDdiTable .USM .pfnFree (
427
453
Context, (void *)PrivateShadowOffset));
428
454
PrivateShadowOffset = 0 ;
429
- LastAllocedSize = 0 ;
455
+ LastPrivateShadowAllocedSize = 0 ;
430
456
}
431
457
432
- UR_CALL (getContext ()->urDdiTable .USM .pfnDeviceAlloc (
433
- Context, Device, nullptr , nullptr , RequiredShadowSize ,
458
+ UR_CALL_THROWS (getContext ()->urDdiTable .USM .pfnDeviceAlloc (
459
+ Context, Device, nullptr , nullptr , NewPrivateShadowSize ,
434
460
(void **)&PrivateShadowOffset));
461
+ LastPrivateShadowAllocedSize = NewPrivateShadowSize;
462
+ UR_CALL_THROWS (EnqueueUSMSet (Queue, (void *)PrivateShadowOffset, (char )0 ,
463
+ NewPrivateShadowSize));
464
+ }
435
465
436
- // Initialize shadow memory
437
- ur_result_t URes = EnqueueUSMSet (Queue, (void *)PrivateShadowOffset,
438
- (char )0 , RequiredShadowSize);
439
- if (URes != UR_RESULT_SUCCESS) {
440
- UR_CALL (getContext ()->urDdiTable .USM .pfnFree (
441
- Context, (void *)PrivateShadowOffset));
442
- PrivateShadowOffset = 0 ;
443
- LastAllocedSize = 0 ;
444
- }
466
+ Base = (uptr *)PrivateBasePtr;
467
+ Begin = PrivateShadowOffset;
468
+ End = PrivateShadowOffset + NewPrivateShadowSize - 1 ;
445
469
446
- LastAllocedSize = RequiredShadowSize;
470
+ } catch (ur_result_t &UrRes) {
471
+ assert (UrRes != UR_RESULT_SUCCESS);
472
+
473
+ if (PrivateBasePtr) {
474
+ UR_CALL_NOCHECK (getContext ()->urDdiTable .USM .pfnFree (
475
+ Context, (void *)PrivateBasePtr));
476
+ PrivateBasePtr = 0 ;
477
+ LastPrivateBaseAllocedSize = 0 ;
447
478
}
448
479
449
- Begin = PrivateShadowOffset;
450
- End = PrivateShadowOffset + RequiredShadowSize - 1 ;
480
+ if (PrivateShadowOffset) {
481
+ UR_CALL_NOCHECK (getContext ()->urDdiTable .USM .pfnFree (
482
+ Context, (void *)PrivateShadowOffset));
483
+ PrivateShadowOffset = 0 ;
484
+ LastPrivateShadowAllocedSize = 0 ;
485
+ }
486
+
487
+ return UrRes;
451
488
}
452
489
453
490
return UR_RESULT_SUCCESS;
0 commit comments