llvm-project/mlir/lib/IR/Attributes.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

69 lines
2.4 KiB
C++

//===- Attributes.cpp - MLIR Affine Expr 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/Attributes.h"
#include "mlir/IR/Dialect.h"
using namespace mlir;
using namespace mlir::detail;
//===----------------------------------------------------------------------===//
// AbstractAttribute
//===----------------------------------------------------------------------===//
void AbstractAttribute::walkImmediateSubElements(
Attribute attr, function_ref<void(Attribute)> walkAttrsFn,
function_ref<void(Type)> walkTypesFn) const {
walkImmediateSubElementsFn(attr, walkAttrsFn, walkTypesFn);
}
Attribute
AbstractAttribute::replaceImmediateSubElements(Attribute attr,
ArrayRef<Attribute> replAttrs,
ArrayRef<Type> replTypes) const {
return replaceImmediateSubElementsFn(attr, replAttrs, replTypes);
}
//===----------------------------------------------------------------------===//
// Attribute
//===----------------------------------------------------------------------===//
/// Return the context this attribute belongs to.
MLIRContext *Attribute::getContext() const { return getDialect().getContext(); }
//===----------------------------------------------------------------------===//
// NamedAttribute
//===----------------------------------------------------------------------===//
NamedAttribute::NamedAttribute(StringAttr name, Attribute value)
: name(name), value(value) {
assert(name && value && "expected valid attribute name and value");
assert(name.size() != 0 && "expected valid attribute name");
}
StringAttr NamedAttribute::getName() const {
return llvm::cast<StringAttr>(name);
}
Dialect *NamedAttribute::getNameDialect() const {
return getName().getReferencedDialect();
}
void NamedAttribute::setName(StringAttr newName) {
assert(name && "expected valid attribute name");
name = newName;
}
bool NamedAttribute::operator<(const NamedAttribute &rhs) const {
return getName().compare(rhs.getName()) < 0;
}
bool NamedAttribute::operator<(StringRef rhs) const {
return getName().getValue().compare(rhs) < 0;
}