[flang-commits] [flang] d8d91b2 - [flang][hlfir] Support type descriptor for initialized character component

Jean Perier via flang-commits flang-commits at lists.llvm.org
Tue Feb 28 06:16:09 PST 2023


Author: Jean Perier
Date: 2023-02-28T15:15:46+01:00
New Revision: d8d91b2a25db8920c44a3dea36fc0eeff93bdfa0

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

LOG: [flang][hlfir] Support type descriptor for initialized character component

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.

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

Added: 
    flang/test/Lower/HLFIR/tdesc-character-comp-init.f90

Modified: 
    flang/lib/Lower/ConvertType.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/ConvertType.cpp b/flang/lib/Lower/ConvertType.cpp
index 4fab5a5a493c..d701749e7657 100644
--- a/flang/lib/Lower/ConvertType.cpp
+++ b/flang/lib/Lower/ConvertType.cpp
@@ -433,11 +433,22 @@ struct TypeBuilderImpl {
     // 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();
   }
 

diff  --git a/flang/test/Lower/HLFIR/tdesc-character-comp-init.f90 b/flang/test/Lower/HLFIR/tdesc-character-comp-init.f90
new file mode 100644
index 000000000000..1ae312e8cc1c
--- /dev/null
+++ b/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>>


        


More information about the flang-commits mailing list