Skip to content

Commit

Permalink
[flang] Lower conversions to HLFIR
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D139196
  • Loading branch information
jeanPerier committed Dec 2, 2022
1 parent 0037e21 commit f24466c
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 8 deletions.
29 changes: 21 additions & 8 deletions flang/lib/Lower/ConvertExprToHLFIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -670,14 +691,6 @@ class HlfirBuilder {
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) {
Expand Down
69 changes: 69 additions & 0 deletions flang/test/Lower/HLFIR/conversion-ops.f90
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f24466c

Please sign in to comment.