MLIR  14.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  /// Methods for support type inquiry through isa, cast, and dyn_cast.
39  static bool classof(Attribute attr);
40 };
41 
42 //===----------------------------------------------------------------------===//
43 // Location
44 //===----------------------------------------------------------------------===//
45 
46 /// This class defines the main interface for locations in MLIR and acts as a
47 /// non-nullable wrapper around a LocationAttr.
48 class Location {
49 public:
50  Location(LocationAttr loc) : impl(loc) {
51  assert(loc && "location should never be null.");
52  }
53  Location(const LocationAttr::ImplType *impl) : impl(impl) {
54  assert(impl && "location should never be null.");
55  }
56 
57  /// Return the context this location is uniqued in.
58  MLIRContext *getContext() const { return impl.getContext(); }
59 
60  /// Access the impl location attribute.
61  operator LocationAttr() const { return impl; }
62  LocationAttr *operator->() const { return const_cast<LocationAttr *>(&impl); }
63 
64  /// Type casting utilities on the underlying location.
65  template <typename U> bool isa() const { return impl.isa<U>(); }
66  template <typename U> U dyn_cast() const { return impl.dyn_cast<U>(); }
67  template <typename U> U cast() const { return impl.cast<U>(); }
68 
69  /// Comparison operators.
70  bool operator==(Location rhs) const { return impl == rhs.impl; }
71  bool operator!=(Location rhs) const { return !(*this == rhs); }
72 
73  /// Print the location.
74  void print(raw_ostream &os) const { impl.print(os); }
75  void dump() const { impl.dump(); }
76 
77  friend ::llvm::hash_code hash_value(Location arg);
78 
79  /// Methods for supporting PointerLikeTypeTraits.
80  const void *getAsOpaquePointer() const { return impl.getAsOpaquePointer(); }
81  static Location getFromOpaquePointer(const void *pointer) {
82  return LocationAttr(reinterpret_cast<const AttributeStorage *>(pointer));
83  }
84 
85 protected:
86  /// The internal backing location attribute.
88 };
89 
90 inline raw_ostream &operator<<(raw_ostream &os, const Location &loc) {
91  loc.print(os);
92  return os;
93 }
94 
95 // Make Location hashable.
96 inline ::llvm::hash_code hash_value(Location arg) {
97  return hash_value(arg.impl);
98 }
99 
100 } // namespace mlir
101 
102 //===----------------------------------------------------------------------===//
103 // Tablegen Attribute Declarations
104 //===----------------------------------------------------------------------===//
105 
106 #define GET_ATTRDEF_CLASSES
107 #include "mlir/IR/BuiltinLocationAttributes.h.inc"
108 
109 namespace mlir {
110 
111 //===----------------------------------------------------------------------===//
112 // OpaqueLoc
113 //===----------------------------------------------------------------------===//
114 
115 /// Returns an instance of opaque location which contains a given pointer to
116 /// an object. The corresponding MLIR location is set to UnknownLoc.
117 template <typename T>
118 inline OpaqueLoc OpaqueLoc::get(T underlyingLocation, MLIRContext *context) {
119  return get(reinterpret_cast<uintptr_t>(underlyingLocation), TypeID::get<T>(),
120  UnknownLoc::get(context));
121 }
122 } // namespace mlir
123 
124 //===----------------------------------------------------------------------===//
125 // LLVM Utilities
126 //===----------------------------------------------------------------------===//
127 
128 namespace llvm {
129 
130 // Type hash just like pointers.
131 template <> struct DenseMapInfo<mlir::Location> {
133  auto *pointer = llvm::DenseMapInfo<void *>::getEmptyKey();
134  return mlir::Location::getFromOpaquePointer(pointer);
135  }
138  return mlir::Location::getFromOpaquePointer(pointer);
139  }
140  static unsigned getHashValue(mlir::Location val) {
141  return mlir::hash_value(val);
142  }
143  static bool isEqual(mlir::Location LHS, mlir::Location RHS) {
144  return LHS == RHS;
145  }
146 };
147 
148 /// We align LocationStorage by 8, so allow LLVM to steal the low bits.
149 template <> struct PointerLikeTypeTraits<mlir::Location> {
150 public:
151  static inline void *getAsVoidPointer(mlir::Location I) {
152  return const_cast<void *>(I.getAsOpaquePointer());
153  }
154  static inline mlir::Location getFromVoidPointer(void *P) {
156  }
157  static constexpr int NumLowBitsAvailable =
158  PointerLikeTypeTraits<mlir::Attribute>::NumLowBitsAvailable;
159 };
160 
161 } // namespace llvm
162 
163 #endif
Include the generated interface declarations.
U cast() const
Definition: Location.h:67
static void * getAsVoidPointer(mlir::Location I)
Definition: Location.h:151
Location(const LocationAttr::ImplType *impl)
Definition: Location.h:53
const void * getAsOpaquePointer() const
Methods for supporting PointerLikeTypeTraits.
Definition: Location.h:80
Location objects represent source locations information in MLIR.
Definition: Location.h:31
raw_ostream & operator<<(raw_ostream &os, const AliasResult &result)
Definition: AliasAnalysis.h:78
static unsigned getHashValue(mlir::Location val)
Definition: Location.h:140
static mlir::Location getEmptyKey()
Definition: Location.h:132
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:48
constexpr Attribute()
Definition: Attributes.h:36
bool isa() const
Type casting utilities on the underlying location.
Definition: Location.h:65
LocationAttr * operator->() const
Definition: Location.h:62
bool operator!=(Location rhs) const
Definition: Location.h:71
friend ::llvm::hash_code hash_value(Attribute arg)
Definition: Attributes.h:128
Attributes are known-constant values of operations.
Definition: Attributes.h:24
static mlir::Location getTombstoneKey()
Definition: Location.h:136
void dump() const
Definition: Location.h:75
static bool isEqual(mlir::Location LHS, mlir::Location RHS)
Definition: Location.h:143
A utility result that is used to signal how to proceed with an ongoing walk:
Definition: Visitors.h:34
static Location getFromOpaquePointer(const void *pointer)
Definition: Location.h:81
static bool classof(Attribute attr)
Methods for support type inquiry through isa, cast, and dyn_cast.
Definition: Location.cpp:66
inline ::llvm::hash_code hash_value(AffineExpr arg)
Make AffineExpr hashable.
Definition: AffineExpr.h:240
void print(raw_ostream &os) const
Print the location.
Definition: Location.h:74
U dyn_cast() const
Definition: Location.h:66
static mlir::Location getFromVoidPointer(void *P)
Definition: Location.h:154
MLIRContext * getContext() const
Return the context this location is uniqued in.
Definition: Location.h:58
bool operator==(Location rhs) const
Comparison operators.
Definition: Location.h:70
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:55
Base storage class appearing in an attribute.
ImplType * impl
Definition: Attributes.h:99
Location(LocationAttr loc)
Definition: Location.h:50
WalkResult walk(function_ref< WalkResult(Location)> walkFn)
Walk all of the locations nested under, and including, the current.
Definition: Location.cpp:40
LocationAttr impl
The internal backing location attribute.
Definition: Location.h:87