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

test: Port integration_tests/arrays_31.f90, arrays_34.f90 and arrays_35.f90 from LFortran and improve LC to compile it #112

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ RUN(NAME array_22.cpp LABELS gcc llvm)
RUN(NAME array_23.cpp LABELS gcc llvm)
RUN(NAME array_24.cpp LABELS gcc llvm)
RUN(NAME array_25.cpp LABELS gcc llvm)
RUN(NAME array_26.cpp LABELS gcc llvm)
RUN(NAME array_27.cpp LABELS gcc)
RUN(NAME array_28.cpp LABELS gcc)


RUN(NAME struct_01.cpp LABELS gcc llvm)
RUN(NAME struct_02.cpp LABELS gcc llvm)
Expand Down
18 changes: 18 additions & 0 deletions integration_tests/array_26.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <iostream>
#include <cmath>
#include <xtensor/xtensor.hpp>
#include <xtensor/xfixed.hpp>
#include "xtensor/xio.hpp"

// LFortran: ./integration_tests/arrays_31.f90

int main() {
xt::xtensor<int, 1> x = xt::empty<int>({5});

int pqr = x.size();

std::cout << pqr << std::endl;
if (pqr != 5) {
exit(2);
}
}
32 changes: 32 additions & 0 deletions integration_tests/array_27.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <iostream>
#include <cmath>
#include <xtensor/xtensor.hpp>
#include <xtensor/xfixed.hpp>
#include "xtensor/xio.hpp"

// LFortran: ./integration_tests/arrays_34.f90


void sub(xt::xtensor<int, 1>& x, int z) {
switch (z) {
case 1:
x.resize({1}); // Resize x to have size 1
x(0) = 1;
break;
default:
std::cout << "z = " << z << std::endl;
std::cout << "z not supported." << std::endl;
}
}

int main() {
xt::xtensor<int, 1> x;

sub(x, 1);
std::cout << x << std::endl;
if (x(0) != 1) {
exit(2);
}

return 0;
}
16 changes: 16 additions & 0 deletions integration_tests/array_28.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <iostream>
#include <cmath>
#include <xtensor/xtensor.hpp>
#include <xtensor/xfixed.hpp>
#include "xtensor/xio.hpp"

// LFortran: ./integration_tests/arrays_35.f90

