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" |
Uday Bondhugula | bc53562 | 2018-08-07 14:24:38 -0700 | [diff] [blame^] | 24 | #include "mlir/IR/IntegerSet.h" |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 25 | #include "mlir/IR/OperationSet.h" |
| 26 | #include "mlir/IR/StandardOps.h" |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 27 | #include "mlir/IR/Types.h" |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 28 | #include "mlir/Support/STLExtras.h" |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame] | 29 | #include "third_party/llvm/llvm/include/llvm/ADT/STLExtras.h" |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 30 | #include "llvm/ADT/DenseSet.h" |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 31 | #include "llvm/ADT/StringMap.h" |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 32 | #include "llvm/ADT/Twine.h" |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 33 | #include "llvm/Support/Allocator.h" |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 34 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 35 | using namespace mlir; |
| 36 | using namespace llvm; |
| 37 | |
| 38 | namespace { |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 39 | struct FunctionTypeKeyInfo : DenseMapInfo<FunctionType *> { |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 40 | // Functions are uniqued based on their inputs and results. |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 41 | using KeyTy = std::pair<ArrayRef<Type *>, ArrayRef<Type *>>; |
| 42 | using DenseMapInfo<FunctionType *>::getHashValue; |
| 43 | using DenseMapInfo<FunctionType *>::isEqual; |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 44 | |
| 45 | static unsigned getHashValue(KeyTy key) { |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 46 | return hash_combine( |
| 47 | hash_combine_range(key.first.begin(), key.first.end()), |
| 48 | hash_combine_range(key.second.begin(), key.second.end())); |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static bool isEqual(const KeyTy &lhs, const FunctionType *rhs) { |
| 52 | if (rhs == getEmptyKey() || rhs == getTombstoneKey()) |
| 53 | return false; |
| 54 | return lhs == KeyTy(rhs->getInputs(), rhs->getResults()); |
| 55 | } |
| 56 | }; |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 57 | |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 58 | struct AffineMapKeyInfo : DenseMapInfo<AffineMap *> { |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 59 | // Affine maps are uniqued based on their dim/symbol counts and affine |
| 60 | // expressions. |
Uday Bondhugula | 0115dbb | 2018-07-11 21:31:07 -0700 | [diff] [blame] | 61 | using KeyTy = std::tuple<unsigned, unsigned, ArrayRef<AffineExpr *>, |
| 62 | ArrayRef<AffineExpr *>>; |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 63 | using DenseMapInfo<AffineMap *>::getHashValue; |
| 64 | using DenseMapInfo<AffineMap *>::isEqual; |
| 65 | |
| 66 | static unsigned getHashValue(KeyTy key) { |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 67 | return hash_combine( |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 68 | std::get<0>(key), std::get<1>(key), |
Uday Bondhugula | 0115dbb | 2018-07-11 21:31:07 -0700 | [diff] [blame] | 69 | hash_combine_range(std::get<2>(key).begin(), std::get<2>(key).end()), |
| 70 | hash_combine_range(std::get<3>(key).begin(), std::get<3>(key).end())); |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 73 | static bool isEqual(const KeyTy &lhs, const AffineMap *rhs) { |
| 74 | if (rhs == getEmptyKey() || rhs == getTombstoneKey()) |
| 75 | return false; |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 76 | return lhs == std::make_tuple(rhs->getNumDims(), rhs->getNumSymbols(), |
Uday Bondhugula | 0115dbb | 2018-07-11 21:31:07 -0700 | [diff] [blame] | 77 | rhs->getResults(), rhs->getRangeSizes()); |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 78 | } |
| 79 | }; |
| 80 | |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 81 | struct VectorTypeKeyInfo : DenseMapInfo<VectorType *> { |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 82 | // Vectors are uniqued based on their element type and shape. |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 83 | using KeyTy = std::pair<Type *, ArrayRef<unsigned>>; |
| 84 | using DenseMapInfo<VectorType *>::getHashValue; |
| 85 | using DenseMapInfo<VectorType *>::isEqual; |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 86 | |
| 87 | static unsigned getHashValue(KeyTy key) { |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 88 | return hash_combine( |
| 89 | DenseMapInfo<Type *>::getHashValue(key.first), |
| 90 | hash_combine_range(key.second.begin(), key.second.end())); |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static bool isEqual(const KeyTy &lhs, const VectorType *rhs) { |
| 94 | if (rhs == getEmptyKey() || rhs == getTombstoneKey()) |
| 95 | return false; |
| 96 | return lhs == KeyTy(rhs->getElementType(), rhs->getShape()); |
| 97 | } |
| 98 | }; |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 99 | |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 100 | struct RankedTensorTypeKeyInfo : DenseMapInfo<RankedTensorType *> { |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 101 | // Ranked tensors are uniqued based on their element type and shape. |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 102 | using KeyTy = std::pair<Type *, ArrayRef<int>>; |
| 103 | using DenseMapInfo<RankedTensorType *>::getHashValue; |
| 104 | using DenseMapInfo<RankedTensorType *>::isEqual; |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 105 | |
| 106 | static unsigned getHashValue(KeyTy key) { |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 107 | return hash_combine( |
| 108 | DenseMapInfo<Type *>::getHashValue(key.first), |
| 109 | hash_combine_range(key.second.begin(), key.second.end())); |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static bool isEqual(const KeyTy &lhs, const RankedTensorType *rhs) { |
| 113 | if (rhs == getEmptyKey() || rhs == getTombstoneKey()) |
| 114 | return false; |
| 115 | return lhs == KeyTy(rhs->getElementType(), rhs->getShape()); |
| 116 | } |
| 117 | }; |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 118 | |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 119 | struct MemRefTypeKeyInfo : DenseMapInfo<MemRefType *> { |
MLIR Team | 718c82f | 2018-07-16 09:45:22 -0700 | [diff] [blame] | 120 | // MemRefs are uniqued based on their element type, shape, affine map |
| 121 | // composition, and memory space. |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 122 | using KeyTy = |
| 123 | std::tuple<Type *, ArrayRef<int>, ArrayRef<AffineMap *>, unsigned>; |
| 124 | using DenseMapInfo<MemRefType *>::getHashValue; |
| 125 | using DenseMapInfo<MemRefType *>::isEqual; |
MLIR Team | 718c82f | 2018-07-16 09:45:22 -0700 | [diff] [blame] | 126 | |
| 127 | static unsigned getHashValue(KeyTy key) { |
| 128 | return hash_combine( |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 129 | DenseMapInfo<Type *>::getHashValue(std::get<0>(key)), |
MLIR Team | 718c82f | 2018-07-16 09:45:22 -0700 | [diff] [blame] | 130 | hash_combine_range(std::get<1>(key).begin(), std::get<1>(key).end()), |
| 131 | hash_combine_range(std::get<2>(key).begin(), std::get<2>(key).end()), |
| 132 | std::get<3>(key)); |
| 133 | } |
| 134 | |
| 135 | static bool isEqual(const KeyTy &lhs, const MemRefType *rhs) { |
| 136 | if (rhs == getEmptyKey() || rhs == getTombstoneKey()) |
| 137 | return false; |
| 138 | return lhs == std::make_tuple(rhs->getElementType(), rhs->getShape(), |
| 139 | rhs->getAffineMaps(), rhs->getMemorySpace()); |
| 140 | } |
| 141 | }; |
| 142 | |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 143 | struct ArrayAttrKeyInfo : DenseMapInfo<ArrayAttr *> { |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 144 | // Array attributes are uniqued based on their elements. |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 145 | using KeyTy = ArrayRef<Attribute *>; |
| 146 | using DenseMapInfo<ArrayAttr *>::getHashValue; |
| 147 | using DenseMapInfo<ArrayAttr *>::isEqual; |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 148 | |
| 149 | static unsigned getHashValue(KeyTy key) { |
| 150 | return hash_combine_range(key.begin(), key.end()); |
| 151 | } |
| 152 | |
| 153 | static bool isEqual(const KeyTy &lhs, const ArrayAttr *rhs) { |
| 154 | if (rhs == getEmptyKey() || rhs == getTombstoneKey()) |
| 155 | return false; |
| 156 | return lhs == rhs->getValue(); |
| 157 | } |
| 158 | }; |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame] | 159 | |
| 160 | struct AttributeListKeyInfo : DenseMapInfo<AttributeListStorage *> { |
| 161 | // Array attributes are uniqued based on their elements. |
| 162 | using KeyTy = ArrayRef<NamedAttribute>; |
| 163 | using DenseMapInfo<AttributeListStorage *>::getHashValue; |
| 164 | using DenseMapInfo<AttributeListStorage *>::isEqual; |
| 165 | |
| 166 | static unsigned getHashValue(KeyTy key) { |
| 167 | return hash_combine_range(key.begin(), key.end()); |
| 168 | } |
| 169 | |
| 170 | static bool isEqual(const KeyTy &lhs, const AttributeListStorage *rhs) { |
| 171 | if (rhs == getEmptyKey() || rhs == getTombstoneKey()) |
| 172 | return false; |
| 173 | return lhs == rhs->getElements(); |
| 174 | } |
| 175 | }; |
| 176 | |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 177 | } // end anonymous namespace. |
| 178 | |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 179 | namespace mlir { |
| 180 | /// This is the implementation of the MLIRContext class, using the pImpl idiom. |
| 181 | /// This class is completely private to this file, so everything is public. |
| 182 | class MLIRContextImpl { |
| 183 | public: |
| 184 | /// We put immortal objects into this allocator. |
| 185 | llvm::BumpPtrAllocator allocator; |
| 186 | |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 187 | /// This is the set of all operations that are registered with the system. |
| 188 | OperationSet operationSet; |
| 189 | |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 190 | /// This is the handler to use to report diagnostics, or null if not |
| 191 | /// registered. |
| 192 | MLIRContext::DiagnosticHandlerTy diagnosticHandler; |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 193 | |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 194 | /// These are identifiers uniqued into this MLIRContext. |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 195 | llvm::StringMap<char, llvm::BumpPtrAllocator &> identifiers; |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 196 | |
Chris Lattner | c325119 | 2018-07-27 13:09:58 -0700 | [diff] [blame] | 197 | // Uniquing table for 'other' types. |
| 198 | OtherType *otherTypes[int(Type::Kind::LAST_OTHER_TYPE) - |
| 199 | int(Type::Kind::FIRST_OTHER_TYPE) + 1] = {nullptr}; |
| 200 | |
| 201 | // Uniquing table for 'float' types. |
| 202 | FloatType *floatTypes[int(Type::Kind::LAST_FLOATING_POINT_TYPE) - |
| 203 | int(Type::Kind::FIRST_FLOATING_POINT_TYPE) + 1] = { |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 204 | nullptr}; |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 205 | |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 206 | // Affine map uniquing. |
| 207 | using AffineMapSet = DenseSet<AffineMap *, AffineMapKeyInfo>; |
| 208 | AffineMapSet affineMaps; |
| 209 | |
Uday Bondhugula | 0b80a16 | 2018-07-03 21:34:58 -0700 | [diff] [blame] | 210 | // Affine binary op expression uniquing. Figure out uniquing of dimensional |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 211 | // or symbolic identifiers. |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 212 | DenseMap<std::tuple<unsigned, AffineExpr *, AffineExpr *>, AffineExpr *> |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 213 | affineExprs; |
| 214 | |
Uday Bondhugula | 4e5078b | 2018-07-24 22:34:09 -0700 | [diff] [blame] | 215 | // Uniqui'ing of AffineDimExpr, AffineSymbolExpr's by their position. |
| 216 | std::vector<AffineDimExpr *> dimExprs; |
| 217 | std::vector<AffineSymbolExpr *> symbolExprs; |
| 218 | |
| 219 | // Uniqui'ing of AffineConstantExpr using constant value as key. |
| 220 | DenseMap<int64_t, AffineConstantExpr *> constExprs; |
| 221 | |
Chris Lattner | f958bbe | 2018-06-29 22:08:05 -0700 | [diff] [blame] | 222 | /// Integer type uniquing. |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 223 | DenseMap<unsigned, IntegerType *> integers; |
Chris Lattner | f958bbe | 2018-06-29 22:08:05 -0700 | [diff] [blame] | 224 | |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 225 | /// Function type uniquing. |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 226 | using FunctionTypeSet = DenseSet<FunctionType *, FunctionTypeKeyInfo>; |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 227 | FunctionTypeSet functions; |
| 228 | |
| 229 | /// Vector type uniquing. |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 230 | using VectorTypeSet = DenseSet<VectorType *, VectorTypeKeyInfo>; |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 231 | VectorTypeSet vectors; |
| 232 | |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 233 | /// Ranked tensor type uniquing. |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 234 | using RankedTensorTypeSet = |
| 235 | DenseSet<RankedTensorType *, RankedTensorTypeKeyInfo>; |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 236 | RankedTensorTypeSet rankedTensors; |
| 237 | |
| 238 | /// Unranked tensor type uniquing. |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 239 | DenseMap<Type *, UnrankedTensorType *> unrankedTensors; |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 240 | |
MLIR Team | 718c82f | 2018-07-16 09:45:22 -0700 | [diff] [blame] | 241 | /// MemRef type uniquing. |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 242 | using MemRefTypeSet = DenseSet<MemRefType *, MemRefTypeKeyInfo>; |
MLIR Team | 718c82f | 2018-07-16 09:45:22 -0700 | [diff] [blame] | 243 | MemRefTypeSet memrefs; |
| 244 | |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 245 | // Attribute uniquing. |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 246 | BoolAttr *boolAttrs[2] = {nullptr}; |
| 247 | DenseMap<int64_t, IntegerAttr *> integerAttrs; |
| 248 | DenseMap<int64_t, FloatAttr *> floatAttrs; |
| 249 | StringMap<StringAttr *> stringAttrs; |
| 250 | using ArrayAttrSet = DenseSet<ArrayAttr *, ArrayAttrKeyInfo>; |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 251 | ArrayAttrSet arrayAttrs; |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 252 | DenseMap<AffineMap *, AffineMapAttr *> affineMapAttrs; |
James Molloy | f0d2f44 | 2018-08-03 01:54:46 -0700 | [diff] [blame] | 253 | DenseMap<Type *, TypeAttr *> typeAttrs; |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame] | 254 | using AttributeListSet = |
| 255 | DenseSet<AttributeListStorage *, AttributeListKeyInfo>; |
| 256 | AttributeListSet attributeLists; |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 257 | |
| 258 | public: |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 259 | MLIRContextImpl() : identifiers(allocator) { |
| 260 | registerStandardOperations(operationSet); |
| 261 | } |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 262 | |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 263 | /// Copy the specified array of elements into memory managed by our bump |
| 264 | /// pointer allocator. This assumes the elements are all PODs. |
James Molloy | 72b0cbe | 2018-08-01 12:55:27 -0700 | [diff] [blame] | 265 | template <typename T> |
| 266 | ArrayRef<T> copyInto(ArrayRef<T> elements) { |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 267 | auto result = allocator.Allocate<T>(elements.size()); |
| 268 | std::uninitialized_copy(elements.begin(), elements.end(), result); |
| 269 | return ArrayRef<T>(result, elements.size()); |
| 270 | } |
| 271 | }; |
| 272 | } // end namespace mlir |
| 273 | |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 274 | MLIRContext::MLIRContext() : impl(new MLIRContextImpl()) {} |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 275 | |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 276 | MLIRContext::~MLIRContext() {} |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 277 | |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 278 | /// Register an issue handler with this LLVM context. The issue handler is |
| 279 | /// passed location information if present (nullptr if not) along with a |
| 280 | /// message and a boolean that indicates whether this is an error or warning. |
| 281 | void MLIRContext::registerDiagnosticHandler( |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 282 | const DiagnosticHandlerTy &handler) { |
| 283 | getImpl().diagnosticHandler = handler; |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /// This emits a diagnostic using the registered issue handle if present, or |
| 287 | /// with the default behavior if not. The MLIR compiler should not generally |
| 288 | /// interact with this, it should use methods on Operation instead. |
| 289 | void MLIRContext::emitDiagnostic(Attribute *location, |
| 290 | const llvm::Twine &message, |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 291 | DiagnosticKind kind) const { |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 292 | // If we had a handler registered, emit the diagnostic using it. |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 293 | auto handler = getImpl().diagnosticHandler; |
| 294 | if (handler && location) |
| 295 | return handler(location, message.str(), kind); |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 296 | |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 297 | // The default behavior for notes and warnings is to ignore them. |
| 298 | if (kind != DiagnosticKind::Error) |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 299 | return; |
| 300 | |
| 301 | // The default behavior for errors is to emit them to stderr and exit. |
| 302 | llvm::errs() << message.str() << "\n"; |
| 303 | llvm::errs().flush(); |
| 304 | exit(1); |
| 305 | } |
| 306 | |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 307 | /// Return the operation set associated with the specified MLIRContext object. |
| 308 | OperationSet &OperationSet::get(MLIRContext *context) { |
| 309 | return context->getImpl().operationSet; |
| 310 | } |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 311 | |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 312 | /// If this operation has a registered operation description in the |
| 313 | /// OperationSet, return it. Otherwise return null. |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 314 | const AbstractOperation *Operation::getAbstractOperation() const { |
| 315 | return OperationSet::get(getContext()).lookup(getName().str()); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 318 | //===----------------------------------------------------------------------===// |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 319 | // Identifier uniquing |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 320 | //===----------------------------------------------------------------------===// |
| 321 | |
| 322 | /// Return an identifier for the specified string. |
| 323 | Identifier Identifier::get(StringRef str, const MLIRContext *context) { |
| 324 | assert(!str.empty() && "Cannot create an empty identifier"); |
| 325 | assert(str.find('\0') == StringRef::npos && |
| 326 | "Cannot create an identifier with a nul character"); |
| 327 | |
| 328 | auto &impl = context->getImpl(); |
| 329 | auto it = impl.identifiers.insert({str, char()}).first; |
| 330 | return Identifier(it->getKeyData()); |
| 331 | } |
| 332 | |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 333 | //===----------------------------------------------------------------------===// |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 334 | // Type uniquing |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 335 | //===----------------------------------------------------------------------===// |
| 336 | |
Chris Lattner | f958bbe | 2018-06-29 22:08:05 -0700 | [diff] [blame] | 337 | IntegerType *IntegerType::get(unsigned width, MLIRContext *context) { |
| 338 | auto &impl = context->getImpl(); |
| 339 | |
| 340 | auto *&result = impl.integers[width]; |
| 341 | if (!result) { |
| 342 | result = impl.allocator.Allocate<IntegerType>(); |
| 343 | new (result) IntegerType(width, context); |
| 344 | } |
| 345 | |
| 346 | return result; |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Chris Lattner | c325119 | 2018-07-27 13:09:58 -0700 | [diff] [blame] | 349 | FloatType *FloatType::get(Kind kind, MLIRContext *context) { |
| 350 | assert(kind >= Kind::FIRST_FLOATING_POINT_TYPE && |
| 351 | kind <= Kind::LAST_FLOATING_POINT_TYPE && "Not an FP type kind"); |
| 352 | auto &impl = context->getImpl(); |
| 353 | |
| 354 | // We normally have these types. |
| 355 | auto *&entry = |
| 356 | impl.floatTypes[(int)kind - int(Kind::FIRST_FLOATING_POINT_TYPE)]; |
| 357 | if (entry) |
| 358 | return entry; |
| 359 | |
| 360 | // On the first use, we allocate them into the bump pointer. |
| 361 | auto *ptr = impl.allocator.Allocate<FloatType>(); |
| 362 | |
| 363 | // Initialize the memory using placement new. |
| 364 | new (ptr) FloatType(kind, context); |
| 365 | |
| 366 | // Cache and return it. |
| 367 | return entry = ptr; |
| 368 | } |
| 369 | |
| 370 | OtherType *OtherType::get(Kind kind, MLIRContext *context) { |
| 371 | assert(kind >= Kind::FIRST_OTHER_TYPE && kind <= Kind::LAST_OTHER_TYPE && |
| 372 | "Not an 'other' type kind"); |
| 373 | auto &impl = context->getImpl(); |
| 374 | |
| 375 | // We normally have these types. |
| 376 | auto *&entry = impl.otherTypes[(int)kind - int(Kind::FIRST_OTHER_TYPE)]; |
| 377 | if (entry) |
| 378 | return entry; |
| 379 | |
| 380 | // On the first use, we allocate them into the bump pointer. |
| 381 | auto *ptr = impl.allocator.Allocate<OtherType>(); |
| 382 | |
| 383 | // Initialize the memory using placement new. |
| 384 | new (ptr) OtherType(kind, context); |
| 385 | |
| 386 | // Cache and return it. |
| 387 | return entry = ptr; |
| 388 | } |
| 389 | |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 390 | FunctionType *FunctionType::get(ArrayRef<Type *> inputs, |
| 391 | ArrayRef<Type *> results, |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 392 | MLIRContext *context) { |
| 393 | auto &impl = context->getImpl(); |
| 394 | |
| 395 | // Look to see if we already have this function type. |
| 396 | FunctionTypeKeyInfo::KeyTy key(inputs, results); |
| 397 | auto existing = impl.functions.insert_as(nullptr, key); |
| 398 | |
| 399 | // If we already have it, return that value. |
| 400 | if (!existing.second) |
| 401 | return *existing.first; |
| 402 | |
| 403 | // On the first use, we allocate them into the bump pointer. |
| 404 | auto *result = impl.allocator.Allocate<FunctionType>(); |
| 405 | |
| 406 | // Copy the inputs and results into the bump pointer. |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 407 | SmallVector<Type *, 16> types; |
| 408 | types.reserve(inputs.size() + results.size()); |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 409 | types.append(inputs.begin(), inputs.end()); |
| 410 | types.append(results.begin(), results.end()); |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 411 | auto typesList = impl.copyInto(ArrayRef<Type *>(types)); |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 412 | |
| 413 | // Initialize the memory using placement new. |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 414 | new (result) |
| 415 | FunctionType(typesList.data(), inputs.size(), results.size(), context); |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 416 | |
| 417 | // Cache and return it. |
| 418 | return *existing.first = result; |
| 419 | } |
| 420 | |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 421 | VectorType *VectorType::get(ArrayRef<unsigned> shape, Type *elementType) { |
| 422 | assert(!shape.empty() && "vector types must have at least one dimension"); |
Chris Lattner | c325119 | 2018-07-27 13:09:58 -0700 | [diff] [blame] | 423 | assert((isa<FloatType>(elementType) || isa<IntegerType>(elementType)) && |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 424 | "vectors elements must be primitives"); |
| 425 | |
| 426 | auto *context = elementType->getContext(); |
| 427 | auto &impl = context->getImpl(); |
| 428 | |
| 429 | // Look to see if we already have this vector type. |
| 430 | VectorTypeKeyInfo::KeyTy key(elementType, shape); |
| 431 | auto existing = impl.vectors.insert_as(nullptr, key); |
| 432 | |
| 433 | // If we already have it, return that value. |
| 434 | if (!existing.second) |
| 435 | return *existing.first; |
| 436 | |
| 437 | // On the first use, we allocate them into the bump pointer. |
| 438 | auto *result = impl.allocator.Allocate<VectorType>(); |
| 439 | |
| 440 | // Copy the shape into the bump pointer. |
| 441 | shape = impl.copyInto(shape); |
| 442 | |
| 443 | // Initialize the memory using placement new. |
Jacques Pienaar | 3cdb854 | 2018-07-23 11:48:22 -0700 | [diff] [blame] | 444 | new (result) VectorType(shape, elementType, context); |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 445 | |
| 446 | // Cache and return it. |
| 447 | return *existing.first = result; |
| 448 | } |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 449 | |
James Molloy | 72b0cbe | 2018-08-01 12:55:27 -0700 | [diff] [blame] | 450 | static bool isValidTensorElementType(Type *type, MLIRContext *context) { |
| 451 | return isa<FloatType>(type) || isa<VectorType>(type) || |
| 452 | isa<IntegerType>(type) || type == Type::getTFString(context); |
| 453 | } |
| 454 | |
Chris Lattner | eee1a2d | 2018-07-04 09:13:39 -0700 | [diff] [blame] | 455 | TensorType::TensorType(Kind kind, Type *elementType, MLIRContext *context) |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 456 | : Type(kind, context), elementType(elementType) { |
James Molloy | 72b0cbe | 2018-08-01 12:55:27 -0700 | [diff] [blame] | 457 | assert(isValidTensorElementType(elementType, context)); |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 458 | assert(isa<TensorType>(this)); |
| 459 | } |
| 460 | |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 461 | RankedTensorType *RankedTensorType::get(ArrayRef<int> shape, |
| 462 | Type *elementType) { |
| 463 | auto *context = elementType->getContext(); |
| 464 | auto &impl = context->getImpl(); |
| 465 | |
| 466 | // Look to see if we already have this ranked tensor type. |
| 467 | RankedTensorTypeKeyInfo::KeyTy key(elementType, shape); |
| 468 | auto existing = impl.rankedTensors.insert_as(nullptr, key); |
| 469 | |
| 470 | // If we already have it, return that value. |
| 471 | if (!existing.second) |
| 472 | return *existing.first; |
| 473 | |
| 474 | // On the first use, we allocate them into the bump pointer. |
| 475 | auto *result = impl.allocator.Allocate<RankedTensorType>(); |
| 476 | |
| 477 | // Copy the shape into the bump pointer. |
| 478 | shape = impl.copyInto(shape); |
| 479 | |
| 480 | // Initialize the memory using placement new. |
| 481 | new (result) RankedTensorType(shape, elementType, context); |
| 482 | |
| 483 | // Cache and return it. |
| 484 | return *existing.first = result; |
| 485 | } |
| 486 | |
| 487 | UnrankedTensorType *UnrankedTensorType::get(Type *elementType) { |
| 488 | auto *context = elementType->getContext(); |
| 489 | auto &impl = context->getImpl(); |
| 490 | |
| 491 | // Look to see if we already have this unranked tensor type. |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 492 | auto *&result = impl.unrankedTensors[elementType]; |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 493 | |
| 494 | // If we already have it, return that value. |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 495 | if (result) |
| 496 | return result; |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 497 | |
| 498 | // On the first use, we allocate them into the bump pointer. |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 499 | result = impl.allocator.Allocate<UnrankedTensorType>(); |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 500 | |
| 501 | // Initialize the memory using placement new. |
| 502 | new (result) UnrankedTensorType(elementType, context); |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 503 | return result; |
| 504 | } |
| 505 | |
MLIR Team | 718c82f | 2018-07-16 09:45:22 -0700 | [diff] [blame] | 506 | MemRefType *MemRefType::get(ArrayRef<int> shape, Type *elementType, |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 507 | ArrayRef<AffineMap *> affineMapComposition, |
MLIR Team | 718c82f | 2018-07-16 09:45:22 -0700 | [diff] [blame] | 508 | unsigned memorySpace) { |
| 509 | auto *context = elementType->getContext(); |
| 510 | auto &impl = context->getImpl(); |
| 511 | |
| 512 | // Look to see if we already have this memref type. |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 513 | auto key = |
| 514 | std::make_tuple(elementType, shape, affineMapComposition, memorySpace); |
MLIR Team | 718c82f | 2018-07-16 09:45:22 -0700 | [diff] [blame] | 515 | auto existing = impl.memrefs.insert_as(nullptr, key); |
| 516 | |
| 517 | // If we already have it, return that value. |
| 518 | if (!existing.second) |
| 519 | return *existing.first; |
| 520 | |
| 521 | // On the first use, we allocate them into the bump pointer. |
| 522 | auto *result = impl.allocator.Allocate<MemRefType>(); |
| 523 | |
| 524 | // Copy the shape into the bump pointer. |
| 525 | shape = impl.copyInto(shape); |
| 526 | |
| 527 | // Copy the affine map composition into the bump pointer. |
| 528 | // TODO(andydavis) Assert that the structure of the composition is valid. |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 529 | affineMapComposition = |
| 530 | impl.copyInto(ArrayRef<AffineMap *>(affineMapComposition)); |
MLIR Team | 718c82f | 2018-07-16 09:45:22 -0700 | [diff] [blame] | 531 | |
| 532 | // Initialize the memory using placement new. |
| 533 | new (result) MemRefType(shape, elementType, affineMapComposition, memorySpace, |
| 534 | context); |
| 535 | // Cache and return it. |
| 536 | return *existing.first = result; |
| 537 | } |
| 538 | |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 539 | //===----------------------------------------------------------------------===// |
| 540 | // Attribute uniquing |
| 541 | //===----------------------------------------------------------------------===// |
| 542 | |
| 543 | BoolAttr *BoolAttr::get(bool value, MLIRContext *context) { |
| 544 | auto *&result = context->getImpl().boolAttrs[value]; |
| 545 | if (result) |
| 546 | return result; |
| 547 | |
| 548 | result = context->getImpl().allocator.Allocate<BoolAttr>(); |
| 549 | new (result) BoolAttr(value); |
| 550 | return result; |
| 551 | } |
| 552 | |
| 553 | IntegerAttr *IntegerAttr::get(int64_t value, MLIRContext *context) { |
| 554 | auto *&result = context->getImpl().integerAttrs[value]; |
| 555 | if (result) |
| 556 | return result; |
| 557 | |
| 558 | result = context->getImpl().allocator.Allocate<IntegerAttr>(); |
| 559 | new (result) IntegerAttr(value); |
| 560 | return result; |
| 561 | } |
| 562 | |
| 563 | FloatAttr *FloatAttr::get(double value, MLIRContext *context) { |
| 564 | // We hash based on the bit representation of the double to ensure we don't |
| 565 | // merge things like -0.0 and 0.0 in the hash comparison. |
| 566 | union { |
| 567 | double floatValue; |
| 568 | int64_t intValue; |
| 569 | }; |
| 570 | floatValue = value; |
| 571 | |
| 572 | auto *&result = context->getImpl().floatAttrs[intValue]; |
| 573 | if (result) |
| 574 | return result; |
| 575 | |
| 576 | result = context->getImpl().allocator.Allocate<FloatAttr>(); |
| 577 | new (result) FloatAttr(value); |
| 578 | return result; |
| 579 | } |
| 580 | |
| 581 | StringAttr *StringAttr::get(StringRef bytes, MLIRContext *context) { |
| 582 | auto it = context->getImpl().stringAttrs.insert({bytes, nullptr}).first; |
| 583 | |
| 584 | if (it->second) |
| 585 | return it->second; |
| 586 | |
| 587 | auto result = context->getImpl().allocator.Allocate<StringAttr>(); |
| 588 | new (result) StringAttr(it->first()); |
| 589 | it->second = result; |
| 590 | return result; |
| 591 | } |
| 592 | |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 593 | ArrayAttr *ArrayAttr::get(ArrayRef<Attribute *> value, MLIRContext *context) { |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 594 | auto &impl = context->getImpl(); |
| 595 | |
| 596 | // Look to see if we already have this. |
| 597 | auto existing = impl.arrayAttrs.insert_as(nullptr, value); |
| 598 | |
| 599 | // If we already have it, return that value. |
| 600 | if (!existing.second) |
| 601 | return *existing.first; |
| 602 | |
| 603 | // On the first use, we allocate them into the bump pointer. |
| 604 | auto *result = impl.allocator.Allocate<ArrayAttr>(); |
| 605 | |
| 606 | // Copy the elements into the bump pointer. |
| 607 | value = impl.copyInto(value); |
| 608 | |
| 609 | // Initialize the memory using placement new. |
| 610 | new (result) ArrayAttr(value); |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 611 | |
| 612 | // Cache and return it. |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 613 | return *existing.first = result; |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 614 | } |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 615 | |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 616 | AffineMapAttr *AffineMapAttr::get(AffineMap *value, MLIRContext *context) { |
MLIR Team | b61885d | 2018-07-18 16:29:21 -0700 | [diff] [blame] | 617 | auto *&result = context->getImpl().affineMapAttrs[value]; |
| 618 | if (result) |
| 619 | return result; |
| 620 | |
| 621 | result = context->getImpl().allocator.Allocate<AffineMapAttr>(); |
| 622 | new (result) AffineMapAttr(value); |
| 623 | return result; |
| 624 | } |
| 625 | |
James Molloy | f0d2f44 | 2018-08-03 01:54:46 -0700 | [diff] [blame] | 626 | TypeAttr *TypeAttr::get(Type *type, MLIRContext *context) { |
| 627 | auto *&result = context->getImpl().typeAttrs[type]; |
| 628 | if (result) |
| 629 | return result; |
| 630 | |
| 631 | result = context->getImpl().allocator.Allocate<TypeAttr>(); |
| 632 | new (result) TypeAttr(type); |
| 633 | return result; |
| 634 | } |
| 635 | |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame] | 636 | /// Perform a three-way comparison between the names of the specified |
| 637 | /// NamedAttributes. |
| 638 | static int compareNamedAttributes(const NamedAttribute *lhs, |
| 639 | const NamedAttribute *rhs) { |
| 640 | return lhs->first.str().compare(rhs->first.str()); |
| 641 | } |
| 642 | |
| 643 | /// Given a list of NamedAttribute's, canonicalize the list (sorting |
| 644 | /// by name) and return the unique'd result. Note that the empty list is |
| 645 | /// represented with a null pointer. |
| 646 | AttributeListStorage *AttributeListStorage::get(ArrayRef<NamedAttribute> attrs, |
| 647 | MLIRContext *context) { |
| 648 | // We need to sort the element list to canonicalize it, but we also don't want |
| 649 | // to do a ton of work in the super common case where the element list is |
| 650 | // already sorted. |
| 651 | SmallVector<NamedAttribute, 8> storage; |
| 652 | switch (attrs.size()) { |
| 653 | case 0: |
| 654 | // An empty list is represented with a null pointer. |
| 655 | return nullptr; |
| 656 | case 1: |
| 657 | // A single element is already sorted. |
| 658 | break; |
| 659 | case 2: |
| 660 | // Don't invoke a general sort for two element case. |
| 661 | if (attrs[0].first.str() > attrs[1].first.str()) { |
| 662 | storage.push_back(attrs[1]); |
| 663 | storage.push_back(attrs[0]); |
| 664 | attrs = storage; |
| 665 | } |
| 666 | break; |
| 667 | default: |
| 668 | // Check to see they are sorted already. |
| 669 | bool isSorted = true; |
| 670 | for (unsigned i = 0, e = attrs.size() - 1; i != e; ++i) { |
| 671 | if (attrs[i].first.str() > attrs[i + 1].first.str()) { |
| 672 | isSorted = false; |
| 673 | break; |
| 674 | } |
| 675 | } |
| 676 | // If not, do a general sort. |
| 677 | if (!isSorted) { |
| 678 | storage.append(attrs.begin(), attrs.end()); |
| 679 | llvm::array_pod_sort(storage.begin(), storage.end(), |
| 680 | compareNamedAttributes); |
| 681 | attrs = storage; |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | // Ok, now that we've canonicalized our attributes, unique them. |
| 686 | auto &impl = context->getImpl(); |
| 687 | |
| 688 | // Look to see if we already have this. |
| 689 | auto existing = impl.attributeLists.insert_as(nullptr, attrs); |
| 690 | |
| 691 | // If we already have it, return that value. |
| 692 | if (!existing.second) |
| 693 | return *existing.first; |
| 694 | |
| 695 | // Otherwise, allocate a new AttributeListStorage, unique it and return it. |
| 696 | auto byteSize = |
| 697 | AttributeListStorage::totalSizeToAlloc<NamedAttribute>(attrs.size()); |
| 698 | auto rawMem = impl.allocator.Allocate(byteSize, alignof(NamedAttribute)); |
| 699 | |
| 700 | // Placement initialize the AggregateSymbolicValue. |
| 701 | auto result = ::new (rawMem) AttributeListStorage(attrs.size()); |
| 702 | std::uninitialized_copy(attrs.begin(), attrs.end(), |
| 703 | result->getTrailingObjects<NamedAttribute>()); |
| 704 | return *existing.first = result; |
| 705 | } |
| 706 | |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 707 | //===----------------------------------------------------------------------===// |
| 708 | // AffineMap and AffineExpr uniquing |
| 709 | //===----------------------------------------------------------------------===// |
| 710 | |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 711 | AffineMap *AffineMap::get(unsigned dimCount, unsigned symbolCount, |
| 712 | ArrayRef<AffineExpr *> results, |
Uday Bondhugula | 0115dbb | 2018-07-11 21:31:07 -0700 | [diff] [blame] | 713 | ArrayRef<AffineExpr *> rangeSizes, |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 714 | MLIRContext *context) { |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 715 | // The number of results can't be zero. |
| 716 | assert(!results.empty()); |
| 717 | |
Uday Bondhugula | 0115dbb | 2018-07-11 21:31:07 -0700 | [diff] [blame] | 718 | assert(rangeSizes.empty() || results.size() == rangeSizes.size()); |
| 719 | |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 720 | auto &impl = context->getImpl(); |
| 721 | |
| 722 | // Check if we already have this affine map. |
Uday Bondhugula | 0115dbb | 2018-07-11 21:31:07 -0700 | [diff] [blame] | 723 | auto key = std::make_tuple(dimCount, symbolCount, results, rangeSizes); |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 724 | auto existing = impl.affineMaps.insert_as(nullptr, key); |
| 725 | |
| 726 | // If we already have it, return that value. |
| 727 | if (!existing.second) |
| 728 | return *existing.first; |
| 729 | |
| 730 | // On the first use, we allocate them into the bump pointer. |
| 731 | auto *res = impl.allocator.Allocate<AffineMap>(); |
| 732 | |
Uday Bondhugula | 1e500b4 | 2018-07-12 18:04:04 -0700 | [diff] [blame] | 733 | // Copy the results and range sizes into the bump pointer. |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 734 | results = impl.copyInto(ArrayRef<AffineExpr *>(results)); |
Uday Bondhugula | 0115dbb | 2018-07-11 21:31:07 -0700 | [diff] [blame] | 735 | rangeSizes = impl.copyInto(ArrayRef<AffineExpr *>(rangeSizes)); |
| 736 | |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 737 | // Initialize the memory using placement new. |
Uday Bondhugula | 0115dbb | 2018-07-11 21:31:07 -0700 | [diff] [blame] | 738 | new (res) AffineMap(dimCount, symbolCount, results.size(), results.data(), |
| 739 | rangeSizes.empty() ? nullptr : rangeSizes.data()); |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 740 | |
| 741 | // Cache and return it. |
| 742 | return *existing.first = res; |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 743 | } |
| 744 | |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 745 | /// Return a binary affine op expression with the specified op type and |
| 746 | /// operands: if it doesn't exist, create it and store it; if it is already |
| 747 | /// present, return from the list. The stored expressions are unique: they are |
| 748 | /// constructed and stored in a simplified/canonicalized form. The result after |
| 749 | /// simplification could be any form of affine expression. |
| 750 | AffineExpr *AffineBinaryOpExpr::get(AffineExpr::Kind kind, AffineExpr *lhs, |
| 751 | AffineExpr *rhs, MLIRContext *context) { |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 752 | auto &impl = context->getImpl(); |
| 753 | |
Uday Bondhugula | 0dd940c | 2018-07-26 00:19:21 -0700 | [diff] [blame] | 754 | // Check if we already have this affine expression, and return it if we do. |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 755 | auto keyValue = std::make_tuple((unsigned)kind, lhs, rhs); |
Uday Bondhugula | 0dd940c | 2018-07-26 00:19:21 -0700 | [diff] [blame] | 756 | auto cached = impl.affineExprs.find(keyValue); |
| 757 | if (cached != impl.affineExprs.end()) |
| 758 | return cached->second; |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 759 | |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 760 | // Simplify the expression if possible. |
| 761 | AffineExpr *simplified; |
| 762 | switch (kind) { |
| 763 | case Kind::Add: |
| 764 | simplified = AffineBinaryOpExpr::simplifyAdd(lhs, rhs, context); |
| 765 | break; |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 766 | case Kind::Mul: |
| 767 | simplified = AffineBinaryOpExpr::simplifyMul(lhs, rhs, context); |
| 768 | break; |
| 769 | case Kind::FloorDiv: |
| 770 | simplified = AffineBinaryOpExpr::simplifyFloorDiv(lhs, rhs, context); |
| 771 | break; |
| 772 | case Kind::CeilDiv: |
| 773 | simplified = AffineBinaryOpExpr::simplifyCeilDiv(lhs, rhs, context); |
| 774 | break; |
| 775 | case Kind::Mod: |
| 776 | simplified = AffineBinaryOpExpr::simplifyMod(lhs, rhs, context); |
| 777 | break; |
| 778 | default: |
| 779 | llvm_unreachable("unexpected binary affine expr"); |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 780 | } |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 781 | |
Uday Bondhugula | 0dd940c | 2018-07-26 00:19:21 -0700 | [diff] [blame] | 782 | // The simplified one would have already been cached; just return it. |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 783 | if (simplified) |
Uday Bondhugula | 0dd940c | 2018-07-26 00:19:21 -0700 | [diff] [blame] | 784 | return simplified; |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 785 | |
Uday Bondhugula | 0dd940c | 2018-07-26 00:19:21 -0700 | [diff] [blame] | 786 | // An expression with these operands will already be in the |
| 787 | // simplified/canonical form. Create and store it. |
| 788 | auto *result = impl.allocator.Allocate<AffineBinaryOpExpr>(); |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 789 | // Initialize the memory using placement new. |
Uday Bondhugula | 0dd940c | 2018-07-26 00:19:21 -0700 | [diff] [blame] | 790 | new (result) AffineBinaryOpExpr(kind, lhs, rhs); |
| 791 | bool inserted = impl.affineExprs.insert({keyValue, result}).second; |
| 792 | assert(inserted && "the expression shouldn't already exist in the map"); |
| 793 | (void)inserted; |
| 794 | return result; |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 795 | } |
| 796 | |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 797 | AffineDimExpr *AffineDimExpr::get(unsigned position, MLIRContext *context) { |
Uday Bondhugula | 4e5078b | 2018-07-24 22:34:09 -0700 | [diff] [blame] | 798 | auto &impl = context->getImpl(); |
| 799 | |
| 800 | // Check if we need to resize. |
| 801 | if (position >= impl.dimExprs.size()) |
| 802 | impl.dimExprs.resize(position + 1, nullptr); |
| 803 | |
| 804 | auto *&result = impl.dimExprs[position]; |
| 805 | if (result) |
| 806 | return result; |
| 807 | |
| 808 | result = impl.allocator.Allocate<AffineDimExpr>(); |
| 809 | // Initialize the memory using placement new. |
| 810 | new (result) AffineDimExpr(position); |
| 811 | return result; |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | AffineSymbolExpr *AffineSymbolExpr::get(unsigned position, |
| 815 | MLIRContext *context) { |
Uday Bondhugula | 4e5078b | 2018-07-24 22:34:09 -0700 | [diff] [blame] | 816 | auto &impl = context->getImpl(); |
| 817 | |
| 818 | // Check if we need to resize. |
| 819 | if (position >= impl.symbolExprs.size()) |
| 820 | impl.symbolExprs.resize(position + 1, nullptr); |
| 821 | |
| 822 | auto *&result = impl.symbolExprs[position]; |
| 823 | if (result) |
| 824 | return result; |
| 825 | |
| 826 | result = impl.allocator.Allocate<AffineSymbolExpr>(); |
| 827 | // Initialize the memory using placement new. |
| 828 | new (result) AffineSymbolExpr(position); |
| 829 | return result; |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | AffineConstantExpr *AffineConstantExpr::get(int64_t constant, |
| 833 | MLIRContext *context) { |
Uday Bondhugula | 4e5078b | 2018-07-24 22:34:09 -0700 | [diff] [blame] | 834 | auto &impl = context->getImpl(); |
| 835 | auto *&result = impl.constExprs[constant]; |
| 836 | |
| 837 | if (result) |
| 838 | return result; |
| 839 | |
| 840 | result = impl.allocator.Allocate<AffineConstantExpr>(); |
| 841 | // Initialize the memory using placement new. |
| 842 | new (result) AffineConstantExpr(constant); |
| 843 | return result; |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 844 | } |
Uday Bondhugula | bc53562 | 2018-08-07 14:24:38 -0700 | [diff] [blame^] | 845 | |
| 846 | //===----------------------------------------------------------------------===// |
| 847 | // Integer Sets: these are allocated into the bump pointer, and are immutable. |
| 848 | // But they aren't uniqued like AffineMap's; there isn't an advantage to. |
| 849 | //===----------------------------------------------------------------------===// |
| 850 | |
| 851 | IntegerSet *IntegerSet::get(unsigned dimCount, unsigned symbolCount, |
| 852 | ArrayRef<AffineExpr *> constraints, |
| 853 | ArrayRef<bool> eqFlags, MLIRContext *context) { |
| 854 | assert(eqFlags.size() == constraints.size()); |
| 855 | |
| 856 | auto &impl = context->getImpl(); |
| 857 | |
| 858 | // Allocate them into the bump pointer. |
| 859 | auto *res = impl.allocator.Allocate<IntegerSet>(); |
| 860 | |
| 861 | // Copy the equalities and inequalities into the bump pointer. |
| 862 | constraints = impl.copyInto(ArrayRef<AffineExpr *>(constraints)); |
| 863 | eqFlags = impl.copyInto(ArrayRef<bool>(eqFlags)); |
| 864 | |
| 865 | // Initialize the memory using placement new. |
| 866 | return new (res) IntegerSet(dimCount, symbolCount, constraints.size(), |
| 867 | constraints.data(), eqFlags.data()); |
| 868 | } |