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

Intial Implementation for SymbolicExpression ttype #1846

Merged
merged 40 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
43bec5e
Added SymbolicSymbol as Intrinsic Function
anutosh491 May 22, 2023
6bdbdef
Supporting sympy imports for CPython
anutosh491 May 22, 2023
be0f715
Added some utilities and removed instantiate function
anutosh491 May 27, 2023
efdfed8
Initial code for supporting assignment and print statements
anutosh491 May 29, 2023
5d152b2
Merge branch 'main' into GSoC_PR1
anutosh491 May 29, 2023
3d19dbf
added verify_args for symbolicsymbol
anutosh491 May 29, 2023
5858009
Added verify_args function for symbolic symbol
anutosh491 May 31, 2023
d680912
Merge branch 'main' into GSoC_PR1
anutosh491 Jun 12, 2023
ab626c6
Add support in C backend for assignment, printing and addition operat…
anutosh491 Jun 13, 2023
53aaa99
Introduced symbolic constant pi
anutosh491 Jun 14, 2023
9010eba
Added support for symengine through the C backend
anutosh491 Jun 14, 2023
06d2087
Merge branch 'main' into GSoC_PR1
anutosh491 Jun 15, 2023
645fa7d
Removed changes related to the LLVM backend and removed warnings
anutosh491 Jun 15, 2023
fccbdde
Updated references
anutosh491 Jun 15, 2023
81c4dba
fixed failing test
anutosh491 Jun 15, 2023
2fc618b
Added a test file for testing basic assignment , addition and printin…
anutosh491 Jun 15, 2023
03bf3b7
Added compiler option for enabling symengine
anutosh491 Jun 15, 2023
9d06c76
Added support for import S from lpython
anutosh491 Jun 15, 2023
6ebe306
Added symengine to the environment
anutosh491 Jun 15, 2023
fd6811b
added sympy and symengine in environment
anutosh491 Jun 15, 2023
f1883c6
added sympy and symengine for supporting imports
anutosh491 Jun 15, 2023
fe749a2
Merge branch 'main' into GSoC_PR1
Thirumalai-Shaktivel Jun 16, 2023
25dda82
Add CI for SymPy tests
Thirumalai-Shaktivel Jun 16, 2023
bd7e0d4
Install sympy in the CI
Thirumalai-Shaktivel Jun 16, 2023
5ca07dd
Merge branch 'main' into GSoC_PR1
certik Jun 16, 2023
3771cb1
Merge branch 'main' into GSoC_PR1
anutosh491 Jun 18, 2023
dab1ef8
Made minor changes in intrinsic_function_registry.h
anutosh491 Jun 18, 2023
c9aea0b
Merge branch 'main' into GSoC_PR1
anutosh491 Jun 18, 2023
c505610
Removed unneccessary additions
anutosh491 Jun 18, 2023
12ae807
Trying to fix failing tests
anutosh491 Jun 18, 2023
2f4fb86
Reverted back the change
anutosh491 Jun 18, 2023
f9324c6
added -Wl after -rpath
anutosh491 Jun 18, 2023
213ade0
remove sympy from Fast
anutosh491 Jun 18, 2023
4e99898
Introduced c_sym and cpython_sym backends
anutosh491 Jun 18, 2023
e232e5d
Replaced sympy with cpython_sym
anutosh491 Jun 18, 2023
6ae14ac
Merge branch 'main' into GSoC_PR1
certik Jun 18, 2023
07e01e4
Fix typo and remove a task in CI
Thirumalai-Shaktivel Jun 19, 2023
5976ec5
minor changes to optimize code
anutosh491 Jun 19, 2023
19f8075
edited verify_args function for SymbolicPi
anutosh491 Jun 19, 2023
9b763f9
Merge branch 'main' into GSoC_PR1
certik Jun 19, 2023
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
2 changes: 2 additions & 0 deletions src/bin/lpython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1244,8 +1244,10 @@ int link_executable(const std::vector<std::string> &infiles,
cmd += s + " ";
}
cmd += " -I " + rtlib_header_dir;
cmd += " -I${CONDA_PREFIX}/include";
cmd += " -L" + base_path
+ " -Wl,-rpath," + base_path + " -l" + runtime_lib + " -lm";
cmd += " -L$CONDA_PREFIX/lib -Wl,-rpath -Wl,$CONDA_PREFIX/lib -lsymengine";
if (compiler_options.enable_cpython) {
std::string py_version = "3.10";
std::string py_flags = R"(-I $CONDA_PREFIX/include/python)" + py_version + R"( -L$CONDA_PREFIX/lib -Wl,-rpath -Wl,$CONDA_PREFIX/lib -lpython)" + py_version + R"()";
Expand Down
1 change: 1 addition & 0 deletions src/libasr/ASR.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ ttype
| Allocatable(ttype type)
| Const(ttype type)
| CPtr()
| SymbolicExpression()
| TypeParameter(identifier param)
| Array(ttype type, dimension* dims)
| FunctionType(ttype* arg_types, ttype? return_var_type,
Expand Down
17 changes: 17 additions & 0 deletions src/libasr/asr_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,9 @@ static inline std::string type_to_str(const ASR::ttype_t *t)
ASR::TypeParameter_t* tp = ASR::down_cast<ASR::TypeParameter_t>(t);
return tp->m_param;
}
case ASR::ttypeType::SymbolicExpression: {
return "symbolic expression";
}
default : throw LCompilersException("Not implemented " + std::to_string(t->type) + ".");
}
}
Expand Down Expand Up @@ -1151,6 +1154,9 @@ static inline std::string get_type_code(const ASR::ttype_t *t, bool use_undersco
return "Const[" + get_type_code(p->m_type, use_underscore_sep,
encode_dimensions_, set_dimensional_hint) + "]";
}
case ASR::ttypeType::SymbolicExpression: {
return "S";
}
default: {
throw LCompilersException("Type encoding not implemented for "
+ std::to_string(t->type));
Expand Down Expand Up @@ -1286,6 +1292,9 @@ static inline std::string type_to_str_python(const ASR::ttype_t *t,
ASR::TypeParameter_t *p = ASR::down_cast<ASR::TypeParameter_t>(t);
return p->m_param;
}
case ASR::ttypeType::SymbolicExpression: {
return "S";
}
default : throw LCompilersException("Not implemented " + std::to_string(t->type));
}
}
Expand Down Expand Up @@ -1648,6 +1657,11 @@ inline int extract_dimensions_from_ttype(ASR::ttype_t *x,
n_dims = extract_dimensions_from_ttype(ASR::down_cast<ASR::Const_t>(x)->m_type, m_dims);
break;
}
case ASR::ttypeType::SymbolicExpression: {
n_dims = 0;
m_dims = nullptr;
break;
}
anutosh491 marked this conversation as resolved.
Show resolved Hide resolved
case ASR::ttypeType::Integer:
case ASR::ttypeType::UnsignedInteger:
case ASR::ttypeType::Real:
Expand Down Expand Up @@ -2279,6 +2293,9 @@ inline bool types_equal(ASR::ttype_t *a, ASR::ttype_t *b,
case ASR::ttypeType::CPtr: {
return true;
}
case ASR::ttypeType::SymbolicExpression: {
return true;
}
case (ASR::ttypeType::Real) : {
ASR::Real_t *a2 = ASR::down_cast<ASR::Real_t>(a);
ASR::Real_t *b2 = ASR::down_cast<ASR::Real_t>(b);
Expand Down
8 changes: 8 additions & 0 deletions src/libasr/codegen/asr_to_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,11 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
dims = convert_dims_c(n_dims, m_dims, v_m_type, is_fixed_size);
sub = format_type_c(dims, type_name, v_m_name, use_ref, dummy);
}
} else if (ASR::is_a<ASR::SymbolicExpression_t>(*v_m_type)) {
headers.insert("symengine/cwrapper.h");
std::string type_name = "basic";
std::string v_m_name = v.m_name;
sub = format_type_c("", type_name, v_m_name, use_ref, dummy);
} else if (ASRUtils::is_logical(*v_m_type)) {
bool is_fixed_size = true;
dims = convert_dims_c(n_dims, m_dims, v_m_type, is_fixed_size);
Expand Down Expand Up @@ -1235,6 +1240,9 @@ R"(
v.pop_back();
v.push_back("creal(" + src + ")");
v.push_back("cimag(" + src + ")");
} else if(value_type->type == ASR::ttypeType::SymbolicExpression){
v.pop_back();
v.push_back("basic_str(" + src + ")");
}
if (i+1!=x.n_values) {
tmp_gen += "\%s";
Expand Down
38 changes: 33 additions & 5 deletions src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,8 @@ R"(#include <stdio.h>
sub = "double complex ";
}
}
} else if (ASR::is_a<ASR::SymbolicExpression_t>(*return_var->m_type)) {
sub = "basic ";
} else if (ASR::is_a<ASR::CPtr_t>(*return_var->m_type)) {
sub = "void* ";
} else if (ASR::is_a<ASR::List_t>(*return_var->m_type)) {
Expand Down Expand Up @@ -664,6 +666,10 @@ R"(#include <stdio.h>
v->m_intent == ASRUtils::intent_return_var ) {
d += ";\n";
}
if (ASR::is_a<ASR::SymbolicExpression_t>(*v->m_type)) {
std::string v_m_name = v->m_name;
d += indent + "basic_new_stack(" + v_m_name + ");\n";
anutosh491 marked this conversation as resolved.
Show resolved Hide resolved
}
decl += check_tmp_buffer() + d;
}
if (ASR::is_a<ASR::TypeParameter_t>(*v->m_type)) {
Expand Down Expand Up @@ -1014,7 +1020,12 @@ R"(#include <stdio.h>
src += alloc + indent + c_ds_api->get_deepcopy(m_target_type, value, target) + "\n";
}
} else {
src += alloc + indent + c_ds_api->get_deepcopy(m_target_type, value, target) + "\n";
if (m_target_type->type == ASR::ttypeType::SymbolicExpression){
ASR::expr_t* m_value_expr = x.m_value;
src += alloc + indent + c_ds_api->get_deepcopy_symbolic(m_value_expr, value, target) + "\n";
} else {
src += alloc + indent + c_ds_api->get_deepcopy(m_target_type, value, target) + "\n";
}
}
} else {
src += indent + c_ds_api->get_deepcopy(m_target_type, value, target) + "\n";
Expand Down Expand Up @@ -2355,7 +2366,6 @@ R"(#include <stdio.h>
}

