3

Is it possible to use the comments in IR in my pass? Basically I want to use the IR annotated with basic block frequency, which is written in comments, as shown below, and I need the frequency value in my pass. I know this is naive method, but it will suffice.

define internal void @MDFilter() #0 {  
entry:  
    ;;; Basic block executed 2 times.  <-- I NEED THIS COMMENT AS A STRING IN MY PASS
  %mdContext = alloca %struct.MD5_CTX, align 8  
  %bytes = alloca i32, align 4  
  %data = alloca [16 x i8], align 16  
  call void @MD5Init(%struct.MD5_CTX* %mdContext)  
  br label %while.cond  
    ;;; Out-edge counts: [2.000000e+00 -> while.cond]  

Any other method to obtain this info is also welcome.

shrm
  • 1,112
  • 2
  • 8
  • 20

1 Answers1

6

No, there is no way to use the comments' contents this way, not without significantly changing the IR parser. However, there's no need to re-invent the wheel; there's a mechanism in LLVM which is intended precisely for these sorts of things - transferring information from the front-end into an LLVM pass - and that is metadata.

So whatever or whoever is adding this information to the IR should add it with metadata instead - see these sources for more information on how to do that:

If you have no control over the generation of data, then you should add some pre-processing step in which you convert the comments to metadata.

In the end the IR should look something like:

define internal void @MDFilter() #0 {  
entry:  
  %mdContext = alloca %struct.MD5_CTX, align 8, !freq !1
  %bytes = alloca i32, align 4  
  %data = alloca [16 x i8], align 16  
  call void @MD5Init(%struct.MD5_CTX* %mdContext)  
  br label %while.cond, !outedge !2

...

!1 = metadata !{i32 2}
!2 = metadata !{float 2.0}

And your pass needs to look for these !freq and !outedge nodes.

Community
  • 1
  • 1
Oak
  • 26,231
  • 8
  • 93
  • 152
  • True, metadata is the way to go. Can I add metadata to basic block objects though ? Also the IR is generated by `llvm-prof`. – shrm Jun 21 '13 at 15:14
  • @mishr nope. Since every basic block contains at least one instruction, though, I'd say a good way is to add the metadata to the first instruction in it. Alternatively, you can just add metadata without associating it with anything, and in the metadata content itself list the function and basic block (by name) that it's referring to. – Oak Jun 21 '13 at 15:32
  • @mishr and re: `llvm-prof`, I think it actually predates the metadata mechanism. Anyway, looking at [its source code](https://llvm.org/svn/llvm-project/llvm/tags/RELEASE_28/tools/llvm-prof/llvm-prof.cpp) it seems relatively easy to change it to emit metadata, though. – Oak Jun 21 '13 at 20:55