2019-05-15 06:03:48 +08:00
|
|
|
//===- StorageUniquer.cpp - Common Storage Class Uniquer ------------------===//
|
2019-04-26 12:01:21 +08:00
|
|
|
//
|
2020-01-26 11:58:30 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-24 01:35:36 +08:00
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-04-26 12:01:21 +08:00
|
|
|
//
|
2019-12-24 01:35:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-04-26 12:01:21 +08:00
|
|
|
|
|
|
|
#include "mlir/Support/StorageUniquer.h"
|
2019-10-20 03:10:34 +08:00
|
|
|
|
2019-04-26 12:01:21 +08:00
|
|
|
#include "mlir/Support/LLVM.h"
|
2020-08-08 04:29:11 +08:00
|
|
|
#include "mlir/Support/TypeID.h"
|
2019-04-26 12:01:21 +08:00
|
|
|
#include "llvm/Support/RWMutex.h"
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
using namespace mlir::detail;
|
|
|
|
|
2020-08-08 04:29:11 +08:00
|
|
|
namespace {
|
2020-08-19 06:59:53 +08:00
|
|
|
/// This class represents a uniquer for storage instances of a specific type
|
|
|
|
/// that has parametric storage. It contains all of the necessary data to unique
|
|
|
|
/// storage instances in a thread safe way. This allows for the main uniquer to
|
|
|
|
/// bucket each of the individual sub-types removing the need to lock the main
|
|
|
|
/// uniquer itself.
|
|
|
|
struct ParametricStorageUniquer {
|
2019-04-26 12:01:21 +08:00
|
|
|
using BaseStorage = StorageUniquer::BaseStorage;
|
|
|
|
using StorageAllocator = StorageUniquer::StorageAllocator;
|
|
|
|
|
|
|
|
/// A lookup key for derived instances of storage objects.
|
|
|
|
struct LookupKey {
|
|
|
|
/// The known hash value of the key.
|
|
|
|
unsigned hashValue;
|
|
|
|
|
|
|
|
/// An equality function for comparing with an existing storage instance.
|
2019-12-19 01:28:48 +08:00
|
|
|
function_ref<bool(const BaseStorage *)> isEqual;
|
2019-04-26 12:01:21 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// A utility wrapper object representing a hashed storage object. This class
|
|
|
|
/// contains a storage object and an existing computed hash value.
|
|
|
|
struct HashedStorage {
|
|
|
|
unsigned hashValue;
|
|
|
|
BaseStorage *storage;
|
|
|
|
};
|
|
|
|
|
2020-08-08 04:29:11 +08:00
|
|
|
/// Storage info for derived TypeStorage objects.
|
|
|
|
struct StorageKeyInfo : DenseMapInfo<HashedStorage> {
|
|
|
|
static HashedStorage getEmptyKey() {
|
2020-08-08 13:31:25 +08:00
|
|
|
return HashedStorage{0, DenseMapInfo<BaseStorage *>::getEmptyKey()};
|
2020-08-08 04:29:11 +08:00
|
|
|
}
|
|
|
|
static HashedStorage getTombstoneKey() {
|
2020-08-08 13:31:25 +08:00
|
|
|
return HashedStorage{0, DenseMapInfo<BaseStorage *>::getTombstoneKey()};
|
2020-08-08 04:29:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned getHashValue(const HashedStorage &key) {
|
|
|
|
return key.hashValue;
|
|
|
|
}
|
|
|
|
static unsigned getHashValue(LookupKey key) { return key.hashValue; }
|
|
|
|
|
|
|
|
static bool isEqual(const HashedStorage &lhs, const HashedStorage &rhs) {
|
|
|
|
return lhs.storage == rhs.storage;
|
|
|
|
}
|
|
|
|
static bool isEqual(const LookupKey &lhs, const HashedStorage &rhs) {
|
|
|
|
if (isEqual(rhs, getEmptyKey()) || isEqual(rhs, getTombstoneKey()))
|
|
|
|
return false;
|
2020-08-19 06:59:53 +08:00
|
|
|
// Invoke the equality function on the lookup key.
|
|
|
|
return lhs.isEqual(rhs.storage);
|
2020-08-08 04:29:11 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-08-19 06:59:53 +08:00
|
|
|
/// The set containing the allocated storage instances.
|
2020-08-08 04:29:11 +08:00
|
|
|
using StorageTypeSet = DenseSet<HashedStorage, StorageKeyInfo>;
|
2020-08-19 06:59:53 +08:00
|
|
|
StorageTypeSet instances;
|
2020-08-08 04:29:11 +08:00
|
|
|
|
|
|
|
/// Allocator to use when constructing derived instances.
|
|
|
|
StorageAllocator allocator;
|
|
|
|
|
|
|
|
/// A mutex to keep type uniquing thread-safe.
|
|
|
|
llvm::sys::SmartRWMutex<true> mutex;
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
namespace mlir {
|
|
|
|
namespace detail {
|
|
|
|
/// This is the implementation of the StorageUniquer class.
|
|
|
|
struct StorageUniquerImpl {
|
|
|
|
using BaseStorage = StorageUniquer::BaseStorage;
|
|
|
|
using StorageAllocator = StorageUniquer::StorageAllocator;
|
|
|
|
|
2020-08-19 06:59:53 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Parametric Storage
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2020-09-09 00:53:24 +08:00
|
|
|
/// Check if an instance of a parametric storage class exists.
|
|
|
|
bool hasParametricStorage(TypeID id) { return parametricUniquers.count(id); }
|
|
|
|
|
2020-08-19 06:59:53 +08:00
|
|
|
/// Get or create an instance of a parametric type.
|
2019-04-26 12:01:21 +08:00
|
|
|
BaseStorage *
|
2020-08-19 06:59:53 +08:00
|
|
|
getOrCreate(TypeID id, unsigned hashValue,
|
2019-12-19 01:28:48 +08:00
|
|
|
function_ref<bool(const BaseStorage *)> isEqual,
|
|
|
|
function_ref<BaseStorage *(StorageAllocator &)> ctorFn) {
|
2020-08-19 06:59:53 +08:00
|
|
|
assert(parametricUniquers.count(id) &&
|
|
|
|
"creating unregistered storage instance");
|
|
|
|
ParametricStorageUniquer::LookupKey lookupKey{hashValue, isEqual};
|
|
|
|
ParametricStorageUniquer &storageUniquer = *parametricUniquers[id];
|
2020-05-03 03:28:57 +08:00
|
|
|
if (!threadingIsEnabled)
|
2020-08-19 06:59:53 +08:00
|
|
|
return getOrCreateUnsafe(storageUniquer, lookupKey, ctorFn);
|
2019-04-26 12:01:21 +08:00
|
|
|
|
|
|
|
// Check for an existing instance in read-only mode.
|
|
|
|
{
|
2020-08-08 04:29:11 +08:00
|
|
|
llvm::sys::SmartScopedReader<true> typeLock(storageUniquer.mutex);
|
2020-08-19 06:59:53 +08:00
|
|
|
auto it = storageUniquer.instances.find_as(lookupKey);
|
|
|
|
if (it != storageUniquer.instances.end())
|
2020-08-08 13:31:25 +08:00
|
|
|
return it->storage;
|
2019-04-26 12:01:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Acquire a writer-lock so that we can safely create the new type instance.
|
2020-08-08 04:29:11 +08:00
|
|
|
llvm::sys::SmartScopedWriter<true> typeLock(storageUniquer.mutex);
|
2020-08-19 06:59:53 +08:00
|
|
|
return getOrCreateUnsafe(storageUniquer, lookupKey, ctorFn);
|
2020-05-03 03:28:57 +08:00
|
|
|
}
|
2020-08-08 04:29:11 +08:00
|
|
|
/// Get or create an instance of a complex derived type in an thread-unsafe
|
|
|
|
/// fashion.
|
2020-05-03 03:28:57 +08:00
|
|
|
BaseStorage *
|
2020-08-19 06:59:53 +08:00
|
|
|
getOrCreateUnsafe(ParametricStorageUniquer &storageUniquer,
|
|
|
|
ParametricStorageUniquer::LookupKey &lookupKey,
|
2020-05-03 03:28:57 +08:00
|
|
|
function_ref<BaseStorage *(StorageAllocator &)> ctorFn) {
|
2020-08-19 06:59:53 +08:00
|
|
|
auto existing = storageUniquer.instances.insert_as({}, lookupKey);
|
2019-04-26 12:01:21 +08:00
|
|
|
if (!existing.second)
|
|
|
|
return existing.first->storage;
|
|
|
|
|
|
|
|
// Otherwise, construct and initialize the derived storage for this type
|
|
|
|
// instance.
|
2020-08-19 06:59:53 +08:00
|
|
|
BaseStorage *storage = ctorFn(storageUniquer.allocator);
|
2020-08-08 13:31:25 +08:00
|
|
|
*existing.first =
|
2020-08-19 06:59:53 +08:00
|
|
|
ParametricStorageUniquer::HashedStorage{lookupKey.hashValue, storage};
|
2020-08-08 13:31:25 +08:00
|
|
|
return storage;
|
2019-04-26 12:01:21 +08:00
|
|
|
}
|
|
|
|
|
2020-08-19 06:59:53 +08:00
|
|
|
/// Erase an instance of a parametric derived type.
|
|
|
|
void erase(TypeID id, unsigned hashValue,
|
2019-12-19 01:28:48 +08:00
|
|
|
function_ref<bool(const BaseStorage *)> isEqual,
|
|
|
|
function_ref<void(BaseStorage *)> cleanupFn) {
|
2020-08-19 06:59:53 +08:00
|
|
|
assert(parametricUniquers.count(id) &&
|
|
|
|
"erasing unregistered storage instance");
|
|
|
|
ParametricStorageUniquer &storageUniquer = *parametricUniquers[id];
|
|
|
|
ParametricStorageUniquer::LookupKey lookupKey{hashValue, isEqual};
|
2019-05-01 01:31:29 +08:00
|
|
|
|
|
|
|
// Acquire a writer-lock so that we can safely erase the type instance.
|
2020-08-08 04:29:11 +08:00
|
|
|
llvm::sys::SmartScopedWriter<true> lock(storageUniquer.mutex);
|
2020-08-19 06:59:53 +08:00
|
|
|
auto existing = storageUniquer.instances.find_as(lookupKey);
|
|
|
|
if (existing == storageUniquer.instances.end())
|
2019-05-01 01:31:29 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Cleanup the storage and remove it from the map.
|
|
|
|
cleanupFn(existing->storage);
|
2020-08-19 06:59:53 +08:00
|
|
|
storageUniquer.instances.erase(existing);
|
2019-05-01 01:31:29 +08:00
|
|
|
}
|
|
|
|
|
2020-07-22 19:03:24 +08:00
|
|
|
/// Mutates an instance of a derived storage in a thread-safe way.
|
|
|
|
LogicalResult
|
2020-08-08 04:29:11 +08:00
|
|
|
mutate(TypeID id,
|
|
|
|
function_ref<LogicalResult(StorageAllocator &)> mutationFn) {
|
2020-08-19 06:59:53 +08:00
|
|
|
assert(parametricUniquers.count(id) &&
|
|
|
|
"mutating unregistered storage instance");
|
|
|
|
ParametricStorageUniquer &storageUniquer = *parametricUniquers[id];
|
2020-07-22 19:03:24 +08:00
|
|
|
if (!threadingIsEnabled)
|
2020-08-08 04:29:11 +08:00
|
|
|
return mutationFn(storageUniquer.allocator);
|
2020-07-22 19:03:24 +08:00
|
|
|
|
2020-08-08 04:29:11 +08:00
|
|
|
llvm::sys::SmartScopedWriter<true> lock(storageUniquer.mutex);
|
|
|
|
return mutationFn(storageUniquer.allocator);
|
2020-07-22 19:03:24 +08:00
|
|
|
}
|
|
|
|
|
2019-04-26 12:01:21 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
2020-08-19 06:59:53 +08:00
|
|
|
// Singleton Storage
|
2019-04-26 12:01:21 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2020-08-19 06:59:53 +08:00
|
|
|
/// Get or create an instance of a singleton storage class.
|
|
|
|
BaseStorage *getSingleton(TypeID id) {
|
|
|
|
BaseStorage *singletonInstance = singletonInstances[id];
|
|
|
|
assert(singletonInstance && "expected singleton instance to exist");
|
|
|
|
return singletonInstance;
|
2019-04-26 12:01:21 +08:00
|
|
|
}
|
|
|
|
|
2020-09-09 00:53:24 +08:00
|
|
|
/// Check if an instance of a singleton storage class exists.
|
|
|
|
bool hasSingleton(TypeID id) { return singletonInstances.count(id); }
|
|
|
|
|
2020-08-19 06:59:53 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Instance Storage
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2020-08-08 04:29:11 +08:00
|
|
|
/// Map of type ids to the storage uniquer to use for registered objects.
|
2020-08-19 06:59:53 +08:00
|
|
|
DenseMap<TypeID, std::unique_ptr<ParametricStorageUniquer>>
|
|
|
|
parametricUniquers;
|
|
|
|
|
|
|
|
/// Map of type ids to a singleton instance when the storage class is a
|
|
|
|
/// singleton.
|
|
|
|
DenseMap<TypeID, BaseStorage *> singletonInstances;
|
|
|
|
|
|
|
|
/// Allocator used for uniquing singleton instances.
|
|
|
|
StorageAllocator singletonAllocator;
|
2020-05-03 03:28:57 +08:00
|
|
|
|
|
|
|
/// Flag specifying if multi-threading is enabled within the uniquer.
|
|
|
|
bool threadingIsEnabled = true;
|
2019-04-26 12:01:21 +08:00
|
|
|
};
|
|
|
|
} // end namespace detail
|
|
|
|
} // namespace mlir
|
|
|
|
|
|
|
|
StorageUniquer::StorageUniquer() : impl(new StorageUniquerImpl()) {}
|
|
|
|
StorageUniquer::~StorageUniquer() {}
|
|
|
|
|
2020-05-03 03:28:57 +08:00
|
|
|
/// Set the flag specifying if multi-threading is disabled within the uniquer.
|
|
|
|
void StorageUniquer::disableMultithreading(bool disable) {
|
|
|
|
impl->threadingIsEnabled = !disable;
|
|
|
|
}
|
|
|
|
|
2019-04-26 12:01:21 +08:00
|
|
|
/// Implementation for getting/creating an instance of a derived type with
|
2020-08-19 06:59:53 +08:00
|
|
|
/// parametric storage.
|
|
|
|
auto StorageUniquer::getParametricStorageTypeImpl(
|
|
|
|
TypeID id, unsigned hashValue,
|
2019-12-19 01:28:48 +08:00
|
|
|
function_ref<bool(const BaseStorage *)> isEqual,
|
2020-04-12 14:00:11 +08:00
|
|
|
function_ref<BaseStorage *(StorageAllocator &)> ctorFn) -> BaseStorage * {
|
2020-08-19 06:59:53 +08:00
|
|
|
return impl->getOrCreate(id, hashValue, isEqual, ctorFn);
|
2019-04-26 12:01:21 +08:00
|
|
|
}
|
|
|
|
|
2020-08-19 06:59:53 +08:00
|
|
|
/// Implementation for registering an instance of a derived type with
|
|
|
|
/// parametric storage.
|
|
|
|
void StorageUniquer::registerParametricStorageTypeImpl(TypeID id) {
|
|
|
|
impl->parametricUniquers.try_emplace(
|
|
|
|
id, std::make_unique<ParametricStorageUniquer>());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Implementation for getting an instance of a derived type with default
|
|
|
|
/// storage.
|
|
|
|
auto StorageUniquer::getSingletonImpl(TypeID id) -> BaseStorage * {
|
|
|
|
return impl->getSingleton(id);
|
|
|
|
}
|
|
|
|
|
2020-09-09 00:53:24 +08:00
|
|
|
/// Test is the storage singleton is initialized.
|
|
|
|
bool StorageUniquer::isSingletonStorageInitialized(TypeID id) {
|
|
|
|
return impl->hasSingleton(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Test is the parametric storage is initialized.
|
|
|
|
bool StorageUniquer::isParametricStorageInitialized(TypeID id) {
|
|
|
|
return impl->hasParametricStorage(id);
|
|
|
|
}
|
|
|
|
|
2020-08-19 06:59:53 +08:00
|
|
|
/// Implementation for registering an instance of a derived type with default
|
|
|
|
/// storage.
|
|
|
|
void StorageUniquer::registerSingletonImpl(
|
|
|
|
TypeID id, function_ref<BaseStorage *(StorageAllocator &)> ctorFn) {
|
|
|
|
assert(!impl->singletonInstances.count(id) &&
|
|
|
|
"storage class already registered");
|
|
|
|
impl->singletonInstances.try_emplace(id, ctorFn(impl->singletonAllocator));
|
2019-04-26 12:01:21 +08:00
|
|
|
}
|
2019-05-01 01:31:29 +08:00
|
|
|
|
2020-08-19 06:59:53 +08:00
|
|
|
/// Implementation for erasing an instance of a derived type with parametric
|
2019-05-01 01:31:29 +08:00
|
|
|
/// storage.
|
2020-08-19 06:59:53 +08:00
|
|
|
void StorageUniquer::eraseImpl(TypeID id, unsigned hashValue,
|
2019-12-19 01:28:48 +08:00
|
|
|
function_ref<bool(const BaseStorage *)> isEqual,
|
2020-04-12 14:00:11 +08:00
|
|
|
function_ref<void(BaseStorage *)> cleanupFn) {
|
2020-08-19 06:59:53 +08:00
|
|
|
impl->erase(id, hashValue, isEqual, cleanupFn);
|
2019-05-01 01:31:29 +08:00
|
|
|
}
|
2020-07-22 19:03:24 +08:00
|
|
|
|
|
|
|
/// Implementation for mutating an instance of a derived storage.
|
|
|
|
LogicalResult StorageUniquer::mutateImpl(
|
2020-08-19 06:59:53 +08:00
|
|
|
TypeID id, function_ref<LogicalResult(StorageAllocator &)> mutationFn) {
|
2020-08-08 04:29:11 +08:00
|
|
|
return impl->mutate(id, mutationFn);
|
2020-07-22 19:03:24 +08:00
|
|
|
}
|