exaloop / exaloop/codon

Incompatible function calling convention impeeds interoperability with C++ in JIT mode

Open
#757 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
16.8k
Forks
603
Avg merge
4d 23h
Merged PRs (30d)
6

Description

If a Codon- side function returns a value large than 16 byte (such as often the case with tuples) C++ code (at least clang that I'm using) cannot receive response from such function correctly. I did some research on this and found that clang and most others use platform ABI-based conventions for optimizing transfer of a large value to the caller. But the Codon's JIT generated code does not adhere to this conversion for some reason.

Here is a simple example:

```
def run() -> None:

dict = Dict[str,Tuple[str, str]]()
dict["abc"] = ("bcd", "def")
dict["cde"] = ("efg", "fgh")

generator = dict.items()

while generator.done() is False:
pair = generator.next()

print (pair[0], pair[1])

if __name__ == "__main__":
run()
```

Codon generates the following code as shown by compiler explorer :

```
mov rsi,QWORD PTR [rsp+0x8]
mov rdi,rbx
call 402f00
mov r9,QWORD PTR [rsp+0x48]
mov r8,QWORD PTR [rsp+0x40]
mov rcx,QWORD PTR [rsp+0x38]
mov rdx,QWORD PTR [rsp+0x30]
mov rdi,QWORD PTR [rsp+0x20]
mov rsi,QWORD PTR [rsp+0x28]
mov QWORD PTR [rsp+0x50],rdi
mov QWORD PTR [rsp+0x58],rsi
mov QWORD PTR [rsp+0x60],rdx
mov QWORD PTR [rsp+0x68],rcx
mov QWORD PTR [rsp+0x70],r8
mov QWORD PTR [rsp+0x78],r9
```

if I pass an address of `generator.next()` to C++ and invoke it (passing generator instance as the first argument) my code crashes.
This is because Clang does not expect the above convention based on passing data via copy on stack (which is inefficient btw)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.