1//===- AttributeDetail.h - MLIR Affine Map details Class --------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This holds implementation details of Attribute.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef ATTRIBUTEDETAIL_H_
14#define ATTRIBUTEDETAIL_H_
15
16#include "mlir/IR/AffineMap.h"
17#include "mlir/IR/AttributeSupport.h"
18#include "mlir/IR/BuiltinAttributes.h"
19#include "mlir/IR/BuiltinTypeInterfaces.h"
20#include "mlir/IR/BuiltinTypes.h"
21#include "mlir/IR/IntegerSet.h"
22#include "mlir/IR/MLIRContext.h"
23#include "llvm/ADT/APFloat.h"
24#include "llvm/Support/Allocator.h"
25#include "llvm/Support/RWMutex.h"
26#include <mutex>
27
28namespace mlir {
29namespace detail {
30
31//===----------------------------------------------------------------------===//
32// Elements Attributes
33//===----------------------------------------------------------------------===//
34
35/// Return the bit width which DenseElementsAttr should use for this type.
36inline size_t getDenseElementBitWidth(Type eltType) {
37 if (auto denseEltType = llvm::dyn_cast<DenseElementType>(Val&: eltType))
38 return denseEltType.getDenseElementBitSize();
39 llvm_unreachable("unsupported element type");
40}
41
42/// An attribute representing a reference to a dense vector or tensor object.
43struct DenseElementsAttributeStorage : public AttributeStorage {
44public:
45 DenseElementsAttributeStorage(ShapedType type) : type(type) {}
46
47 ShapedType type;
48};
49
50/// An attribute representing a reference to a dense vector or tensor object.
51struct DenseTypedElementsAttrStorage : public DenseElementsAttributeStorage {
52 DenseTypedElementsAttrStorage(ShapedType ty, ArrayRef<char> data)
53 : DenseElementsAttributeStorage(ty), data(data) {}
54
55 struct KeyTy {
56 KeyTy(ShapedType type, ArrayRef<char> data, llvm::hash_code hashCode)
57 : type(type), data(data), hashCode(hashCode) {}
58
59 /// The type of the dense elements.
60 ShapedType type;
61
62 /// The raw buffer for the data storage.
63 ArrayRef<char> data;
64
65 /// The computed hash code for the storage data.
66 llvm::hash_code hashCode;
67 };
68
69 /// Compare this storage instance with the provided key.
70 bool operator==(const KeyTy &key) const {
71 return key.type == type && key.data == data;
72 }
73
74 /// Construct a key from a shaped type and raw data buffer.
75 static KeyTy getKey(ShapedType ty, ArrayRef<char> data) {
76 // Handle an empty storage instance.
77 if (data.empty())
78 return KeyTy(ty, data, 0);
79
80 size_t elementWidth = getDenseElementBitWidth(eltType: ty.getElementType());
81 // Dense elements are padded to 8-bits.
82 size_t storageSize = llvm::divideCeil(Numerator: elementWidth, CHAR_BIT);
83
84 // If the data buffer holds a single element, it is a known splat.
85 if (data.size() == storageSize)
86 return KeyTy(ty, data, llvm::hash_value(S: data));
87
88 assert(((data.size() / storageSize) ==
89 static_cast<size_t>(ty.getNumElements())) &&
90 "data does not hold expected number of elements");
91
92 // Create the initial hash value with just the first element.
93 auto firstElt = data.take_front(N: storageSize);
94 auto hashVal = llvm::hash_value(S: firstElt);
95
96 // Check to see if this storage represents a splat. If it doesn't then
97 // combine the hash for the data starting with the first non splat element.
98 for (size_t i = storageSize, e = data.size(); i != e; i += storageSize)
99 if (memcmp(s1: data.data(), s2: &data[i], n: storageSize))
100 return KeyTy(ty, data, llvm::hash_combine(args: hashVal, args: data.drop_front(N: i)));
101
102 // Otherwise, this is a splat so just return the hash of the first element.
103 return KeyTy(ty, firstElt, hashVal);
104 }
105
106 /// Hash the key for the storage.
107 static llvm::hash_code hashKey(const KeyTy &key) {
108 return llvm::hash_combine(args: key.type, args: key.hashCode);
109 }
110
111 /// Construct a new storage instance.
112 static DenseTypedElementsAttrStorage *
113 construct(AttributeStorageAllocator &allocator, KeyTy key) {
114 // If the data buffer is non-empty, we copy it into the allocator with a
115 // 64-bit alignment.
116 ArrayRef<char> copy, data = key.data;
117 if (!data.empty()) {
118 char *rawData = reinterpret_cast<char *>(
119 allocator.allocate(size: data.size(), alignment: alignof(uint64_t)));
120 std::memcpy(dest: rawData, src: data.data(), n: data.size());
121 copy = ArrayRef<char>(rawData, data.size());
122 }
123
124 return new (allocator.allocate<DenseTypedElementsAttrStorage>())
125 DenseTypedElementsAttrStorage(key.type, copy);
126 }
127
128 ArrayRef<char> data;
129};
130
131/// An attribute representing a reference to a dense vector or tensor object
132/// containing strings.
133struct DenseStringElementsAttrStorage : public DenseElementsAttributeStorage {
134 DenseStringElementsAttrStorage(ShapedType ty, ArrayRef<StringRef> data)
135 : DenseElementsAttributeStorage(ty), data(data) {}
136
137 struct KeyTy {
138 KeyTy(ShapedType type, ArrayRef<StringRef> data, llvm::hash_code hashCode)
139 : type(type), data(data), hashCode(hashCode) {}
140
141 /// The type of the dense elements.
142 ShapedType type;
143
144 /// The raw buffer for the data storage.
145 ArrayRef<StringRef> data;
146
147 /// The computed hash code for the storage data.
148 llvm::hash_code hashCode;
149 };
150
151 /// Compare this storage instance with the provided key.
152 bool operator==(const KeyTy &key) const {
153 if (key.type != type)
154 return false;
155
156 // Otherwise, we can default to just checking the data. StringRefs compare
157 // by contents.
158 return key.data == data;
159 }
160
161 /// Construct a key from a shaped type and StringRef data buffer.
162 static KeyTy getKey(ShapedType ty, ArrayRef<StringRef> data) {
163 // Handle an empty storage instance.
164 if (data.empty())
165 return KeyTy(ty, data, 0);
166
167 // If the data buffer holds a single element, it is a known splat.
168 if (data.size() == 1)
169 return KeyTy(ty, data, llvm::hash_value(S: data.front()));
170
171 // Create the initial hash value with just the first element.
172 const auto &firstElt = data.front();
173 auto hashVal = llvm::hash_value(S: firstElt);
174
175 // Check to see if this storage represents a splat. If it doesn't then
176 // combine the hash for the data starting with the first non splat element.
177 for (size_t i = 1, e = data.size(); i != e; ++i)
178 if (firstElt != data[i])
179 return KeyTy(ty, data, llvm::hash_combine(args: hashVal, args: data.drop_front(N: i)));
180
181 // Otherwise, this is a splat so just return the hash of the first element.
182 return KeyTy(ty, data.take_front(), hashVal);
183 }
184
185 /// Hash the key for the storage.
186 static llvm::hash_code hashKey(const KeyTy &key) {
187 return llvm::hash_combine(args: key.type, args: key.hashCode);
188 }
189
190 /// Construct a new storage instance.
191 static DenseStringElementsAttrStorage *
192 construct(AttributeStorageAllocator &allocator, KeyTy key) {
193 // If the data buffer is non-empty, we copy it into the allocator with a
194 // 64-bit alignment.
195 ArrayRef<StringRef> copy, data = key.data;
196 if (data.empty()) {
197 return new (allocator.allocate<DenseStringElementsAttrStorage>())
198 DenseStringElementsAttrStorage(key.type, copy);
199 }
200
201 size_t numEntries = data.size();
202
203 // Compute the amount data needed to store the ArrayRef and StringRef
204 // contents.
205 size_t dataSize = sizeof(StringRef) * numEntries;
206 for (size_t i = 0; i < numEntries; ++i)
207 dataSize += data[i].size();
208
209 char *rawData = reinterpret_cast<char *>(
210 allocator.allocate(size: dataSize, alignment: alignof(uint64_t)));
211
212 // Setup a mutable array ref of our string refs so that we can update their
213 // contents.
214 auto mutableCopy = MutableArrayRef<StringRef>(
215 reinterpret_cast<StringRef *>(rawData), numEntries);
216 auto *stringData = rawData + numEntries * sizeof(StringRef);
217
218 for (size_t i = 0; i < numEntries; ++i) {
219 memcpy(dest: stringData, src: data[i].data(), n: data[i].size());
220 mutableCopy[i] = StringRef(stringData, data[i].size());
221 stringData += data[i].size();
222 }
223
224 copy =
225 ArrayRef<StringRef>(reinterpret_cast<StringRef *>(rawData), numEntries);
226
227 return new (allocator.allocate<DenseStringElementsAttrStorage>())
228 DenseStringElementsAttrStorage(key.type, copy);
229 }
230
231 ArrayRef<StringRef> data;
232};
233
234//===----------------------------------------------------------------------===//
235// StringAttr
236//===----------------------------------------------------------------------===//
237
238struct StringAttrStorage : public AttributeStorage {
239 StringAttrStorage(StringRef value, Type type)
240 : type(type), value(value), referencedDialect(nullptr) {}
241
242 /// The hash key is a tuple of the parameter types.
243 using KeyTy = std::pair<StringRef, Type>;
244 bool operator==(const KeyTy &key) const {
245 return value == key.first && type == key.second;
246 }
247 static ::llvm::hash_code hashKey(const KeyTy &key) {
248 return DenseMapInfo<KeyTy>::getHashValue(PairVal: key);
249 }
250
251 /// Define a construction method for creating a new instance of this
252 /// storage.
253 static StringAttrStorage *construct(AttributeStorageAllocator &allocator,
254 const KeyTy &key) {
255 return new (allocator.allocate<StringAttrStorage>())
256 StringAttrStorage(allocator.copyInto(str: key.first), key.second);
257 }
258
259 /// Initialize the storage given an MLIRContext.
260 void initialize(MLIRContext *context);
261
262 /// The type of the string.
263 Type type;
264 /// The raw string value.
265 StringRef value;
266 /// If the string value contains a dialect namespace prefix (e.g.
267 /// dialect.blah), this is the dialect referenced.
268 Dialect *referencedDialect;
269};
270
271//===----------------------------------------------------------------------===//
272// DistinctAttr
273//===----------------------------------------------------------------------===//
274
275/// An attribute to store a distinct reference to another attribute.
276struct DistinctAttrStorage : public AttributeStorage {
277 using KeyTy = Attribute;
278
279 DistinctAttrStorage(Attribute referencedAttr)
280 : referencedAttr(referencedAttr) {}
281
282 /// Returns the referenced attribute as key.
283 KeyTy getAsKey() const { return KeyTy(referencedAttr); }
284
285 /// The referenced attribute.
286 Attribute referencedAttr;
287};
288
289/// A specialized attribute uniquer for distinct attributes that always
290/// allocates since the distinct attribute instances use the address of their
291/// storage as unique identifier.
292class DistinctAttributeUniquer {
293public:
294 /// Creates a distinct attribute storage. Allocates every time since the
295 /// address of the storage serves as unique identifier.
296 template <typename T, typename... Args>
297 static T get(MLIRContext *context, Args &&...args) {
298 static_assert(std::is_same_v<typename T::ImplType, DistinctAttrStorage>,
299 "expects a distinct attribute storage");
300 DistinctAttrStorage *storage = DistinctAttributeUniquer::allocateStorage(
301 context, referencedAttr: std::forward<Args>(args)...);
302 storage->initializeAbstractAttribute(
303 abstractAttr: AbstractAttribute::lookup(typeID: DistinctAttr::getTypeID(), context));
304 return storage;
305 }
306
307private:
308 /// Allocates a distinct attribute storage.
309 static DistinctAttrStorage *allocateStorage(MLIRContext *context,
310 Attribute referencedAttr);
311};
312
313/// An allocator for distinct attribute storage instances. Uses a synchronized
314/// BumpPtrAllocator to ensure thread-safety. The allocated storage is deleted
315/// when the DistinctAttributeAllocator is destroyed.
316class DistinctAttributeAllocator final {
317public:
318 DistinctAttributeAllocator() = default;
319 DistinctAttributeAllocator(DistinctAttributeAllocator &&) = delete;
320 DistinctAttributeAllocator(const DistinctAttributeAllocator &) = delete;
321 DistinctAttributeAllocator &
322 operator=(const DistinctAttributeAllocator &) = delete;
323
324 DistinctAttrStorage *allocate(Attribute referencedAttr) {
325 llvm::sys::SmartScopedWriter<true> guard(allocatorMutex);
326 llvm::BumpPtrAllocator &alloc =
327 transientAllocator ? *transientAllocator : allocator;
328 return new (alloc.Allocate<DistinctAttrStorage>())
329 DistinctAttrStorage(referencedAttr);
330 }
331
332 void beginTransientScope() {
333 assert(!transientAllocator &&
334 "distinct attribute allocator is already in a transient scope");
335 transientAllocator = std::make_unique<llvm::BumpPtrAllocator>();
336 }
337
338 void endTransientScope() { transientAllocator.reset(); }
339
340 bool isInTransientScope() const { return transientAllocator != nullptr; }
341
342private:
343 /// Used to allocate distinct attribute storages in base layer.
344 llvm::BumpPtrAllocator allocator;
345
346 /// Used to allocate distinct attribute storages in transient layer.
347 std::unique_ptr<llvm::BumpPtrAllocator> transientAllocator;
348
349 /// Used to synchronize access to the allocator. Uses a RW mutex so that
350 /// isInTransientScope() can take a cheap reader lock.
351 mutable llvm::sys::SmartRWMutex<true> allocatorMutex;
352};
353} // namespace detail
354} // namespace mlir
355
356#endif // ATTRIBUTEDETAIL_H_
357