Skip to content

Commit 18066b5

Browse files
committed
[mlir] Update Location to use new casting infra
This allows for using the llvm namespace cast methods instead of the ones on the Location class. The Location class method are kept for now, but we'll want to remove these eventually (with a really long lead time). Related change: https://reviews.llvm.org/D135870 Differential Revision: https://reviews.llvm.org/D136520
1 parent e3bb359 commit 18066b5

File tree

4 files changed

+45
-12
lines changed

4 files changed

+45
-12
lines changed

mlir/include/mlir/IR/Location.h

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ class Location {
6565
/// Type casting utilities on the underlying location.
6666
template <typename U>
6767
bool isa() const {
68-
return impl.isa<U>();
68+
return llvm::isa<U>(*this);
6969
}
7070
template <typename U>
7171
U dyn_cast() const {
72-
return impl.dyn_cast<U>();
72+
return llvm::dyn_cast<U>(*this);
7373
}
7474
template <typename U>
7575
U cast() const {
76-
return impl.cast<U>();
76+
return llvm::cast<U>(*this);
7777
}
7878

7979
/// Comparison operators.
@@ -170,6 +170,39 @@ struct PointerLikeTypeTraits<mlir::Location> {
170170
PointerLikeTypeTraits<mlir::Attribute>::NumLowBitsAvailable;
171171
};
172172

173+
/// The constructors in mlir::Location ensure that the class is a non-nullable
174+
/// wrapper around mlir::LocationAttr. Override default behavior and always
175+
/// return true for isPresent().
176+
template <>
177+
struct ValueIsPresent<mlir::Location> {
178+
using UnwrappedType = mlir::Location;
179+
static inline bool isPresent(const mlir::Location &location) { return true; }
180+
};
181+
182+
/// Add support for llvm style casts. We provide a cast between To and From if
183+
/// From is mlir::Location or derives from it.
184+
template <typename To, typename From>
185+
struct CastInfo<To, From,
186+
std::enable_if_t<
187+
std::is_same_v<mlir::Location, std::remove_const_t<From>> ||
188+
std::is_base_of_v<mlir::Location, From>>>
189+
: DefaultDoCastIfPossible<To, From, CastInfo<To, From>> {
190+
191+
static inline bool isPossible(mlir::Location location) {
192+
/// Return a constant true instead of a dynamic true when casting to self or
193+
/// up the hierarchy. Additionally, all casting info is deferred to the
194+
/// wrapped mlir::LocationAttr instance stored in mlir::Location.
195+
return std::is_same_v<To, std::remove_const_t<From>> ||
196+
isa<To>(static_cast<mlir::LocationAttr>(location));
197+
}
198+
199+
static inline To castFailed() { return To(); }
200+
201+
static inline To doCast(mlir::Location location) {
202+
return To(location->getImpl());
203+
}
204+
};
205+
173206
} // namespace llvm
174207

175208
#endif

mlir/lib/AsmParser/Parser.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -768,15 +768,15 @@ ParseResult OperationParser::finalize() {
768768
auto &attributeAliases = state.symbols.attributeAliasDefinitions;
769769
auto locID = TypeID::get<DeferredLocInfo *>();
770770
auto resolveLocation = [&, this](auto &opOrArgument) -> LogicalResult {
771-
auto fwdLoc = opOrArgument.getLoc().template dyn_cast<OpaqueLoc>();
771+
auto fwdLoc = dyn_cast<OpaqueLoc>(opOrArgument.getLoc());
772772
if (!fwdLoc || fwdLoc.getUnderlyingTypeID() != locID)
773773
return success();
774774
auto locInfo = deferredLocsReferences[fwdLoc.getUnderlyingLocation()];
775775
Attribute attr = attributeAliases.lookup(locInfo.identifier);
776776
if (!attr)
777777
return this->emitError(locInfo.loc)
778778
<< "operation location alias was never defined";
779-
auto locAttr = attr.dyn_cast<LocationAttr>();
779+
auto locAttr = dyn_cast<LocationAttr>(attr);
780780
if (!locAttr)
781781
return this->emitError(locInfo.loc)
782782
<< "expected location, but found '" << attr << "'";
@@ -1930,7 +1930,7 @@ ParseResult OperationParser::parseLocationAlias(LocationAttr &loc) {
19301930
// If this alias can be resolved, do it now.
19311931
Attribute attr = state.symbols.attributeAliasDefinitions.lookup(identifier);
19321932
if (attr) {
1933-
if (!(loc = attr.dyn_cast<LocationAttr>()))
1933+
if (!(loc = dyn_cast<LocationAttr>(attr)))
19341934
return emitError(tok.getLoc())
19351935
<< "expected location, but found '" << attr << "'";
19361936
} else {

mlir/lib/IR/Diagnostics.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -404,12 +404,12 @@ static Optional<FileLineColLoc> getFileLineColLoc(Location loc) {
404404

405405
/// Return a processable CallSiteLoc from the given location.
406406
static Optional<CallSiteLoc> getCallSiteLoc(Location loc) {
407-
if (auto nameLoc = loc.dyn_cast<NameLoc>())
408-
return getCallSiteLoc(loc.cast<NameLoc>().getChildLoc());
409-
if (auto callLoc = loc.dyn_cast<CallSiteLoc>())
407+
if (auto nameLoc = dyn_cast<NameLoc>(loc))
408+
return getCallSiteLoc(cast<NameLoc>(loc).getChildLoc());
409+
if (auto callLoc = dyn_cast<CallSiteLoc>(loc))
410410
return callLoc;
411-
if (auto fusedLoc = loc.dyn_cast<FusedLoc>()) {
412-
for (auto subLoc : loc.cast<FusedLoc>().getLocations()) {
411+
if (auto fusedLoc = dyn_cast<FusedLoc>(loc)) {
412+
for (auto subLoc : cast<FusedLoc>(loc).getLocations()) {
413413
if (auto callLoc = getCallSiteLoc(subLoc)) {
414414
return callLoc;
415415
}

mlir/lib/IR/Location.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Location FusedLoc::get(ArrayRef<Location> locs, Attribute metadata,
105105
for (auto loc : locs) {
106106
// If the location is a fused location we decompose it if it has no
107107
// metadata or the metadata is the same as the top level metadata.
108-
if (auto fusedLoc = loc.dyn_cast<FusedLoc>()) {
108+
if (auto fusedLoc = llvm::dyn_cast<FusedLoc>(loc)) {
109109
if (fusedLoc.getMetadata() == metadata) {
110110
// UnknownLoc's have already been removed from FusedLocs so we can
111111
// simply add all of the internal locations.

0 commit comments

Comments
 (0)