llvm-project/mlir/lib/Dialect/LLVMIR/IR/LLVMInterfaces.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

116 lines
4.6 KiB
C++

//===- LLVMInterfaces.cpp - LLVM Interfaces ---------------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file defines op interfaces for the LLVM dialect in MLIR.
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/LLVMIR/LLVMInterfaces.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
using namespace mlir;
using namespace mlir::LLVM;
/// Verifies the given array attribute contains symbol references and checks the
/// referenced symbol types using the provided verification function.
static LogicalResult
verifySymbolRefs(Operation *op, StringRef name, ArrayAttr symbolRefs,
llvm::function_ref<LogicalResult(Operation *, SymbolRefAttr)>
verifySymbolType) {
assert(symbolRefs && "expected a non-null attribute");
// Verify that the attribute is a symbol ref array attribute,
// because this constraint is not verified for all attribute
// names processed here (e.g. 'tbaa'). This verification
// is redundant in some cases.
if (!llvm::all_of(symbolRefs, [](Attribute attr) {
return attr && llvm::isa<SymbolRefAttr>(attr);
}))
return op->emitOpError() << name
<< " attribute failed to satisfy constraint: "
"symbol ref array attribute";
for (SymbolRefAttr symbolRef : symbolRefs.getAsRange<SymbolRefAttr>()) {
StringAttr metadataName = symbolRef.getRootReference();
StringAttr symbolName = symbolRef.getLeafReference();
// We want @metadata::@symbol, not just @symbol
if (metadataName == symbolName) {
return op->emitOpError() << "expected '" << symbolRef
<< "' to specify a fully qualified reference";
}
auto metadataOp = SymbolTable::lookupNearestSymbolFrom<LLVM::MetadataOp>(
op->getParentOp(), metadataName);
if (!metadataOp)
return op->emitOpError()
<< "expected '" << symbolRef << "' to reference a metadata op";
Operation *symbolOp =
SymbolTable::lookupNearestSymbolFrom(metadataOp, symbolName);
if (!symbolOp)
return op->emitOpError()
<< "expected '" << symbolRef << "' to be a valid reference";
if (failed(verifySymbolType(symbolOp, symbolRef))) {
return failure();
}
}
return success();
}
/// Verifies the given array attribute contains symbol references that point to
/// metadata operations of the given type.
template <typename OpTy>
LogicalResult verifySymbolRefsPointTo(Operation *op, StringRef name,
ArrayAttr symbolRefs) {
if (!symbolRefs)
return success();
auto verifySymbolType = [op](Operation *symbolOp,
SymbolRefAttr symbolRef) -> LogicalResult {
if (!isa<OpTy>(symbolOp)) {
return op->emitOpError()
<< "expected '" << symbolRef << "' to resolve to a "
<< OpTy::getOperationName();
}
return success();
};
return verifySymbolRefs(op, name, symbolRefs, verifySymbolType);
}
//===----------------------------------------------------------------------===//
// AccessGroupOpInterface
//===----------------------------------------------------------------------===//
LogicalResult mlir::LLVM::detail::verifyAccessGroupOpInterface(Operation *op) {
auto iface = cast<AccessGroupOpInterface>(op);
if (failed(verifySymbolRefsPointTo<LLVM::AccessGroupMetadataOp>(
iface, "access groups", iface.getAccessGroupsOrNull())))
return failure();
return success();
}
//===----------------------------------------------------------------------===//
// AliasAnalysisOpInterface
//===----------------------------------------------------------------------===//
LogicalResult
mlir::LLVM::detail::verifyAliasAnalysisOpInterface(Operation *op) {
auto iface = cast<AliasAnalysisOpInterface>(op);
if (failed(verifySymbolRefsPointTo<LLVM::AliasScopeMetadataOp>(
iface, "alias scopes", iface.getAliasScopesOrNull())))
return failure();
if (failed(verifySymbolRefsPointTo<LLVM::AliasScopeMetadataOp>(
iface, "noalias scopes", iface.getNoAliasScopesOrNull())))
return failure();
if (failed(verifySymbolRefsPointTo<LLVM::TBAATagOp>(
iface, "tbaa tags", iface.getTBAATagsOrNull())))
return failure();
return success();
}
#include "mlir/Dialect/LLVMIR/LLVMInterfaces.cpp.inc"