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