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

LPython generates invalid C code when deep copying user defined objects #2125

Closed
emoise-gsi opened this issue Jul 9, 2023 · 2 comments · Fixed by #2137
Closed

LPython generates invalid C code when deep copying user defined objects #2125

emoise-gsi opened this issue Jul 9, 2023 · 2 comments · Fixed by #2137
Assignees
Labels
bug Something isn't working

Comments

@emoise-gsi
Copy link

Source code:
bug.py

from lpython import packed, dataclass, i32, ccallback, CPtr, ccall

@packed
@dataclass
class inner_struct:
    inner_field: i32 = 0

@packed
@dataclass
class outer_struct:
    inner_s : inner_struct = inner_struct()

@ccall
def outer_struct_instance_from_ptr(p : CPtr) -> outer_struct:
    pass

@ccallback
def print_outer_struct(p : CPtr) -> None:
    outer_struct_instance : outer_struct = outer_struct_instance_from_ptr(p)
    inner_struct_instance : inner_struct = outer_struct_instance.inner_s
    print(inner_struct_instance.inner_field)

compiled with lpython v0.18.0:
lpython -I $CONDA_PREFIX/lib/python3.10/ bug.py --show-c > bug.c

generates bug.c

#include <inttypes.h>

#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <lfortran_intrinsics.h>

struct dimension_descriptor
{
    int32_t lower_bound, length;
};
struct __attribute__((packed)) inner_struct {
 int32_t inner_field;
};

struct __attribute__((packed)) outer_struct {
 struct inner_struct inner_s;
};


inline void struct_deepcopy_outer_struct(struct outer_struct* src, struct outer_struct* dest);
inline void struct_deepcopy_inner_struct(struct inner_struct* src, struct inner_struct* dest);


// Implementations
void outer_struct_instance_from_ptr(void* p, struct outer_struct* _lpython_return_variable);

void print_outer_struct(void* p)
{
    struct outer_struct __libasr__created__var__0__libasr_created_return_var__value;
    struct outer_struct* __libasr__created__var__0__libasr_created_return_var_ = &__libasr__created__var__0__libasr_created_return_var__value;
    struct inner_struct inner_struct_instance_value;
    struct inner_struct* inner_struct_instance = &inner_struct_instance_value;
    struct outer_struct outer_struct_instance_value;
    struct outer_struct* outer_struct_instance = &outer_struct_instance_value;
    outer_struct_instance_from_ptr(p, __libasr__created__var__0__libasr_created_return_var_);
    struct_deepcopy_outer_struct(__libasr__created__var__0__libasr_created_return_var_, outer_struct_instance);
    struct_deepcopy_inner_struct(&outer_struct_instance->inner_s, inner_struct_instance);
    printf("%d\n", inner_struct_instance->inner_field);
}

int main(int argc, char* argv[])
{
    _lpython_set_argv(argc, argv);
    return 0;
}

void struct_deepcopy_outer_struct(struct outer_struct* src, struct outer_struct* dest) {
void struct_deepcopy_inner_struct(struct inner_struct* src, struct inner_struct* dest) {
    dest->inner_field =  src->inner_field;
}

    struct_deepcopy_inner_struct(&(src->inner_s), &(dest->inner_s));;
}

The problem is in the last lines:

void struct_deepcopy_outer_struct(struct outer_struct* src, struct outer_struct* dest) {
void struct_deepcopy_inner_struct(struct inner_struct* src, struct inner_struct* dest) {
    dest->inner_field =  src->inner_field;
}

    struct_deepcopy_inner_struct(&(src->inner_s), &(dest->inner_s));;
}

I presume the intended output is supposed to be

void struct_deepcopy_outer_struct(struct outer_struct* src, struct outer_struct* dest) {
    struct_deepcopy_inner_struct(&(src->inner_s), &(dest->inner_s));;
}

void struct_deepcopy_inner_struct(struct inner_struct* src, struct inner_struct* dest) {
    dest->inner_field =  src->inner_field;
}
@rebcabin rebcabin added the bug Something isn't working label Jul 9, 2023
@rebcabin
Copy link
Contributor

rebcabin commented Jul 9, 2023

Is this a blocker? Or can you progress with a workaround?

@emoise-gsi
Copy link
Author

It's not a blocker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants