[mlir] Add folder for complex.ReOp and complex.ImOp.

Now that complex constants are supported, we can also fold.

Differential Revision: https://reviews.llvm.org/D102616
This commit is contained in:
Adrian Kuegel 2021-05-18 10:40:49 +02:00
parent 092a3ce569
commit fa765a0944
3 changed files with 37 additions and 0 deletions

View File

@ -144,6 +144,7 @@ def ImOp : Complex_Op<"im",
let results = (outs AnyFloat:$imaginary);
let assemblyFormat = "$complex attr-dict `:` type($complex)";
let hasFolder = 1;
}
//===----------------------------------------------------------------------===//
@ -185,6 +186,7 @@ def ReOp : Complex_Op<"re",
let results = (outs AnyFloat:$real);
let assemblyFormat = "$complex attr-dict `:` type($complex)";
let hasFolder = 1;
}

View File

@ -17,3 +17,19 @@ using namespace mlir::complex;
#define GET_OP_CLASSES
#include "mlir/Dialect/Complex/IR/ComplexOps.cpp.inc"
OpFoldResult ReOp::fold(ArrayRef<Attribute> operands) {
assert(operands.size() == 1 && "unary op takes 1 operand");
ArrayAttr arrayAttr = operands[0].dyn_cast_or_null<ArrayAttr>();
if (arrayAttr && arrayAttr.size() == 2)
return arrayAttr[0];
return {};
}
OpFoldResult ImOp::fold(ArrayRef<Attribute> operands) {
assert(operands.size() == 1 && "unary op takes 1 operand");
ArrayAttr arrayAttr = operands[0].dyn_cast_or_null<ArrayAttr>();
if (arrayAttr && arrayAttr.size() == 2)
return arrayAttr[1];
return {};
}

View File

@ -0,0 +1,19 @@
// RUN: mlir-opt %s -canonicalize | FileCheck %s
// CHECK-LABEL: func @real_of_const(
func @real_of_const() -> f32 {
// CHECK: %[[CST:.*]] = constant 1.000000e+00 : f32
// CHECK-NEXT: return %[[CST]] : f32
%complex = constant [1.0 : f32, 0.0 : f32] : complex<f32>
%1 = complex.re %complex : complex<f32>
return %1 : f32
}
// CHECK-LABEL: func @imag_of_const(
func @imag_of_const() -> f32 {
// CHECK: %[[CST:.*]] = constant 0.000000e+00 : f32
// CHECK-NEXT: return %[[CST]] : f32
%complex = constant [1.0 : f32, 0.0 : f32] : complex<f32>
%1 = complex.im %complex : complex<f32>
return %1 : f32
}