[LLVMdev] LLVM IR Code Annotation

Devang Patel dpatel at apple.com
Tue Oct 4 14:59:50 PDT 2011

Hi Griffin,

On Oct 4, 2011, at 8:01 AM, Griffin Wright wrote:

> Hello,
> I am working on an LLVM pass which, among other things, selectively duplicates instructions in a program.  
> This results in a few new instructions added to the final IR to facilitate checking, and I'd like to annotate the generated code file with a comment or something like it that would indicate that a given specific instruction has some certain property.  Essentially, just printing out flag values in a comment form in the final bitcode file that I could see when looking at the .s file generated with llc.
> How could I go about doing this?

The way I see it, you need a way to attach and preserve your annotation with an LLVM instruction, carry the annotation to corresponding MachineInstr and finally print the annotation in output .s file as a comment.

One way to do this would be:
1) Create a MDString for your comment and attach it to an LLVM instruction using setMetadata()/getMetadata() interface.
2) While converting LLVM instruction, use MDString (you will have to add new data member) in MachineInstr to hold your incoming annotation.
3) While emitting .s file, update AsmPrinter to check for your annotation with each MachineInstr and emit comment appropriately.


More information about the llvm-dev mailing list