From 65c7efbcf3013411fcfbdf2e0d32e68d5f3a94e2 Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Mon, 11 Mar 2024 16:06:38 +0530 Subject: [PATCH 1/3] DEV: Added support for std::vector::clear and std::vector::size --- src/lc/clang_ast_to_asr.cpp | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/lc/clang_ast_to_asr.cpp b/src/lc/clang_ast_to_asr.cpp index 773f08c..4eaf8ce 100644 --- a/src/lc/clang_ast_to_asr.cpp +++ b/src/lc/clang_ast_to_asr.cpp @@ -39,6 +39,7 @@ enum SpecialFunc { Iota, Sqrt, PushBack, + Clear, }; std::map special_function_map = { @@ -66,6 +67,7 @@ std::map special_function_map = { {"operator\"\"i", SpecialFunc::Iota}, {"sqrt", SpecialFunc::Sqrt}, {"push_back", SpecialFunc::PushBack}, + {"clear", SpecialFunc::Clear}, }; class OneTimeUseString { @@ -1166,12 +1168,19 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor(*ASRUtils::expr_type(callee)) ) { + tmp = ASR::make_ListLen_t(al, Lloc(x), callee, + ASRUtils::TYPE(ASR::make_Integer_t(al, Lloc(x), 4)), nullptr); + } else { + throw std::runtime_error("Only xt::xtensor::size and std::vector::size are supported."); + } } else if (sf == SpecialFunc::Fill) { if( args.size() != 1 ) { throw std::runtime_error("xt::xtensor::fill should be called with only one argument."); @@ -1333,6 +1342,13 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor 1 ) { + throw std::runtime_error("std::vector::clear should be called with only one argument."); + } + + tmp = ASR::make_ListClear_t(al, Lloc(x), callee); + is_stmt_created = true; } else { throw std::runtime_error("Only printf and exit special functions supported"); } From 44a0135071516f583af8721346c1c2900f6ad445 Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Mon, 11 Mar 2024 16:07:36 +0530 Subject: [PATCH 2/3] TEST: Ported integration_tests/test_list_06.py from LPython --- integration_tests/vector_02.cpp | 96 +++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 integration_tests/vector_02.cpp diff --git a/integration_tests/vector_02.cpp b/integration_tests/vector_02.cpp new file mode 100644 index 0000000..31d2ea9 --- /dev/null +++ b/integration_tests/vector_02.cpp @@ -0,0 +1,96 @@ +#include +#include + +#define assert(cond) if( !(cond) ) { \ + exit(2); \ +} \ + +void check_mat_and_vec(std::vector>& mat, std::vector& vec) { + int32_t rows = mat.size(); + int32_t cols = vec.size(); + int32_t i; + int32_t j; + + for( i = 0; i < rows; i++ ) { + for( j = 0; j < cols; j++ ) { + assert( mat[i][j] == double(i + j) ); + } + } + + for( i = 0; i < cols; i++ ) { + assert( vec[i] == 2.0 * float(i) ); + } +} + +void test_list_of_lists() { + std::vector>>> arrays = {}; + std::vector>> array = {}; + std::vector> mat = {}; + std::vector vec = {}; + int32_t rows = 10; + int32_t cols = 5; + int32_t i; + int32_t j; + int32_t k; + int32_t l; + + for( i = 0; i < rows; i++ ) { + for( j = 0; j < cols; j++ ) { + vec.push_back(double(i + j)); + } + mat.push_back(vec); + vec.clear(); + } + + for( i = 0; i < cols; i++ ) { + vec.push_back(2.0 * double(i)); + } + + check_mat_and_vec(mat, vec); + + for( k = 0; k < rows; k++ ) { + array.push_back(mat); + for( i = 0; i < rows; i++ ) { + for( j = 0; j < cols; j++ ) { + mat[i][j] += double(1); + } + } + } + + for( k = 0; k < rows; k++ ) { + for( i = 0; i < rows; i++ ) { + for( j = 0; j < cols; j++ ) { + assert( mat[i][j] - array[k][i][j] == double(rows - k) ); + } + } + } + + for( l = 0; l < 2 * rows; l++ ) { + arrays.push_back(array); + for( i = 0; i < rows; i++ ) { + for( j = 0; j < rows; j++ ) { + for( k = 0; k < cols; k++ ) { + array[i][j][k] += 1.0; + } + } + } + } + + for( l = 0; l < 2 * rows; l++ ) { + for( i = 0; i < rows; i++ ) { + for( j = 0; j < rows; j++ ) { + for( k = 0; k < cols; k++ ) { + std::cout << (array[i][j][k] - arrays[l][i][j][k]); + std::cout << 2 * rows - l; + assert( array[i][j][k] - arrays[l][i][j][k] == double(2 * rows - l) ); + } + } + } + } +} + +int main() { + +test_list_of_lists(); + +} From 60aff8ab4cb4062ce441ff800957348f4e7b6661 Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Mon, 11 Mar 2024 16:07:49 +0530 Subject: [PATCH 3/3] TEST: Register vector_02.cpp --- integration_tests/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index 0988769..7dcca26 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -234,5 +234,6 @@ RUN(NAME union_01.cpp LABELS gcc llvm NOFAST) RUN(NAME union_02.cpp LABELS gcc llvm NOFAST) RUN(NAME vector_01.cpp LABELS gcc llvm NOFAST) +RUN(NAME vector_02.cpp LABELS gcc llvm NOFAST) RUN(NAME loop_01.cpp LABELS gcc llvm NOFAST)