Skip to content

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

Merged
merged 5 commits into from
Jun 13, 2023

Conversation

Thirumalai-Shaktivel
Copy link
Collaborator

@Thirumalai-Shaktivel Thirumalai-Shaktivel commented Jun 12, 2023

Related to #703

@Thirumalai-Shaktivel
Copy link
Collaborator Author

Sometimes I get:

python(12438,0x1e33a1e00) malloc: Heap corruption detected, free list is damaged at 0x600000e7cb00
*** Incorrect guard value: 4634274385308418048
python(12438,0x1e33a1e00) malloc: *** set a breakpoint in malloc_error_break to debug

we need to fix this!

i: i32
for i in range(n):
x[i] *= 5.0
return x
Copy link
Collaborator Author

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.

Copy link
Contributor

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[:]:
Copy link
Contributor

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.

@Thirumalai-Shaktivel
Copy link
Collaborator Author

Cool, @certik This PR is ready, kindly review and provide feedback, if any.

@Thirumalai-Shaktivel Thirumalai-Shaktivel marked this pull request as ready for review June 13, 2023 08:36
@Thirumalai-Shaktivel
Copy link
Collaborator Author

This: #1891 (comment) is fixed!

Copy link
Contributor

@certik certik left a 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.

@certik certik enabled auto-merge June 13, 2023 13:59
@certik certik merged commit 549ef01 into lcompilers:main Jun 13, 2023
@Thirumalai-Shaktivel Thirumalai-Shaktivel deleted the lpython_02 branch June 14, 2023 04:33
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.

2 participants