[llvm-dev] Adding comments in Assembly

Mattias Eriksson V via llvm-dev llvm-dev at lists.llvm.org
Wed Dec 16 06:32:23 PST 2015

On 12/16/2015 01:05 PM, Jonas Paulsson wrote:
> Hi Kumail,
> It's the AsmPrinter that by itself checks if the block is in a loop. See
> lib/CodeGen/AsmPrinter/AsmPrinter.cpp - emitBasicBlockLoopComments().
> I think comments are useful, and find this to be a missing feature. I
> have experience
> with adding asm comment strings based on a map from MachineInstr* to
> std::string. This
> worked quite well, but was not ideal. Mattias - do you have any comment?

We have the setup which Jonas describes in the target that I am working 
on. It works most of the time but has some problems.

If we add a comment on an MI and then a later optimization/expansion 
replaces this instruction, the comment will be lost.

Also, such "lost" comments can show up on other instructions which just 
happen to get the same value of the pointer (MachineInstr *).

> An alternative might be to have a global asm-comment string map and make
> each MI that
> carries a comment have an index for the comment. This would be better
> than using the
> address while cloning / expanding instructions, I think.
> I am not sure about metadata, as it is not guaranteed to be preserved.

I think metadata is probably be a good idea. It feels like a good fit, 
but I do not have any experience with using it for this purpose.


> Feel free to give it a try if you want to implement this Kumail!
> Otherwise, unfortunately
> the answer is today that it can't be done, unless you are happy with
> hacking into
> the AsmPrinter on your end.
> /Jonas
>> Hi,
>> Which file should I edit, if i want to add comments to an assembly
>> instruction.
>> For example,
>>   mov r1, 0    #r1 is variable a
>> Should I add it as a metadata? Secondly, how are comments inserted at
>> the beginning of loops?
>> Best Regards,
>> Kumail

More information about the llvm-dev mailing list