Skip to content

Commit d12ce4e

Browse files
authored
Implement CPtr deepcopy and handle CPtr type passing into functions (#1310)
1 parent 5f2ccbe commit d12ce4e

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

integration_tests/bindc_03.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,27 @@ def f(q_void: CPtr) -> None:
3030
print(el)
3131
assert el == i * i + i%2
3232

33+
def h(q_void: CPtr) -> None:
34+
i: i32
35+
el: i32
36+
q: Pointer[i32[:]]
37+
c_p_pointer(q_void, q)
38+
for i in range(10):
39+
# TODO: Use q[i] directly in the assert.
40+
el = q[i]
41+
print(el)
42+
assert el == i * i + i%2
43+
3344
def run():
3445
a: CPtr
3546
array_wrapped: ArrayWrapped = ArrayWrapped(a)
47+
array_wrapped1: ArrayWrapped
3648
size: i32
3749
size = 10
3850
a = get_array(size)
3951
array_wrapped.array = a
4052
f(array_wrapped.array)
53+
array_wrapped1 = array_wrapped
54+
h(array_wrapped1.array)
4155

4256
run()

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5979,22 +5979,26 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
59795979
// at the beginning of the function to avoid
59805980
// using alloca inside a loop, which would
59815981
// run out of stack
5982-
llvm::BasicBlock &entry_block = builder->GetInsertBlock()->getParent()->getEntryBlock();
5983-
llvm::IRBuilder<> builder0(context);
5984-
builder0.SetInsertPoint(&entry_block, entry_block.getFirstInsertionPt());
5985-
llvm::AllocaInst *target = builder0.CreateAlloca(
5986-
target_type, nullptr, "call_arg_value");
59875982
if( ASR::is_a<ASR::ArrayItem_t>(*x.m_args[i].m_value) ||
59885983
ASR::is_a<ASR::StructInstanceMember_t>(*x.m_args[i].m_value) ) {
59895984
value = CreateLoad(value);
59905985
}
5991-
if( ASR::is_a<ASR::Tuple_t>(*arg_type) ||
5992-
ASR::is_a<ASR::List_t>(*arg_type) ) {
5993-
llvm_utils->deepcopy(value, target, arg_type, module.get(), name2memidx);
5986+
if( !ASR::is_a<ASR::CPtr_t>(*arg_type) ) {
5987+
llvm::BasicBlock &entry_block = builder->GetInsertBlock()->getParent()->getEntryBlock();
5988+
llvm::IRBuilder<> builder0(context);
5989+
builder0.SetInsertPoint(&entry_block, entry_block.getFirstInsertionPt());
5990+
llvm::AllocaInst *target = builder0.CreateAlloca(
5991+
target_type, nullptr, "call_arg_value");
5992+
if( ASR::is_a<ASR::Tuple_t>(*arg_type) ||
5993+
ASR::is_a<ASR::List_t>(*arg_type) ) {
5994+
llvm_utils->deepcopy(value, target, arg_type, module.get(), name2memidx);
5995+
} else {
5996+
builder->CreateStore(value, target);
5997+
}
5998+
tmp = target;
59945999
} else {
5995-
builder->CreateStore(value, target);
6000+
tmp = value;
59966001
}
5997-
tmp = target;
59986002
}
59996003
}
60006004
}

src/libasr/codegen/llvm_utils.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ namespace LFortran {
313313
}
314314
break ;
315315
};
316-
case ASR::ttypeType::Character: {
316+
case ASR::ttypeType::Character:
317+
case ASR::ttypeType::CPtr: {
317318
LLVM::CreateStore(*builder, src, dest);
318319
break ;
319320
}

0 commit comments

Comments
 (0)