-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
@llvm/pr-subscribers-flang-fir-hlfir Author: Peter Klausler (klausler) ChangesStructure 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:
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>(
|
There was a problem hiding this 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. |
There was a problem hiding this comment.
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.
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.