[flang] Lower complex constant
authorValentin Clement <clementval@gmail.com>
Wed, 23 Feb 2022 17:01:58 +0000 (18:01 +0100)
committerValentin Clement <clementval@gmail.com>
Wed, 23 Feb 2022 17:03:30 +0000 (18:03 +0100)
Add ability to lower complex constant.

This patch is part of the upstreaming effort from fir-dev branch.

Reviewed By: PeteSteinfeld

Differential Revision: https://reviews.llvm.org/D120402

Co-authored-by: Kiran Chandramohan <kiran.chandramohan@arm.com>
Co-authored-by: Eric Schweitz <eschweitz@nvidia.com>
Co-authored-by: Jean Perier <jperier@nvidia.com>
flang/lib/Lower/ConvertExpr.cpp
flang/test/Lower/assignment.f90

index 013adb7..0a939db 100644 (file)
@@ -19,6 +19,7 @@
 #include "flang/Lower/IntrinsicCall.h"
 #include "flang/Lower/SymbolMap.h"
 #include "flang/Lower/Todo.h"
+#include "flang/Optimizer/Builder/Complex.h"
 #include "flang/Semantics/expression.h"
 #include "flang/Semantics/symbol.h"
 #include "flang/Semantics/tools.h"
@@ -277,7 +278,9 @@ public:
 
   template <int KIND>
   ExtValue genval(const Fortran::evaluate::ComplexConstructor<KIND> &op) {
-    TODO(getLoc(), "genval ComplexConstructor");
+    mlir::Value realPartValue = genunbox(op.left());
+    return fir::factory::Complex{builder, getLoc()}.createComplex(
+        KIND, realPartValue, genunbox(op.right()));
   }
 
   template <int KIND>
@@ -381,7 +384,14 @@ public:
         return genRealConstant<KIND>(builder.getContext(), floatVal);
       }
     } else if constexpr (TC == Fortran::common::TypeCategory::Complex) {
-      TODO(getLoc(), "genval complex constant");
+      using TR =
+          Fortran::evaluate::Type<Fortran::common::TypeCategory::Real, KIND>;
+      Fortran::evaluate::ComplexConstructor<KIND> ctor(
+          Fortran::evaluate::Expr<TR>{
+              Fortran::evaluate::Constant<TR>{value.REAL()}},
+          Fortran::evaluate::Expr<TR>{
+              Fortran::evaluate::Constant<TR>{value.AIMAG()}});
+      return genunbox(ctor);
     } else /*constexpr*/ {
       llvm_unreachable("unhandled constant");
     }
index 26aa336..f2f81c3 100644 (file)
@@ -284,3 +284,17 @@ end
 ! CHECK: fir.store %[[C10]] to %[[D]] : !fir.ref<f80>
 ! CHECK: %[[C16:.*]] = arith.constant 1.600000e+01 : f128
 ! CHECK: fir.store %[[C16]] to %[[E]] : !fir.ref<f128>
+
+subroutine complex_constant()
+  complex(4) :: a
+  a = (0, 1)
+end
+
+! CHECK-LABEL: func @_QPcomplex_constant()
+! CHECK:         %[[A:.*]] = fir.alloca !fir.complex<4> {bindc_name = "a", uniq_name = "_QFcomplex_constantEa"}
+! CHECK:         %[[C0:.*]] = arith.constant 0.000000e+00 : f32
+! CHECK:         %[[C1:.*]] = arith.constant 1.000000e+00 : f32
+! CHECK:         %[[UNDEF:.*]] = fir.undefined !fir.complex<4>
+! CHECK:         %[[INS0:.*]] = fir.insert_value %[[UNDEF]], %[[C0]], [0 : index] : (!fir.complex<4>, f32) -> !fir.complex<4>
+! CHECK:         %[[INS1:.*]] = fir.insert_value %[[INS0]], %[[C1]], [1 : index] : (!fir.complex<4>, f32) -> !fir.complex<4>
+! CHECK:         fir.store %[[INS1]] to %[[A]] : !fir.ref<!fir.complex<4>>