6

I am working with LLVM 3.4 and want to obtain the line number information of source file from IR. The IR is generated from simple c code with Clang. I want to obtain the line number in source c file from the line in IR body.

I tried this -

  1. For Instruction BI, unsigned Line = Line = BI->getDebugLoc().getLine();
  2. For Loop L, std::cout << L->getStartLoc().getLine();

But, the result stored/printed is always 0. I don't know how to obtain line number in the source from LLVM IR.

My Source C file is -

#include <stdio.h>

int main()
{

 int i;

 int inbuf[100];
 int outbuf[100];

 for(i = 0; i < 100; ++i)        
        inbuf[i] ^= outbuf[i];

 inbuf[1] += 402;
 inbuf[6] += 107;
 inbuf[97] += 231;

 for(i = 0; i < 100; ++i)       
 {
         inbuf[i] += outbuf[i];
 }

 inbuf[47] += 312;  

    //print-statements 
 for (i=0;i<100;i++) {
        printf("inbuf[%d] = %d\n",i,inbuf[i]);              
}

return 0;

Command Used- ~/llvm/build/Release+Asserts/bin/clang -O3 -fno-unroll-loops -fno-vectorize -fno-slp-vectorize -S -emit-llvm sample.c -o sample.ll

Thanks!

Shail Dave
  • 63
  • 1
  • 5
  • You are optimizing so there is no reason to expect the output to represent the input sources in source line number order nor to expect there to be chunks of code per line of source. If llvm has a library that fills those and that is what you are asking maybe there is no line for the thing you are looking at. maybe try unoptimized and see if that changes things. – old_timer Jul 12 '16 at 03:01
  • Thanks! I tried with -O0 but It still shows 0. command: ~/llvm/build/Release+Asserts/bin/clang -O0 -S -emit-llvm sample.c -o sample.ll I have working pass which can detect and analyze the loops and instructions. I am using it inside function - runOnLoop(Loop *L, LPPassManager &LPM) – Shail Dave Jul 12 '16 at 03:07
  • You don't appear to be asking for debug info in your compile command line. – Mat Jul 12 '16 at 03:11
  • Thanks Mat! Should I use Debug version for that? Also I tried to include -fstandalone-debug option. But, it is still printing the 0. – Shail Dave Jul 12 '16 at 04:36

1 Answers1

4

To get line number information into .ll file your must specify both -O0 and -g flags for clang.

http://llvm.org/docs/SourceLevelDebugging.html#debugging-optimized-code

Line numbers are stored in specialized metadata nodes.

http://llvm.org/docs/LangRef.html#specialized-metadata-nodes

So the full command line must look like this:

~/llvm/build/Release+Asserts/bin/clang -O0 -g -S -emit-llvm sample.c -o sample.ll

  • Thanks! Adding -g works. I added first the flag -fstandalone-debug. But, adding -g worked with both -O0 and -O3. Thanks a lot! :) – Shail Dave Jul 12 '16 at 05:52
  • 2
    You definitely don't need -O0. You'll definitely want -g, perhaps even -gcolumn-info if you want column information. -fstandalone-debug only controls whether or not a type is emitted into the debug info - normally we'll optimize out the type if we're not emitting the key function on the idea that wherever we emit a key function for the class we'll make sure the debug info is there. – echristo Jul 12 '16 at 19:45