<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 12 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
pre
        {mso-style-priority:99;
        mso-style-link:"HTML Preformatted Char";
        margin:0in;
        margin-bottom:.0001pt;
        font-size:10.0pt;
        font-family:"Courier New";}
span.EmailStyle17
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
span.HTMLPreformattedChar
        {mso-style-name:"HTML Preformatted Char";
        mso-style-priority:99;
        mso-style-link:"HTML Preformatted";
        font-family:"Courier New";}
.MsoChpDefault
        {mso-style-type:export-only;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">The original test case was the following:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">/////<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">int foo(unsigned x, unsigned y, int *z, int *h) {<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">  int Result = 0;<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">  for (unsigned i = 0; i < x; i++) {    // line 3<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">    int Val = 0;<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">    for (unsigned j = 0; j < y; ++j) {  // line 5<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">      if (Val == z[j]) {                // line 6<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">        Val += 42;                      // line 7<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">        break;<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">      }<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">    }<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">    Result += Val;                      // line 9<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">  }<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New""><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">  return Result;<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">}<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">/////<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Quoting Andrea’s investigation:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<pre><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">At –O2, the inner loop at line 5 is rotated. </span><span style="font-size:11.0pt;font-family:"Calibri","sans-serif"">The comparison `j < y` at iteration 0 of the inner loop becomes a comparison between 'y' and zero.<o:p></o:p></span></pre>
<pre><span style="font-size:11.0pt;font-family:"Calibri","sans-serif"">Since 'y' is a loop invariant (for both loops), the compiler moves the icmp between 'y' and zero in<o:p></o:p></span></pre>
<pre><span style="font-size:11.0pt;font-family:"Calibri","sans-serif"">the outer loop preheader. <o:p></o:p></span></pre>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<pre>Later on, CodeGenPrepare decides to sink the icmp within the basic block that uses it. So, we end<o:p></o:p></pre>
<pre>up rematerializing the computation in its original place (after loop rotation).<o:p></o:p></pre>
<pre>However, this time, the rematerialized instruction doesn't seem to be associated with any debug info.<o:p></o:p></pre>
<pre><o:p> </o:p></pre>
<pre>Compiling the test case with –O2 –g –s you see assembly code:<o:p></o:p></pre>
<pre><o:p> </o:p></pre>
<pre>.LBB0_3:                                # %for.cond1.preheader.prol<o:p></o:p></pre>
<pre>        movl    $1, %r8d<o:p></o:p></pre>
<pre>        xorl    %eax, %eax<o:p></o:p></pre>
<pre>        testl   %esi, %esi<o:p></o:p></pre>
<pre>        je      .LBB0_7<o:p></o:p></pre>
<pre><o:p> </o:p></pre>
<pre><o:p> </o:p></pre>
<pre>Instead of:<o:p></o:p></pre>
<pre><o:p> </o:p></pre>
<pre>.LBB0_3:                                # %for.cond1.preheader.prol<o:p></o:p></pre>
<pre>        movl    $1, %r8d<o:p></o:p></pre>
<pre>        xorl    %eax, %eax<o:p></o:p></pre>
<pre>.Ltmp4:<o:p></o:p></pre>
<pre>        .loc    1 5 28 is_stmt 1 discriminator 1 # test.cpp:5:28<o:p></o:p></pre>
<pre>        testl   %esi, %esi<o:p></o:p></pre>
<pre>        je      .LBB0_7<o:p></o:p></pre>
<pre><o:p> </o:p></pre>
<pre><o:p> </o:p></pre>
<pre><o:p> </o:p></pre>
<pre>-- wolfgang<o:p></o:p></pre>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt">
<div>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">From:</span></b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> Dehao Chen [mailto:danielcdh@gmail.com]
<br>
<b>Sent:</b> Thursday, September 15, 2016 3:32 PM<br>
<b>To:</b> David Blaikie<br>
<b>Cc:</b> reviews+D24632+public+5e33fe097ec6bcd7@reviews.llvm.org; Pieb, Wolfgang; aprantl@apple.com; andrea.dibiagio@gmail.com; llvm-commits; Junbum Lim<br>
<b>Subject:</b> Re: [PATCH] D24632: Preserve the debug location when sinking compare instructions<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">For AutoFDO, we want to remove(or set 0-line) debug info for the instructions that could be executed speculatively (i.e. instruction gets more count that it should). For this patch, it's more like code cloning without speculation. Basically
 the cloned code will split the profile counts among different copies, so its effect will be: make the profiled count smaller than it should have. When we post-process samples, we intend to use the max_count we get from all cloned instructions (well, technically
 not exactly "max", there is some voting to prevent from speculated case). As a result, we don't want to lose debug info for clones (as soon as it's not speculated clones) because it gives us better chance to capture the "max".<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">The same applies to debugging. We don't want to see gdb go to speculated source locations as we don't expect it getting executed. But for clones, as soon as it's actually logically executed, we want to have the source location reserved.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">I'm wondering what's the source code that has been affected by this.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Dehao<o:p></o:p></p>
