Skip to content

Commit 1652163

Browse files
authored
Merge pull request #2453 from anutosh491/certik/dont_load_cptr
Don't load CPtr if the variable under consideration is an argument
2 parents 25dfb85 + 91e6bd2 commit 1652163

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,7 @@ RUN(NAME symbolics_10 LABELS cpython_sym c_sym llvm_sym NOFAST)
717717
RUN(NAME symbolics_11 LABELS cpython_sym c_sym llvm_sym NOFAST)
718718
RUN(NAME symbolics_12 LABELS cpython_sym c_sym llvm_sym NOFAST)
719719
RUN(NAME symbolics_13 LABELS cpython_sym c_sym llvm_sym NOFAST)
720+
RUN(NAME symbolics_14 LABELS cpython_sym llvm_sym NOFAST)
720721
RUN(NAME test_gruntz LABELS cpython_sym llvm_sym NOFAST)
721722

722723
RUN(NAME sizeof_01 LABELS llvm c

integration_tests/symbolics_14.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from lpython import S
2+
from sympy import Symbol
3+
4+
def mmrv(x: S) -> list[S]:
5+
l1: list[S] = [x]
6+
return l1
7+
8+
def test_mrv1():
9+
x: S = Symbol("x")
10+
ans: list[S] = mmrv(x)
11+
element_1: S = ans[0]
12+
print(element_1)
13+
assert element_1 == x
14+
15+
test_mrv1()

src/libasr/asr_utils.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4737,6 +4737,26 @@ static inline bool is_simd_array(ASR::expr_t *v) {
47374737
== ASR::array_physical_typeType::SIMDArray);
47384738
}
47394739

4740+
static inline bool is_argument_of_type_CPtr(ASR::expr_t *var) {
4741+
bool is_argument = false;
4742+
if (ASR::is_a<ASR::CPtr_t>(*expr_type(var))) {
4743+
if (ASR::is_a<ASR::Var_t>(*var)) {
4744+
ASR::symbol_t *var_sym = ASR::down_cast<ASR::Var_t>(var)->m_v;
4745+
if (ASR::is_a<ASR::Variable_t>(*var_sym)) {
4746+
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(var_sym);
4747+
if (v->m_intent == intent_local ||
4748+
v->m_intent == intent_return_var ||
4749+
!v->m_intent) {
4750+
is_argument = false;
4751+
} else {
4752+
is_argument = true;
4753+
}
4754+
}
4755+
}
4756+
}
4757+
return is_argument;
4758+
}
4759+
47404760
} // namespace ASRUtils
47414761

47424762
} // namespace LCompilers

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ using ASRUtils::intent_local;
6363
using ASRUtils::intent_return_var;
6464
using ASRUtils::determine_module_dependencies;
6565
using ASRUtils::is_arg_dummy;
66+
using ASRUtils::is_argument_of_type_CPtr;
6667

6768
void string_init(llvm::LLVMContext &context, llvm::Module &module,
6869
llvm::IRBuilder<> &builder, llvm::Value* arg_size, llvm::Value* arg_string) {
@@ -1245,8 +1246,12 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
12451246
llvm::Value* const_list = builder->CreateAlloca(const_list_type, nullptr, "const_list");
12461247
list_api->list_init(type_code, const_list, *module, x.n_args, x.n_args);
12471248
int64_t ptr_loads_copy = ptr_loads;
1248-
ptr_loads = 1;
12491249
for( size_t i = 0; i < x.n_args; i++ ) {
1250+
if (is_argument_of_type_CPtr(x.m_args[i])) {
1251+
ptr_loads = 0;
1252+
} else {
1253+
ptr_loads = 1;
1254+
}
12501255
this->visit_expr(*x.m_args[i]);
12511256
llvm::Value* item = tmp;
12521257
llvm::Value* pos = llvm::ConstantInt::get(context, llvm::APInt(32, i));

0 commit comments

Comments
 (0)