diff --git a/projects/clr/hipamd/src/hip_fatbin.cpp b/projects/clr/hipamd/src/hip_fatbin.cpp index 5d14637131..8fb5c11ea6 100644 --- a/projects/clr/hipamd/src/hip_fatbin.cpp +++ b/projects/clr/hipamd/src/hip_fatbin.cpp @@ -48,22 +48,16 @@ FatBinaryInfo::FatBinaryInfo(const char* fname, const void* image) } FatBinaryInfo::~FatBinaryInfo() { - // Different devices in the same model have the same binary_image_ - std::set toDelete; // Release per device fat bin info. for (int dev_id = 0; dev_id < dev_programs_.size(); dev_id++) { if (dev_programs_[dev_id] != nullptr) { - auto& binaryInfo = dev_programs_[dev_id]->binary(*g_devices[dev_id]->devices()[0]); - if (std::get<0>(binaryInfo) && std::get<1>(binaryInfo).second == 0 && - std::get<0>(binaryInfo) != image_) { - toDelete.insert(std::get<0>(binaryInfo)); - } dev_programs_[dev_id]->release(); dev_programs_[dev_id] = nullptr; } } - for (auto itemData : toDelete) { - delete[] reinterpret_cast(itemData); + // Release Code object allocations + for (const auto& i : code_obj_allocations_) { + delete[] reinterpret_cast(i); } ReleaseImageAndFile(); } @@ -296,7 +290,8 @@ static bool UncompressAndPopulateCodeObject( comgr_helper::ComgrDataUniqueHandle item_handle(item); size_t item_name_size = 0; - if (auto comgr_status = amd::Comgr::get_data_name(item_handle.get(), &item_name_size, nullptr); + if (auto comgr_status = + amd::Comgr::get_data_name(item_handle.get(), &item_name_size, nullptr); comgr_status != AMD_COMGR_STATUS_SUCCESS) { LogError("Failed to get data size"); break; @@ -376,10 +371,9 @@ static bool PopulateCodeObjectMap( for (const auto& item : query_list_array) { if (item.size > 0) { - char* d = new char[item.size]; - std::memcpy(reinterpret_cast(d), reinterpret_cast(image) + item.offset, - item.size); - code_obj_map[item.isa] = std::make_pair(d, item.size); + // Map the offset pointer and size from the image + auto loc = reinterpret_cast(image) + item.offset; + code_obj_map[item.isa] = std::make_pair(loc, item.size); } } @@ -462,12 +456,17 @@ hipError_t FatBinaryInfo::ExtractFatBinaryUsingCOMGR(const std::vectorsecond.second]; - std::memcpy(co, reinterpret_cast(native_co->second.first), - native_co->second.second); - hip_status = AddDevProgram(device, co, native_co->second.second, 0); + LogPrintfInfo("Using native code object for device: %s co: %s", device_name.c_str(), + native_co->first.c_str()); + hip_status = AddDevProgram(device, native_co->second.first, native_co->second.second, 0); if (hip_status != hipSuccess) { break; } } else if (generic_co != code_obj_map.end() && !HIP_FORCE_SPIRV_CODEOBJECT) { - char* co = new char[generic_co->second.second]; - std::memcpy(co, reinterpret_cast(generic_co->second.first), - generic_co->second.second); - hip_status = AddDevProgram(device, co, generic_co->second.second, 0); + LogPrintfInfo("Using generic code object for device: %s co: %s", device_name.c_str(), + generic_co->first.c_str()); + hip_status = AddDevProgram(device, generic_co->second.first, generic_co->second.second, 0); if (hip_status != hipSuccess) { break; } } else if (spirv_isa_found) { + LogPrintfInfo("Using spirv code object for device: %s", device_name.c_str()); std::string target_id = device->devices()[0]->isa().targetId(); std::string isa = "amdgcn-amd-amdhsa--" + target_id; @@ -620,6 +616,7 @@ hipError_t FatBinaryInfo::ExtractFatBinaryUsingCOMGR(const std::vector(co.second.first); - } - return hip_status; } diff --git a/projects/clr/hipamd/src/hip_fatbin.hpp b/projects/clr/hipamd/src/hip_fatbin.hpp index b89ef412f4..f3e7322d94 100644 --- a/projects/clr/hipamd/src/hip_fatbin.hpp +++ b/projects/clr/hipamd/src/hip_fatbin.hpp @@ -86,8 +86,9 @@ class FatBinaryInfo { std::vector dev_programs_; //!< Program info per Device - std::shared_ptr ufd_; //!< Unique file descriptor - amd::Monitor fb_lock_{true}; //!< Lock for the fat binary access + std::shared_ptr ufd_; //!< Unique file descriptor + amd::Monitor fb_lock_{true}; //!< Lock for the fat binary access + std::unordered_set code_obj_allocations_; //!< Track allocations for code objects }; }; // namespace hip