Hi, Recently I came across some IR produced by a frontend that had unnamed function arguments. For example something like this. ``` define i32 @foo(i32, i32, i32) #0 { %x = add i32 %1, %2 ret i32 %x } ``` I had never seen this before, so I took a look at the LLVM language reference manual and the section on functions [1] doesn't say anything about what "argument list" can be (other than the possibility of it being empty). The above LLVM IR was confusing to me because I usually see that unnamed registers start counting from 1 (i.e. %1 = add ...). However it seems (after calling the dump() method on the arguments of the function) that actually unnamed parameters count from 0 so the three arguments are in registers %0 %1 %2 and not in registers %1 %2 %3 (which is what I was expecting) I'm slightly surprised that unnamed function arguments are allowed at all. Maybe there is a use case but I can't think of a good one off the top of my head. I think it should be documented in the LLVM reference manual what the register names are for unnamed arguments. This would also need to consider the case where some arguments are named and some are not. E.g. ``` ; Function Attrs: nounwind uwtable define i32 @foo(i32 %y, i32, i32) #0 { %x = add i32 %y, %1 ret i32 %x } ``` AFAICT this is equivalent to the IR shown earlier but it might not be immediately obvious that it is. [1] http://llvm.org/docs/LangRef.html#functions Thanks, Dan.
Tim Northover
2014-Aug-13 14:48 UTC
[LLVMdev] Functions with unnamed parameters in LLVM IR
> The above LLVM IR was confusing to me because I usually see that > unnamed registers start counting from 1 (i.e. %1 = add ...).There's a (usually hidden) %0 representing the entry basic block there. The general rule is "start from 0 and keep counting; skip named values".> I'm slightly surprised that unnamed function arguments are allowed at > all. Maybe there is a use case but I can't think of a good one off the > top of my head.They're useful for ensuring ABI conformance: padding out (hardware) registers that you don't want to use for a particular call. You might map void foo(int32_t a, int64_t b) to "declare void @foo(i32 %a, i32, i64 %b)" for example, if your 64-bit value had to start at an even (32-bit) register number, as is the case on ARM. (Actually, it's not necessary there, but is in more complicated cases).> I think it should be documented in the LLVM reference manual what the > register names are for unnamed arguments.It does sound reasonable to add a note that function arguments get included too, since they're not obviously value computations. Cheers. Tim.
On 13 August 2014 15:48, Tim Northover <t.p.northover at gmail.com> wrote:>> The above LLVM IR was confusing to me because I usually see that >> unnamed registers start counting from 1 (i.e. %1 = add ...). > > There's a (usually hidden) %0 representing the entry basic block > there. The general rule is "start from 0 and keep counting; skip named > values".Okay that's good to know. So unnamed arguments get assigned temporary registers before the entry block? If so the entry block isn't always %0.>> I'm slightly surprised that unnamed function arguments are allowed at >> all. Maybe there is a use case but I can't think of a good one off the >> top of my head. > > They're useful for ensuring ABI conformance: padding out (hardware) > registers that you don't want to use for a particular call. > > You might map void foo(int32_t a, int64_t b) to "declare void @foo(i32 > %a, i32, i64 %b)" for example, if your 64-bit value had to start at an > even (32-bit) register number, as is the case on ARM. (Actually, it's > not necessary there, but is in more complicated cases).Thanks for the concrete use case.>> I think it should be documented in the LLVM reference manual what the >> register names are for unnamed arguments. > > It does sound reasonable to add a note that function arguments get > included too, since they're not obviously value computations.Included in what? Anyway I've attached a patch that tries to clear this up. Is this good enough to commit? -------------- next part -------------- A non-text attachment was scrubbed... Name: 0001-Add-note-to-LangRef-about-how-function-arguments-can.patch Type: text/x-patch Size: 1348 bytes Desc: not available URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140814/4535a568/attachment.bin>
Reasonably Related Threads
- [LLVMdev] Functions with unnamed parameters in LLVM IR
- [LLVMdev] Functions with unnamed parameters in LLVM IR
- [LLVMdev] Functions with unnamed parameters in LLVM IR
- Potential ambiguity in the grammar of LLVM IR assembly
- [LLVMdev] Question About the LLVM IR unnamed values!