[LLVMdev] Functions with unnamed parameters in LLVM IR

Dan Liew dan at su-root.co.uk
Wed Aug 13 07:06:26 PDT 2014


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.



More information about the llvm-dev mailing list