From e9d2f173913da17f4e63b68fcee6d7f30d9e2a56 Mon Sep 17 00:00:00 2001 From: Valentin Clement Date: Wed, 23 Feb 2022 18:01:58 +0100 Subject: [PATCH] [flang] Lower complex constant 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 Co-authored-by: Eric Schweitz Co-authored-by: Jean Perier --- flang/lib/Lower/ConvertExpr.cpp | 14 ++++++++++++-- flang/test/Lower/assignment.f90 | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/flang/lib/Lower/ConvertExpr.cpp b/flang/lib/Lower/ConvertExpr.cpp index 013adb7..0a939db 100644 --- a/flang/lib/Lower/ConvertExpr.cpp +++ b/flang/lib/Lower/ConvertExpr.cpp @@ -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 ExtValue genval(const Fortran::evaluate::ComplexConstructor &op) { - TODO(getLoc(), "genval ComplexConstructor"); + mlir::Value realPartValue = genunbox(op.left()); + return fir::factory::Complex{builder, getLoc()}.createComplex( + KIND, realPartValue, genunbox(op.right())); } template @@ -381,7 +384,14 @@ public: return genRealConstant(builder.getContext(), floatVal); } } else if constexpr (TC == Fortran::common::TypeCategory::Complex) { - TODO(getLoc(), "genval complex constant"); + using TR = + Fortran::evaluate::Type; + Fortran::evaluate::ComplexConstructor ctor( + Fortran::evaluate::Expr{ + Fortran::evaluate::Constant{value.REAL()}}, + Fortran::evaluate::Expr{ + Fortran::evaluate::Constant{value.AIMAG()}}); + return genunbox(ctor); } else /*constexpr*/ { llvm_unreachable("unhandled constant"); } diff --git a/flang/test/Lower/assignment.f90 b/flang/test/Lower/assignment.f90 index 26aa336..f2f81c3 100644 --- a/flang/test/Lower/assignment.f90 +++ b/flang/test/Lower/assignment.f90 @@ -284,3 +284,17 @@ end ! CHECK: fir.store %[[C10]] to %[[D]] : !fir.ref ! CHECK: %[[C16:.*]] = arith.constant 1.600000e+01 : f128 ! CHECK: fir.store %[[C16]] to %[[E]] : !fir.ref + +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> -- 2.7.4