Skip to content

Commit

Permalink
Merge pull request #23 from czgdp1807/lc_14
Browse files Browse the repository at this point in the history
Reshape ``ArrayConstant`` if needed when initialising an array variable
  • Loading branch information
czgdp1807 authored Dec 18, 2023
2 parents 08dd2b9 + 3597b8d commit d7e1c43
Show file tree
Hide file tree
Showing 11 changed files with 393 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ jobs:
shell: bash -l -e {0}
if: contains(matrix.os, 'ubuntu') || contains(matrix.os, 'macos')
run: |
export CPATH=$CONDA_PREFIX/include:$CPATH
lc --ast-dump tests/test.cpp
lc --extra-arg=-Isrc/runtime/include examples/expr2.c --asr-dump
./run_tests.py
Expand All @@ -77,5 +78,6 @@ jobs:
shell: bash -l -e {0}
if: contains(matrix.os, 'ubuntu') || contains(matrix.os, 'macos')
run: |
export CPATH=$CONDA_PREFIX/include:$CPATH
./integration_tests/run_tests.py -b gcc llvm wasm c
./integration_tests/run_tests.py -b gcc llvm wasm c -f
19 changes: 13 additions & 6 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ macro(RUN_UTIL RUN_FAIL RUN_NAME RUN_FILE_NAME RUN_LABELS RUN_EXTRAFILES RUN_EXT
OR (LC_BACKEND STREQUAL "c") OR (LC_BACKEND STREQUAL "fortran"))
add_custom_command(
OUTPUT ${name}.o
COMMAND ${LC} -c ${extra_args} ${CMAKE_CURRENT_SOURCE_DIR}/${file_name} -o ${name}.o --extra-arg -I${CMAKE_CURRENT_SOURCE_DIR}/../src/runtime/include --
COMMAND ${LC} -c ${CMAKE_CURRENT_SOURCE_DIR}/${file_name} -o ${name}.o ${extra_args} --
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${file_name}
VERBATIM)
add_executable(${name} ${name}.o ${extra_files})
Expand Down Expand Up @@ -176,10 +176,17 @@ endmacro(RUN)
# x86 --- compile to x86 binary directly
# wasm --- compile to WASM binary directly

RUN(NAME expr1.c LABELS gcc c wasm llvm NOFAST)
RUN(NAME expr2.c LABELS gcc c wasm llvm NOFAST)
RUN(NAME expr3.c FAIL LABELS gcc c wasm llvm NOFAST)
RUN(NAME expr1.c LABELS gcc c wasm llvm NOFAST
EXTRA_ARGS --extra-arg=-I${CMAKE_CURRENT_SOURCE_DIR}/../src/runtime/include)
RUN(NAME expr2.c LABELS gcc c wasm llvm NOFAST
EXTRA_ARGS --extra-arg=-I${CMAKE_CURRENT_SOURCE_DIR}/../src/runtime/include)
RUN(NAME expr3.c FAIL LABELS gcc c wasm llvm NOFAST
EXTRA_ARGS --extra-arg=-I${CMAKE_CURRENT_SOURCE_DIR}/../src/runtime/include)

# arrays
RUN(NAME array_01.cpp LABELS gcc llvm NOFAST)
RUN(NAME array_02.cpp LABELS gcc llvm NOFAST)
RUN(NAME array_01.cpp LABELS gcc llvm NOFAST
EXTRA_ARGS --extra-arg=-I${CMAKE_CURRENT_SOURCE_DIR}/../src/runtime/include)
RUN(NAME array_02.cpp LABELS gcc llvm NOFAST
EXTRA_ARGS --extra-arg=-I${CMAKE_CURRENT_SOURCE_DIR}/../src/runtime/include)
RUN(NAME array_03.cpp LABELS gcc llvm NOFAST
EXTRA_ARGS --extra-arg=-I${CONDA_PREFIX}/include)
19 changes: 19 additions & 0 deletions integration_tests/array_03.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <iostream>
#include "xtensor/xtensor.hpp"
#include "xtensor/xio.hpp"
#include "xtensor/xview.hpp"

int main() {

xt::xtensor<double, 2> arr1; /* { // TODO: Uncomment this initializer
{1.0, 2.0, 3.0},
{2.0, 5.0, 7.0},
{2.0, 5.0, 7.0}}; */

xt::xtensor<double, 1> arr2 {5.0, 6.0, 7.0};

// xt::xarray<double> res = xt::view(arr1, 1) + arr2; // TODO: Uncomment this statement
std::cout << arr2;

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

int main() {

xt::xtensor<double, 2> arr1 = {
{1.0, 2.0, 3.0},
{2.0, 5.0, 7.0},
{2.0, 5.0, 7.0}};

xt::xtensor<double, 1> arr2 {5.0, 6.0, 7.0};

// xt::xarray<double> res = xt::view(arr1, 1) + arr2; // TODO: Uncomment this statement
std::cout << arr2;

return 0;
}
2 changes: 1 addition & 1 deletion integration_tests/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def main():

# Setup
global NO_OF_THREADS, fast_tests
os.environ["PATH"] += os.pathsep + LC_PATH
# os.environ["PATH"] += os.pathsep + LC_PATH
# delete previously created directories (if any)
for backend in SUPPORTED_BACKENDS:
run_cmd(f"rm -rf {BASE_DIR}/test-{backend}")
Expand Down
2 changes: 1 addition & 1 deletion run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,4 @@ def is_included(backend):
filename, update_reference, extra_args)

