<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/110313>110313</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [DebugInfo] Redundant `.loc` due to Clang issues in codegen of `for` statement
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang:codegen,
            debuginfo
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
            pogo59
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          pogo59
      </td>
    </tr>
</table>

<pre>
    I'm filing this issue as a follow-up to PR108300. Ordinarily I'd track this as an internal issue within Sony, but as I'm retiring from Sony effective today, I won't have access to my test case or explanation. I do intend to look at this in the next couple of weeks.

Here's my example. It's the simplest form of the `for` statement; more complicated forms might have additional issues, but let's start small.
```
int i = 2;
void test_for() {
#line 300
  for (;          i < 10; ++i)
    i += 2;
}
```
Compile with `-g -gno-column-info -S -emit-llvm` and we see this IR and location metadata:
```
for.cond:                                         ; preds = %for.inc, %entry
  %0 = load i32, ptr @i, align 4, !dbg !18
  %cmp = icmp slt i32 %0, 10, !dbg !18
  br i1 %cmp, label %for.body, label %for.end, !dbg !21

!14 = distinct !DISubprogram(....)
!17 = !DILocation(line: 300, scope: !14)
!18 = !DILocation(line: 300, scope: !19)
!19 = distinct !DILexicalBlock(scope: !20, file: !3, line: 300)
!20 = distinct !DILexicalBlock(scope: !14, file: !3, line: 300)
!21 = !DILocation(line: 300, scope: !20)
```
Observe that the evaluation of the condition uses `!18` as its `DILocation`, while the conditional branch uses `!21`. These have the same source location, but different scopes. When it comes time to emit the `.loc` directives for the machine instructions (in `llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp`), the fact that the branch has a different `DILocation` causes us to generate a new `.loc` directive. The fact that the only difference is the _scope_ (which is irrelevant to `.loc`) is not considered.

Look at the assembly code for the same block:
```
.Ltmp0:
        .loc    0 300 0 prologue_end # tc13837-for.cpp:300:0
        jmp     .LBB0_1
.LBB0_1: # %for.cond
                                        # =>This Inner Loop Header: Depth=1
.Ltmp1:
        .loc    0 300 0 is_stmt 0               # tc13837-for.cpp:300:0
        cmpl    $10, i(%rip)
.Ltmp2:
        .loc    0 300 0 # tc13837-for.cpp:300:0
        jge     .LBB0_4
```
The `.loc` at `.Ltmp1` is emitted because it is at the start of a new basic block. But the `.loc` at `.Ltmp2` is 100% redundant; it's emitted only because of the scope confusion described above.

Arguably we could filter out the redundant `.loc` directives in LLVM, but I claim the root cause is in Clang codegen; see the PR cited above, which fixed the equivalent problem with `if` statements simply by correcting how Clang sets up its notion of `DebugLoc`.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUV91u2zoSfhrmZmBBouS_C1_E8WZPgCxO0Ra7lwFFjiSeUqSWpOLk7RdDSU7ck3Zbo2jsEefv48w3IxGCbi3iga2PjPPBtW69Z5yz9elGjLFz_jDJbmqnXg8PjG97aLTRtoXY6QA6hBFBBBDQOGPceTUOEB18-lzkuzLPM_jTK22F1-YVSF1B9EJ-m7RJz4K2Eb0VZjZ21rHTFr44-8r4HdRjpHOTa49Re3LeeNenI4BNgzLqZ4TolEgqD3B2lvFthE48IwgpMQSKqn-FiCGCFAHBecCXwQgronY2gwdQLsViFZ01zn0DEec0LcQOweJLBOnGwSC4Bs6I30LG8hPLb6f__0CPjG8DecIX0Q8GM3iISUQGgiZRiNA435MJErJN3jjPNjmEKCL2aCMrj9A7jyBdPxgtRUSVdAL0uu2WxJTSFPsCXVjwMji5DFH4CKEXxixhbvL5X_qpbQQNrDwBZ-Vxkj07rRJMTxQV3zG-B7adHzJeGm0Ryny2ABQW0LHyCJcP2byDIich40fGj5rx_aKRnvPjtVu2PX0Y4p3rB22msiCoVi2sWutW0pmxtyttGwerL7DCXseVMc89ASmsgjNCQJzu7-FzEhkn02VDj1EoEQUrbz902jifSWcVK2_hVz-U6-BRhYQn42syoq2kS2F8jTb61wUBxtd5OmacUKBLToeG6IFVuabvwujWQjXpFqp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