-
Notifications
You must be signed in to change notification settings - Fork 170
Handle array as return variable using the lpython decorator #1891
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
Sometimes I get:
we need to fix this! |
i: i32 | ||
for i in range(n): | ||
x[i] *= 5.0 | ||
return x |
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.
Here is the generated C code for the above function:
void multiply(int32_t n, struct r64* x, struct r64* _lpython_return_variable)
{
int32_t __1_t;
int32_t __1_v;
int32_t i;
for (i=0; i<=n - 1; i++) {
x->data[(i - x->dims[0].lower_bound)] = x->data[(i - x->dims[0].lower_bound)]* 5.00000000000000000e+00;
}
__1_v = ((int32_t)x->dims[1-1].lower_bound);
for (__1_t=((int32_t)_lpython_return_variable->dims[1-1].lower_bound); __1_t<=((int32_t) _lpython_return_variable->dims[1-1].length + _lpython_return_variable->dims[1-1].lower_bound - 1); __1_t++) {
_lpython_return_variable->data[(__1_t - _lpython_return_variable->dims[0].lower_bound)] = x->data[(__1_v - x->dims[0].lower_bound)];
__1_v = __1_v + 1;
}
return;
}
Here, I think the second for loop iteration should access the x
variable instead of _lpython_return_variable
. I need to investigate more about this.
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.
The second loop seems to copy x
into _lpython_return_variable
, which seems correct:
_lpython_return_variable->data[(__1_t - _lpython_return_variable->dims[0].lower_bound)] = x->data[(__1_v - x->dims[0].lower_bound)];
from lpython import i32, f64, lpython | ||
|
||
@lpython | ||
def multiply(n: i32, x: f64[:]) -> f64[:]: |
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.
This needs to be changed to:
n = TypeVar("n")
def multiply(n: i32, x: f64[:]) -> f64[n]:
The return array must have a length that is determined using the function arguments.
a43eb1d
to
67b71d6
Compare
Cool, @certik This PR is ready, kindly review and provide feedback, if any. |
This: #1891 (comment) is fixed! |
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.
I think this looks good.
Related to #703