[flang-commits] [flang] fa459d9 - [flang] Rewrite "1*j" to "(j)", not "j", when j is a variable

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Thu Jun 22 07:23:41 PDT 2023


Author: Peter Klausler
Date: 2023-06-22T07:05:51-07:00
New Revision: fa459d988276bb88ab577dd7a38b42a85ac8db94

URL: https://github.com/llvm/llvm-project/commit/fa459d988276bb88ab577dd7a38b42a85ac8db94
DIFF: https://github.com/llvm/llvm-project/commit/fa459d988276bb88ab577dd7a38b42a85ac8db94.diff

LOG: [flang] Rewrite "1*j" to "(j)", not "j", when j is a variable

Expression folding currently unconditionally rewrites "1*j"
to "j", which is wrong when "j" is a variable, as it transforms
an expression into a variable and can lead to incorrect associations
in contexts like an actual argument or an ASSOCIATE selector.
Transform "1*j" to a parenthesized "(j)" when "j" is a variable.
Fixes LLVM bug https://github.com/llvm/llvm-project/issues/63259.

Differential Revision: https://reviews.llvm.org/D153457

Added: 
    flang/test/Evaluate/rewrite04.f90

Modified: 
    flang/lib/Evaluate/fold-implementation.h

Removed: 
    


################################################################################
diff  --git a/flang/lib/Evaluate/fold-implementation.h b/flang/lib/Evaluate/fold-implementation.h
index ad18d7ed83b1b..50e9b3bc22cfa 100644
--- a/flang/lib/Evaluate/fold-implementation.h
+++ b/flang/lib/Evaluate/fold-implementation.h
@@ -1764,7 +1764,12 @@ Expr<T> FoldOperation(FoldingContext &context, Negate<T> &&x) {
   }
   auto &operand{x.left()};
   if (auto *nn{std::get_if<Negate<T>>(&x.left().u)}) {
-    return std::move(nn->left()); // -(-x) -> x
+    // -(-x) -> (x)
+    if (IsVariable(nn->left())) {
+      return FoldOperation(context, Parentheses<T>{std::move(nn->left())});
+    } else {
+      return std::move(nn->left());
+    }
   } else if (auto value{GetScalarConstantValue<T>(operand)}) {
     if constexpr (T::category == TypeCategory::Integer) {
       auto negated{value->Negate()};
@@ -1883,9 +1888,13 @@ Expr<T> FoldOperation(FoldingContext &context, Multiply<T> &&x) {
       if (c->IsZero()) {
         return std::move(x.left());
       } else if (c->CompareSigned(Scalar<T>{1}) == Ordering::Equal) {
-        return std::move(x.right());
+        if (IsVariable(x.right())) {
+          return FoldOperation(context, Parentheses<T>{std::move(x.right())});
+        } else {
+          return std::move(x.right());
+        }
       } else if (c->CompareSigned(Scalar<T>{-1}) == Ordering::Equal) {
-        return Expr<T>{Negate<T>{std::move(x.right())}};
+        return FoldOperation(context, Negate<T>{std::move(x.right())});
       }
     }
   }

diff  --git a/flang/test/Evaluate/rewrite04.f90 b/flang/test/Evaluate/rewrite04.f90
new file mode 100644
index 0000000000000..220be291c3f2c
--- /dev/null
+++ b/flang/test/Evaluate/rewrite04.f90
@@ -0,0 +1,5 @@
+! RUN: %flang_fc1 -fdebug-unparse %s 2>&1 | FileCheck %s
+! Ensure folding of 1*j is a parenthesized (j) when j is a variable.
+call foo(1*j)
+!CHECK: CALL foo((j))
+end


        


More information about the flang-commits mailing list