Passing Information from Caller to Callee at SelectionDAG node

Hi, I’m building a custom backend and in order to implement variadic arguments correctly, I need to pass information regarding the number of arguments that are passed via the stack from the caller to the callee.
I need this communication as I need to know the size of all arguments passed via the stack from the caller to the callee in order to initialise va_start correctly. I was wondering what the best way is to pass information from the LowerCall function (caller) in TargetISelLowering.cpp to the LowerFormalArguments function (for the callee)?

The common approach is to record the address (and possibly the size) of the last non-variadic argument in YourTargetMachineFunctionInfo in LowerFormalArguments and access that information from the function which lowers ISD::VASTART. LowerCall has nothing to do with it.
You can find examples in almost any other target, search for setVarArgsFrameIndex.

Thanks for your prompt response. I have looked at others and have a system that uses the approach you suggested, but I’m having a particular problem that I think is particular to the backend I’m building. The key difference is that the other approaches allow accessing stack arguments that are stored above the top of the stack, which I cannot do.

The stack I am using grows downwards towards more negative addresses and all my stack passed arguments need to be below the top of the stack. This means that either the caller needs to know that it is calling a variadic function and place variadic arguments on stack below the space allocated for variadic arguments that are passed on registers and later stored on stack by the callee. Alternatively, if the callee knows the number of variadic arguments that are allocated on stack in advance then it can allocate the variadic arguments passed by registers after those allocated on stack.

This is why I was hoping to be able to communicate this information between the two. I noticed the caller has access to the Callee SDNode which is a TargetGlobalAddress and I was wondering if it was possible to use that to access the callee machine function? Open to other suggestions, or ideas on how to implement this incase I’m missing something.

Other targets might be allowed to access the stack above the top, but they don’t (usually) do that. When they pass arguments, they first allocate space for them on the stack, and only then they store the arguments.

The caller, of course, knows what it is calling and what arguments it wants to pass. So you can just compute the required size for variadic arguments, allocate the stack space for them, and then store them one by one below the new stack top.

The callee can’t know this at compile time. There may be many callers, each passing different number of arguments. The caller and the callee may be in different translation units. The caller may call the callee indirectly. So you can’t pass such information between LowerCall and LowerFormalArguments, because you don’t generally have it.
From the callee’s point of view, you don’t need to know how much space the caller has allocated. You just read variadic arguments one by one as written in the source program. If you ever cross the stack boundary, that would mean that the program is buggy. You only need to know where the variadic area starts. For that you can use the address of the last fixed argument, for example.

I was thinking of the RISCV backend, which I think uses positive offsets from the top of the frame just for arguments passed via the stack. But this being said, I will explore other backends to see how they do this. But this makes sense that we cannot pass the information from the caller to the callee especially if they are in different translational units. Thanks.

Just in case.

Positive offsets from the stack top are below the stack top if the stack grows towards decreasing addresses.

Yep, that makes sense. I was just wondering, how do I test from the caller if the callee is a variadic function signature or not? The caller receives a global address SDNode of the callee, and I was wondering if there was a way to use this node to test if the callee has variadic arguments or not?

Apologies, please ignore this. I just noticed that the caller function comes with an annotation of IsVarArg = True.