blob: 697cae32abea3c617e17ad4f4b5af2455db9cf9d [file] [log] [blame]
Chris Lattnerf7e22732018-06-22 22:03:48 -07001//===- MLIRContext.cpp - MLIR Type Classes --------------------------------===//
2//
3// Copyright 2019 The MLIR Authors.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16// =============================================================================
17
18#include "mlir/IR/MLIRContext.h"
Chris Lattnerdf1a2fc2018-07-05 21:20:59 -070019#include "AttributeListStorage.h"
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -070020#include "mlir/IR/AffineExpr.h"
21#include "mlir/IR/AffineMap.h"
Chris Lattner36b4ed12018-07-04 10:43:29 -070022#include "mlir/IR/Attributes.h"
23#include "mlir/IR/Identifier.h"
Chris Lattnerff0d5902018-07-05 09:12:11 -070024#include "mlir/IR/OperationSet.h"
25#include "mlir/IR/StandardOps.h"
Chris Lattnerf7e22732018-06-22 22:03:48 -070026#include "mlir/IR/Types.h"
Chris Lattner36b4ed12018-07-04 10:43:29 -070027#include "mlir/Support/STLExtras.h"
Chris Lattnerdf1a2fc2018-07-05 21:20:59 -070028#include "third_party/llvm/llvm/include/llvm/ADT/STLExtras.h"
Chris Lattnerf7e22732018-06-22 22:03:48 -070029#include "llvm/ADT/DenseSet.h"
Chris Lattnered65a732018-06-28 20:45:33 -070030#include "llvm/ADT/StringMap.h"
Chris Lattnerf7e22732018-06-22 22:03:48 -070031#include "llvm/Support/Allocator.h"
32using namespace mlir;
33using namespace llvm;
34
35namespace {
36struct FunctionTypeKeyInfo : DenseMapInfo<FunctionType*> {
37 // Functions are uniqued based on their inputs and results.
38 using KeyTy = std::pair<ArrayRef<Type*>, ArrayRef<Type*>>;
39 using DenseMapInfo<FunctionType*>::getHashValue;
40 using DenseMapInfo<FunctionType*>::isEqual;
41
42 static unsigned getHashValue(KeyTy key) {
43 return hash_combine(hash_combine_range(key.first.begin(), key.first.end()),
44 hash_combine_range(key.second.begin(),
45 key.second.end()));
46 }
47
48 static bool isEqual(const KeyTy &lhs, const FunctionType *rhs) {
49 if (rhs == getEmptyKey() || rhs == getTombstoneKey())
50 return false;
51 return lhs == KeyTy(rhs->getInputs(), rhs->getResults());
52 }
53};
Uday Bondhugula015cbb12018-07-03 20:16:08 -070054
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -070055struct AffineMapKeyInfo : DenseMapInfo<AffineMap *> {
Uday Bondhugula015cbb12018-07-03 20:16:08 -070056 // Affine maps are uniqued based on their dim/symbol counts and affine
57 // expressions.
Chris Lattner36b4ed12018-07-04 10:43:29 -070058 using KeyTy = std::tuple<unsigned, unsigned, ArrayRef<AffineExpr *>>;
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -070059 using DenseMapInfo<AffineMap *>::getHashValue;
60 using DenseMapInfo<AffineMap *>::isEqual;
61
62 static unsigned getHashValue(KeyTy key) {
Uday Bondhugula015cbb12018-07-03 20:16:08 -070063 return hash_combine(
Chris Lattner36b4ed12018-07-04 10:43:29 -070064 std::get<0>(key), std::get<1>(key),
65 hash_combine_range(std::get<2>(key).begin(), std::get<2>(key).end()));
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -070066 }
67
Uday Bondhugula015cbb12018-07-03 20:16:08 -070068 static bool isEqual(const KeyTy &lhs, const AffineMap *rhs) {
69 if (rhs == getEmptyKey() || rhs == getTombstoneKey())
70 return false;
Chris Lattner36b4ed12018-07-04 10:43:29 -070071 return lhs == std::make_tuple(rhs->getNumDims(), rhs->getNumSymbols(),
72 rhs->getResults());
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -070073 }
74};
75
Chris Lattnerf7e22732018-06-22 22:03:48 -070076struct VectorTypeKeyInfo : DenseMapInfo<VectorType*> {
77 // Vectors are uniqued based on their element type and shape.
78 using KeyTy = std::pair<Type*, ArrayRef<unsigned>>;
79 using DenseMapInfo<VectorType*>::getHashValue;
80 using DenseMapInfo<VectorType*>::isEqual;
81
82 static unsigned getHashValue(KeyTy key) {
83 return hash_combine(DenseMapInfo<Type*>::getHashValue(key.first),
84 hash_combine_range(key.second.begin(),
85 key.second.end()));
86 }
87
88 static bool isEqual(const KeyTy &lhs, const VectorType *rhs) {
89 if (rhs == getEmptyKey() || rhs == getTombstoneKey())
90 return false;
91 return lhs == KeyTy(rhs->getElementType(), rhs->getShape());
92 }
93};
Chris Lattner36b4ed12018-07-04 10:43:29 -070094
MLIR Team355ec862018-06-23 18:09:09 -070095struct RankedTensorTypeKeyInfo : DenseMapInfo<RankedTensorType*> {
96 // Ranked tensors are uniqued based on their element type and shape.
97 using KeyTy = std::pair<Type*, ArrayRef<int>>;
98 using DenseMapInfo<RankedTensorType*>::getHashValue;
99 using DenseMapInfo<RankedTensorType*>::isEqual;
100
101 static unsigned getHashValue(KeyTy key) {
102 return hash_combine(DenseMapInfo<Type*>::getHashValue(key.first),
103 hash_combine_range(key.second.begin(),
104 key.second.end()));
105 }
106
107 static bool isEqual(const KeyTy &lhs, const RankedTensorType *rhs) {
108 if (rhs == getEmptyKey() || rhs == getTombstoneKey())
109 return false;
110 return lhs == KeyTy(rhs->getElementType(), rhs->getShape());
111 }
112};
Chris Lattner36b4ed12018-07-04 10:43:29 -0700113
114struct ArrayAttrKeyInfo : DenseMapInfo<ArrayAttr*> {
115 // Array attributes are uniqued based on their elements.
116 using KeyTy = ArrayRef<Attribute*>;
117 using DenseMapInfo<ArrayAttr*>::getHashValue;
118 using DenseMapInfo<ArrayAttr*>::isEqual;
119
120 static unsigned getHashValue(KeyTy key) {
121 return hash_combine_range(key.begin(), key.end());
122 }
123
124 static bool isEqual(const KeyTy &lhs, const ArrayAttr *rhs) {
125 if (rhs == getEmptyKey() || rhs == getTombstoneKey())
126 return false;
127 return lhs == rhs->getValue();
128 }
129};
Chris Lattnerdf1a2fc2018-07-05 21:20:59 -0700130
131struct AttributeListKeyInfo : DenseMapInfo<AttributeListStorage *> {
132 // Array attributes are uniqued based on their elements.
133 using KeyTy = ArrayRef<NamedAttribute>;
134 using DenseMapInfo<AttributeListStorage *>::getHashValue;
135 using DenseMapInfo<AttributeListStorage *>::isEqual;
136
137 static unsigned getHashValue(KeyTy key) {
138 return hash_combine_range(key.begin(), key.end());
139 }
140
141 static bool isEqual(const KeyTy &lhs, const AttributeListStorage *rhs) {
142 if (rhs == getEmptyKey() || rhs == getTombstoneKey())
143 return false;
144 return lhs == rhs->getElements();
145 }
146};
147
Chris Lattnerf7e22732018-06-22 22:03:48 -0700148} // end anonymous namespace.
149
150
151namespace mlir {
152/// This is the implementation of the MLIRContext class, using the pImpl idiom.
153/// This class is completely private to this file, so everything is public.
154class MLIRContextImpl {
155public:
156 /// We put immortal objects into this allocator.
157 llvm::BumpPtrAllocator allocator;
158
Chris Lattnerff0d5902018-07-05 09:12:11 -0700159 /// This is the set of all operations that are registered with the system.
160 OperationSet operationSet;
161
Chris Lattnered65a732018-06-28 20:45:33 -0700162 /// These are identifiers uniqued into this MLIRContext.
163 llvm::StringMap<char, llvm::BumpPtrAllocator&> identifiers;
164
Chris Lattnerf7e22732018-06-22 22:03:48 -0700165 // Primitive type uniquing.
Chris Lattnereee1a2d2018-07-04 09:13:39 -0700166 PrimitiveType *primitives[int(Type::Kind::LAST_PRIMITIVE_TYPE)+1] = {nullptr};
Chris Lattnerf7e22732018-06-22 22:03:48 -0700167
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700168 // Affine map uniquing.
169 using AffineMapSet = DenseSet<AffineMap *, AffineMapKeyInfo>;
170 AffineMapSet affineMaps;
171
Uday Bondhugula0b80a162018-07-03 21:34:58 -0700172 // Affine binary op expression uniquing. Figure out uniquing of dimensional
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700173 // or symbolic identifiers.
Chris Lattner36b4ed12018-07-04 10:43:29 -0700174 DenseMap<std::tuple<unsigned, AffineExpr *, AffineExpr *>,
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700175 AffineBinaryOpExpr *>
176 affineExprs;
177
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700178 /// Integer type uniquing.
179 DenseMap<unsigned, IntegerType*> integers;
180
Chris Lattnerf7e22732018-06-22 22:03:48 -0700181 /// Function type uniquing.
182 using FunctionTypeSet = DenseSet<FunctionType*, FunctionTypeKeyInfo>;
183 FunctionTypeSet functions;
184
185 /// Vector type uniquing.
186 using VectorTypeSet = DenseSet<VectorType*, VectorTypeKeyInfo>;
187 VectorTypeSet vectors;
188
MLIR Team355ec862018-06-23 18:09:09 -0700189 /// Ranked tensor type uniquing.
190 using RankedTensorTypeSet = DenseSet<RankedTensorType*,
191 RankedTensorTypeKeyInfo>;
192 RankedTensorTypeSet rankedTensors;
193
194 /// Unranked tensor type uniquing.
195 DenseMap<Type*, UnrankedTensorType*> unrankedTensors;
196
Chris Lattner36b4ed12018-07-04 10:43:29 -0700197 // Attribute uniquing.
198 BoolAttr *boolAttrs[2] = { nullptr };
199 DenseMap<int64_t, IntegerAttr*> integerAttrs;
200 DenseMap<int64_t, FloatAttr*> floatAttrs;
201 StringMap<StringAttr*> stringAttrs;
202 using ArrayAttrSet = DenseSet<ArrayAttr*, ArrayAttrKeyInfo>;
203 ArrayAttrSet arrayAttrs;
Chris Lattnerdf1a2fc2018-07-05 21:20:59 -0700204 using AttributeListSet =
205 DenseSet<AttributeListStorage *, AttributeListKeyInfo>;
206 AttributeListSet attributeLists;
Chris Lattnerf7e22732018-06-22 22:03:48 -0700207
208public:
Chris Lattnerff0d5902018-07-05 09:12:11 -0700209 MLIRContextImpl() : identifiers(allocator) {
210 registerStandardOperations(operationSet);
211 }
Chris Lattnered65a732018-06-28 20:45:33 -0700212
Chris Lattnerf7e22732018-06-22 22:03:48 -0700213 /// Copy the specified array of elements into memory managed by our bump
214 /// pointer allocator. This assumes the elements are all PODs.
215 template<typename T>
216 ArrayRef<T> copyInto(ArrayRef<T> elements) {
217 auto result = allocator.Allocate<T>(elements.size());
218 std::uninitialized_copy(elements.begin(), elements.end(), result);
219 return ArrayRef<T>(result, elements.size());
220 }
221};
222} // end namespace mlir
223
224MLIRContext::MLIRContext() : impl(new MLIRContextImpl()) {
225}
226
227MLIRContext::~MLIRContext() {
228}
229
Chris Lattnerff0d5902018-07-05 09:12:11 -0700230/// Return the operation set associated with the specified MLIRContext object.
231OperationSet &OperationSet::get(MLIRContext *context) {
232 return context->getImpl().operationSet;
233}
Chris Lattnerf7e22732018-06-22 22:03:48 -0700234
Chris Lattnered65a732018-06-28 20:45:33 -0700235//===----------------------------------------------------------------------===//
Chris Lattner36b4ed12018-07-04 10:43:29 -0700236// Identifier uniquing
Chris Lattnered65a732018-06-28 20:45:33 -0700237//===----------------------------------------------------------------------===//
238
239/// Return an identifier for the specified string.
240Identifier Identifier::get(StringRef str, const MLIRContext *context) {
241 assert(!str.empty() && "Cannot create an empty identifier");
242 assert(str.find('\0') == StringRef::npos &&
243 "Cannot create an identifier with a nul character");
244
245 auto &impl = context->getImpl();
246 auto it = impl.identifiers.insert({str, char()}).first;
247 return Identifier(it->getKeyData());
248}
249
Chris Lattnered65a732018-06-28 20:45:33 -0700250//===----------------------------------------------------------------------===//
Chris Lattner36b4ed12018-07-04 10:43:29 -0700251// Type uniquing
Chris Lattnered65a732018-06-28 20:45:33 -0700252//===----------------------------------------------------------------------===//
253
Chris Lattnereee1a2d2018-07-04 09:13:39 -0700254PrimitiveType *PrimitiveType::get(Kind kind, MLIRContext *context) {
255 assert(kind <= Kind::LAST_PRIMITIVE_TYPE && "Not a primitive type kind");
Chris Lattnerf7e22732018-06-22 22:03:48 -0700256 auto &impl = context->getImpl();
257
258 // We normally have these types.
259 if (impl.primitives[(int)kind])
260 return impl.primitives[(int)kind];
261
262 // On the first use, we allocate them into the bump pointer.
263 auto *ptr = impl.allocator.Allocate<PrimitiveType>();
264
265 // Initialize the memory using placement new.
266 new(ptr) PrimitiveType(kind, context);
267
268 // Cache and return it.
269 return impl.primitives[(int)kind] = ptr;
270}
271
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700272IntegerType *IntegerType::get(unsigned width, MLIRContext *context) {
273 auto &impl = context->getImpl();
274
275 auto *&result = impl.integers[width];
276 if (!result) {
277 result = impl.allocator.Allocate<IntegerType>();
278 new (result) IntegerType(width, context);
279 }
280
281 return result;
Chris Lattnerf7e22732018-06-22 22:03:48 -0700282}
283
284FunctionType *FunctionType::get(ArrayRef<Type*> inputs, ArrayRef<Type*> results,
285 MLIRContext *context) {
286 auto &impl = context->getImpl();
287
288 // Look to see if we already have this function type.
289 FunctionTypeKeyInfo::KeyTy key(inputs, results);
290 auto existing = impl.functions.insert_as(nullptr, key);
291
292 // If we already have it, return that value.
293 if (!existing.second)
294 return *existing.first;
295
296 // On the first use, we allocate them into the bump pointer.
297 auto *result = impl.allocator.Allocate<FunctionType>();
298
299 // Copy the inputs and results into the bump pointer.
300 SmallVector<Type*, 16> types;
301 types.reserve(inputs.size()+results.size());
302 types.append(inputs.begin(), inputs.end());
303 types.append(results.begin(), results.end());
304 auto typesList = impl.copyInto(ArrayRef<Type*>(types));
305
306 // Initialize the memory using placement new.
307 new (result) FunctionType(typesList.data(), inputs.size(), results.size(),
308 context);
309
310 // Cache and return it.
311 return *existing.first = result;
312}
313
Chris Lattnerf7e22732018-06-22 22:03:48 -0700314VectorType *VectorType::get(ArrayRef<unsigned> shape, Type *elementType) {
315 assert(!shape.empty() && "vector types must have at least one dimension");
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700316 assert((isa<PrimitiveType>(elementType) || isa<IntegerType>(elementType)) &&
Chris Lattnerf7e22732018-06-22 22:03:48 -0700317 "vectors elements must be primitives");
318
319 auto *context = elementType->getContext();
320 auto &impl = context->getImpl();
321
322 // Look to see if we already have this vector type.
323 VectorTypeKeyInfo::KeyTy key(elementType, shape);
324 auto existing = impl.vectors.insert_as(nullptr, key);
325
326 // If we already have it, return that value.
327 if (!existing.second)
328 return *existing.first;
329
330 // On the first use, we allocate them into the bump pointer.
331 auto *result = impl.allocator.Allocate<VectorType>();
332
333 // Copy the shape into the bump pointer.
334 shape = impl.copyInto(shape);
335
336 // Initialize the memory using placement new.
337 new (result) VectorType(shape, cast<PrimitiveType>(elementType), context);
338
339 // Cache and return it.
340 return *existing.first = result;
341}
MLIR Team355ec862018-06-23 18:09:09 -0700342
343
Chris Lattnereee1a2d2018-07-04 09:13:39 -0700344TensorType::TensorType(Kind kind, Type *elementType, MLIRContext *context)
MLIR Team355ec862018-06-23 18:09:09 -0700345 : Type(kind, context), elementType(elementType) {
Chris Lattnerf958bbe2018-06-29 22:08:05 -0700346 assert((isa<PrimitiveType>(elementType) || isa<VectorType>(elementType) ||
347 isa<IntegerType>(elementType)) &&
MLIR Team355ec862018-06-23 18:09:09 -0700348 "tensor elements must be primitives or vectors");
349 assert(isa<TensorType>(this));
350}
351
MLIR Team355ec862018-06-23 18:09:09 -0700352RankedTensorType *RankedTensorType::get(ArrayRef<int> shape,
353 Type *elementType) {
354 auto *context = elementType->getContext();
355 auto &impl = context->getImpl();
356
357 // Look to see if we already have this ranked tensor type.
358 RankedTensorTypeKeyInfo::KeyTy key(elementType, shape);
359 auto existing = impl.rankedTensors.insert_as(nullptr, key);
360
361 // If we already have it, return that value.
362 if (!existing.second)
363 return *existing.first;
364
365 // On the first use, we allocate them into the bump pointer.
366 auto *result = impl.allocator.Allocate<RankedTensorType>();
367
368 // Copy the shape into the bump pointer.
369 shape = impl.copyInto(shape);
370
371 // Initialize the memory using placement new.
372 new (result) RankedTensorType(shape, elementType, context);
373
374 // Cache and return it.
375 return *existing.first = result;
376}
377
378UnrankedTensorType *UnrankedTensorType::get(Type *elementType) {
379 auto *context = elementType->getContext();
380 auto &impl = context->getImpl();
381
382 // Look to see if we already have this unranked tensor type.
Chris Lattner36b4ed12018-07-04 10:43:29 -0700383 auto *&result = impl.unrankedTensors[elementType];
MLIR Team355ec862018-06-23 18:09:09 -0700384
385 // If we already have it, return that value.
Chris Lattner36b4ed12018-07-04 10:43:29 -0700386 if (result)
387 return result;
MLIR Team355ec862018-06-23 18:09:09 -0700388
389 // On the first use, we allocate them into the bump pointer.
Chris Lattner36b4ed12018-07-04 10:43:29 -0700390 result = impl.allocator.Allocate<UnrankedTensorType>();
MLIR Team355ec862018-06-23 18:09:09 -0700391
392 // Initialize the memory using placement new.
393 new (result) UnrankedTensorType(elementType, context);
Chris Lattner36b4ed12018-07-04 10:43:29 -0700394 return result;
395}
396
397//===----------------------------------------------------------------------===//
398// Attribute uniquing
399//===----------------------------------------------------------------------===//
400
401BoolAttr *BoolAttr::get(bool value, MLIRContext *context) {
402 auto *&result = context->getImpl().boolAttrs[value];
403 if (result)
404 return result;
405
406 result = context->getImpl().allocator.Allocate<BoolAttr>();
407 new (result) BoolAttr(value);
408 return result;
409}
410
411IntegerAttr *IntegerAttr::get(int64_t value, MLIRContext *context) {
412 auto *&result = context->getImpl().integerAttrs[value];
413 if (result)
414 return result;
415
416 result = context->getImpl().allocator.Allocate<IntegerAttr>();
417 new (result) IntegerAttr(value);
418 return result;
419}
420
421FloatAttr *FloatAttr::get(double value, MLIRContext *context) {
422 // We hash based on the bit representation of the double to ensure we don't
423 // merge things like -0.0 and 0.0 in the hash comparison.
424 union {
425 double floatValue;
426 int64_t intValue;
427 };
428 floatValue = value;
429
430 auto *&result = context->getImpl().floatAttrs[intValue];
431 if (result)
432 return result;
433
434 result = context->getImpl().allocator.Allocate<FloatAttr>();
435 new (result) FloatAttr(value);
436 return result;
437}
438
439StringAttr *StringAttr::get(StringRef bytes, MLIRContext *context) {
440 auto it = context->getImpl().stringAttrs.insert({bytes, nullptr}).first;
441
442 if (it->second)
443 return it->second;
444
445 auto result = context->getImpl().allocator.Allocate<StringAttr>();
446 new (result) StringAttr(it->first());
447 it->second = result;
448 return result;
449}
450
451ArrayAttr *ArrayAttr::get(ArrayRef<Attribute*> value, MLIRContext *context) {
452 auto &impl = context->getImpl();
453
454 // Look to see if we already have this.
455 auto existing = impl.arrayAttrs.insert_as(nullptr, value);
456
457 // If we already have it, return that value.
458 if (!existing.second)
459 return *existing.first;
460
461 // On the first use, we allocate them into the bump pointer.
462 auto *result = impl.allocator.Allocate<ArrayAttr>();
463
464 // Copy the elements into the bump pointer.
465 value = impl.copyInto(value);
466
467 // Initialize the memory using placement new.
468 new (result) ArrayAttr(value);
MLIR Team355ec862018-06-23 18:09:09 -0700469
470 // Cache and return it.
Chris Lattner36b4ed12018-07-04 10:43:29 -0700471 return *existing.first = result;
MLIR Team355ec862018-06-23 18:09:09 -0700472}
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700473
Chris Lattnerdf1a2fc2018-07-05 21:20:59 -0700474/// Perform a three-way comparison between the names of the specified
475/// NamedAttributes.
476static int compareNamedAttributes(const NamedAttribute *lhs,
477 const NamedAttribute *rhs) {
478 return lhs->first.str().compare(rhs->first.str());
479}
480
481/// Given a list of NamedAttribute's, canonicalize the list (sorting
482/// by name) and return the unique'd result. Note that the empty list is
483/// represented with a null pointer.
484AttributeListStorage *AttributeListStorage::get(ArrayRef<NamedAttribute> attrs,
485 MLIRContext *context) {
486 // We need to sort the element list to canonicalize it, but we also don't want
487 // to do a ton of work in the super common case where the element list is
488 // already sorted.
489 SmallVector<NamedAttribute, 8> storage;
490 switch (attrs.size()) {
491 case 0:
492 // An empty list is represented with a null pointer.
493 return nullptr;
494 case 1:
495 // A single element is already sorted.
496 break;
497 case 2:
498 // Don't invoke a general sort for two element case.
499 if (attrs[0].first.str() > attrs[1].first.str()) {
500 storage.push_back(attrs[1]);
501 storage.push_back(attrs[0]);
502 attrs = storage;
503 }
504 break;
505 default:
506 // Check to see they are sorted already.
507 bool isSorted = true;
508 for (unsigned i = 0, e = attrs.size() - 1; i != e; ++i) {
509 if (attrs[i].first.str() > attrs[i + 1].first.str()) {
510 isSorted = false;
511 break;
512 }
513 }
514 // If not, do a general sort.
515 if (!isSorted) {
516 storage.append(attrs.begin(), attrs.end());
517 llvm::array_pod_sort(storage.begin(), storage.end(),
518 compareNamedAttributes);
519 attrs = storage;
520 }
521 }
522
523 // Ok, now that we've canonicalized our attributes, unique them.
524 auto &impl = context->getImpl();
525
526 // Look to see if we already have this.
527 auto existing = impl.attributeLists.insert_as(nullptr, attrs);
528
529 // If we already have it, return that value.
530 if (!existing.second)
531 return *existing.first;
532
533 // Otherwise, allocate a new AttributeListStorage, unique it and return it.
534 auto byteSize =
535 AttributeListStorage::totalSizeToAlloc<NamedAttribute>(attrs.size());
536 auto rawMem = impl.allocator.Allocate(byteSize, alignof(NamedAttribute));
537
538 // Placement initialize the AggregateSymbolicValue.
539 auto result = ::new (rawMem) AttributeListStorage(attrs.size());
540 std::uninitialized_copy(attrs.begin(), attrs.end(),
541 result->getTrailingObjects<NamedAttribute>());
542 return *existing.first = result;
543}
544
Chris Lattner36b4ed12018-07-04 10:43:29 -0700545//===----------------------------------------------------------------------===//
546// AffineMap and AffineExpr uniquing
547//===----------------------------------------------------------------------===//
548
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700549AffineMap *AffineMap::get(unsigned dimCount, unsigned symbolCount,
550 ArrayRef<AffineExpr *> results,
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700551 MLIRContext *context) {
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700552 // The number of results can't be zero.
553 assert(!results.empty());
554
555 auto &impl = context->getImpl();
556
557 // Check if we already have this affine map.
Chris Lattner36b4ed12018-07-04 10:43:29 -0700558 auto key = std::make_tuple(dimCount, symbolCount, results);
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700559 auto existing = impl.affineMaps.insert_as(nullptr, key);
560
561 // If we already have it, return that value.
562 if (!existing.second)
563 return *existing.first;
564
565 // On the first use, we allocate them into the bump pointer.
566 auto *res = impl.allocator.Allocate<AffineMap>();
567
568 // Copy the results into the bump pointer.
569 results = impl.copyInto(ArrayRef<AffineExpr *>(results));
570
571 // Initialize the memory using placement new.
572 new (res) AffineMap(dimCount, symbolCount, results.size(), results.data());
573
574 // Cache and return it.
575 return *existing.first = res;
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700576}
577
578AffineBinaryOpExpr *AffineBinaryOpExpr::get(AffineExpr::Kind kind,
579 AffineExpr *lhsOperand,
580 AffineExpr *rhsOperand,
581 MLIRContext *context) {
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700582 auto &impl = context->getImpl();
583
584 // Check if we already have this affine expression.
Chris Lattner36b4ed12018-07-04 10:43:29 -0700585 auto keyValue = std::make_tuple((unsigned)kind, lhsOperand, rhsOperand);
586 auto *&result = impl.affineExprs[keyValue];
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700587
588 // If we already have it, return that value.
589 if (!result) {
590 // On the first use, we allocate them into the bump pointer.
591 result = impl.allocator.Allocate<AffineBinaryOpExpr>();
592
593 // Initialize the memory using placement new.
594 new (result) AffineBinaryOpExpr(kind, lhsOperand, rhsOperand);
595 }
596 return result;
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700597}
598
Chris Lattner36b4ed12018-07-04 10:43:29 -0700599// TODO(bondhugula): complete uniquing of remaining AffineExpr sub-classes.
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700600AffineAddExpr *AffineAddExpr::get(AffineExpr *lhsOperand,
601 AffineExpr *rhsOperand,
602 MLIRContext *context) {
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700603 return cast<AffineAddExpr>(
604 AffineBinaryOpExpr::get(Kind::Add, lhsOperand, rhsOperand, context));
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700605}
606
Uday Bondhugula015cbb12018-07-03 20:16:08 -0700607AffineSubExpr *AffineSubExpr::get(AffineExpr *lhsOperand,
608 AffineExpr *rhsOperand,
609 MLIRContext *context) {
610 return cast<AffineSubExpr>(
611 AffineBinaryOpExpr::get(Kind::Sub, lhsOperand, rhsOperand, context));
612}
613
614AffineMulExpr *AffineMulExpr::get(AffineExpr *lhsOperand,
615 AffineExpr *rhsOperand,
616 MLIRContext *context) {
617 return cast<AffineMulExpr>(
618 AffineBinaryOpExpr::get(Kind::Mul, lhsOperand, rhsOperand, context));
619}
620
621AffineFloorDivExpr *AffineFloorDivExpr::get(AffineExpr *lhsOperand,
622 AffineExpr *rhsOperand,
623 MLIRContext *context) {
624 return cast<AffineFloorDivExpr>(
625 AffineBinaryOpExpr::get(Kind::FloorDiv, lhsOperand, rhsOperand, context));
626}
627
628AffineCeilDivExpr *AffineCeilDivExpr::get(AffineExpr *lhsOperand,
629 AffineExpr *rhsOperand,
630 MLIRContext *context) {
631 return cast<AffineCeilDivExpr>(
632 AffineBinaryOpExpr::get(Kind::CeilDiv, lhsOperand, rhsOperand, context));
633}
634
635AffineModExpr *AffineModExpr::get(AffineExpr *lhsOperand,
636 AffineExpr *rhsOperand,
637 MLIRContext *context) {
638 return cast<AffineModExpr>(
639 AffineBinaryOpExpr::get(Kind::Mod, lhsOperand, rhsOperand, context));
640}
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -0700641
642AffineDimExpr *AffineDimExpr::get(unsigned position, MLIRContext *context) {
643 // TODO(bondhugula): complete this
644 // FIXME: this should be POD
645 return new AffineDimExpr(position);
646}
647
648AffineSymbolExpr *AffineSymbolExpr::get(unsigned position,
649 MLIRContext *context) {
650 // TODO(bondhugula): complete this
651 // FIXME: this should be POD
652 return new AffineSymbolExpr(position);
653}
654
655AffineConstantExpr *AffineConstantExpr::get(int64_t constant,
656 MLIRContext *context) {
657 // TODO(bondhugula): complete this
658 // FIXME: this should be POD
659 return new AffineConstantExpr(constant);
660}