[flang] Lower conversions to HLFIR
authorJean Perier <jperier@nvidia.com>
Fri, 2 Dec 2022 15:19:30 +0000 (16:19 +0100)
committerJean Perier <jperier@nvidia.com>
Fri, 2 Dec 2022 15:19:38 +0000 (16:19 +0100)
Differential Revision: https://reviews.llvm.org/D139196

flang/lib/Lower/ConvertExprToHLFIR.cpp
flang/test/Lower/HLFIR/conversion-ops.f90 [new file with mode: 0644]

index 5a29f9d..30b59b0 100644 (file)
@@ -592,6 +592,27 @@ struct UnaryOp<Fortran::evaluate::Parentheses<T>> {
   }
 };
 
+template <Fortran::common::TypeCategory TC1, int KIND,
+          Fortran::common::TypeCategory TC2>
+struct UnaryOp<
+    Fortran::evaluate::Convert<Fortran::evaluate::Type<TC1, KIND>, TC2>> {
+  using Op =
+      Fortran::evaluate::Convert<Fortran::evaluate::Type<TC1, KIND>, TC2>;
+  static hlfir::EntityWithAttributes gen(mlir::Location loc,
+                                         fir::FirOpBuilder &builder,
+                                         const Op &op, hlfir::Entity lhs) {
+    if constexpr (TC1 == Fortran::common::TypeCategory::Character &&
+                  TC2 == TC1) {
+      TODO(loc, "character conversion in HLFIR");
+    } else {
+      mlir::Type type = Fortran::lower::getFIRType(builder.getContext(), TC1,
+                                                   KIND, /*params=*/llvm::None);
+      mlir::Value res = builder.convertWithSemantics(loc, type, lhs);
+      return hlfir::EntityWithAttributes{res};
+    }
+  }
+};
+
 /// Lower Expr to HLFIR.
 class HlfirBuilder {
 public:
@@ -670,14 +691,6 @@ private:
     TODO(getLoc(), "lowering ArrayCtor to HLFIR");
   }
 
