Skip to content

Support for pointer to structs #614

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 8 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ RUN(NAME test_unary_minus LABELS cpython llvm)
RUN(NAME test_unary_plus LABELS cpython llvm)
RUN(NAME test_issue_518 LABELS cpython llvm)
RUN(NAME structs_01 LABELS cpython llvm)
RUN(NAME structs_02 LABELS llvm)
RUN(NAME structs_03 LABELS llvm)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can figure out, structs_02.py isn't possible with CPython because ccallable isn't supported there. For struct_03.py I get the following error,

Traceback (most recent call last):
  File "/Users/czgdp1807/lpython_project/lpython/integration_tests/structs_03.py", line 8, in <module>
    def f(pa: Pointer[A]):
TypeError: 'type' object is not subscriptable

@certik Do you know how to fix the above?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CPython can't use ccallable, at least in the near future. The only way to make it work is by wrapping LPython itself into Python and calling it to take care of this. Which might not be a bad idea, but it's heavyweight and we should do that later. For now we won't test ccallable using cpython.

Regarding the above error, I think Pointer[A] does not work in CPython yet. That one we should get working.


# Just CPython
RUN(NAME test_builtin_bin LABELS cpython)
28 changes: 28 additions & 0 deletions integration_tests/structs_02.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from ltypes import i32, f32, dataclass, CPtr, Pointer, c_p_pointer, pointer

@dataclass
class A:
x: i32
y: f32

@ccallable
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@certik How will we support ccallable in CPython?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its not supported in LPython as well. What do we mean by ccallable? Anything that can be called from C code?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. LPython supports it, it works with both LLVM and C. It can't work with CPython, since we can't build the C library that expects this function. The only way to support it from CPython would be to somehow expose LPython as a Python library and use it to generate C code, and then link it, etc. Too complex, we can maybe do that later, but for now we do not support @ccallable in CPython.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get the following error,

semantic error: Decorator: ccallable is not supported
  --> integration_tests/structs_02.py:10:1 - 17:12
   |
10 |    def f(a: CPtr) -> None:
   |    ^^^^^^^^^^^^^^^^^^^^^^^...
...
   |
17 |        y = a2.y
   | ...^^^^^^^^^^^^ 


Note: if any of the above error or warning messages are not clear or are lacking
context please report it to us (we consider that a bug that needs to be fixed).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note the update - #614 (comment)

def f(a: CPtr) -> None:
x: i32
y: f32
a1: A
a2: Pointer[A]
a1 = A(3, 3.25)
a2 = pointer(a1)
print(a2, pointer(a1))
x = a2.x
y = a2.y
assert x == 3
assert y == 3.25
c_p_pointer(a, a2)
print(a, a2, pointer(a1))

def g():
b: CPtr
f(b)

g()
21 changes: 21 additions & 0 deletions integration_tests/structs_03.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from ltypes import i32, f32, dataclass, Pointer, pointer

@dataclass
class A:
x: i32
y: f32

def f(pa: Pointer[A]):
print(pa.x)
print(pa.y)

def g():
x: A
x = A(5, 5.5)
px: Pointer[A]
px = pointer(x)
px.x = 5
px.y = 5.5
f(px)

g()
94 changes: 51 additions & 43 deletions src/libasr/asr_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,49 @@ static inline ASR::ttype_t* TYPE(const ASR::asr_t *f)
return ASR::down_cast<ASR::ttype_t>(f);
}

