[Mlir-commits] [mlir] b99f892 - [mlir] Fold complex.re(complex.create) and complex.im(complex.create)
Adrian Kuegel
llvmlistbot at llvm.org
Wed May 26 01:53:38 PDT 2021
Author: Adrian Kuegel
Date: 2021-05-26T10:53:05+02:00
New Revision: b99f892b025b553680c7e5dbcf15ab7301e3fa57
URL: https://github.com/llvm/llvm-project/commit/b99f892b025b553680c7e5dbcf15ab7301e3fa57
DIFF: https://github.com/llvm/llvm-project/commit/b99f892b025b553680c7e5dbcf15ab7301e3fa57.diff
LOG: [mlir] Fold complex.re(complex.create) and complex.im(complex.create)
This extends the folding we already have. A test needs to be adjusted.
Differential Revision: https://reviews.llvm.org/D103141
Added:
Modified:
mlir/lib/Dialect/Complex/IR/ComplexOps.cpp
mlir/test/Conversion/ComplexToLLVM/convert-to-llvm.mlir
mlir/test/Dialect/Complex/canonicalize.mlir
Removed:
################################################################################
diff --git a/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp b/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp
index cf89508c64c75..271ba66b7a487 100644
--- a/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp
+++ b/mlir/lib/Dialect/Complex/IR/ComplexOps.cpp
@@ -24,6 +24,8 @@ OpFoldResult ReOp::fold(ArrayRef<Attribute> operands) {
ArrayAttr arrayAttr = operands[0].dyn_cast_or_null<ArrayAttr>();
if (arrayAttr && arrayAttr.size() == 2)
return arrayAttr[0];
+ if (auto createOp = dyn_cast_or_null<CreateOp>(getOperand().getDefiningOp()))
+ return createOp.getOperand(0);
return {};
}
@@ -32,5 +34,7 @@ OpFoldResult ImOp::fold(ArrayRef<Attribute> operands) {
ArrayAttr arrayAttr = operands[0].dyn_cast_or_null<ArrayAttr>();
if (arrayAttr && arrayAttr.size() == 2)
return arrayAttr[1];
+ if (auto createOp = dyn_cast_or_null<CreateOp>(getOperand().getDefiningOp()))
+ return createOp.getOperand(1);
return {};
}
diff --git a/mlir/test/Conversion/ComplexToLLVM/convert-to-llvm.mlir b/mlir/test/Conversion/ComplexToLLVM/convert-to-llvm.mlir
index 6ad3595915a03..0faff5329035b 100644
--- a/mlir/test/Conversion/ComplexToLLVM/convert-to-llvm.mlir
+++ b/mlir/test/Conversion/ComplexToLLVM/convert-to-llvm.mlir
@@ -1,19 +1,24 @@
// RUN: mlir-opt %s -convert-complex-to-llvm | FileCheck %s
-// CHECK-LABEL: func @complex_numbers
-// CHECK-NEXT: %[[REAL0:.*]] = constant 1.200000e+00 : f32
-// CHECK-NEXT: %[[IMAG0:.*]] = constant 3.400000e+00 : f32
+// CHECK-LABEL: func @complex_create
+// CHECK-SAME: (%[[REAL0:.*]]: f32, %[[IMAG0:.*]]: f32)
// CHECK-NEXT: %[[CPLX0:.*]] = llvm.mlir.undef : !llvm.struct<(f32, f32)>
// CHECK-NEXT: %[[CPLX1:.*]] = llvm.insertvalue %[[REAL0]], %[[CPLX0]][0] : !llvm.struct<(f32, f32)>
// CHECK-NEXT: %[[CPLX2:.*]] = llvm.insertvalue %[[IMAG0]], %[[CPLX1]][1] : !llvm.struct<(f32, f32)>
-// CHECK-NEXT: %[[REAL1:.*]] = llvm.extractvalue %[[CPLX2:.*]][0] : !llvm.struct<(f32, f32)>
-// CHECK-NEXT: %[[IMAG1:.*]] = llvm.extractvalue %[[CPLX2:.*]][1] : !llvm.struct<(f32, f32)>
-func @complex_numbers() {
- %real0 = constant 1.2 : f32
- %imag0 = constant 3.4 : f32
- %cplx2 = complex.create %real0, %imag0 : complex<f32>
- %real1 = complex.re%cplx2 : complex<f32>
- %imag1 = complex.im %cplx2 : complex<f32>
+func @complex_create(%real: f32, %imag: f32) -> complex<f32> {
+ %cplx2 = complex.create %real, %imag : complex<f32>
+ return %cplx2 : complex<f32>
+}
+
+// CHECK-LABEL: func @complex_extract
+// CHECK-SAME: (%[[CPLX:.*]]: complex<f32>)
+// CHECK-NEXT: %[[CAST0:.*]] = llvm.mlir.cast %[[CPLX]] : complex<f32> to !llvm.struct<(f32, f32)>
+// CHECK-NEXT: %[[REAL:.*]] = llvm.extractvalue %[[CAST0]][0] : !llvm.struct<(f32, f32)>
+// CHECK-NEXT: %[[CAST1:.*]] = llvm.mlir.cast %[[CPLX]] : complex<f32> to !llvm.struct<(f32, f32)>
+// CHECK-NEXT: %[[IMAG:.*]] = llvm.extractvalue %[[CAST1]][1] : !llvm.struct<(f32, f32)>
+func @complex_extract(%cplx: complex<f32>) {
+ %real1 = complex.re %cplx : complex<f32>
+ %imag1 = complex.im %cplx : complex<f32>
return
}
diff --git a/mlir/test/Dialect/Complex/canonicalize.mlir b/mlir/test/Dialect/Complex/canonicalize.mlir
index 3ad66f908f538..0f8e5fe80bfd5 100644
--- a/mlir/test/Dialect/Complex/canonicalize.mlir
+++ b/mlir/test/Dialect/Complex/canonicalize.mlir
@@ -9,6 +9,17 @@ func @real_of_const() -> f32 {
return %1 : f32
}
+// CHECK-LABEL: func @real_of_create_op(
+func @real_of_create_op() -> f32 {
+ // CHECK: %[[CST:.*]] = constant 1.000000e+00 : f32
+ // CHECK-NEXT: return %[[CST]] : f32
+ %real = constant 1.0 : f32
+ %imag = constant 0.0 : f32
+ %complex = complex.create %real, %imag : 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
@@ -17,3 +28,14 @@ func @imag_of_const() -> f32 {
%1 = complex.im %complex : complex<f32>
return %1 : f32
}
+
+// CHECK-LABEL: func @imag_of_create_op(
+func @imag_of_create_op() -> f32 {
+ // CHECK: %[[CST:.*]] = constant 0.000000e+00 : f32
+ // CHECK-NEXT: return %[[CST]] : f32
+ %real = constant 1.0 : f32
+ %imag = constant 0.0 : f32
+ %complex = complex.create %real, %imag : complex<f32>
+ %1 = complex.im %complex : complex<f32>
+ return %1 : f32
+}
More information about the Mlir-commits
mailing list