void visit_IntrinsicFunction(const ASR::IntrinsicFunction_t &x) {
LCOMPILERS_ASSERT(x.n_args == 1)
std::string out;
switch (x.m_intrinsic_id) {
SET_INTRINSIC_NAME(Sin, "sin");
Expand All @@ -2371,16 +2381,34 @@ R"(#include <stdio.h>
SET_INTRINSIC_NAME(Exp, "exp");
SET_INTRINSIC_NAME(Exp2, "exp2");
SET_INTRINSIC_NAME(Expm1, "expm1");
SET_INTRINSIC_NAME(SymbolicSymbol, "Symbol");
SET_INTRINSIC_NAME(SymbolicPi, "pi");
case (static_cast<int64_t>(ASRUtils::IntrinsicFunctions::SymbolicAdd)): {
LCOMPILERS_ASSERT(x.n_args == 2);
this->visit_expr(*x.m_args[0]);
std::string arg1 = src;
this->visit_expr(*x.m_args[1]);
std::string arg2 = src;
out = arg1 + "," + arg2;
src = out;
break;
}
default : {
throw LCompilersException("IntrinsicFunction: `"
+ ASRUtils::get_intrinsic_name(x.m_intrinsic_id)
+ "` is not implemented");
}
}
headers.insert("math.h");
this->visit_expr(*x.m_args[0]);
out += "(" + src + ")";
src = out;
if (x.n_args == 0){
src = out;
} else if (x.n_args == 1) {
this->visit_expr(*x.m_args[0]);
if (x.m_intrinsic_id != static_cast<int64_t>(ASRUtils::IntrinsicFunctions::SymbolicSymbol)) {
out = "(" + src + ")";
anutosh491 marked this conversation as resolved.
Show resolved Hide resolved
src = out;
}
}
}
};

