[flang-commits] [PATCH] D103568: [flang] Fix folding of CMPLX
Peter Klausler via Phabricator via flang-commits
flang-commits at lists.llvm.org
Wed Jun 2 17:01:16 PDT 2021
klausler created this revision.
klausler added a reviewer: jeanPerier.
klausler added a project: Flang.
Herald added a subscriber: jdoerfert.
klausler requested review of this revision.
The code for folding calls to the intrinsic function CMPLX was
incorrectly dependent on the number of arguments to distinguish its
two cases (conversion from one kind of complex to another, and
composition of a complex value from real & imaginary parts).
This was wrong since the optional KIND= argument has already been
taken into account by intrinsic processing; instead, the type of
the first argument should decide the issue.
https://reviews.llvm.org/D103568
Files:
flang/lib/Evaluate/fold-complex.cpp
Index: flang/lib/Evaluate/fold-complex.cpp
===================================================================
--- flang/lib/Evaluate/fold-complex.cpp
+++ flang/lib/Evaluate/fold-complex.cpp
@@ -34,25 +34,23 @@
return FoldElementalIntrinsic<T, T>(
context, std::move(funcRef), &Scalar<T>::CONJG);
} else if (name == "cmplx") {
- using Part = typename T::Part;
- if (args.size() == 2) { // CMPLX(X, [KIND])
+ if (args.size() > 0 && args[0].has_value()) {
if (auto *x{UnwrapExpr<Expr<SomeComplex>>(args[0])}) {
+ // CMPLX(X [, KIND]) with complex X
return Fold(context, ConvertToType<T>(std::move(*x)));
+ } else {
+ // CMPLX(X [, Y [, KIND]]) with non-complex X
+ using Part = typename T::Part;
+ Expr<SomeType> re{std::move(*args[0].value().UnwrapExpr())};
+ Expr<SomeType> im{args.size() >= 2 && args[1].has_value()
+ ? std::move(*args[1]->UnwrapExpr())
+ : AsGenericExpr(Constant<Part>{Scalar<Part>{}})};
+ return Fold(context,
+ Expr<T>{
+ ComplexConstructor<KIND>{ToReal<KIND>(context, std::move(re)),
+ ToReal<KIND>(context, std::move(im))}});
}
- Expr<SomeType> re{std::move(*args[0].value().UnwrapExpr())};
- Expr<SomeType> im{AsGenericExpr(Constant<Part>{Scalar<Part>{}})};
- return Fold(context,
- Expr<T>{ComplexConstructor<KIND>{ToReal<KIND>(context, std::move(re)),
- ToReal<KIND>(context, std::move(im))}});
}
- // CMPLX(X, [Y, KIND])
- CHECK(args.size() == 3);
- Expr<SomeType> re{std::move(*args[0].value().UnwrapExpr())};
- Expr<SomeType> im{args[1] ? std::move(*args[1].value().UnwrapExpr())
- : AsGenericExpr(Constant<Part>{Scalar<Part>{}})};
- return Fold(context,
- Expr<T>{ComplexConstructor<KIND>{ToReal<KIND>(context, std::move(re)),
- ToReal<KIND>(context, std::move(im))}});
} else if (name == "merge") {
return FoldMerge<T>(context, std::move(funcRef));
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D103568.349417.patch
Type: text/x-patch
Size: 2068 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20210603/47e4db71/attachment-0001.bin>
More information about the flang-commits
mailing list