static inline char *symbol_name(const ASR::symbol_t *f)
{
switch (f->type) {
case ASR::symbolType::Program: {
return ASR::down_cast<ASR::Program_t>(f)->m_name;
}
case ASR::symbolType::Module: {
return ASR::down_cast<ASR::Module_t>(f)->m_name;
}
case ASR::symbolType::Subroutine: {
return ASR::down_cast<ASR::Subroutine_t>(f)->m_name;
}
case ASR::symbolType::Function: {
return ASR::down_cast<ASR::Function_t>(f)->m_name;
}
case ASR::symbolType::GenericProcedure: {
return ASR::down_cast<ASR::GenericProcedure_t>(f)->m_name;
}
case ASR::symbolType::DerivedType: {
return ASR::down_cast<ASR::DerivedType_t>(f)->m_name;
}
case ASR::symbolType::Variable: {
return ASR::down_cast<ASR::Variable_t>(f)->m_name;
}
case ASR::symbolType::ExternalSymbol: {
return ASR::down_cast<ASR::ExternalSymbol_t>(f)->m_name;
}
case ASR::symbolType::ClassProcedure: {
return ASR::down_cast<ASR::ClassProcedure_t>(f)->m_name;
}
case ASR::symbolType::CustomOperator: {
return ASR::down_cast<ASR::CustomOperator_t>(f)->m_name;
}
case ASR::symbolType::AssociateBlock: {
return ASR::down_cast<ASR::AssociateBlock_t>(f)->m_name;
}
case ASR::symbolType::Block: {
return ASR::down_cast<ASR::Block_t>(f)->m_name;
}
default : throw LFortranException("Not implemented");
}
}

static inline ASR::symbol_t *symbol_get_past_external(ASR::symbol_t *f)
{
if (f->type == ASR::symbolType::ExternalSymbol) {
Expand Down Expand Up @@ -200,6 +243,14 @@ static inline std::string type_to_str_python(const ASR::ttype_t *t)
case ASR::ttypeType::CPtr: {
return "CPtr";
}
case ASR::ttypeType::Derived: {
ASR::Derived_t* d = ASR::down_cast<ASR::Derived_t>(t);
return symbol_name(d->m_derived_type);
}
case ASR::ttypeType::Pointer: {
ASR::Pointer_t* p = ASR::down_cast<ASR::Pointer_t>(t);
return "Pointer[" + type_to_str_python(p->m_type) + "]";
}
default : throw LFortranException("Not implemented " + std::to_string(t->type));
}
}
Expand Down Expand Up @@ -241,49 +292,6 @@ static inline ASR::expr_t* expr_value(ASR::expr_t *f)
return ASR::expr_value0(f);
}

static inline char *symbol_name(const ASR::symbol_t *f)
{
switch (f->type) {
case ASR::symbolType::Program: {
return ASR::down_cast<ASR::Program_t>(f)->m_name;
}
case ASR::symbolType::Module: {
return ASR::down_cast<ASR::Module_t>(f)->m_name;
}
case ASR::symbolType::Subroutine: {
return ASR::down_cast<ASR::Subroutine_t>(f)->m_name;
}
case ASR::symbolType::Function: {
return ASR::down_cast<ASR::Function_t>(f)->m_name;
}
case ASR::symbolType::GenericProcedure: {
return ASR::down_cast<ASR::GenericProcedure_t>(f)->m_name;
}
case ASR::symbolType::DerivedType: {
return ASR::down_cast<ASR::DerivedType_t>(f)->m_name;
}
case ASR::symbolType::Variable: {
return ASR::down_cast<ASR::Variable_t>(f)->m_name;
}
case ASR::symbolType::ExternalSymbol: {
return ASR::down_cast<ASR::ExternalSymbol_t>(f)->m_name;
}
case ASR::symbolType::ClassProcedure: {
return ASR::down_cast<ASR::ClassProcedure_t>(f)->m_name;
}
case ASR::symbolType::CustomOperator: {
return ASR::down_cast<ASR::CustomOperator_t>(f)->m_name;
}
case ASR::symbolType::AssociateBlock: {
return ASR::down_cast<ASR::AssociateBlock_t>(f)->m_name;
}
case ASR::symbolType::Block: {
return ASR::down_cast<ASR::Block_t>(f)->m_name;
}
default : throw LFortranException("Not implemented");
}
}

static inline SymbolTable *symbol_parent_symtab(const ASR::symbol_t *f)
{
switch (f->type) {
Expand Down
57 changes: 38 additions & 19 deletions src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,10 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
return;
}
der_type_name = "";
uint64_t ptr_loads_copy = ptr_loads;
ptr_loads = ptr_loads_copy - ASR::is_a<ASR::Pointer_t>(*ASRUtils::expr_type(x.m_v));
this->visit_expr(*x.m_v);
ptr_loads = ptr_loads_copy;
ASR::Variable_t* member = down_cast<ASR::Variable_t>(symbol_get_past_external(x.m_m));
std::string member_name = std::string(member->m_name);
LFORTRAN_ASSERT(der_type_name.size() != 0);
Expand Down Expand Up @@ -1608,15 +1611,9 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
}
case (ASR::ttypeType::Pointer) : {
ASR::ttype_t *t2 = ASR::down_cast<ASR::Pointer_t>(asr_type)->m_type;
switch (t2->type) {
case (ASR::ttypeType::Derived) : {
throw CodeGenError("Pointers for Derived type not implemented yet in conversion.");
}
default :
llvm_type = get_type_from_ttype_t(t2, m_storage, is_array_type,
llvm_type = get_type_from_ttype_t(t2, m_storage, is_array_type,
is_malloc_array_type, m_dims, n_dims, a_kind);
llvm_type = llvm_type->getPointerTo();
}
llvm_type = llvm_type->getPointerTo();
break;
}
case (ASR::ttypeType::List) : {
Expand Down Expand Up @@ -2654,9 +2651,14 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>

void visit_CPtrToPointer(const ASR::CPtrToPointer_t& x) {
ASR::expr_t *cptr = x.m_cptr, *fptr = x.m_ptr, *shape = x.m_shape;
int reduce_loads = 0;
if( ASR::is_a<ASR::Var_t>(*cptr) ) {
ASR::Variable_t* cptr_var = ASRUtils::EXPR2VAR(cptr);
reduce_loads = cptr_var->m_intent == ASRUtils::intent_in;
}
if( ASRUtils::is_array(ASRUtils::expr_type(fptr)) ) {
uint64_t ptr_loads_copy = ptr_loads;
ptr_loads = 1;
ptr_loads = 1 - reduce_loads;
this->visit_expr(*cptr);
llvm::Value* llvm_cptr = tmp;
ptr_loads = 0;
Expand Down Expand Up @@ -2706,7 +2708,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
}
} else {
uint64_t ptr_loads_copy = ptr_loads;
ptr_loads = 1;
ptr_loads = 1 - reduce_loads;
this->visit_expr(*cptr);
llvm::Value* llvm_cptr = tmp;
ptr_loads = 0;
Expand Down Expand Up @@ -3689,13 +3691,17 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
switch (t2->type) {
case ASR::ttypeType::Integer:
case ASR::ttypeType::Real:
case ASR::ttypeType::Complex: {
case ASR::ttypeType::Complex:
case ASR::ttypeType::Derived: {
if( t2->type == ASR::ttypeType::Derived ) {
ASR::Derived_t* d = ASR::down_cast<ASR::Derived_t>(t2);
der_type_name = ASRUtils::symbol_name(d->m_derived_type);
}
fetch_ptr(x);
break;
}
case ASR::ttypeType::Character:
case ASR::ttypeType::Logical:
case ASR::ttypeType::Derived: {
case ASR::ttypeType::Logical: {
break;
}
default:
Expand Down Expand Up @@ -4080,7 +4086,16 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
std::vector<std::string> fmt;
for (size_t i=0; i<x.n_values; i++) {
uint64_t ptr_loads_copy = ptr_loads;
ptr_loads = 1;
int reduce_loads = 0;
ptr_loads = 2;
if( ASR::is_a<ASR::Var_t>(*x.m_values[i]) ) {
ASR::Variable_t* var = ASRUtils::EXPR2VAR(x.m_values[i]);
reduce_loads = var->m_intent == ASRUtils::intent_in;
if( ASR::is_a<ASR::Pointer_t>(*var->m_type) ) {
ptr_loads = 1;
}
}
ptr_loads = ptr_loads - reduce_loads;
this->visit_expr_wrapper(x.m_values[i], true);
ptr_loads = ptr_loads_copy;
ASR::expr_t *v = x.m_values[i];
Expand Down Expand Up @@ -4235,11 +4250,13 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
template <typename T>
inline void set_func_subrout_params(T* func_subrout, ASR::abiType& x_abi,
std::uint32_t& m_h, ASR::Variable_t*& orig_arg,
std::string& orig_arg_name, size_t arg_idx) {
std::string& orig_arg_name, ASR::intentType& arg_intent,
size_t arg_idx) {
m_h = get_hash((ASR::asr_t*)func_subrout);
orig_arg = EXPR2VAR(func_subrout->m_args[arg_idx]);
orig_arg_name = orig_arg->m_name;
x_abi = func_subrout->m_abi;
arg_intent = orig_arg->m_intent;
}


Expand All @@ -4255,6 +4272,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
ASR::Subroutine_t* sub = down_cast<ASR::Subroutine_t>(func_subrout);
x_abi = sub->m_abi;
}
// TODO: Below if check is dead. Remove.
if( x_abi == ASR::abiType::Intrinsic ) {
if( name == "lbound" || name == "ubound" ) {
ASR::Variable_t *arg = EXPR2VAR(x.m_args[0].m_value);
Expand Down Expand Up @@ -4283,23 +4301,24 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
tmp = llvm_symtab[h];
func_subrout = symbol_get_past_external(x.m_name);
x_abi = (ASR::abiType) 0;
ASR::intentType orig_arg_intent = ASR::intentType::Unspecified;
std::uint32_t m_h;
ASR::Variable_t *orig_arg = nullptr;
std::string orig_arg_name = "";
if( func_subrout->type == ASR::symbolType::Function ) {
ASR::Function_t* func = down_cast<ASR::Function_t>(func_subrout);
set_func_subrout_params(func, x_abi, m_h, orig_arg, orig_arg_name, i);
set_func_subrout_params(func, x_abi, m_h, orig_arg, orig_arg_name, orig_arg_intent, i);
} else if( func_subrout->type == ASR::symbolType::Subroutine ) {
ASR::Subroutine_t* sub = down_cast<ASR::Subroutine_t>(func_subrout);
set_func_subrout_params(sub, x_abi, m_h, orig_arg, orig_arg_name, i);
set_func_subrout_params(sub, x_abi, m_h, orig_arg, orig_arg_name, orig_arg_intent, i);
} else if( func_subrout->type == ASR::symbolType::ClassProcedure ) {
ASR::ClassProcedure_t* clss_proc = ASR::down_cast<ASR::ClassProcedure_t>(func_subrout);
if( clss_proc->m_proc->type == ASR::symbolType::Subroutine ) {
ASR::Subroutine_t* sub = down_cast<ASR::Subroutine_t>(clss_proc->m_proc);
set_func_subrout_params(sub, x_abi, m_h, orig_arg, orig_arg_name, i);
set_func_subrout_params(sub, x_abi, m_h, orig_arg, orig_arg_name, orig_arg_intent, i);
} else if( clss_proc->m_proc->type == ASR::symbolType::Function ) {
ASR::Function_t* func = down_cast<ASR::Function_t>(clss_proc->m_proc);
set_func_subrout_params(func, x_abi, m_h, orig_arg, orig_arg_name, i);
set_func_subrout_params(func, x_abi, m_h, orig_arg, orig_arg_name, orig_arg_intent, i);
}
} else {
LFORTRAN_ASSERT(false)
Expand Down
Loading