@@ -682,28 +682,6 @@ ur_result_t SanitizerInterceptor::prepareLaunch(
682682 LocalWorkSize[Dim];
683683 }
684684
685- auto EnqueueAllocateShadowMemory = [Context = ContextInfo->Handle ,
686- Device = DeviceInfo->Handle ,
687- Queue](size_t Size, uptr &Ptr) {
688- void *Allocated = nullptr ;
689- auto URes = getContext ()->urDdiTable .USM .pfnDeviceAlloc (
690- Context, Device, nullptr , nullptr , Size, &Allocated);
691- if (URes != UR_RESULT_SUCCESS) {
692- return URes;
693- }
694- // Initialize shadow memory
695- URes = EnqueueUSMBlockingSet (Queue, Allocated, 0 , Size);
696- if (URes != UR_RESULT_SUCCESS) {
697- [[maybe_unused]] auto URes =
698- getContext ()->urDdiTable .USM .pfnFree (Context, Allocated);
699- assert (URes == UR_RESULT_SUCCESS &&
700- " urUSMFree failed at allocating shadow memory" );
701- Allocated = nullptr ;
702- }
703- Ptr = (uptr)Allocated;
704- return URes;
705- };
706-
707685 auto LocalMemoryUsage =
708686 GetKernelLocalMemorySize (Kernel, DeviceInfo->Handle );
709687 auto PrivateMemoryUsage =
@@ -715,86 +693,45 @@ ur_result_t SanitizerInterceptor::prepareLaunch(
715693
716694 // Write shadow memory offset for local memory
717695 if (getOptions ().DetectLocals ) {
718- // CPU needn't this
719- if (DeviceInfo->Type == DeviceType::GPU_PVC ||
720- DeviceInfo->Type == DeviceType::GPU_DG2) {
721- const size_t LocalMemorySize =
722- GetDeviceLocalMemorySize (DeviceInfo->Handle );
723- const size_t LocalShadowMemorySize =
724- (NumWG * LocalMemorySize) >> ASAN_SHADOW_SCALE;
725-
726- getContext ()->logger .debug (
727- " LocalMemory(WorkGroup={}, LocalMemorySize={}, "
728- " LocalShadowMemorySize={})" ,
729- NumWG, LocalMemorySize, LocalShadowMemorySize);
730-
731- if (EnqueueAllocateShadowMemory (
732- LocalShadowMemorySize,
733- LaunchInfo.Data ->LocalShadowOffset ) !=
734- UR_RESULT_SUCCESS) {
735- getContext ()->logger .warning (
736- " Failed to allocate shadow memory for local "
737- " memory, maybe the number of workgroup ({}) is too "
738- " large" ,
739- NumWG);
740- getContext ()->logger .warning (
741- " Skip checking local memory of kernel <{}>" ,
742- GetKernelName (Kernel));
743- } else {
744- LaunchInfo.Data ->LocalShadowOffsetEnd =
745- LaunchInfo.Data ->LocalShadowOffset +
746- LocalShadowMemorySize - 1 ;
747-
748- ContextInfo->Stats .UpdateShadowMalloced (
749- LocalShadowMemorySize);
750-
751- getContext ()->logger .info (
752- " ShadowMemory(Local, {} - {})" ,
753- (void *)LaunchInfo.Data ->LocalShadowOffset ,
754- (void *)LaunchInfo.Data ->LocalShadowOffsetEnd );
755- }
696+ if (DeviceInfo->Shadow ->AllocLocalShadow (
697+ Queue, NumWG, LaunchInfo.Data ->LocalShadowOffset ,
698+ LaunchInfo.Data ->LocalShadowOffsetEnd ) !=
699+ UR_RESULT_SUCCESS) {
700+ getContext ()->logger .warning (
701+ " Failed to allocate shadow memory for local "
702+ " memory, maybe the number of workgroup ({}) is too "
703+ " large" ,
704+ NumWG);
705+ getContext ()->logger .warning (
706+ " Skip checking local memory of kernel <{}>" ,
707+ GetKernelName (Kernel));
708+ } else {
709+ getContext ()->logger .info (
710+ " ShadowMemory(Local, WorkGroup{}, {} - {})" , NumWG,
711+ (void *)LaunchInfo.Data ->LocalShadowOffset ,
712+ (void *)LaunchInfo.Data ->LocalShadowOffsetEnd );
756713 }
757714 }
758715
759716 // Write shadow memory offset for private memory
760717 if (getOptions ().DetectPrivates ) {
761- if (DeviceInfo->Type == DeviceType::CPU) {
762- LaunchInfo.Data ->PrivateShadowOffset =
763- DeviceInfo->Shadow ->ShadowBegin ;
764- } else if (DeviceInfo->Type == DeviceType::GPU_PVC ||
765- DeviceInfo->Type == DeviceType::GPU_DG2) {
766- const size_t PrivateShadowMemorySize =
767- (NumWG * ASAN_PRIVATE_SIZE) >> ASAN_SHADOW_SCALE;
768-
769- getContext ()->logger .debug (" PrivateMemory(WorkGroup={}, "
770- " PrivateShadowMemorySize={})" ,
771- NumWG, PrivateShadowMemorySize);
772-
773- if (EnqueueAllocateShadowMemory (
774- PrivateShadowMemorySize,
775- LaunchInfo.Data ->PrivateShadowOffset ) !=
776- UR_RESULT_SUCCESS) {
777- getContext ()->logger .warning (
778- " Failed to allocate shadow memory for private "
779- " memory, maybe the number of workgroup ({}) is too "
780- " large" ,
781- NumWG);
782- getContext ()->logger .warning (
783- " Skip checking private memory of kernel <{}>" ,
784- GetKernelName (Kernel));
785- } else {
786- LaunchInfo.Data ->PrivateShadowOffsetEnd =
787- LaunchInfo.Data ->PrivateShadowOffset +
788- PrivateShadowMemorySize - 1 ;
789-
790- ContextInfo->Stats .UpdateShadowMalloced (
791- PrivateShadowMemorySize);
792-
793- getContext ()->logger .info (
794- " ShadowMemory(Private, {} - {})" ,
795- (void *)LaunchInfo.Data ->PrivateShadowOffset ,
796- (void *)LaunchInfo.Data ->PrivateShadowOffsetEnd );
797- }
718+ if (DeviceInfo->Shadow ->AllocPrivateShadow (
719+ Queue, NumWG, LaunchInfo.Data ->PrivateShadowOffset ,
720+ LaunchInfo.Data ->PrivateShadowOffsetEnd ) !=
721+ UR_RESULT_SUCCESS) {
722+ getContext ()->logger .warning (
723+ " Failed to allocate shadow memory for private "
724+ " memory, maybe the number of workgroup ({}) is too "
725+ " large" ,
726+ NumWG);
727+ getContext ()->logger .warning (
728+ " Skip checking private memory of kernel <{}>" ,
729+ GetKernelName (Kernel));
730+ } else {
731+ getContext ()->logger .info (
732+ " ShadowMemory(Private, WorkGroup{}, {} - {})" , NumWG,
733+ (void *)LaunchInfo.Data ->PrivateShadowOffset ,
734+ (void *)LaunchInfo.Data ->PrivateShadowOffsetEnd );
798735 }
799736 }
800737 } while (false );
@@ -878,25 +815,7 @@ ur_result_t USMLaunchInfo::updateKernelInfo(const KernelInfo &KI) {
878815USMLaunchInfo::~USMLaunchInfo () {
879816 [[maybe_unused]] ur_result_t Result;
880817 if (Data) {
881- auto Type = GetDeviceType (Context, Device);
882818 auto ContextInfo = getContext ()->interceptor ->getContextInfo (Context);
883- if (Type == DeviceType::GPU_PVC || Type == DeviceType::GPU_DG2) {
884- if (Data->PrivateShadowOffset ) {
885- ContextInfo->Stats .UpdateShadowFreed (
886- Data->PrivateShadowOffsetEnd - Data->PrivateShadowOffset +
887- 1 );
888- Result = getContext ()->urDdiTable .USM .pfnFree (
889- Context, (void *)Data->PrivateShadowOffset );
890- assert (Result == UR_RESULT_SUCCESS);
891- }
892- if (Data->LocalShadowOffset ) {
893- ContextInfo->Stats .UpdateShadowFreed (
894- Data->LocalShadowOffsetEnd - Data->LocalShadowOffset + 1 );
895- Result = getContext ()->urDdiTable .USM .pfnFree (
896- Context, (void *)Data->LocalShadowOffset );
897- assert (Result == UR_RESULT_SUCCESS);
898- }
899- }
900819 if (Data->LocalArgs ) {
901820 Result = getContext ()->urDdiTable .USM .pfnFree (
902821 Context, (void *)Data->LocalArgs );
0 commit comments