Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 1 | //===- 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 Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame^] | 19 | #include "AttributeListStorage.h" |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 20 | #include "mlir/IR/AffineExpr.h" |
| 21 | #include "mlir/IR/AffineMap.h" |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 22 | #include "mlir/IR/Attributes.h" |
| 23 | #include "mlir/IR/Identifier.h" |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 24 | #include "mlir/IR/OperationSet.h" |
| 25 | #include "mlir/IR/StandardOps.h" |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 26 | #include "mlir/IR/Types.h" |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 27 | #include "mlir/Support/STLExtras.h" |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame^] | 28 | #include "third_party/llvm/llvm/include/llvm/ADT/STLExtras.h" |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 29 | #include "llvm/ADT/DenseSet.h" |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 30 | #include "llvm/ADT/StringMap.h" |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 31 | #include "llvm/Support/Allocator.h" |
| 32 | using namespace mlir; |
| 33 | using namespace llvm; |
| 34 | |
| 35 | namespace { |
| 36 | struct 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 Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 54 | |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 55 | struct AffineMapKeyInfo : DenseMapInfo<AffineMap *> { |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 56 | // Affine maps are uniqued based on their dim/symbol counts and affine |
| 57 | // expressions. |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 58 | using KeyTy = std::tuple<unsigned, unsigned, ArrayRef<AffineExpr *>>; |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 59 | using DenseMapInfo<AffineMap *>::getHashValue; |
| 60 | using DenseMapInfo<AffineMap *>::isEqual; |
| 61 | |
| 62 | static unsigned getHashValue(KeyTy key) { |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 63 | return hash_combine( |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 64 | std::get<0>(key), std::get<1>(key), |
| 65 | hash_combine_range(std::get<2>(key).begin(), std::get<2>(key).end())); |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 68 | static bool isEqual(const KeyTy &lhs, const AffineMap *rhs) { |
| 69 | if (rhs == getEmptyKey() || rhs == getTombstoneKey()) |
| 70 | return false; |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 71 | return lhs == std::make_tuple(rhs->getNumDims(), rhs->getNumSymbols(), |
| 72 | rhs->getResults()); |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 73 | } |
| 74 | }; |
| 75 | |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 76 | struct 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 Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 94 | |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 95 | struct 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 Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 113 | |
| 114 | struct 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 Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame^] | 130 | |
| 131 | struct 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 Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 148 | } // end anonymous namespace. |
| 149 | |
| 150 | |
| 151 | namespace 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. |
| 154 | class MLIRContextImpl { |
| 155 | public: |
| 156 | /// We put immortal objects into this allocator. |
| 157 | llvm::BumpPtrAllocator allocator; |
| 158 | |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 159 | /// This is the set of all operations that are registered with the system. |
| 160 | OperationSet operationSet; |
| 161 | |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 162 | /// These are identifiers uniqued into this MLIRContext. |
| 163 | llvm::StringMap<char, llvm::BumpPtrAllocator&> identifiers; |
| 164 | |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 165 | // Primitive type uniquing. |
Chris Lattner | eee1a2d | 2018-07-04 09:13:39 -0700 | [diff] [blame] | 166 | PrimitiveType *primitives[int(Type::Kind::LAST_PRIMITIVE_TYPE)+1] = {nullptr}; |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 167 | |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 168 | // Affine map uniquing. |
| 169 | using AffineMapSet = DenseSet<AffineMap *, AffineMapKeyInfo>; |
| 170 | AffineMapSet affineMaps; |
| 171 | |
Uday Bondhugula | 0b80a16 | 2018-07-03 21:34:58 -0700 | [diff] [blame] | 172 | // Affine binary op expression uniquing. Figure out uniquing of dimensional |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 173 | // or symbolic identifiers. |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 174 | DenseMap<std::tuple<unsigned, AffineExpr *, AffineExpr *>, |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 175 | AffineBinaryOpExpr *> |
| 176 | affineExprs; |
| 177 | |
Chris Lattner | f958bbe | 2018-06-29 22:08:05 -0700 | [diff] [blame] | 178 | /// Integer type uniquing. |
| 179 | DenseMap<unsigned, IntegerType*> integers; |
| 180 | |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 181 | /// 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 Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 189 | /// 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 Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 197 | // 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 Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame^] | 204 | using AttributeListSet = |
| 205 | DenseSet<AttributeListStorage *, AttributeListKeyInfo>; |
| 206 | AttributeListSet attributeLists; |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 207 | |
| 208 | public: |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 209 | MLIRContextImpl() : identifiers(allocator) { |
| 210 | registerStandardOperations(operationSet); |
| 211 | } |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 212 | |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 213 | /// 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 | |
| 224 | MLIRContext::MLIRContext() : impl(new MLIRContextImpl()) { |
| 225 | } |
| 226 | |
| 227 | MLIRContext::~MLIRContext() { |
| 228 | } |
| 229 | |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 230 | /// Return the operation set associated with the specified MLIRContext object. |
| 231 | OperationSet &OperationSet::get(MLIRContext *context) { |
| 232 | return context->getImpl().operationSet; |
| 233 | } |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 234 | |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 235 | //===----------------------------------------------------------------------===// |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 236 | // Identifier uniquing |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 237 | //===----------------------------------------------------------------------===// |
| 238 | |
| 239 | /// Return an identifier for the specified string. |
| 240 | Identifier 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 Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 250 | //===----------------------------------------------------------------------===// |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 251 | // Type uniquing |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 252 | //===----------------------------------------------------------------------===// |
| 253 | |
Chris Lattner | eee1a2d | 2018-07-04 09:13:39 -0700 | [diff] [blame] | 254 | PrimitiveType *PrimitiveType::get(Kind kind, MLIRContext *context) { |
| 255 | assert(kind <= Kind::LAST_PRIMITIVE_TYPE && "Not a primitive type kind"); |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 256 | 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 Lattner | f958bbe | 2018-06-29 22:08:05 -0700 | [diff] [blame] | 272 | IntegerType *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 Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | FunctionType *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 Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 314 | VectorType *VectorType::get(ArrayRef<unsigned> shape, Type *elementType) { |
| 315 | assert(!shape.empty() && "vector types must have at least one dimension"); |
Chris Lattner | f958bbe | 2018-06-29 22:08:05 -0700 | [diff] [blame] | 316 | assert((isa<PrimitiveType>(elementType) || isa<IntegerType>(elementType)) && |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 317 | "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 Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 342 | |
| 343 | |
Chris Lattner | eee1a2d | 2018-07-04 09:13:39 -0700 | [diff] [blame] | 344 | TensorType::TensorType(Kind kind, Type *elementType, MLIRContext *context) |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 345 | : Type(kind, context), elementType(elementType) { |
Chris Lattner | f958bbe | 2018-06-29 22:08:05 -0700 | [diff] [blame] | 346 | assert((isa<PrimitiveType>(elementType) || isa<VectorType>(elementType) || |
| 347 | isa<IntegerType>(elementType)) && |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 348 | "tensor elements must be primitives or vectors"); |
| 349 | assert(isa<TensorType>(this)); |
| 350 | } |
| 351 | |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 352 | RankedTensorType *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 | |
| 378 | UnrankedTensorType *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 Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 383 | auto *&result = impl.unrankedTensors[elementType]; |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 384 | |
| 385 | // If we already have it, return that value. |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 386 | if (result) |
| 387 | return result; |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 388 | |
| 389 | // On the first use, we allocate them into the bump pointer. |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 390 | result = impl.allocator.Allocate<UnrankedTensorType>(); |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 391 | |
| 392 | // Initialize the memory using placement new. |
| 393 | new (result) UnrankedTensorType(elementType, context); |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 394 | return result; |
| 395 | } |
| 396 | |
| 397 | //===----------------------------------------------------------------------===// |
| 398 | // Attribute uniquing |
| 399 | //===----------------------------------------------------------------------===// |
| 400 | |
| 401 | BoolAttr *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 | |
| 411 | IntegerAttr *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 | |
| 421 | FloatAttr *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 | |
| 439 | StringAttr *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 | |
| 451 | ArrayAttr *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 Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 469 | |
| 470 | // Cache and return it. |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 471 | return *existing.first = result; |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 472 | } |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 473 | |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame^] | 474 | /// Perform a three-way comparison between the names of the specified |
| 475 | /// NamedAttributes. |
| 476 | static 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. |
| 484 | AttributeListStorage *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 Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 545 | //===----------------------------------------------------------------------===// |
| 546 | // AffineMap and AffineExpr uniquing |
| 547 | //===----------------------------------------------------------------------===// |
| 548 | |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 549 | AffineMap *AffineMap::get(unsigned dimCount, unsigned symbolCount, |
| 550 | ArrayRef<AffineExpr *> results, |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 551 | MLIRContext *context) { |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 552 | // 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 Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 558 | auto key = std::make_tuple(dimCount, symbolCount, results); |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 559 | 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 Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | AffineBinaryOpExpr *AffineBinaryOpExpr::get(AffineExpr::Kind kind, |
| 579 | AffineExpr *lhsOperand, |
| 580 | AffineExpr *rhsOperand, |
| 581 | MLIRContext *context) { |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 582 | auto &impl = context->getImpl(); |
| 583 | |
| 584 | // Check if we already have this affine expression. |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 585 | auto keyValue = std::make_tuple((unsigned)kind, lhsOperand, rhsOperand); |
| 586 | auto *&result = impl.affineExprs[keyValue]; |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 587 | |
| 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 Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 599 | // TODO(bondhugula): complete uniquing of remaining AffineExpr sub-classes. |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 600 | AffineAddExpr *AffineAddExpr::get(AffineExpr *lhsOperand, |
| 601 | AffineExpr *rhsOperand, |
| 602 | MLIRContext *context) { |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 603 | return cast<AffineAddExpr>( |
| 604 | AffineBinaryOpExpr::get(Kind::Add, lhsOperand, rhsOperand, context)); |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 607 | AffineSubExpr *AffineSubExpr::get(AffineExpr *lhsOperand, |
| 608 | AffineExpr *rhsOperand, |
| 609 | MLIRContext *context) { |
| 610 | return cast<AffineSubExpr>( |
| 611 | AffineBinaryOpExpr::get(Kind::Sub, lhsOperand, rhsOperand, context)); |
| 612 | } |
| 613 | |
| 614 | AffineMulExpr *AffineMulExpr::get(AffineExpr *lhsOperand, |
| 615 | AffineExpr *rhsOperand, |
| 616 | MLIRContext *context) { |
| 617 | return cast<AffineMulExpr>( |
| 618 | AffineBinaryOpExpr::get(Kind::Mul, lhsOperand, rhsOperand, context)); |
| 619 | } |
| 620 | |
| 621 | AffineFloorDivExpr *AffineFloorDivExpr::get(AffineExpr *lhsOperand, |
| 622 | AffineExpr *rhsOperand, |
| 623 | MLIRContext *context) { |
| 624 | return cast<AffineFloorDivExpr>( |
| 625 | AffineBinaryOpExpr::get(Kind::FloorDiv, lhsOperand, rhsOperand, context)); |
| 626 | } |
| 627 | |
| 628 | AffineCeilDivExpr *AffineCeilDivExpr::get(AffineExpr *lhsOperand, |
| 629 | AffineExpr *rhsOperand, |
| 630 | MLIRContext *context) { |
| 631 | return cast<AffineCeilDivExpr>( |
| 632 | AffineBinaryOpExpr::get(Kind::CeilDiv, lhsOperand, rhsOperand, context)); |
| 633 | } |
| 634 | |
| 635 | AffineModExpr *AffineModExpr::get(AffineExpr *lhsOperand, |
| 636 | AffineExpr *rhsOperand, |
| 637 | MLIRContext *context) { |
| 638 | return cast<AffineModExpr>( |
| 639 | AffineBinaryOpExpr::get(Kind::Mod, lhsOperand, rhsOperand, context)); |
| 640 | } |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 641 | |
| 642 | AffineDimExpr *AffineDimExpr::get(unsigned position, MLIRContext *context) { |
| 643 | // TODO(bondhugula): complete this |
| 644 | // FIXME: this should be POD |
| 645 | return new AffineDimExpr(position); |
| 646 | } |
| 647 | |
| 648 | AffineSymbolExpr *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 | |
| 655 | AffineConstantExpr *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 | } |