Expand Down
37 changes: 37 additions & 0 deletions src/libasr/codegen/c_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <libasr/asr.h>
#include <libasr/asr_utils.h>
#include <libasr/pass/intrinsic_function_registry.h>

namespace LCompilers {

Expand Down Expand Up @@ -527,6 +528,39 @@ class CCPPDSUtils {
return result;
}

std::string get_deepcopy_symbolic(ASR::expr_t *value_expr, std::string value, std::string target) {
std::string result;
if (ASR::is_a<ASR::Var_t>(*value_expr)) {
result = "basic_assign(" + target + ", " + value + ");";
} else if (ASR::is_a<ASR::IntrinsicFunction_t>(*value_expr)) {
ASR::IntrinsicFunction_t* intrinsic_func = ASR::down_cast<ASR::IntrinsicFunction_t>(value_expr);
int64_t intrinsic_id = intrinsic_func->m_intrinsic_id;
switch (static_cast<LCompilers::ASRUtils::IntrinsicFunctions>(intrinsic_id)) {
case LCompilers::ASRUtils::IntrinsicFunctions::SymbolicSymbol: {
result = "symbol_set(" + target + ", " + value + ");";
break;
}
case LCompilers::ASRUtils::IntrinsicFunctions::SymbolicAdd: {
size_t delimiterPos = value.find(",");
std::string leftPart = value.substr(0, delimiterPos);
std::string rightPart = value.substr(delimiterPos + 1);
result = "basic_add(" + target + ", " + leftPart + ", " + rightPart + ");";
break;
}
case LCompilers::ASRUtils::IntrinsicFunctions::SymbolicPi: {
result = "basic_const_pi(" + target + ");";
break;
}
default: {
throw LCompilersException("IntrinsicFunction: `"
+ LCompilers::ASRUtils::get_intrinsic_name(intrinsic_id)
+ "` is not implemented");
}
}
}
return result;
}
anutosh491 marked this conversation as resolved.
Show resolved Hide resolved

std::string get_type(ASR::ttype_t *t) {
LCOMPILERS_ASSERT(CUtils::is_non_primitive_DT(t));
if (ASR::is_a<ASR::List_t>(*t)) {
Expand Down Expand Up @@ -594,6 +628,9 @@ class CCPPDSUtils {
case ASR::ttypeType::Complex: {
return "(%f, %f)";
}
case ASR::ttypeType::SymbolicExpression: {
return "%s";
}
case ASR::ttypeType::Pointer: {
if( !deref_ptr ) {
return "%p";
Expand Down
Loading
Loading