Skip to content

Conversation

eugeneepshteyn
Copy link
Contributor

@eugeneepshteyn eugeneepshteyn commented Jul 30, 2025

New implementation of MayNeedCopy() is used to consolidate copy-in/copy-out checks.

IsAssumedShape() and IsAssumedRank() were simplified and are both now in Fortran::semantics workspace.

preparePresentUserCallActualArgument() in lowering was modified to use MayNeedCopyInOut()

Fixes #138471

Copy link

github-actions bot commented Jul 30, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Contributor

@klausler klausler left a comment

Choose a reason for hiding this comment

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

It might be cleaner to do the analysis at the time that the ProcedureRef is created by expression semantics rather than patching the IR later in lowering. It could then be used in Semantics/check-call.cpp for all of the errors and warnings that depend on copy-in/copy-out instead of the ad-hoc analysis currently done that relies on contiguity &c.

Copy link
Contributor

@klausler klausler left a comment

Choose a reason for hiding this comment

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

Really looking good now!

@@ -110,6 +110,7 @@ class ActualArgument {

std::optional<DynamicType> GetType() const;
int Rank() const;
bool IsArray() const;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is IsArray() redundant with Rank() > 0? If not, how do they differ -- assumed-rank? (In the terminology of the Fortran standard, "assumed-rank" dummy arguments are actually not arrays.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the terminology of the Fortran standard, "assumed-rank" dummy arguments are actually not arrays.

To avoid confusion, I'll move the checks elsewhere.

@@ -248,6 +254,7 @@ struct DummyDataObject {
bool CanBePassedViaImplicitInterface(std::string *whyNot = nullptr) const;
bool IsPassedByDescriptor(bool isBindC) const;
llvm::raw_ostream &Dump(llvm::raw_ostream &) const;
bool IsArray() const;
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above, is this redundant with Rank() > 0?

(dummyTreatAsArray && !dummyIsPolymorphic) || dummyIsVoidStar ||
dummyObj_.attrs.test(
characteristics::DummyDataObject::Attr::Contiguous)};
if (!actualTreatAsContiguous && dummyNeedsContiguity) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Last four lines could be a single return statement.

const characteristics::DummyDataObject &dummyObj_;
};

static bool MayNeedCopyIn(FoldingContext &fc, const ActualArgument &actual,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you combine this function with the next one? It might be clearer and more maintainable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, simplified it further

Copy link
Contributor

@klausler klausler left a comment

Choose a reason for hiding this comment

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

Looks great; thank you.

// non-contiguous.
// Copy-out: vector subscripts could refer to duplicate elements, can't
// copy out.
return forCopyOut ? !HasVectorSubscript(*actual) : true;
Copy link
Contributor

Choose a reason for hiding this comment

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

!(forCopyOut && HasVectorSubscript(...)) would be clearer, I think.

@eugeneepshteyn eugeneepshteyn merged commit 4b6a4aa into llvm:main Aug 26, 2025
9 checks passed
@eugeneepshteyn eugeneepshteyn deleted the copy-inout-review branch August 26, 2025 22:40
@tblah tblah added this to the LLVM 21.x Release milestone Aug 28, 2025
@github-project-automation github-project-automation bot moved this to Needs Triage in LLVM Release Status Aug 28, 2025
@tblah tblah removed this from the LLVM 21.x Release milestone Aug 28, 2025
tblah pushed a commit to tblah/llvm-project that referenced this pull request Aug 28, 2025
Backport of llvm#151408

New implementation of `MayNeedCopy()` is used to consolidate
copy-in/copy-out checks.

`IsAssumedShape()` and `IsAssumedRank()` were simplified and are both
now in `Fortran::semantics` workspace.

`preparePresentUserCallActualArgument()` in lowering was modified to use
`MayNeedCopyInOut()`

Fixes llvm#138471

Conflicts in backport were trivial. Remove modifications of new code
not present in 21.x branch:
	flang/lib/Semantics/check-omp-structure.cpp
	flang/lib/Semantics/resolve-directives.cpp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Needs Triage
Development

Successfully merging this pull request may close these issues.

[flang] bug passing non-contiguous array to MPI procedure
4 participants