Skip to content

Create "char buffer[...]" for "i8[...]" typed arrays #1346

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 7 commits into from
Dec 5, 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
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ RUN(NAME structs_11 LABELS cpython llvm c)
RUN(NAME structs_12 LABELS cpython llvm c)
RUN(NAME structs_13 LABELS llvm c
EXTRAFILES structs_13b.c)
RUN(NAME structs_14 LABELS cpython llvm c)
RUN(NAME structs_15 LABELS cpython llvm c)
RUN(NAME sizeof_01 LABELS llvm c
EXTRAFILES sizeof_01b.c)
Expand Down
32 changes: 32 additions & 0 deletions integration_tests/structs_14.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from ltypes import i8, dataclass, i32, ccallable
from numpy import empty, int8
from copy import deepcopy

@dataclass
class buffer_struct:
buffer: i8[32]

@ccallable
@dataclass
class buffer_struct_clink:
buffer: i8[32]

def f():
i: i32
buffer_var: i8[32] = empty(32, dtype=int8)
buffer_: buffer_struct = buffer_struct(deepcopy(buffer_var))
buffer_clink_: buffer_struct_clink = buffer_struct_clink(deepcopy(buffer_var))
print(buffer_.buffer[15])
print(buffer_clink_.buffer[15])

for i in range(32):
buffer_.buffer[i] = i8(i + 1)
buffer_clink_.buffer[i] = i8(i + 2)

for i in range(32):
print(i, buffer_.buffer[i], buffer_clink_.buffer[i])
assert buffer_.buffer[i] == i8(i + 1)
assert buffer_clink_.buffer[i] == i8(i + 2)
assert buffer_clink_.buffer[i] - buffer_.buffer[i] == i8(1)

f()
44 changes: 44 additions & 0 deletions src/libasr/asr_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,23 @@ static inline ASR::ttype_t* symbol_type(const ASR::symbol_t *f)
return nullptr;
}

static inline ASR::abiType symbol_abi(const ASR::symbol_t *f)
{
switch( f->type ) {
case ASR::symbolType::Variable: {
return ASR::down_cast<ASR::Variable_t>(f)->m_abi;
}
case ASR::symbolType::EnumType: {
return ASR::down_cast<ASR::EnumType_t>(f)->m_abi;
}
default: {
throw LCompilersException("Cannot return ABI of, " +
std::to_string(f->type) + " symbol.");
}
}
return ASR::abiType::Source;
}

