Skip to content

Commit cd0be43

Browse files
[flang] Fix passing parts of array as writeable argument
Always create a boxargument if we have an array, so that we can pass the address, rather than create a temporary array (that wasn't being copied back). This allows subroutines and functions to modify parts of arrays passed into them. Fixes #1001
1 parent 410a9c2 commit cd0be43

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

flang/lib/Lower/ConvertExpr.cpp

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,21 +2073,27 @@ class ScalarExprLowering {
20732073

20742074
if (arg.passBy == PassBy::BaseAddress || arg.passBy == PassBy::BoxChar) {
20752075
auto argAddr = [&]() -> ExtValue {
2076-
// Non contiguous variable need to be copied into a contiguous temp,
2077-
// and the temp need to be copied back after the call in case it was
2078-
// modified.
2079-
if (Fortran::evaluate::IsVariable(*expr) && expr->Rank() > 0 &&
2080-
!Fortran::evaluate::IsSimplyContiguous(
2081-
*expr, converter.getFoldingContext())) {
2076+
ExtValue baseAddr;
2077+
if (Fortran::evaluate::IsVariable(*expr) && expr->Rank() > 0) {
20822078
auto box = genBoxArg(*expr);
2083-
auto temp = genTempFromMold(box, ".copyinout");
2084-
if (arg.mayBeReadByCall())
2085-
genArrayCopy(temp, box);
2086-
if (arg.mayBeModifiedByCall())
2087-
copyOutPairs.emplace_back(box, temp);
2088-
return temp;
2089-
}
2090-
auto baseAddr = genExtAddr(*expr);
2079+
if (!Fortran::evaluate::IsSimplyContiguous(
2080+
*expr, converter.getFoldingContext())) {
2081+
// Non contiguous variable need to be copied into a contiguous
2082+
// temp, and the temp need to be copied back after the call in
2083+
// case it was modified.
2084+
auto temp = genTempFromMold(box, ".copyinout");
2085+
if (arg.mayBeReadByCall())
2086+
genArrayCopy(temp, box);
2087+
if (arg.mayBeModifiedByCall())
2088+
copyOutPairs.emplace_back(box, temp);
2089+
return temp;
2090+
}
2091+
// Contiguous: just use the box we created above!
2092+
// This gets "unboxed" below, if needed.
2093+
baseAddr = box;
2094+
} else
2095+
baseAddr = genExtAddr(*expr);
2096+
20912097
// Scalar and contiguous expressions may be lowered to a fir.box,
20922098
// either to account for potential polymorphism, or because lowering
20932099
// did not account for some contiguity hints.

0 commit comments

Comments
 (0)