[flang-commits] [flang] ec13942 - [flang] prevent undefined behavior in character MAXLOC folding
Jean Perier via flang-commits
flang-commits at lists.llvm.org
Tue Mar 29 00:34:43 PDT 2022
Author: Jean Perier
Date: 2022-03-29T09:34:09+02:00
New Revision: ec13942e71daad88267d73ff57b854b816c2a722
URL: https://github.com/llvm/llvm-project/commit/ec13942e71daad88267d73ff57b854b816c2a722
DIFF: https://github.com/llvm/llvm-project/commit/ec13942e71daad88267d73ff57b854b816c2a722.diff
LOG: [flang] prevent undefined behavior in character MAXLOC folding
When folding MAXLOC/MINLOC, the current element being compared was moved twice
in row in case it became the new extremum. With numeric and logical types, it
made no difference (std::move is a no-op for them), but for characters
where the string storage is actually moved, it caused the new extremum to
be set to the empty string, leading to wrong results.
Note: I could have left the first std::move relating to logical Findloc, but it
brings nothing and makes the code less auditable, so I also removed it.
Differential Revision: https://reviews.llvm.org/D122590
Added:
Modified:
flang/lib/Evaluate/fold-integer.cpp
flang/test/Evaluate/fold-findloc.f90
Removed:
################################################################################
diff --git a/flang/lib/Evaluate/fold-integer.cpp b/flang/lib/Evaluate/fold-integer.cpp
index ff2504e971ff7..6cd0370e203c2 100644
--- a/flang/lib/Evaluate/fold-integer.cpp
+++ b/flang/lib/Evaluate/fold-integer.cpp
@@ -331,14 +331,12 @@ template <WhichLocation WHICH> class LocationHelper {
if constexpr (T::category == TypeCategory::Logical) {
// array(at) .EQV. value?
static_assert(WHICH == WhichLocation::Findloc);
- cmp.emplace(
- ConvertToType<LogicalResult>(Expr<T>{LogicalOperation<T::kind>{
- LogicalOperator::Eqv, Expr<T>{Constant<T>{std::move(element)}},
- Expr<T>{Constant<T>{*value}}}}));
+ cmp.emplace(ConvertToType<LogicalResult>(
+ Expr<T>{LogicalOperation<T::kind>{LogicalOperator::Eqv,
+ Expr<T>{Constant<T>{element}}, Expr<T>{Constant<T>{*value}}}}));
} else { // compare array(at) to value
- cmp.emplace(
- PackageRelation(relation, Expr<T>{Constant<T>{std::move(element)}},
- Expr<T>{Constant<T>{*value}}));
+ cmp.emplace(PackageRelation(relation, Expr<T>{Constant<T>{element}},
+ Expr<T>{Constant<T>{*value}}));
}
Expr<LogicalResult> folded{Fold(context_, std::move(*cmp))};
result = GetScalarConstantValue<LogicalResult>(folded).value().IsTrue();
diff --git a/flang/test/Evaluate/fold-findloc.f90 b/flang/test/Evaluate/fold-findloc.f90
index 399afca97de55..5e79fce0b1c46 100644
--- a/flang/test/Evaluate/fold-findloc.f90
+++ b/flang/test/Evaluate/fold-findloc.f90
@@ -60,4 +60,10 @@ module m1
logical, parameter :: test_xi3b = all(maxloc(ia3, back=.true.) == [0,0,0])
logical, parameter :: test_xi3c = all(maxloc(ia3, dim=2) == reshape([0,0,0,0],shape=[2,2]))
logical, parameter :: test_xi3d = all(maxloc(ia3, dim=2, back=.true.) == reshape([0,0,0,0],shape=[2,2]))
+
+ character(1), parameter :: a(4) = ['a', 'b', 'a', 'b']
+ logical, parameter :: test_char1 = all(maxloc(a).eq.[2])
+ logical, parameter :: test_char2 = all(minloc(a).eq.[1])
+ logical, parameter :: test_char3 = all(maxloc(a, back=.true.).eq.[4])
+ logical, parameter :: test_char4 = all(minloc(a, back=.true.).eq.[3])
end module
More information about the flang-commits
mailing list