Skip to content

[Sprint] Bug fixes for LLVM and C backends #1291

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

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ RUN(NAME structs_07 LABELS llvm c
RUN(NAME structs_08 LABELS cpython llvm c)
RUN(NAME structs_09 LABELS cpython llvm c)
RUN(NAME structs_10 LABELS cpython llvm c)
RUN(NAME structs_11 LABELS cpython llvm c)
RUN(NAME sizeof_01 LABELS llvm c
EXTRAFILES sizeof_01b.c)
RUN(NAME enum_01 LABELS cpython llvm c)
Expand Down
18 changes: 18 additions & 0 deletions integration_tests/structs_11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from ltypes import i32, f64, dataclass

@dataclass
class A:
x: i32
y: f64

def f(x_: i32, y_: f64) -> A:
a_struct: A = A(x_, y_)
return a_struct

def test_struct_return():
b: A = f(0, 1.0)
print(b.x, b.y)
assert b.x == 0
assert b.y == 1.0

test_struct_return()
28 changes: 20 additions & 8 deletions src/libasr/codegen/asr_to_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
ASR::expr_t *length = m_dims[i].m_length;
if (!length) {
dims += "*";
is_fixed_size = false;
} else {
visit_expr(*length);
array_size += "*" + src;
Expand Down Expand Up @@ -285,7 +286,7 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
if( !is_fixed_size ) {
sub += indent + format_type_c("*", type_name_copy, std::string(v_m_name) + "_data",
use_ref, dummy);
sub += " = " + dims + ";\n";
sub += ";\n";
} else {
sub += indent + format_type_c(dims, type_name_copy, std::string(v_m_name) + "_data",
use_ref, dummy) + ";\n";
Expand Down Expand Up @@ -360,14 +361,25 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
} else if(ASR::is_a<ASR::Struct_t>(*t2)) {
ASR::Struct_t *t = ASR::down_cast<ASR::Struct_t>(t2);
std::string der_type_name = ASRUtils::symbol_name(t->m_derived_type);
bool is_fixed_size = true;
std::string dims = convert_dims_c(t->n_dims, t->m_dims, v_m_type, is_fixed_size);
std::string ptr_char = "*";
if( !use_ptr_for_derived_type ) {
ptr_char.clear();
if( is_array ) {
bool is_fixed_size = true;
std::string dims = convert_dims_c(t->n_dims, t->m_dims, v_m_type, is_fixed_size, true);
std::string encoded_type_name = "x" + der_type_name;
std::string type_name = std::string("struct ") + der_type_name;
generate_array_decl(sub, std::string(v.m_name), type_name, dims,
encoded_type_name, t->m_dims, t->n_dims,
use_ref, dummy,
v.m_intent != ASRUtils::intent_in &&
v.m_intent != ASRUtils::intent_inout,
is_fixed_size);
} else {
std::string ptr_char = "*";
if( !use_ptr_for_derived_type ) {
ptr_char.clear();
}
sub = format_type_c("", "struct " + der_type_name + ptr_char,
v.m_name, use_ref, dummy);
}
sub = format_type_c(dims, "struct " + der_type_name + ptr_char,
v.m_name, use_ref, dummy);
} else if(ASR::is_a<ASR::CPtr_t>(*t2)) {
sub = format_type_c("", "void**", v.m_name, false, false);
} else {
Expand Down
16 changes: 14 additions & 2 deletions src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,9 @@ R"(#include <stdio.h>
ASR::Pointer_t* ptr_type = ASR::down_cast<ASR::Pointer_t>(return_var->m_type);
std::string pointer_type_str = CUtils::get_c_type_from_ttype_t(ptr_type->m_type);
sub = pointer_type_str + "*";
} else if (ASR::is_a<ASR::Struct_t>(*return_var->m_type)) {
std::string struct_type_str = CUtils::get_c_type_from_ttype_t(return_var->m_type);
sub = struct_type_str + " ";
} else {
throw CodeGenError("Return type not supported in function '" +
std::string(x.m_name) +
Expand Down Expand Up @@ -437,8 +440,15 @@ R"(#include <stdio.h>
for (auto &item : x.m_symtab->get_scope()) {
if (ASR::is_a<ASR::Variable_t>(*item.second)) {
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(item.second);
if (v->m_intent == LFortran::ASRUtils::intent_local || v->m_intent == LFortran::ASRUtils::intent_return_var) {
decl += indent + self().convert_variable_decl(*v);
if (v->m_intent == LFortran::ASRUtils::intent_local ||
v->m_intent == LFortran::ASRUtils::intent_return_var) {
if (is_c) {
decl += indent + self().convert_variable_decl(*v, true,
!(ASR::is_a<ASR::Struct_t>(*v->m_type) &&
std::string(v->m_name) == "_lpython_return_variable"), true);
} else {
decl += indent + self().convert_variable_decl(*v);
}
if( !ASR::is_a<ASR::Const_t>(*v->m_type) ||
v->m_intent == ASRUtils::intent_return_var ) {
decl += ";\n";
Expand Down Expand Up @@ -671,6 +681,8 @@ R"(#include <stdio.h>
value = "*" + value;
} else if (ASR::is_a<ASR::ArrayItem_t>(*x.m_value)) {
value = "&" + value;
} else if (ASR::is_a<ASR::FunctionCall_t>(*x.m_value)) {
target = "*" + target;
}
}
if( !from_std_vector_helper.empty() ) {
Expand Down
5 changes: 4 additions & 1 deletion src/libasr/codegen/asr_to_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,11 @@ class ASRToCPPVisitor : public BaseCCPPVisitor<ASRToCPPVisitor>
}

std::string convert_variable_decl(const ASR::Variable_t &v, bool use_static=true,
bool use_templates_for_arrays=false)
bool use_templates_for_arrays=false, bool _=false)
{
if( _ ) {

}
std::string sub;
bool use_ref = (v.m_intent == LFortran::ASRUtils::intent_out ||

Expand Down
23 changes: 11 additions & 12 deletions src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1685,8 +1685,8 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
llvm::Value* array = nullptr;
if( ASR::is_a<ASR::Var_t>(*x.m_v) ) {
ASR::Variable_t *v = ASRUtils::EXPR2VAR(x.m_v);
if( ASR::is_a<ASR::Struct_t>(*v->m_type) ) {
ASR::Struct_t* der_type = ASR::down_cast<ASR::Struct_t>(v->m_type);
if( ASR::is_a<ASR::Struct_t>(*ASRUtils::get_contained_type(v->m_type)) ) {
ASR::Struct_t* der_type = ASR::down_cast<ASR::Struct_t>(ASRUtils::get_contained_type(v->m_type));
der_type_name = ASRUtils::symbol_name(ASRUtils::symbol_get_past_external(der_type->m_derived_type));
}
uint32_t v_h = get_hash((ASR::asr_t*)v);
Expand Down Expand Up @@ -1920,7 +1920,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
if( ASR::is_a<ASR::UnionRef_t>(*x.m_v) ) {
ptr_loads = 0;
} else {
ptr_loads = ptr_loads_copy - ASR::is_a<ASR::Pointer_t>(*x_m_v_type);
ptr_loads = 1;
}
this->visit_expr(*x.m_v);
ptr_loads = ptr_loads_copy;
Expand Down Expand Up @@ -3346,9 +3346,10 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
return_type = get_type_from_ttype_t_util(ASRUtils::get_contained_type(return_var_type0))->getPointerTo();
break;
}
case (ASR::ttypeType::Struct) :
throw CodeGenError("Struct return type not implemented yet");
case (ASR::ttypeType::Struct) : {
return_type = get_type_from_ttype_t_util(return_var_type0);
break;
}
case (ASR::ttypeType::Tuple) : {
ASR::Tuple_t* asr_tuple = ASR::down_cast<ASR::Tuple_t>(return_var_type0);
std::string type_code = ASRUtils::get_type_code(asr_tuple->m_type,
Expand Down Expand Up @@ -5870,13 +5871,11 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
uint64_t ptr_loads_copy = ptr_loads;
ptr_loads = !LLVM::is_llvm_struct(arg_type);
this->visit_expr_wrapper(x.m_args[i].m_value);
if( x_abi == ASR::abiType::BindC ) {
if( ASR::is_a<ASR::ArrayItem_t>(*x.m_args[i].m_value) ||
ASR::is_a<ASR::StructInstanceMember_t>(*x.m_args[i].m_value) ||
(ASR::is_a<ASR::CPtr_t>(*arg_type) &&
ASR::is_a<ASR::StructInstanceMember_t>(*x.m_args[i].m_value)) ) {
tmp = LLVM::CreateLoad(*builder, tmp);
}
if( ASR::is_a<ASR::ArrayItem_t>(*x.m_args[i].m_value) ||
ASR::is_a<ASR::StructInstanceMember_t>(*x.m_args[i].m_value) ||
(ASR::is_a<ASR::CPtr_t>(*arg_type) &&
ASR::is_a<ASR::StructInstanceMember_t>(*x.m_args[i].m_value)) ) {
tmp = LLVM::CreateLoad(*builder, tmp);
}
llvm::Value *value = tmp;
ptr_loads = ptr_loads_copy;
Expand Down