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
217 lines
7.1 KiB
C++
217 lines
7.1 KiB
C++
//===- ComplexOps.cpp - MLIR Complex Operations ---------------------------===//
|
|
//
|
|
// 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/Dialect/Complex/IR/Complex.h"
|
|
#include "mlir/IR/Builders.h"
|
|
#include "mlir/IR/Matchers.h"
|
|
|
|
using namespace mlir;
|
|
using namespace mlir::complex;
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// ConstantOp
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
OpFoldResult ConstantOp::fold(FoldAdaptor adaptor) {
|
|
return getValue();
|
|
}
|
|
|
|
void ConstantOp::getAsmResultNames(
|
|
function_ref<void(Value, StringRef)> setNameFn) {
|
|
setNameFn(getResult(), "cst");
|
|
}
|
|
|
|
bool ConstantOp::isBuildableWith(Attribute value, Type type) {
|
|
if (auto arrAttr = llvm::dyn_cast<ArrayAttr>(value)) {
|
|
auto complexTy = llvm::dyn_cast<ComplexType>(type);
|
|
if (!complexTy || arrAttr.size() != 2)
|
|
return false;
|
|
auto complexEltTy = complexTy.getElementType();
|
|
if (auto fre = llvm::dyn_cast<FloatAttr>(arrAttr[0])) {
|
|
auto im = llvm::dyn_cast<FloatAttr>(arrAttr[1]);
|
|
return im && fre.getType() == complexEltTy &&
|
|
im.getType() == complexEltTy;
|
|
}
|
|
if (auto ire = llvm::dyn_cast<IntegerAttr>(arrAttr[0])) {
|
|
auto im = llvm::dyn_cast<IntegerAttr>(arrAttr[1]);
|
|
return im && ire.getType() == complexEltTy &&
|
|
im.getType() == complexEltTy;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
LogicalResult ConstantOp::verify() {
|
|
ArrayAttr arrayAttr = getValue();
|
|
if (arrayAttr.size() != 2) {
|
|
return emitOpError(
|
|
"requires 'value' to be a complex constant, represented as array of "
|
|
"two values");
|
|
}
|
|
|
|
auto complexEltTy = getType().getElementType();
|
|
auto re = llvm::dyn_cast<FloatAttr>(arrayAttr[0]);
|
|
auto im = llvm::dyn_cast<FloatAttr>(arrayAttr[1]);
|
|
if (!re || !im)
|
|
return emitOpError("requires attribute's elements to be float attributes");
|
|
if (complexEltTy != re.getType() || complexEltTy != im.getType()) {
|
|
return emitOpError()
|
|
<< "requires attribute's element types (" << re.getType() << ", "
|
|
<< im.getType()
|
|
<< ") to match the element type of the op's return type ("
|
|
<< complexEltTy << ")";
|
|
}
|
|
return success();
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// CreateOp
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
OpFoldResult CreateOp::fold(FoldAdaptor adaptor) {
|
|
// Fold complex.create(complex.re(op), complex.im(op)).
|
|
if (auto reOp = getOperand(0).getDefiningOp<ReOp>()) {
|
|
if (auto imOp = getOperand(1).getDefiningOp<ImOp>()) {
|
|
if (reOp.getOperand() == imOp.getOperand()) {
|
|
return reOp.getOperand();
|
|
}
|
|
}
|
|
}
|
|
return {};
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// ImOp
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
OpFoldResult ImOp::fold(FoldAdaptor adaptor) {
|
|
ArrayAttr arrayAttr = adaptor.getComplex().dyn_cast_or_null<ArrayAttr>();
|
|
if (arrayAttr && arrayAttr.size() == 2)
|
|
return arrayAttr[1];
|
|
if (auto createOp = getOperand().getDefiningOp<CreateOp>())
|
|
return createOp.getOperand(1);
|
|
return {};
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// ReOp
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
OpFoldResult ReOp::fold(FoldAdaptor adaptor) {
|
|
ArrayAttr arrayAttr = adaptor.getComplex().dyn_cast_or_null<ArrayAttr>();
|
|
if (arrayAttr && arrayAttr.size() == 2)
|
|
return arrayAttr[0];
|
|
if (auto createOp = getOperand().getDefiningOp<CreateOp>())
|
|
return createOp.getOperand(0);
|
|
return {};
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// AddOp
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
OpFoldResult AddOp::fold(FoldAdaptor adaptor) {
|
|
// complex.add(complex.sub(a, b), b) -> a
|
|
if (auto sub = getLhs().getDefiningOp<SubOp>())
|
|
if (getRhs() == sub.getRhs())
|
|
return sub.getLhs();
|
|
|
|
// complex.add(b, complex.sub(a, b)) -> a
|
|
if (auto sub = getRhs().getDefiningOp<SubOp>())
|
|
if (getLhs() == sub.getRhs())
|
|
return sub.getLhs();
|
|
|
|
// complex.add(a, complex.constant<0.0, 0.0>) -> a
|
|
if (auto constantOp = getRhs().getDefiningOp<ConstantOp>()) {
|
|
auto arrayAttr = constantOp.getValue();
|
|
if (llvm::cast<FloatAttr>(arrayAttr[0]).getValue().isZero() &&
|
|
llvm::cast<FloatAttr>(arrayAttr[1]).getValue().isZero()) {
|
|
return getLhs();
|
|
}
|
|
}
|
|
|
|
return {};
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// SubOp
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
OpFoldResult SubOp::fold(FoldAdaptor adaptor) {
|
|
// complex.sub(complex.add(a, b), b) -> a
|
|
if (auto add = getLhs().getDefiningOp<AddOp>())
|
|
if (getRhs() == add.getRhs())
|
|
return add.getLhs();
|
|
|
|
// complex.sub(a, complex.constant<0.0, 0.0>) -> a
|
|
if (auto constantOp = getRhs().getDefiningOp<ConstantOp>()) {
|
|
auto arrayAttr = constantOp.getValue();
|
|
if (llvm::cast<FloatAttr>(arrayAttr[0]).getValue().isZero() &&
|
|
llvm::cast<FloatAttr>(arrayAttr[1]).getValue().isZero()) {
|
|
return getLhs();
|
|
}
|
|
}
|
|
|
|
return {};
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// NegOp
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
OpFoldResult NegOp::fold(FoldAdaptor adaptor) {
|
|
// complex.neg(complex.neg(a)) -> a
|
|
if (auto negOp = getOperand().getDefiningOp<NegOp>())
|
|
return negOp.getOperand();
|
|
|
|
return {};
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// LogOp
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
OpFoldResult LogOp::fold(FoldAdaptor adaptor) {
|
|
// complex.log(complex.exp(a)) -> a
|
|
if (auto expOp = getOperand().getDefiningOp<ExpOp>())
|
|
return expOp.getOperand();
|
|
|
|
return {};
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// ExpOp
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
OpFoldResult ExpOp::fold(FoldAdaptor adaptor) {
|
|
// complex.exp(complex.log(a)) -> a
|
|
if (auto logOp = getOperand().getDefiningOp<LogOp>())
|
|
return logOp.getOperand();
|
|
|
|
return {};
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// ConjOp
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
OpFoldResult ConjOp::fold(FoldAdaptor adaptor) {
|
|
// complex.conj(complex.conj(a)) -> a
|
|
if (auto conjOp = getOperand().getDefiningOp<ConjOp>())
|
|
return conjOp.getOperand();
|
|
|
|
return {};
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// TableGen'd op method definitions
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#define GET_OP_CLASSES
|
|
#include "mlir/Dialect/Complex/IR/ComplexOps.cpp.inc"
|