<div dir="ltr"><div>Hi Eric, thanks for the quick answer. Which version are you using?.</div><div>Here we tried it with (both MacOS):<br><div><br><div><span style="font-size:13px">Apple LLVM version 6.1.0 (clang-602.0.49</span><span style="font-size:13px">) (based on LLVM 3.6.0svn)</span><br style="font-size:13px"><span style="font-size:13px">clang version 3.5.1 (tags/RELEASE_3</span><span style="font-size:13px">51/final)</span></div><div><br></div></div></div><div>Best regards.</div></div><div class="gmail_extra"><br><div class="gmail_quote">2015-04-23 11:20 GMT+10:00 Eric Christopher <span dir="ltr"><<a href="mailto:echristo@gmail.com" target="_blank">echristo@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Try upgrading :)<br><br><div><div>dzur:~/tmp> ~/builds/build-llvm/Debug+Asserts/bin/clang -g -S -emit-llvm -o - foo.c | grep "\!22"</div><div>  call void @llvm.dbg.declare(metadata i32* %f, metadata !21, metadata !13), !dbg !22</div><div>  store i32 %add, i32* %f, align 4, !dbg !22</div><div>!22 = !MDLocation(line: 5, column: 12, scope: !4)</div></div></div><br><div class="gmail_quote"><div><div class="h5">On Wed, Apr 22, 2015 at 6:13 PM Pablo González de Aledo <<a href="mailto:pablo.aledo@gmail.com" target="_blank">pablo.aledo@gmail.com</a>> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><div dir="ltr"><span style="font-size:13px">I am trying to locate instructions in an LLVM Pass by line and column number (reported by an third-party tool) to instrument them. To achieve this, I am compiling my source files with `clang -g -O0 -emit-llvm` and looking for the information in the metadata using this code:</span><br style="font-size:13px"><br style="font-size:13px"><span style="font-size:13px">    const DebugLoc &location = instruction->getDebugLoc();</span><br style="font-size:13px"><span style="font-size:13px">    // location.getLine()</span><br style="font-size:13px"><span style="font-size:13px">    // location.getCol()</span><br style="font-size:13px"><br style="font-size:13px"><span style="font-size:13px">Unfortunately, this information is absolutely imprecise. Consider the following implementation of the Fibonacci function:</span><br style="font-size:13px"><br style="font-size:13px"><span style="font-size:13px">    unsigned fib(unsigned n) {</span><br style="font-size:13px"><span style="font-size:13px">        if (n < 2)</span><br style="font-size:13px"><span style="font-size:13px">            return n;</span><br style="font-size:13px"><br style="font-size:13px"><span style="font-size:13px">        unsigned f = fib(n - 1) + fib(n - 2);</span><br style="font-size:13px"><span style="font-size:13px">        return f;</span><br style="font-size:13px"><span style="font-size:13px">    }</span><br style="font-size:13px"><br style="font-size:13px"><span style="font-size:13px">I would like to locate the single LLVM instruction corresponding to the assignment `unsigned f = ...` in the resulting LLVM IR. I am not interested in all the calculations of the right-hand side. The generated LLVM block including relevant debug metadata is:</span><br style="font-size:13px"><br style="font-size:13px"><span style="font-size:13px">    [...]</span><br style="font-size:13px"><br style="font-size:13px"><span style="font-size:13px">    if.end:                                           ; preds = %entry</span><br style="font-size:13px"><span style="font-size:13px">      call void @llvm.dbg.declare(metadata !{i32* %f}, metadata !17), !dbg !18</span><br style="font-size:13px"><span style="font-size:13px">      %2 = load i32* %n.addr, align 4, !dbg !19</span><br style="font-size:13px"><span style="font-size:13px">      %sub = sub i32 %2, 1, !dbg !19</span><br style="font-size:13px"><span style="font-size:13px">      %call = call i32 @fib(i32 %sub), !dbg !19</span><br style="font-size:13px"><span style="font-size:13px">      %3 = load i32* %n.addr, align 4, !dbg !20</span><br style="font-size:13px"><span style="font-size:13px">      %sub1 = sub i32 %3, 2, !dbg !20</span><br style="font-size:13px"><span style="font-size:13px">      %call2 = call i32 @fib(i32 %sub1), !dbg !20</span><br style="font-size:13px"><span style="font-size:13px">      %add = add i32 %call, %call2, !dbg !20</span><br style="font-size:13px"><span style="font-size:13px">      store i32 %add, i32* %f, align 4, !dbg !20</span><br style="font-size:13px"><span style="font-size:13px">      %4 = load i32* %f, align 4, !dbg !21</span><br style="font-size:13px"><span style="font-size:13px">      store i32 %4, i32* %retval, !dbg !21</span><br style="font-size:13px"><span style="font-size:13px">      br label %return, !dbg !21</span><br style="font-size:13px"><br style="font-size:13px"><span style="font-size:13px">    [...]</span><br style="font-size:13px"><br style="font-size:13px"><span style="font-size:13px">    !17 = metadata !{i32 786688, metadata !4, metadata !"f", metadata !5, i32 5, metadata !8, i32 0, i32 0} ; [ DW_TAG_auto_variable ] [f] [line 5]</span><br style="font-size:13px"><span style="font-size:13px">    !18 = metadata !{i32 5, i32 11, metadata !4, null}</span><br style="font-size:13px"><span style="font-size:13px">    !19 = metadata !{i32 5, i32 15, metadata !4, null}</span><br style="font-size:13px"><span style="font-size:13px">    !20 = metadata !{i32 5, i32 28, metadata !4, null}</span><br style="font-size:13px"><span style="font-size:13px">    !21 = metadata !{i32 6, i32 2, metadata !4, null}</span><br style="font-size:13px"><span style="font-size:13px">    !22 = metadata !{i32 7, i32 1, metadata !4, null}</span><br style="font-size:13px"><br style="font-size:13px"><span style="font-size:13px">As you can see, the metadata `!dbg !20` of the `store` instruction points to **line 5 column 28**, which is the call to `fib(n - 2)`. Even worse, the add operation and the subtraction `n - 2` both also point to that function call, identified by `!dbg !20`.</span><br style="font-size:13px"><br style="font-size:13px"><span style="font-size:13px">Interestingly, the Clang AST emitted by `clang -Xclang -ast-dump -fsyntax-only` has all that information. Thus, I suspect that it is somehow lost during the code generation phase. It seems that during code generation Clang reaches some internal sequence point and associates all following instructions to that position until the next sequence point (e.g. function call) occurs. For completeness, here is the declaration statement in the AST:</span><br style="font-size:13px"><br style="font-size:13px"><span style="font-size:13px">    |-DeclStmt 0x7ffec3869f48 <line:5:2, col:38></span><br style="font-size:13px"><span style="font-size:13px">    | `-VarDecl 0x7ffec382d680 <col:2, col:37> col:11 used f 'unsigned int' cinit</span><br style="font-size:13px"><span style="font-size:13px">    |   `-BinaryOperator 0x7ffec3869f20 <col:15, col:37> 'unsigned int' '+'</span><br style="font-size:13px"><span style="font-size:13px">    |     |-CallExpr 0x7ffec382d7e0 <col:15, col:24> 'unsigned int'</span><br style="font-size:13px"><span style="font-size:13px">    |     | |-ImplicitCastExpr 0x7ffec382d7c8 <col:15> 'unsigned int (*)(unsigned int)' <FunctionToPointerDecay></span><br style="font-size:13px"><span style="font-size:13px">    |     | | `-DeclRefExpr 0x7ffec382d6d8 <col:15> 'unsigned int (unsigned int)' Function 0x7ffec382d490 'fib' 'unsigned int (unsigned int)'</span><br style="font-size:13px"><span style="font-size:13px">    |     | `-BinaryOperator 0x7ffec382d778 <col:19, col:23> 'unsigned int' '-'</span><br style="font-size:13px"><span style="font-size:13px">    |     |   |-ImplicitCastExpr 0x7ffec382d748 <col:19> 'unsigned int' <LValueToRValue></span><br style="font-size:13px"><span style="font-size:13px">    |     |   | `-DeclRefExpr 0x7ffec382d700 <col:19> 'unsigned int' lvalue ParmVar 0x7ffec382d3d0 'n' 'unsigned int'</span><br style="font-size:13px"><span style="font-size:13px">    |     |   `-ImplicitCastExpr 0x7ffec382d760 <col:23> 'unsigned int' <IntegralCast></span><br style="font-size:13px"><span style="font-size:13px">    |     |     `-IntegerLiteral 0x7ffec382d728 <col:23> 'int' 1</span><br style="font-size:13px"><span style="font-size:13px">    |     `-CallExpr 0x7ffec3869ef0 <col:28, col:37> 'unsigned int'</span><br style="font-size:13px"><span style="font-size:13px">    |       |-ImplicitCastExpr 0x7ffec3869ed8 <col:28> 'unsigned int (*)(unsigned int)' <FunctionToPointerDecay></span><br style="font-size:13px"><span style="font-size:13px">    |       | `-DeclRefExpr 0x7ffec3869e10 <col:28> 'unsigned int (unsigned int)' Function 0x7ffec382d490 'fib' 'unsigned int (unsigned int)'</span><br style="font-size:13px"><span style="font-size:13px">    |       `-BinaryOperator 0x7ffec3869eb0 <col:32, col:36> 'unsigned int' '-'</span><br style="font-size:13px"><span style="font-size:13px">    |         |-ImplicitCastExpr 0x7ffec3869e80 <col:32> 'unsigned int' <LValueToRValue></span><br style="font-size:13px"><span style="font-size:13px">    |         | `-DeclRefExpr 0x7ffec3869e38 <col:32> 'unsigned int' lvalue ParmVar 0x7ffec382d3d0 'n' 'unsigned int'</span><br style="font-size:13px"><span style="font-size:13px">    |         `-ImplicitCastExpr 0x7ffec3869e98 <col:36> 'unsigned int' <IntegralCast></span><br style="font-size:13px"><span style="font-size:13px">    |           `-IntegerLiteral 0x7ffec3869e60 <col:36> 'int' 2</span><br style="font-size:13px"><br style="font-size:13px"><span style="font-size:13px">Is it either possible to improve the accuracy of the debug metadata, or resolve the corresponding instruction in a different way? Ideally, I would like to leave Clang untouched, i.e. not modify and recompile it.</span><br>
</div></div></div>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
</blockquote></div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><font size="1" color="#999999"><div><font face="'courier new', monospace">_                                      =   (</font></div><div><font face="'courier new', monospace">                                        255,</font></div><div><font face="'courier new', monospace">                                      lambda</font></div><div><font face="'courier new', monospace">                               V       ,B,c</font></div><div><font face="'courier new', monospace">                             :c   and Y(V*V+B,B,  c</font></div><div><font face="'courier new', monospace">                               -1)if(abs(V)<6)else</font></div><div><font face="'courier new', monospace">               (              2+c-4*abs(V)**-0.4)/i</font></div><div><font face="'courier new', monospace">                 )  ;v,      x=1500,1000;C=range(v*x</font></div><div><font face="'courier new', monospace">                  );import  struct;P=struct.pack;M,\</font></div><div><font face="'courier new', monospace">            j  ='<QIIHHHH',open('M.bmp','wb').write</font></div><div><font face="'courier new', monospace">for X in j('BM'+P(M,v*x*3+26,26,12,v,x,1,24))or C:</font></div><div><font face="'courier new', monospace">            i  ,Y=_;j(P('BBB',*(lambda T:(T*80+T**9</font></div><div><font face="'courier new', monospace">                  *i-950*T  **99,T*70-880*T**18+701*</font></div><div><font face="'courier new', monospace">                 T  **9     ,T*i**(1-T**45*2)))(sum(</font></div><div><font face="'courier new', monospace">               [              Y(0,(A%3/3.+X%v+(X/v+</font></div><div><font face="'courier new', monospace">                               A/3/3.-x/2)/1j)*2.5</font></div><div><font face="'courier new', monospace">                             /x   -2.7,i)**2 for  \</font></div><div><font face="'courier new', monospace">                               A       in C</font></div><div><font face="'courier new', monospace">                                      [:9]])</font></div><div><font face="'courier new', monospace">                                        /9)</font></div><div><font face="'courier new', monospace">                                       )   )</font></div><div><font face="'courier new', monospace">-. .----------------------------------------------------</font></div><div><font face="'courier new', monospace">  Y</font></div><div><font face="'courier new', monospace">  ,,  ,---,</font></div><div><font face="'courier new', monospace"> (_,\/_\_/_\</font></div><div><font face="'courier new', monospace">   \.\_/_\_/></font></div><div><font face="'courier new', monospace">   '-'   '-'</font></div></font></div></div>
</div>