static inline ASR::ttype_t* get_contained_type(ASR::ttype_t* asr_type) {
switch( asr_type->type ) {
case ASR::ttypeType::List: {
Expand Down Expand Up @@ -138,6 +155,20 @@ static inline ASR::ttype_t* get_contained_type(ASR::ttype_t* asr_type) {
}
}

static inline ASR::abiType expr_abi(ASR::expr_t* e) {
switch( e->type ) {
case ASR::exprType::Var: {
return ASRUtils::symbol_abi(ASR::down_cast<ASR::Var_t>(e)->m_v);
}
case ASR::exprType::StructInstanceMember: {
return ASRUtils::symbol_abi(ASR::down_cast<ASR::StructInstanceMember_t>(e)->m_m);
}
default:
throw LCompilersException("Cannot extract the ABI of " +
std::to_string(e->type) + " expression.");
}
}

static inline std::string type_to_str(const ASR::ttype_t *t)
{
switch (t->type) {
Expand Down Expand Up @@ -1369,6 +1400,19 @@ inline int extract_dimensions_from_ttype(ASR::ttype_t *x,
return n_dims;
}

static inline bool is_fixed_size_array(ASR::dimension_t* m_dims, size_t n_dims) {
if( n_dims == 0 ) {
return false;
}
for( size_t i = 0; i < n_dims; i++ ) {
int64_t dim_size = -1;
if( !ASRUtils::extract_value(ASRUtils::expr_value(m_dims[i].m_length), dim_size) ) {
return false;
}
}
return true;
}

inline int extract_n_dims_from_ttype(ASR::ttype_t *x) {
ASR::dimension_t* m_dims_temp = nullptr;
return extract_dimensions_from_ttype(x, m_dims_temp);
Expand Down
74 changes: 55 additions & 19 deletions src/libasr/codegen/asr_to_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,13 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
std::string mem_var_name = current_scope->get_unique_name(itr.first + std::to_string(counter));
counter += 1;
sub += indent + convert_variable_decl(*mem_var, true, true, true, true, mem_var_name) + ";\n";
sub += indent + name + "->" + itr.first + " = " + mem_var_name + ";\n";
if( mem_var->m_abi == ASR::abiType::BindC &&
ASR::is_a<ASR::Integer_t>(*mem_var->m_type) &&
ASRUtils::extract_kind_from_ttype_t(mem_var->m_type) == 1 ) {
sub += indent + "strcpy(" + name + "->" + itr.first + ", " + mem_var_name + ");\n";
} else {
sub += indent + name + "->" + itr.first + " = " + mem_var_name + ";\n";
}
} else if( ASR::is_a<ASR::Struct_t>(*mem_type) ) {
ASR::Struct_t* struct_t = ASR::down_cast<ASR::Struct_t>(mem_type);
ASR::StructType_t* struct_type_t = ASR::down_cast<ASR::StructType_t>(
Expand Down Expand Up @@ -238,16 +244,26 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
if( is_array ) {
bool is_fixed_size = true;
dims = convert_dims_c(t->n_dims, t->m_dims, v_m_type, is_fixed_size, true);
std::string encoded_type_name = "i" + std::to_string(t->m_kind * 8);
bool is_struct_type_member = ASR::is_a<ASR::StructType_t>(
*ASR::down_cast<ASR::symbol_t>(v.m_parent_symtab->asr_owner));
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 &&
v.m_intent != ASRUtils::intent_out &&
!is_struct_type_member) || force_declare, is_fixed_size);
if( t->m_kind == 1 && v.m_abi == ASR::abiType::BindC && is_fixed_size ) {
if( !force_declare ) {
force_declare_name = std::string(v.m_name);
}
sub = "char " + force_declare_name + dims;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not using int8_t ? It seems that's cleaner and consistent with i16, i32, i64, etc.

Copy link
Collaborator Author

@czgdp1807 czgdp1807 Dec 5, 2022

Choose a reason for hiding this comment

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

I think in the issue it was mentioned to create char buffer[64] for buffer: i8[64].

} else {
std::string encoded_type_name = "i" + std::to_string(t->m_kind * 8);
bool is_struct_type_member = ASR::is_a<ASR::StructType_t>(
*ASR::down_cast<ASR::symbol_t>(v.m_parent_symtab->asr_owner));
if( !force_declare ) {
force_declare_name = std::string(v.m_name);
}
generate_array_decl(sub, force_declare_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 &&
v.m_intent != ASRUtils::intent_out &&
!is_struct_type_member) || force_declare, is_fixed_size);
}
} else {
bool is_fixed_size = true;
dims = convert_dims_c(t->n_dims, t->m_dims, v_m_type, is_fixed_size);
Expand Down Expand Up @@ -1024,9 +1040,18 @@ R"(
this->visit_expr(*x.m_v);
std::string array = src;
std::string out = array;
ASR::ttype_t* x_mv_type = ASRUtils::expr_type(x.m_v);
ASR::dimension_t* m_dims;
ASRUtils::extract_dimensions_from_ttype(ASRUtils::expr_type(x.m_v), m_dims);
out += "->data[";
int n_dims = ASRUtils::extract_dimensions_from_ttype(x_mv_type, m_dims);
bool is_i8_array = (ASR::is_a<ASR::Integer_t>(*x_mv_type) &&
ASRUtils::extract_kind_from_ttype_t(x_mv_type) == 1 &&
ASRUtils::is_fixed_size_array(m_dims, n_dims) &&
ASRUtils::expr_abi(x.m_v) == ASR::abiType::BindC);
if( is_i8_array ) {
out += "[";
} else {
out += "->data[";
}
std::string index = "";
for (size_t i=0; i<x.n_args; i++) {
std::string current_index = "";
Expand All @@ -1036,13 +1061,24 @@ R"(
src = "/* FIXME right index */";
}

current_index += "(" + src + " - " + array + "->dims["
+ std::to_string(i) + "].lower_bound)";
for( size_t j = i + 1; j < x.n_args; j++ ) {
std::string length = array + "->dims[" + std::to_string(j) + "].length";
current_index += " * " + length;
if( is_i8_array ) {
current_index += src;
for( size_t j = i + 1; j < x.n_args; j++ ) {
int64_t dim_size;
ASRUtils::extract_value(m_dims[j].m_length, dim_size);
std::string length = std::to_string(dim_size);
current_index += " * " + length;
}
index += current_index;
} else {
current_index += "(" + src + " - " + array + "->dims["
+ std::to_string(i) + "].lower_bound)";
for( size_t j = i + 1; j < x.n_args; j++ ) {
std::string length = array + "->dims[" + std::to_string(j) + "].length";
current_index += " * " + length;
}
index += current_index;
}
index += current_index;
if (i < x.n_args - 1) {
index += " + ";
}
Expand Down
38 changes: 36 additions & 2 deletions src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,41 @@ R"(#include <stdio.h>
alloc = indent + target + " = " + "(char *) malloc((strlen(" +
value + ") + 1 ) * sizeof(char));\n";
}
src += alloc + indent + c_ds_api->get_deepcopy(m_target_type, value, target) + "\n";
if( ASRUtils::is_array(m_target_type) && ASRUtils::is_array(m_value_type) ) {
bool is_target_i8_array = (ASR::is_a<ASR::Integer_t>(*m_target_type) &&
ASRUtils::extract_kind_from_ttype_t(m_target_type) == 1 &&
ASRUtils::expr_abi(x.m_target) == ASR::abiType::BindC);
bool is_value_i8_array = (ASR::is_a<ASR::Integer_t>(*m_value_type) &&
ASRUtils::extract_kind_from_ttype_t(m_value_type) == 1 &&
ASRUtils::expr_abi(x.m_value) == ASR::abiType::BindC);
bool is_target_fixed_size = false, is_value_fixed_size = false;
if( is_target_i8_array ) {
ASR::Integer_t* target_integer_t = ASR::down_cast<ASR::Integer_t>(m_target_type);
if( ASRUtils::is_fixed_size_array(target_integer_t->m_dims, target_integer_t->n_dims) ) {
is_target_fixed_size = true;
}
}
if( is_value_i8_array ) {
ASR::Integer_t* value_integer_t = ASR::down_cast<ASR::Integer_t>(m_value_type);
if( ASRUtils::is_fixed_size_array(value_integer_t->m_dims, value_integer_t->n_dims) ) {
is_value_fixed_size = true;
}
}
if( (is_target_i8_array && is_target_fixed_size) ||
(is_value_i8_array && is_value_fixed_size) ) {
if( !(is_target_i8_array && is_target_fixed_size) ) {
target = "(char*) " + target + "->data";
}
if( !(is_value_i8_array && is_value_fixed_size) ) {
value = "(char*) " + value + "->data";
}
src += indent + "strcpy(" + target + ", " + value + ");\n";
} else {
src += alloc + indent + c_ds_api->get_deepcopy(m_target_type, value, target) + "\n";
}
} else {
src += alloc + indent + c_ds_api->get_deepcopy(m_target_type, value, target) + "\n";
}
} else {
src += indent + c_ds_api->get_deepcopy(m_target_type, value, target) + "\n";
}
Expand Down Expand Up @@ -879,7 +913,7 @@ R"(#include <stdio.h>
step = "1";
}
self().visit_expr(*x.m_a);

ASR::ttype_t* t_ttype = ASRUtils::expr_type(x.m_a);
ASR::List_t* t = ASR::down_cast<ASR::List_t>(t_ttype);
std::string list_var = std::move(src);
Expand Down
Loading