Skip to content

Commit

Permalink
[ASR Pass] Symbolic: Simplify basic_unaryop to return SubroutineCall
Browse files Browse the repository at this point in the history
  • Loading branch information
Thirumalai-Shaktivel committed Nov 25, 2023
1 parent 3903658 commit 79066ee
Showing 1 changed file with 57 additions and 65 deletions.
122 changes: 57 additions & 65 deletions src/libasr/pass/replace_symbolic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,51 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
return ASRUtils::STMT(ASR::make_SubroutineCall_t(al, loc, basic_binop_sym,
basic_binop_sym, call_args.p, call_args.n, nullptr));
}

ASR::stmt_t *basic_unaryop(const Location &loc, const std::string &fn_name,
ASR::expr_t* target, ASR::expr_t* op_01) {
symbolic_dependencies.push_back(fn_name);
ASR::symbol_t *basic_unaryop_sym = current_scope->resolve_symbol(fn_name);
if ( !basic_unaryop_sym ) {
std::string header = "symengine/cwrapper.h";
SymbolTable* fn_symtab = al.make_new<SymbolTable>(current_scope->parent);

Vec<ASR::expr_t*> args; args.reserve(al, 2);
ASR::symbol_t* arg1 = ASR::down_cast<ASR::symbol_t>(ASR::make_Variable_t(
al, loc, fn_symtab, s2c(al, "x"), nullptr, 0, ASR::intentType::In,
nullptr, nullptr, ASR::storage_typeType::Default, ASRUtils::TYPE(ASR::make_CPtr_t(al, loc)),
nullptr, ASR::abiType::BindC, ASR::Public, ASR::presenceType::Required, true));
fn_symtab->add_symbol(s2c(al, "x"), arg1);
args.push_back(al, ASRUtils::EXPR(ASR::make_Var_t(al, loc, arg1)));
ASR::symbol_t* arg2 = ASR::down_cast<ASR::symbol_t>(ASR::make_Variable_t(
al, loc, fn_symtab, s2c(al, "y"), nullptr, 0, ASR::intentType::In,
nullptr, nullptr, ASR::storage_typeType::Default, ASRUtils::TYPE(ASR::make_CPtr_t(al, loc)),
nullptr, ASR::abiType::BindC, ASR::Public, ASR::presenceType::Required, true));
fn_symtab->add_symbol(s2c(al, "y"), arg2);
args.push_back(al, ASRUtils::EXPR(ASR::make_Var_t(al, loc, arg2)));

Vec<ASR::stmt_t*> body; body.reserve(al, 1);
Vec<char*> dep; dep.reserve(al, 1);
basic_unaryop_sym = ASR::down_cast<ASR::symbol_t>(ASRUtils::make_Function_t_util(al, loc,
fn_symtab, s2c(al, fn_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, fn_name), false, false, false,
false, false, nullptr, 0, false, false, false, s2c(al, header)));
current_scope->parent->add_symbol(s2c(al, fn_name), basic_unaryop_sym);
}

Vec<ASR::call_arg_t> call_args;
call_args.reserve(al, 2);
ASR::call_arg_t call_arg;
call_arg.loc = loc;
call_arg.m_value = target;
call_args.push_back(al, call_arg);
call_arg.m_value = handle_argument(al, loc, op_01);
call_args.push_back(al, call_arg);

return ASRUtils::STMT(ASR::make_SubroutineCall_t(al, loc, basic_unaryop_sym,
basic_unaryop_sym, call_args.p, call_args.n, nullptr));
}
/********************************** Utils *********************************/

void visit_Function(const ASR::Function_t &x) {
Expand Down Expand Up @@ -801,59 +846,6 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
}
}

