[flang-commits] [flang] 26ceeee - [flang] Lower complex constructor to HLFIR

Jean Perier via flang-commits flang-commits at lists.llvm.org
Fri Dec 2 05:20:49 PST 2022


Author: Jean Perier
Date: 2022-12-02T14:20:22+01:00
New Revision: 26ceeee7575f9381752a458a388c35e161f8e16e

URL: https://github.com/llvm/llvm-project/commit/26ceeee7575f9381752a458a388c35e161f8e16e
DIFF: https://github.com/llvm/llvm-project/commit/26ceeee7575f9381752a458a388c35e161f8e16e.diff

LOG: [flang] Lower complex constructor to HLFIR

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

Added: 
    

Modified: 
    flang/lib/Lower/ConvertExprToHLFIR.cpp
    flang/test/Lower/HLFIR/binary-ops.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/ConvertExprToHLFIR.cpp b/flang/lib/Lower/ConvertExprToHLFIR.cpp
index eade15ce08d1..9daec6828a7a 100644
--- 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 @@ struct BinaryOp<Fortran::evaluate::LogicalOperation<KIND>> {
   }
 };
 
+template <int KIND>
+struct BinaryOp<Fortran::evaluate::ComplexConstructor<KIND>> {
+  using Op = Fortran::evaluate::ComplexConstructor<KIND>;
+  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
index 79dbad0448ea..fb7b7648f713 100644
--- a/flang/test/Lower/HLFIR/binary-ops.f90
+++ b/flang/test/Lower/HLFIR/binary-ops.f90
@@ -323,3 +323,30 @@ subroutine logical_neqv(x, y, z)
 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<f32>) -> (!fir.ref<f32>, !fir.ref<f32>)
+! CHECK:  %[[VAL_4:.*]]:2 = hlfir.declare %{{.*}}y"} : (!fir.ref<f32>) -> (!fir.ref<f32>, !fir.ref<f32>)
+! CHECK:  %[[VAL_6:.*]] = fir.load %[[VAL_3]]#0 : !fir.ref<f32>
+! CHECK:  %[[VAL_7:.*]] = fir.load %[[VAL_4]]#0 : !fir.ref<f32>
+! 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<f64>) -> (!fir.ref<f64>, !fir.ref<f64>)
+! CHECK:  %[[VAL_4:.*]] = fir.load %[[VAL_2]]#0 : !fir.ref<f64>
+! 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>


        


More information about the flang-commits mailing list