MLIR  21.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 directly under, and including, the
36  /// current. This means that if a location is nested under a non-location
37  /// attribute, it will *not* be walked by this method. This walk is performed
38  /// in pre-order to get this behavior.
40 
41  /// Return an instance of the given location type if one is nested under the
42  /// current location. Returns nullptr if one could not be found.
43  template <typename T>
45  T result = {};
46  walk([&](auto loc) {
47  if (auto typedLoc = llvm::dyn_cast<T>(loc)) {
48  result = typedLoc;
49  return WalkResult::interrupt();
50  }
51  return WalkResult::advance();
52  });
53  return result;
54  }
55 
56  /// Methods for support type inquiry through isa, cast, and dyn_cast.
57  static bool classof(Attribute attr);
58 };
59 
60 //===----------------------------------------------------------------------===//
61 // Location
62 //===----------------------------------------------------------------------===//
63 
64 /// This class defines the main interface for locations in MLIR and acts as a
65 /// non-nullable wrapper around a LocationAttr.
66 class Location {
67 public:
68  Location(LocationAttr loc) : impl(loc) {
69  assert(loc && "location should never be null.");
70  }
72  assert(impl && "location should never be null.");
73  }
74 
75  /// Return the context this location is uniqued in.
76  MLIRContext *getContext() const { return impl.getContext(); }
77 
78  /// Access the impl location attribute.
79  operator LocationAttr() const { return impl; }
80  LocationAttr *operator->() const { return const_cast<LocationAttr *>(&impl); }
81 
82  /// Comparison operators.
83  bool operator==(Location rhs) const { return impl == rhs.impl; }
84  bool operator!=(Location rhs) const { return !(*this == rhs); }
85 
86  /// Print the location.
87  void print(raw_ostream &os) const { impl.print(os); }
88  void dump() const { impl.dump(); }
89 
90  friend ::llvm::hash_code hash_value(Location arg);
91 
92  /// Methods for supporting PointerLikeTypeTraits.
93  const void *getAsOpaquePointer() const { return impl.getAsOpaquePointer(); }
94  static Location getFromOpaquePointer(const void *pointer) {
95  return LocationAttr(reinterpret_cast<const AttributeStorage *>(pointer));
96  }
97 
98  /// Support llvm style casting.
99  static bool classof(Attribute attr) { return llvm::isa<LocationAttr>(attr); }
100 
101 protected:
102  /// The internal backing location attribute.
104 };
105 
106 inline raw_ostream &operator<<(raw_ostream &os, const Location &loc) {
107  loc.print(os);
108  return os;
109 }
110 
111 // Make Location hashable.
112 inline ::llvm::hash_code hash_value(Location arg) {
113  return hash_value(arg.impl);
114 }
115 
116 } // namespace mlir
117 
118 //===----------------------------------------------------------------------===//
119 // Tablegen Attribute Declarations
120 //===----------------------------------------------------------------------===//
121 
122 // Forward declaration for class created later.
123 namespace mlir::detail {
124 struct FileLineColRangeAttrStorage;
125 } // namespace mlir::detail
126 
127 #define GET_ATTRDEF_CLASSES
128 #include "mlir/IR/BuiltinLocationAttributes.h.inc"
129 
130 namespace mlir {
131 
132 //===----------------------------------------------------------------------===//
133 // FusedLoc
134 //===----------------------------------------------------------------------===//
135 
136 /// This class represents a fused location whose metadata is known to be an
137 /// instance of the given type.
138 template <typename MetadataT>
139 class FusedLocWith : public FusedLoc {
140 public:
141  using FusedLoc::FusedLoc;
142 
143  /// Return the metadata associated with this fused location.
144  MetadataT getMetadata() const {
145  return llvm::cast<MetadataT>(FusedLoc::getMetadata());
146  }
147 
148  /// Support llvm style casting.
149  static bool classof(Attribute attr) {
150  auto fusedLoc = llvm::dyn_cast<FusedLoc>(attr);
151  return fusedLoc && mlir::isa_and_nonnull<MetadataT>(fusedLoc.getMetadata());
152  }
153 };
154 
155 //===----------------------------------------------------------------------===//
156 // FileLineColLoc
157 //===----------------------------------------------------------------------===//
158 
159 /// An instance of this location represents a tuple of file, line number, and
160 /// column number. This is similar to the type of location that you get from
161 /// most source languages.
162 ///
163 /// FileLineColLoc is a view to FileLineColRange with one line and column.
165 public:
166  using FileLineColRange::FileLineColRange;
167 
168  static FileLineColLoc get(StringAttr filename, unsigned line,
169  unsigned column);
170  static FileLineColLoc get(MLIRContext *context, StringRef fileName,
171  unsigned line, unsigned column);
172 
173  StringAttr getFilename() const;
174  unsigned getLine() const;
175  unsigned getColumn() const;
176 };
177 
178 /// Returns true iff the given location is a FileLineColRange with exactly one
179 /// line and column.
181 
182 //===----------------------------------------------------------------------===//
183 // OpaqueLoc
184 //===----------------------------------------------------------------------===//
185 
186 /// Returns an instance of opaque location which contains a given pointer to
187 /// an object. The corresponding MLIR location is set to UnknownLoc.
188 template <typename T>
189 inline OpaqueLoc OpaqueLoc::get(T underlyingLocation, MLIRContext *context) {
190  return get(reinterpret_cast<uintptr_t>(underlyingLocation), TypeID::get<T>(),
191  UnknownLoc::get(context));
192 }
193 
194 //===----------------------------------------------------------------------===//
195 // SubElements
196 //===----------------------------------------------------------------------===//
197 
198 /// Enable locations to be introspected as sub-elements.
199 template <>
201  static void walk(Location param, AttrTypeImmediateSubElementWalker &walker) {
202  walker.walk(param);
203  }
205  TypeSubElementReplacements &typeRepls) {
206  return cast<LocationAttr>(attrRepls.take_front(1)[0]);
207  }
208 };
209 
210 } // namespace mlir
211 
212 //===----------------------------------------------------------------------===//
213 // LLVM Utilities
214 //===----------------------------------------------------------------------===//
215 
216 namespace llvm {
217 
218 // Type hash just like pointers.
219 template <>
220 struct DenseMapInfo<mlir::Location> {
222  auto *pointer = llvm::DenseMapInfo<void *>::getEmptyKey();
223  return mlir::Location::getFromOpaquePointer(pointer);
224  }
227  return mlir::Location::getFromOpaquePointer(pointer);
228  }
229  static unsigned getHashValue(mlir::Location val) {
230  return mlir::hash_value(val);
231  }
232  static bool isEqual(mlir::Location LHS, mlir::Location RHS) {
233  return LHS == RHS;
234  }
235 };
236 
237 /// We align LocationStorage by 8, so allow LLVM to steal the low bits.
238 template <>
239 struct PointerLikeTypeTraits<mlir::Location> {
240 public:
241  static inline void *getAsVoidPointer(mlir::Location I) {
242  return const_cast<void *>(I.getAsOpaquePointer());
243  }
244  static inline mlir::Location getFromVoidPointer(void *P) {
246  }
247  static constexpr int NumLowBitsAvailable =
249 };
250 
251 /// The constructors in mlir::Location ensure that the class is a non-nullable
252 /// wrapper around mlir::LocationAttr. Override default behavior and always
253 /// return true for isPresent().
254 template <>
255 struct ValueIsPresent<mlir::Location> {
257  static inline bool isPresent(const mlir::Location &location) { return true; }
258 };
259 
260 /// Add support for llvm style casts. We provide a cast between To and From if
261 /// From is mlir::Location or derives from it.
262 template <typename To, typename From>
263 struct CastInfo<To, From,
264  std::enable_if_t<
265  std::is_same_v<mlir::Location, std::remove_const_t<From>> ||
266  std::is_base_of_v<mlir::Location, From>>>
267  : DefaultDoCastIfPossible<To, From, CastInfo<To, From>> {
268 
269  static inline bool isPossible(mlir::Location location) {
270  /// Return a constant true instead of a dynamic true when casting to self or
271  /// up the hierarchy. Additionally, all casting info is deferred to the
272  /// wrapped mlir::LocationAttr instance stored in mlir::Location.
273  return std::is_same_v<To, std::remove_const_t<From>> ||
274  isa<To>(static_cast<mlir::LocationAttr>(location));
275  }
276 
277  static inline To castFailed() { return To(); }
278 
279  static inline To doCast(mlir::Location location) {
280  return To(location->getImpl());
281  }
282 };
283 
284 } // namespace llvm
285 
286 #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:144
An instance of this location represents a tuple of file, line number, and column number.
Definition: Location.h:164
unsigned getLine() const
Definition: Location.cpp:176
StringAttr getFilename() const
Definition: Location.cpp:172
static FileLineColLoc get(StringAttr filename, unsigned line, unsigned column)
Definition: Location.cpp:160
unsigned getColumn() const
Definition: Location.cpp:178
This class represents a fused location whose metadata is known to be an instance of the given type.
Definition: Location.h:139
MetadataT getMetadata() const
Return the metadata associated with this fused location.
Definition: Location.h:144
static bool classof(Attribute attr)
Support llvm style casting.
Definition: Location.h:149
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 directly under, and including, the current.
Definition: Location.cpp:127
static bool classof(Attribute attr)
Methods for support type inquiry through isa, cast, and dyn_cast.
Definition: Location.cpp:140
T findInstanceOf()
Return an instance of the given location type if one is nested under the current location.
Definition: Location.h:44
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:66
const void * getAsOpaquePointer() const
Methods for supporting PointerLikeTypeTraits.
Definition: Location.h:93
MLIRContext * getContext() const
Return the context this location is uniqued in.
Definition: Location.h:76
friend ::llvm::hash_code hash_value(Location arg)
Definition: Location.h:112
Location(LocationAttr loc)
Definition: Location.h:68
static bool classof(Attribute attr)
Support llvm style casting.
Definition: Location.h:99
Location(const LocationAttr::ImplType *impl)
Definition: Location.h:71
void dump() const
Definition: Location.h:88
LocationAttr impl
The internal backing location attribute.
Definition: Location.h:103
static Location getFromOpaquePointer(const void *pointer)
Definition: Location.h:94
bool operator!=(Location rhs) const
Definition: Location.h:84
LocationAttr * operator->() const
Definition: Location.h:80
bool operator==(Location rhs) const
Comparison operators.
Definition: Location.h:83
void print(raw_ostream &os) const
Print the location.
Definition: Location.h:87
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
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition: CallGraph.h:229
AttrTypeReplacer.
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:247
raw_ostream & operator<<(raw_ostream &os, const AliasResult &result)
Definition: AliasAnalysis.h:78
bool isStrictFileLineColLoc(Location loc)
Returns true iff the given location is a FileLineColRange with exactly one line and column.
Definition: Location.cpp:180
static bool isEqual(mlir::Location LHS, mlir::Location RHS)
Definition: Location.h:232
static unsigned getHashValue(mlir::Location val)
Definition: Location.h:229
static mlir::Location getEmptyKey()
Definition: Location.h:221
static mlir::Location getTombstoneKey()
Definition: Location.h:225
static void * getAsVoidPointer(mlir::Location I)
Definition: Location.h:241
static mlir::Location getFromVoidPointer(void *P)
Definition: Location.h:244
static bool isPresent(const mlir::Location &location)
Definition: Location.h:257
static Location replace(Location param, AttrSubElementReplacements &attrRepls, TypeSubElementReplacements &typeRepls)
Definition: Location.h:204
static void walk(Location param, AttrTypeImmediateSubElementWalker &walker)
Definition: Location.h:201
This class provides support for interacting with the SubElementInterfaces for different types of para...