Skip to content

[flang] Fix crash in HLFIR generation #118399

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 3, 2024
Merged

Conversation

klausler
Copy link
Contributor

@klausler klausler commented Dec 2, 2024

Structure constructors with multiple components would crash when components were from parent types. The search for the right parent component must be done anew for each component.

Fixes #118270, #96994, and #105848.

@klausler klausler requested a review from jeanPerier December 2, 2024 21:49
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Dec 2, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 2, 2024

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

Author: Peter Klausler (klausler)

Changes

Structure constructors with multiple components would crash when components were from parent types. The search for the right parent component must be done anew for each component.

Fixes #118270.


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

1 Files Affected:

  • (modified) flang/lib/Lower/ConvertExprToHLFIR.cpp (+5-6)
diff --git a/flang/lib/Lower/ConvertExprToHLFIR.cpp b/flang/lib/Lower/ConvertExprToHLFIR.cpp
index e93fbc562f9b13..4ab319b016caf7 100644
--- a/flang/lib/Lower/ConvertExprToHLFIR.cpp
+++ b/flang/lib/Lower/ConvertExprToHLFIR.cpp
@@ -1696,18 +1696,17 @@ class HlfirBuilder {
     // required chains of hlfir.designate to address the parent components so
     // that the StructureConstructor can later be lowered by addressing these
     // parent components if needed. Note: the front-end orders the components in
-    // structure constructors. The code below relies on the component to appear
-    // in order.
+    // structure constructors.
     using ValueAndParent = std::tuple<const Fortran::lower::SomeExpr &,
                                       const Fortran::semantics::Symbol &,
                                       hlfir::EntityWithAttributes>;
     llvm::SmallVector<ValueAndParent> valuesAndParents;
-    Fortran::lower::ComponentReverseIterator compIterator(
-        ctor.result().derivedTypeSpec());
-    hlfir::EntityWithAttributes currentParent = varOp;
     for (const auto &value : llvm::reverse(ctor.values())) {
       const Fortran::semantics::Symbol &compSym = *value.first;
-      while (!compIterator.lookup(compSym.name())) {
+      hlfir::EntityWithAttributes currentParent = varOp;
+      for (Fortran::lower::ComponentReverseIterator compIterator(
+               ctor.result().derivedTypeSpec());
+           !compIterator.lookup(compSym.name());) {
         const auto &parentType = compIterator.advanceToParentType();
         llvm::StringRef parentName = toStringRef(parentType.name());
         auto baseRecTy = mlir::cast<fir::RecordType>(

@aradi aradi mentioned this pull request Dec 3, 2024
Copy link
Contributor

@jeanPerier jeanPerier left a comment

Choose a reason for hiding this comment

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

Thanks for the fix. I liked the "sorted after semantic analysis" invariant to avoid the quadratic aspect, and I do not really get why it does not hold here. But given most structure constructors will likely have under a dozen components, the compiler performance point is likely moot, so if you think semantic cannot easily/cheaply guarantee the ordering, I am fine with this patch.

@@ -1696,18 +1696,17 @@ class HlfirBuilder {
// required chains of hlfir.designate to address the parent components so
// that the StructureConstructor can later be lowered by addressing these
// parent components if needed. Note: the front-end orders the components in
// structure constructors. The code below relies on the component to appear
// in order.
Copy link
Contributor

Choose a reason for hiding this comment

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

Why was that assumption broken by the fact that the parent type was from a module in a different source file? It is true if the module is defined in the same source file.

Structure constructors with multiple components would crash when
components were from parent types.  The search for the right parent
component must be done anew for each component.

Fixes llvm#118270,
llvm#96994, and
llvm#105848.
@klausler klausler merged commit 2d57333 into llvm:main Dec 3, 2024
8 checks passed
@klausler klausler deleted the bug118270 branch December 3, 2024 20:11
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] Compiler crash
3 participants