My Project
Location.h
Go to the documentation of this file.
1 //===- Location.h - MLIR Location Classes -----------------------*- C++ -*-===//
2 //
3 // Part of the MLIR 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 
19 namespace mlir {
20 
21 class Attribute;
22 class MLIRContext;
23 class Identifier;
24 
25 namespace detail {
26 
27 struct CallSiteLocationStorage;
28 struct FileLineColLocationStorage;
29 struct FusedLocationStorage;
30 struct LocationStorage;
31 struct NameLocationStorage;
32 struct OpaqueLocationStorage;
33 struct UnknownLocationStorage;
34 
35 } // namespace detail
36 
39 class LocationAttr : public Attribute {
40 public:
42 
44  static bool classof(Attribute attr) {
47  }
48 };
49 
52 class Location {
53 public:
54  Location(LocationAttr loc) : impl(loc) {
55  assert(loc && "location should never be null.");
56  }
57 
59  operator LocationAttr() const { return impl; }
60  LocationAttr *operator->() const { return const_cast<LocationAttr *>(&impl); }
61 
63  template <typename U> bool isa() const { return impl.isa<U>(); }
64  template <typename U> U dyn_cast() const { return impl.dyn_cast<U>(); }
65  template <typename U> U cast() const { return impl.cast<U>(); }
66 
68  bool operator==(Location rhs) const { return impl == rhs.impl; }
69  bool operator!=(Location rhs) const { return !(*this == rhs); }
70 
72  void print(raw_ostream &os) const { impl.print(os); }
73  void dump() const { impl.dump(); }
74 
75  friend ::llvm::hash_code hash_value(Location arg);
76 
78  const void *getAsOpaquePointer() const { return impl.getAsOpaquePointer(); }
79  static Location getFromOpaquePointer(const void *pointer) {
80  return LocationAttr(reinterpret_cast<const AttributeStorage *>(pointer));
81  }
82 
83 protected:
86 };
87 
88 inline raw_ostream &operator<<(raw_ostream &os, const Location &loc) {
89  loc.print(os);
90  return os;
91 }
92 
98  : public Attribute::AttrBase<CallSiteLoc, LocationAttr,
99  detail::CallSiteLocationStorage> {
100 public:
101  using Base::Base;
102 
104  static Location get(Location callee, Location caller);
105 
109  static Location get(Location name, ArrayRef<Location> frames);
110 
112  Location getCallee() const;
113 
115  Location getCaller() const;
116 
118  static bool kindof(unsigned kind) {
120  }
121 };
122 
127  : public Attribute::AttrBase<FileLineColLoc, LocationAttr,
128  detail::FileLineColLocationStorage> {
129 public:
130  using Base::Base;
131 
133  static Location get(Identifier filename, unsigned line, unsigned column,
134  MLIRContext *context);
135  static Location get(StringRef filename, unsigned line, unsigned column,
136  MLIRContext *context);
137 
138  StringRef getFilename() const;
139 
140  unsigned getLine() const;
141  unsigned getColumn() const;
142 
144  static bool kindof(unsigned kind) {
146  }
147 };
148 
151 class FusedLoc : public Attribute::AttrBase<FusedLoc, LocationAttr,
152  detail::FusedLocationStorage> {
153 public:
154  using Base::Base;
155 
159  static Location get(ArrayRef<Location> locs, Attribute metadata,
160  MLIRContext *context);
161  static Location get(ArrayRef<Location> locs, MLIRContext *context) {
162  return get(locs, Attribute(), context);
163  }
164 
165  ArrayRef<Location> getLocations() const;
166 
169  Attribute getMetadata() const;
170 
172  static bool kindof(unsigned kind) {
173  return kind == StandardAttributes::FusedLocation;
174  }
175 };
176 
178 class NameLoc : public Attribute::AttrBase<NameLoc, LocationAttr,
179  detail::NameLocationStorage> {
180 public:
181  using Base::Base;
182 
185  static Location get(Identifier name, Location child);
186 
188  static Location get(Identifier name, MLIRContext *context);
189 
191  Identifier getName() const;
192 
194  Location getChildLoc() const;
195 
197  static bool kindof(unsigned kind) {
198  return kind == StandardAttributes::NameLocation;
199  }
200 };
201 
204 class UnknownLoc : public Attribute::AttrBase<UnknownLoc, LocationAttr> {
205 public:
206  using Base::Base;
207 
209  static Location get(MLIRContext *context);
210 
212  static bool kindof(unsigned kind) {
214  }
215 };
216 
223 class OpaqueLoc : public Attribute::AttrBase<OpaqueLoc, LocationAttr,
224  detail::OpaqueLocationStorage> {
225 public:
226  using Base::Base;
227 
230  template <typename T>
231  static Location get(T underlyingLocation, MLIRContext *context) {
232  return get(reinterpret_cast<uintptr_t>(underlyingLocation),
233  ClassID::getID<T>(), UnknownLoc::get(context));
234  }
235 
238  template <typename T>
239  static Location get(T underlyingLocation, Location fallbackLocation) {
240  return get(reinterpret_cast<uintptr_t>(underlyingLocation),
241  ClassID::getID<T>(), fallbackLocation);
242  }
243 
245  template <typename T> static T getUnderlyingLocation(Location location) {
246  assert(isa<T>(location));
247  return reinterpret_cast<T>(
248  location.cast<mlir::OpaqueLoc>().getUnderlyingLocation());
249  }
250 
254  template <typename T>
256  return isa<T>(location)
257  ? reinterpret_cast<T>(
259  : T(nullptr);
260  }
261 
264  template <typename T> static bool isa(Location location) {
265  auto opaque_loc = location.dyn_cast<OpaqueLoc>();
266  return opaque_loc && opaque_loc.getClassId() == ClassID::getID<T>();
267  }
268 
270  uintptr_t getUnderlyingLocation() const;
271 
273  ClassID *getClassId() const;
274 
276  Location getFallbackLocation() const;
277 
279  static bool kindof(unsigned kind) {
280  return kind == StandardAttributes::OpaqueLocation;
281  }
282 
283 private:
284  static Location get(uintptr_t underlyingLocation, ClassID *classID,
285  Location fallbackLocation);
286 };
287 
288 // Make Location hashable.
289 inline ::llvm::hash_code hash_value(Location arg) {
290  return hash_value(arg.impl);
291 }
292 
293 } // end namespace mlir
294 
295 namespace llvm {
296 
297 // Type hash just like pointers.
298 template <> struct DenseMapInfo<mlir::Location> {
301  return mlir::Location::getFromOpaquePointer(pointer);
302  }
305  return mlir::Location::getFromOpaquePointer(pointer);
306  }
307  static unsigned getHashValue(mlir::Location val) {
308  return mlir::hash_value(val);
309  }
310  static bool isEqual(mlir::Location LHS, mlir::Location RHS) {
311  return LHS == RHS;
312  }
313 };
314 
316 template <> struct PointerLikeTypeTraits<mlir::Location> {
317 public:
318  static inline void *getAsVoidPointer(mlir::Location I) {
319  return const_cast<void *>(I.getAsOpaquePointer());
320  }
321  static inline mlir::Location getFromVoidPointer(void *P) {
323  }
324  enum {
325  NumLowBitsAvailable =
326  PointerLikeTypeTraits<mlir::Attribute>::NumLowBitsAvailable
327  };
328 };
329 
330 } // namespace llvm
331 
332 #endif
Definition: InferTypeOpInterface.cpp:20
U cast() const
Definition: Location.h:65
static void * getAsVoidPointer(mlir::Location I)
Definition: Location.h:318
Definition: Location.h:151
Definition: STLExtras.h:95
Definition: PassRegistry.cpp:413
const void * getAsOpaquePointer() const
Methods for supporting PointerLikeTypeTraits.
Definition: Location.h:78
Represents an identity name attached to a child location.
Definition: Location.h:178
Definition: LLVM.h:45
Definition: Location.h:223
static bool kindof(unsigned kind)
Methods for support type inquiry through isa, cast, and dyn_cast.
Definition: Location.h:197
Definition: Identifier.h:26
Definition: Location.h:39
ClassID * getClassId() const
Returns a ClassID* that represents the underlying objects c++ type.
Definition: Location.cpp:133
static unsigned getHashValue(mlir::Location val)
Definition: Location.h:307
static mlir::Location getEmptyKey()
Definition: Location.h:299
static T getUnderlyingLocation(Location location)
Returns a pointer to some data structure that opaque location stores.
Definition: Location.h:245
Definition: Location.h:52
static T getUnderlyingLocationOrNull(Location location)
Definition: Location.h:255
Definition: Attributes.h:153
Definition: LLVM.h:37
Definition: Attributes.h:155
bool isa() const
Type casting utilities on the underlying location.
Definition: Location.h:63
LocationAttr * operator->() const
Definition: Location.h:60
static bool kindof(unsigned kind)
Methods for support type inquiry through isa, cast, and dyn_cast.
Definition: Location.h:212
bool operator!=(Location rhs) const
Definition: Location.h:69
Definition: Attributes.h:53
static mlir::Location getTombstoneKey()
Definition: Location.h:303
Definition: Location.h:204
Definition: Attributes.h:152
void dump() const
Definition: Location.h:73
static bool isEqual(mlir::Location LHS, mlir::Location RHS)
Definition: Location.h:310
static Location getFromOpaquePointer(const void *pointer)
Definition: Location.h:79
Attribute()
Definition: Attributes.h:72
static bool classof(Attribute attr)
Methods for support type inquiry through isa, cast, and dyn_cast.
Definition: Location.h:44
inline ::llvm::hash_code hash_value(AffineExpr arg)
Make AffineExpr hashable.
Definition: AffineExpr.h:201
void print(raw_ostream &os) const
Print the location.
Definition: Location.h:72
U dyn_cast() const
Definition: Location.h:64
static mlir::Location getFromVoidPointer(void *P)
Definition: Location.h:321
Definition: Attributes.h:154
bool operator==(Location rhs) const
Comparison operators.
Definition: Location.h:68
Definition: Location.h:97
Definition: Location.h:126
Definition: StorageUniquerSupport.h:30
Definition: MLIRContext.h:34
static bool kindof(unsigned kind)
Methods for support type inquiry through isa, cast, and dyn_cast.
Definition: Location.h:172
static bool isa(Location location)
Definition: Location.h:264
static bool kindof(unsigned kind)
Methods for support type inquiry through isa, cast, and dyn_cast.
Definition: Location.h:144
static bool kindof(unsigned kind)
Methods for support type inquiry through isa, cast, and dyn_cast.
Definition: Location.h:279
raw_ostream & operator<<(raw_ostream &os, SubViewOp::Range &range)
Definition: Ops.cpp:2759
Location(LocationAttr loc)
Definition: Location.h:54
static Location get(MLIRContext *context)
Get an instance of the UnknownLoc.
Definition: MLIRContext.cpp:559
unsigned getKind() const
Return the classification for this attribute.
Definition: Attributes.h:94
Locations.
Definition: Attributes.h:150
LocationAttr impl
The internal backing location attribute.
Definition: Location.h:85
static bool kindof(unsigned kind)
Methods for support type inquiry through isa, cast, and dyn_cast.
Definition: Location.h:118