[LLVMdev] PSA: Perfectly forwarding thunks can now be expressed in LLVM IR with musttail and varargs

Reid Kleckner rnk at google.com
Tue Sep 2 15:54:22 PDT 2014


I needed this functionality to solve http://llvm.org/PR20653, but it
obviously has far more general applications.

You can do it like this:

define i32 @my_forwarding_thunk(i32 %arg1, i8* %arg2, ...) {
  ... ; define new_arg1 and new_arg2
  %r = musttail call i32 (i32, i8*, ...)* @the_true_target(i32 %new_arg1,
i8* %new_arg2, ...)
  ret i32 %r
}
declare i32 @the_true_target(i32, i8*, ...)

The varargs convention (usually) matches the standard function call
convention, and everything will line up if you do an indirect call like
this:

declare i32 @the_true_target(%struct.Foo* %this, i64, i8)
define i32 @my_forwarding_thunk(%struct.Foo* %this, ...) {
  %fptr = ... ; Compute fptr by bitcasting @the_true_target to the varargs
type
  %r = musttail call i32 (%struct.Foo*, ...)* %fptr(%struct.Foo* %this, ...)
  ret i32 %r
}

Currently this functionality is only implemented for x86 in the absence of
inreg and for x86_64 in the general case, but I'd like to see it
implemented for the CPU backends. I'm happy to do some of them, but I don't
have the time to do all of them.

Alternatively, it would be great if we could handle forwarding of unused
register parameters in variadic functions in a general way. Perhaps CCState
should surface this information.

Thoughts? This seemed like a reasonable way to represent such thunks, but
I'd like to know if there are objections.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140902/ce169733/attachment.html>


More information about the llvm-dev mailing list