9 #ifndef MLIR_SUPPORT_STORAGEUNIQUER_H 10 #define MLIR_SUPPORT_STORAGEUNIQUER_H 13 #include "llvm/ADT/DenseMap.h" 14 #include "llvm/ADT/DenseSet.h" 15 #include "llvm/Support/Allocator.h" 19 struct StorageUniquerImpl;
22 template <
typename ImplTy,
typename... Args>
26 template <
typename ImplTy,
typename T>
96 auto result = allocator.Allocate<T>(elements.size());
97 std::uninitialized_copy(elements.begin(), elements.end(), result);
105 return StringRef(result.data(), str.size());
109 template <
typename T> T *
allocate() {
return allocator.Allocate<T>(); }
113 return allocator.Allocate(size, alignment);
118 llvm::BumpPtrAllocator allocator;
125 template <
typename Storage,
typename Arg,
typename... Args>
126 Storage *
get(std::function<void(Storage *)> initFn,
unsigned kind, Arg &&arg,
130 getKey<Storage>(std::forward<Arg>(arg), std::forward<Args>(args)...);
133 unsigned hashValue = getHash<Storage>(kind, derivedKey);
136 std::function<bool(const BaseStorage *)> isEqual =
138 return static_cast<const Storage &
>(*existing) == derivedKey;
142 std::function<BaseStorage *(StorageAllocator &)> ctorFn =
144 auto *storage = Storage::construct(allocator, derivedKey);
151 return static_cast<Storage *
>(getImpl(kind, hashValue, isEqual, ctorFn));
158 template <
typename Storage>
159 Storage *
get(std::function<void(Storage *)> initFn,
unsigned kind) {
161 auto *storage =
new (allocator.allocate<Storage>()) Storage();
166 return static_cast<Storage *
>(getImpl(kind, ctorFn));
171 template <
typename Storage,
typename Arg,
typename... Args>
172 void erase(
unsigned kind, Arg &&arg, Args &&... args) {
175 getKey<Storage>(std::forward<Arg>(arg), std::forward<Args>(args)...);
178 unsigned hashValue = getHash<Storage>(kind, derivedKey);
181 std::function<bool(const BaseStorage *)> isEqual =
183 return static_cast<const Storage &
>(*existing) == derivedKey;
187 eraseImpl(kind, hashValue, isEqual, [](
BaseStorage *storage) {
188 static_cast<Storage *
>(storage)->cleanup();
195 BaseStorage *getImpl(
unsigned kind,
unsigned hashValue,
206 void eraseImpl(
unsigned kind,
unsigned hashValue,
211 std::unique_ptr<detail::StorageUniquerImpl> impl;
219 template <
typename ImplTy,
typename... Args>
220 static typename std::enable_if<
222 typename ImplTy::KeyTy>::type
223 getKey(Args &&... args) {
224 return ImplTy::getKey(args...);
228 template <
typename ImplTy,
typename... Args>
229 static typename std::enable_if<
231 typename ImplTy::KeyTy>::type
232 getKey(Args &&... args) {
233 return typename ImplTy::KeyTy(args...);
242 template <
typename ImplTy,
typename DerivedKey>
243 static typename std::enable_if<
245 ::llvm::hash_code>::type
246 getHash(
unsigned kind,
const DerivedKey &derivedKey) {
247 return llvm::hash_combine(kind, ImplTy::hashKey(derivedKey));
251 template <
typename ImplTy,
typename DerivedKey>
252 static typename std::enable_if<
253 !is_detected<detail::has_impltype_hash_t, ImplTy, DerivedKey>::value,
254 ::llvm::hash_code>::type
255 getHash(
unsigned kind,
const DerivedKey &derivedKey) {
256 return llvm::hash_combine(
Definition: InferTypeOpInterface.cpp:20
decltype(ImplTy::hashKey(std::declval< T >())) has_impltype_hash_t
Trait to check if ImplTy provides a 'hashKey' method for 'T'.
Definition: StorageUniquer.h:27
This is the implementation of the StorageUniquer class.
Definition: StorageUniquer.cpp:20
StringRef copyInto(StringRef str)
Definition: StorageUniquer.h:103
Definition: StorageUniquer.h:89
T * allocate()
Allocate an instance of the provided type.
Definition: StorageUniquer.h:109
typename detail::detector< void, Op, Args... >::value_t is_detected
Definition: STLExtras.h:125
BaseStorage()
Definition: StorageUniquer.h:77
Definition: StorageUniquer.h:64
void erase(unsigned kind, Arg &&arg, Args &&... args)
Definition: StorageUniquer.h:172
Definition: StorageUniquer.h:71
unsigned getKind() const
Get the kind classification of this storage.
Definition: StorageUniquer.h:74
decltype(ImplTy::getKey(std::declval< Args >()...)) has_impltype_getkey_t
Trait to check if ImplTy provides a 'getKey' method with types 'Args'.
Definition: StorageUniquer.h:23
Definition: StandardTypes.h:63
ArrayRef< T > copyInto(ArrayRef< T > elements)
Definition: StorageUniquer.h:93
void * allocate(size_t size, size_t alignment)
Allocate 'size' bytes of 'alignment' aligned memory.
Definition: StorageUniquer.h:112