MLIR  16.0.0git
Location.h
Go to the documentation of this file.
1 //===- Location.h - MLIR Location Classes -----------------------*- 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 // These classes provide the ability to relate MLIR objects back to source
10 // location position information.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_IR_LOCATION_H
15 #define MLIR_IR_LOCATION_H
16 
17 #include "mlir/IR/Attributes.h"
19 #include "llvm/Support/PointerLikeTypeTraits.h"
20 
21 namespace mlir {
22 
23 class Location;
24 class WalkResult;
25 
26 //===----------------------------------------------------------------------===//
27 // LocationAttr
28 //===----------------------------------------------------------------------===//
29 
30 /// Location objects represent source locations information in MLIR.
31 /// LocationAttr acts as the anchor for all Location based attributes.
32 class LocationAttr : public Attribute {
33 public:
35 
36  /// Walk all of the locations nested under, and including, the current.
38 
39  /// Return an instance of the given location type if one is nested under the
40  /// current location. Returns nullptr if one could not be found.
41  template <typename T>
43  T result = {};
44  walk([&](auto loc) {
45  if (auto typedLoc = llvm::dyn_cast<T>(loc)) {
46  result = typedLoc;
47  return WalkResult::interrupt();
48  }
49  return WalkResult::advance();
50  });
51  return result;
52  }
53 
54  /// Methods for support type inquiry through isa, cast, and dyn_cast.
55  static bool classof(Attribute attr);
56 };
57 
58 //===----------------------------------------------------------------------===//
59 // Location
60 //===----------------------------------------------------------------------===//
61 
62 /// This class defines the main interface for locations in MLIR and acts as a
63 /// non-nullable wrapper around a LocationAttr.
64 class Location {
65 public:
66  Location(LocationAttr loc) : impl(loc) {
67  assert(loc && "location should never be null.");
68  }
70  assert(impl && "location should never be null.");
71  }
72 
73  /// Return the context this location is uniqued in.
74  MLIRContext *getContext() const { return impl.getContext(); }
75 
76  /// Access the impl location attribute.
77  operator LocationAttr() const { return impl; }
78  LocationAttr *operator->() const { return const_cast<LocationAttr *>(&impl); }
79 
80  /// Type casting utilities on the underlying location.
81  template <typename U>
82  bool isa() const {
83  return llvm::isa<U>(*this);
84  }
85  template <typename U>
86  U dyn_cast() const {
87  return llvm::dyn_cast<U>(*this);
88  }
89  template <typename U>
90  U cast() const {
91  return llvm::cast<U>(*this);
92  }
93 
94  /// Comparison operators.
95  bool operator==(Location rhs) const { return impl == rhs.impl; }
96  bool operator!=(Location rhs) const { return !(*this == rhs); }
97 
98  /// Print the location.
99  void print(raw_ostream &os) const { impl.print(os); }
100  void dump() const { impl.dump(); }
101 
102  friend ::llvm::hash_code hash_value(Location arg);
103 
104  /// Methods for supporting PointerLikeTypeTraits.
105  const void *getAsOpaquePointer() const { return impl.getAsOpaquePointer(); }
106  static Location getFromOpaquePointer(const void *pointer) {
107  return LocationAttr(reinterpret_cast<const AttributeStorage *>(pointer));
108  }
109 
110  /// Support llvm style casting.
111  static bool classof(Attribute attr) { return llvm::isa<LocationAttr>(attr); }
112 
113 protected:
114  /// The internal backing location attribute.
116 };
117 
118 inline raw_ostream &operator<<(raw_ostream &os, const Location &loc) {
119  loc.print(os);
120  return os;
121 }
122 
123 // Make Location hashable.
124 inline ::llvm::hash_code hash_value(Location arg) {
125  return hash_value(arg.impl);
126 }
127 
128 } // namespace mlir
129 
130 //===----------------------------------------------------------------------===//
131 // Tablegen Attribute Declarations
132 //===----------------------------------------------------------------------===//
133 
134 #define GET_ATTRDEF_CLASSES
135 #include "mlir/IR/BuiltinLocationAttributes.h.inc"
136 
137 namespace mlir {
138 
139 //===----------------------------------------------------------------------===//
140 // FusedLoc
141 //===----------------------------------------------------------------------===//
142 
143 /// This class represents a fused location whose metadata is known to be an
144 /// instance of the given type.
145 template <typename MetadataT>
146 class FusedLocWith : public FusedLoc {
147 public:
148  using FusedLoc::FusedLoc;
149 
150  /// Return the metadata associated with this fused location.
151  MetadataT getMetadata() const {
152  return FusedLoc::getMetadata().template cast<MetadataT>();
153  }
154 
155  /// Support llvm style casting.
156  static bool classof(Attribute attr) {
157  auto fusedLoc = attr.dyn_cast<FusedLoc>();
158  return fusedLoc && fusedLoc.getMetadata().isa_and_nonnull<MetadataT>();
159  }
160 };
161 
162 //===----------------------------------------------------------------------===//
163 // OpaqueLoc
164 //===----------------------------------------------------------------------===//
165 
166 /// Returns an instance of opaque location which contains a given pointer to
167 /// an object. The corresponding MLIR location is set to UnknownLoc.
168 template <typename T>
169 inline OpaqueLoc OpaqueLoc::get(T underlyingLocation, MLIRContext *context) {
170  return get(reinterpret_cast<uintptr_t>(underlyingLocation), TypeID::get<T>(),
171  UnknownLoc::get(context));
172 }
173 
174 //===----------------------------------------------------------------------===//
175 // SubElementInterfaces
176 //===----------------------------------------------------------------------===//
177 
178 /// Enable locations to be introspected as sub-elements.
179 template <>
181  static void walk(Location param, AttrTypeSubElementWalker &walker) {
182  walker.walk(param);
183  }
185  TypeSubElementReplacements &typeRepls) {
186  return cast<LocationAttr>(attrRepls.take_front(1)[0]);
187  }
188 };
189 
190 } // namespace mlir
191 
192 //===----------------------------------------------------------------------===//
193 // LLVM Utilities
194 //===----------------------------------------------------------------------===//
195 
196 namespace llvm {
197 
198 // Type hash just like pointers.
199 template <>
200 struct DenseMapInfo<mlir::Location> {
202  auto *pointer = llvm::DenseMapInfo<void *>::getEmptyKey();
203  return mlir::Location::getFromOpaquePointer(pointer);
204  }
207  return mlir::Location::getFromOpaquePointer(pointer);
208  }
209  static unsigned getHashValue(mlir::Location val) {
210  return mlir::hash_value(val);
211  }
212  static bool isEqual(mlir::Location LHS, mlir::Location RHS) {
213  return LHS == RHS;
214  }
215 };
216 
217 /// We align LocationStorage by 8, so allow LLVM to steal the low bits.
218 template <>
219 struct PointerLikeTypeTraits<mlir::Location> {
220 public:
221  static inline void *getAsVoidPointer(mlir::Location I) {
222  return const_cast<void *>(I.getAsOpaquePointer());
223  }
224  static inline mlir::Location getFromVoidPointer(void *P) {
226  }
227  static constexpr int NumLowBitsAvailable =
229 };
230 
231 /// The constructors in mlir::Location ensure that the class is a non-nullable
232 /// wrapper around mlir::LocationAttr. Override default behavior and always
233 /// return true for isPresent().
234 template <>
235 struct ValueIsPresent<mlir::Location> {
237  static inline bool isPresent(const mlir::Location &location) { return true; }
238 };
239 
240 /// Add support for llvm style casts. We provide a cast between To and From if
241 /// From is mlir::Location or derives from it.
242 template <typename To, typename From>
243 struct CastInfo<To, From,
244  std::enable_if_t<
245  std::is_same_v<mlir::Location, std::remove_const_t<From>> ||
246  std::is_base_of_v<mlir::Location, From>>>
247  : DefaultDoCastIfPossible<To, From, CastInfo<To, From>> {
248 
249  static inline bool isPossible(mlir::Location location) {
250  /// Return a constant true instead of a dynamic true when casting to self or
251  /// up the hierarchy. Additionally, all casting info is deferred to the
252  /// wrapped mlir::LocationAttr instance stored in mlir::Location.
253  return std::is_same_v<To, std::remove_const_t<From>> ||
254  isa<To>(static_cast<mlir::LocationAttr>(location));
255  }
256 
257  static inline To castFailed() { return To(); }
258 
259  static inline To doCast(mlir::Location location) {
260  return To(location->getImpl());
261  }
262 };
263 
264 } // namespace llvm
265 
266 #endif
This class is used by AttrTypeSubElementHandler instances to process sub element replacements.
ArrayRef< T > take_front(unsigned n)
Take the first N replacements as an ArrayRef, dropping them from this replacement list.
AttrTypeSubElementHandler.
void walk(Attribute element)
Walk an attribute.
Base storage class appearing in an attribute.
Attributes are known-constant values of operations.
Definition: Attributes.h:25
U dyn_cast() const
Definition: Attributes.h:127
constexpr Attribute()=default
ImplType * getImpl() const
Return the internal Attribute implementation.
Definition: Attributes.h:105
This class represents a fused location whose metadata is known to be an instance of the given type.
Definition: Location.h:146
MetadataT getMetadata() const
Return the metadata associated with this fused location.
Definition: Location.h:151
static bool classof(Attribute attr)
Support llvm style casting.
Definition: Location.h:156
Location objects represent source locations information in MLIR.
Definition: Location.h:32
WalkResult walk(function_ref< WalkResult(Location)> walkFn)
Walk all of the locations nested under, and including, the current.
Definition: Location.cpp:40
static bool classof(Attribute attr)
Methods for support type inquiry through isa, cast, and dyn_cast.
Definition: Location.cpp:66
T findInstanceOf()
Return an instance of the given location type if one is nested under the current location.
Definition: Location.h:42
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:64
const void * getAsOpaquePointer() const
Methods for supporting PointerLikeTypeTraits.
Definition: Location.h:105
MLIRContext * getContext() const
Return the context this location is uniqued in.
Definition: Location.h:74
friend ::llvm::hash_code hash_value(Location arg)
Definition: Location.h:124
Location(LocationAttr loc)
Definition: Location.h:66
static bool classof(Attribute attr)
Support llvm style casting.
Definition: Location.h:111
bool isa() const
Type casting utilities on the underlying location.
Definition: Location.h:82
Location(const LocationAttr::ImplType *impl)
Definition: Location.h:69
void dump() const
Definition: Location.h:100
LocationAttr impl
The internal backing location attribute.
Definition: Location.h:115
static Location getFromOpaquePointer(const void *pointer)
Definition: Location.h:106
bool operator!=(Location rhs) const
Definition: Location.h:96
LocationAttr * operator->() const
Definition: Location.h:78
U cast() const
Definition: Location.h:90
U dyn_cast() const
Definition: Location.h:86
bool operator==(Location rhs) const
Comparison operators.
Definition: Location.h:95
void print(raw_ostream &os) const
Print the location.
Definition: Location.h:99
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:56
A utility result that is used to signal how to proceed with an ongoing walk:
Definition: Visitors.h:34
static WalkResult advance()
Definition: Visitors.h:51
static WalkResult interrupt()
Definition: Visitors.h:50
Include the generated interface declarations.
Definition: CallGraph.h:229
Include the generated interface declarations.
inline ::llvm::hash_code hash_value(AffineExpr arg)
Make AffineExpr hashable.
Definition: AffineExpr.h:240
raw_ostream & operator<<(raw_ostream &os, const AliasResult &result)
Definition: AliasAnalysis.h:78
static bool isEqual(mlir::Location LHS, mlir::Location RHS)
Definition: Location.h:212
static unsigned getHashValue(mlir::Location val)
Definition: Location.h:209
static mlir::Location getEmptyKey()
Definition: Location.h:201
static mlir::Location getTombstoneKey()
Definition: Location.h:205
static void * getAsVoidPointer(mlir::Location I)
Definition: Location.h:221
static mlir::Location getFromVoidPointer(void *P)
Definition: Location.h:224
static bool isPresent(const mlir::Location &location)
Definition: Location.h:237
static void walk(Location param, AttrTypeSubElementWalker &walker)
Definition: Location.h:181
static Location replace(Location param, AttrSubElementReplacements &attrRepls, TypeSubElementReplacements &typeRepls)
Definition: Location.h:184
This class provides support for interacting with the SubElementInterfaces for different types of para...