Skip to content

Commit

Permalink
Merge branch 'main' into backend_lpython
Browse files Browse the repository at this point in the history
  • Loading branch information
certik authored Jan 20, 2024
2 parents c2ab636 + 30b5bfc commit 36f61f4
Show file tree
Hide file tree
Showing 78 changed files with 1,456 additions and 1,257 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ Please follow the below steps for Windows:
./run_tests.py
```

- Update test references:
```
./run_tests.py -u
```

- Run integration tests:

```bash
Expand Down
7 changes: 6 additions & 1 deletion cmake/UserOverride.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
# g++
set(common "-Wall -Wextra")
set(CMAKE_CXX_FLAGS_RELEASE_INIT "${common} -O3 -march=native -funroll-loops -DNDEBUG")
if (CMAKE_SYSTEM_PROCESSOR MATCHES "ppc|powerpc")
set(native "-mtune=native")
else ()
set(native "-march=native")
endif ()
set(CMAKE_CXX_FLAGS_RELEASE_INIT "${common} -O3 ${native} -funroll-loops -DNDEBUG")
set(CMAKE_CXX_FLAGS_DEBUG_INIT "${common} -g -ggdb")
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
# icpc
Expand Down
Binary file added expr
Binary file not shown.
3 changes: 2 additions & 1 deletion integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,8 @@ RUN(NAME symbolics_10 LABELS cpython_sym c_sym llvm_sym NOFAST)
RUN(NAME symbolics_11 LABELS cpython_sym c_sym llvm_sym NOFAST)
RUN(NAME symbolics_12 LABELS cpython_sym c_sym llvm_sym NOFAST)
RUN(NAME symbolics_13 LABELS cpython_sym c_sym llvm_sym NOFAST)
RUN(NAME test_gruntz LABELS cpython_sym llvm_sym NOFAST)
RUN(NAME symbolics_14 LABELS cpython_sym llvm_sym NOFAST)
RUN(NAME test_gruntz LABELS cpython_sym c_sym llvm_sym NOFAST)

RUN(NAME sizeof_01 LABELS llvm c
EXTRAFILES sizeof_01b.c)
Expand Down
10 changes: 5 additions & 5 deletions integration_tests/elemental_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,17 @@ def elemental_sin():

verify1d(array1d, sin1d, 256)

arraynd: f64[256, 64, 16] = empty((256, 64, 16), dtype=float64)
sinnd: f64[256, 64, 16] = empty((256, 64, 16), dtype=float64)
arraynd: f64[200, 64, 16] = empty((200, 64, 16), dtype=float64)
sinnd: f64[200, 64, 16] = empty((200, 64, 16), dtype=float64)

for i in range(256):
for i in range(200):
for j in range(64):
for k in range(16):
arraynd[i, j, k] = float(i + j + k)

sinnd = sin(arraynd)**2.0

verifynd(arraynd, sinnd, 256, 64, 16)
verifynd(arraynd, sinnd, 200, 64, 16)

def elemental_cos():
i: i32
Expand Down Expand Up @@ -162,4 +162,4 @@ def elemental_trig_identity():
elemental_cos()
elemental_trig_identity()
elemental_sum()
elemental_mul()
elemental_mul()
15 changes: 15 additions & 0 deletions integration_tests/symbolics_14.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from lpython import S
from sympy import Symbol

def mmrv(x: S) -> list[S]:
l1: list[S] = [x]
return l1

def test_mrv1():
x: S = Symbol("x")
ans: list[S] = mmrv(x)
element_1: S = ans[0]
print(element_1)
assert element_1 == x

test_mrv1()
23 changes: 23 additions & 0 deletions integration_tests/test_str_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,28 @@ def test_constant_str_subscript():
assert "abc"[2] == "c"
assert "abc"[:2] == "ab"

