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" |
Chris Lattner | 4613d9e | 2018-08-19 21:17:22 -0700 | [diff] [blame] | 23 | #include "mlir/IR/Function.h" |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 24 | #include "mlir/IR/Identifier.h" |
Uday Bondhugula | bc53562 | 2018-08-07 14:24:38 -0700 | [diff] [blame] | 25 | #include "mlir/IR/IntegerSet.h" |
Chris Lattner | fc647d5 | 2018-08-27 21:05:16 -0700 | [diff] [blame] | 26 | #include "mlir/IR/Location.h" |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 27 | #include "mlir/IR/OperationSet.h" |
| 28 | #include "mlir/IR/StandardOps.h" |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 29 | #include "mlir/IR/Types.h" |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 30 | #include "mlir/Support/STLExtras.h" |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 31 | #include "llvm/ADT/DenseSet.h" |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 32 | #include "llvm/ADT/StringMap.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: |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 184 | /// This is the set of all operations that are registered with the system. |
| 185 | OperationSet operationSet; |
| 186 | |
Chris Lattner | fc647d5 | 2018-08-27 21:05:16 -0700 | [diff] [blame] | 187 | /// We put location info into this allocator, since it is generally not |
| 188 | /// touched by compiler passes. |
| 189 | llvm::BumpPtrAllocator locationAllocator; |
| 190 | |
| 191 | /// The singleton for UnknownLoc. |
| 192 | UnknownLoc *theUnknownLoc = nullptr; |
| 193 | |
| 194 | /// These are filename locations uniqued into this MLIRContext. |
| 195 | llvm::StringMap<char, llvm::BumpPtrAllocator &> filenames; |
| 196 | |
| 197 | /// FileLineColLoc uniquing. |
| 198 | DenseMap<std::tuple<const char *, unsigned, unsigned>, FileLineColLoc *> |
| 199 | fileLineColLocs; |
| 200 | |
| 201 | /// We put immortal objects into this allocator. |
| 202 | llvm::BumpPtrAllocator allocator; |
| 203 | |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 204 | /// This is the handler to use to report diagnostics, or null if not |
| 205 | /// registered. |
| 206 | MLIRContext::DiagnosticHandlerTy diagnosticHandler; |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 207 | |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 208 | /// These are identifiers uniqued into this MLIRContext. |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 209 | llvm::StringMap<char, llvm::BumpPtrAllocator &> identifiers; |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 210 | |
Chris Lattner | c325119 | 2018-07-27 13:09:58 -0700 | [diff] [blame] | 211 | // Uniquing table for 'other' types. |
| 212 | OtherType *otherTypes[int(Type::Kind::LAST_OTHER_TYPE) - |
| 213 | int(Type::Kind::FIRST_OTHER_TYPE) + 1] = {nullptr}; |
| 214 | |
| 215 | // Uniquing table for 'float' types. |
| 216 | FloatType *floatTypes[int(Type::Kind::LAST_FLOATING_POINT_TYPE) - |
| 217 | int(Type::Kind::FIRST_FLOATING_POINT_TYPE) + 1] = { |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 218 | nullptr}; |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 219 | |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 220 | // Affine map uniquing. |
| 221 | using AffineMapSet = DenseSet<AffineMap *, AffineMapKeyInfo>; |
| 222 | AffineMapSet affineMaps; |
| 223 | |
Uday Bondhugula | 0b80a16 | 2018-07-03 21:34:58 -0700 | [diff] [blame] | 224 | // Affine binary op expression uniquing. Figure out uniquing of dimensional |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 225 | // or symbolic identifiers. |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 226 | DenseMap<std::tuple<unsigned, AffineExpr *, AffineExpr *>, AffineExpr *> |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 227 | affineExprs; |
| 228 | |
Uday Bondhugula | 4e5078b | 2018-07-24 22:34:09 -0700 | [diff] [blame] | 229 | // Uniqui'ing of AffineDimExpr, AffineSymbolExpr's by their position. |
| 230 | std::vector<AffineDimExpr *> dimExprs; |
| 231 | std::vector<AffineSymbolExpr *> symbolExprs; |
| 232 | |
| 233 | // Uniqui'ing of AffineConstantExpr using constant value as key. |
| 234 | DenseMap<int64_t, AffineConstantExpr *> constExprs; |
| 235 | |
Chris Lattner | f958bbe | 2018-06-29 22:08:05 -0700 | [diff] [blame] | 236 | /// Integer type uniquing. |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 237 | DenseMap<unsigned, IntegerType *> integers; |
Chris Lattner | f958bbe | 2018-06-29 22:08:05 -0700 | [diff] [blame] | 238 | |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 239 | /// Function type uniquing. |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 240 | using FunctionTypeSet = DenseSet<FunctionType *, FunctionTypeKeyInfo>; |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 241 | FunctionTypeSet functions; |
| 242 | |
| 243 | /// Vector type uniquing. |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 244 | using VectorTypeSet = DenseSet<VectorType *, VectorTypeKeyInfo>; |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 245 | VectorTypeSet vectors; |
| 246 | |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 247 | /// Ranked tensor type uniquing. |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 248 | using RankedTensorTypeSet = |
| 249 | DenseSet<RankedTensorType *, RankedTensorTypeKeyInfo>; |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 250 | RankedTensorTypeSet rankedTensors; |
| 251 | |
| 252 | /// Unranked tensor type uniquing. |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 253 | DenseMap<Type *, UnrankedTensorType *> unrankedTensors; |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 254 | |
MLIR Team | 718c82f | 2018-07-16 09:45:22 -0700 | [diff] [blame] | 255 | /// MemRef type uniquing. |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 256 | using MemRefTypeSet = DenseSet<MemRefType *, MemRefTypeKeyInfo>; |
MLIR Team | 718c82f | 2018-07-16 09:45:22 -0700 | [diff] [blame] | 257 | MemRefTypeSet memrefs; |
| 258 | |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 259 | // Attribute uniquing. |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 260 | BoolAttr *boolAttrs[2] = {nullptr}; |
| 261 | DenseMap<int64_t, IntegerAttr *> integerAttrs; |
| 262 | DenseMap<int64_t, FloatAttr *> floatAttrs; |
| 263 | StringMap<StringAttr *> stringAttrs; |
| 264 | using ArrayAttrSet = DenseSet<ArrayAttr *, ArrayAttrKeyInfo>; |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 265 | ArrayAttrSet arrayAttrs; |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 266 | DenseMap<AffineMap *, AffineMapAttr *> affineMapAttrs; |
James Molloy | f0d2f44 | 2018-08-03 01:54:46 -0700 | [diff] [blame] | 267 | DenseMap<Type *, TypeAttr *> typeAttrs; |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame] | 268 | using AttributeListSet = |
| 269 | DenseSet<AttributeListStorage *, AttributeListKeyInfo>; |
| 270 | AttributeListSet attributeLists; |
Chris Lattner | 1aa4632 | 2018-08-21 17:55:22 -0700 | [diff] [blame] | 271 | DenseMap<const Function *, FunctionAttr *> functionAttrs; |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 272 | |
| 273 | public: |
Chris Lattner | fc647d5 | 2018-08-27 21:05:16 -0700 | [diff] [blame] | 274 | MLIRContextImpl() : filenames(locationAllocator), identifiers(allocator) { |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 275 | registerStandardOperations(operationSet); |
| 276 | } |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 277 | |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 278 | /// Copy the specified array of elements into memory managed by our bump |
| 279 | /// pointer allocator. This assumes the elements are all PODs. |
James Molloy | 72b0cbe | 2018-08-01 12:55:27 -0700 | [diff] [blame] | 280 | template <typename T> |
| 281 | ArrayRef<T> copyInto(ArrayRef<T> elements) { |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 282 | auto result = allocator.Allocate<T>(elements.size()); |
| 283 | std::uninitialized_copy(elements.begin(), elements.end(), result); |
| 284 | return ArrayRef<T>(result, elements.size()); |
| 285 | } |
| 286 | }; |
| 287 | } // end namespace mlir |
| 288 | |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 289 | MLIRContext::MLIRContext() : impl(new MLIRContextImpl()) {} |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 290 | |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 291 | MLIRContext::~MLIRContext() {} |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 292 | |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 293 | /// Register an issue handler with this LLVM context. The issue handler is |
| 294 | /// passed location information if present (nullptr if not) along with a |
| 295 | /// message and a boolean that indicates whether this is an error or warning. |
| 296 | void MLIRContext::registerDiagnosticHandler( |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 297 | const DiagnosticHandlerTy &handler) { |
| 298 | getImpl().diagnosticHandler = handler; |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Chris Lattner | ea5c3dc | 2018-08-21 08:42:19 -0700 | [diff] [blame] | 301 | /// Return the current diagnostic handler, or null if none is present. |
| 302 | auto MLIRContext::getDiagnosticHandler() const -> DiagnosticHandlerTy { |
| 303 | return getImpl().diagnosticHandler; |
| 304 | } |
| 305 | |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 306 | /// This emits a diagnostic using the registered issue handle if present, or |
| 307 | /// with the default behavior if not. The MLIR compiler should not generally |
| 308 | /// interact with this, it should use methods on Operation instead. |
Chris Lattner | fc647d5 | 2018-08-27 21:05:16 -0700 | [diff] [blame] | 309 | void MLIRContext::emitDiagnostic(Location *location, const llvm::Twine &message, |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 310 | DiagnosticKind kind) const { |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 311 | // If we had a handler registered, emit the diagnostic using it. |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 312 | auto handler = getImpl().diagnosticHandler; |
| 313 | if (handler && location) |
| 314 | return handler(location, message.str(), kind); |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 315 | |
Chris Lattner | f7bdf95 | 2018-08-05 21:12:29 -0700 | [diff] [blame] | 316 | // The default behavior for notes and warnings is to ignore them. |
| 317 | if (kind != DiagnosticKind::Error) |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 318 | return; |
| 319 | |
Chris Lattner | 7879f84 | 2018-09-02 22:01:45 -0700 | [diff] [blame] | 320 | auto &os = llvm::errs(); |
| 321 | |
| 322 | if (auto fileLoc = dyn_cast<FileLineColLoc>(location)) |
| 323 | os << fileLoc->getFilename() << ':' << fileLoc->getLine() << ':' |
| 324 | << fileLoc->getColumn() << ": "; |
| 325 | |
| 326 | os << "error: "; |
Chris Lattner | fc647d5 | 2018-08-27 21:05:16 -0700 | [diff] [blame] | 327 | |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 328 | // The default behavior for errors is to emit them to stderr and exit. |
Chris Lattner | 7879f84 | 2018-09-02 22:01:45 -0700 | [diff] [blame] | 329 | os << message.str() << '\n'; |
| 330 | os.flush(); |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 331 | exit(1); |
| 332 | } |
| 333 | |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 334 | /// Return the operation set associated with the specified MLIRContext object. |
| 335 | OperationSet &OperationSet::get(MLIRContext *context) { |
| 336 | return context->getImpl().operationSet; |
| 337 | } |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 338 | |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 339 | /// If this operation has a registered operation description in the |
| 340 | /// OperationSet, return it. Otherwise return null. |
Chris Lattner | 9586506 | 2018-08-01 10:18:59 -0700 | [diff] [blame] | 341 | const AbstractOperation *Operation::getAbstractOperation() const { |
| 342 | return OperationSet::get(getContext()).lookup(getName().str()); |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 345 | //===----------------------------------------------------------------------===// |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 346 | // Identifier uniquing |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 347 | //===----------------------------------------------------------------------===// |
| 348 | |
| 349 | /// Return an identifier for the specified string. |
| 350 | Identifier Identifier::get(StringRef str, const MLIRContext *context) { |
| 351 | assert(!str.empty() && "Cannot create an empty identifier"); |
| 352 | assert(str.find('\0') == StringRef::npos && |
| 353 | "Cannot create an identifier with a nul character"); |
| 354 | |
| 355 | auto &impl = context->getImpl(); |
| 356 | auto it = impl.identifiers.insert({str, char()}).first; |
| 357 | return Identifier(it->getKeyData()); |
| 358 | } |
| 359 | |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 360 | //===----------------------------------------------------------------------===// |
Chris Lattner | fc647d5 | 2018-08-27 21:05:16 -0700 | [diff] [blame] | 361 | // Location uniquing |
| 362 | //===----------------------------------------------------------------------===// |
| 363 | |
| 364 | UnknownLoc *UnknownLoc::get(MLIRContext *context) { |
| 365 | auto &impl = context->getImpl(); |
| 366 | if (auto *result = impl.theUnknownLoc) |
| 367 | return result; |
| 368 | |
| 369 | impl.theUnknownLoc = impl.allocator.Allocate<UnknownLoc>(); |
| 370 | new (impl.theUnknownLoc) UnknownLoc(); |
| 371 | return impl.theUnknownLoc; |
| 372 | } |
| 373 | |
| 374 | UniquedFilename UniquedFilename::get(StringRef filename, MLIRContext *context) { |
| 375 | auto &impl = context->getImpl(); |
| 376 | auto it = impl.filenames.insert({filename, char()}).first; |
| 377 | return UniquedFilename(it->getKeyData()); |
| 378 | } |
| 379 | |
| 380 | FileLineColLoc *FileLineColLoc::get(UniquedFilename filename, unsigned line, |
| 381 | unsigned column, MLIRContext *context) { |
| 382 | auto &impl = context->getImpl(); |
| 383 | auto &entry = |
| 384 | impl.fileLineColLocs[std::make_tuple(filename.data(), line, column)]; |
| 385 | if (!entry) { |
| 386 | entry = impl.allocator.Allocate<FileLineColLoc>(); |
| 387 | new (entry) FileLineColLoc(filename, line, column); |
| 388 | } |
| 389 | |
| 390 | return entry; |
| 391 | } |
| 392 | |
| 393 | //===----------------------------------------------------------------------===// |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 394 | // Type uniquing |
Chris Lattner | ed65a73 | 2018-06-28 20:45:33 -0700 | [diff] [blame] | 395 | //===----------------------------------------------------------------------===// |
| 396 | |
Chris Lattner | f958bbe | 2018-06-29 22:08:05 -0700 | [diff] [blame] | 397 | IntegerType *IntegerType::get(unsigned width, MLIRContext *context) { |
| 398 | auto &impl = context->getImpl(); |
| 399 | |
| 400 | auto *&result = impl.integers[width]; |
| 401 | if (!result) { |
| 402 | result = impl.allocator.Allocate<IntegerType>(); |
| 403 | new (result) IntegerType(width, context); |
| 404 | } |
| 405 | |
| 406 | return result; |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Chris Lattner | c325119 | 2018-07-27 13:09:58 -0700 | [diff] [blame] | 409 | FloatType *FloatType::get(Kind kind, MLIRContext *context) { |
| 410 | assert(kind >= Kind::FIRST_FLOATING_POINT_TYPE && |
| 411 | kind <= Kind::LAST_FLOATING_POINT_TYPE && "Not an FP type kind"); |
| 412 | auto &impl = context->getImpl(); |
| 413 | |
| 414 | // We normally have these types. |
| 415 | auto *&entry = |
| 416 | impl.floatTypes[(int)kind - int(Kind::FIRST_FLOATING_POINT_TYPE)]; |
| 417 | if (entry) |
| 418 | return entry; |
| 419 | |
| 420 | // On the first use, we allocate them into the bump pointer. |
| 421 | auto *ptr = impl.allocator.Allocate<FloatType>(); |
| 422 | |
| 423 | // Initialize the memory using placement new. |
| 424 | new (ptr) FloatType(kind, context); |
| 425 | |
| 426 | // Cache and return it. |
| 427 | return entry = ptr; |
| 428 | } |
| 429 | |
| 430 | OtherType *OtherType::get(Kind kind, MLIRContext *context) { |
| 431 | assert(kind >= Kind::FIRST_OTHER_TYPE && kind <= Kind::LAST_OTHER_TYPE && |
| 432 | "Not an 'other' type kind"); |
| 433 | auto &impl = context->getImpl(); |
| 434 | |
| 435 | // We normally have these types. |
| 436 | auto *&entry = impl.otherTypes[(int)kind - int(Kind::FIRST_OTHER_TYPE)]; |
| 437 | if (entry) |
| 438 | return entry; |
| 439 | |
| 440 | // On the first use, we allocate them into the bump pointer. |
| 441 | auto *ptr = impl.allocator.Allocate<OtherType>(); |
| 442 | |
| 443 | // Initialize the memory using placement new. |
| 444 | new (ptr) OtherType(kind, context); |
| 445 | |
| 446 | // Cache and return it. |
| 447 | return entry = ptr; |
| 448 | } |
| 449 | |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 450 | FunctionType *FunctionType::get(ArrayRef<Type *> inputs, |
| 451 | ArrayRef<Type *> results, |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 452 | MLIRContext *context) { |
| 453 | auto &impl = context->getImpl(); |
| 454 | |
| 455 | // Look to see if we already have this function type. |
| 456 | FunctionTypeKeyInfo::KeyTy key(inputs, results); |
| 457 | auto existing = impl.functions.insert_as(nullptr, key); |
| 458 | |
| 459 | // If we already have it, return that value. |
| 460 | if (!existing.second) |
| 461 | return *existing.first; |
| 462 | |
| 463 | // On the first use, we allocate them into the bump pointer. |
| 464 | auto *result = impl.allocator.Allocate<FunctionType>(); |
| 465 | |
| 466 | // Copy the inputs and results into the bump pointer. |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 467 | SmallVector<Type *, 16> types; |
| 468 | types.reserve(inputs.size() + results.size()); |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 469 | types.append(inputs.begin(), inputs.end()); |
| 470 | types.append(results.begin(), results.end()); |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 471 | auto typesList = impl.copyInto(ArrayRef<Type *>(types)); |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 472 | |
| 473 | // Initialize the memory using placement new. |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 474 | new (result) |
| 475 | FunctionType(typesList.data(), inputs.size(), results.size(), context); |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 476 | |
| 477 | // Cache and return it. |
| 478 | return *existing.first = result; |
| 479 | } |
| 480 | |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 481 | VectorType *VectorType::get(ArrayRef<unsigned> shape, Type *elementType) { |
| 482 | assert(!shape.empty() && "vector types must have at least one dimension"); |
Chris Lattner | c325119 | 2018-07-27 13:09:58 -0700 | [diff] [blame] | 483 | assert((isa<FloatType>(elementType) || isa<IntegerType>(elementType)) && |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 484 | "vectors elements must be primitives"); |
| 485 | |
| 486 | auto *context = elementType->getContext(); |
| 487 | auto &impl = context->getImpl(); |
| 488 | |
| 489 | // Look to see if we already have this vector type. |
| 490 | VectorTypeKeyInfo::KeyTy key(elementType, shape); |
| 491 | auto existing = impl.vectors.insert_as(nullptr, key); |
| 492 | |
| 493 | // If we already have it, return that value. |
| 494 | if (!existing.second) |
| 495 | return *existing.first; |
| 496 | |
| 497 | // On the first use, we allocate them into the bump pointer. |
| 498 | auto *result = impl.allocator.Allocate<VectorType>(); |
| 499 | |
| 500 | // Copy the shape into the bump pointer. |
| 501 | shape = impl.copyInto(shape); |
| 502 | |
| 503 | // Initialize the memory using placement new. |
Jacques Pienaar | 3cdb854 | 2018-07-23 11:48:22 -0700 | [diff] [blame] | 504 | new (result) VectorType(shape, elementType, context); |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 505 | |
| 506 | // Cache and return it. |
| 507 | return *existing.first = result; |
| 508 | } |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 509 | |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 510 | RankedTensorType *RankedTensorType::get(ArrayRef<int> shape, |
| 511 | Type *elementType) { |
| 512 | auto *context = elementType->getContext(); |
| 513 | auto &impl = context->getImpl(); |
| 514 | |
| 515 | // Look to see if we already have this ranked tensor type. |
| 516 | RankedTensorTypeKeyInfo::KeyTy key(elementType, shape); |
| 517 | auto existing = impl.rankedTensors.insert_as(nullptr, key); |
| 518 | |
| 519 | // If we already have it, return that value. |
| 520 | if (!existing.second) |
| 521 | return *existing.first; |
| 522 | |
| 523 | // On the first use, we allocate them into the bump pointer. |
| 524 | auto *result = impl.allocator.Allocate<RankedTensorType>(); |
| 525 | |
| 526 | // Copy the shape into the bump pointer. |
| 527 | shape = impl.copyInto(shape); |
| 528 | |
| 529 | // Initialize the memory using placement new. |
| 530 | new (result) RankedTensorType(shape, elementType, context); |
| 531 | |
| 532 | // Cache and return it. |
| 533 | return *existing.first = result; |
| 534 | } |
| 535 | |
| 536 | UnrankedTensorType *UnrankedTensorType::get(Type *elementType) { |
| 537 | auto *context = elementType->getContext(); |
| 538 | auto &impl = context->getImpl(); |
| 539 | |
| 540 | // Look to see if we already have this unranked tensor type. |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 541 | auto *&result = impl.unrankedTensors[elementType]; |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 542 | |
| 543 | // If we already have it, return that value. |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 544 | if (result) |
| 545 | return result; |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 546 | |
| 547 | // On the first use, we allocate them into the bump pointer. |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 548 | result = impl.allocator.Allocate<UnrankedTensorType>(); |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 549 | |
| 550 | // Initialize the memory using placement new. |
| 551 | new (result) UnrankedTensorType(elementType, context); |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 552 | return result; |
| 553 | } |
| 554 | |
MLIR Team | 718c82f | 2018-07-16 09:45:22 -0700 | [diff] [blame] | 555 | MemRefType *MemRefType::get(ArrayRef<int> shape, Type *elementType, |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 556 | ArrayRef<AffineMap *> affineMapComposition, |
MLIR Team | 718c82f | 2018-07-16 09:45:22 -0700 | [diff] [blame] | 557 | unsigned memorySpace) { |
| 558 | auto *context = elementType->getContext(); |
| 559 | auto &impl = context->getImpl(); |
| 560 | |
| 561 | // Look to see if we already have this memref type. |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 562 | auto key = |
| 563 | std::make_tuple(elementType, shape, affineMapComposition, memorySpace); |
MLIR Team | 718c82f | 2018-07-16 09:45:22 -0700 | [diff] [blame] | 564 | auto existing = impl.memrefs.insert_as(nullptr, key); |
| 565 | |
| 566 | // If we already have it, return that value. |
| 567 | if (!existing.second) |
| 568 | return *existing.first; |
| 569 | |
| 570 | // On the first use, we allocate them into the bump pointer. |
| 571 | auto *result = impl.allocator.Allocate<MemRefType>(); |
| 572 | |
| 573 | // Copy the shape into the bump pointer. |
| 574 | shape = impl.copyInto(shape); |
| 575 | |
| 576 | // Copy the affine map composition into the bump pointer. |
| 577 | // TODO(andydavis) Assert that the structure of the composition is valid. |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 578 | affineMapComposition = |
| 579 | impl.copyInto(ArrayRef<AffineMap *>(affineMapComposition)); |
MLIR Team | 718c82f | 2018-07-16 09:45:22 -0700 | [diff] [blame] | 580 | |
| 581 | // Initialize the memory using placement new. |
| 582 | new (result) MemRefType(shape, elementType, affineMapComposition, memorySpace, |
| 583 | context); |
| 584 | // Cache and return it. |
| 585 | return *existing.first = result; |
| 586 | } |
| 587 | |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 588 | //===----------------------------------------------------------------------===// |
| 589 | // Attribute uniquing |
| 590 | //===----------------------------------------------------------------------===// |
| 591 | |
| 592 | BoolAttr *BoolAttr::get(bool value, MLIRContext *context) { |
| 593 | auto *&result = context->getImpl().boolAttrs[value]; |
| 594 | if (result) |
| 595 | return result; |
| 596 | |
| 597 | result = context->getImpl().allocator.Allocate<BoolAttr>(); |
| 598 | new (result) BoolAttr(value); |
| 599 | return result; |
| 600 | } |
| 601 | |
| 602 | IntegerAttr *IntegerAttr::get(int64_t value, MLIRContext *context) { |
| 603 | auto *&result = context->getImpl().integerAttrs[value]; |
| 604 | if (result) |
| 605 | return result; |
| 606 | |
| 607 | result = context->getImpl().allocator.Allocate<IntegerAttr>(); |
| 608 | new (result) IntegerAttr(value); |
| 609 | return result; |
| 610 | } |
| 611 | |
| 612 | FloatAttr *FloatAttr::get(double value, MLIRContext *context) { |
| 613 | // We hash based on the bit representation of the double to ensure we don't |
| 614 | // merge things like -0.0 and 0.0 in the hash comparison. |
| 615 | union { |
| 616 | double floatValue; |
| 617 | int64_t intValue; |
| 618 | }; |
| 619 | floatValue = value; |
| 620 | |
| 621 | auto *&result = context->getImpl().floatAttrs[intValue]; |
| 622 | if (result) |
| 623 | return result; |
| 624 | |
| 625 | result = context->getImpl().allocator.Allocate<FloatAttr>(); |
| 626 | new (result) FloatAttr(value); |
| 627 | return result; |
| 628 | } |
| 629 | |
| 630 | StringAttr *StringAttr::get(StringRef bytes, MLIRContext *context) { |
| 631 | auto it = context->getImpl().stringAttrs.insert({bytes, nullptr}).first; |
| 632 | |
| 633 | if (it->second) |
| 634 | return it->second; |
| 635 | |
| 636 | auto result = context->getImpl().allocator.Allocate<StringAttr>(); |
| 637 | new (result) StringAttr(it->first()); |
| 638 | it->second = result; |
| 639 | return result; |
| 640 | } |
| 641 | |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 642 | ArrayAttr *ArrayAttr::get(ArrayRef<Attribute *> value, MLIRContext *context) { |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 643 | auto &impl = context->getImpl(); |
| 644 | |
| 645 | // Look to see if we already have this. |
| 646 | auto existing = impl.arrayAttrs.insert_as(nullptr, value); |
| 647 | |
| 648 | // If we already have it, return that value. |
| 649 | if (!existing.second) |
| 650 | return *existing.first; |
| 651 | |
| 652 | // On the first use, we allocate them into the bump pointer. |
| 653 | auto *result = impl.allocator.Allocate<ArrayAttr>(); |
| 654 | |
| 655 | // Copy the elements into the bump pointer. |
| 656 | value = impl.copyInto(value); |
| 657 | |
Chris Lattner | ea5c3dc | 2018-08-21 08:42:19 -0700 | [diff] [blame] | 658 | // Check to see if any of the elements have a function attr. |
| 659 | bool hasFunctionAttr = false; |
| 660 | for (auto *elt : value) |
| 661 | if (elt->isOrContainsFunction()) { |
| 662 | hasFunctionAttr = true; |
| 663 | break; |
| 664 | } |
| 665 | |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 666 | // Initialize the memory using placement new. |
Chris Lattner | ea5c3dc | 2018-08-21 08:42:19 -0700 | [diff] [blame] | 667 | new (result) ArrayAttr(value, hasFunctionAttr); |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 668 | |
| 669 | // Cache and return it. |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 670 | return *existing.first = result; |
MLIR Team | 355ec86 | 2018-06-23 18:09:09 -0700 | [diff] [blame] | 671 | } |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 672 | |
James Molloy | 87d8102 | 2018-07-23 11:44:40 -0700 | [diff] [blame] | 673 | AffineMapAttr *AffineMapAttr::get(AffineMap *value, MLIRContext *context) { |
MLIR Team | b61885d | 2018-07-18 16:29:21 -0700 | [diff] [blame] | 674 | auto *&result = context->getImpl().affineMapAttrs[value]; |
| 675 | if (result) |
| 676 | return result; |
| 677 | |
| 678 | result = context->getImpl().allocator.Allocate<AffineMapAttr>(); |
| 679 | new (result) AffineMapAttr(value); |
| 680 | return result; |
| 681 | } |
| 682 | |
James Molloy | f0d2f44 | 2018-08-03 01:54:46 -0700 | [diff] [blame] | 683 | TypeAttr *TypeAttr::get(Type *type, MLIRContext *context) { |
| 684 | auto *&result = context->getImpl().typeAttrs[type]; |
| 685 | if (result) |
| 686 | return result; |
| 687 | |
| 688 | result = context->getImpl().allocator.Allocate<TypeAttr>(); |
| 689 | new (result) TypeAttr(type); |
| 690 | return result; |
| 691 | } |
| 692 | |
Chris Lattner | 1aa4632 | 2018-08-21 17:55:22 -0700 | [diff] [blame] | 693 | FunctionAttr *FunctionAttr::get(const Function *value, MLIRContext *context) { |
| 694 | assert(value && "Cannot get FunctionAttr for a null function"); |
| 695 | |
Chris Lattner | 4613d9e | 2018-08-19 21:17:22 -0700 | [diff] [blame] | 696 | auto *&result = context->getImpl().functionAttrs[value]; |
| 697 | if (result) |
| 698 | return result; |
| 699 | |
| 700 | result = context->getImpl().allocator.Allocate<FunctionAttr>(); |
Chris Lattner | 1aa4632 | 2018-08-21 17:55:22 -0700 | [diff] [blame] | 701 | new (result) FunctionAttr(const_cast<Function *>(value)); |
Chris Lattner | 4613d9e | 2018-08-19 21:17:22 -0700 | [diff] [blame] | 702 | return result; |
| 703 | } |
| 704 | |
Chris Lattner | 1aa4632 | 2018-08-21 17:55:22 -0700 | [diff] [blame] | 705 | FunctionType *FunctionAttr::getType() const { return getValue()->getType(); } |
| 706 | |
Chris Lattner | 4613d9e | 2018-08-19 21:17:22 -0700 | [diff] [blame] | 707 | /// This function is used by the internals of the Function class to null out |
| 708 | /// attributes refering to functions that are about to be deleted. |
| 709 | void FunctionAttr::dropFunctionReference(Function *value) { |
| 710 | // Check to see if there was an attribute referring to this function. |
| 711 | auto &functionAttrs = value->getContext()->getImpl().functionAttrs; |
| 712 | |
| 713 | // If not, then we're done. |
| 714 | auto it = functionAttrs.find(value); |
| 715 | if (it == functionAttrs.end()) |
| 716 | return; |
| 717 | |
| 718 | // If so, null out the function reference in the attribute (to avoid dangling |
| 719 | // pointers) and remove the entry from the map so the map doesn't contain |
| 720 | // dangling keys. |
| 721 | it->second->value = nullptr; |
| 722 | functionAttrs.erase(it); |
| 723 | } |
| 724 | |
Chris Lattner | df1a2fc | 2018-07-05 21:20:59 -0700 | [diff] [blame] | 725 | /// Perform a three-way comparison between the names of the specified |
| 726 | /// NamedAttributes. |
| 727 | static int compareNamedAttributes(const NamedAttribute *lhs, |
| 728 | const NamedAttribute *rhs) { |
| 729 | return lhs->first.str().compare(rhs->first.str()); |
| 730 | } |
| 731 | |
| 732 | /// Given a list of NamedAttribute's, canonicalize the list (sorting |
| 733 | /// by name) and return the unique'd result. Note that the empty list is |
| 734 | /// represented with a null pointer. |
| 735 | AttributeListStorage *AttributeListStorage::get(ArrayRef<NamedAttribute> attrs, |
| 736 | MLIRContext *context) { |
| 737 | // We need to sort the element list to canonicalize it, but we also don't want |
| 738 | // to do a ton of work in the super common case where the element list is |
| 739 | // already sorted. |
| 740 | SmallVector<NamedAttribute, 8> storage; |
| 741 | switch (attrs.size()) { |
| 742 | case 0: |
| 743 | // An empty list is represented with a null pointer. |
| 744 | return nullptr; |
| 745 | case 1: |
| 746 | // A single element is already sorted. |
| 747 | break; |
| 748 | case 2: |
| 749 | // Don't invoke a general sort for two element case. |
| 750 | if (attrs[0].first.str() > attrs[1].first.str()) { |
| 751 | storage.push_back(attrs[1]); |
| 752 | storage.push_back(attrs[0]); |
| 753 | attrs = storage; |
| 754 | } |
| 755 | break; |
| 756 | default: |
| 757 | // Check to see they are sorted already. |
| 758 | bool isSorted = true; |
| 759 | for (unsigned i = 0, e = attrs.size() - 1; i != e; ++i) { |
| 760 | if (attrs[i].first.str() > attrs[i + 1].first.str()) { |
| 761 | isSorted = false; |
| 762 | break; |
| 763 | } |
| 764 | } |
| 765 | // If not, do a general sort. |
| 766 | if (!isSorted) { |
| 767 | storage.append(attrs.begin(), attrs.end()); |
| 768 | llvm::array_pod_sort(storage.begin(), storage.end(), |
| 769 | compareNamedAttributes); |
| 770 | attrs = storage; |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | // Ok, now that we've canonicalized our attributes, unique them. |
| 775 | auto &impl = context->getImpl(); |
| 776 | |
| 777 | // Look to see if we already have this. |
| 778 | auto existing = impl.attributeLists.insert_as(nullptr, attrs); |
| 779 | |
| 780 | // If we already have it, return that value. |
| 781 | if (!existing.second) |
| 782 | return *existing.first; |
| 783 | |
| 784 | // Otherwise, allocate a new AttributeListStorage, unique it and return it. |
| 785 | auto byteSize = |
| 786 | AttributeListStorage::totalSizeToAlloc<NamedAttribute>(attrs.size()); |
| 787 | auto rawMem = impl.allocator.Allocate(byteSize, alignof(NamedAttribute)); |
| 788 | |
| 789 | // Placement initialize the AggregateSymbolicValue. |
| 790 | auto result = ::new (rawMem) AttributeListStorage(attrs.size()); |
| 791 | std::uninitialized_copy(attrs.begin(), attrs.end(), |
| 792 | result->getTrailingObjects<NamedAttribute>()); |
| 793 | return *existing.first = result; |
| 794 | } |
| 795 | |
Chris Lattner | 36b4ed1 | 2018-07-04 10:43:29 -0700 | [diff] [blame] | 796 | //===----------------------------------------------------------------------===// |
| 797 | // AffineMap and AffineExpr uniquing |
| 798 | //===----------------------------------------------------------------------===// |
| 799 | |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 800 | AffineMap *AffineMap::get(unsigned dimCount, unsigned symbolCount, |
| 801 | ArrayRef<AffineExpr *> results, |
Uday Bondhugula | 0115dbb | 2018-07-11 21:31:07 -0700 | [diff] [blame] | 802 | ArrayRef<AffineExpr *> rangeSizes, |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 803 | MLIRContext *context) { |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 804 | // The number of results can't be zero. |
| 805 | assert(!results.empty()); |
| 806 | |
Uday Bondhugula | 0115dbb | 2018-07-11 21:31:07 -0700 | [diff] [blame] | 807 | assert(rangeSizes.empty() || results.size() == rangeSizes.size()); |
| 808 | |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 809 | auto &impl = context->getImpl(); |
| 810 | |
| 811 | // Check if we already have this affine map. |
Uday Bondhugula | 0115dbb | 2018-07-11 21:31:07 -0700 | [diff] [blame] | 812 | auto key = std::make_tuple(dimCount, symbolCount, results, rangeSizes); |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 813 | auto existing = impl.affineMaps.insert_as(nullptr, key); |
| 814 | |
| 815 | // If we already have it, return that value. |
| 816 | if (!existing.second) |
| 817 | return *existing.first; |
| 818 | |
| 819 | // On the first use, we allocate them into the bump pointer. |
| 820 | auto *res = impl.allocator.Allocate<AffineMap>(); |
| 821 | |
Uday Bondhugula | 1e500b4 | 2018-07-12 18:04:04 -0700 | [diff] [blame] | 822 | // Copy the results and range sizes into the bump pointer. |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 823 | results = impl.copyInto(ArrayRef<AffineExpr *>(results)); |
Uday Bondhugula | 0115dbb | 2018-07-11 21:31:07 -0700 | [diff] [blame] | 824 | rangeSizes = impl.copyInto(ArrayRef<AffineExpr *>(rangeSizes)); |
| 825 | |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 826 | // Initialize the memory using placement new. |
Uday Bondhugula | 0115dbb | 2018-07-11 21:31:07 -0700 | [diff] [blame] | 827 | new (res) AffineMap(dimCount, symbolCount, results.size(), results.data(), |
| 828 | rangeSizes.empty() ? nullptr : rangeSizes.data()); |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 829 | |
| 830 | // Cache and return it. |
| 831 | return *existing.first = res; |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 832 | } |
| 833 | |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 834 | /// Return a binary affine op expression with the specified op type and |
| 835 | /// operands: if it doesn't exist, create it and store it; if it is already |
| 836 | /// present, return from the list. The stored expressions are unique: they are |
| 837 | /// constructed and stored in a simplified/canonicalized form. The result after |
| 838 | /// simplification could be any form of affine expression. |
| 839 | AffineExpr *AffineBinaryOpExpr::get(AffineExpr::Kind kind, AffineExpr *lhs, |
| 840 | AffineExpr *rhs, MLIRContext *context) { |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 841 | auto &impl = context->getImpl(); |
| 842 | |
Uday Bondhugula | 0dd940c | 2018-07-26 00:19:21 -0700 | [diff] [blame] | 843 | // 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] | 844 | auto keyValue = std::make_tuple((unsigned)kind, lhs, rhs); |
Uday Bondhugula | 0dd940c | 2018-07-26 00:19:21 -0700 | [diff] [blame] | 845 | auto cached = impl.affineExprs.find(keyValue); |
| 846 | if (cached != impl.affineExprs.end()) |
| 847 | return cached->second; |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 848 | |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 849 | // Simplify the expression if possible. |
| 850 | AffineExpr *simplified; |
| 851 | switch (kind) { |
| 852 | case Kind::Add: |
| 853 | simplified = AffineBinaryOpExpr::simplifyAdd(lhs, rhs, context); |
| 854 | break; |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 855 | case Kind::Mul: |
| 856 | simplified = AffineBinaryOpExpr::simplifyMul(lhs, rhs, context); |
| 857 | break; |
| 858 | case Kind::FloorDiv: |
| 859 | simplified = AffineBinaryOpExpr::simplifyFloorDiv(lhs, rhs, context); |
| 860 | break; |
| 861 | case Kind::CeilDiv: |
| 862 | simplified = AffineBinaryOpExpr::simplifyCeilDiv(lhs, rhs, context); |
| 863 | break; |
| 864 | case Kind::Mod: |
| 865 | simplified = AffineBinaryOpExpr::simplifyMod(lhs, rhs, context); |
| 866 | break; |
| 867 | default: |
| 868 | llvm_unreachable("unexpected binary affine expr"); |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 869 | } |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 870 | |
Uday Bondhugula | 0dd940c | 2018-07-26 00:19:21 -0700 | [diff] [blame] | 871 | // The simplified one would have already been cached; just return it. |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 872 | if (simplified) |
Uday Bondhugula | 0dd940c | 2018-07-26 00:19:21 -0700 | [diff] [blame] | 873 | return simplified; |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 874 | |
Uday Bondhugula | 0dd940c | 2018-07-26 00:19:21 -0700 | [diff] [blame] | 875 | // An expression with these operands will already be in the |
| 876 | // simplified/canonical form. Create and store it. |
| 877 | auto *result = impl.allocator.Allocate<AffineBinaryOpExpr>(); |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 878 | // Initialize the memory using placement new. |
Uday Bondhugula | 0dd940c | 2018-07-26 00:19:21 -0700 | [diff] [blame] | 879 | new (result) AffineBinaryOpExpr(kind, lhs, rhs); |
| 880 | bool inserted = impl.affineExprs.insert({keyValue, result}).second; |
| 881 | assert(inserted && "the expression shouldn't already exist in the map"); |
| 882 | (void)inserted; |
| 883 | return result; |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 884 | } |
| 885 | |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 886 | AffineDimExpr *AffineDimExpr::get(unsigned position, MLIRContext *context) { |
Uday Bondhugula | 4e5078b | 2018-07-24 22:34:09 -0700 | [diff] [blame] | 887 | auto &impl = context->getImpl(); |
| 888 | |
| 889 | // Check if we need to resize. |
| 890 | if (position >= impl.dimExprs.size()) |
| 891 | impl.dimExprs.resize(position + 1, nullptr); |
| 892 | |
| 893 | auto *&result = impl.dimExprs[position]; |
| 894 | if (result) |
| 895 | return result; |
| 896 | |
| 897 | result = impl.allocator.Allocate<AffineDimExpr>(); |
| 898 | // Initialize the memory using placement new. |
| 899 | new (result) AffineDimExpr(position); |
| 900 | return result; |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | AffineSymbolExpr *AffineSymbolExpr::get(unsigned position, |
| 904 | MLIRContext *context) { |
Uday Bondhugula | 4e5078b | 2018-07-24 22:34:09 -0700 | [diff] [blame] | 905 | auto &impl = context->getImpl(); |
| 906 | |
| 907 | // Check if we need to resize. |
| 908 | if (position >= impl.symbolExprs.size()) |
| 909 | impl.symbolExprs.resize(position + 1, nullptr); |
| 910 | |
| 911 | auto *&result = impl.symbolExprs[position]; |
| 912 | if (result) |
| 913 | return result; |
| 914 | |
| 915 | result = impl.allocator.Allocate<AffineSymbolExpr>(); |
| 916 | // Initialize the memory using placement new. |
| 917 | new (result) AffineSymbolExpr(position); |
| 918 | return result; |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | AffineConstantExpr *AffineConstantExpr::get(int64_t constant, |
| 922 | MLIRContext *context) { |
Uday Bondhugula | 4e5078b | 2018-07-24 22:34:09 -0700 | [diff] [blame] | 923 | auto &impl = context->getImpl(); |
| 924 | auto *&result = impl.constExprs[constant]; |
| 925 | |
| 926 | if (result) |
| 927 | return result; |
| 928 | |
| 929 | result = impl.allocator.Allocate<AffineConstantExpr>(); |
| 930 | // Initialize the memory using placement new. |
| 931 | new (result) AffineConstantExpr(constant); |
| 932 | return result; |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 933 | } |
Uday Bondhugula | bc53562 | 2018-08-07 14:24:38 -0700 | [diff] [blame] | 934 | |
| 935 | //===----------------------------------------------------------------------===// |
| 936 | // Integer Sets: these are allocated into the bump pointer, and are immutable. |
| 937 | // But they aren't uniqued like AffineMap's; there isn't an advantage to. |
| 938 | //===----------------------------------------------------------------------===// |
| 939 | |
| 940 | IntegerSet *IntegerSet::get(unsigned dimCount, unsigned symbolCount, |
| 941 | ArrayRef<AffineExpr *> constraints, |
| 942 | ArrayRef<bool> eqFlags, MLIRContext *context) { |
| 943 | assert(eqFlags.size() == constraints.size()); |
| 944 | |
| 945 | auto &impl = context->getImpl(); |
| 946 | |
| 947 | // Allocate them into the bump pointer. |
| 948 | auto *res = impl.allocator.Allocate<IntegerSet>(); |
| 949 | |
| 950 | // Copy the equalities and inequalities into the bump pointer. |
| 951 | constraints = impl.copyInto(ArrayRef<AffineExpr *>(constraints)); |
| 952 | eqFlags = impl.copyInto(ArrayRef<bool>(eqFlags)); |
| 953 | |
| 954 | // Initialize the memory using placement new. |
| 955 | return new (res) IntegerSet(dimCount, symbolCount, constraints.size(), |
| 956 | constraints.data(), eqFlags.data()); |
| 957 | } |