1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
//===- 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/Arith/IR/Arith.h"
#include "mlir/Dialect/Complex/IR/Complex.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/Matchers.h"
#include "mlir/IR/PatternMatch.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();
}
//===----------------------------------------------------------------------===//
// BitcastOp
//===----------------------------------------------------------------------===//
OpFoldResult BitcastOp::fold(FoldAdaptor bitcast) {
if (getOperand().getType() == getType())
return getOperand();
return {};
}
LogicalResult BitcastOp::verify() {
auto operandType = getOperand().getType();
auto resultType = getType();
// We allow this to be legal as it can be folded away.
if (operandType == resultType)
return success();
if (!operandType.isIntOrFloat() && !isa<ComplexType>(operandType)) {
return emitOpError("operand must be int/float/complex");
}
if (!resultType.isIntOrFloat() && !isa<ComplexType>(resultType)) {
return emitOpError("result must be int/float/complex");
}
if (isa<ComplexType>(operandType) == isa<ComplexType>(resultType)) {
return emitOpError("requires input or output is a complex type");
}
if (isa<ComplexType>(resultType))
std::swap(operandType, resultType);
int32_t operandBitwidth = dyn_cast<ComplexType>(operandType)
.getElementType()
.getIntOrFloatBitWidth() *
2;
int32_t resultBitwidth = resultType.getIntOrFloatBitWidth();
if (operandBitwidth != resultBitwidth) {
return emitOpError("casting bitwidths do not match");
}
return success();
}
struct MergeComplexBitcast final : OpRewritePattern<BitcastOp> {
using OpRewritePattern<BitcastOp>::OpRewritePattern;
LogicalResult matchAndRewrite(BitcastOp op,
PatternRewriter &rewriter) const override {
if (auto defining = op.getOperand().getDefiningOp<BitcastOp>()) {
rewriter.replaceOpWithNewOp<BitcastOp>(op, op.getType(),
defining.getOperand());
return success();
}
if (auto defining = op.getOperand().getDefiningOp<arith::BitcastOp>()) {
rewriter.replaceOpWithNewOp<BitcastOp>(op, op.getType(),
defining.getOperand());
return success();
}
return failure();
}
};
struct MergeArithBitcast final : OpRewritePattern<arith::BitcastOp> {
using OpRewritePattern<arith::BitcastOp>::OpRewritePattern;
LogicalResult matchAndRewrite(arith::BitcastOp op,
PatternRewriter &rewriter) const override {
if (auto defining = op.getOperand().getDefiningOp<complex::BitcastOp>()) {
rewriter.replaceOpWithNewOp<complex::BitcastOp>(op, op.getType(),
defining.getOperand());
return success();
}
return failure();
}
};
struct ArithBitcast final : OpRewritePattern<BitcastOp> {
using OpRewritePattern<complex::BitcastOp>::OpRewritePattern;
LogicalResult matchAndRewrite(BitcastOp op,
PatternRewriter &rewriter) const override {
if (isa<ComplexType>(op.getType()) ||
isa<ComplexType>(op.getOperand().getType()))
return failure();
rewriter.replaceOpWithNewOp<arith::BitcastOp>(op, op.getType(),
op.getOperand());
return success();
}
};
void BitcastOp::getCanonicalizationPatterns(RewritePatternSet &results,
MLIRContext *context) {
results.add<ArithBitcast, MergeComplexBitcast, MergeArithBitcast>(context);
}
//===----------------------------------------------------------------------===//
// 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 =
llvm::dyn_cast_if_present<ArrayAttr>(adaptor.getComplex());
if (arrayAttr && arrayAttr.size() == 2)
return arrayAttr[1];
if (auto createOp = getOperand().getDefiningOp<CreateOp>())
return createOp.getOperand(1);
return {};
}
namespace {
template <typename OpKind, int ComponentIndex>
struct FoldComponentNeg final : OpRewritePattern<OpKind> {
using OpRewritePattern<OpKind>::OpRewritePattern;
LogicalResult matchAndRewrite(OpKind op,
PatternRewriter &rewriter) const override {
auto negOp = op.getOperand().template getDefiningOp<NegOp>();
if (!negOp)
return failure();
auto createOp = negOp.getComplex().template getDefiningOp<CreateOp>();
if (!createOp)
return failure();
Type elementType = createOp.getType().getElementType();
assert(isa<FloatType>(elementType));
rewriter.replaceOpWithNewOp<arith::NegFOp>(
op, elementType, createOp.getOperand(ComponentIndex));
return success();
}
};
} // namespace
void ImOp::getCanonicalizationPatterns(RewritePatternSet &results,
MLIRContext *context) {
results.add<FoldComponentNeg<ImOp, 1>>(context);
}
//===----------------------------------------------------------------------===//
// ReOp
//===----------------------------------------------------------------------===//
OpFoldResult ReOp::fold(FoldAdaptor adaptor) {
ArrayAttr arrayAttr =
llvm::dyn_cast_if_present<ArrayAttr>(adaptor.getComplex());
if (arrayAttr && arrayAttr.size() == 2)
return arrayAttr[0];
if (auto createOp = getOperand().getDefiningOp<CreateOp>())
return createOp.getOperand(0);
return {};
}
void ReOp::getCanonicalizationPatterns(RewritePatternSet &results,
MLIRContext *context) {
results.add<FoldComponentNeg<ReOp, 0>>(context);
}
//===----------------------------------------------------------------------===//
// 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 {};
}
//===----------------------------------------------------------------------===//
// MulOp
//===----------------------------------------------------------------------===//
OpFoldResult MulOp::fold(FoldAdaptor adaptor) {
auto constant = getRhs().getDefiningOp<ConstantOp>();
if (!constant)
return {};
ArrayAttr arrayAttr = constant.getValue();
APFloat real = cast<FloatAttr>(arrayAttr[0]).getValue();
APFloat imag = cast<FloatAttr>(arrayAttr[1]).getValue();
if (!imag.isZero())
return {};
// complex.mul(a, complex.constant<1.0, 0.0>) -> a
if (real == APFloat(real.getSemantics(), 1))
return getLhs();
return {};
}
//===----------------------------------------------------------------------===//
// TableGen'd op method definitions
//===----------------------------------------------------------------------===//
#define GET_OP_CLASSES
#include "mlir/Dialect/Complex/IR/ComplexOps.cpp.inc"
|