Skip to content

Commit 3aa037f

Browse files
authored
Merge pull request #41 from czgdp1807/lc_23
Ported ``integration_tests/arrays_op_28.f90`` from LFortran and improve LC to support it
2 parents 3152c22 + b9bf59e commit 3aa037f

File tree

5 files changed

+57
-13
lines changed

5 files changed

+57
-13
lines changed

integration_tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,5 @@ RUN(NAME array_10.cpp LABELS gcc llvm NOFAST
200200
EXTRA_ARGS --extra-arg=-I${CONDA_PREFIX}/include)
201201
RUN(NAME array_11.cpp LABELS gcc llvm NOFAST
202202
EXTRA_ARGS --extra-arg=-I${CONDA_PREFIX}/include)
203+
RUN(NAME array_12.cpp LABELS gcc llvm NOFAST
204+
EXTRA_ARGS --extra-arg=-I${CONDA_PREFIX}/include)

integration_tests/array_12.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <iostream>
2+
#include <xtensor/xtensor.hpp>
3+
#include <xtensor/xfixed.hpp>
4+
#include "xtensor/xio.hpp"
5+
#include "xtensor/xview.hpp"
6+
7+
xt::xtensor<double, 1> func(const xt::xtensor<double, 1>& R, const xt::xtensor<double, 1>& V) {
8+
xt::xtensor<double, 1> Vmid = xt::empty<double>({R.shape(0) - 1});
9+
int i;
10+
11+
for( i = 0; i < Vmid.shape(0); i++ ) {
12+
Vmid(i) = R(i)*V(i);
13+
}
14+
15+
return Vmid;
16+
}
17+
18+
int main() {
19+
20+
xt::xtensor_fixed<double, xt::xshape<5>> R;
21+
xt::xtensor_fixed<double, xt::xshape<5>> V;
22+
xt::xtensor_fixed<double, xt::xshape<3>> Vmid;
23+
24+
R.fill(45.0);
25+
V.fill(23.0);
26+
27+
xt::view(Vmid, xt::range(0, 3)) = func(xt::view(R, xt::range(0, 4)), xt::view(V, xt::range(0, 4)));
28+
std::cout << Vmid << std::endl;
29+
if( xt::any(xt::not_equal(Vmid, 1035.00)) ) {
30+
exit(2);
31+
}
32+
33+
return 0;
34+
}

src/lc/clang_ast_to_asr.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,11 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
433433
name = current_scope->get_unique_name("param");
434434
}
435435
ASR::ttype_t* type = ClangTypeToASRType(x->getType());
436+
ASR::intentType intent_type = ASR::intentType::InOut;
437+
if( ASR::is_a<ASR::Const_t>(*type) ) {
438+
intent_type = ASR::intentType::In;
439+
type = ASRUtils::type_get_past_const(type);
440+
}
436441
if( x->getType()->getTypeClass() != clang::Type::LValueReference &&
437442
ASRUtils::is_array(type) ) {
438443
throw std::runtime_error("Array objects should be passed by reference only.");
@@ -1188,8 +1193,8 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
11881193
tmp = ASR::make_RealCompare_t(al, loc, lhs,
11891194
cmpop_type, rhs, result_type, nullptr);
11901195
} else {
1191-
throw SemanticError("Only integer and real types are supported so "
1192-
"far for comparison operator", loc);
1196+
throw std::runtime_error("Only integer and real types are supported so "
1197+
"far for comparison operator, found: " + ASRUtils::type_to_str(ASRUtils::expr_type(lhs)));
11931198
}
11941199
}
11951200

@@ -1205,8 +1210,8 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
12051210
tmp = ASR::make_RealBinOp_t(al, loc, lhs,
12061211
binop_type, rhs, ASRUtils::expr_type(lhs), nullptr);
12071212
} else {
1208-
throw SemanticError("Only integer and real types are supported so "
1209-
"far for binary operator", loc);
1213+
throw std::runtime_error("Only integer and real types are supported so "
1214+
"far for binary operator, found: " + ASRUtils::type_to_str(ASRUtils::expr_type(lhs)));
12101215
}
12111216
}
12121217

