<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 - Incorrect remapping of DIArgList for LocalAsMetadata"
   href="https://bugs.llvm.org/show_bug.cgi?id=52552">52552</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Incorrect remapping of DIArgList for LocalAsMetadata
          </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>All
          </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>Transformation Utilities
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>mark.p.mendell@intel.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=25459" name="attach_25459" title="foo.ll - sample file containing dbg.value with DIArgList">attachment 25459</a> <a href="attachment.cgi?id=25459&action=edit" title="foo.ll - sample file containing dbg.value with DIArgList">[details]</a></span>
foo.ll - sample file containing dbg.value with DIArgList

Mapper::mapValue doesn't handle LocalAsMetadata in a DIArgList.  It replaces it
with undef, which is not properly written/read by the BitCode handlers.
This can be shown using llvm-link which causes remapping to be done.
foo.ll contains:
  call void @llvm.dbg.value(metadata !DIArgList(i32 0, i32 %A), metadata !5,
metadata !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_minus,
DW_OP_stack_value)), !dbg !10

If it is linked with foo2.ll (nothing interesting in it), the result is
call void @llvm.dbg.value(metadata !DIArgList(i32 0, i32 undef), metadata !5,
metadata !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_minus,
DW_OP_stack_value)), !dbg !10
If you then try running llvm-dis on the resulting BC file, it will crash
llvm-link -o result.bc foo.ll foo2.ll
llvm-dis result.bc # boom

The contents of foo.ll debug information is mostly garbage, just left over from
a cut down failing test case.


Tested fix:
ValueMapper.cpp ====
***************
*** 400,412 ****
          // be mapped via mapValue (apart from constants when we have no
          // module level changes, which have an identity mapping).
          if ((Flags & RF_NoModuleLevelChanges) &&
isa<ConstantAsMetadata>(VAM)) {
            MappedArgs.push_back(VAM);
          } else if (Value *LV = mapValue(VAM->getValue())) {
            MappedArgs.push_back(
!               LV == VAM->getValue() ? VAM : ValueAsMetadata::get(LV));
          } else {
            // If we cannot map the value, set the argument as undef.
            MappedArgs.push_back(ValueAsMetadata::get(
                UndefValue::get(VAM->getValue()->getType())));
          }
        }
--- 400,414 ----
          // be mapped via mapValue (apart from constants when we have no
          // module level changes, which have an identity mapping).
          if ((Flags & RF_NoModuleLevelChanges) &&
isa<ConstantAsMetadata>(VAM)) {
            MappedArgs.push_back(VAM);
          } else if (Value *LV = mapValue(VAM->getValue())) {
            MappedArgs.push_back(
!               LV == VAM->getValue() ? VAM : ValueAsMetadata::get(LV));  
!         } else if (isa<LocalAsMetadata>(VAM)) {
!           MappedArgs.push_back(VAM);
          } else {
            // If we cannot map the value, set the argument as undef.
            MappedArgs.push_back(ValueAsMetadata::get(
                UndefValue::get(VAM->getValue()->getType())));
          }
        }</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>