let results = (outs AnyFloat:$imaginary);
let assemblyFormat = "$complex attr-dict `:` type($complex)";
+ let hasFolder = 1;
}
//===----------------------------------------------------------------------===//
let results = (outs AnyFloat:$real);
let assemblyFormat = "$complex attr-dict `:` type($complex)";
+ let hasFolder = 1;
}
#define GET_OP_CLASSES
#include "mlir/Dialect/Complex/IR/ComplexOps.cpp.inc"
+
+OpFoldResult ReOp::fold(ArrayRef<Attribute> operands) {
+ ArrayAttr arrayAttr = operands[0].dyn_cast<ArrayAttr>();
+ return arrayAttr[0];
+}
+
+OpFoldResult ImOp::fold(ArrayRef<Attribute> operands) {
+ ArrayAttr arrayAttr = operands[0].dyn_cast<ArrayAttr>();
+ return arrayAttr[1];
+}
--- /dev/null
+// 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
+}