[flang-commits] [PATCH] D157340: [flang] Fix folding of character array kind conversion

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Mon Aug 7 15:04:42 PDT 2023


klausler created this revision.
klausler added a reviewer: PeteSteinfeld.
klausler added a project: Flang.
Herald added a subscriber: jdoerfert.
Herald added a reviewer: sscalpone.
Herald added a project: All.
klausler requested review of this revision.

The folding of conversions of character arrays from one kind to
another was not preserving the value of the character length expression
on the intermediate array constructor, causing spurious errors   
when defining named constant arrays of one kind of character with
arrays of another kind.

Fixes llvm-test-suite/Fortran/gfortran/regression/widechar_10.f90.


https://reviews.llvm.org/D157340

Files:
  flang/lib/Evaluate/fold-implementation.h
  flang/test/Evaluate/folding31.f90


Index: flang/test/Evaluate/folding31.f90
===================================================================
--- /dev/null
+++ flang/test/Evaluate/folding31.f90
@@ -0,0 +1,6 @@
+! RUN: %python %S/test_folding.py %s %flang_fc1
+! Test folding of character array conversion.
+module m
+  character(*,4), parameter :: str4arr(1) = ['a']
+  logical, parameter :: test = str4arr(1) == 4_'a'
+end
Index: flang/lib/Evaluate/fold-implementation.h
===================================================================
--- flang/lib/Evaluate/fold-implementation.h
+++ flang/lib/Evaluate/fold-implementation.h
@@ -1349,6 +1349,7 @@
 template <typename RESULT, typename OPERAND>
 std::optional<Expr<RESULT>> MapOperation(FoldingContext &context,
     std::function<Expr<RESULT>(Expr<OPERAND> &&)> &&f, const Shape &shape,
+    [[maybe_unused]] std::optional<Expr<SubscriptInteger>> &&length,
     Expr<OPERAND> &&values) {
   ArrayConstructor<RESULT> result{values};
   if constexpr (common::HasMember<OPERAND, AllIntrinsicCategoryTypes>) {
@@ -1369,6 +1370,11 @@
       result.Push(Fold(context, f(std::move(scalar))));
     }
   }
+  if constexpr (RESULT::category == TypeCategory::Character) {
+    if (length) {
+      result.set_LEN(std::move(*length));
+    }
+  }
   return FromArrayConstructor(context, std::move(result), shape);
 }
 
@@ -1506,9 +1512,9 @@
   return FromArrayConstructor(context, std::move(result), shape);
 }
 
-template <typename DERIVED, typename RESULT, typename LEFT, typename RIGHT>
+template <typename DERIVED, typename RESULT, typename... OPD>
 std::optional<Expr<SubscriptInteger>> ComputeResultLength(
-    Operation<DERIVED, RESULT, LEFT, RIGHT> &operation) {
+    Operation<DERIVED, RESULT, OPD...> &operation) {
   if constexpr (RESULT::category == TypeCategory::Character) {
     return Expr<RESULT>{operation.derived()}.LEN();
   }
@@ -1529,7 +1535,8 @@
   if (expr.Rank() > 0) {
     if (std::optional<Shape> shape{GetShape(context, expr)}) {
       if (auto values{AsFlatArrayConstructor(expr)}) {
-        return MapOperation(context, std::move(f), *shape, std::move(*values));
+        return MapOperation(context, std::move(f), *shape,
+            ComputeResultLength(operation), std::move(*values));
       }
     }
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157340.547965.patch
Type: text/x-patch
Size: 2254 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230807/a6465403/attachment-0001.bin>


More information about the flang-commits mailing list