Skip to content
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

consider calls as complex openarray assignment to iterator params #24333

Merged
merged 2 commits into from
Oct 25, 2024

Conversation

metagn
Copy link
Collaborator

@metagn metagn commented Oct 20, 2024

fixes #13417, fixes #19703

When passing an expression to an openarray iterator parameter: If the expression is a statement list (considered "complex"), it's assigned in a non-deep-copying way to a temporary variable first, then this variable is used as a parameter. If it's not a statement list, i.e. a call or a symbol, the parameter is substituted directly with the given expression. In the case of calls, this results in the call potentially being executed more than once, or can cause redefined variables in the codegen.

To fix this, calls are also considered as "complex" assignments to openarrays, as long as the return type of the call is not openarray as the generated assignment in that case has issues/is unimplemented (caused a segfault here in datamancer).

As for why creating a temporary isn't the default only with exceptions for things like nkSym, the "non-deep-copying" way of assignment apparently still causes arrays to be copied according to a comment in the code. I'm not sure to what extent this is true: if it still happens on ARC/ORC, if it happens for every array length, or if we can fix it by passing arrays by reference. Otherwise, a more general way to assign to openarrays might be needed, but I'm not sure if the compiler can easily do this.

@metagn metagn marked this pull request as draft October 20, 2024 21:15
@metagn metagn marked this pull request as ready for review October 21, 2024 15:55
@Araq Araq merged commit d303c28 into nim-lang:devel Oct 25, 2024
19 checks passed
Copy link
Contributor

Thanks for your hard work on this PR!
The lines below are statistics of the Nim compiler built from d303c28

Hint: mm: orc; opt: speed; options: -d:release
175635 lines; 9.631s; 654.605MiB peakmem

narimiran pushed a commit that referenced this pull request Jan 14, 2025
…4333)

fixes #13417, fixes #19703

When passing an expression to an `openarray` iterator parameter: If the
expression is a statement list (considered "complex"), it's assigned in
a non-deep-copying way to a temporary variable first, then this variable
is used as a parameter. If it's not a statement list, i.e. a call or a
symbol, the parameter is substituted directly with the given expression.
In the case of calls, this results in the call potentially being
executed more than once, or can cause redefined variables in the
codegen.

To fix this, calls are also considered as "complex" assignments to
openarrays, as long as the return type of the call is not `openarray` as
the generated assignment in that case has issues/is unimplemented
(caused a segfault [here in
datamancer](https://github.com/SciNim/Datamancer/blob/47ba4d81bf240a7755b73bc48c1cec9b638d18ae/src/datamancer/dataframe.nim#L1580)).

As for why creating a temporary isn't the default only with exceptions
for things like `nkSym`, the "non-deep-copying" way of assignment
apparently still causes arrays to be copied according to a comment in
the code. I'm not sure to what extent this is true: if it still happens
on ARC/ORC, if it happens for every array length, or if we can fix it by
passing arrays by reference. Otherwise, a more general way to assign to
openarrays might be needed, but I'm not sure if the compiler can easily
do this.

(cherry picked from commit d303c28)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants