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

feat: Support (ArrayItem = ArrayConstant) type of Assignments with array indices #5824

Merged
merged 3 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Support (ArrayItem = ArrayConstant) type of Assigns
  • Loading branch information
jinangshah21 committed Dec 25, 2024
commit 0a51b1ce1689a88cdc1642f38c99054331c8b145
67 changes: 27 additions & 40 deletions src/libasr/pass/array_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2151,84 +2151,71 @@ class ArrayOpVisitor : public ASR::CallReplacerOnExpressionsVisitor<ArrayOpVisit
resultvar2value[replacer.result_var] = array_broadcast->m_array;
}
}
} else if (PassUtils::is_array(x.m_value)) {
if (ASR::is_a<ASR::ArrayItem_t>(*x.m_target)) {
ASR::ArrayItem_t* array_item_target = ASR::down_cast<ASR::ArrayItem_t>(x.m_target);
for (size_t i=0;i<array_item_target->n_args;i++) {
if(ASRUtils::is_array(ASRUtils::expr_type(array_item_target->m_args[i].m_right))){
ASR::expr_t* array_constant = x.m_value;
ASRUtils::ASRBuilder b(al, array_item_target->base.base.loc);

Vec<ASR::expr_t*> idx_vars;
Vec<ASR::expr_t*> temp_idx_vars;
Vec<ASR::stmt_t*> doloop_body;
ASR::ArrayItem_t* array_item_target = ASR::down_cast<ASR::ArrayItem_t>(x.m_target);
PassUtils::ReplacerUtils::create_do_loop(al, array_item_target->base.base.loc, array_item_target,
idx_vars, temp_idx_vars, doloop_body, [=, &doloop_body, &b, &idx_vars, &temp_idx_vars] () {
ASR::expr_t* res = PassUtils::create_array_ref(array_item_target->m_v, temp_idx_vars, al, current_scope);
ASR::expr_t* ref = PassUtils::create_array_ref(array_constant, idx_vars, al, current_scope);
ASR::stmt_t* assign = b.Assignment(res, ref);
doloop_body.push_back(al, assign);
}, current_scope, &pass_result);

remove_original_statement_value = true;
}
}
} else if ( ASRUtils::is_array(ASRUtils::expr_type(x.m_value)) ) {
size_t n_rhs_dim = 0;
ASR::expr_t* rhs_array_var = nullptr;
if (ASR::is_a<ASR::ArrayItem_t>(*x.m_value)) {
rhs_array_var = ASR::down_cast<ASR::ArrayItem_t>(x.m_value)->m_v;
n_rhs_dim = ASR::down_cast<ASR::ArrayItem_t>(x.m_value)->n_args;
} else if (ASR::is_a<ASR::Var_t>(*x.m_value)) {
n_rhs_dim = ASRUtils::extract_n_dims_from_ttype(ASRUtils::expr_type(x.m_value));
rhs_array_var = x.m_value;
} else if (ASR::is_a<ASR::ArraySection_t>(*x.m_value)) {
rhs_array_var = ASR::down_cast<ASR::ArraySection_t>(x.m_value)->m_v;
n_rhs_dim = ASR::down_cast<ASR::ArraySection_t>(x.m_value)->n_args;
}
resultvar2value[replacer.result_var] = original_value;
} else if ( ASR::is_a<ASR::ArrayItem_t>(*x.m_value) ) {
ASR::ArrayItem_t* array_item_value = ASR::down_cast<ASR::ArrayItem_t>(x.m_value);
if (ASR::is_a<ASR::ArrayItem_t>(*x.m_target)) {
LCOMPILERS_ASSERT(rhs_array_var != nullptr);
ASR::ArrayItem_t* array_item_target = ASR::down_cast<ASR::ArrayItem_t>(x.m_target);
bool is_array_index = false;
for (size_t i=0; i<array_item_target->n_args; i++) {
if(ASRUtils::is_array(ASRUtils::expr_type(array_item_target->m_args[i].m_right))){
is_array_index = true;
}
}
if(is_array_index){
if (is_array_index) {
ASRUtils::ASRBuilder b(al, array_item_target->base.base.loc);
ASR::ttype_t* integer_type = ASR::down_cast<ASR::ttype_t>(
ASR::make_Integer_t(al, array_item_target->base.base.loc, 4));
Vec<ASR::dimension_t> temporary_array_dims;
temporary_array_dims.reserve(al, 1);
for(size_t i = 0; i < array_item_value->n_args; i++) {
for(size_t i = 0; i < n_rhs_dim; i++) {
ASR::dimension_t temporary_array_dim;
temporary_array_dim.loc = array_item_value->m_v->base.loc;
temporary_array_dim.loc = rhs_array_var->base.loc;
temporary_array_dim.m_start = ASRUtils::EXPR(ASR::make_IntegerConstant_t(
al, array_item_value->m_v->base.loc, 1, integer_type));
al, rhs_array_var->base.loc, 1, integer_type));
temporary_array_dim.m_length = ASRUtils::EXPR(ASR::make_ArraySize_t(
al, array_item_value->m_v->base.loc, array_item_value->m_v,
al, rhs_array_var->base.loc, rhs_array_var,
b.i32(i + 1), integer_type, nullptr));
temporary_array_dims.push_back(al, temporary_array_dim);
}

ASR::ttype_t* temporary_array_type = ASRUtils::TYPE(ASR::make_Array_t(
al, array_item_value->m_v->base.loc, ASRUtils::extract_type(
ASRUtils::expr_type(array_item_value->m_v)),
al, rhs_array_var->base.loc, ASRUtils::extract_type(
ASRUtils::expr_type(rhs_array_var)),
temporary_array_dims.p, temporary_array_dims.size(),
ASR::array_physical_typeType::PointerToDataArray));

ASR::expr_t* temporary_array = PassUtils::create_var(
0, "_temporary_value", array_item_value->m_v->base.loc,
0, "_temporary_value", rhs_array_var->base.loc,
ASRUtils::duplicate_type(al, temporary_array_type), al, current_scope);

Vec<ASR::expr_t*> idx_vars;
Vec<ASR::expr_t*> temp_idx_vars_lhs;
Vec<ASR::expr_t*> temp_idx_vars_rhs;
Vec<ASR::stmt_t*> doloop_body;
PassUtils::ReplacerUtils::create_do_loop(al, array_item_value->base.base.loc, array_item_value->n_args,
array_item_value->m_v, idx_vars, doloop_body, [=, &doloop_body, &b, &idx_vars] () {
PassUtils::ReplacerUtils::create_do_loop(al, rhs_array_var->base.loc, n_rhs_dim,
rhs_array_var, idx_vars, doloop_body, [=, &doloop_body, &b, &idx_vars] () {
ASR::expr_t* res = PassUtils::create_array_ref(temporary_array, idx_vars, al, current_scope);
ASR::expr_t* ref = PassUtils::create_array_ref(array_item_value->m_v, idx_vars, al, current_scope);
ASR::expr_t* ref = PassUtils::create_array_ref(rhs_array_var, idx_vars, al, current_scope);
ASR::stmt_t* assign = b.Assignment(res, ref);
doloop_body.push_back(al, assign);
}, current_scope, &pass_result);
doloop_body.resize(al, 0);
idx_vars.resize(al, 0);

PassUtils::ReplacerUtils::create_do_loop_assign(al, array_item_target->base.base.loc, array_item_target,
array_item_value,idx_vars, temp_idx_vars_lhs, temp_idx_vars_rhs, doloop_body,
PassUtils::ReplacerUtils::create_do_loop_assign(al, array_item_target->base.base.loc, x.m_target,
x.m_value, idx_vars, temp_idx_vars_lhs, temp_idx_vars_rhs, doloop_body,
[=, &temp_idx_vars_lhs, &temp_idx_vars_rhs, &doloop_body, &b] () {
ASR::expr_t* res = PassUtils::create_array_ref(array_item_target->m_v, temp_idx_vars_lhs, al, current_scope);
ASR::expr_t* ref = PassUtils::create_array_ref(temporary_array, temp_idx_vars_rhs, al, current_scope);
Expand All @@ -2237,7 +2224,7 @@ class ArrayOpVisitor : public ASR::CallReplacerOnExpressionsVisitor<ArrayOpVisit
}, current_scope, &pass_result);

remove_original_statement_value = true;
current_expr = const_cast<ASR::expr_t**>(&(array_item_value->m_v));
current_expr = const_cast<ASR::expr_t**>(&(rhs_array_var));
}
}
resultvar2value[replacer.result_var] = original_value;
Expand Down
144 changes: 97 additions & 47 deletions src/libasr/pass/pass_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -885,62 +885,112 @@ namespace LCompilers {

template <typename LOOP_BODY>
static inline void create_do_loop_assign(Allocator& al, const Location& loc,
ASR::ArrayItem_t* lhs, ASR::ArrayItem_t* rhs, Vec<ASR::expr_t*>& idx_vars, Vec<ASR::expr_t*>& temp_idx_vars_lhs,
ASR::expr_t* lhs, ASR::expr_t* rhs, Vec<ASR::expr_t*>& idx_vars, Vec<ASR::expr_t*>& temp_idx_vars_lhs,
Vec<ASR::expr_t*>& temp_idx_vars_rhs, Vec<ASR::stmt_t*>& doloop_body, LOOP_BODY loop_body,
SymbolTable* current_scope, Vec<ASR::stmt_t*>* result_vec) {
int n_array_indices = 0;
for( size_t i = 0; i < rhs->n_args; i++ ) {
if(ASRUtils::is_array(ASRUtils::expr_type(rhs->m_args[i].m_right))){
n_array_indices++;
if (ASR::is_a<ASR::ArrayItem_t>(*lhs) && ASR::is_a<ASR::ArrayItem_t>(*rhs)) {
// Case : A([1,2,3]) = B([1,2,3])
ASR::ArrayItem_t* lhs_array = ASR::down_cast<ASR::ArrayItem_t>(lhs);
ASR::ArrayItem_t* rhs_array = ASR::down_cast<ASR::ArrayItem_t>(rhs);
int n_array_indices = 0;
for( size_t i = 0; i < rhs_array->n_args; i++ ) {
if (ASRUtils::is_array(ASRUtils::expr_type(rhs_array->m_args[i].m_right))) {
n_array_indices++;
}
}
}
if(n_array_indices == 0) return;
PassUtils::create_idx_vars(idx_vars, n_array_indices, loc, al, current_scope, "_t");
temp_idx_vars_rhs.reserve(al, rhs->n_args);
temp_idx_vars_lhs.reserve(al, rhs->n_args);
for( size_t i = 0, j = 0; i < rhs->n_args; i++ ) {
if(ASRUtils::is_array(ASRUtils::expr_type(rhs->m_args[i].m_right))){
ASR::expr_t* ref = PassUtils::create_array_ref(rhs->m_args[i].m_right, idx_vars[j], al, current_scope);
temp_idx_vars_rhs.push_back(al, ref);
j++;
} else {
temp_idx_vars_rhs.push_back(al, rhs->m_args[i].m_right);
if(n_array_indices == 0) return;
PassUtils::create_idx_vars(idx_vars, n_array_indices, loc, al, current_scope, "_t");
temp_idx_vars_rhs.reserve(al, rhs_array->n_args);
temp_idx_vars_lhs.reserve(al, lhs_array->n_args);
for( size_t i = 0, j = 0; i < rhs_array->n_args; i++ ) {
if (ASRUtils::is_array(ASRUtils::expr_type(rhs_array->m_args[i].m_right))) {
ASR::expr_t* ref = PassUtils::create_array_ref(rhs_array->m_args[i].m_right, idx_vars[j], al, current_scope);
temp_idx_vars_rhs.push_back(al, ref);
j++;
} else {
temp_idx_vars_rhs.push_back(al, rhs_array->m_args[i].m_right);
}
}
}
for( size_t i = 0, j = 0; i < lhs->n_args; i++ ) {
if(ASRUtils::is_array(ASRUtils::expr_type(lhs->m_args[i].m_right))){
ASR::expr_t* ref = PassUtils::create_array_ref(lhs->m_args[i].m_right, idx_vars[j], al, current_scope);
temp_idx_vars_lhs.push_back(al, ref);
j++;
} else {
temp_idx_vars_lhs.push_back(al, lhs->m_args[i].m_right);
for( size_t i = 0, j = 0; i < lhs_array->n_args; i++ ) {
if (ASRUtils::is_array(ASRUtils::expr_type(lhs_array->m_args[i].m_right))) {
ASR::expr_t* ref = PassUtils::create_array_ref(lhs_array->m_args[i].m_right, idx_vars[j], al, current_scope);
temp_idx_vars_lhs.push_back(al, ref);
j++;
} else {
temp_idx_vars_lhs.push_back(al, lhs_array->m_args[i].m_right);
}
}
}

ASR::stmt_t* doloop = nullptr;
for( size_t i = 0, j = 0; i < rhs->n_args; i++ ) {
ASR::do_loop_head_t head;
head.m_v = idx_vars[j];
if(ASRUtils::is_array(ASRUtils::expr_type(rhs->m_args[i].m_right))) {
head.m_start = PassUtils::get_bound(rhs->m_args[i].m_right, 1, "lbound", al);
head.m_end = PassUtils::get_bound(rhs->m_args[i].m_right, 1, "ubound", al);
head.m_increment = nullptr;
j++;
} else {
continue;
}
head.loc = head.m_v->base.loc;
ASR::stmt_t* doloop = nullptr;
for( size_t i = 0, j = 0; i < rhs_array->n_args; i++ ) {
ASR::do_loop_head_t head;
head.m_v = idx_vars[j];
if (ASRUtils::is_array(ASRUtils::expr_type(rhs_array->m_args[i].m_right))) {
head.m_start = PassUtils::get_bound(rhs_array->m_args[i].m_right, 1, "lbound", al);
head.m_end = PassUtils::get_bound(rhs_array->m_args[i].m_right, 1, "ubound", al);
head.m_increment = nullptr;
j++;
} else {
continue;
}
head.loc = head.m_v->base.loc;

doloop_body.reserve(al, 1);
if( doloop == nullptr ) {
loop_body();
} else {
doloop_body.push_back(al, doloop);
doloop_body.reserve(al, 1);
if( doloop == nullptr ) {
loop_body();
} else {
doloop_body.push_back(al, doloop);
}
doloop = ASRUtils::STMT(ASR::make_DoLoop_t(al, loc, nullptr, head,
doloop_body.p, doloop_body.size(), nullptr, 0));
}
doloop = ASRUtils::STMT(ASR::make_DoLoop_t(al, loc, nullptr, head,
doloop_body.p, doloop_body.size(), nullptr, 0));
result_vec->push_back(al, doloop);
} else if (ASR::is_a<ASR::ArrayItem_t>(*lhs) && ASR::is_a<ASR::Var_t>(*rhs)) {
// Case : A([1,2,3]) = A2
ASR::ArrayItem_t* lhs_array = ASR::down_cast<ASR::ArrayItem_t>(lhs);
int n_array_indices = ASRUtils::extract_n_dims_from_ttype(ASRUtils::expr_type(rhs));
if(n_array_indices == 0) return;
PassUtils::create_idx_vars(idx_vars, n_array_indices, loc, al, current_scope, "_t");
temp_idx_vars_rhs.reserve(al, n_array_indices);
temp_idx_vars_lhs.reserve(al, lhs_array->n_args);
for( int i = 0; i < n_array_indices; i++ ) {
temp_idx_vars_rhs.push_back(al, idx_vars[i]);
}
for( size_t i = 0, j = 0; i < lhs_array->n_args; i++ ) {
if (ASRUtils::is_array(ASRUtils::expr_type(lhs_array->m_args[i].m_right))) {
ASR::expr_t* ref = PassUtils::create_array_ref(lhs_array->m_args[i].m_right, idx_vars[j], al, current_scope);
temp_idx_vars_lhs.push_back(al, ref);
j++;
} else {
temp_idx_vars_lhs.push_back(al, lhs_array->m_args[i].m_right);
}
}

ASR::stmt_t* doloop = nullptr;
for( size_t i = 0, j = 0; i < lhs_array->n_args; i++ ) {
ASR::do_loop_head_t head;
head.m_v = idx_vars[j];
if (ASRUtils::is_array(ASRUtils::expr_type(lhs_array->m_args[i].m_right))) {
head.m_start = PassUtils::get_bound(lhs_array->m_args[i].m_right, 1, "lbound", al);
head.m_end = PassUtils::get_bound(lhs_array->m_args[i].m_right, 1, "ubound", al);
head.m_increment = nullptr;
j++;
} else {
continue;
}
head.loc = head.m_v->base.loc;

doloop_body.reserve(al, 1);
if( doloop == nullptr ) {
loop_body();
} else {
doloop_body.push_back(al, doloop);
}
doloop = ASRUtils::STMT(ASR::make_DoLoop_t(al, loc, nullptr, head,
doloop_body.p, doloop_body.size(), nullptr, 0));
}
result_vec->push_back(al, doloop);
}
if(doloop != nullptr) result_vec->push_back(al, doloop);
}

void visit_ArrayConstant(ASR::ArrayConstant_t* x, Allocator& al,
Expand Down