@@ -68,7 +68,9 @@ ur_result_t MsanInterceptor::allocateMemory(ur_context_handle_t Context,
68
68
69
69
*ResultPtr = Allocated;
70
70
71
- ContextInfo->MaxAllocatedSize = std::max (ContextInfo->MaxAllocatedSize , Size );
71
+ if (Type != AllocType::DEVICE_USM) {
72
+ ContextInfo->CleanShadowSize = std::max (ContextInfo->CleanShadowSize , Size );
73
+ }
72
74
73
75
// For host/shared usm, we only record the alloc size.
74
76
if (Type != AllocType::DEVICE_USM) {
@@ -138,15 +140,16 @@ ur_result_t MsanInterceptor::postLaunchKernel(ur_kernel_handle_t Kernel,
138
140
// FIXME: We must use block operation here, until we support
139
141
// urEventSetCallback
140
142
auto Result = getContext ()->urDdiTable .Queue .pfnFinish (Queue);
143
+ UR_CALL (LaunchInfo.Data .syncFromDevice (Queue));
141
144
142
145
if (Result == UR_RESULT_SUCCESS) {
143
- const auto &Report = LaunchInfo.Data -> Report ;
146
+ const auto &Report = LaunchInfo.Data . Host . Report ;
144
147
145
148
if (!Report.Flag ) {
146
149
return Result;
147
150
}
148
151
149
- ReportUsesUninitializedValue (LaunchInfo.Data -> Report , Kernel);
152
+ ReportUsesUninitializedValue (LaunchInfo.Data . Host . Report , Kernel);
150
153
151
154
exitWithErrors ();
152
155
}
@@ -286,8 +289,8 @@ MsanInterceptor::registerDeviceGlobals(ur_program_handle_t Program) {
286
289
MsanShadowMemoryDG2::IsDeviceUSM (GVInfo.Addr ))) {
287
290
UR_CALL (DeviceInfo->Shadow ->EnqueuePoisonShadow (Queue, GVInfo.Addr ,
288
291
GVInfo.Size , 0 ));
289
- ContextInfo->MaxAllocatedSize =
290
- std::max (ContextInfo->MaxAllocatedSize , GVInfo.Size );
292
+ ContextInfo->CleanShadowSize =
293
+ std::max (ContextInfo->CleanShadowSize , GVInfo.Size );
291
294
}
292
295
}
293
296
}
@@ -471,16 +474,20 @@ ur_result_t MsanInterceptor::prepareLaunch(
471
474
472
475
// Set LaunchInfo
473
476
auto ContextInfo = getContextInfo (LaunchInfo.Context );
474
- LaunchInfo.Data ->GlobalShadowOffset = DeviceInfo->Shadow ->ShadowBegin ;
475
- LaunchInfo.Data ->GlobalShadowOffsetEnd = DeviceInfo->Shadow ->ShadowEnd ;
477
+ LaunchInfo.Data .Host .GlobalShadowOffset = DeviceInfo->Shadow ->ShadowBegin ;
478
+ LaunchInfo.Data .Host .GlobalShadowOffsetEnd = DeviceInfo->Shadow ->ShadowEnd ;
479
+
480
+ LaunchInfo.Data .Host .DeviceTy = DeviceInfo->Type ;
481
+ LaunchInfo.Data .Host .Debug = getContext ()->Options .Debug ? 1 : 0 ;
476
482
477
- LaunchInfo. Data -> DeviceTy = DeviceInfo-> Type ;
478
- LaunchInfo. Data -> Debug = getContext ()-> Options . Debug ? 1 : 0 ;
483
+ // Clean shadow
484
+ // Its content is always zero, and is used for unsupport memory types
479
485
UR_CALL (getContext ()->urDdiTable .USM .pfnDeviceAlloc (
480
486
ContextInfo->Handle , DeviceInfo->Handle , nullptr , nullptr ,
481
- ContextInfo->MaxAllocatedSize , (void **)&LaunchInfo.Data ->CleanShadow ));
482
- UR_CALL (EnqueueUSMBlockingSet (Queue, (void *)LaunchInfo.Data ->CleanShadow , 0 ,
483
- ContextInfo->MaxAllocatedSize , 0 , nullptr ,
487
+ ContextInfo->CleanShadowSize ,
488
+ (void **)&LaunchInfo.Data .Host .CleanShadow ));
489
+ UR_CALL (EnqueueUSMBlockingSet (Queue, (void *)LaunchInfo.Data .Host .CleanShadow ,
490
+ 0 , ContextInfo->CleanShadowSize , 0 , nullptr ,
484
491
nullptr ));
485
492
486
493
if (LaunchInfo.LocalWorkSize .empty ()) {
@@ -510,8 +517,8 @@ ur_result_t MsanInterceptor::prepareLaunch(
510
517
// Write shadow memory offset for local memory
511
518
if (KernelInfo.IsCheckLocals ) {
512
519
if (DeviceInfo->Shadow ->AllocLocalShadow (
513
- Queue, NumWG, LaunchInfo.Data -> LocalShadowOffset ,
514
- LaunchInfo.Data -> LocalShadowOffsetEnd ) != UR_RESULT_SUCCESS) {
520
+ Queue, NumWG, LaunchInfo.Data . Host . LocalShadowOffset ,
521
+ LaunchInfo.Data . Host . LocalShadowOffsetEnd ) != UR_RESULT_SUCCESS) {
515
522
getContext ()->logger .warning (
516
523
" Failed to allocate shadow memory for local "
517
524
" memory, maybe the number of workgroup ({}) is too "
@@ -520,18 +527,18 @@ ur_result_t MsanInterceptor::prepareLaunch(
520
527
getContext ()->logger .warning (" Skip checking local memory of kernel <{}> " ,
521
528
GetKernelName (Kernel));
522
529
} else {
523
- getContext ()->logger .debug (" ShadowMemory(Local, WorkGroup={}, {} - {}) " ,
524
- NumWG,
525
- (void *)LaunchInfo.Data -> LocalShadowOffset ,
526
- (void *)LaunchInfo.Data -> LocalShadowOffsetEnd );
530
+ getContext ()->logger .debug (
531
+ " ShadowMemory(Local, WorkGroup={}, {} - {}) " , NumWG,
532
+ (void *)LaunchInfo.Data . Host . LocalShadowOffset ,
533
+ (void *)LaunchInfo.Data . Host . LocalShadowOffsetEnd );
527
534
}
528
535
}
529
536
530
537
// Write shadow memory offset for private memory
531
538
if (KernelInfo.IsCheckPrivates ) {
532
539
if (DeviceInfo->Shadow ->AllocPrivateShadow (
533
- Queue, NumWG, LaunchInfo.Data -> PrivateShadowOffset ,
534
- LaunchInfo.Data -> PrivateShadowOffsetEnd ) != UR_RESULT_SUCCESS) {
540
+ Queue, NumWG, LaunchInfo.Data . Host . PrivateShadowOffset ,
541
+ LaunchInfo.Data . Host . PrivateShadowOffsetEnd ) != UR_RESULT_SUCCESS) {
535
542
getContext ()->logger .warning (
536
543
" Failed to allocate shadow memory for private "
537
544
" memory, maybe the number of workgroup ({}) is too "
@@ -542,8 +549,8 @@ ur_result_t MsanInterceptor::prepareLaunch(
542
549
} else {
543
550
getContext ()->logger .debug (
544
551
" ShadowMemory(Private, WorkGroup={}, {} - {})" , NumWG,
545
- (void *)LaunchInfo.Data -> PrivateShadowOffset ,
546
- (void *)LaunchInfo.Data -> PrivateShadowOffsetEnd );
552
+ (void *)LaunchInfo.Data . Host . PrivateShadowOffset ,
553
+ (void *)LaunchInfo.Data . Host . PrivateShadowOffsetEnd );
547
554
}
548
555
// Write local arguments info
549
556
if (!KernelInfo.LocalArgs .empty ()) {
@@ -553,22 +560,26 @@ ur_result_t MsanInterceptor::prepareLaunch(
553
560
getContext ()->logger .debug (" LocalArgs (argIndex={}, size={})" , ArgIndex,
554
561
ArgInfo.Size );
555
562
}
556
- UR_CALL (LaunchInfo.importLocalArgsInfo (Queue, LocalArgsInfo));
563
+ UR_CALL (LaunchInfo.Data . importLocalArgsInfo (Queue, LocalArgsInfo));
557
564
}
558
565
}
559
566
567
+ // sync msan runtime data to device side
568
+ UR_CALL (LaunchInfo.Data .syncToDevice (Queue));
569
+
560
570
getContext ()->logger .info (
561
571
" LaunchInfo {} (GlobalShadow={}, LocalShadow={}, PrivateShadow={}, "
562
572
" CleanShadow={}, LocalArgs={}, NumLocalArgs={}, Device={}, Debug={})" ,
563
- (void *)LaunchInfo.Data , (void *)LaunchInfo.Data ->GlobalShadowOffset ,
564
- (void *)LaunchInfo.Data ->LocalShadowOffset ,
565
- (void *)LaunchInfo.Data ->PrivateShadowOffset ,
566
- (void *)LaunchInfo.Data ->CleanShadow , (void *)LaunchInfo.Data ->LocalArgs ,
567
- LaunchInfo.Data ->NumLocalArgs , ToString (LaunchInfo.Data ->DeviceTy ),
568
- LaunchInfo.Data ->Debug );
569
-
570
- ur_result_t URes =
571
- EnqueueWriteGlobal (" __MsanLaunchInfo" , &LaunchInfo.Data , sizeof (uptr));
573
+ (void *)LaunchInfo.Data .getDevicePtr (),
574
+ (void *)LaunchInfo.Data .Host .GlobalShadowOffset ,
575
+ (void *)LaunchInfo.Data .Host .LocalShadowOffset ,
576
+ (void *)LaunchInfo.Data .Host .PrivateShadowOffset ,
577
+ (void *)LaunchInfo.Data .Host .CleanShadow ,
578
+ (void *)LaunchInfo.Data .Host .LocalArgs , LaunchInfo.Data .Host .NumLocalArgs ,
579
+ ToString (LaunchInfo.Data .Host .DeviceTy ), LaunchInfo.Data .Host .Debug );
580
+
581
+ ur_result_t URes = EnqueueWriteGlobal (
582
+ " __MsanLaunchInfo" , &LaunchInfo.Data .DevicePtr , sizeof (uptr));
572
583
if (URes != UR_RESULT_SUCCESS) {
573
584
getContext ()->logger .info (" EnqueueWriteGlobal(__MsanLaunchInfo) "
574
585
" failed, maybe empty kernel: {}" ,
@@ -641,47 +652,30 @@ ContextInfo::~ContextInfo() {
641
652
ur_result_t USMLaunchInfo::initialize () {
642
653
UR_CALL (getContext ()->urDdiTable .Context .pfnRetain (Context));
643
654
UR_CALL (getContext ()->urDdiTable .Device .pfnRetain (Device));
644
- UR_CALL (getContext ()->urDdiTable .USM .pfnSharedAlloc (
645
- Context, Device, nullptr , nullptr , sizeof (MsanLaunchInfo),
646
- (void **)&Data));
647
- *Data = MsanLaunchInfo{};
648
655
return UR_RESULT_SUCCESS;
649
656
}
650
657
651
- USMLaunchInfo ::~USMLaunchInfo () {
652
- [[maybe_unused]] ur_result_t Result;
653
- if (Data) {
654
- if (Data-> CleanShadow ) {
655
- Result = getContext ()-> urDdiTable . USM . pfnFree (Context,
656
- ( void *)Data-> CleanShadow );
657
- assert (Result == UR_RESULT_SUCCESS);
658
- }
659
- Result = getContext ()->urDdiTable .USM .pfnFree (Context, (void *)Data );
658
+ MsanRuntimeDataWrapper ::~MsanRuntimeDataWrapper () {
659
+ if (Host. CleanShadow ) {
660
+ [[maybe_unused]] auto Result =
661
+ getContext ()-> urDdiTable . USM . pfnFree (Context, ( void *)Host. CleanShadow );
662
+ assert ( Result == UR_RESULT_SUCCESS);
663
+ }
664
+ if (DevicePtr) {
665
+ [[maybe_unused]] auto Result =
666
+ getContext ()->urDdiTable .USM .pfnFree (Context, (void *)DevicePtr );
660
667
assert (Result == UR_RESULT_SUCCESS);
661
668
}
669
+ }
670
+
671
+ USMLaunchInfo::~USMLaunchInfo () {
672
+ [[maybe_unused]] ur_result_t Result;
662
673
Result = getContext ()->urDdiTable .Context .pfnRelease (Context);
663
674
assert (Result == UR_RESULT_SUCCESS);
664
675
Result = getContext ()->urDdiTable .Device .pfnRelease (Device);
665
676
assert (Result == UR_RESULT_SUCCESS);
666
677
}
667
678
668
- ur_result_t USMLaunchInfo::importLocalArgsInfo (
669
- ur_queue_handle_t Queue, const std::vector<MsanLocalArgsInfo> &LocalArgs) {
670
- assert (!LocalArgs.empty ());
671
-
672
- Data->NumLocalArgs = LocalArgs.size ();
673
- const size_t LocalArgsInfoSize = sizeof (MsanLocalArgsInfo) * LocalArgs.size ();
674
- UR_CALL (getContext ()->urDdiTable .USM .pfnSharedAlloc (
675
- Context, Device, nullptr , nullptr , LocalArgsInfoSize,
676
- ur_cast<void **>(&Data->LocalArgs )));
677
-
678
- UR_CALL (getContext ()->urDdiTable .Enqueue .pfnUSMMemcpy (
679
- Queue, true , Data->LocalArgs , LocalArgs.data (), LocalArgsInfoSize, 0 ,
680
- nullptr , nullptr ));
681
-
682
- return UR_RESULT_SUCCESS;
683
- }
684
-
685
679
} // namespace msan
686
680
687
681
using namespace msan ;
0 commit comments