[LLVMdev] Returning a structure
Duncan Sands
baldrick at free.fr
Wed Jan 27 01:12:42 PST 2010
Hi Rob,
> I’m using the C API (with a few additions in a single cpp file) to write a compiler. In one part of my code, I want to build a function that returns a structure. The LLVM Assembly Language Reference Manual gives this as a possible return value:
>
>> ret { i32, i8 } { i32 4, i8 2 }
>
> Meanwhile, compiling this C function:
>
>> Range make_range(Index location, Index length) {
>> return (Range){ location, length };
>> }
>
> reveals clang’s IR output to be quite a bit more complex:
>
>> %struct._Range = type <{ i64, i64 }>
>>
>> define void @make_range(%struct._Range* noalias sret %agg.result, i64 %location, i64 %length) nounwind {
that's because clang is doing something more complicated than you are: it is
producing a function that conforms to the platform ABI for passing parameters.
The platform ABI specifies in this case that the valued should be returned via
a special in-memory mechanism (thus the "sret" extra function parameter) rather
than in a bunch of registers (which is what you are doing). The whole "why does
the front-end have to worry about the ABI" discussion has occurred several times
on the mailing list (eg: yesterday), please search the archives.
> Returning a structure packed into a single i64 value or by reference in an argument would seem to be details of the platform’s ABI. Does my compiler really need to be aware of these details, or can I just return a structure as per the documentation quoted above?
Yes, it needs to be aware of the details. There has been some discussion of
providing a helper library for doing this, see
http://llvm.org/bugs/show_bug.cgi?id=4246
> If the latter case, how do I build that instruction with the builder? Attempts to use LLVMConstStruct are crashing, I assume because the arguments to it in my use are not actually constants (: A quick search through IRBuilder didn’t give me any clues either, so here I am (:
What instruction, the funky return statement? You create a return statement for
which the argument is a Value of the appropriate struct type.
Ciao,
Duncan.
More information about the llvm-dev
mailing list