Skip to content
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
16 changes: 16 additions & 0 deletions src/libasr/pass/intrinsic_function_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ enum class IntrinsicScalarFunctions : int64_t {
SymbolicAddQ,
SymbolicMulQ,
SymbolicPowQ,
SymbolicLogQ,
SymbolicExpQ,
// ...
};

Expand Down Expand Up @@ -146,6 +148,8 @@ inline std::string get_intrinsic_name(int x) {
INTRINSIC_NAME_CASE(SymbolicAddQ)
INTRINSIC_NAME_CASE(SymbolicMulQ)
INTRINSIC_NAME_CASE(SymbolicPowQ)
INTRINSIC_NAME_CASE(SymbolicLogQ)
INTRINSIC_NAME_CASE(SymbolicExpQ)
default : {
throw LCompilersException("pickle: intrinsic_id not implemented");
}
Expand Down Expand Up @@ -3146,6 +3150,8 @@ namespace X {
create_symbolic_query_macro(SymbolicAddQ)
create_symbolic_query_macro(SymbolicMulQ)
create_symbolic_query_macro(SymbolicPowQ)
create_symbolic_query_macro(SymbolicLogQ)
create_symbolic_query_macro(SymbolicExpQ)


#define create_symbolic_unary_macro(X) \
Expand Down Expand Up @@ -3307,6 +3313,10 @@ namespace IntrinsicScalarFunctionRegistry {
{nullptr, &SymbolicMulQ::verify_args}},
{static_cast<int64_t>(IntrinsicScalarFunctions::SymbolicPowQ),
{nullptr, &SymbolicPowQ::verify_args}},
{static_cast<int64_t>(IntrinsicScalarFunctions::SymbolicLogQ),
{nullptr, &SymbolicLogQ::verify_args}},
{static_cast<int64_t>(IntrinsicScalarFunctions::SymbolicExpQ),
{nullptr, &SymbolicExpQ::verify_args}},
};

static const std::map<int64_t, std::string>& intrinsic_function_id_to_name = {
Expand Down Expand Up @@ -3417,6 +3427,10 @@ namespace IntrinsicScalarFunctionRegistry {
"SymbolicMulQ"},
{static_cast<int64_t>(IntrinsicScalarFunctions::SymbolicPowQ),
"SymbolicPowQ"},
{static_cast<int64_t>(IntrinsicScalarFunctions::SymbolicLogQ),
"SymbolicLogQ"},
{static_cast<int64_t>(IntrinsicScalarFunctions::SymbolicExpQ),
"SymbolicExpQ"},
};


Expand Down Expand Up @@ -3475,6 +3489,8 @@ namespace IntrinsicScalarFunctionRegistry {
{"AddQ", {&SymbolicAddQ::create_SymbolicAddQ, &SymbolicAddQ::eval_SymbolicAddQ}},
{"MulQ", {&SymbolicMulQ::create_SymbolicMulQ, &SymbolicMulQ::eval_SymbolicMulQ}},
{"PowQ", {&SymbolicPowQ::create_SymbolicPowQ, &SymbolicPowQ::eval_SymbolicPowQ}},
{"LogQ", {&SymbolicLogQ::create_SymbolicLogQ, &SymbolicLogQ::eval_SymbolicLogQ}},
{"ExpQ", {&SymbolicExpQ::create_SymbolicExpQ, &SymbolicExpQ::eval_SymbolicExpQ}},
};

static inline bool is_intrinsic_function(const std::string& name) {
Expand Down
29 changes: 29 additions & 0 deletions src/libasr/pass/replace_symbolic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,28 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
ASRUtils::TYPE(ASR::make_Logical_t(al, loc, 4)), nullptr));
break;
}
case LCompilers::ASRUtils::IntrinsicScalarFunctions::SymbolicLogQ: {
ASR::symbol_t* basic_get_type_sym = declare_basic_get_type_function(al, loc, module_scope);
ASR::expr_t* value1 = handle_argument(al, loc, intrinsic_func->m_args[0]);
Vec<ASR::call_arg_t> call_args;
call_args.reserve(al, 1);
ASR::call_arg_t call_arg;
call_arg.loc = loc;
call_arg.m_value = value1;
call_args.push_back(al, call_arg);
ASR::expr_t* function_call = ASRUtils::EXPR(ASRUtils::make_FunctionCall_t_util(al, loc,
basic_get_type_sym, basic_get_type_sym, call_args.p, call_args.n,
ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 4)), nullptr, nullptr));
// Using 29 as the right value of the IntegerCompare node as it represents SYMENGINE_LOG through SYMENGINE_ENUM
return ASRUtils::EXPR(ASR::make_IntegerCompare_t(al, loc, function_call, ASR::cmpopType::Eq,
ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, loc, 29, ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 4)))),
ASRUtils::TYPE(ASR::make_Logical_t(al, loc, 4)), nullptr));
break;
}
case LCompilers::ASRUtils::IntrinsicScalarFunctions::SymbolicExpQ: {
// TODO
break;
}
default: {
throw LCompilersException("IntrinsicFunction: `"
+ ASRUtils::get_intrinsic_name(intrinsic_id)
Expand Down Expand Up @@ -1437,6 +1459,13 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi

ASR::stmt_t *assert_stmt = ASRUtils::STMT(ASR::make_Assert_t(al, x.base.base.loc, test, x.m_msg));
pass_result.push_back(al, assert_stmt);
} else if (ASR::is_a<ASR::IntrinsicScalarFunction_t>(*x.m_test)) {
ASR::IntrinsicScalarFunction_t* intrinsic_func = ASR::down_cast<ASR::IntrinsicScalarFunction_t>(x.m_test);
if (intrinsic_func->m_type->type == ASR::ttypeType::Logical) {
ASR::expr_t* test = process_attributes(al, x.base.base.loc, x.m_test, module_scope);
ASR::stmt_t *assert_stmt = ASRUtils::STMT(ASR::make_Assert_t(al, x.base.base.loc, test, x.m_msg));
pass_result.push_back(al, assert_stmt);
}
}
}
};
Expand Down
6 changes: 6 additions & 0 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6040,6 +6040,12 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
} else if (symbolic_type == "Pow") {
tmp = attr_handler.eval_symbolic_is_Pow(se, al, x.base.base.loc, args, diag);
return;
} else if (symbolic_type == "log") {
tmp = attr_handler.eval_symbolic_is_log(se, al, x.base.base.loc, args, diag);
return;
} else if (symbolic_type == "exp") {
tmp = attr_handler.eval_symbolic_is_exp(se, al, x.base.base.loc, args, diag);
return;
} else {
throw SemanticError(symbolic_type + " symbolic type not supported yet", x.base.base.loc);
}
Expand Down
28 changes: 28 additions & 0 deletions src/lpython/semantics/python_attribute_eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,34 @@ struct AttributeHandler {
{ throw SemanticError(msg, loc); });
}

