Skip to content

WIP: Implementing Symbolic ASR pass #2200

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

Closed
wants to merge 12 commits into from
Closed
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 src/libasr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ set(SRC
pass/unused_functions.cpp
pass/flip_sign.cpp
pass/div_to_mul.cpp
pass/replace_symbolic.cpp
pass/intrinsic_function.cpp
pass/fma.cpp
pass/loop_vectorise.cpp
Expand Down
1 change: 1 addition & 0 deletions src/libasr/gen_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"replace_implied_do_loops",
"replace_init_expr",
"inline_function_calls",
"replace_symbolic",
"replace_intrinsic_function",
"loop_unroll",
"loop_vectorise",
Expand Down
3 changes: 3 additions & 0 deletions src/libasr/pass/pass_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <libasr/pass/replace_arr_slice.h>
#include <libasr/pass/replace_flip_sign.h>
#include <libasr/pass/replace_div_to_mul.h>
#include <libasr/pass/replace_symbolic.h>
#include <libasr/pass/replace_intrinsic_function.h>
#include <libasr/pass/replace_fma.h>
#include <libasr/pass/loop_unroll.h>
Expand Down Expand Up @@ -71,6 +72,7 @@ namespace LCompilers {
{"global_stmts", &pass_wrap_global_stmts},
{"implied_do_loops", &pass_replace_implied_do_loops},
{"array_op", &pass_replace_array_op},
{"symbolic", &pass_replace_symbolic},
{"intrinsic_function", &pass_replace_intrinsic_function},
{"arr_slice", &pass_replace_arr_slice},
{"print_arr", &pass_replace_print_arr},
Expand Down Expand Up @@ -203,6 +205,7 @@ namespace LCompilers {
"subroutine_from_function",
"where",
"array_op",
"symbolic",
"intrinsic_function",
"array_op",
"pass_array_by_data",
Expand Down
165 changes: 165 additions & 0 deletions src/libasr/pass/replace_symbolic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#include <libasr/asr.h>
#include <libasr/containers.h>
#include <libasr/exception.h>
#include <libasr/asr_utils.h>
#include <libasr/asr_verify.h>
#include <libasr/pass/replace_symbolic.h>
#include <libasr/pass/pass_utils.h>

#include <vector>


namespace LCompilers {

using ASR::down_cast;
using ASR::is_a;


class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisitor>
{
public:
ReplaceSymbolicVisitor(Allocator &al_) :
PassVisitor(al_, nullptr) {
Comment on lines +21 to +22
Copy link
Collaborator

@Thirumalai-Shaktivel Thirumalai-Shaktivel Jul 26, 2023

Choose a reason for hiding this comment

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

If this: #2200 (comment) doesn't work, pass m_global_scope as an argument to ReplaceSymbolicVisitor and pass that symtab to PassVisitor instead of nullptr.

pass_result.reserve(al, 1);
}

bool symbolic_replaces_with_CPtr_Function = false;

void visit_Function(const ASR::Function_t &x) {
// FIXME: this is a hack, we need to pass in a non-const `x`,
// which requires to generate a TransformVisitor.
ASR::Function_t &xx = const_cast<ASR::Function_t&>(x);
SymbolTable* current_scope_copy = this->current_scope;
this->current_scope = xx.m_symtab;
for (auto &item : x.m_symtab->get_scope()) {
if (ASR::is_a<ASR::Variable_t>(*item.second)) {
ASR::Variable_t *s = ASR::down_cast<ASR::Variable_t>(item.second);
this->visit_Variable(*s);
}
}
transform_stmts(xx.m_body, xx.n_body);

// Add "basic_new_stack" to dependencies if needed
if (symbolic_replaces_with_CPtr_Function) {
SetChar function_dependencies;
function_dependencies.n = 0;
function_dependencies.reserve(al, 1);
for( size_t i = 0; i < xx.n_dependencies; i++ ) {
function_dependencies.push_back(al, xx.m_dependencies[i]);
}
function_dependencies.push_back(al, s2c(al, "basic_new_stack"));
xx.n_dependencies = function_dependencies.size();
xx.m_dependencies = function_dependencies.p;
}
this->current_scope = current_scope_copy;
}

void visit_Variable(const ASR::Variable_t& x) {
ASR::Variable_t& xx = const_cast<ASR::Variable_t&>(x);
SymbolTable* current_scope_copy = current_scope;
current_scope = xx.m_parent_symtab;
Comment on lines +59 to +60
Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove this, as we already assign it in the Function visitor!

if (xx.m_type->type == ASR::ttypeType::SymbolicExpression) {
SymbolTable* module_scope = current_scope->parent;
symbolic_replaces_with_CPtr_Function = true;
std::string var_name = xx.m_name;
std::string placeholder = "_" + std::string(var_name);

// defining CPtr variable
ASR::ttype_t *type1 = ASRUtils::TYPE(ASR::make_CPtr_t(al, xx.base.base.loc));
xx.m_type = type1;

ASR::ttype_t *type2 = ASRUtils::TYPE(ASR::make_Integer_t(al, xx.base.base.loc, 8));
ASR::symbol_t* sym2 = ASR::down_cast<ASR::symbol_t>(
ASR::make_Variable_t(al, xx.base.base.loc, current_scope,
s2c(al, placeholder), nullptr, 0,
xx.m_intent, nullptr,
nullptr, xx.m_storage,
type2, nullptr, xx.m_abi,
xx.m_access, xx.m_presence,
xx.m_value_attr));

current_scope->add_symbol(s2c(al, placeholder), sym2);

std::string new_name = "basic_new_stack";
if (!module_scope->get_symbol(new_name)) {
std::string header = "symengine/cwrapper.h";
SymbolTable *fn_symtab = al.make_new<SymbolTable>(module_scope);

Vec<ASR::expr_t*> args;
{
args.reserve(al, 1);
ASR::symbol_t *arg = ASR::down_cast<ASR::symbol_t>(ASR::make_Variable_t(
al, xx.base.base.loc, fn_symtab, s2c(al, "x"), nullptr, 0, ASR::intentType::In,
nullptr, nullptr, ASR::storage_typeType::Default, type1, nullptr,
ASR::abiType::BindC, ASR::Public, ASR::presenceType::Required, true));
fn_symtab->add_symbol(s2c(al, "x"), arg);
args.push_back(al, ASRUtils::EXPR(ASR::make_Var_t(al, xx.base.base.loc, arg)));
}

Vec<ASR::stmt_t*> body;
body.reserve(al, 1);

Vec<char *> dep;
dep.reserve(al, 1);

ASR::asr_t* new_subrout = ASRUtils::make_Function_t_util(al, xx.base.base.loc,
fn_symtab, s2c(al, new_name), dep.p, dep.n, args.p, args.n, body.p, body.n,
nullptr, ASR::abiType::BindC, ASR::accessType::Public,
ASR::deftypeType::Interface, s2c(al, new_name), false, false, false,
false, false, nullptr, 0, false, false, false, s2c(al, header));
ASR::symbol_t *new_symbol = ASR::down_cast<ASR::symbol_t>(new_subrout);
module_scope->add_symbol(new_name, new_symbol);
}

ASR::symbol_t* var_sym = current_scope->get_symbol(var_name);
ASR::symbol_t* placeholder_sym = current_scope->get_symbol(placeholder);
ASR::expr_t* target1 = ASRUtils::EXPR(ASR::make_Var_t(al, xx.base.base.loc, placeholder_sym));
ASR::expr_t* target2 = ASRUtils::EXPR(ASR::make_Var_t(al, xx.base.base.loc, var_sym));

// statement 1
ASR::expr_t* value1 = ASRUtils::EXPR(ASR::make_Cast_t(al, xx.base.base.loc,
ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, xx.base.base.loc, 0,
ASRUtils::TYPE(ASR::make_Integer_t(al, xx.base.base.loc, 4)))),
(ASR::cast_kindType)ASR::cast_kindType::IntegerToInteger, type2,
ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, xx.base.base.loc, 0, type2))));

// statement 2
ASR::expr_t* value2 = ASRUtils::EXPR(ASR::make_PointerNullConstant_t(al, xx.base.base.loc, type1));

// statement 3
ASR::expr_t* get_pointer_node = ASRUtils::EXPR(ASR::make_GetPointer_t(al, xx.base.base.loc,
target1, ASRUtils::TYPE(ASR::make_Pointer_t(al, xx.base.base.loc, type2)), nullptr));
ASR::expr_t* value3 = ASRUtils::EXPR(ASR::make_PointerToCPtr_t(al, xx.base.base.loc, get_pointer_node,
type1, nullptr));

// statement 4
ASR::symbol_t* basic_new_stack_sym = module_scope->get_symbol(new_name);
Vec<ASR::call_arg_t> call_args;
call_args.reserve(al, 1);
ASR::call_arg_t call_arg;
call_arg.loc = xx.base.base.loc;
call_arg.m_value = target2;
call_args.push_back(al, call_arg);

// defining the assignment statement
ASR::stmt_t* stmt1 = ASRUtils::STMT(ASR::make_Assignment_t(al, xx.base.base.loc, target1, value1, nullptr));
ASR::stmt_t* stmt2 = ASRUtils::STMT(ASR::make_Assignment_t(al, xx.base.base.loc, target2, value2, nullptr));
ASR::stmt_t* stmt3 = ASRUtils::STMT(ASR::make_Assignment_t(al, xx.base.base.loc, target2, value3, nullptr));
ASR::stmt_t* stmt4 = ASRUtils::STMT(ASR::make_SubroutineCall_t(al, xx.base.base.loc, basic_new_stack_sym,
basic_new_stack_sym, call_args.p, call_args.n, nullptr));

pass_result.push_back(al, stmt1);
pass_result.push_back(al, stmt2);
pass_result.push_back(al, stmt3);
pass_result.push_back(al, stmt4);
}
current_scope = current_scope_copy;
}
};

void pass_replace_symbolic(Allocator &al, ASR::TranslationUnit_t &unit,
const LCompilers::PassOptions& /*pass_options*/) {
ReplaceSymbolicVisitor v(al);
v.visit_TranslationUnit(unit);
}
} // namespace LCompilers
14 changes: 14 additions & 0 deletions src/libasr/pass/replace_symbolic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef LIBASR_PASS_REPLACE_SYMBOLIC_H
#define LIBASR_PASS_REPLACE_SYMBOLIC_H

#include <libasr/asr.h>
#include <libasr/utils.h>

namespace LCompilers {

void pass_replace_symbolic(Allocator &al, ASR::TranslationUnit_t &unit,
const PassOptions &pass_options);

} // namespace LCompilers

#endif // LIBASR_PASS_REPLACE_SYMBOLIC_H