13#ifndef ATTRIBUTEDETAIL_H_
14#define ATTRIBUTEDETAIL_H_
23#include "llvm/ADT/APFloat.h"
24#include "llvm/Support/Allocator.h"
36 if (
auto denseEltType = llvm::dyn_cast<DenseElementType>(eltType))
37 return denseEltType.getDenseElementBitSize();
38 llvm_unreachable(
"unsupported element type");
81 size_t storageSize = llvm::divideCeil(elementWidth, CHAR_BIT);
84 if (
data.size() == storageSize)
87 assert(((
data.size() / storageSize) ==
88 static_cast<size_t>(ty.getNumElements())) &&
89 "data does not hold expected number of elements");
92 auto firstElt =
data.take_front(storageSize);
93 auto hashVal = llvm::hash_value(firstElt);
97 for (
size_t i = storageSize, e =
data.size(); i != e; i += storageSize)
98 if (memcmp(
data.data(), &
data[i], storageSize))
99 return KeyTy(ty,
data, llvm::hash_combine(hashVal,
data.drop_front(i)));
102 return KeyTy(ty, firstElt, hashVal);
117 char *rawData =
reinterpret_cast<char *
>(
119 std::memcpy(rawData,
data.data(),
data.size());
167 if (
data.size() == 1)
171 const auto &firstElt =
data.front();
172 auto hashVal = llvm::hash_value(firstElt);
176 for (
size_t i = 1, e =
data.size(); i != e; ++i)
177 if (firstElt !=
data[i])
178 return KeyTy(ty,
data, llvm::hash_combine(hashVal,
data.drop_front(i)));
181 return KeyTy(ty,
data.take_front(), hashVal);
200 size_t numEntries =
data.size();
204 size_t dataSize =
sizeof(StringRef) * numEntries;
205 for (
size_t i = 0; i < numEntries; ++i)
206 dataSize +=
data[i].size();
208 char *rawData =
reinterpret_cast<char *
>(
209 allocator.
allocate(dataSize,
alignof(uint64_t)));
214 reinterpret_cast<StringRef *
>(rawData), numEntries);
215 auto *stringData = rawData + numEntries *
sizeof(StringRef);
217 for (
size_t i = 0; i < numEntries; ++i) {
219 mutableCopy[i] = StringRef(stringData,
data[i].size());
220 stringData +=
data[i].size();
242 using KeyTy = std::pair<StringRef, Type>;
244 return value == key.first &&
type == key.second;
295 template <
typename T,
typename... Args>
297 static_assert(std::is_same_v<typename T::ImplType, DistinctAttrStorage>,
298 "expects a distinct attribute storage");
300 context, std::forward<Args>(args)...);
324 std::scoped_lock<std::mutex> guard(allocatorMutex);
332 llvm::BumpPtrAllocator allocator;
335 std::mutex allocatorMutex;
static void copy(Location loc, Value dst, Value src, Value size, OpBuilder &builder)
Copies the given number of bytes from src to dst pointers.
static const AbstractAttribute & lookup(TypeID typeID, MLIRContext *context)
Look up the specified abstract attribute in the MLIRContext and return a reference to it.
Base storage class appearing in an attribute.
void initializeAbstractAttribute(const AbstractAttribute &abstractAttr)
Set the abstract attribute for this storage instance.
Attributes are known-constant values of operations.
TypeID getTypeID()
Return a unique identifier for the concrete attribute type.
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
MLIRContext is the top-level object for a collection of MLIR operations.
ArrayRef< T > copyInto(ArrayRef< T > elements)
Copy the specified array of elements into memory managed by our bump pointer allocator.
T * allocate()
Allocate an instance of the provided type.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
DistinctAttrStorage * allocate(Attribute referencedAttr)
DistinctAttributeAllocator(const DistinctAttributeAllocator &)=delete
DistinctAttributeAllocator()=default
DistinctAttributeAllocator & operator=(const DistinctAttributeAllocator &)=delete
DistinctAttributeAllocator(DistinctAttributeAllocator &&)=delete
A specialized attribute uniquer for distinct attributes that always allocates since the distinct attr...
static T get(MLIRContext *context, Args &&...args)
Creates a distinct attribute storage.
size_t getDenseElementBitWidth(Type eltType)
Return the bit width which DenseElementsAttr should use for this type.
Include the generated interface declarations.
llvm::DenseMapInfo< T, Enable > DenseMapInfo
StorageUniquer::StorageAllocator AttributeStorageAllocator
DenseElementsAttributeStorage(ShapedType type)
ShapedType type
The type of the dense elements.
ArrayRef< StringRef > data
The raw buffer for the data storage.
KeyTy(ShapedType type, ArrayRef< StringRef > data, llvm::hash_code hashCode)
llvm::hash_code hashCode
The computed hash code for the storage data.
An attribute representing a reference to a dense vector or tensor object containing strings.
ArrayRef< StringRef > data
bool operator==(const KeyTy &key) const
Compare this storage instance with the provided key.
static KeyTy getKey(ShapedType ty, ArrayRef< StringRef > data)
Construct a key from a shaped type and StringRef data buffer.
DenseStringElementsAttrStorage(ShapedType ty, ArrayRef< StringRef > data)
static DenseStringElementsAttrStorage * construct(AttributeStorageAllocator &allocator, KeyTy key)
Construct a new storage instance.
static llvm::hash_code hashKey(const KeyTy &key)
Hash the key for the storage.
KeyTy(ShapedType type, ArrayRef< char > data, llvm::hash_code hashCode)
ArrayRef< char > data
The raw buffer for the data storage.
ShapedType type
The type of the dense elements.
llvm::hash_code hashCode
The computed hash code for the storage data.
An attribute representing a reference to a dense vector or tensor object.
DenseTypedElementsAttrStorage(ShapedType ty, ArrayRef< char > data)
bool operator==(const KeyTy &key) const
Compare this storage instance with the provided key.
static DenseTypedElementsAttrStorage * construct(AttributeStorageAllocator &allocator, KeyTy key)
Construct a new storage instance.
static llvm::hash_code hashKey(const KeyTy &key)
Hash the key for the storage.
static KeyTy getKey(ShapedType ty, ArrayRef< char > data)
Construct a key from a shaped type and raw data buffer.
An attribute to store a distinct reference to another attribute.
DistinctAttrStorage(Attribute referencedAttr)
Attribute referencedAttr
The referenced attribute.
KeyTy getAsKey() const
Returns the referenced attribute as key.
Type type
The type of the string.
StringRef value
The raw string value.
bool operator==(const KeyTy &key) const
::llvm::hash_code hashKey(const KeyTy &key)
StringAttrStorage(StringRef value, Type type)
static StringAttrStorage * construct(AttributeStorageAllocator &allocator, const KeyTy &key)
Define a construction method for creating a new instance of this storage.
Dialect * referencedDialect
If the string value contains a dialect namespace prefix (e.g.
std::pair< StringRef, Type > KeyTy
The hash key is a tuple of the parameter types.
void initialize(MLIRContext *context)
Initialize the storage given an MLIRContext.