Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 324f583

Browse files
Thirumalai-ShaktivelAgent-Hellboy
authored andcommitted
[C] Use memcpy for initializing SIMDArray's
1 parent f74f6bb commit 324f583

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/libasr/asr_utils.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1521,7 +1521,12 @@ void make_ArrayBroadcast_t_util(Allocator& al, const Location& loc,
15211521
if (ret_type == nullptr) {
15221522
// TODO: Construct appropriate return type here
15231523
// For now simply coping the type from expr1
1524-
ret_type = expr1_type;
1524+
if (ASRUtils::is_simd_array(expr1)) {
1525+
// TODO: Make this more general; do not check for SIMDArray
1526+
ret_type = ASRUtils::duplicate_type(al, expr1_type);
1527+
} else {
1528+
ret_type = expr1_type;
1529+
}
15251530
}
15261531
expr2 = ASRUtils::EXPR(ASR::make_ArrayBroadcast_t(al, loc, expr2, dest_shape, ret_type, value));
15271532

src/libasr/codegen/asr_to_c_cpp.h

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1246,7 +1246,37 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
12461246
bool is_value_dict = ASR::is_a<ASR::Dict_t>(*m_value_type);
12471247
bool alloc_return_var = false;
12481248
std::string indent(indentation_level*indentation_spaces, ' ');
1249-
if (ASR::is_a<ASR::Var_t>(*x.m_target)) {
1249+
if (ASRUtils::is_simd_array(x.m_target)) {
1250+
this->visit_expr(*x.m_target);
1251+
target = src;
1252+
if (ASR::is_a<ASR::Var_t>(*x.m_value) ||
1253+
ASR::is_a<ASR::ArraySection_t>(*x.m_value)) {
1254+
std::string arr_element_type = CUtils::get_c_type_from_ttype_t(
1255+
ASRUtils::expr_type(x.m_value));
1256+
std::string size = std::to_string(ASRUtils::get_fixed_size_of_array(
1257+
ASRUtils::expr_type(x.m_target)));
1258+
std::string value;
1259+
if (ASR::is_a<ASR::ArraySection_t>(*x.m_value)) {
1260+
ASR::ArraySection_t *arr = ASR::down_cast<ASR::ArraySection_t>(x.m_value);
1261+
this->visit_expr(*arr->m_v);
1262+
value = src;
1263+
if(!ASR::is_a<ASR::ArrayBound_t>(*arr->m_args->m_left)) {
1264+
this->visit_expr(*arr->m_args->m_left);
1265+
int n_dims = ASRUtils::extract_n_dims_from_ttype(arr->m_type) - 1;
1266+
value += "->data + (" + src + " - "+ value +"->dims["
1267+
+ std::to_string(n_dims) +"].lower_bound)";
1268+
} else {
1269+
value += "->data";
1270+
}
1271+
} else if (ASR::is_a<ASR::Var_t>(*x.m_value)) {
1272+
this->visit_expr(*x.m_value);
1273+
value = src + "->data";
1274+
}
1275+
src = indent + "memcpy(&"+ target +", "+ value +", sizeof("
1276+
+ arr_element_type + ") * "+ size +");\n";
1277+
return;
1278+
}
1279+
} else if (ASR::is_a<ASR::Var_t>(*x.m_target)) {
12501280
ASR::Var_t* x_m_target = ASR::down_cast<ASR::Var_t>(x.m_target);
12511281
visit_Var(*x_m_target);
12521282
target = src;

0 commit comments

Comments
 (0)