Arguments name IR LLVM

Hi everybody,

I want to read the name of the arguments of fucntion IR LLVM, I have the following function define:

define void @vecadd(i32, float* nocapture readonly, float* nocapture readonly, float* nocapture) local_unnamed_addr #0 {

As we see, the argument has no name, if we look at call function in main function we see the arguments name (a, b and c):

call void @vecadd(i32 10, float* getelementptr inbounds ([10 x float], [10 x float]* @a, i64 0, i64 0), float* getelementptr inbounds ([10 x float], [10 x float]* @b, i64 0, i64 0), float* nonnull %3)

The problem is, how can I get arguments name from the instruction inside define function because I just have the instructions in define function.

Any idea please?

Kinds regards

Mohamed Messelka

Hi Mohamed,

Hi Mohamed,

As we see, the argument has no name, if we look at call function in main function we see the arguments name (a, b and c):

call void @vecadd(i32 10, float* getelementptr inbounds ([10 x float], [10 x float]* @a, i64 0, i64 0), float* getelementptr inbounds ([10 x float], [10 x float]* @b, i64 0, i64 0), float* nonnull %3)

Some kind of name, but not a good one for a couple of reasons: it'll
vary between callsites, and may not even be referencable in @vecadd
itself if it's a local name to the caller.

The problem is, how can I get arguments name from the instruction inside define function because I just have the instructions in define function.

When names are omitted in LLVM, it starts assigning them automatically
by counting up %0, %1, .... A function's (unnamed) arguments will
always start at %0 so in that case it's pretty easy to calculate the
proper value. In more complicated cases you'd use the class
"ModuleSlotTracker" to compute it for you, but that's really not very
efficient.

But the chances are pretty good that you don't need the name anyway.
It's only really useful for debug output, and not generally worth the
hassle of computing manually even there. What are you really trying to
do?

Cheers.

Tim.

(Adding llvm-dev back to reply list).

I want to generate DFG with names because later I need them, so how to get the names from call function :

Normally any graph structure you build up would be constructed
directly from the "Value *" pointers rather than by trying to
cross-reference names of Values. You say you need them later, but what
for?

call void @vecadd(i32 10, float* getelementptr inbounds ([10 x float], [10 x float]* @a, i64 0, i64 0), float* getelementptr inbounds ([10 x float], [10 x float]* @b, i64 0, i64 0), float* nonnull %3)

any idea?

Something like this would work:

    std::string CurName;
    unsigned ValueNo = 0;
    for (auto &Arg : F.args()) {
      CurName = Arg.getName();
      if (CurName.empty())
        CurName = std::to_string(ValueNo++);
      [... Use CurName ...]
    }

But please think really hard about why you're trying to do this.
Wanting to use names of LLVM instructions is a large red flag that
there's something wrong.

Cheers.

Tim.

(Adding llvm-dev back again. Please use reply-all).

1- I need them to read the correct values in fake memory to test some acceleration.

That doesn't sound like a plausible use-case for the name of a Value.

2- you code is good but the problem is: in define function has no names for arguments

The arguments have no explicit names, but they're still implicitly
called %0, %1, %2, ... Within the function that's what they'll be
referred to as. For example:

    define i32 @foo(i32) {
      %val = add i32 %0, 1
      ret i32 %val
    }

will add 1 to its first argument and return it.

but for call fucntion has name for arguments as you see @a and @b:

call void @vecadd(i32 10, float* getelementptr inbounds ([10 x float], [10 x float]* @a, i64 0, i64 0), float* getelementptr inbounds ([10 x float], [10 x float]* @b, i64 0, i64 0), float* nonnull %3)

At that particular callsite, yes. But that won't be true in general,
and (for example) %3 in that call is completely meaningless inside
@vecadd.

Cheers.

Tim.

OP’s use case for the names aside, I think we should consider changing LLVM’s IR printer to print unnamed arguments in function definitions as %0, %1, etc, like we do for instructions. We can skip the names for Function declarations since nothing can refer to them, but it’s confusing to see %0 references with no definition. Unnamed BBs are another common source of confusion, but I’d leave that alone for now.

Reid

Bitcode parser doesn’t like “numerical” names for parameters:

opt: <stdin>:1:22: error: expected ')' at end of argument list
define void @foo(i32 %0) {
                     ^

I don’t think it makes the grammar ambiguous, so it seems like it could be fixed.

OP's use case for the names aside, I think we should consider changing LLVM's IR printer to print unnamed arguments in function definitions as %0, %1, etc, like we do for instructions.

I agree. I was thinking the same during the discussion. So:
https://reviews.llvm.org/D65582.

Tim.