static ASR::asr_t* eval_symbolic_is_log(ASR::expr_t *s, Allocator &al, const Location &loc,
Vec<ASR::expr_t*> &args, diag::Diagnostics &/*diag*/) {
Vec<ASR::expr_t*> args_with_list;
args_with_list.reserve(al, args.size() + 1);
args_with_list.push_back(al, s);
for(size_t i = 0; i < args.size(); i++) {
args_with_list.push_back(al, args[i]);
}
ASRUtils::create_intrinsic_function create_function =
ASRUtils::IntrinsicScalarFunctionRegistry::get_create_function("LogQ");
return create_function(al, loc, args_with_list, [&](const std::string &msg, const Location &loc)
{ throw SemanticError(msg, loc); });
}

static ASR::asr_t* eval_symbolic_is_exp(ASR::expr_t *s, Allocator &al, const Location &loc,
Vec<ASR::expr_t*> &args, diag::Diagnostics &/*diag*/) {
Vec<ASR::expr_t*> args_with_list;
args_with_list.reserve(al, args.size() + 1);
args_with_list.push_back(al, s);
for(size_t i = 0; i < args.size(); i++) {
args_with_list.push_back(al, args[i]);
}
ASRUtils::create_intrinsic_function create_function =
ASRUtils::IntrinsicScalarFunctionRegistry::get_create_function("ExpQ");
return create_function(al, loc, args_with_list, [&](const std::string &msg, const Location &loc)
{ throw SemanticError(msg, loc); });
}

}; // AttributeHandler

} // namespace LCompilers::LPython
Expand Down