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