[llvm-dev] Correct way to pass int128 from LLVM to C++ function (MSVC)
Reid Kleckner via llvm-dev
llvm-dev at lists.llvm.org
Wed Dec 21 09:12:20 PST 2016
The Windows x64 ABI rules say that anything larger than 8 bytes is passed
by reference.[1] Because MSVC doesn't support the __int128 type on x64,
nobody has made sure that the LLVM i128 type is passed in a way that
follows the local ABI rules. I think LLVM should probably pass i128 the
same way it passes <2 x i64> on Win64, which is indirectly in memory.
Until LLVM is fixed, you can work around the problem by passing i128 by
pointer, which you're probably already doing. Given that we will ultimately
pass it by pointer in order to obey the ABI, it's a pretty reasonable
workaround.
[1] https://msdn.microsoft.com/en-us/library/ms235286.aspx
On Wed, Dec 21, 2016 at 4:35 AM, Stefan de Bruijn via llvm-dev <
llvm-dev at lists.llvm.org> wrote:
> Hi,
>
>
>
> I’ve been attempting to call a C++ function from LLVM code. The LLVM code
> is JIT’ted and executed from C++. Basically my idea was as follows:
>
>
>
> -- LLVM IR code –
>
>
>
> ; ModuleID = 'native'
>
>
>
> define i32 @main(i32, i8**) {
>
> code:
>
> call void @"Helper::Test"(i128 128932)
>
> ret i32 0
>
>
>
> -- C++ code –
>
>
>
> struct int128
>
> {
>
> uint64_t v1;
>
> uint64_t v2;
>
> // … code omitted… *1
>
> };
>
>
>
> struct Helper
>
> {
>
> static void Test(int128 value)
>
> {
>
> }
>
> };
>
>
>
> If I try to execute the code, I notice that MSVC++ seems to expect a
> pointer. Most notably, if you implement the copy constructor: int128(const
> int128& o) { … } , the address of o will be 128932 . As for assembler
> output, I noticed that %ecx is set to the value, %edx is zero’d and then
> the method is called.
>
>
>
> As evidenced by the resulting crash, this is apparently not the right way
> to do something like this… Obviously, I can also try to pass the value by
> pointer - but will then get issues with return values. Alternatively, I can
> introduce a 2x64-bit structure, fill that with the int128 and pass that.
>
>
>
> My question is: what’s the correct way to do something like this?
>
>
>
> Kind regards,
>
> Stefan.
>
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20161221/d99008bb/attachment.html>
More information about the llvm-dev
mailing list