-
-
Notifications
You must be signed in to change notification settings - Fork 133
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
Pass: Passing Array descriptor by value #77
Changes from 2 commits
eafdd5a
f110eac
7e9bbd4
287298d
58148fb
3e4fbe4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,42 +18,126 @@ namespace LFortran { | |
* integer :: a(2, 3) | ||
*/ | ||
|
||
class ArrDimsPropagate : public ASR::StatementsFirstBaseWalkVisitor<ArrDimsPropagate> | ||
class ArrDimsPropagate : public ASR::StatementWalkVisitor<ArrDimsPropagate> | ||
{ | ||
private: | ||
Allocator &m_al; | ||
public: | ||
ArrDimsPropagate(Allocator &al) : m_al(al) { } | ||
ArrDimsPropagate(Allocator &al) : StatementWalkVisitor(al) { } | ||
|
||
void visit_FunctionCall(const ASR::FunctionCall_t &x) { | ||
ASR::Function_t *fn = ASR::down_cast<ASR::Function_t>(ASRUtils::symbol_get_past_external(x.m_name)); | ||
|
||
Vec<ASR::call_arg_t> new_args; | ||
new_args.reserve(al, x.n_args); | ||
|
||
for (size_t i = 0; i < x.n_args; i++) { | ||
if (ASR::is_a<ASR::Var_t>(*x.m_args[i].m_value) && ASRUtils::is_array(ASRUtils::expr_type(x.m_args[i].m_value))) { | ||
ASR::Variable_t* v = ASRUtils::EXPR2VAR(x.m_args[i].m_value); | ||
ASR::Variable_t *fn_param = ASRUtils::EXPR2VAR(fn->m_args[i]); | ||
ASR::dimension_t* m_dims; | ||
int n_dims = ASRUtils::extract_dimensions_from_ttype(fn_param->m_type, m_dims); | ||
if (n_dims > 0 && !m_dims[0].m_length && ASRUtils::check_equal_type(v->m_type, fn_param->m_type)) { | ||
fn_param->m_type = v->m_type; | ||
size_t n_dims = ASRUtils::extract_dimensions_from_ttype(v->m_type, m_dims); | ||
for (size_t j = 0; j < n_dims; j++) { | ||
auto type = ASR::make_Integer_t(al, v->base.base.loc, 4 /* FIXME: support other kinds */, nullptr, 0); | ||
auto dim = ASR::make_IntegerConstant_t(al, v->base.base.loc, j + 1, ASRUtils::TYPE(type)); | ||
auto call_array_size = ASR::make_ArraySize_t(al, v->base.base.loc, v->m_value, ASRUtils::EXPR(dim), | ||
ASRUtils::TYPE(type), nullptr); | ||
ASR::call_arg_t new_arg; | ||
new_arg.loc = v->base.base.loc; | ||
new_arg.m_value = ASRUtils::EXPR(call_array_size); | ||
new_args.push_back(al, new_arg); | ||
} | ||
} | ||
new_args.push_back(al, x.m_args[i]); | ||
} | ||
|
||
ASR::FunctionCall_t xx = const_cast<ASR::FunctionCall_t &>(x); | ||
xx.n_args = new_args.size(); | ||
xx.m_args = new_args.p; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Lines /home/ubaid/OpenSource/lfortran/src/libasr/pass/arr_dims_propagate.cpp:50:29: warning: variable ‘xx’ set but not used [-Wunused-but-set-variable]
50 | ASR::FunctionCall_t xx = const_cast<ASR::FunctionCall_t &>(x); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The solution here is to have a visitor that allows to modify the ASR tree in-place. We just have to do it. |
||
} | ||
|
||
void visit_SubroutineCall(const ASR::SubroutineCall_t &x) { | ||
ASR::Subroutine_t *sb = ASR::down_cast<ASR::Subroutine_t>(ASRUtils::symbol_get_past_external(x.m_name)); | ||
Vec<ASR::call_arg_t> new_args; | ||
new_args.reserve(al, x.n_args); | ||
|
||
for (size_t i = 0; i < x.n_args; i++) { | ||
if (ASR::is_a<ASR::Var_t>(*x.m_args[i].m_value) && ASRUtils::is_array(ASRUtils::expr_type(x.m_args[i].m_value))) { | ||
ASR::Variable_t* v = ASRUtils::EXPR2VAR(x.m_args[i].m_value); | ||
ASR::Variable_t *sb_param = ASRUtils::EXPR2VAR(sb->m_args[i]); | ||
ASR::dimension_t* m_dims; | ||
int n_dims = ASRUtils::extract_dimensions_from_ttype(sb_param->m_type, m_dims); | ||
if (n_dims > 0 && !m_dims[0].m_length && ASRUtils::check_equal_type(v->m_type, sb_param->m_type)) { | ||
sb_param->m_type = v->m_type; | ||
size_t n_dims = ASRUtils::extract_dimensions_from_ttype(v->m_type, m_dims); | ||
for (size_t j = 0; j < n_dims; j++) { | ||
auto type = ASR::make_Integer_t(al, v->base.base.loc, 4 /* FIXME: support other kinds */, nullptr, 0); | ||
auto dim = ASR::make_IntegerConstant_t(al, v->base.base.loc, j + 1, ASRUtils::TYPE(type)); | ||
auto call_array_size = ASR::make_ArraySize_t(al, v->base.base.loc, v->m_value, ASRUtils::EXPR(dim), | ||
ASRUtils::TYPE(type), nullptr); | ||
ASR::call_arg_t new_arg; | ||
new_arg.loc = v->base.base.loc; | ||
new_arg.m_value = ASRUtils::EXPR(call_array_size); | ||
new_args.push_back(al, new_arg); | ||
} | ||
} | ||
new_args.push_back(al, x.m_args[i]); | ||
} | ||
|
||
ASR::SubroutineCall_t xx = const_cast<ASR::SubroutineCall_t &>(x); | ||
xx.n_args = new_args.size(); | ||
xx.m_args = new_args.p; | ||
} | ||
|
||
void visit_Function(const ASR::Function_t &x) { | ||
Vec<ASR::expr_t*> params; | ||
params.reserve(al, x.n_args); | ||
|
||
for (size_t i = 0; i < x.n_args; i++) { | ||
ASR::Variable_t *arg = ASRUtils::EXPR2VAR(x.m_args[i]); | ||
if (ASRUtils::is_array(arg->m_type)) { | ||
|
||
ASR::dimension_t* m_dims; | ||
size_t n_dims = ASRUtils::extract_dimensions_from_ttype(arg->m_type, m_dims); | ||
|
||
for (size_t j = 0; j < n_dims; j++) { | ||
auto type = ASR::make_Integer_t(al, arg->base.base.loc, 4 /* FIXME: support other kinds */, nullptr, 0); | ||
auto variable = ASR::make_Variable_t(al, arg->base.base.loc, nullptr, s2c(al, "n" + std::string(arg->m_name) + std::to_string(j + 1)), | ||
ASR::intentType::In, nullptr, nullptr, ASR::storage_typeType::Default, | ||
ASRUtils::TYPE(type), ASR::abiType::Source, ASR::accessType::Public, | ||
ASR::presenceType::Required, false); | ||
auto var = ASR::make_Var_t(al, arg->base.base.loc, ASR::down_cast<ASR::symbol_t>(variable)); | ||
params.push_back(al, ASRUtils::EXPR(var)); | ||
m_dims[j].m_length = ASRUtils::EXPR(var); | ||
} | ||
} | ||
params.push_back(al, x.m_args[i]); | ||
} | ||
|
||
ASR::Function_t xx = const_cast<ASR::Function_t &>(x); | ||
xx.n_args = params.size(); | ||
xx.m_args = params.p; | ||
} | ||
|
||
void visit_Subroutine(const ASR::Subroutine_t &x) { | ||
Vec<ASR::expr_t*> params; | ||
params.reserve(al, x.n_args); | ||
|
||
for (size_t i = 0; i < x.n_args; i++) { | ||
ASR::Variable_t *arg = ASRUtils::EXPR2VAR(x.m_args[i]); | ||
if (ASRUtils::is_array(arg->m_type)) { | ||
|
||
ASR::dimension_t* m_dims; | ||
size_t n_dims = ASRUtils::extract_dimensions_from_ttype(arg->m_type, m_dims); | ||
|
||
for (size_t j = 0; j < n_dims; j++) { | ||
auto type = ASR::make_Integer_t(al, arg->base.base.loc, 4 /* FIXME: support other kinds */, nullptr, 0); | ||
auto variable = ASR::make_Variable_t(al, arg->base.base.loc, nullptr, s2c(al, "n" + std::string(arg->m_name) + std::to_string(j + 1)), | ||
ASR::intentType::In, nullptr, nullptr, ASR::storage_typeType::Default, | ||
ASRUtils::TYPE(type), ASR::abiType::Source, ASR::accessType::Public, | ||
ASR::presenceType::Required, false); | ||
auto var = ASR::make_Var_t(al, arg->base.base.loc, ASR::down_cast<ASR::symbol_t>(variable)); | ||
params.push_back(al, ASRUtils::EXPR(var)); | ||
m_dims[j].m_length = ASRUtils::EXPR(var); | ||
} | ||
} | ||
params.push_back(al, x.m_args[i]); | ||
} | ||
|
||
ASR::Subroutine_t xx = const_cast<ASR::Subroutine_t &>(x); | ||
xx.n_args = params.size(); | ||
xx.m_args = params.p; | ||
} | ||
}; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On line
21
, do we need to usePassUtils::PassVisitor
or isASR::StatementWalkVisitor
also fine/works?