<div dir="ltr"><div>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".</div><div><br></div><div>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.</div><div><br></div><div>I'm wondering what's the source code that has been affected by this.</div><div><br></div><div>Dehao</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Sep 15, 2016 at 2:56 PM, David Blaikie <span dir="ltr"><<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">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?<br><br><div class="gmail_quote"><div dir="ltr">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:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">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" rel="noreferrer" target="_blank">https://reviews.llvm.org/<wbr>D24632</a><br>
<br>
Files:<br>
  lib/CodeGen/CodeGenPrepare.cpp<br>
  test/DebugInfo/Generic/sunk-<wbr>compare.ll<br>
<br>
Index: test/DebugInfo/Generic/sunk-<wbr>compare.ll<br>
==============================<wbr>==============================<wbr>=======<br>
--- test/DebugInfo/Generic/sunk-<wbr>compare.ll<br>
+++ test/DebugInfo/Generic/sunk-<wbr>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" rel="noreferrer" target="_blank">other.bb</a> by the codegen prepare pass.<br>
+;<br>
+; CHECK:       <a href="http://other.bb" rel="noreferrer" 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:<wbr>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" rel="noreferrer" target="_blank">other.bb</a>, !dbg !6<br>
+<br>
+<a href="http://other.bb" rel="noreferrer" target="_blank">other.bb</a>:<br>
+  br i1 %cmp17, label %<a href="http://exit1.bb" rel="noreferrer" target="_blank">exit1.bb</a>, label %<a href="http://exit2.bb" rel="noreferrer" target="_blank">exit2.bb</a>, !dbg !7<br>
+<br>
+<a href="http://exit1.bb" rel="noreferrer" 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" rel="noreferrer" target="_blank">exit2.bb</a>:<br>
+  ret i32 44, !dbg !9<br>
+<br>
+}<br>
+<br>
+!<a href="http://llvm.dbg.cu" rel="noreferrer" 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>
==============================<wbr>==============================<wbr>=======<br>
--- lib/CodeGen/CodeGenPrepare.cpp<br>
+++ lib/CodeGen/CodeGenPrepare.cpp<br>
@@ -926,6 +926,8 @@<br>
       InsertedCmp =<br>
           CmpInst::Create(CI-><wbr>getOpcode(), CI->getPredicate(),<br>
                           CI->getOperand(0), CI->getOperand(1), "", &*InsertPt);<br>
+      // Propagate the debug info.<br>
+      InsertedCmp->setDebugLoc(CI-><wbr>getDebugLoc());<br>
     }<br>
<br>
     // Replace a use of the cmp with a use of the new cmp.<br>
<br>
<br>
</blockquote></div></div>
</blockquote></div><br></div>