[flang-commits] [PATCH] D144960: [flang][hlfir] Support type descriptor for initialized character component

Jean Perier via Phabricator via flang-commits flang-commits at lists.llvm.org
Tue Feb 28 05:32:43 PST 2023


jeanPerier created this revision.
jeanPerier added a reviewer: clementval.
jeanPerier added a project: Flang.
Herald added subscribers: sunshaoce, mehdi_amini, jdoerfert.
Herald added a project: All.
jeanPerier requested review of this revision.

These compiler generated component descriptor include designators packaged
as CLASS(*) for simplicity. HLFIR hit an assert in an std::get trying to
recover an Expr<SomeChar> while translating the expression type.
Use the dynamic type of the CLASS(*) expr in that case to recover the
compiler length.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D144960

Files:
  flang/lib/Lower/ConvertType.cpp
  flang/test/Lower/HLFIR/tdesc-character-comp-init.f90


Index: flang/test/Lower/HLFIR/tdesc-character-comp-init.f90
===================================================================
--- /dev/null
+++ flang/test/Lower/HLFIR/tdesc-character-comp-init.f90
@@ -0,0 +1,13 @@
+! Test lowering of derived type descriptor for types with
+! a default initialized character component.
+
+! RUN: bbc -emit-fir -hlfir -o - %s | FileCheck %s
+
+subroutine test()
+  type t
+    character(5) :: character_comp = "hello"
+  end type
+  type(t) :: x
+end subroutine
+! CHECK-LABEL: fir.global {{.*}} @_QFtestE.c.t constant
+! CHECK: fir.address_of(@_QFtestE.di.t.character_comp) : !fir.ref<!fir.char<1,5>>
Index: flang/lib/Lower/ConvertType.cpp
===================================================================
--- flang/lib/Lower/ConvertType.cpp
+++ flang/lib/Lower/ConvertType.cpp
@@ -433,11 +433,22 @@
     // Do not use dynamic type length here. We would miss constant
     // lengths opportunities because dynamic type only has the length
     // if it comes from a declaration.
-    auto charExpr =
-        std::get<Fortran::evaluate::Expr<Fortran::evaluate::SomeCharacter>>(
-            expr.u);
-    if (auto constantLen = toInt64(charExpr.LEN()))
-      return *constantLen;
+    if (const auto *charExpr = std::get_if<
+            Fortran::evaluate::Expr<Fortran::evaluate::SomeCharacter>>(
+            &expr.u)) {
+      if (auto constantLen = toInt64(charExpr->LEN()))
+        return *constantLen;
+    } else if (auto dynamicType = expr.GetType()) {
+      // When generating derived type type descriptor as structure constructor,
+      // semantics wraps designators to data component initialization into
+      // CLASS(*), regardless of their actual type.
+      // GetType() will recover the actual symbol type as the dynamic type, so
+      // getCharacterLength may be reached even if expr is packaged as an
+      // Expr<SomeDerived> instead of an Expr<SomeChar>.
+      // Just use the dynamic type here again to retrieve the length.
+      if (auto constantLen = toInt64(dynamicType->GetCharLength()))
+        return *constantLen;
+    }
     return fir::SequenceType::getUnknownExtent();
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D144960.501106.patch
Type: text/x-patch
Size: 2158 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230228/ec32e649/attachment-0001.bin>


More information about the flang-commits mailing list