MLIR 24.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
20namespace mlir {
21
22class Location;
23class WalkResult;
24class 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.
32class LocationAttr : public Attribute {
33public:
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.
76class Location {
77public:
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#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
99 LLVM_DUMP_METHOD void dump() const;
100#endif
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
113protected:
114 /// The internal backing location attribute.
116};
117
118inline raw_ostream &operator<<(raw_ostream &os, const Location &loc) {
119 loc.print(os);
120 return os;
121}
122
123// Make Location hashable.
124inline ::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// Forward declaration for class created later.
135namespace mlir::detail {
137} // namespace mlir::detail
138
139#define GET_ATTRDEF_CLASSES
140#include "mlir/IR/BuiltinLocationAttributes.h.inc"
141
142namespace mlir {
143
144//===----------------------------------------------------------------------===//
145// FusedLoc
146//===----------------------------------------------------------------------===//
147
148/// This class represents a fused location whose metadata is known to be an
149/// instance of the given type.
150template <typename MetadataT>
151class FusedLocWith : public FusedLoc {
152public:
153 using FusedLoc::FusedLoc;
154
155 /// Return the metadata associated with this fused location.
156 MetadataT getMetadata() const {
157 return llvm::cast<MetadataT>(FusedLoc::getMetadata());
158 }
159
160 /// Support llvm style casting.
161 static bool classof(Attribute attr) {
162 auto fusedLoc = llvm::dyn_cast<FusedLoc>(attr);
163 return fusedLoc && mlir::isa_and_nonnull<MetadataT>(fusedLoc.getMetadata());
164 }
165};
166
167//===----------------------------------------------------------------------===//
168// FileLineColLoc
169//===----------------------------------------------------------------------===//
170
171/// An instance of this location represents a tuple of file, line number, and
172/// column number. This is similar to the type of location that you get from
173/// most source languages.
174///
175/// FileLineColLoc is a view to FileLineColRange with one line and column.
177public:
178 using FileLineColRange::FileLineColRange;
179
180 static FileLineColLoc get(StringAttr filename, unsigned line,
181 unsigned column);
182 static FileLineColLoc get(MLIRContext *context, StringRef fileName,
183 unsigned line, unsigned column);
184
185 StringAttr getFilename() const;
186 unsigned getLine() const;
187 unsigned getColumn() const;
188};
189
190/// Returns true iff the given location is a FileLineColRange with exactly one
191/// line and column.
193
194//===----------------------------------------------------------------------===//
195// OpaqueLoc
196//===----------------------------------------------------------------------===//
197
198/// Returns an instance of opaque location which contains a given pointer to
199/// an object. The corresponding MLIR location is set to UnknownLoc.
200template <typename T>
201inline OpaqueLoc OpaqueLoc::get(T underlyingLocation, MLIRContext *context) {
202 return get(reinterpret_cast<uintptr_t>(underlyingLocation), TypeID::get<T>(),
203 UnknownLoc::get(context));
204}
205
206//===----------------------------------------------------------------------===//
207// SubElements
208//===----------------------------------------------------------------------===//
209
210/// Enable locations to be introspected as sub-elements.
211template <>
213 static void walk(Location param, AttrTypeImmediateSubElementWalker &walker) {
214 walker.walk(param);
215 }
217 TypeSubElementReplacements &typeRepls) {
218 return cast<LocationAttr>(attrRepls.take_front(1)[0]);
219 }
220};
221
222} // namespace mlir
223
224//===----------------------------------------------------------------------===//
225// LLVM Utilities
226//===----------------------------------------------------------------------===//
227
228namespace llvm {
229
230// Type hash just like pointers.
231template <>
232struct DenseMapInfo<mlir::Location> {
233 static unsigned getHashValue(mlir::Location val) {
234 return mlir::hash_value(val);
235 }
236 static bool isEqual(mlir::Location LHS, mlir::Location RHS) {
237 return LHS == RHS;
238 }
239};
240
241/// We align LocationStorage by 8, so allow LLVM to steal the low bits.
242template <>
243struct PointerLikeTypeTraits<mlir::Location> {
244public:
245 static inline void *getAsVoidPointer(mlir::Location I) {
246 return const_cast<void *>(I.getAsOpaquePointer());
247 }
248 static inline mlir::Location getFromVoidPointer(void *P) {
250 }
251 static constexpr int NumLowBitsAvailable =
252 PointerLikeTypeTraits<mlir::Attribute>::NumLowBitsAvailable;
253};
254
255/// The constructors in mlir::Location ensure that the class is a non-nullable
256/// wrapper around mlir::LocationAttr. Override default behavior and always
257/// return true for isPresent().
258template <>
259struct ValueIsPresent<mlir::Location> {
261 static inline bool isPresent(const mlir::Location &location) { return true; }
262};
263
264/// Add support for llvm style casts. We provide a cast between To and From if
265/// From is mlir::Location or derives from it.
266template <typename To, typename From>
267struct CastInfo<To, From,
268 std::enable_if_t<
269 std::is_same_v<mlir::Location, std::remove_const_t<From>> ||
270 std::is_base_of_v<mlir::Location, From>>>
271 : DefaultDoCastIfPossible<To, From, CastInfo<To, From>> {
272
273 static inline bool isPossible(mlir::Location location) {
274 /// Return a constant true instead of a dynamic true when casting to self or
275 /// up the hierarchy. Additionally, all casting info is deferred to the
276 /// wrapped mlir::LocationAttr instance stored in mlir::Location.
277 return std::is_same_v<To, std::remove_const_t<From>> ||
278 isa<To>(static_cast<mlir::LocationAttr>(location));
279 }
280
281 static inline To castFailed() { return To(); }
282
283 static inline To doCast(mlir::Location location) {
284 return To(location->getImpl());
285 }
286};
287
288} // namespace llvm
289
290#endif
void walk(Attribute element)
Walk an attribute.
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:146
MLIRContext * getContext() const
Return the context this attribute belongs to.
AttributeStorage ImplType
Definition Attributes.h:33
An instance of this location represents a tuple of file, line number, and column number.
Definition Location.h:176
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:151
MetadataT getMetadata() const
Return the metadata associated with this fused location.
Definition Location.h:156
static bool classof(Attribute attr)
Support llvm style casting.
Definition Location.h:161
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
constexpr Attribute()=default
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
MLIRContext * getContext() const
Return the context this location is uniqued in.
Definition Location.h:86
Location(LocationAttr loc)
Definition Location.h:78
static bool classof(Attribute attr)
Support llvm style casting.
Definition Location.h:111
friend::llvm::hash_code hash_value(Location arg)
Location(const LocationAttr::ImplType *impl)
Definition Location.h:81
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:94
LocationAttr * operator->() const
Definition Location.h:90
const void * getAsOpaquePointer() const
Methods for supporting PointerLikeTypeTraits.
Definition Location.h:105
bool operator==(Location rhs) const
Comparison operators.
Definition Location.h:93
LLVM_DUMP_METHOD void dump() const
Definition Location.cpp:114
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:71
static TypeID get()
Construct a type info object for the given type T.
Definition TypeID.h:245
A utility result that is used to signal how to proceed with an ongoing walk:
Definition WalkResult.h:29
static WalkResult advance()
Definition WalkResult.h:47
static WalkResult interrupt()
Definition WalkResult.h:46
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition CallGraph.h:229
AttrTypeReplacer.
Include the generated interface declarations.
raw_ostream & operator<<(raw_ostream &os, const AliasResult &result)
AttrTypeSubElementReplacements< Attribute > AttrSubElementReplacements
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:249
llvm::function_ref< Fn > function_ref
Definition LLVM.h:155
AttrTypeSubElementReplacements< Type > TypeSubElementReplacements
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:236
static unsigned getHashValue(mlir::Location val)
Definition Location.h:233
static void * getAsVoidPointer(mlir::Location I)
Definition Location.h:245
static mlir::Location getFromVoidPointer(void *P)
Definition Location.h:248
static bool isPresent(const mlir::Location &location)
Definition Location.h:261
static Location replace(Location param, AttrSubElementReplacements &attrRepls, TypeSubElementReplacements &typeRepls)
Definition Location.h:216
static void walk(Location param, AttrTypeImmediateSubElementWalker &walker)
Definition Location.h:213
This class provides support for interacting with the SubElementInterfaces for different types of para...