llvm-project/mlir/lib/IR/Location.cpp
Tres Popp c1fa60b4cd [mlir] Update method cast calls to function calls
The MLIR classes Type/Attribute/Operation/Op/Value support
cast/dyn_cast/isa/dyn_cast_or_null functionality through llvm's doCast
functionality in addition to defining methods with the same name.
This change begins the migration of uses of the method to the
corresponding function call as has been decided as more consistent.

Note that there still exist classes that only define methods directly,
such as AffineExpr, and this does not include work currently to support
a functional cast/isa call.

Context:

* https://mlir.llvm.org/deprecation/ at "Use the free function variants for dyn_cast/cast/isa/…"
* Original discussion at https://discourse.llvm.org/t/preferred-casting-style-going-forward/68443

Implementation:
This follows a previous patch that updated calls
`op.cast<T>()-> cast<T>(op)`. However some cases could not handle an
unprefixed `cast` call due to occurrences of variables named cast, or
occurring inside of class definitions which would resolve to the method.
All C++ files that did not work automatically with `cast<T>()` are
updated here to `llvm::cast` and similar with the intention that they
can be easily updated after the methods are removed through a
find-replace.

See https://github.com/llvm/llvm-project/compare/main...tpopp:llvm-project:tidy-cast-check
for the clang-tidy check that is used and then update printed
occurrences of the function to include `llvm::` before.

One can then run the following:
```
ninja -C $BUILD_DIR clang-tidy

run-clang-tidy -clang-tidy-binary=$BUILD_DIR/bin/clang-tidy -checks='-*,misc-cast-functions'\
                 -export-fixes /tmp/cast/casts.yaml mlir/*\
                 -header-filter=mlir/ -fix

rm -rf $BUILD_DIR/tools/mlir/**/*.inc
```

Differential Revision: https://reviews.llvm.org/D150348
2023-05-12 11:21:30 +02:00

124 lines
4.6 KiB
C++

//===- Location.cpp - MLIR Location Classes -------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "mlir/IR/Location.h"
#include "mlir/IR/BuiltinDialect.h"
#include "mlir/IR/Visitors.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/TypeSwitch.h"
using namespace mlir;
using namespace mlir::detail;
//===----------------------------------------------------------------------===//
/// Tablegen Attribute Definitions
//===----------------------------------------------------------------------===//
#define GET_ATTRDEF_CLASSES
#include "mlir/IR/BuiltinLocationAttributes.cpp.inc"
//===----------------------------------------------------------------------===//
// BuiltinDialect
//===----------------------------------------------------------------------===//
void BuiltinDialect::registerLocationAttributes() {
addAttributes<
#define GET_ATTRDEF_LIST
#include "mlir/IR/BuiltinLocationAttributes.cpp.inc"
>();
}
//===----------------------------------------------------------------------===//
// LocationAttr
//===----------------------------------------------------------------------===//
WalkResult LocationAttr::walk(function_ref<WalkResult(Location)> walkFn) {
if (walkFn(*this).wasInterrupted())
return WalkResult::interrupt();
return TypeSwitch<LocationAttr, WalkResult>(*this)
.Case([&](CallSiteLoc callLoc) -> WalkResult {
if (callLoc.getCallee()->walk(walkFn).wasInterrupted())
return WalkResult::interrupt();
return callLoc.getCaller()->walk(walkFn);
})
.Case([&](FusedLoc fusedLoc) -> WalkResult {
for (Location subLoc : fusedLoc.getLocations())
if (subLoc->walk(walkFn).wasInterrupted())
return WalkResult::interrupt();
return WalkResult::advance();
})
.Case([&](NameLoc nameLoc) -> WalkResult {
return nameLoc.getChildLoc()->walk(walkFn);
})
.Case([&](OpaqueLoc opaqueLoc) -> WalkResult {
return opaqueLoc.getFallbackLocation()->walk(walkFn);
})
.Default(WalkResult::advance());
}
/// Methods for support type inquiry through isa, cast, and dyn_cast.
bool LocationAttr::classof(Attribute attr) {
return llvm::isa<CallSiteLoc, FileLineColLoc, FusedLoc, NameLoc, OpaqueLoc,
UnknownLoc>(attr);
}
//===----------------------------------------------------------------------===//
// CallSiteLoc
//===----------------------------------------------------------------------===//
CallSiteLoc CallSiteLoc::get(Location name, ArrayRef<Location> frames) {
assert(!frames.empty() && "required at least 1 call frame");
Location caller = frames.back();
for (auto frame : llvm::reverse(frames.drop_back()))
caller = CallSiteLoc::get(frame, caller);
return CallSiteLoc::get(name, caller);
}
//===----------------------------------------------------------------------===//
// FusedLoc
//===----------------------------------------------------------------------===//
Location FusedLoc::get(ArrayRef<Location> locs, Attribute metadata,
MLIRContext *context) {
// Unique the set of locations to be fused.
llvm::SmallSetVector<Location, 4> decomposedLocs;
for (auto loc : locs) {
// If the location is a fused location we decompose it if it has no
// metadata or the metadata is the same as the top level metadata.
if (auto fusedLoc = llvm::dyn_cast<FusedLoc>(loc)) {
if (fusedLoc.getMetadata() == metadata) {
// UnknownLoc's have already been removed from FusedLocs so we can
// simply add all of the internal locations.
decomposedLocs.insert(fusedLoc.getLocations().begin(),
fusedLoc.getLocations().end());
continue;
}
}
// Otherwise, only add known locations to the set.
if (!llvm::isa<UnknownLoc>(loc))
decomposedLocs.insert(loc);
}
locs = decomposedLocs.getArrayRef();
// Handle the simple cases of less than two locations. Ensure the metadata (if
// provided) is not dropped.
if (locs.empty()) {
if (!metadata)
return UnknownLoc::get(context);
// TODO: Investigate ASAN failure when using implicit conversion from
// Location to ArrayRef<Location> below.
return Base::get(context, ArrayRef<Location>{UnknownLoc::get(context)},
metadata);
}
if (locs.size() == 1 && !metadata)
return locs.front();
return Base::get(context, locs, metadata);
}