if __name__ == "__main__":
tester_main("LC", single_test)
tester_main("LC", single_test, True)
39 changes: 39 additions & 0 deletions src/lc/clang_ast_to_asr.h
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,41 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
return true;
}

void add_reshape_if_needed(ASR::expr_t*& expr, ASR::expr_t* target_expr) {
ASR::ttype_t* expr_type = ASRUtils::expr_type(expr);
ASR::ttype_t* target_expr_type = ASRUtils::expr_type(target_expr);
ASR::dimension_t *expr_dims = nullptr, *target_expr_dims = nullptr;
size_t expr_rank = ASRUtils::extract_dimensions_from_ttype(expr_type, expr_dims);
size_t target_expr_rank = ASRUtils::extract_dimensions_from_ttype(target_expr_type, target_expr_dims);
if( expr_rank == target_expr_rank ||
ASRUtils::extract_physical_type(target_expr_type) ==
ASR::array_physical_typeType::FixedSizeArray ) {
return ;
}

const Location& loc = expr->base.loc;
Vec<ASR::expr_t*> new_shape_; new_shape_.reserve(al, target_expr_rank);
for( size_t i = 0; i < target_expr_rank; i++ ) {
new_shape_.push_back(al, ASRUtils::get_size(target_expr, i + 1, al));
}

Vec<ASR::dimension_t> new_shape_dims; new_shape_dims.reserve(al, 1);
ASR::dimension_t new_shape_dim; new_shape_dim.loc = loc;
new_shape_dim.m_length = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, loc,
target_expr_rank, ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 4))));
new_shape_dim.m_start = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, loc,
0, ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 4))));
new_shape_dims.push_back(al, new_shape_dim);
ASR::ttype_t* new_shape_type = ASRUtils::TYPE(ASR::make_Array_t(al, loc,
ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 4)), new_shape_dims.p,
new_shape_dims.size(), ASR::array_physical_typeType::FixedSizeArray));
ASR::expr_t* new_shape = ASRUtils::EXPR(ASR::make_ArrayConstant_t(al, loc,
new_shape_.p, new_shape_.size(), new_shape_type, ASR::arraystorageType::RowMajor));
ASR::expr_t* reshaped_expr = ASRUtils::EXPR(ASR::make_ArrayReshape_t(al, loc, expr,
new_shape, target_expr_type, nullptr));
expr = reshaped_expr;
}

bool TraverseVarDecl(clang::VarDecl *x) {
std::string name = x->getName().str();
if( scopes.size() > 0 ) {
Expand Down Expand Up @@ -551,6 +586,7 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
assignment_target = nullptr;
if( tmp != nullptr ) {
ASR::expr_t* init_val = ASRUtils::EXPR(tmp);
add_reshape_if_needed(init_val, var);
tmp = ASR::make_Assignment_t(al, Lloc(x), var, init_val, nullptr);
is_stmt_created = true;
}
Expand Down Expand Up @@ -635,10 +671,13 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
Vec<ASR::expr_t*> init_exprs;
init_exprs.reserve(al, x->getNumInits());
clang::Expr** clang_inits = x->getInits();
ASR::expr_t* assignment_target_copy = assignment_target;
assignment_target = nullptr;
for( size_t i = 0; i < x->getNumInits(); i++ ) {
TraverseStmt(clang_inits[i]);
init_exprs.push_back(al, ASRUtils::EXPR(tmp));
}
assignment_target = assignment_target_copy;
ASR::ttype_t* type = ASRUtils::expr_type(init_exprs[init_exprs.size() - 1]);
Vec<ASR::dimension_t> dims; dims.reserve(al, 1);
ASR::dimension_t dim; dim.loc = Lloc(x);
Expand Down
7 changes: 4 additions & 3 deletions src/libasr/compiler_tester/tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ def run_test(testname, basename, cmd, infile, update_reference=False,
log.debug(s + " " + check())


def tester_main(compiler, single_test):
def tester_main(compiler, single_test, is_lcompilers_executable_installed=False):
parser = argparse.ArgumentParser(description=f"{compiler} Test Suite")
parser.add_argument("-u", "--update", action="store_true",
help="update all reference results")
Expand Down Expand Up @@ -387,8 +387,9 @@ def tester_main(compiler, single_test):
no_color = args.no_color

# So that the tests find the `lcompiler` executable
os.environ["PATH"] = os.path.join(SRC_DIR, "bin") \
+ os.pathsep + os.environ["PATH"]
if not is_lcompilers_executable_installed:
os.environ["PATH"] = os.path.join(SRC_DIR, "bin") \
+ os.pathsep + os.environ["PATH"]
test_data = toml.load(open(os.path.join(ROOT_DIR, "tests", "tests.toml")))
filtered_tests = test_data["test"]
if specific_tests:
Expand Down
13 changes: 13 additions & 0 deletions tests/reference/asr-array_04-be6ac8a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "asr-array_04-be6ac8a",
"cmd": "lc --asr-dump --no-color {infile} -o {outfile} -extra-arg=\"\"",
"infile": "tests/../integration_tests/array_04.cpp",
"infile_hash": "a3e79a9500684211e6e080dc262e6f1e5005e257df6d05381f9dc9c6",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-array_04-be6ac8a.stdout",
"stdout_hash": "8ccda65df8bedf43f3a0c20870b16ce84c05bc2dad0df3e41ddd122e",
"stderr": null,
"stderr_hash": null,
"returncode": 0
}
Loading

0 comments on commit d7e1c43

Please sign in to comment.