<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Merging inlined llvm.dbg.value creates broken metadata"
   href="https://bugs.llvm.org/show_bug.cgi?id=36410">36410</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Merging inlined llvm.dbg.value creates broken metadata
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>DebugInfo
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>uweigand@de.ibm.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>aprantl@apple.com, llvm-bugs@lists.llvm.org, vsk@apple.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=19882" name="attach_19882" title="Test case - run with "opt -simplifycfg"">attachment 19882</a> <a href="attachment.cgi?id=19882&action=edit" title="Test case - run with "opt -simplifycfg"">[details]</a></span>
Test case - run with "opt -simplifycfg"

Running the attached test case through "opt -simplifycfg" results in:

mismatched subprogram between llvm.dbg.value variable and !dbg attachment
LLVM ERROR: Broken module found, compilation aborted!

The problem seems to be that (after inlining) the test case contains two
llvm.dbg.value statements that were inlined from a subroutine:

  call void @llvm.dbg.value(metadata i64 %vala, metadata !8, metadata
!DIExpression()), !dbg !12

  call void @llvm.dbg.value(metadata i64 %valb, metadata !8, metadata
!DIExpression()), !dbg !13

!4 = distinct !DISubprogram(name: "callee", scope: !2, file: !2, line: 1, type:
!5, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: false, unit:
!1, variables: !7)      

!10 = distinct !DISubprogram(name: "caller", scope: !2, file: !2, line: 5,
type: !5, isLocal: false, isDefinition: true, scopeLine: 5, isOptimized: false,
unit: !1, variables: !3)
!11 = distinct !DILocation(line: 6, scope: !10)
!12 = !DILocation(line: 2, scope: !4, inlinedAt: !11)
!13 = !DILocation(line: 3, scope: !4, inlinedAt: !11)

These two statements are now being merged as part of a SimplifyCFG operation. 
The resulting llvm.dbg.value instruction gets a new !dbg metadata as part of an
applyMergedLocation call.  This is now:

  call void @llvm.dbg.value(metadata i64 %vala, metadata !7, metadata
!DIExpression()), !dbg !11

!11 = !DILocation(line: 0, scope: !4)

The numbers are off since all the metadata was renamed.  But the important
change is that this is now no longer an "inline" location, but a location that
points directly to some line in "caller".  But the *variable* identified by the
llvm.dbg.value of course remains a local variable of the inlined "callee"
function.

The module verifier now aborts because a llvm.dbg.value statement for a
variable in "callee" has a location in "caller".

Reading the code in applyMergedLocation / getMergedLocation and the comments in 
<a href="https://reviews.llvm.org/D39628">https://reviews.llvm.org/D39628</a>
this behavior seems to have been deliberate?  But at least for llvm.dbg.value
statements this doesn't look appropriate.

I'm not sure exactly how to fix that, so any advice would be appreciated.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>