Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Added support for subs attribute #2695

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

Merged
merged 1 commit into from
May 10, 2024
Merged
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
8 changes: 8 additions & 0 deletions integration_tests/symbolics_05.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,12 @@ def test_operations():
assert(c.args[0] == x)
assert(d.args[0] == x)

# test subs
b1: S = b.subs(x, y)
b1 = b1.subs(z, y)
assert(a.subs(x, y) == S(4)*y**S(2))
assert(b1 == S(27)*y**S(3))
assert(c.subs(x, y) == sin(y))
assert(d.subs(x, z) == cos(z))

test_operations()
6 changes: 6 additions & 0 deletions src/libasr/pass/intrinsic_function_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ inline std::string get_intrinsic_name(int x) {
INTRINSIC_NAME_CASE(SymbolicInteger)
INTRINSIC_NAME_CASE(SymbolicDiff)
INTRINSIC_NAME_CASE(SymbolicExpand)
INTRINSIC_NAME_CASE(SymbolicSubs)
INTRINSIC_NAME_CASE(SymbolicSin)
INTRINSIC_NAME_CASE(SymbolicCos)
INTRINSIC_NAME_CASE(SymbolicLog)
Expand Down Expand Up @@ -435,6 +436,8 @@ namespace IntrinsicElementalFunctionRegistry {
{nullptr, &SymbolicDiff::verify_args}},
{static_cast<int64_t>(IntrinsicElementalFunctions::SymbolicExpand),
{nullptr, &SymbolicExpand::verify_args}},
{static_cast<int64_t>(IntrinsicElementalFunctions::SymbolicSubs),
{nullptr, &SymbolicSubs::verify_args}},
{static_cast<int64_t>(IntrinsicElementalFunctions::SymbolicSin),
{nullptr, &SymbolicSin::verify_args}},
{static_cast<int64_t>(IntrinsicElementalFunctions::SymbolicCos),
Expand Down Expand Up @@ -724,6 +727,8 @@ namespace IntrinsicElementalFunctionRegistry {
"SymbolicDiff"},
{static_cast<int64_t>(IntrinsicElementalFunctions::SymbolicExpand),
"SymbolicExpand"},
{static_cast<int64_t>(IntrinsicElementalFunctions::SymbolicSubs),
"SymbolicSubs"},
{static_cast<int64_t>(IntrinsicElementalFunctions::SymbolicSin),
"SymbolicSin"},
{static_cast<int64_t>(IntrinsicElementalFunctions::SymbolicCos),
Expand Down Expand Up @@ -889,6 +894,7 @@ namespace IntrinsicElementalFunctionRegistry {
{"SymbolicInteger", {&SymbolicInteger::create_SymbolicInteger, &SymbolicInteger::eval_SymbolicInteger}},
{"diff", {&SymbolicDiff::create_SymbolicDiff, &SymbolicDiff::eval_SymbolicDiff}},
{"expand", {&SymbolicExpand::create_SymbolicExpand, &SymbolicExpand::eval_SymbolicExpand}},
{"subs", {&SymbolicSubs::create_SymbolicSubs, &SymbolicSubs::eval_SymbolicSubs}},
{"SymbolicSin", {&SymbolicSin::create_SymbolicSin, &SymbolicSin::eval_SymbolicSin}},
{"SymbolicCos", {&SymbolicCos::create_SymbolicCos, &SymbolicCos::eval_SymbolicCos}},
{"SymbolicLog", {&SymbolicLog::create_SymbolicLog, &SymbolicLog::eval_SymbolicLog}},
Expand Down
59 changes: 59 additions & 0 deletions src/libasr/pass/intrinsic_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ enum class IntrinsicElementalFunctions : int64_t {
SymbolicInteger,
SymbolicDiff,
SymbolicExpand,
SymbolicSubs,
SymbolicSin,
SymbolicCos,
SymbolicLog,
Expand Down Expand Up @@ -5651,6 +5652,64 @@ create_symbolic_binary_macro(SymbolicDiv)
create_symbolic_binary_macro(SymbolicPow)
create_symbolic_binary_macro(SymbolicDiff)

#define create_symbolic_ternary_macro(X) \
namespace X{ \
static inline void verify_args(const ASR::IntrinsicElementalFunction_t& x, \
diag::Diagnostics& diagnostics) { \
ASRUtils::require_impl(x.n_args == 3, "Intrinsic function `"#X"` accepts" \
"exactly 3 arguments", x.base.base.loc, diagnostics); \
\
ASR::ttype_t* arg1_type = ASRUtils::expr_type(x.m_args[0]); \
ASR::ttype_t* arg2_type = ASRUtils::expr_type(x.m_args[1]); \
ASR::ttype_t* arg3_type = ASRUtils::expr_type(x.m_args[2]); \
\
ASRUtils::require_impl(ASR::is_a<ASR::SymbolicExpression_t>(*arg1_type) && \
ASR::is_a<ASR::SymbolicExpression_t>(*arg2_type) && \
ASR::is_a<ASR::SymbolicExpression_t>(*arg3_type), \
"All arguments of `"#X"` must be of type SymbolicExpression", \
x.base.base.loc, diagnostics); \
} \
\
static inline ASR::expr_t* eval_##X(Allocator &/*al*/, const Location &/*loc*/, \
ASR::ttype_t *, Vec<ASR::expr_t*> &/*args*/, diag::Diagnostics& /*diag*/) { \
/*TODO*/ \
return nullptr; \
} \
\
static inline ASR::asr_t* create_##X(Allocator& al, const Location& loc, \
Vec<ASR::expr_t*>& args, \
diag::Diagnostics& diag) { \
if (args.size() != 3) { \
append_error(diag, "Intrinsic function `"#X"` accepts exactly 3 arguments", \
loc); \
return nullptr; \
} \
\
for (size_t i = 0; i < args.size(); i++) { \
ASR::ttype_t* argtype = ASRUtils::expr_type(args[i]); \
if(!ASR::is_a<ASR::SymbolicExpression_t>(*argtype)) { \
append_error(diag, \
"Arguments of `"#X"` function must be of type SymbolicExpression", \
args[i]->base.loc); \
return nullptr; \
} \
} \
\
Vec<ASR::expr_t*> arg_values; \
arg_values.reserve(al, args.size()); \
for( size_t i = 0; i < args.size(); i++ ) { \
arg_values.push_back(al, ASRUtils::expr_value(args[i])); \
} \
ASR::ttype_t *to_type = ASRUtils::TYPE(ASR::make_SymbolicExpression_t(al, loc)); \
ASR::expr_t* compile_time_value = eval_##X(al, loc, to_type, arg_values, diag); \
return ASR::make_IntrinsicElementalFunction_t(al, loc, \
static_cast<int64_t>(IntrinsicElementalFunctions::X), \
args.p, args.size(), 0, to_type, compile_time_value); \
} \
} // namespace X

create_symbolic_ternary_macro(SymbolicSubs)

#define create_symbolic_constants_macro(X) \
namespace X { \
static inline void verify_args(const ASR::IntrinsicElementalFunction_t& x, \
Expand Down
17 changes: 17 additions & 0 deletions src/libasr/pass/replace_symbolic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
"basic_const_"#name, target)); \
break; }

#define BASIC_TERNARYOP(SYM, name) \
case LCompilers::ASRUtils::IntrinsicElementalFunctions::Symbolic##SYM: { \
pass_result.push_back(al, basic_ternaryop(loc, "basic_"#name, target, \
x->m_args[0], x->m_args[1], x->m_args[2])); \
break; }

#define BASIC_BINOP(SYM, name) \
case LCompilers::ASRUtils::IntrinsicElementalFunctions::Symbolic##SYM: { \
pass_result.push_back(al, basic_binop(loc, "basic_"#name, target, \
Expand Down Expand Up @@ -241,6 +247,16 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
return SubroutineCall(loc, basic_const_sym, {value});
}

ASR::stmt_t *basic_ternaryop(const Location &loc, const std::string &fn_name,
ASR::expr_t* target, ASR::expr_t* op_01, ASR::expr_t* op_02, ASR::expr_t* op_03) {
ASR::ttype_t *cptr_type = ASRUtils::TYPE(ASR::make_CPtr_t(al, loc));
ASR::symbol_t* basic_ternaryop_sym = create_bindc_function(loc, fn_name,
{cptr_type, cptr_type, cptr_type, cptr_type});
return SubroutineCall(loc, basic_ternaryop_sym, {target,
handle_argument(al, loc, op_01), handle_argument(al, loc, op_02),
handle_argument(al, loc, op_03)});
}

ASR::stmt_t *basic_binop(const Location &loc, const std::string &fn_name,
ASR::expr_t* target, ASR::expr_t* op_01, ASR::expr_t* op_02) {
ASR::ttype_t *cptr_type = ASRUtils::TYPE(ASR::make_CPtr_t(al, loc));
Expand Down Expand Up @@ -462,6 +478,7 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
BASIC_UNARYOP(Exp, exp)
BASIC_UNARYOP(Abs, abs)
BASIC_UNARYOP(Expand, expand)
BASIC_TERNARYOP(Subs, subs2)
case LCompilers::ASRUtils::IntrinsicElementalFunctions::SymbolicGetArgument: {
// Define necessary function symbols
ASR::expr_t* value1 = handle_argument(al, loc, x->m_args[0]);
Expand Down
8 changes: 4 additions & 4 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7565,7 +7565,7 @@ we will have to use something else.
} else {
st = current_scope->resolve_symbol(mod_name);
std::set<std::string> symbolic_attributes = {
"diff", "expand", "has"
"diff", "expand", "has", "subs"
};
std::set<std::string> symbolic_constants = {
"pi", "E", "oo"
Expand Down Expand Up @@ -7640,7 +7640,7 @@ we will have to use something else.
} else if (AST::is_a<AST::BinOp_t>(*at->m_value)) {
AST::BinOp_t* bop = AST::down_cast<AST::BinOp_t>(at->m_value);
std::set<std::string> symbolic_attributes = {
"diff", "expand", "has"
"diff", "expand", "has", "subs"
};
if (symbolic_attributes.find(at->m_attr) != symbolic_attributes.end()){
switch (bop->m_op) {
Expand Down Expand Up @@ -7687,7 +7687,7 @@ we will have to use something else.
} else if (AST::is_a<AST::Call_t>(*at->m_value)) {
AST::Call_t* call = AST::down_cast<AST::Call_t>(at->m_value);
std::set<std::string> symbolic_attributes = {
"diff", "expand", "has"
"diff", "expand", "has", "subs"
};
if (symbolic_attributes.find(at->m_attr) != symbolic_attributes.end()){
std::set<std::string> symbolic_functions = {
Expand Down Expand Up @@ -7819,7 +7819,7 @@ we will have to use something else.
if (!s) {
std::string intrinsic_name = call_name;
std::set<std::string> not_cpython_builtin = {
"sin", "cos", "gamma", "tan", "asin", "acos", "atan", "sinh", "cosh", "tanh", "exp", "exp2", "expm1", "Symbol", "diff", "expand", "trunc", "fix",
"sin", "cos", "gamma", "tan", "asin", "acos", "atan", "sinh", "cosh", "tanh", "exp", "exp2", "expm1", "Symbol", "diff", "expand", "trunc", "fix", "subs",
"sum" // For sum called over lists
};
std::set<std::string> symbolic_functions = {
Expand Down
16 changes: 15 additions & 1 deletion src/lpython/semantics/python_attribute_eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ struct AttributeHandler {
{"expand", &eval_symbolic_expand},
{"has", &eval_symbolic_has_symbol},
{"is_integer", &eval_symbolic_is_integer},
{"is_positive", &eval_symbolic_is_positive}
{"is_positive", &eval_symbolic_is_positive},
{"subs", &eval_symbolic_subs}
};
}

Expand Down Expand Up @@ -590,6 +591,19 @@ struct AttributeHandler {
return create_function(al, loc, args_with_list, diag);
}

static ASR::asr_t* eval_symbolic_subs(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::IntrinsicElementalFunctionRegistry::get_create_function("subs");
return create_function(al, loc, args_with_list, diag);
}

}; // AttributeHandler

} // namespace LCompilers::LPython
Expand Down
Loading