@@ -108,11 +108,15 @@ ur_result_t ShadowMemoryGPU::Setup() {
108108 // TODO: Protect Bad Zone
109109 auto Result = getContext ()->urDdiTable .VirtualMem .pfnReserve (
110110 Context, nullptr , ShadowSize, (void **)&ShadowBegin);
111- if (Result == UR_RESULT_SUCCESS) {
112- ShadowEnd = ShadowBegin + ShadowSize;
113- // Retain the context which reserves shadow memory
114- getContext ()->urDdiTable .Context .pfnRetain (Context);
111+ if (Result != UR_RESULT_SUCCESS) {
112+ getContext ()->logger .error (
113+ " Shadow memory reserved failed with size {}: {}" ,
114+ (void *)ShadowSize, Result);
115+ return Result;
115116 }
117+ ShadowEnd = ShadowBegin + ShadowSize;
118+ // Retain the context which reserves shadow memory
119+ getContext ()->urDdiTable .Context .pfnRetain (Context);
116120
117121 // Set shadow memory for null pointer
118122 // For GPU, wu use up to 1 page of shadow memory
@@ -137,6 +141,24 @@ ur_result_t ShadowMemoryGPU::Destory() {
137141 Context, (void *)PrivateShadowOffset));
138142 PrivateShadowOffset = 0 ;
139143 }
144+
145+ static ur_result_t Result = [this ]() {
146+ const size_t PageSize = GetVirtualMemGranularity (Context, Device);
147+ for (auto [MappedPtr, PhysicalMem] : VirtualMemMaps) {
148+ UR_CALL (getContext ()->urDdiTable .VirtualMem .pfnUnmap (
149+ Context, (void *)MappedPtr, PageSize));
150+ UR_CALL (
151+ getContext ()->urDdiTable .PhysicalMem .pfnRelease (PhysicalMem));
152+ }
153+ UR_CALL (getContext ()->urDdiTable .VirtualMem .pfnFree (
154+ Context, (const void *)ShadowBegin, GetShadowSize ()));
155+ UR_CALL (getContext ()->urDdiTable .Context .pfnRelease (Context));
156+ return UR_RESULT_SUCCESS;
157+ }();
158+ if (!Result) {
159+ return Result;
160+ }
161+
140162 if (LocalShadowOffset != 0 ) {
141163 UR_CALL (getContext ()->urDdiTable .USM .pfnFree (
142164 Context, (void *)LocalShadowOffset));
@@ -205,19 +227,8 @@ ur_result_t ShadowMemoryGPU::EnqueuePoisonShadow(ur_queue_handle_t Queue,
205227 return URes;
206228 }
207229
208- VirtualMemMaps[MappedPtr]. first = PhysicalMem;
230+ VirtualMemMaps[MappedPtr] = PhysicalMem;
209231 }
210-
211- // We don't need to record virtual memory map for null pointer,
212- // since it doesn't have an alloc info.
213- if (Ptr == 0 ) {
214- continue ;
215- }
216-
217- auto AllocInfoIt =
218- getAsanInterceptor ()->findAllocInfoByAddress (Ptr);
219- assert (AllocInfoIt);
220- VirtualMemMaps[MappedPtr].second .insert ((*AllocInfoIt)->second );
221232 }
222233 }
223234
@@ -235,35 +246,6 @@ ur_result_t ShadowMemoryGPU::EnqueuePoisonShadow(ur_queue_handle_t Queue,
235246 return UR_RESULT_SUCCESS;
236247}
237248
238- ur_result_t ShadowMemoryGPU::ReleaseShadow (std::shared_ptr<AllocInfo> AI) {
239- uptr ShadowBegin = MemToShadow (AI->AllocBegin );
240- uptr ShadowEnd = MemToShadow (AI->AllocBegin + AI->AllocSize );
241- assert (ShadowBegin <= ShadowEnd);
242-
243- static const size_t PageSize = GetVirtualMemGranularity (Context, Device);
244-
245- for (auto MappedPtr = RoundDownTo (ShadowBegin, PageSize);
246- MappedPtr <= ShadowEnd; MappedPtr += PageSize) {
247- std::scoped_lock<ur_mutex> Guard (VirtualMemMapsMutex);
248- if (VirtualMemMaps.find (MappedPtr) == VirtualMemMaps.end ()) {
249- continue ;
250- }
251- VirtualMemMaps[MappedPtr].second .erase (AI);
252- if (VirtualMemMaps[MappedPtr].second .empty ()) {
253- UR_CALL (getContext ()->urDdiTable .VirtualMem .pfnUnmap (
254- Context, (void *)MappedPtr, PageSize));
255- UR_CALL (getContext ()->urDdiTable .PhysicalMem .pfnRelease (
256- VirtualMemMaps[MappedPtr].first ));
257- getContext ()->logger .debug (" urVirtualMemUnmap: {} ~ {}" ,
258- (void *)MappedPtr,
259- (void *)(MappedPtr + PageSize - 1 ));
260- VirtualMemMaps.erase (MappedPtr);
261- }
262- }
263-
264- return UR_RESULT_SUCCESS;
265- }
266-
267249ur_result_t ShadowMemoryGPU::AllocLocalShadow (ur_queue_handle_t Queue,
268250 uint32_t NumWG, uptr &Begin,
269251 uptr &End) {
0 commit comments