From 37a717ee4aace97de2ce6baa8ca87c2e5274ad32 Mon Sep 17 00:00:00 2001 From: advik Date: Wed, 17 Jul 2024 00:53:32 +0530 Subject: [PATCH 1/3] Add support for in place sets and dictionaries in function calls --- integration_tests/test_dict_14.py | 6 ++++++ integration_tests/test_global_set.py | 6 ++++++ src/libasr/codegen/asr_to_llvm.cpp | 12 +++++++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/integration_tests/test_dict_14.py b/integration_tests/test_dict_14.py index 4fe91c687f..c25ae15fe5 100644 --- a/integration_tests/test_dict_14.py +++ b/integration_tests/test_dict_14.py @@ -1,5 +1,8 @@ from lpython import i32 +def takes_dict(a: dict[i32, i32]) -> dict[i32, i32]: + return {1:1, 2:2} + def test_dict(): d_i32: dict[i32, i32] = {5: 1, 5: 2} d_str: dict[str, i32] = {'a': 1, 'a': 2} @@ -62,4 +65,7 @@ def test_dict(): l_i32_2.append(i) assert l_i32_2 == [30] + w: dict[i32, i32] = takes_dict({1:1, 2:2}) + assert len(w) == 2 + test_dict() diff --git a/integration_tests/test_global_set.py b/integration_tests/test_global_set.py index 487f12d108..ffffbdd3f4 100644 --- a/integration_tests/test_global_set.py +++ b/integration_tests/test_global_set.py @@ -1,9 +1,15 @@ from lpython import i32 +def takes_set(a: set[i32]) -> set[i32]: + return {1, 2, 3} + s1: set[str] = {"a", "b", "c", "a"} s2: set[i32] = {1, 2, 3, 1} s3: set[tuple[i32, i32]] = {(1, 2), (2, 3), (4, 5)} +s4: set[i32] = takes_set({1, 2}) + assert len(s1) == 3 assert len(s2) == 3 assert len(s3) == 3 +assert len(s4) == 3 diff --git a/src/libasr/codegen/asr_to_llvm.cpp b/src/libasr/codegen/asr_to_llvm.cpp index 407be4083b..4414f75548 100644 --- a/src/libasr/codegen/asr_to_llvm.cpp +++ b/src/libasr/codegen/asr_to_llvm.cpp @@ -8859,6 +8859,14 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor target_type = llvm_utils->get_type_from_ttype_t_util(arg_type_, module.get()); break; } + case (ASR::ttypeType::Dict): { + target_type = llvm_utils->get_type_from_ttype_t_util(arg_type_, module.get()); + break; + } + case (ASR::ttypeType::Set): { + target_type = llvm_utils->get_type_from_ttype_t_util(arg_type_, module.get()); + break; + } default : throw CodeGenError("Type " + ASRUtils::type_to_str(arg_type) + " not implemented yet."); } @@ -8913,7 +8921,9 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor llvm::AllocaInst *target = builder0.CreateAlloca( target_type, nullptr, "call_arg_value"); if( ASR::is_a(*arg_type) || - ASR::is_a(*arg_type) ) { + ASR::is_a(*arg_type) || + ASR::is_a(*arg_type) || + ASR::is_a(*arg_type)) { llvm_utils->deepcopy(value, target, arg_type, module.get(), name2memidx); } else { builder->CreateStore(value, target); From 5dd163c00ccdc03b536a9f54cbe2c9ddaf4a984d Mon Sep 17 00:00:00 2001 From: advik Date: Fri, 26 Jul 2024 14:31:30 +0530 Subject: [PATCH 2/3] Skip testing for fast --- integration_tests/CMakeLists.txt | 1 + integration_tests/test_dict_14.py | 6 ------ integration_tests/test_global_set.py | 6 ------ integration_tests/test_params.py | 12 ++++++++++++ 4 files changed, 13 insertions(+), 12 deletions(-) create mode 100644 integration_tests/test_params.py diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index e35e0d2397..ddc335f7aa 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -569,6 +569,7 @@ RUN(NAME test_tuple_04 LABELS cpython llvm llvm_jit c) RUN(NAME test_tuple_concat LABELS cpython llvm llvm_jit) RUN(NAME test_tuple_nested LABELS cpython llvm llvm_jit) RUN(NAME test_const_dict LABELS cpython llvm llvm_jit) +RUN(NAME test_params LABELS cpython llvm llvm_jit NOFAST) RUN(NAME test_dict_01 LABELS cpython llvm llvm_jit c) RUN(NAME test_dict_02 LABELS cpython llvm llvm_jit c NOFAST) RUN(NAME test_dict_03 LABELS cpython llvm llvm_jit NOFAST) diff --git a/integration_tests/test_dict_14.py b/integration_tests/test_dict_14.py index c25ae15fe5..4fe91c687f 100644 --- a/integration_tests/test_dict_14.py +++ b/integration_tests/test_dict_14.py @@ -1,8 +1,5 @@ from lpython import i32 -def takes_dict(a: dict[i32, i32]) -> dict[i32, i32]: - return {1:1, 2:2} - def test_dict(): d_i32: dict[i32, i32] = {5: 1, 5: 2} d_str: dict[str, i32] = {'a': 1, 'a': 2} @@ -65,7 +62,4 @@ def test_dict(): l_i32_2.append(i) assert l_i32_2 == [30] - w: dict[i32, i32] = takes_dict({1:1, 2:2}) - assert len(w) == 2 - test_dict() diff --git a/integration_tests/test_global_set.py b/integration_tests/test_global_set.py index ffffbdd3f4..487f12d108 100644 --- a/integration_tests/test_global_set.py +++ b/integration_tests/test_global_set.py @@ -1,15 +1,9 @@ from lpython import i32 -def takes_set(a: set[i32]) -> set[i32]: - return {1, 2, 3} - s1: set[str] = {"a", "b", "c", "a"} s2: set[i32] = {1, 2, 3, 1} s3: set[tuple[i32, i32]] = {(1, 2), (2, 3), (4, 5)} -s4: set[i32] = takes_set({1, 2}) - assert len(s1) == 3 assert len(s2) == 3 assert len(s3) == 3 -assert len(s4) == 3 diff --git a/integration_tests/test_params.py b/integration_tests/test_params.py new file mode 100644 index 0000000000..d7f5370030 --- /dev/null +++ b/integration_tests/test_params.py @@ -0,0 +1,12 @@ +def takes_set(a: set[i32]) -> set[i32]: + return {1, 2, 3} + +def takes_dict(a: dict[i32, i32]) -> dict[i32, i32]: + return {1:1, 2:2} + +s: set[i32] = takes_set({1, 2}) + +assert len(s) == 3 + +w: dict[i32, i32] = takes_dict({1:1, 2:2}) +assert len(w) == 2 From 4d91f3cefac2f35353d8884e083770d7faa35e30 Mon Sep 17 00:00:00 2001 From: advik Date: Fri, 26 Jul 2024 14:44:35 +0530 Subject: [PATCH 3/3] Import i32 --- integration_tests/test_params.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/integration_tests/test_params.py b/integration_tests/test_params.py index d7f5370030..0748dcfd2a 100644 --- a/integration_tests/test_params.py +++ b/integration_tests/test_params.py @@ -1,3 +1,5 @@ +from lpython import i32 + def takes_set(a: set[i32]) -> set[i32]: return {1, 2, 3}