@@ -243,6 +243,12 @@ ProgramManager::createURProgram(const RTDeviceBinaryImage &Img,
243243 : createBinaryProgram (Ctx, Devices, Binaries.data (), Lengths.data (),
244244 ProgMetadata);
245245
246+ {
247+ std::lock_guard<std::mutex> Lock (MNativeProgramsMutex);
248+ // associate the UR program with the image it was created for
249+ NativePrograms.insert ({Res, &Img});
250+ }
251+
246252 Ctx->addDeviceGlobalInitializer (Res, Devices, &Img);
247253
248254 if constexpr (DbgProgMgr > 1 )
@@ -744,8 +750,7 @@ setSpecializationConstants(const std::shared_ptr<device_image_impl> &InputImpl,
744750 }
745751}
746752
747- static inline void
748- CheckAndDecompressImage ([[maybe_unused]] RTDeviceBinaryImage *Img) {
753+ static inline void CheckAndDecompressImage ([[maybe_unused]] RTDeviceBinaryImage *Img) {
749754#ifndef SYCL_RT_ZSTD_NOT_AVAIABLE
750755 if (auto CompImg = dynamic_cast <CompressedRTDeviceBinaryImage *>(Img))
751756 if (CompImg->IsCompressed ())
@@ -821,6 +826,7 @@ ur_program_handle_t ProgramManager::getBuiltURProgram(
821826 // Should always come last!
822827 appendCompileEnvironmentVariablesThatAppend (CompileOpts);
823828 appendLinkEnvironmentVariablesThatAppend (LinkOpts);
829+ // getOrCreateURProgram adds NativePrg to NativePrograms storage.
824830 auto [NativePrg, DeviceCodeWasInCache] = getOrCreateURProgram (
825831 Img, AllImages, Context, {Device}, CompileOpts + LinkOpts, SpecConsts);
826832
@@ -862,6 +868,7 @@ ur_program_handle_t ProgramManager::getBuiltURProgram(
862868 SerializedObj ImgSpecConsts =
863869 DeviceImageImpl->get_spec_const_blob_ref ();
864870
871+ // adds NativePrg to NativePrograms
865872 ur_program_handle_t NativePrg =
866873 createURProgram (*BinImg, Context, {Device});
867874
@@ -874,6 +881,7 @@ ur_program_handle_t ProgramManager::getBuiltURProgram(
874881 std::vector<ur_device_handle_t > Devs = {
875882 getSyclObjImpl (Device).get ()->getHandleRef ()};
876883 ;
884+ // build adds BuiltProgram to NativePrograms storage
877885 ProgramPtr BuiltProgram = build (
878886 std::move (ProgramManaged), ContextImpl, CompileOpts, LinkOpts, Devs,
879887 DeviceLibReqMask, ProgramsToLink,
@@ -886,10 +894,8 @@ ur_program_handle_t ProgramManager::getBuiltURProgram(
886894
887895 {
888896 std::lock_guard<std::mutex> Lock (MNativeProgramsMutex);
889- // NativePrograms map does not intend to keep reference to program handle,
890- // so keys in the map can be invalid (reference count went to zero and the
891- // underlying program disposed of). Protecting from incorrect values by
892- // removal of map entries with same handle (obviously invalid entries).
897+ // NativePrograms map does not intend to keep reference to program handle, so keys in the map can be invalid (reference count went to zero and the underlying program disposed of).
898+ // Protecting from incorrect values by removal of map entries with same handle (obviously invalid entries).
893899 std::ignore = NativePrograms.erase (BuiltProgram.get ());
894900 NativePrograms.insert ({BuiltProgram.get (), &Img});
895901 for (RTDeviceBinaryImage *LinkedImg : DeviceImagesToLink) {
@@ -2655,6 +2661,7 @@ device_image_plain ProgramManager::build(const device_image_plain &DeviceImage,
26552661
26562662 // Device is not used when creating program from SPIRV, so passing only one
26572663 // device is OK.
2664+ // getOrCreateURProgram adds NativePrg to NativePrograms storage (no extra ref).
26582665 auto [NativePrg, DeviceCodeWasInCache] = getOrCreateURProgram (
26592666 Img, {&Img}, Context, Devs, CompileOpts + LinkOpts, SpecConsts);
26602667
@@ -2688,7 +2695,7 @@ device_image_plain ProgramManager::build(const device_image_plain &DeviceImage,
26882695 URDevices, DeviceLibReqMask, ExtraProgramsToLink);
26892696
26902697 emitBuiltProgramInfo (BuiltProgram.get (), ContextImpl);
2691-
2698+
26922699 {
26932700 std::lock_guard<std::mutex> Lock (MNativeProgramsMutex);
26942701 NativePrograms.insert ({BuiltProgram.get (), &Img});
0 commit comments