@@ -1441,7 +1446,7 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
14411446
break;
14421447
}
14431448
default: {
1444-
throw SemanticError("Only postfix increment is supported so far", Lloc(x));
1449+
throw std::runtime_error("Only postfix increment is supported so far");
14451450
}
14461451
}
14471452
return true;

src/libasr/asr_utils.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1796,8 +1796,9 @@ static inline bool is_pointer(ASR::ttype_t *x) {
17961796
static inline bool is_integer(ASR::ttype_t &x) {
17971797
return ASR::is_a<ASR::Integer_t>(
17981798
*type_get_past_array(
1799-
type_get_past_allocatable(
1800-
type_get_past_pointer(&x))));
1799+
type_get_past_allocatable(
1800+
type_get_past_pointer(
1801+
type_get_past_const(&x)))));
18011802
}
18021803

18031804
static inline bool is_unsigned_integer(ASR::ttype_t &x) {
@@ -1810,8 +1811,9 @@ static inline bool is_unsigned_integer(ASR::ttype_t &x) {
18101811
static inline bool is_real(ASR::ttype_t &x) {
18111812
return ASR::is_a<ASR::Real_t>(
18121813
*type_get_past_array(
1813-
type_get_past_allocatable(
1814-
type_get_past_pointer(&x))));
1814+
type_get_past_allocatable(
1815+
type_get_past_pointer(
1816+
type_get_past_const(&x)))));
18151817
}
18161818

18171819
static inline bool is_character(ASR::ttype_t &x) {
@@ -1978,7 +1980,8 @@ inline int extract_dimensions_from_ttype(ASR::ttype_t *x,
19781980
static inline ASR::ttype_t *extract_type(ASR::ttype_t *type) {
19791981
return type_get_past_array(
19801982
type_get_past_allocatable(
1981-
type_get_past_pointer(type)));
1983+
type_get_past_pointer(
1984+
type_get_past_const(type))));
19821985
}
19831986

19841987
static inline bool is_fixed_size_array(ASR::dimension_t* m_dims, size_t n_dims) {

src/libasr/codegen/llvm_array_utils.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ namespace LCompilers {
412412
builder->CreateStore(stride, s_val);
413413
llvm::Value* l_val = llvm_utils->create_gep(dim_val, 1);
414414
llvm::Value* dim_size_ptr = llvm_utils->create_gep(dim_val, 2);
415-
builder->CreateStore(llvm::ConstantInt::get(context, llvm::APInt(32, 1)), l_val);
415+
builder->CreateStore(llvm::ConstantInt::get(context, llvm::APInt(32, 0)), l_val);
416416
llvm::Value* dim_size = this->get_dimension_size(
417417
this->get_pointer_to_dimension_descriptor(source_dim_des_arr,
418418
llvm::ConstantInt::get(context, llvm::APInt(32, r))));
@@ -464,7 +464,7 @@ namespace LCompilers {
464464
llvm::Value* target_stride = get_stride(target_dim_des, false);
465465
builder->CreateStore(value_stride, target_stride);
466466
// Diverges from LPython, 0 should be stored there.
467-
builder->CreateStore(llvm::ConstantInt::get(llvm::Type::getInt32Ty(context), llvm::APInt(32, 1)),
467+
builder->CreateStore(llvm::ConstantInt::get(llvm::Type::getInt32Ty(context), llvm::APInt(32, 0)),
468468
get_lower_bound(target_dim_des, false));
469469
builder->CreateStore(dim_length,
470470
get_dimension_size(target_dim_des, false));
@@ -517,7 +517,7 @@ namespace LCompilers {
517517
llvm::Value* target_dim_des = llvm_utils->create_ptr_gep(target_dim_des_array, j);
518518
builder->CreateStore(stride,
519519
get_stride(target_dim_des, false));
520-
builder->CreateStore(llvm::ConstantInt::get(llvm::Type::getInt32Ty(context), llvm::APInt(32, 1)),
520+
builder->CreateStore(llvm::ConstantInt::get(llvm::Type::getInt32Ty(context), llvm::APInt(32, 0)),
521521
get_lower_bound(target_dim_des, false));
522522
builder->CreateStore(dim_length,
523523
get_dimension_size(target_dim_des, false));

0 commit comments

Comments
 (0)