def test_str_split():
a: str = "1,2,3"
b: str = "1,2,,3,"
c: str = "1and2and3"
d: str = "1 2 3"
e: str = " 1 2 3 "
f: str = "123"
res: list[str] = a.split(",")
res1: list[str] = b.split(",")
res2: list[str] = c.split("and")
res3: list[str] = d.split()
res4: list[str] = e.split()
res5: list[str] = f.split(" ")
# res6: list[str] = "".split(" ")
assert res == ["1", "2", "3"]
assert res1 == ["1", "2", "", "3", ""]
assert res2 == ["1", "2", "3"]
assert res3 == ["1", "2", "3"]
assert res4 == ["1", "2", "3"]
assert res5 == ["123"]
# assert res6 == [""]

def check():
f()
test_str_concat()
Expand All @@ -137,5 +159,6 @@ def check():
test_str_title()
test_str_istitle()
test_str_isalpha()
test_str_split()

check()
20 changes: 20 additions & 0 deletions src/libasr/asr_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4737,6 +4737,26 @@ static inline bool is_simd_array(ASR::expr_t *v) {
== ASR::array_physical_typeType::SIMDArray);
}

static inline bool is_argument_of_type_CPtr(ASR::expr_t *var) {
bool is_argument = false;
if (ASR::is_a<ASR::CPtr_t>(*expr_type(var))) {
if (ASR::is_a<ASR::Var_t>(*var)) {
ASR::symbol_t *var_sym = ASR::down_cast<ASR::Var_t>(var)->m_v;
if (ASR::is_a<ASR::Variable_t>(*var_sym)) {
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(var_sym);
if (v->m_intent == intent_local ||
v->m_intent == intent_return_var ||
!v->m_intent) {
is_argument = false;
} else {
is_argument = true;
}
}
}
}
return is_argument;
}

} // namespace ASRUtils

} // namespace LCompilers
Expand Down
11 changes: 5 additions & 6 deletions src/libasr/codegen/asr_to_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,11 +415,6 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
headers.insert("complex.h");
convert_variable_decl_util(v, is_array, declare_as_constant, use_ref, dummy,
force_declare, force_declare_name, n_dims, m_dims, v_m_type, dims, sub);
} 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)) {
convert_variable_decl_util(v, is_array, declare_as_constant, use_ref, dummy,
force_declare, force_declare_name, n_dims, m_dims, v_m_type, dims, sub);
Expand Down Expand Up @@ -529,7 +524,11 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
} else if (ASR::is_a<ASR::List_t>(*v_m_type)) {
ASR::List_t* t = ASR::down_cast<ASR::List_t>(v_m_type);
std::string list_type_c = c_ds_api->get_list_type(t);
sub = format_type_c("", list_type_c, v.m_name,
std::string name = v.m_name;
if (v.m_intent == ASRUtils::intent_out) {
name = "*" + name;
}
sub = format_type_c("", list_type_c, name,
false, false);
} else if (ASR::is_a<ASR::Tuple_t>(*v_m_type)) {
ASR::Tuple_t* t = ASR::down_cast<ASR::Tuple_t>(v_m_type);
Expand Down
23 changes: 22 additions & 1 deletion src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,15 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
|| param->m_intent == ASRUtils::intent_out)
&& !ASRUtils::is_aggregate_type(param->m_type))) {
args += "&" + src;
} else if (param->m_intent == ASRUtils::intent_out) {
if (ASR::is_a<ASR::List_t>(*param->m_type)) {
ASR::List_t* list_type = ASR::down_cast<ASR::List_t>(param->m_type);
if (list_type->m_type->type == ASR::ttypeType::CPtr){
args += "&" + src;
}
} else {
args += src;
}
} else {
args += src;
}
Expand Down Expand Up @@ -1367,7 +1376,19 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
if( is_target_list && is_value_list ) {
ASR::List_t* list_target = ASR::down_cast<ASR::List_t>(ASRUtils::expr_type(x.m_target));
std::string list_dc_func = c_ds_api->get_list_deepcopy_func(list_target);
src += indent + list_dc_func + "(&" + value + ", &" + target + ");\n\n";
if (ASR::is_a<ASR::Var_t>(*x.m_target)) {
ASR::symbol_t *target_sym = ASR::down_cast<ASR::Var_t>(x.m_target)->m_v;
if (ASR::is_a<ASR::Variable_t>(*target_sym)) {
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(target_sym);
if (v->m_intent == ASRUtils::intent_out) {
src += indent + list_dc_func + "(&" + value + ", " + target + ");\n\n";
} else {
src += indent + list_dc_func + "(&" + value + ", &" + target + ");\n\n";
}
}
} else {
src += indent + list_dc_func + "(&" + value + ", &" + target + ");\n\n";
}
} else if ( is_target_tup && is_value_tup ) {
ASR::Tuple_t* tup_target = ASR::down_cast<ASR::Tuple_t>(ASRUtils::expr_type(x.m_target));
std::string dc_func = c_ds_api->get_tuple_deepcopy_func(tup_target);
Expand Down
7 changes: 6 additions & 1 deletion src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ using ASRUtils::intent_local;
using ASRUtils::intent_return_var;
using ASRUtils::determine_module_dependencies;
using ASRUtils::is_arg_dummy;
using ASRUtils::is_argument_of_type_CPtr;

void string_init(llvm::LLVMContext &context, llvm::Module &module,
llvm::IRBuilder<> &builder, llvm::Value* arg_size, llvm::Value* arg_string) {
Expand Down Expand Up @@ -1245,8 +1246,12 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
llvm::Value* const_list = builder->CreateAlloca(const_list_type, nullptr, "const_list");
list_api->list_init(type_code, const_list, *module, x.n_args, x.n_args);
int64_t ptr_loads_copy = ptr_loads;
ptr_loads = 1;
for( size_t i = 0; i < x.n_args; i++ ) {
if (is_argument_of_type_CPtr(x.m_args[i])) {
ptr_loads = 0;
} else {
ptr_loads = 1;
}
this->visit_expr(*x.m_args[i]);
llvm::Value* item = tmp;
llvm::Value* pos = llvm::ConstantInt::get(context, llvm::APInt(32, i));
Expand Down
5 changes: 5 additions & 0 deletions src/libasr/stacktrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
// For registering SIGSEGV callbacks
#include <csignal>

#ifdef __APPLE__
// For PATH_MAX
# include <sys/syslimits.h>
#endif


// The following C headers are needed for some specific C functionality (see
// the comments), which is not available in C++:
Expand Down
3 changes: 3 additions & 0 deletions src/lpython/parser/parser_stype.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ static_assert(std::is_trivial<YYSTYPE>::value);
// Ensure the YYSTYPE size is equal to Vec<AST::ast_t*>, which is a required member, so
// YYSTYPE must be at least as big, but it should not be bigger, otherwise it
// would reduce performance.
// A temporary fix for PowerPC 32-bit, where the following assert fails with (16 == 12).
#ifndef __ppc__
static_assert(sizeof(YYSTYPE) == sizeof(Vec<LPython::AST::ast_t*>));
#endif

static_assert(std::is_standard_layout<Location>::value);
static_assert(std::is_trivial<Location>::value);
Expand Down
28 changes: 28 additions & 0 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,11 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
return nullptr;
}


void visit_AsyncFunctionDef(const AST::AsyncFunctionDef_t &x){
throw SemanticError("The `async` keyword is currently not supported", x.base.base.loc);
}

void visit_expr_list(AST::expr_t** exprs, size_t n,
Vec<ASR::expr_t*>& exprs_vec) {
LCOMPILERS_ASSERT(exprs_vec.reserve_called);
Expand Down Expand Up @@ -6761,6 +6766,29 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
// Push string and substring argument on top of Vector (or Function Arguments Stack basically)
fn_args.push_back(al, str);
fn_args.push_back(al, value);
} else if(attr_name == "split") {
if(args.size() > 1) {
throw SemanticError("str.split() takes at most one argument for now.", loc);
}
fn_call_name = "_lpython_str_split";
ASR::call_arg_t str;
str.loc = loc;
str.m_value = s_var;

if (args.size() == 1) {
ASR::expr_t *arg_value = args[0].m_value;
ASR::ttype_t *arg_value_type = ASRUtils::expr_type(arg_value);
if (!ASRUtils::is_character(*arg_value_type)) {
throw SemanticError("str.split() takes one argument of type: str", loc);
}
ASR::call_arg_t value;
value.loc = loc;
value.m_value = args[0].m_value;
fn_args.push_back(al, str);
fn_args.push_back(al, value);
} else {
fn_args.push_back(al, str);
}
} else if(attr_name.size() > 2 && attr_name[0] == 'i' && attr_name[1] == 's') {
/*
String Validation Methods i.e all "is" based functions are handled here
Expand Down
1 change: 1 addition & 0 deletions src/lpython/semantics/python_comptime_eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ struct PythonIntrinsicProcedures {
{"_lpython_str_rstrip", {m_builtin, &not_implemented}},
{"_lpython_str_lstrip", {m_builtin, &not_implemented}},
{"_lpython_str_strip", {m_builtin, &not_implemented}},
{"_lpython_str_split", {m_builtin, &not_implemented}},
{"_lpython_str_swapcase", {m_builtin, &not_implemented}},
{"_lpython_str_startswith", {m_builtin, &not_implemented}},
{"_lpython_str_endswith", {m_builtin, &not_implemented}},
Expand Down
40 changes: 39 additions & 1 deletion src/runtime/lpython_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,44 @@ def _lpython_str_strip(x: str) -> str:
res = _lpython_str_rstrip(res)
return res

@overload
def _lpython_str_split(x: str) -> list[str]:
sep: str = ' '
res: list[str] = []
start:i32 = 0
ind: i32
x_strip: str = _lpython_str_strip(x)
if (x_strip == ""):
return res
while True:
while (start < len(x_strip) and x_strip[start] == ' '):
start += 1
ind = _lpython_str_find(x_strip[start:len(x_strip)], sep)
if ind == -1:
res.append(x_strip[start:len(x_strip)])
break
else:
res.append(x_strip[start:start + ind])
start += ind + len(sep)
return res

@overload
def _lpython_str_split(x: str, sep:str) -> list[str]:
if len(sep) == 0:
raise ValueError('empty separator')
res: list[str] = []
start:i32 = 0
ind: i32
while True:
ind = _lpython_str_find(x[start:len(x)], sep)
if ind == -1:
res.append(x[start:len(x)])
break
else:
res.append(x[start:start + ind])
start += ind + len(sep)
return res

@overload
def _lpython_str_swapcase(s: str) -> str:
res :str = ""
Expand Down Expand Up @@ -870,7 +908,7 @@ def _lpython_str_partition(s:str, sep: str) -> tuple[str, str, str]:
if len(s) == 0:
raise ValueError('empty string cannot be partitioned')
if len(sep) == 0:
raise ValueError('empty seperator')
raise ValueError('empty separator')
res : tuple[str, str, str]
ind : i32
ind = _lpython_str_find(s, sep)
Expand Down
4 changes: 4 additions & 0 deletions tests/errors/test_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
async def test_async():
print("done")

test_async()
2 changes: 1 addition & 1 deletion tests/reference/asr-array_01_decl-39cf894.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-array_01_decl-39cf894.stdout",
"stdout_hash": "489d2e6a364cc6020f2942b94738849349928901f1269b975a6e2464",
"stdout_hash": "5d4751789e2ddcd882c4d6026f801ba32cfc227fafff7395a788bdd9",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
Loading

0 comments on commit 36f61f4

Please sign in to comment.