diff --git a/flang/lib/Lower/ConvertExprToHLFIR.cpp b/flang/lib/Lower/ConvertExprToHLFIR.cpp --- a/flang/lib/Lower/ConvertExprToHLFIR.cpp +++ b/flang/lib/Lower/ConvertExprToHLFIR.cpp @@ -20,6 +20,7 @@ #include "flang/Lower/IntrinsicCall.h" #include "flang/Lower/StatementContext.h" #include "flang/Lower/SymbolMap.h" +#include "flang/Optimizer/Builder/Complex.h" #include "flang/Optimizer/Builder/Runtime/Character.h" #include "flang/Optimizer/Builder/Todo.h" #include "flang/Optimizer/HLFIR/HLFIROps.h" @@ -487,6 +488,19 @@ } }; +template +struct BinaryOp> { + using Op = Fortran::evaluate::ComplexConstructor; + static hlfir::EntityWithAttributes gen(mlir::Location loc, + fir::FirOpBuilder &builder, + const Op &op, hlfir::Entity lhs, + hlfir::Entity rhs) { + mlir::Value res = + fir::factory::Complex{builder, loc}.createComplex(KIND, lhs, rhs); + return hlfir::EntityWithAttributes{res}; + } +}; + /// Lower Expr to HLFIR. class HlfirBuilder { public: diff --git a/flang/test/Lower/HLFIR/binary-ops.f90 b/flang/test/Lower/HLFIR/binary-ops.f90 --- a/flang/test/Lower/HLFIR/binary-ops.f90 +++ b/flang/test/Lower/HLFIR/binary-ops.f90 @@ -323,3 +323,30 @@ end subroutine ! CHECK-LABEL: func.func @_QPlogical_neqv( ! CHECK: %[[VAL_10:.*]] = arith.cmpi ne + +subroutine cmplx_ctor(z, x, y) + complex :: z + real :: x, y + z = cmplx(x, y) +end subroutine +! CHECK-LABEL: func.func @_QPcmplx_ctor( +! CHECK: %[[VAL_3:.*]]:2 = hlfir.declare %{{.*}}x"} : (!fir.ref) -> (!fir.ref, !fir.ref) +! CHECK: %[[VAL_4:.*]]:2 = hlfir.declare %{{.*}}y"} : (!fir.ref) -> (!fir.ref, !fir.ref) +! CHECK: %[[VAL_6:.*]] = fir.load %[[VAL_3]]#0 : !fir.ref +! CHECK: %[[VAL_7:.*]] = fir.load %[[VAL_4]]#0 : !fir.ref +! CHECK: %[[VAL_8:.*]] = fir.undefined !fir.complex<4> +! CHECK: %[[VAL_9:.*]] = fir.insert_value %[[VAL_8]], %[[VAL_6]], [0 : index] : (!fir.complex<4>, f32) -> !fir.complex<4> +! CHECK: %[[VAL_10:.*]] = fir.insert_value %[[VAL_9]], %[[VAL_7]], [1 : index] : (!fir.complex<4>, f32) -> !fir.complex<4> + +subroutine cmplx_ctor_2(z, x) + complex(8) :: z + real(8) :: x + z = cmplx(x, 1._8, kind=8) +end subroutine +! CHECK-LABEL: func.func @_QPcmplx_ctor_2( +! CHECK: %[[VAL_2:.*]]:2 = hlfir.declare %{{.*}}x"} : (!fir.ref) -> (!fir.ref, !fir.ref) +! CHECK: %[[VAL_4:.*]] = fir.load %[[VAL_2]]#0 : !fir.ref +! CHECK: %[[VAL_5:.*]] = arith.constant 1.000000e+00 : f64 +! CHECK: %[[VAL_6:.*]] = fir.undefined !fir.complex<8> +! CHECK: %[[VAL_7:.*]] = fir.insert_value %[[VAL_6]], %[[VAL_4]], [0 : index] : (!fir.complex<8>, f64) -> !fir.complex<8> +! CHECK: %[[VAL_8:.*]] = fir.insert_value %[[VAL_7]], %[[VAL_5]], [1 : index] : (!fir.complex<8>, f64) -> !fir.complex<8>