int main() {
xt::xtensor<float, 1> x = xt::empty<float>({2});
x = { 9.0, 2.1 };

std::cout << x << std::endl;
if (std::abs(x(0) - 9.0) > 1e-7) exit(2);
if (std::abs(x(1) - 2.1) > 1e-7) exit(2);
}
5 changes: 5 additions & 0 deletions src/libasr/asr_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2482,6 +2482,11 @@ static inline ASR::ttype_t* duplicate_type_without_dims(Allocator& al, const ASR
ASR::TypeParameter_t* tp = ASR::down_cast<ASR::TypeParameter_t>(t);
return ASRUtils::TYPE(ASR::make_TypeParameter_t(al, loc, tp->m_param));
}
case ASR::ttypeType::Const: {
ASR::Const_t* c = ASR::down_cast<ASR::Const_t>(t);
return ASRUtils::TYPE(ASR::make_Const_t(al, loc,
duplicate_type_without_dims(al, c->m_type, loc)));
}
default : throw LCompilersException("Not implemented " + std::to_string(t->type));
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/libasr/casting_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ namespace LCompilers::CastingUtil {
} else {
dest = ASRUtils::extract_type(dest);
}
if (ASR::is_a<ASR::ArrayConstant_t>(*expr)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think casting should be done only on the basis of type. Using expression to decide casting operation seems more like a hard-coded approach.

ASR::ArrayConstant_t* arr = ASR::down_cast<ASR::ArrayConstant_t>(expr);
for (size_t i = 0; i < arr->n_args; i++) {
arr->m_args[i] = ASRUtils::expr_value(ASRUtils::EXPR(ASRUtils::make_Cast_t_value(al, loc, arr->m_args[i],
cast_kind, ASRUtils::expr_type(arr->m_args[i]))));
Comment on lines +152 to +153
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems we are casting each array element to the type of that same array element. So, why is a casting being needed?

}
return ASRUtils::EXPR((ASR::asr_t*) arr);
}
return ASRUtils::EXPR(ASRUtils::make_Cast_t_value(al, loc, expr, cast_kind, dest));
}
}
5 changes: 5 additions & 0 deletions src/libasr/codegen/llvm_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,11 @@ namespace LCompilers {
el_type = character_type;
break;
}
case ASR::ttypeType::Const: {
ASR::ttype_t* t2 = ASR::down_cast<ASR::Const_t>(m_type_)->m_type;
el_type = get_el_type(t2, module);
break;
}
default:
LCOMPILERS_ASSERT(false);
break;
Expand Down
13 changes: 13 additions & 0 deletions tests/reference/asr-array_28-e2b02d8.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "asr-array_28-e2b02d8",
"cmd": "lc --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/../integration_tests/array_28.cpp",
"infile_hash": "d2a5fada0de4142e17c05431214c0b594476f069036ddddbbfeaa334",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-array_28-e2b02d8.stdout",
"stdout_hash": "797d6000c250772de2e9d7017adb9194c0bd9a6694b04d4ff6b472d6",
"stderr": null,
"stderr_hash": null,
"returncode": 0
}
254 changes: 254 additions & 0 deletions tests/reference/asr-array_28-e2b02d8.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
(TranslationUnit
(SymbolTable
1
{
main:
(Function
(SymbolTable
2
{
__return_var:
(Variable
2
__return_var
[]
ReturnVar
()
()
Default
(Integer 4)
()
Source
Public
Required
.false.
),
x:
(Variable
2
x
[]
Local
()
()
Default
(Allocatable
(Array
(Real 8)
[(()
())]
DescriptorArray
)
)
()
Source
Public
Required
.false.
)
})
main
(FunctionType
[]
(Integer 4)
Source
Implementation
()
.false.
.false.
.false.
.false.
.false.
[]
.false.
)
[]
[]
[(Allocate
[((Var 2 x)
[((IntegerConstant 0 (Integer 4))
(IntegerConstant 2 (Integer 4)))]
()
())]
()
()
()
)
(Allocate
[((Var 2 x)
[((IntegerConstant 0 (Integer 4))
(IntegerConstant 2 (Integer 4)))]
()
())]
()
()
()
)
(=
(Var 2 x)
(ArrayConstant
[(RealConstant
9.000000
(Const
(Real 8)
)
)
(RealConstant
2.100000
(Const
(Real 8)
)
)]
(Array
(Const
(Real 8)
)
[((IntegerConstant 0 (Integer 4))
(IntegerConstant 2 (Integer 4)))]
FixedSizeArray
)
RowMajor
)
()
)
(Print
[(Var 2 x)]
(StringConstant
""
(Character 1 1 ())
)
(StringConstant
""
(Character 1 1 ())
)
)
(If
(RealCompare
(IntrinsicScalarFunction
Abs
[(RealBinOp
(Cast
(ArrayItem
(Var 2 x)
[(()
(IntegerConstant 0 (Integer 4))
())]
(Real 8)
RowMajor
()
)
RealToReal
(Real 8)
()
)
Sub
(RealConstant
9.000000
(Real 8)
)
(Real 8)
()
)]
0
(Real 8)
()
)
Gt
(RealConstant
0.000000
(Real 8)
)
(Logical 4)
()
)
[]
[]
)
(If
(RealCompare
(IntrinsicScalarFunction
Abs
[(RealBinOp
(Cast
(ArrayItem
(Var 2 x)
[(()
(IntegerConstant 1 (Integer 4))
())]
(Real 8)
RowMajor
()
)
RealToReal
(Real 8)
()
)
Sub
(RealConstant
2.100000
(Real 8)
)
(Real 8)
()
)]
0
(Real 8)
()
)
Gt
(RealConstant
0.000000
(Real 8)
)
(Logical 4)
()
)
[]
[]
)]
(Var 2 __return_var)
Public
.false.
.false.
()
),
main_program:
(Program
(SymbolTable
3
{
exit_code:
(Variable
3
exit_code
[]
Local
()
()
Default
(Integer 4)
()
Source
Public
Required
.false.
)
})
main_program
[main]
[(=
(Var 3 exit_code)
(FunctionCall
1 main
1 main
[]
(Integer 4)
()
()
)
()
)]
)
})
[]
)
4 changes: 4 additions & 0 deletions tests/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ asr = true
[[test]]
filename = "../integration_tests/array_04.cpp"
asr = true

[[test]]
filename = "../integration_tests/array_28.cpp"
asr = true
Comment on lines +36 to +39
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[[test]]
filename = "../integration_tests/array_28.cpp"
asr = true
# TODO: Remove after integration_tests/array_28.cpp compiles with LC
[[test]]
filename = "../integration_tests/array_28.cpp"
asr = true

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reference testing should be as limited as possible.

Loading