<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 - Loop Unswitch creates duplicated dbg.declare"
   href="https://bugs.llvm.org/show_bug.cgi?id=33355">33355</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Loop Unswitch creates duplicated dbg.declare
          </td>
        </tr>

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

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

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

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

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

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>Scalar Optimizations
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>mattias.v.eriksson@ericsson.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=18591" name="attach_18591" title="Reproducer for duplicated dbg.declare">attachment 18591</a> <a href="attachment.cgi?id=18591&action=edit" title="Reproducer for duplicated dbg.declare">[details]</a></span>
Reproducer for duplicated dbg.declare

The function f2 has a byval argument that is dead ("dum"). f2 is
inlined into f3 and the dbg.declare is inlined to reflect
that "dum" is "p1" in the inlined code.

Input to 'Function Integration/Inlining':

; Function Attrs: nounwind readonly
define i16 @f2(%struct.S* byval nocapture readnone align 1 %dum)
local_unnamed_addr #0 !dbg !11 {
entry:
  call void @llvm.dbg.declare(metadata %struct.S* %dum, metadata !18, metadata
!19), !dbg !20
  %0 = load i16, i16* @a, align 1, !dbg !21, !tbaa !22
  ret i16 %0, !dbg !26
}


define void @f3(%struct.S* byval align 1 %p1) local_unnamed_addr #2 !dbg !27 {
entry:
  call void @llvm.dbg.declare(metadata %struct.S* %p1, metadata !31, metadata
!19), !dbg !32
  br label %lbl1, !dbg !33

lbl1:                                             ; preds = %lbl1, %entry
  %call = call i16 @f2(%struct.S* byval nonnull align 1 %p1), !dbg !34

After 'Function Integration/Inlining':

; Function Attrs: nounwind
define void @f3(%struct.S* byval align 1 %p1) local_unnamed_addr #2 !dbg !27 {
entry:
  call void @llvm.dbg.declare(metadata %struct.S* %p1, metadata !31, metadata
!19), !dbg !32
  br label %lbl1, !dbg !33

lbl1:                                             ; preds = %lbl1, %entry
  call void @llvm.dbg.declare(metadata %struct.S* %p1, metadata !18, metadata
!19), !dbg !34
  %0 = load i16, i16* @a, align 1, !dbg !36, !tbaa !22
  call void @f1(i16 %0), !dbg !37

Some relevant metadata:
!18 = !DILocalVariable(name: "dum", arg: 1, scope: !11, file: !3, line: 8,
type: !14)
!19 = !DIExpression()
!31 = !DILocalVariable(name: "p1", arg: 1, scope: !27, file: !3, line: 15,
type: !14)

A while later, 'Unswitch loops' duplicates dum's dbg.declare:

define void @f3(%struct.S* byval nocapture readonly align 1 %p1)
local_unnamed_addr #2 !dbg !27 {
...
lbl1.us:                                          ; preds = %lbl1.us,
%entry.split.us
  call void @llvm.dbg.declare(metadata %struct.S* %p1, metadata !18, metadata
!19), !dbg !34
  %1 = load i16, i16* @a, align 1, !dbg !36, !tbaa !22
  call void @f1(i16 %1), !dbg !37
...
lbl1:                                             ; preds = %lbl1, %entry.split
  call void @llvm.dbg.declare(metadata %struct.S* %p1, metadata !18, metadata
!19), !dbg !34
  %2 = load i16, i16* @a, align 1, !dbg !36, !tbaa !22
  call void @f1(i16 %2), !dbg !37

And then in the late stages of llc I get a crash in DwarfDebug
because it expects that there can't be duplicate dbg.declares.
(Same assertion as in <a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Assertion failed: ("conflicting locations for variable")"
   href="show_bug.cgi?id=33157">bug 33157</a>.)

I am a bit confused about how dbg.declare is supposed to work in
this case. The documentation says: "This intrinsic provides
information about a local element (e.g., variable).  The first
argument is metadata holding the alloca for the variable." But
there is no alloca associated with %p1 here. Is this an omission
in the documentation?

Are duplicated dbg.declares allowed? Then the bug is in
DwarfDebug for crashing, not in loop unswitch.

Reproduce like this:
opt -S -inline -functionattrs -licm -loop-unswitch -o - inline.ll | grep
dbg.declare | sort | uniq -c
      1   call void @llvm.dbg.declare(metadata %struct.S* %dum, metadata !18,
metadata !19), !dbg !20
      2   call void @llvm.dbg.declare(metadata %struct.S* %p1, metadata !18,
metadata !19), !dbg !34
      1   call void @llvm.dbg.declare(metadata %struct.S* %p1, metadata !31,
metadata !19), !dbg !32
      1 declare void @llvm.dbg.declare(metadata, metadata, metadata) #1</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>