[LLVMdev] First-class aggregate semantics

Kenneth Uildriks kennethuil at gmail.com
Thu Jan 7 13:57:44 PST 2010


On Thu, Jan 7, 2010 at 3:38 PM, David Greene <dag at cray.com> wrote:
> On Thursday 07 January 2010 15:28, Dustin Laurence wrote:
>> I think I'm missing something basic about the semantics of returning an
>> aggregate type (in my case, a structure) from a function.  Returning a
>> structure containing only compile-time constants is simple enough.  But
>> I don't quite get how this works with a struct composed at run-time.  If
>> I constructed it on the stack with alloca, would I be letting a stack
>> variable escape to to a context where it doesn't exist if I return it?
>> Or does the return semantics guarantee it will be copied (or space
>> allocated in the caller) appropriately?  Otherwise I should abandon the
>> idea of returning such a struct and simply pass in a pointer to space
>> allocated in the caller.
>>
>> I think my confusion stems from thinking in terms of high-level
>> languages and not having done nearly enough assembly work to know what
>> LLVM really wants to do, and I'd be grateful for a clue about the
>> idiomatic way to do this.
>
> The way this works on many targets is that the caller allocates stack
> space in its frame for the returned struct and passes a pointer to it
> as a first "hidden" argument to the callee.  The callee then copies
> that data into the space pointed to by the address.
>
> This is all specified by the ABI so it varies by processor and OS.
> The target-dependent lowering pieces of LLVM should take care of it.
>
> Long-term, first-class status means that returns of structs should
> "just work" and you don't need to worry about getting a pointer to
> invalid memory.  I believe right now, however, only structs up to a
> certain size are supported, perhaps because under some ABIs, small
> structs can be returned in registers and one doesn't need to worry
> about generating the hidden argument.
>
> Someone working directly on this can answer with more authority.

On x86, the hidden argument is generated automatically at codegen time
if it's needed.  As far as I know, other platforms don't yet have that
support.




More information about the llvm-dev mailing list