Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flang] Fix compiler crash in semantics #79609

Merged
merged 1 commit into from
Jan 29, 2024
Merged

Conversation

klausler
Copy link
Contributor

The runtime type information table generator couldn't handle a null pointer returned correctly for a original (not instantiated) derived type with kind parameters.

Fixes #79590.

The runtime type information table generator couldn't
handle a null pointer returned correctly for a original
(not instantiated) derived type with kind parameters.

Fixes llvm#79590.
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels Jan 26, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Jan 26, 2024

@llvm/pr-subscribers-flang-semantics

Author: Peter Klausler (klausler)

Changes

The runtime type information table generator couldn't handle a null pointer returned correctly for a original (not instantiated) derived type with kind parameters.

Fixes #79590.


Full diff: https://github.com/llvm/llvm-project/pull/79609.diff

2 Files Affected:

  • (modified) flang/lib/Semantics/runtime-type-info.cpp (+17-14)
  • (added) flang/test/Semantics/typeinfo08.f90 (+26)
diff --git a/flang/lib/Semantics/runtime-type-info.cpp b/flang/lib/Semantics/runtime-type-info.cpp
index 8ccb98663f885ab..de71083a21ff503 100644
--- a/flang/lib/Semantics/runtime-type-info.cpp
+++ b/flang/lib/Semantics/runtime-type-info.cpp
@@ -788,20 +788,23 @@ evaluate::StructureConstructor RuntimeTableBuilder::DescribeComponent(
     const DerivedTypeSpec &spec{dyType.GetDerivedTypeSpec()};
     Scope *derivedScope{const_cast<Scope *>(
         spec.scope() ? spec.scope() : spec.typeSymbol().scope())};
-    const Symbol *derivedDescription{DescribeType(DEREF(derivedScope))};
-    AddValue(values, componentSchema_, "derived"s,
-        evaluate::AsGenericExpr(evaluate::Expr<evaluate::SomeDerived>{
-            evaluate::Designator<evaluate::SomeDerived>{
-                DEREF(derivedDescription)}}));
-    // Package values of LEN parameters, if any
-    if (const SymbolVector * specParams{GetTypeParameters(spec.typeSymbol())}) {
-      for (SymbolRef ref : *specParams) {
-        const auto &tpd{ref->get<TypeParamDetails>()};
-        if (tpd.attr() == common::TypeParamAttr::Len) {
-          if (const ParamValue * paramValue{spec.FindParameter(ref->name())}) {
-            lenParams.emplace_back(GetValue(*paramValue, parameters));
-          } else {
-            lenParams.emplace_back(GetValue(tpd.init(), parameters));
+    if (const Symbol * derivedDescription{DescribeType(DEREF(derivedScope))}) {
+      AddValue(values, componentSchema_, "derived"s,
+          evaluate::AsGenericExpr(evaluate::Expr<evaluate::SomeDerived>{
+              evaluate::Designator<evaluate::SomeDerived>{
+                  DEREF(derivedDescription)}}));
+      // Package values of LEN parameters, if any
+      if (const SymbolVector *
+          specParams{GetTypeParameters(spec.typeSymbol())}) {
+        for (SymbolRef ref : *specParams) {
+          const auto &tpd{ref->get<TypeParamDetails>()};
+          if (tpd.attr() == common::TypeParamAttr::Len) {
+            if (const ParamValue *
+                paramValue{spec.FindParameter(ref->name())}) {
+              lenParams.emplace_back(GetValue(*paramValue, parameters));
+            } else {
+              lenParams.emplace_back(GetValue(tpd.init(), parameters));
+            }
           }
         }
       }
diff --git a/flang/test/Semantics/typeinfo08.f90 b/flang/test/Semantics/typeinfo08.f90
new file mode 100644
index 000000000000000..7509070fa3eec5c
--- /dev/null
+++ b/flang/test/Semantics/typeinfo08.f90
@@ -0,0 +1,26 @@
+!RUN: bbc --dump-symbols %s | FileCheck %s
+!RUN: %flang_fc1 -fdebug-dump-symbols %s | FileCheck %s
+! Regression test for a crash (llvm-project/issues/79590)
+module m
+  type t(k1)
+     integer,kind :: k1
+  end type
+  type s(l1)
+     integer,len :: l1
+     type(t(3)) :: t1
+  end type
+end module
+
+!CHECK: Module scope: m size=0 alignment=1 sourceRange=113 bytes
+!CHECK: .c.s, SAVE, TARGET (CompilerCreated, ReadOnly): ObjectEntity type: TYPE(component) shape: 0_8:0_8 init:[component::component(name=.n.t1,genre=1_1,category=5_1,kind=0_1,rank=0_1,offset=0_8,characterlen=value(genre=1_1,value=0_8),lenvalue=NULL(),bounds=NULL(),initialization=NULL())]
+!CHECK: .dt.s, SAVE, TARGET (CompilerCreated, ReadOnly): ObjectEntity type: TYPE(derivedtype) init:derivedtype(binding=NULL(),name=.n.s,sizeinbytes=0_8,uninstantiated=NULL(),kindparameter=NULL(),lenparameterkind=.lpk.s,component=.c.s,procptr=NULL(),special=NULL(),specialbitset=0_4,hasparent=0_1,noinitializationneeded=1_1,nodestructionneeded=1_1,nofinalizationneeded=1_1)
+!CHECK: .lpk.s, SAVE, TARGET (CompilerCreated, ReadOnly): ObjectEntity type: INTEGER(1) shape: 0_8:0_8 init:[INTEGER(1)::4_1]
+!CHECK: .n.s, SAVE, TARGET (CompilerCreated, ReadOnly): ObjectEntity type: CHARACTER(1_8,1) init:"s"
+!CHECK: .n.t1, SAVE, TARGET (CompilerCreated, ReadOnly): ObjectEntity type: CHARACTER(2_8,1) init:"t1"
+!CHECK: s, PUBLIC: DerivedType components: t1
+!CHECK: t, PUBLIC: DerivedType
+!CHECK: DerivedType scope: t sourceRange=27 bytes
+!CHECK: k1: TypeParam type:INTEGER(4) Kind
+!CHECK: DerivedType scope: s size=0 alignment=1 sourceRange=43 bytes
+!CHECK: l1: TypeParam type:INTEGER(4) Len
+!CHECK: t1: ObjectEntity type: TYPE(t(k1=3_4))

Copy link
Contributor

@clementval clementval left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@klausler klausler merged commit 74bebee into llvm:main Jan 29, 2024
6 checks passed
@klausler klausler deleted the bug79590 branch January 29, 2024 21:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:semantics flang Flang issues not falling into any other category
Projects
None yet
3 participants