Skip to content

Fix a bug in returning reshape() expressions #27403

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

bradcray
Copy link
Member

@bradcray bradcray commented Jun 17, 2025

One of the follow-ups I had lined up for the new aliasing reshape() capability was to determine why one of the four cases in returnReshape.chpl wasn't working even though the others were. Specifically, this call:

proc bar() {
  return reshape(A, {1..2, 1..2});
}

was getting a lifetime checking error about returning a scoped variable, when other similar calls like:

proc foo() {
  return reshape(A, 1..2, 1..2);
}

were not.

Digging into this a bit more, I'm still not entirely certain why the two cases were resulting in different behavior, though I did find that the foo() routine should have been getting a similar error, in that capturing its result into a variable and modifying it:

var B = foo();
B[1,1] = 5;

was resulting in a modification to 'A' as well, even though returning an array and storing it in a variable should result in a deep copy.

Once I realized this, it didn't take long to determine that this is another place in the compiler where the current logic determines the existence of an array view by virtue of the expression's type. Updating it to also look for the new FLAG_IS_ARRAY_VIEW flag fixed the aliasing issue while also resolving the original error about returning a scoped variable.

To lock this fix in, I updated the test to make it check that none of the modifications to the returned arrays resulted in a modification to the original.

While here, I also looked for other calls to isAliasingArrayType() that didn't have a similar check for the new flag and added the check, hoping to head off additional bugs down the road.

One of the follow-ups I had lined up for the new aliasing reshape()
capability was to determine why one of the four cases in
returnReshape.chpl wasn't working even though the others were.
Specifically, this call:

```chapel
proc bar() {
  return reshape(A, {1..2, 1..2});
}
```

was getting a lifetime checking error about returning a scoped
variable, when other similar calls like:

```chapel
proc foo() {
  return reshape(A, 1..2, 1..2);
}
```

were not.

Digging into this a bit more, I'm still not entirely certain why the
two cases were resulting in different behavior, though I did find that
the foo() routine should have been getting a similar error, in that
capturing its result into a variable and modifying it:

```chapel
var B = foo();
B[1,1] = 5;
```

was resulting in a modification to 'A' as well, even though returning
an array and storing it in a variable should result in a deep copy.

Once I realized this, it didn't take long to determine that this is
another place in the compiler where the current logic determines the
existence of an array view by virtue of the expression's type.
Updating it to look for the new FLAG_IS_ARRAY_VIEW flag fixed the
aliasing issue while also resolving the original error about returning
a scoped variable.

While here, I updated the test to make it check that none of the
modifications to the returned arrays resulted in a modification to the
original.

---
Signed-off-by: Brad Chamberlain <bradcray@users.noreply.github.com>
---
Signed-off-by: Brad Chamberlain <bradcray@users.noreply.github.com>
@bradcray bradcray marked this pull request as draft June 17, 2025 01:10
@bradcray bradcray marked this pull request as draft June 17, 2025 01:10
---
Signed-off-by: Brad Chamberlain <bradcray@users.noreply.github.com>
@bradcray
Copy link
Member Author

@dlongnecke-cray : Here's another follow on from the reshape PR that I regret not having the time to dig into pre-release (as it revealed a bug in the 2.5 implementation). Would you be willing to review?

@bradcray bradcray marked this pull request as ready for review June 17, 2025 02:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant