[llvm-dev] C returning struct by value

James Y Knight via llvm-dev llvm-dev at lists.llvm.org
Mon Mar 28 09:14:23 PDT 2016


Unfortunately, the understanding of the C calling convention is split
between LLVM and Clang in a rather difficult to understand (and poorly
documented) manner. Needing to put the struct return value in an 'sret'
argument is but one of the many ways in which you need to adjust your LLVM
function definition from the "obvious" lowering of a C function.

I don't think anyone feels it ideal, but it's what we have. There are a
number of reasons why this is not completely trivial to fix.

On Mon, Mar 28, 2016 at 12:48 AM, Michael Nicolella via llvm-dev <
llvm-dev at lists.llvm.org> wrote:

> I'm new to using LLVM and I've started work on a compiler for a language
> that can interface with C. One thing that caught me off guard was returning
> a struct from a function by value. It seems that when calling a C function
> I need to emit llvm ir that, in the caller, emits an alloca for the
> returned structure, and the C function signature should return void and
> take a first argument with the pointer annotated with the "sret" attribute.
>
> Can someone help me understand why this detail needs to be understood by
> the frontend, and isn't handled by maybe annotating the C function with
> some attribute that says it should conform to a certain calling convention
> - then just having the signature be to naturally return the struct by
> value, but the backend knows how to transform it to what it should be?
>
> Cheers
> -Mike
> _______________________________________________
> 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/20160328/0f1282aa/attachment.html>


More information about the llvm-dev mailing list