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

    <tr>
        <th>Summary</th>
        <td>
            [mlir] MLIR -> LLVM IR debug location mapping fails on name-only locations
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    I ran into an issue where LLVM's verifier was failing saying there was no location information even though I did have that information in MLIR:
```
llvm.dbg.value intrinsic requires a !dbg attachment
  call void @llvm.dbg.value(metadata ptr %1, metadata !79, metadata !DIExpression())
label %3
ptr @predict_dispatch_0_generic_128
```

This op originated from the following MLIR:
```mlir
#loc15 = loc("Mul:")
#loc16 = loc("bert/Mul@__inference_predict_6353")
#loc17 = loc("Cast:")
#loc18 = loc("bert/Cast@__inference_predict_6353")
#loc19 = loc("Const:")
#loc20 = loc("bert/Const_1@__inference_predict_6353")
#loc524 = loc(fused[#loc15, #loc16, #loc17, #loc18, #loc19, #loc20])
llvm.intr.dbg.value #di_local_variable = %arg1 : !llvm.ptr<struct<"iree_hal_executable_dispatch_state_v0_t", (i32, i32, i16, i16, i32, i32, i16, i8, i8, ptr<i32>, ptr<ptr<i8>>, ptr<i64>)>> loc(#loc524)
```

The culprit is the recursive location transformation walking NamedLoc children until it bottoms out on an UnknownLoc:
https://github.com/llvm/llvm-project/blob/8ab77a78ba059c0f5f357d7b1062b25b5ab0cb61/mlir/lib/Target/LLVMIR/DebugTranslation.cpp#L300-L301
The result of the translation is then a `@llvm.dbg.value` without any location information and LLVM does not like that.

I'm not quite sure how to map NamedLoc-only locations to DILocation but to prevent compilation errors the export for these debug intrinsics could check to see if location translation produces null locations and drop the op.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVs2O4ygQfhpyKXWEwY6TQw6znYkUKb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