[LLVMdev] Exact meaning of byval

Duncan Sands baldrick at free.fr
Thu Jul 10 02:21:13 PDT 2008


Hi, byval means that the parameter setup code for performing a call
copies the struct onto the stack, in an appropriate position relative
to the other call parameters.

> I'm particularly confused by the "between the caller and the callee" part. The
> way I see this, the responsibility for the copying should be with either the
> caller or the callee,

It is with the caller.  It would be nice to do an explicit memcpy in the caller,
but the problem is that the value has to end up in a very precise place on the
stack and there is no way of representing this in the LLVM IR.

> not somewhere in between. In particular, I think byval
> could either mean:
> 	a) The callee is not allowed to modify the argument. If the original code
> 	modifies the argument, the callee should make a copy and modify that
> 	instead.

It is the same as passing anything else (for example an integer) by value:
the callee can modify the value if it likes, but this doesn't cause any
changes to the value the callee has.  That's because, just like for an
integer, a copy is passed to the callee (in a register or on the stack;
for byval arguments it's always on the stack).

> 	b) The caller will always pass in a copy of the object, which the callee is
> 	free to modify and will be thrown away afterwards.

This is how it is done.

> In both cases, it seems that byval argument must always be a valid pointer.

Correct.  I thought this was stated in the LangRef?

> From the code, I suspect that option b is the case. I would think that option
> a is the better option, since it can prevent copies when the callee doesn't
> even modify the value (but this stems from C ABI or something?)

If the callee doesn't modify the argument, then it might be possible to drop
the byval attribute (you also have to worry about aliasing etc, but some IPO
pass could do it).

Ciao,

Duncan.



More information about the llvm-dev mailing list