[IR] value name & AsmPrinter

Hi,

I use Value::getName and Value::print to obtain the string representation of IR variables such as %x.
I have two questions:

  • It seems that a value sometimes does not have a name. Could anyone explain when the name is available?
  • When the name is NULL, I use print to obtain the string representation of an instruction and manipulate it on my own. But it seems print is extremely slow for large IR. Does anyone know some idea for optimizing this process?

I found a similar question.
I have a similar task. I wonder if there is any improvement introduced after then.

It’s available when someone sets a name to it, simple as that. Perhaps getNameOrAsOperand would work for you, however I don’t know how it compares performance-wise.

Value name are here for debug, and there is a setting in the LLVMContext to disable them I believe.
Clang by default will disable support for Value name in release builds for example.

(the -f(no-)discard-value-names option)

Thank you all.
I already use -fno-discard-value-names, so I can obtain some variable names such as %x.
But still cannot get temp variable names such as %1 using getName.

A variable named like “%1” literally has no name–getName() returns the empty string in that case. Instead, the numbers that these use are dynamically assigned during dumping to IR (you’ll notice that if you insert or remove an unnamed instruction in the same function before the one you’re printing out, the number will change). Using the printAsOperand method instead will use the same underlying machinery to get its correct string printed out.

1 Like

Hi.

I have a further question.
I wonder why Module::print is much faster than iterating Value::print for all instructions in a module. I found that the latter is extremely slower than the first.

I suspect that every call to print needs to construct the unique name for a SSA value which require traversing a function to number them.

Thanks a lot. You are right. I found a workaround.