@@ -27,6 +27,15 @@ uptr MemToShadow_CPU(uptr USM_SHADOW_BASE, uptr UPtr) {
2727 return USM_SHADOW_BASE + (UPtr >> ASAN_SHADOW_SCALE);
2828}
2929
30+ uptr MemToShadow_DG2 (uptr USM_SHADOW_BASE, uptr UPtr) {
31+ if (UPtr & 0xFFFF000000000000ULL ) { // Device USM
32+ return USM_SHADOW_BASE + 0x80000000000ULL +
33+ ((UPtr & 0x7FFFFFFFFFFFULL ) >> ASAN_SHADOW_SCALE);
34+ } else { // Host/Shared USM
35+ return USM_SHADOW_BASE + (UPtr >> ASAN_SHADOW_SCALE);
36+ }
37+ }
38+
3039uptr MemToShadow_PVC (uptr USM_SHADOW_BASE, uptr UPtr) {
3140 if (UPtr & 0xFF00000000000000ULL ) { // Device USM
3241 return USM_SHADOW_BASE + 0x80000000000ULL +
@@ -56,6 +65,9 @@ ur_result_t enqueueMemSetShadow(ur_context_handle_t Context,
5665 return UR_RESULT_SUCCESS;
5766 }
5867 if (DeviceInfo->Type == DeviceType::CPU) {
68+ // /
69+ // / CPU Device: CPU needs to use a special memset function
70+ // /
5971 uptr ShadowBegin = MemToShadow_CPU (DeviceInfo->ShadowOffset , Ptr);
6072 uptr ShadowEnd =
6173 MemToShadow_CPU (DeviceInfo->ShadowOffset , Ptr + Size - 1 );
@@ -72,10 +84,25 @@ ur_result_t enqueueMemSetShadow(ur_context_handle_t Context,
7284 (void *)ShadowBegin, ShadowEnd - ShadowBegin + 1 ,
7385 (void *)(size_t )Value);
7486 MemSet ((void *)ShadowBegin, Value, ShadowEnd - ShadowBegin + 1 );
75- } else if (DeviceInfo->Type == DeviceType::GPU_PVC) {
76- uptr ShadowBegin = MemToShadow_PVC (DeviceInfo->ShadowOffset , Ptr);
77- uptr ShadowEnd =
78- MemToShadow_PVC (DeviceInfo->ShadowOffset , Ptr + Size - 1 );
87+ } else {
88+ // /
89+ // / GPU Device: GPU needs to manually map physical memory before memset
90+ // /
91+ uptr ShadowBegin = 0 , ShadowEnd = 0 ;
92+
93+ if (DeviceInfo->Type == DeviceType::GPU_PVC) {
94+ ShadowBegin = MemToShadow_PVC (DeviceInfo->ShadowOffset , Ptr);
95+ ShadowEnd =
96+ MemToShadow_PVC (DeviceInfo->ShadowOffset , Ptr + Size - 1 );
97+ } else if (DeviceInfo->Type == DeviceType::GPU_DG2) {
98+ ShadowBegin = MemToShadow_DG2 (DeviceInfo->ShadowOffset , Ptr);
99+ ShadowEnd =
100+ MemToShadow_DG2 (DeviceInfo->ShadowOffset , Ptr + Size - 1 );
101+ } else {
102+ getContext ()->logger .error (" Unsupport device type" );
103+ return UR_RESULT_ERROR_INVALID_ARGUMENT;
104+ }
105+
79106 assert (ShadowBegin <= ShadowEnd);
80107 {
81108 static const size_t PageSize =
@@ -108,7 +135,9 @@ ur_result_t enqueueMemSetShadow(ur_context_handle_t Context,
108135 Context, (void *)MappedPtr, PageSize, PhysicalMem, 0 ,
109136 UR_VIRTUAL_MEM_ACCESS_FLAG_READ_WRITE);
110137 if (URes != UR_RESULT_SUCCESS) {
111- getContext ()->logger .debug (" urVirtualMemMap(): {}" , URes);
138+ getContext ()->logger .debug (" urVirtualMemMap({}, {}): {}" ,
139+ (void *)MappedPtr, PageSize,
140+ URes);
112141 }
113142
114143 // Initialize to zero
@@ -137,9 +166,6 @@ ur_result_t enqueueMemSetShadow(ur_context_handle_t Context,
137166 getContext ()->logger .error (" urEnqueueUSMFill(): {}" , URes);
138167 return URes;
139168 }
140- } else {
141- getContext ()->logger .error (" Unsupport device type" );
142- return UR_RESULT_ERROR_INVALID_ARGUMENT;
143169 }
144170 return UR_RESULT_SUCCESS;
145171}
@@ -158,6 +184,7 @@ SanitizerInterceptor::SanitizerInterceptor(logger::Logger &logger)
158184SanitizerInterceptor::~SanitizerInterceptor () {
159185 DestroyShadowMemoryOnCPU ();
160186 DestroyShadowMemoryOnPVC ();
187+ DestroyShadowMemoryOnDG2 ();
161188}
162189
163190// / The memory chunk allocated from the underlying allocator looks like this:
@@ -390,6 +417,8 @@ ur_result_t DeviceInfo::allocShadowMemory(ur_context_handle_t Context) {
390417 UR_CALL (SetupShadowMemoryOnCPU (ShadowOffset, ShadowOffsetEnd));
391418 } else if (Type == DeviceType::GPU_PVC) {
392419 UR_CALL (SetupShadowMemoryOnPVC (Context, ShadowOffset, ShadowOffsetEnd));
420+ } else if (Type == DeviceType::GPU_DG2) {
421+ UR_CALL (SetupShadowMemoryOnDG2 (Context, ShadowOffset, ShadowOffsetEnd));
393422 } else {
394423 getContext ()->logger .error (" Unsupport device type" );
395424 return UR_RESULT_ERROR_INVALID_ARGUMENT;
@@ -586,12 +615,6 @@ SanitizerInterceptor::insertDevice(ur_device_handle_t Device,
586615
587616 DI = std::make_shared<ur_sanitizer_layer::DeviceInfo>(Device);
588617
589- // Query device type
590- DI->Type = GetDeviceType (Device);
591- if (DI->Type == DeviceType::UNKNOWN) {
592- return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
593- }
594-
595618 // Query alignment
596619 UR_CALL (getContext ()->urDdiTable .Device .pfnGetInfo (
597620 Device, UR_DEVICE_INFO_MEM_BASE_ADDR_ALIGN, sizeof (DI->Alignment ),
@@ -786,7 +809,8 @@ ur_result_t SanitizerInterceptor::prepareLaunch(
786809 // Write shadow memory offset for local memory
787810 if (Options (logger).DetectLocals ) {
788811 // CPU needn't this
789- if (DeviceInfo->Type == DeviceType::GPU_PVC) {
812+ if (DeviceInfo->Type == DeviceType::GPU_PVC ||
813+ DeviceInfo->Type == DeviceType::GPU_DG2) {
790814 const size_t LocalMemorySize =
791815 GetDeviceLocalMemorySize (DeviceInfo->Handle );
792816 const size_t LocalShadowMemorySize =
@@ -826,7 +850,8 @@ ur_result_t SanitizerInterceptor::prepareLaunch(
826850 if (Options (logger).DetectPrivates ) {
827851 if (DeviceInfo->Type == DeviceType::CPU) {
828852 LaunchInfo.Data ->PrivateShadowOffset = DeviceInfo->ShadowOffset ;
829- } else if (DeviceInfo->Type == DeviceType::GPU_PVC) {
853+ } else if (DeviceInfo->Type == DeviceType::GPU_PVC ||
854+ DeviceInfo->Type == DeviceType::GPU_DG2) {
830855 const size_t PrivateShadowMemorySize =
831856 (NumWG * ASAN_PRIVATE_SIZE) >> ASAN_SHADOW_SCALE;
832857
@@ -907,8 +932,8 @@ ur_result_t USMLaunchInfo::updateKernelInfo(const KernelInfo &KI) {
907932USMLaunchInfo::~USMLaunchInfo () {
908933 [[maybe_unused]] ur_result_t Result;
909934 if (Data) {
910- auto Type = GetDeviceType (Device);
911- if (Type == DeviceType::GPU_PVC) {
935+ auto Type = GetDeviceType (Context, Device);
936+ if (Type == DeviceType::GPU_PVC || Type == DeviceType::GPU_DG2 ) {
912937 if (Data->PrivateShadowOffset ) {
913938 Result = getContext ()->urDdiTable .USM .pfnFree (
914939 Context, (void *)Data->PrivateShadowOffset );
0 commit comments