</div>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal">On Thu, Sep 15, 2016 at 2:56 PM, David Blaikie <<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>> wrote:<o:p></o:p></p>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt">This seems like the opposite of the other recent change to remove/zero out the debug location of an instruction being commoned into a preceeding basic block.<br>
<br>
Adding Dehao.<br>
<br>
Wouldn't this hurt profile based optimizations by attributing code passing through
<a href="http://other.bb" target="_blank">other.bb</a> to the entry? Making the Entry seem more common than it is?<o:p></o:p></p>
<div>
<div>
<p class="MsoNormal">On Thu, Sep 15, 2016 at 2:52 PM Wolfgang Pieb <<a href="mailto:wolfgang.pieb@sony.com" target="_blank">wolfgang.pieb@sony.com</a>> wrote:<o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<p class="MsoNormal" style="margin-bottom:12.0pt">wolfgangp created this revision.<br>
wolfgangp added reviewers: aprantl, dblaikie.<br>
wolfgangp added subscribers: llvm-commits, andreadb.<br>
<br>
When the CodeGenPrepare pass sinks a compare instruction into the basic block of a user, it should preserve its debug location. Not doing so negatively affects source line attribution for debugging and AutoFDO.<br>
<br>
Patch by Andrea Di Biagio<br>
<br>
<a href="https://reviews.llvm.org/D24632" target="_blank">https://reviews.llvm.org/D24632</a><br>
<br>
Files:<br>
  lib/CodeGen/CodeGenPrepare.cpp<br>
  test/DebugInfo/Generic/sunk-compare.ll<br>
<br>
Index: test/DebugInfo/Generic/sunk-compare.ll<br>
===================================================================<br>
--- test/DebugInfo/Generic/sunk-compare.ll<br>
+++ test/DebugInfo/Generic/sunk-compare.ll<br>
@@ -0,0 +1,46 @@<br>
+; RUN: opt -S -codegenprepare < %s | FileCheck %s<br>
+;<br>
+; This test case has been generated by hand but is inspired by the<br>
+; observation that compares that are sunk into the basic blocks where<br>
+; their results are used did not retain their debug locs. This caused<br>
+; sample profiling to attribute code to the wrong source lines.<br>
+;<br>
+; We check that the compare instruction retains its debug loc after<br>
+; it is sunk into <a href="http://other.bb" target="_blank">other.bb</a> by the codegen prepare pass.<br>
+;<br>
+; CHECK:       <a href="http://other.bb" target="_blank">other.bb</a>:<br>
+; CHECK-NEXT:  icmp{{.*}}%x, 0, !dbg ![[MDHANDLE:[0-9]*]]<br>
+; CHECK:       ![[MDHANDLE]] = !DILocation(line: 2<br>
+;<br>
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"<br>
+<br>
+define i32 @_Z3fooii(i32 %x, i32 %y) !dbg !5 {<br>
+entry:<br>
+  %cmp17 = icmp sgt i32 %x, 0, !dbg !6<br>
+  br label %<a href="http://other.bb" target="_blank">other.bb</a>, !dbg !6<br>
+<br>
+<a href="http://other.bb" target="_blank">other.bb</a>:<br>
+  br i1 %cmp17, label %<a href="http://exit1.bb" target="_blank">exit1.bb</a>, label %<a href="http://exit2.bb" target="_blank">exit2.bb</a>, !dbg !7<br>
+<br>
+<a href="http://exit1.bb" target="_blank">exit1.bb</a>:<br>
+  %0 = add i32 %y, 42, !dbg !8<br>
+  ret i32 %0, !dbg !8<br>
+<br>
+<a href="http://exit2.bb" target="_blank">exit2.bb</a>:<br>
+  ret i32 44, !dbg !9<br>
+<br>
+}<br>
+<br>
+!<a href="http://llvm.dbg.cu" target="_blank">llvm.dbg.cu</a> = !{!0}<br>
+!llvm.module.flags = !{!3, !4}<br>
+<br>
+!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, globals: !2)<br>
+!1 = !DIFile(filename: "test.cpp", directory: "/debuginfo/bug/cgp")<br>
+!2 = !{}<br>
+!3 = !{i32 2, !"Dwarf Version", i32 4}<br>
+!4 = !{i32 2, !"Debug Info Version", i32 3}<br>
+!5 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: !1, file: !1, line: 1, isLocal: false, isDefinition: true, scopeLine: 1, flags: DIFlagPrototyped, isOptimized: false, unit: !0, variables: !2)<br>
+!6 = !DILocation(line: 2, column: 0, scope: !5)<br>
+!7 = !DILocation(line: 3, column: 0, scope: !5)<br>
+!8 = !DILocation(line: 4, column: 0, scope: !5)<br>
+!9 = !DILocation(line: 5, column: 0, scope: !5)<br>
Index: lib/CodeGen/CodeGenPrepare.cpp<br>
===================================================================<br>
--- lib/CodeGen/CodeGenPrepare.cpp<br>
+++ lib/CodeGen/CodeGenPrepare.cpp<br>
@@ -926,6 +926,8 @@<br>
       InsertedCmp =<br>
           CmpInst::Create(CI->getOpcode(), CI->getPredicate(),<br>
                           CI->getOperand(0), CI->getOperand(1), "", &*InsertPt);<br>
+      // Propagate the debug info.<br>
+      InsertedCmp->setDebugLoc(CI->getDebugLoc());<br>
     }<br>
<br>
     // Replace a use of the cmp with a use of the new cmp.<br>
<br>
<o:p></o:p></p>
</blockquote>
</div>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
</div>
</body>
</html>