@@ -243,12 +243,6 @@ 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-
252246 Ctx->addDeviceGlobalInitializer (Res, Devices, &Img);
253247
254248 if constexpr (DbgProgMgr > 1 )
@@ -509,7 +503,7 @@ std::pair<ur_program_handle_t, bool> ProgramManager::getOrCreateURProgram(
509503 const std::vector<const RTDeviceBinaryImage *> &AllImages,
510504 const context &Context, const std::vector<device> &Devices,
511505 const std::string &CompileAndLinkOptions, SerializedObj SpecConsts) {
512- ur_program_handle_t NativePrg; // TODO: Or native?
506+ ur_program_handle_t NativePrg;
513507
514508 auto BinProg = PersistentDeviceCodeCache::getItemFromDisc (
515509 Devices[0 ], AllImages, SpecConsts, CompileAndLinkOptions);
@@ -750,7 +744,8 @@ setSpecializationConstants(const std::shared_ptr<device_image_impl> &InputImpl,
750744 }
751745}
752746
753- static inline void CheckAndDecompressImage ([[maybe_unused]] RTDeviceBinaryImage *Img) {
747+ static inline void
748+ CheckAndDecompressImage ([[maybe_unused]] RTDeviceBinaryImage *Img) {
754749#ifndef SYCL_RT_ZSTD_NOT_AVAIABLE
755750 if (auto CompImg = dynamic_cast <CompressedRTDeviceBinaryImage *>(Img))
756751 if (CompImg->IsCompressed ())
@@ -891,6 +886,11 @@ ur_program_handle_t ProgramManager::getBuiltURProgram(
891886
892887 {
893888 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).
893+ std::ignore = NativePrograms.erase (BuiltProgram.get ());
894894 NativePrograms.insert ({BuiltProgram.get (), &Img});
895895 for (RTDeviceBinaryImage *LinkedImg : DeviceImagesToLink) {
896896 NativePrograms.insert ({BuiltProgram.get (), LinkedImg});
0 commit comments