@@ -821,26 +821,49 @@ Error GenericDeviceTy::init(GenericPluginTy &Plugin) {
821821 return Plugin::success ();
822822}
823823
824- Error GenericDeviceTy::deinit (GenericPluginTy &Plugin) {
825- for (DeviceImageTy *Image : LoadedImages)
826- if (auto Err = callGlobalDestructors (Plugin, *Image))
827- return Err;
824+ Error GenericDeviceTy::unloadBinary (DeviceImageTy *Image) {
825+ if (auto Err = callGlobalDestructors (Plugin, *Image))
826+ return Err;
828827
829828 if (OMPX_DebugKind.get () & uint32_t (DeviceDebugKind::AllocationTracker)) {
830829 GenericGlobalHandlerTy &GHandler = Plugin.getGlobalHandler ();
831- for (auto *Image : LoadedImages) {
832- DeviceMemoryPoolTrackingTy ImageDeviceMemoryPoolTracking = {0 , 0 , ~0U , 0 };
833- GlobalTy TrackerGlobal (" __omp_rtl_device_memory_pool_tracker" ,
834- sizeof (DeviceMemoryPoolTrackingTy),
835- &ImageDeviceMemoryPoolTracking);
836- if (auto Err =
837- GHandler.readGlobalFromDevice (*this , *Image, TrackerGlobal)) {
838- consumeError (std::move (Err));
839- continue ;
840- }
841- DeviceMemoryPoolTracking.combine (ImageDeviceMemoryPoolTracking);
830+ DeviceMemoryPoolTrackingTy ImageDeviceMemoryPoolTracking = {0 , 0 , ~0U , 0 };
831+ GlobalTy TrackerGlobal (" __omp_rtl_device_memory_pool_tracker" ,
832+ sizeof (DeviceMemoryPoolTrackingTy),
833+ &ImageDeviceMemoryPoolTracking);
834+ if (auto Err =
835+ GHandler.readGlobalFromDevice (*this , *Image, TrackerGlobal)) {
836+ consumeError (std::move (Err));
842837 }
838+ DeviceMemoryPoolTracking.combine (ImageDeviceMemoryPoolTracking);
839+ }
840+
841+ GenericGlobalHandlerTy &Handler = Plugin.getGlobalHandler ();
842+ auto ProfOrErr = Handler.readProfilingGlobals (*this , *Image);
843+ if (!ProfOrErr)
844+ return ProfOrErr.takeError ();
845+
846+ if (!ProfOrErr->empty ()) {
847+ // Dump out profdata
848+ if ((OMPX_DebugKind.get () & uint32_t (DeviceDebugKind::PGODump)) ==
849+ uint32_t (DeviceDebugKind::PGODump))
850+ ProfOrErr->dump ();
851+
852+ // Write data to profiling file
853+ if (auto Err = ProfOrErr->write ())
854+ return Err;
855+ }
843856
857+ return unloadBinaryImpl (Image);
858+ }
859+
860+ Error GenericDeviceTy::deinit (GenericPluginTy &Plugin) {
861+ for (auto &I : LoadedImages)
862+ if (auto Err = unloadBinary (I))
863+ return Err;
864+ LoadedImages.clear ();
865+
866+ if (OMPX_DebugKind.get () & uint32_t (DeviceDebugKind::AllocationTracker)) {
844867 // TODO: Write this by default into a file.
845868 printf (" \n\n |-----------------------\n "
846869 " | Device memory tracker:\n "
@@ -856,25 +879,6 @@ Error GenericDeviceTy::deinit(GenericPluginTy &Plugin) {
856879 DeviceMemoryPoolTracking.AllocationMax );
857880 }
858881
859- for (auto *Image : LoadedImages) {
860- GenericGlobalHandlerTy &Handler = Plugin.getGlobalHandler ();
861- auto ProfOrErr = Handler.readProfilingGlobals (*this , *Image);
862- if (!ProfOrErr)
863- return ProfOrErr.takeError ();
864-
865- if (ProfOrErr->empty ())
866- continue ;
867-
868- // Dump out profdata
869- if ((OMPX_DebugKind.get () & uint32_t (DeviceDebugKind::PGODump)) ==
870- uint32_t (DeviceDebugKind::PGODump))
871- ProfOrErr->dump ();
872-
873- // Write data to profiling file
874- if (auto Err = ProfOrErr->write ())
875- return Err;
876- }
877-
878882 // Delete the memory manager before deinitializing the device. Otherwise,
879883 // we may delete device allocations after the device is deinitialized.
880884 if (MemoryManager)
0 commit comments