-  template <Fortran::common::TypeCategory TC1, int KIND,
-            Fortran::common::TypeCategory TC2>
-  hlfir::EntityWithAttributes
-  gen(const Fortran::evaluate::Convert<Fortran::evaluate::Type<TC1, KIND>, TC2>
-          &convert) {
-    TODO(getLoc(), "lowering convert to HLFIR");
-  }
-
   template <typename D, typename R, typename O>
   hlfir::EntityWithAttributes
   gen(const Fortran::evaluate::Operation<D, R, O> &op) {
diff --git a/flang/test/Lower/HLFIR/conversion-ops.f90 b/flang/test/Lower/HLFIR/conversion-ops.f90
new file mode 100644 (file)
index 0000000..230d74e
--- /dev/null
@@ -0,0 +1,69 @@
+! Test lowering of intrinsic conversions to HLFIR
+! RUN: bbc -emit-fir -hlfir -o - %s 2>&1 | FileCheck %s
+
+subroutine test
+  integer(4) :: i4
+  integer(8) :: i8
+  real(4) :: r4
+  real(8) :: r8
+  complex(4) :: z4
+  complex(8) :: z8
+
+  logical(4) :: l4
+  logical(8) :: l8
+
+  i4 = i8
+! CHECK:  fir.convert %{{.*}} : (i64) -> i32
+  i4 = r4
+! CHECK:  fir.convert %{{.*}} : (f32) -> i32
+  i4 = r8
+! CHECK:  fir.convert %{{.*}} : (f64) -> i32
+  i4 = z4
+! CHECK:  %[[VAL_23:.*]] = fir.extract_value %{{.*}}, [0 : index] : (!fir.complex<4>) -> f32
+! CHECK:  fir.convert %[[VAL_23]] : (f32) -> i32
+  i4 = z8
+! CHECK:  %[[VAL_26:.*]] = fir.extract_value %{{.*}}, [0 : index] : (!fir.complex<8>) -> f64
+! CHECK:  fir.convert %[[VAL_26]] : (f64) -> i32
+
+  r4 = i4
+! CHECK:  fir.convert %{{.*}} : (i32) -> f32
+  r4 = i8
+! CHECK:  fir.convert %{{.*}} : (i64) -> f32
+  r4 = r8
+! CHECK:  fir.convert %{{.*}} : (f64) -> f32
+  r4 = z4
+! CHECK:  fir.extract_value %{{.*}}, [0 : index] : (!fir.complex<4>) -> f32
+  r4 = z8
+! CHECK:  %[[VAL_36:.*]] = fir.load %{{.*}} : !fir.ref<!fir.complex<8>>
+! CHECK:  %[[VAL_37:.*]] = fir.extract_value %[[VAL_36]], [0 : index] : (!fir.complex<8>) -> f64
+! CHECK:  fir.convert %[[VAL_37]] : (f64) -> f32
+
+  z4 = i4
+! CHECK:  %[[VAL_40:.*]] = fir.convert %{{.*}} : (i32) -> f32
+! CHECK:  %[[VAL_41:.*]] = arith.constant 0.000000e+00 : f32
+! CHECK:  %[[VAL_42:.*]] = fir.undefined !fir.complex<4>
+! CHECK:  %[[VAL_43:.*]] = fir.insert_value %[[VAL_42]], %[[VAL_40]], [0 : index] : (!fir.complex<4>, f32) -> !fir.complex<4>
+! CHECK:  fir.insert_value %[[VAL_43]], %[[VAL_41]], [1 : index] : (!fir.complex<4>, f32) -> !fir.complex<4>
+  z4 = i8
+! CHECK:  %[[VAL_46:.*]] = fir.convert %{{.*}} : (i64) -> f32
+! CHECK:  %[[VAL_47:.*]] = arith.constant 0.000000e+00 : f32
+! CHECK:  %[[VAL_48:.*]] = fir.undefined !fir.complex<4>
+! CHECK:  %[[VAL_49:.*]] = fir.insert_value %[[VAL_48]], %[[VAL_46]], [0 : index] : (!fir.complex<4>, f32) -> !fir.complex<4>
+! CHECK:  fir.insert_value %[[VAL_49]], %[[VAL_47]], [1 : index] : (!fir.complex<4>, f32) -> !fir.complex<4>
+  z4 = r4
+! CHECK:  %[[VAL_52:.*]] = arith.constant 0.000000e+00 : f32
+! CHECK:  %[[VAL_53:.*]] = fir.undefined !fir.complex<4>
+! CHECK:  %[[VAL_54:.*]] = fir.insert_value %[[VAL_53]], %{{.*}}, [0 : index] : (!fir.complex<4>, f32) -> !fir.complex<4>
+! CHECK:  fir.insert_value %[[VAL_54]], %[[VAL_52]], [1 : index] : (!fir.complex<4>, f32) -> !fir.complex<4>
+  z4 = r8
+! CHECK:  %[[VAL_57:.*]] = fir.convert %{{.*}} : (f64) -> f32
+! CHECK:  %[[VAL_58:.*]] = arith.constant 0.000000e+00 : f32
+! CHECK:  %[[VAL_59:.*]] = fir.undefined !fir.complex<4>
+! CHECK:  %[[VAL_60:.*]] = fir.insert_value %[[VAL_59]], %[[VAL_57]], [0 : index] : (!fir.complex<4>, f32) -> !fir.complex<4>
+! CHECK:  fir.insert_value %[[VAL_60]], %[[VAL_58]], [1 : index] : (!fir.complex<4>, f32) -> !fir.complex<4>
+  z4 = z8
+! CHECK:  fir.convert %{{.*}} : (!fir.complex<8>) -> !fir.complex<4>
+
+  l4 = l8
+! CHECK:  fir.convert %{{.*}} : (!fir.logical<8>) -> !fir.logical<4>
+end subroutine