Skip to content

[flang] Do not instantiate components in initial targets as objects #75778

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

Merged
merged 1 commit into from
Dec 19, 2023

Conversation

jeanPerier
Copy link
Contributor

Lowering was instantiating component symbols (but the last) in initial target designator as if they were whole objects, leading to collisions and bugs.

Fixes #75728

Lowering was instantiating component symbols (but the last) in
initial target designator as if they were whole objects, leading to
collisions and bugs.

Fixes llvm#75728
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Dec 18, 2023
@llvmbot
Copy link
Member

llvmbot commented Dec 18, 2023

@llvm/pr-subscribers-flang-fir-hlfir

Author: None (jeanPerier)

Changes

Lowering was instantiating component symbols (but the last) in initial target designator as if they were whole objects, leading to collisions and bugs.

Fixes #75728


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

2 Files Affected:

  • (modified) flang/lib/Lower/ConvertVariable.cpp (+4)
  • (added) flang/test/Lower/HLFIR/initial-target-component.f90 (+17)
diff --git a/flang/lib/Lower/ConvertVariable.cpp b/flang/lib/Lower/ConvertVariable.cpp
index 364de33d00a627..ad44de71ee828a 100644
--- a/flang/lib/Lower/ConvertVariable.cpp
+++ b/flang/lib/Lower/ConvertVariable.cpp
@@ -238,6 +238,10 @@ mlir::Value Fortran::lower::genInitialDataTarget(
         /*nonDeferredParams=*/std::nullopt);
   // Pointer initial data target, and NULL(mold).
   for (const auto &sym : Fortran::evaluate::CollectSymbols(initialTarget)) {
+    // Derived type component symbols should not be instantiated as objects
+    // on their own.
+    if (sym->owner().IsDerivedType())
+      continue;
     // Length parameters processing will need care in global initializer
     // context.
     if (hasDerivedTypeWithLengthParameters(sym))
diff --git a/flang/test/Lower/HLFIR/initial-target-component.f90 b/flang/test/Lower/HLFIR/initial-target-component.f90
new file mode 100644
index 00000000000000..cfee3adbc70beb
--- /dev/null
+++ b/flang/test/Lower/HLFIR/initial-target-component.f90
@@ -0,0 +1,17 @@
+! Test https://github.com/llvm/llvm-project/issues/75728 fix.
+! RUN: bbc -emit-hlfir -o - -I nw %s | FileCheck %s
+
+subroutine test()
+  type t
+    complex :: z
+  end type
+  type(t), target, save :: obj
+  real, pointer :: p => obj%z%re
+end subroutine
+! CHECK-LABEL:   fir.global internal @_QFtestEp : !fir.box<!fir.ptr<f32>> {
+! CHECK-NEXT:      %[[VAL_0:.*]] = fir.address_of(@_QFtestEobj) : !fir.ref<!fir.type<_QFtestTt{z:!fir.complex<4>}>>
+! CHECK-NEXT:      %[[VAL_1:.*]]:2 = hlfir.declare %[[VAL_0]] {fortran_attrs = #fir.var_attrs<target>, uniq_name = "_QFtestEobj"} : (!fir.ref<!fir.type<_QFtestTt{z:!fir.complex<4>}>>) -> (!fir.ref<!fir.type<_QFtestTt{z:!fir.complex<4>}>>, !fir.ref<!fir.type<_QFtestTt{z:!fir.complex<4>}>>)
+! CHECK-NEXT:      %[[VAL_2:.*]] = hlfir.designate %[[VAL_1]]#0{"z"}  real : (!fir.ref<!fir.type<_QFtestTt{z:!fir.complex<4>}>>) -> !fir.ref<f32>
+! CHECK-NEXT:      %[[VAL_3:.*]] = fir.embox %[[VAL_2]] : (!fir.ref<f32>) -> !fir.box<f32>
+! CHECK-NEXT:      %[[VAL_4:.*]] = fir.rebox %[[VAL_3]] : (!fir.box<f32>) -> !fir.box<!fir.ptr<f32>>
+! CHECK-NEXT:      fir.has_value %[[VAL_4]] : !fir.box<!fir.ptr<f32>>

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!

@jeanPerier jeanPerier merged commit 41096d1 into llvm:main Dec 19, 2023
@jeanPerier jeanPerier deleted the jpr-fix-75728 branch December 19, 2023 09:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Flang] too much error messages at the compilation
3 participants