Skip to content

Commit a50cec7

Browse files
committed
[flang] Fix CMPLX folding with complex arguments
CMPLX folding was expecting only one arguments in case X argument is complex. This is wrong since there is also the optional KIND argument. Reviewed By: schweitz Differential Revision: https://reviews.llvm.org/D84936
1 parent 0bd918c commit a50cec7

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

flang/lib/Evaluate/fold-complex.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Expr<Type<TypeCategory::Complex, KIND>> FoldIntrinsicFunction(
3636
context, std::move(funcRef), &Scalar<T>::CONJG);
3737
} else if (name == "cmplx") {
3838
using Part = typename T::Part;
39-
if (args.size() == 1) {
39+
if (args.size() == 2) { // CMPLX(X, [KIND])
4040
if (auto *x{UnwrapExpr<Expr<SomeComplex>>(args[0])}) {
4141
return Fold(context, ConvertToType<T>(std::move(*x)));
4242
}
@@ -46,7 +46,8 @@ Expr<Type<TypeCategory::Complex, KIND>> FoldIntrinsicFunction(
4646
Expr<T>{ComplexConstructor<KIND>{ToReal<KIND>(context, std::move(re)),
4747
ToReal<KIND>(context, std::move(im))}});
4848
}
49-
CHECK(args.size() == 2 || args.size() == 3);
49+
// CMPLX(X, [Y, KIND])
50+
CHECK(args.size() == 3);
5051
Expr<SomeType> re{std::move(*args[0].value().UnwrapExpr())};
5152
Expr<SomeType> im{args[1] ? std::move(*args[1].value().UnwrapExpr())
5253
: AsGenericExpr(Constant<Part>{Scalar<Part>{}})};

flang/test/Evaluate/folding01.f90

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ module m
6363
logical, parameter :: test_ne_i1 =.NOT.(2.NE.2)
6464
logical, parameter :: test_ne_i2 = -2.NE.2
6565

66+
! Check conversions
67+
logical, parameter :: test_cmplx1 = cmplx((1._4, -1._4)).EQ.((1._4, -1._4))
68+
logical, parameter :: test_cmplx2 = cmplx((1._4, -1._4), 8).EQ.((1._8, -1._8))
69+
logical, parameter :: test_cmplx3 = cmplx(1._4, -1._4).EQ.((1._4, -1._4))
70+
logical, parameter :: test_cmplx4 = cmplx(1._4, -1._4, 8).EQ.((1._8, -1._8))
71+
logical, parameter :: test_cmplx5 = cmplx(1._4).EQ.((1._4, 0._4))
72+
logical, parameter :: test_cmplx6 = cmplx(1._4, kind=8).EQ.((1._8, 0._8))
73+
6674
! Check integer intrinsic operation folding
6775
logical, parameter :: test_unaryminus_i = (-(-1)).EQ.1
6876
logical, parameter :: test_unaryplus_i = (+1).EQ.1

0 commit comments

Comments
 (0)