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

Pass: Passing Array descriptor by value #77

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 2 additions & 30 deletions grammar/asdl_cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,33 +501,6 @@ def visitField(self, field):
self.emit( "this->visit_symbol(*a.second);", 3)
self.emit("}", 2)

class StatementsFirstWalkVisitorVisitor(ASTWalkVisitorVisitor, ASDLVisitor):

def visitModule(self, mod):
self.emit("/" + "*"*78 + "/")
self.emit("// Statements First Visitor base class")
self.emit("")
self.emit("template <class Derived>")
self.emit("class StatementsFirstBaseWalkVisitor : public BaseVisitor<Derived>")
self.emit("{")
self.emit("private:")
self.emit(" Derived& self() { return static_cast<Derived&>(*this); }")
self.emit("public:")
super(ASTWalkVisitorVisitor, self).visitModule(mod)
self.emit("};")

def make_visitor(self, name, fields):
self.emit("void visit_%s(const %s_t &x) {" % (name, name), 1)
self.used = False
have_body = False
for field in fields[::-1]:
self.visitField(field)
if not self.used:
# Note: a better solution would be to change `&x` to `& /* x */`
# above, but we would need to change emit to return a string.
self.emit("if ((bool&)x) { } // Suppress unused warning", 2)
self.emit("}", 1)

# This class generates a visitor that prints the tree structure of AST/ASR
class TreeVisitorVisitor(ASDLVisitor):

Expand Down Expand Up @@ -2129,9 +2102,8 @@ def add_masks(fields, node):

visitors = [ASTNodeVisitor0, ASTNodeVisitor1, ASTNodeVisitor,
ASTVisitorVisitor1, ASTVisitorVisitor1b, ASTVisitorVisitor2,
ASTWalkVisitorVisitor, TreeVisitorVisitor, PickleVisitorVisitor,
StatementsFirstWalkVisitorVisitor, SerializationVisitorVisitor,
DeserializationVisitorVisitor]
ASTWalkVisitorVisitor, PickleVisitorVisitor,
SerializationVisitorVisitor, DeserializationVisitorVisitor]


def main(argv):
Expand Down
6 changes: 3 additions & 3 deletions src/libasr/codegen/asr_to_wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <libasr/exception.h>
#include <libasr/asr_utils.h>

// #include <lfortran/pickle.h>
#include <lfortran/pickle.h>

namespace LFortran {

Expand Down Expand Up @@ -1591,8 +1591,8 @@ Result<Vec<uint8_t>> asr_to_wasm_bytes_stream(ASR::TranslationUnit_t &asr, Alloc
pass_replace_do_loops(al, asr);
pass_propagate_arr_dims(al, asr);

// std::cout << pickle(asr, true /* use colors */, true /* indent */,
// true /* with_intrinsic_modules */) << std::endl;
std::cout << pickle(asr, true /* use colors */, true /* indent */,
true /* with_intrinsic_modules */) << std::endl;
try {
v.visit_asr((ASR::asr_t &)asr);
} catch (const CodeGenError &e) {
Expand Down
112 changes: 98 additions & 14 deletions src/libasr/pass/arr_dims_propagate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,126 @@ namespace LFortran {
* integer :: a(2, 3)
*/

class ArrDimsPropagate : public ASR::StatementsFirstBaseWalkVisitor<ArrDimsPropagate>
class ArrDimsPropagate : public ASR::StatementWalkVisitor<ArrDimsPropagate>
Copy link
Member Author

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 use PassUtils::PassVisitor or is ASR::StatementWalkVisitor also fine/works?

{
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;
Copy link
Member Author

Choose a reason for hiding this comment

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

(Lines 50 to 52) Since, the arguments passed to visit_FunctionCall() (and others) are const, it was no possible to update them. So, I used a const_cast<> to allow us to update values but this leads to warnings as follows during build:

/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);

Copy link
Contributor

Choose a reason for hiding this comment

The 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;
}
};

Expand Down