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