void perform_symbolic_unary_operation(Allocator &al, const Location &loc, SymbolTable* module_scope,
const std::string& new_name, ASR::expr_t* value1, ASR::expr_t* value2) {
symbolic_dependencies.push_back(new_name);
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, 2);
ASR::symbol_t* arg1 = ASR::down_cast<ASR::symbol_t>(ASR::make_Variable_t(
al, loc, fn_symtab, s2c(al, "x"), nullptr, 0, ASR::intentType::In,
nullptr, nullptr, ASR::storage_typeType::Default, ASRUtils::TYPE(ASR::make_CPtr_t(al, loc)),
nullptr, ASR::abiType::BindC, ASR::Public, ASR::presenceType::Required, true));
fn_symtab->add_symbol(s2c(al, "x"), arg1);
args.push_back(al, ASRUtils::EXPR(ASR::make_Var_t(al, loc, arg1)));
ASR::symbol_t* arg2 = ASR::down_cast<ASR::symbol_t>(ASR::make_Variable_t(
al, loc, fn_symtab, s2c(al, "y"), nullptr, 0, ASR::intentType::In,
nullptr, nullptr, ASR::storage_typeType::Default, ASRUtils::TYPE(ASR::make_CPtr_t(al, loc)),
nullptr, ASR::abiType::BindC, ASR::Public, ASR::presenceType::Required, true));
fn_symtab->add_symbol(s2c(al, "y"), arg2);
args.push_back(al, ASRUtils::EXPR(ASR::make_Var_t(al, loc, arg2)));

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, 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(s2c(al, new_name), new_symbol);
}

ASR::symbol_t* func_sym = module_scope->get_symbol(new_name);
Vec<ASR::call_arg_t> call_args;
call_args.reserve(al, 2);
ASR::call_arg_t call_arg1, call_arg2;
call_arg1.loc = loc;
call_arg1.m_value = value1;
call_arg2.loc = loc;
call_arg2.m_value = value2;
call_args.push_back(al, call_arg1);
call_args.push_back(al, call_arg2);

ASR::stmt_t* stmt = ASRUtils::STMT(ASR::make_SubroutineCall_t(al, loc, func_sym,
func_sym, call_args.p, call_args.n, nullptr));
pass_result.push_back(al, stmt);
}

ASR::expr_t* handle_argument(Allocator &al, const Location &loc, ASR::expr_t* arg) {
if (ASR::is_a<ASR::Var_t>(*arg)) {
return arg;
Expand All @@ -868,12 +860,6 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
return ASRUtils::EXPR(ASR::make_Var_t(al, loc, var_sym));
}

void process_unary_operator(Allocator &al, const Location &loc, ASR::IntrinsicScalarFunction_t* x, SymbolTable* module_scope,
const std::string& new_name, ASR::expr_t* target) {
ASR::expr_t* value1 = handle_argument(al, loc, x->m_args[0]);
perform_symbolic_unary_operation(al, loc, module_scope, new_name, target, value1);
}

void process_intrinsic_function(Allocator &al, const Location &loc, ASR::IntrinsicScalarFunction_t* x, SymbolTable* module_scope,
ASR::expr_t* target){
int64_t intrinsic_id = x->m_intrinsic_id;
Expand Down Expand Up @@ -921,27 +907,33 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
break;
}
case LCompilers::ASRUtils::IntrinsicScalarFunctions::SymbolicSin: {
process_unary_operator(al, loc, x, module_scope, "basic_sin", target);
pass_result.push_back(al, basic_unaryop(loc, "basic_sin", target,
x->m_args[0]));
break;
}
case LCompilers::ASRUtils::IntrinsicScalarFunctions::SymbolicCos: {
process_unary_operator(al, loc, x, module_scope, "basic_cos", target);
pass_result.push_back(al, basic_unaryop(loc, "basic_cos", target,
x->m_args[0]));
break;
}
case LCompilers::ASRUtils::IntrinsicScalarFunctions::SymbolicLog: {
process_unary_operator(al, loc, x, module_scope, "basic_log", target);
pass_result.push_back(al, basic_unaryop(loc, "basic_log", target,
x->m_args[0]));
break;
}
case LCompilers::ASRUtils::IntrinsicScalarFunctions::SymbolicExp: {
process_unary_operator(al, loc, x, module_scope, "basic_exp", target);
pass_result.push_back(al, basic_unaryop(loc, "basic_exp", target,
x->m_args[0]));
break;
}
case LCompilers::ASRUtils::IntrinsicScalarFunctions::SymbolicAbs: {
process_unary_operator(al, loc, x, module_scope, "basic_abs", target);
pass_result.push_back(al, basic_unaryop(loc, "basic_abs", target,
x->m_args[0]));
break;
}
case LCompilers::ASRUtils::IntrinsicScalarFunctions::SymbolicExpand: {
process_unary_operator(al, loc, x, module_scope, "basic_expand", target);
pass_result.push_back(al, basic_unaryop(loc, "basic_expand", target,
x->m_args[0]));
break;
}
case LCompilers::ASRUtils::IntrinsicScalarFunctions::SymbolicGetArgument: {
Expand Down

0 comments on commit 79066ee

Please sign in to comment.