Skip to content

Commit 793997d

Browse files
authored
Merge pull request #2632 from czgdp1807/sync_libas_contd
Sync ``libasr`` with LC
2 parents 4287779 + 05f5ed4 commit 793997d

19 files changed

+293
-65
lines changed

src/libasr/asr_utils.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,6 +2013,12 @@ static inline bool is_pointer(ASR::ttype_t *x) {
20132013
}
20142014

20152015
static inline bool is_integer(ASR::ttype_t &x) {
2016+
// return ASR::is_a<ASR::Integer_t>(
2017+
// *type_get_past_const(
2018+
// type_get_past_array(
2019+
// type_get_past_allocatable(
2020+
// type_get_past_pointer(
2021+
// type_get_past_const(&x))))));
20162022
return ASR::is_a<ASR::Integer_t>(
20172023
*type_get_past_const(
20182024
type_get_past_array(
@@ -2028,6 +2034,11 @@ static inline bool is_unsigned_integer(ASR::ttype_t &x) {
20282034
}
20292035

20302036
static inline bool is_real(ASR::ttype_t &x) {
2037+
// return ASR::is_a<ASR::Real_t>(
2038+
// *type_get_past_const(
2039+
// type_get_past_array(
2040+
// type_get_past_allocatable(
2041+
// type_get_past_pointer(&x)))));
20312042
return ASR::is_a<ASR::Real_t>(
20322043
*type_get_past_array(
20332044
type_get_past_allocatable(
@@ -2196,6 +2207,10 @@ inline size_t extract_dimensions_from_ttype(ASR::ttype_t *x,
21962207
}
21972208

21982209
static inline ASR::ttype_t *extract_type(ASR::ttype_t *type) {
2210+
// return type_get_past_const(
2211+
// type_get_past_array(
2212+
// type_get_past_allocatable(
2213+
// type_get_past_pointer(type))));
21992214
return type_get_past_array(
22002215
type_get_past_allocatable(
22012216
type_get_past_pointer(type)));
@@ -3035,6 +3050,12 @@ inline bool types_equal(ASR::ttype_t *a, ASR::ttype_t *b,
30353050
if( a == nullptr && b == nullptr ) {
30363051
return true;
30373052
}
3053+
// a = ASRUtils::type_get_past_const(
3054+
// ASRUtils::type_get_past_allocatable(
3055+
// ASRUtils::type_get_past_pointer(a)));
3056+
// b = ASRUtils::type_get_past_const(
3057+
// ASRUtils::type_get_past_allocatable(
3058+
// ASRUtils::type_get_past_pointer(b)));
30383059
a = ASRUtils::type_get_past_allocatable(ASRUtils::type_get_past_pointer(a));
30393060
b = ASRUtils::type_get_past_allocatable(ASRUtils::type_get_past_pointer(b));
30403061
if( !check_for_dimensions ) {

src/libasr/asr_verify.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,8 @@ class VerifyVisitor : public BaseWalkVisitor<VerifyVisitor>
525525
ASR::is_a<ASR::CustomOperator_t>(*a.second) ) {
526526
continue ;
527527
}
528+
// TODO: Uncomment the following line
529+
// ASR::ttype_t* var_type = ASRUtils::extract_type(ASRUtils::symbol_type(a.second));
528530
ASR::ttype_t* var_type = ASRUtils::type_get_past_pointer(ASRUtils::symbol_type(a.second));
529531
char* aggregate_type_name = nullptr;
530532
ASR::symbol_t* sym = nullptr;

src/libasr/casting_utils.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ namespace LCompilers::CastingUtil {
8484
}
8585

8686
int casted_expr_signal = 2;
87+
// TODO: Uncomment the following
88+
// ASR::ttypeType left_Type = ASRUtils::extract_type(left_type)->type,
89+
// right_Type = ASRUtils::extract_type(right_type)->type;
8790
ASR::ttypeType left_Type = left_type->type, right_Type = right_type->type;
8891
int left_kind = ASRUtils::extract_kind_from_ttype_t(left_type);
8992
int right_kind = ASRUtils::extract_kind_from_ttype_t(right_type);
@@ -121,6 +124,9 @@ namespace LCompilers::CastingUtil {
121124
if( ASR::is_a<ASR::Const_t>(*src) ) {
122125
src = ASRUtils::get_contained_type(src);
123126
}
127+
// TODO: Uncomment the following
128+
// ASR::ttypeType src_type = ASRUtils::extract_type(src)->type;
129+
// ASR::ttypeType dest_type = ASRUtils::extract_type(dest)->type;
124130
ASR::ttypeType src_type = src->type;
125131
ASR::ttypeType dest_type = dest->type;
126132
ASR::cast_kindType cast_kind;

src/libasr/casting_utils.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
#define LFORTRAN_CASTING_UTILS_H
33

44

5-
#include <functional>
6-
75
#include <libasr/asr.h>
86

97
namespace LCompilers::CastingUtil {

src/libasr/codegen/asr_to_c_cpp.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,9 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
11571157
+ "' not implemented");
11581158
}
11591159
} else {
1160+
if (fn_name == "main") {
1161+
fn_name = "_xx_lcompilers_changed_main_xx";
1162+
}
11601163
src = fn_name + "(" + construct_call_args(fn, x.n_args, x.m_args) + ")";
11611164
}
11621165
last_expr_precedence = 2;

src/libasr/codegen/asr_to_fortran.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,7 @@ class ASRToFortranVisitor : public ASR::BaseVisitor<ASRToFortranVisitor>
12671267
SET_INTRINSIC_NAME(StringContainsSet, "verify");
12681268
SET_INTRINSIC_NAME(StringFindSet, "scan");
12691269
SET_INTRINSIC_NAME(SubstrIndex, "index");
1270+
SET_INTRINSIC_NAME(Modulo, "modulo");
12701271
default : {
12711272
throw LCompilersException("IntrinsicElementalFunction: `"
12721273
+ ASRUtils::get_intrinsic_name(x.m_intrinsic_id)

src/libasr/codegen/asr_to_julia.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,6 +1905,7 @@ class ASRToJuliaVisitor : public ASR::BaseVisitor<ASRToJuliaVisitor>
19051905
SET_INTRINSIC_NAME(StringContainsSet, "verify");
19061906
SET_INTRINSIC_NAME(StringFindSet, "scan");
19071907
SET_INTRINSIC_NAME(SubstrIndex, "index");
1908+
SET_INTRINSIC_NAME(Modulo, "modulo");
19081909
default : {
19091910
throw LCompilersException("IntrinsicFunction: `"
19101911
+ ASRUtils::get_intrinsic_name(x.m_intrinsic_id)

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 71 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,7 +1505,8 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
15051505
this->visit_expr(*x.m_v);
15061506
ptr_loads = ptr_loads_copy;
15071507
llvm::Value* union_llvm = tmp;
1508-
ASR::Variable_t* member_var = ASR::down_cast<ASR::Variable_t>(x.m_m);
1508+
ASR::Variable_t* member_var = ASR::down_cast<ASR::Variable_t>(
1509+
ASRUtils::symbol_get_past_external(x.m_m));
15091510
ASR::ttype_t* member_type_asr = ASRUtils::get_contained_type(member_var->m_type);
15101511
if( ASR::is_a<ASR::Struct_t>(*member_type_asr) ) {
15111512
ASR::Struct_t* d = ASR::down_cast<ASR::Struct_t>(member_type_asr);
@@ -2424,7 +2425,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
24242425
this->visit_expr(*x.m_shape);
24252426
llvm::Value* shape = tmp;
24262427
ASR::ttype_t* x_m_array_type = ASRUtils::expr_type(x.m_array);
2427-
ASR::array_physical_typeType array_physical_type = ASRUtils::extract_physical_type(x_m_array_type);
2428+
ASR::array_physical_typeType array_physical_type = ASRUtils::extract_physical_type(x_m_array_type);
24282429
switch( array_physical_type ) {
24292430
case ASR::array_physical_typeType::DescriptorArray: {
24302431
ASR::ttype_t* asr_data_type = ASRUtils::duplicate_type_without_dims(al,
@@ -4549,6 +4550,8 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
45494550
bool is_value_set = ASR::is_a<ASR::Set_t>(*asr_value_type);
45504551
bool is_target_struct = ASR::is_a<ASR::Struct_t>(*asr_target_type);
45514552
bool is_value_struct = ASR::is_a<ASR::Struct_t>(*asr_value_type);
4553+
bool is_value_list_to_array = (ASR::is_a<ASR::Cast_t>(*x.m_value) &&
4554+
ASR::down_cast<ASR::Cast_t>(x.m_value)->m_kind == ASR::cast_kindType::ListToArray);
45524555
if (ASR::is_a<ASR::StringSection_t>(*x.m_target)) {
45534556
handle_StringSection_Assignment(x.m_target, x.m_value);
45544557
if (tmp == strings_to_be_deallocated.back()) {
@@ -4767,7 +4770,37 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
47674770
target = CreateLoad(target);
47684771
}
47694772
ASR::ttype_t *cont_type = ASRUtils::get_contained_type(asr_target_type);
4770-
if (ASRUtils::is_array(cont_type) && ASRUtils::is_array(cont_type) ) {
4773+
if ( ASRUtils::is_array(cont_type) ) {
4774+
if( is_value_list_to_array ) {
4775+
this->visit_expr_wrapper(x.m_value, true);
4776+
llvm::Value* list_data = tmp;
4777+
int64_t ptr_loads_copy = ptr_loads;
4778+
ptr_loads = 0;
4779+
this->visit_expr(*ASR::down_cast<ASR::Cast_t>(x.m_value)->m_arg);
4780+
llvm::Value* plist = tmp;
4781+
ptr_loads = ptr_loads_copy;
4782+
llvm::Value* array_data = nullptr;
4783+
if( ASRUtils::extract_physical_type(asr_target_type) ==
4784+
ASR::array_physical_typeType::DescriptorArray ) {
4785+
array_data = LLVM::CreateLoad(*builder,
4786+
arr_descr->get_pointer_to_data(LLVM::CreateLoad(*builder, target)));
4787+
} else if( ASRUtils::extract_physical_type(asr_target_type) ==
4788+
ASR::array_physical_typeType::FixedSizeArray ) {
4789+
array_data = llvm_utils->create_gep(target, 0);
4790+
} else {
4791+
LCOMPILERS_ASSERT(false);
4792+
}
4793+
llvm::Value* size = list_api->len(plist);
4794+
llvm::Type* el_type = llvm_utils->get_type_from_ttype_t_util(
4795+
ASRUtils::extract_type(ASRUtils::expr_type(x.m_value)), module.get());
4796+
llvm::DataLayout data_layout(module.get());
4797+
uint64_t size_ = data_layout.getTypeAllocSize(el_type);
4798+
size = builder->CreateMul(size, llvm::ConstantInt::get(
4799+
llvm::Type::getInt32Ty(context), llvm::APInt(32, size_)));
4800+
builder->CreateMemCpy(array_data, llvm::MaybeAlign(),
4801+
list_data, llvm::MaybeAlign(), size);
4802+
return ;
4803+
}
47714804
if( asr_target->m_type->type == ASR::ttypeType::Character) {
47724805
target = CreateLoad(arr_descr->get_pointer_to_data(target));
47734806
}
@@ -5030,17 +5063,19 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
50305063
} else if(
50315064
m_new == ASR::array_physical_typeType::PointerToDataArray &&
50325065
m_old == ASR::array_physical_typeType::FixedSizeArray) {
5033-
if( (ASRUtils::expr_value(m_arg) &&
5066+
if( ((ASRUtils::expr_value(m_arg) &&
50345067
!ASR::is_a<ASR::ArrayConstant_t>(*ASRUtils::expr_value(m_arg))) ||
5035-
ASRUtils::expr_value(m_arg) == nullptr ) {
5068+
ASRUtils::expr_value(m_arg) == nullptr ) &&
5069+
!ASR::is_a<ASR::ArrayConstructor_t>(*m_arg) ) {
50365070
tmp = llvm_utils->create_gep(tmp, 0);
50375071
}
50385072
} else if(
50395073
m_new == ASR::array_physical_typeType::UnboundedPointerToDataArray &&
50405074
m_old == ASR::array_physical_typeType::FixedSizeArray) {
5041-
if( (ASRUtils::expr_value(m_arg) &&
5075+
if( ((ASRUtils::expr_value(m_arg) &&
50425076
!ASR::is_a<ASR::ArrayConstant_t>(*ASRUtils::expr_value(m_arg))) ||
5043-
ASRUtils::expr_value(m_arg) == nullptr ) {
5077+
ASRUtils::expr_value(m_arg) == nullptr) &&
5078+
!ASR::is_a<ASR::ArrayConstructor_t>(*m_arg) ) {
50445079
tmp = llvm_utils->create_gep(tmp, 0);
50455080
}
50465081
} else if (
@@ -5054,9 +5089,10 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
50545089
} else if(
50555090
m_new == ASR::array_physical_typeType::DescriptorArray &&
50565091
m_old == ASR::array_physical_typeType::FixedSizeArray) {
5057-
if( (ASRUtils::expr_value(m_arg) &&
5092+
if( ((ASRUtils::expr_value(m_arg) &&
50585093
!ASR::is_a<ASR::ArrayConstant_t>(*ASRUtils::expr_value(m_arg))) ||
5059-
ASRUtils::expr_value(m_arg) == nullptr ) {
5094+
ASRUtils::expr_value(m_arg) == nullptr) &&
5095+
!ASR::is_a<ASR::ArrayConstructor_t>(*m_arg) ) {
50605096
tmp = llvm_utils->create_gep(tmp, 0);
50615097
}
50625098
PointerToData_to_Descriptor(m_type, m_type_for_dimensions);
@@ -5094,9 +5130,10 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
50945130
m_old == ASR::array_physical_typeType::CharacterArraySinglePointer) {
50955131
//
50965132
if (ASRUtils::is_fixed_size_array(m_type)) {
5097-
if( (ASRUtils::expr_value(m_arg) &&
5133+
if( ((ASRUtils::expr_value(m_arg) &&
50985134
!ASR::is_a<ASR::ArrayConstant_t>(*ASRUtils::expr_value(m_arg))) ||
5099-
ASRUtils::expr_value(m_arg) == nullptr ) {
5135+
ASRUtils::expr_value(m_arg) == nullptr) &&
5136+
!ASR::is_a<ASR::ArrayConstructor_t>(*m_arg) ) {
51005137
tmp = llvm_utils->create_gep(tmp, 0);
51015138
}
51025139
} else {
@@ -6396,7 +6433,8 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
63966433

63976434
}
63986435

6399-
void visit_ArrayConstructor(const ASR::ArrayConstructor_t &x) {
6436+
template <typename T>
6437+
void visit_ArrayConstructorUtil(const T& x) {
64006438
llvm::Type* el_type = nullptr;
64016439
ASR::ttype_t* x_m_type = ASRUtils::type_get_past_array(x.m_type);
64026440
if (ASR::is_a<ASR::Integer_t>(*x_m_type)) {
@@ -6444,52 +6482,12 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
64446482
tmp = llvm_utils->create_gep(p_fxn, 0);
64456483
}
64466484

6485+
void visit_ArrayConstructor(const ASR::ArrayConstructor_t &x) {
6486+
visit_ArrayConstructorUtil(x);
6487+
}
6488+
64476489
void visit_ArrayConstant(const ASR::ArrayConstant_t &x) {
6448-
llvm::Type* el_type = nullptr;
6449-
ASR::ttype_t* x_m_type = ASRUtils::type_get_past_array(x.m_type);
6450-
if (ASR::is_a<ASR::Integer_t>(*x_m_type)) {
6451-
el_type = llvm_utils->getIntType(ASR::down_cast<ASR::Integer_t>(x_m_type)->m_kind);
6452-
} else if (ASR::is_a<ASR::Real_t>(*x_m_type)) {
6453-
switch (ASR::down_cast<ASR::Real_t>(x_m_type)->m_kind) {
6454-
case (4) :
6455-
el_type = llvm::Type::getFloatTy(context); break;
6456-
case (8) :
6457-
el_type = llvm::Type::getDoubleTy(context); break;
6458-
default :
6459-
throw CodeGenError("ConstArray real kind not supported yet");
6460-
}
6461-
} else if (ASR::is_a<ASR::Logical_t>(*x_m_type)) {
6462-
el_type = llvm::Type::getInt1Ty(context);
6463-
} else if (ASR::is_a<ASR::Character_t>(*x_m_type)) {
6464-
el_type = character_type;
6465-
} else if (ASR::is_a<ASR::Complex_t>(*x_m_type)) {
6466-
int complex_kind = ASR::down_cast<ASR::Complex_t>(x_m_type)->m_kind;
6467-
if( complex_kind == 4 ) {
6468-
el_type = llvm_utils->complex_type_4;
6469-
} else if( complex_kind == 8 ) {
6470-
el_type = llvm_utils->complex_type_8;
6471-
} else {
6472-
LCOMPILERS_ASSERT(false);
6473-
}
6474-
} else {
6475-
throw CodeGenError("ConstArray type not supported yet");
6476-
}
6477-
// Create <n x float> type, where `n` is the length of the `x` constant array
6478-
llvm::Type* type_fxn = FIXED_VECTOR_TYPE::get(el_type, x.n_args);
6479-
// Create a pointer <n x float>* to a stack allocated <n x float>
6480-
llvm::AllocaInst *p_fxn = builder->CreateAlloca(type_fxn, nullptr);
6481-
// Assign the array elements to `p_fxn`.
6482-
for (size_t i=0; i < x.n_args; i++) {
6483-
llvm::Value *llvm_el = llvm_utils->create_gep(p_fxn, i);
6484-
ASR::expr_t *el = x.m_args[i];
6485-
int64_t ptr_loads_copy = ptr_loads;
6486-
ptr_loads = 2;
6487-
this->visit_expr_wrapper(el, true);
6488-
ptr_loads = ptr_loads_copy;
6489-
builder->CreateStore(tmp, llvm_el);
6490-
}
6491-
// Return the vector as float* type:
6492-
tmp = llvm_utils->create_gep(p_fxn, 0);
6490+
visit_ArrayConstructorUtil(x);
64936491
}
64946492

64956493
void visit_Assert(const ASR::Assert_t &x) {
@@ -6654,7 +6652,8 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
66546652
this->visit_expr_wrapper(x->m_value, true);
66556653
return;
66566654
}
6657-
ASR::ttype_t *t2_ = ASRUtils::type_get_past_array(x->m_type);
6655+
ASR::ttype_t *t2_ = ASRUtils::type_get_past_const(
6656+
ASRUtils::type_get_past_array(x->m_type));
66586657
switch( t2_->type ) {
66596658
case ASR::ttypeType::Pointer:
66606659
case ASR::ttypeType::Allocatable: {
@@ -7248,6 +7247,19 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
72487247
tmp = builder->CreateSelect(cmp, zero_str, one_str);
72497248
break;
72507249
}
7250+
case (ASR::cast_kindType::ListToArray) : {
7251+
if( !ASR::is_a<ASR::List_t>(*ASRUtils::expr_type(x.m_arg)) ) {
7252+
throw CodeGenError("The argument of ListToArray cast should "
7253+
"be a list/std::vector, found, " + ASRUtils::type_to_str(
7254+
ASRUtils::expr_type(x.m_arg)));
7255+
}
7256+
int64_t ptr_loads_copy = ptr_loads;
7257+
ptr_loads = 0;
7258+
this->visit_expr(*x.m_arg);
7259+
ptr_loads = ptr_loads_copy;
7260+
tmp = LLVM::CreateLoad(*builder, list_api->get_pointer_to_list_data(tmp));
7261+
break;
7262+
}
72517263
default : throw CodeGenError("Cast kind not implemented");
72527264
}
72537265
}
@@ -9792,6 +9804,7 @@ Result<std::unique_ptr<LLVMModule>> asr_to_llvm(ASR::TranslationUnit_t &asr,
97929804
v.module->print(os, nullptr);
97939805
std::cout << os.str();
97949806
msg = "asr_to_llvm: module failed verification. Error:\n" + err.str();
9807+
std::cout << msg << std::endl;
97959808
diagnostics.diagnostics.push_back(diag::Diagnostic(msg,
97969809
diag::Level::Error, diag::Stage::CodeGen));
97979810
Error error;

src/libasr/intrinsic_func_registry_util_gen.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
"ret_type_arg_idx": 0
3939
},
4040
],
41+
"Modulo": [
42+
{
43+
"args": [("int", "int"), ("real", "real")],
44+
"ret_type_arg_idx": 0
45+
},
46+
],
4147
"BesselJ0": [
4248
{
4349
"args": [("real",)],

src/libasr/pass/array_op.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,9 @@ class ReplaceArrayOp: public ASR::BaseExprReplacer<ReplaceArrayOp> {
10171017

10181018

10191019
void replace_Cast(ASR::Cast_t* x) {
1020+
if( x->m_kind == ASR::cast_kindType::ListToArray ) {
1021+
return ;
1022+
}
10201023
const Location& loc = x->base.base.loc;
10211024
ASR::Cast_t* x_ = x;
10221025
if( ASR::is_a<ASR::ArrayReshape_t>(*x->m_arg) ) {

0 commit comments

Comments
 (0)