Skip to content

[IR] Fix ir StorageManager bug #54503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions paddle/ir/core/storage_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,23 @@ namespace ir {
struct ParametricStorageManager {
using StorageBase = StorageManager::StorageBase;

ParametricStorageManager() {}
explicit ParametricStorageManager(std::function<void(StorageBase *)> destroy)
: destroy_(destroy) {}

~ParametricStorageManager() {
IR_ENFORCE(
destroy_,
"The desturctor of ParametricStorageManager should not be nullptr.");
for (const auto &instance : parametric_instances_) {
delete instance.second;
destroy_(instance.second);
}
parametric_instances_.clear();
}

// Get the storage of parametric type, if not in the cache, create and
// insert the cache.
StorageBase *GetOrCreate(std::size_t hash_value,
std::function<bool(const StorageBase *)> equal_func,
std::function<bool(StorageBase *)> equal_func,
std::function<StorageBase *()> constructor) {
if (parametric_instances_.count(hash_value) != 0) {
auto pr = parametric_instances_.equal_range(hash_value);
Expand All @@ -62,6 +66,7 @@ struct ParametricStorageManager {
// In order to prevent hash conflicts, the unordered_multimap data structure
// is used for storage.
std::unordered_multimap<size_t, StorageBase *> parametric_instances_;
std::function<void(StorageBase *)> destroy_;
};

StorageManager::StorageManager() {}
Expand Down Expand Up @@ -95,12 +100,13 @@ StorageManager::StorageBase *StorageManager::GetParameterlessStorageImpl(
return parameterless_instance;
}

void StorageManager::RegisterParametricStorageImpl(TypeId type_id) {
void StorageManager::RegisterParametricStorageImpl(
TypeId type_id, std::function<void(StorageBase *)> destroy) {
std::lock_guard<ir::SpinLock> guard(parametric_instance_lock_);
VLOG(4) << "Register a parametric storage of: [TypeId_hash="
<< std::hash<ir::TypeId>()(type_id) << "].";
parametric_instance_.emplace(type_id,
std::make_unique<ParametricStorageManager>());
parametric_instance_.emplace(
type_id, std::make_unique<ParametricStorageManager>(destroy));
}

void StorageManager::RegisterParameterlessStorageImpl(
Expand Down
7 changes: 5 additions & 2 deletions paddle/ir/core/storage_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ class StorageManager {
///
template <typename Storage>
void RegisterParametricStorage(TypeId type_id) {
return RegisterParametricStorageImpl(type_id);
return RegisterParametricStorageImpl(type_id, [](StorageBase *storage) {
delete static_cast<Storage *>(storage);
});
}

///
Expand Down Expand Up @@ -129,7 +131,8 @@ class StorageManager {

StorageBase *GetParameterlessStorageImpl(TypeId type_id);

void RegisterParametricStorageImpl(TypeId type_id);
void RegisterParametricStorageImpl(
TypeId type_id, std::function<void(StorageBase *)> destroy);

void RegisterParameterlessStorageImpl(
TypeId type_id, std::function<StorageBase *()> constructor);
Expand Down