MLIR  20.0.0git
TypeDetail.h
Go to the documentation of this file.
1 //===- TypeDetail.h - MLIR Type storage details -----------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This holds implementation details of Type.
10 //
11 //===----------------------------------------------------------------------===//
12 #ifndef TYPEDETAIL_H_
13 #define TYPEDETAIL_H_
14 
15 #include "mlir/IR/AffineMap.h"
16 #include "mlir/IR/BuiltinTypes.h"
17 #include "mlir/IR/MLIRContext.h"
19 #include "mlir/IR/TypeRange.h"
20 #include "llvm/ADT/bit.h"
21 #include "llvm/Support/TrailingObjects.h"
22 
23 namespace mlir {
24 
25 namespace detail {
26 
27 /// Integer Type Storage and Uniquing.
30  IntegerType::SignednessSemantics signedness)
32 
33  /// The hash key used for uniquing.
34  using KeyTy = std::tuple<unsigned, IntegerType::SignednessSemantics>;
35 
36  static llvm::hash_code hashKey(const KeyTy &key) {
37  return llvm::hash_value(key);
38  }
39 
40  bool operator==(const KeyTy &key) const {
41  return KeyTy(width, signedness) == key;
42  }
43 
45  KeyTy key) {
46  return new (allocator.allocate<IntegerTypeStorage>())
47  IntegerTypeStorage(std::get<0>(key), std::get<1>(key));
48  }
49 
50  KeyTy getAsKey() const { return KeyTy(width, signedness); }
51 
52  unsigned width : 30;
53  IntegerType::SignednessSemantics signedness : 2;
54 };
55 
56 /// Function Type Storage and Uniquing.
59  Type const *inputsAndResults)
62 
63  /// The hash key used for uniquing.
64  using KeyTy = std::tuple<TypeRange, TypeRange>;
65  bool operator==(const KeyTy &key) const {
66  if (std::get<0>(key) == getInputs())
67  return std::get<1>(key) == getResults();
68  return false;
69  }
70 
71  /// Construction.
73  const KeyTy &key) {
74  auto [inputs, results] = key;
75 
76  // Copy the inputs and results into the bump pointer.
78  types.reserve(inputs.size() + results.size());
79  types.append(inputs.begin(), inputs.end());
80  types.append(results.begin(), results.end());
81  auto typesList = allocator.copyInto(ArrayRef<Type>(types));
82 
83  // Initialize the memory using placement new.
84  return new (allocator.allocate<FunctionTypeStorage>())
85  FunctionTypeStorage(inputs.size(), results.size(), typesList.data());
86  }
87 
90  }
93  }
94 
95  KeyTy getAsKey() const { return KeyTy(getInputs(), getResults()); }
96 
97  unsigned numInputs;
98  unsigned numResults;
100 };
101 
102 /// A type representing a collection of other types.
103 struct TupleTypeStorage final
104  : public TypeStorage,
105  public llvm::TrailingObjects<TupleTypeStorage, Type> {
106  using KeyTy = TypeRange;
107 
108  TupleTypeStorage(unsigned numTypes) : numElements(numTypes) {}
109 
110  /// Construction.
112  TypeRange key) {
113  // Allocate a new storage instance.
114  auto byteSize = TupleTypeStorage::totalSizeToAlloc<Type>(key.size());
115  auto *rawMem = allocator.allocate(byteSize, alignof(TupleTypeStorage));
116  auto *result = ::new (rawMem) TupleTypeStorage(key.size());
117 
118  // Copy in the element types into the trailing storage.
119  std::uninitialized_copy(key.begin(), key.end(),
120  result->getTrailingObjects<Type>());
121  return result;
122  }
123 
124  bool operator==(const KeyTy &key) const { return key == getTypes(); }
125 
126  /// Return the number of held types.
127  unsigned size() const { return numElements; }
128 
129  /// Return the held types.
131  return {getTrailingObjects<Type>(), size()};
132  }
133 
134  KeyTy getAsKey() const { return getTypes(); }
135 
136  /// The number of tuple elements.
137  unsigned numElements;
138 };
139 
140 /// Checks if the memorySpace has supported Attribute type.
141 bool isSupportedMemorySpace(Attribute memorySpace);
142 
143 /// Wraps deprecated integer memory space to the new Attribute form.
144 Attribute wrapIntegerMemorySpace(unsigned memorySpace, MLIRContext *ctx);
145 
146 /// Replaces default memorySpace (integer == `0`) with empty Attribute.
148 
149 /// [deprecated] Returns the memory space in old raw integer representation.
150 /// New `Attribute getMemorySpace()` method should be used instead.
151 unsigned getMemorySpaceAsInt(Attribute memorySpace);
152 
153 } // namespace detail
154 } // namespace mlir
155 
156 #endif // TYPEDETAIL_H_
Attributes are known-constant values of operations.
Definition: Attributes.h:25
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
This is a utility allocator used to allocate memory for instances of derived types.
ArrayRef< T > copyInto(ArrayRef< T > elements)
Copy the specified array of elements into memory managed by our bump pointer allocator.
T * allocate()
Allocate an instance of the provided type.
This class provides an abstraction over the various different ranges of value types.
Definition: TypeRange.h:36
Base storage class appearing in a Type.
Definition: TypeSupport.h:166
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
Attribute wrapIntegerMemorySpace(unsigned memorySpace, MLIRContext *ctx)
Wraps deprecated integer memory space to the new Attribute form.
unsigned getMemorySpaceAsInt(Attribute memorySpace)
[deprecated] Returns the memory space in old raw integer representation.
bool isSupportedMemorySpace(Attribute memorySpace)
Checks if the memorySpace has supported Attribute type.
Attribute skipDefaultMemorySpace(Attribute memorySpace)
Replaces default memorySpace (integer == 0) with empty Attribute.
inline ::llvm::hash_code hash_value(const PolynomialBase< D, T > &arg)
Definition: Polynomial.h:262
Include the generated interface declarations.
Function Type Storage and Uniquing.
Definition: TypeDetail.h:57
std::tuple< TypeRange, TypeRange > KeyTy
The hash key used for uniquing.
Definition: TypeDetail.h:64
ArrayRef< Type > getResults() const
Definition: TypeDetail.h:91
ArrayRef< Type > getInputs() const
Definition: TypeDetail.h:88
FunctionTypeStorage(unsigned numInputs, unsigned numResults, Type const *inputsAndResults)
Definition: TypeDetail.h:58
static FunctionTypeStorage * construct(TypeStorageAllocator &allocator, const KeyTy &key)
Construction.
Definition: TypeDetail.h:72
bool operator==(const KeyTy &key) const
Definition: TypeDetail.h:65
Integer Type Storage and Uniquing.
Definition: TypeDetail.h:28
bool operator==(const KeyTy &key) const
Definition: TypeDetail.h:40
static IntegerTypeStorage * construct(TypeStorageAllocator &allocator, KeyTy key)
Definition: TypeDetail.h:44
static llvm::hash_code hashKey(const KeyTy &key)
Definition: TypeDetail.h:36
IntegerTypeStorage(unsigned width, IntegerType::SignednessSemantics signedness)
Definition: TypeDetail.h:29
IntegerType::SignednessSemantics signedness
Definition: TypeDetail.h:53
std::tuple< unsigned, IntegerType::SignednessSemantics > KeyTy
The hash key used for uniquing.
Definition: TypeDetail.h:34
A type representing a collection of other types.
Definition: TypeDetail.h:105
unsigned numElements
The number of tuple elements.
Definition: TypeDetail.h:137
unsigned size() const
Return the number of held types.
Definition: TypeDetail.h:127
static TupleTypeStorage * construct(TypeStorageAllocator &allocator, TypeRange key)
Construction.
Definition: TypeDetail.h:111
ArrayRef< Type > getTypes() const
Return the held types.
Definition: TypeDetail.h:130
TupleTypeStorage(unsigned numTypes)
Definition: TypeDetail.h:108
bool operator==(const KeyTy &key) const
Definition: TypeDetail.h:124